我有一个程序,一般解决一维布朗运动使用欧拉的方法。作为一个随机过程,我想把它平均在许多粒子上。但是我发现当我增加粒子的数量时,它会超载,得到std::badalloc错误,我知道这是一个内存错误。
这是我的完整代码
#include <iostream>
#include <vector>
#include <fstream>
#include <cmath>
#include <cstdlib>
#include <limits>
#include <ctime>
using namespace std;
// Box-Muller Method to generate gaussian numbers
double generateGaussianNoise(double mu, double sigma) {
const double epsilon = std::numeric_limits<double>::min();
const double tau = 2.0 * 3.14159265358979323846;
static double z0, z1;
static bool generate;
generate = !generate;
if (!generate) return z1 * sigma + mu;
double u1, u2;
do {
u1 = rand() * (1.0 / RAND_MAX);
u2 = rand() * (1.0 / RAND_MAX);
} while (u1 <= epsilon);
z0 = sqrt(-2.0 * log(u1)) * cos(tau * u2);
z1 = sqrt(-2.0 * log(u1)) * sin(tau * u2);
return z0 * sigma + mu;
}
int main() {
// Initialize Variables
double gg; // Gaussian Number Picked from distribution
// Integrator
double t0 = 0; // Setting the Time Window
double tf = 10;
double n = 5000; // Number of Steps
double h = (tf - t0) / n; // Time Step Size
// Set Constants
const double pii = atan(1) * 4; // pi
const double eta = 1; // viscous constant
const double m = 1; // mass
const double aa = 1; // radius
const double Temp = 30; // Temperature in Kelvins
const double KB = 1; // Boltzmann Constant
const double alpha = (6 * pii * eta * aa);
// More Constants
const double mu = 0; // Gaussian Mean
const double sigma = 1; // Gaussian Std Deviation
const double ng = n; // No. of pts to generate for Gauss distribution
const double npart = 1000; // No. of Particles
// Initial Conditions
double x0 = 0;
double y0 = 0;
double t = t0;
// Vectors
vector<double> storX; // Vector that keeps displacement values
vector<double> storY; // Vector that keeps velocity values
vector<double> storT; // Vector to store time
vector<double> storeGaussian; // Vector to store Gaussian numbers generated
vector<double> holder; // Placeholder Vector for calculation operations
vector<double> mainstore; // Vector that holds the final value desired
storT.push_back(t0);
// Prepares mainstore
for (int z = 0; z < (n+1); z++) {
mainstore.push_back(0);
}
for (int NN = 0; NN < npart; NN++) {
holder.clear();
storX.clear();
storY.clear();
storT.clear();
storT.push_back(0);
// Prepares holder
for (int z = 0; z < (n+1); z++) {
holder.push_back(0);
storX.push_back(0);
storY.push_back(0);
}
// Gaussian Generator
srand(time(NULL));
for (double iiii = 0; iiii < ng; iiii++) {
gg = generateGaussianNoise(0, 1); // generateGaussianNoise(mu,sigma)
storeGaussian.push_back(gg);
}
// Solver
for (int ii = 0; ii < n; ii++) {
storY[ii + 1] =
storY[ii] - (alpha / m) * storY[ii] * h +
(sqrt(2 * alpha * KB * Temp) / m) * sqrt(h) * storeGaussian[ii];
storX[ii + 1] = storX[ii] + storY[ii] * h;
holder[ii + 1] =
pow(storX[ii + 1], 2); // Finds the displacement squared
t = t + h;
storT.push_back(t);
}
// Updates the Main Storage
for (int z = 0; z < storX.size(); z++) {
mainstore[z] = mainstore[z] + holder[z];
}
}
// Average over the number of particles
for (int z = 0; z < storX.size(); z++) {
mainstore[z] = mainstore[z] / (npart);
}
// Outputs the data
ofstream fout("LangevinEulerTest.txt");
for (int jj = 0; jj < storX.size(); jj++) {
fout << storT[jj] << '\t' << mainstore[jj] << '\t' << storX[jj] << endl;
}
return 0;
}正如你所看到的,npart是我改变以改变粒子数量的变量。但是每次迭代之后,我确实清除了我的存储向量,比如storX、storY.。所以在纸上,粒子的数量应该不会影响记忆?我只是调用编译器多次重复,然后添加到主存储向量mainstore中。我正在一台4GB内存的计算机上运行我的代码。
如果有人能指出我在逻辑上的错误或提出改进建议,我会非常感激的。
编辑:当前粒子的数量被设置为npart = 1000。因此,当我试图将它提升到喜欢npart = 20000或npart = 50000时,它会给我内存错误。
Edit2我已经编辑了代码,以便为每个存储向量分配额外的索引。但是它似乎没有修复内存溢出。
发布于 2015-09-17 09:21:25
在求解器部分有一个超出界限的异常。storY的大小为n,您可以访问ii+1,在这里,我将访问n-1。所以你的代码提供了。storY的尺寸是5000。允许使用0到4999之间的索引进行访问(包括),但尝试使用索引5000进行访问。storX、holder和mainstore也是如此。
另外,在添加新变量之前,storeGaussian不会被清除。它对每一个半部分循环增长n。无论如何,您只访问求解器部分中它的第一个n值。
发布于 2015-09-17 09:23:03
请注意,vector::clear从向量中移除所有元素,但不一定改变向量的容量(即它的存储数组),请参见文档。
这不会在这里造成问题,因为您将在下一次运行中重用相同的数组,但是在使用向量时要注意这一点。
https://stackoverflow.com/questions/32626502
复制相似问题