where to allocate array for particle properties

Submitted by Silias on Thu, 02/23/2012 - 13:31

Hi everybody,

I'd like to allocate memory in a fix for an array to store certain particle properties.

I thought I define the array within the headerfile with following line:

double *particle_prop_;

Then I wanted to allocate memory in the constructor of the fix, like:

int nlocal = atom->nlocal;
particle_prop_ = (double*)memory->create_1d_double_array(nlocal, 0,"particle_prop_");

and later I wanted to use the array in a method for some calculations.

... But the simulation crashes by segmentation faults at some mysterious lines somewhere in the code, sometimes within the variable.cpp and sometimes within the region.cpp.

Now I realised that nlocal is not yet initialized within the constructor of my fix, so the allocation process did not work.
Then, I moved the memory allocation from the constructor part of the fix to the method, where I need the array and wrote

if (Nsteps==0)
{
particle_prop_ = (double*)memory->create_1d_double_array(nlocal, 0,"particle_prop_");
Nsteps++;
}

and initialised Nsteps in the constructor part with 0, so that the allocation-process is envoked only on the first time, the method is called.

But that way didn't work either, now I get the error:
ERROR on proc 0: Failed to allocate -248 bytes for array particle_prop_

What is the problem - how should I proceed?

Many thanks in advance,

kind regards,

Sebastian

Silias | Thu, 02/23/2012 - 13:53

ok,
last error resolved, in

particle_prop_ = (double*)memory->create_1d_double_array(nlocal, 0,"particle_prop_");

I used nlocal and 0 in the wrong order, it should be:

particle_prop_ = (double*)memory->create_1d_double_array(0, nlocal,"particle_prop_");

But again the question:
from the object-oriented point of view, the memory should be allocated in the constructor, but because of the unknown number of particle to the time, the constructor of the fixes is called, its not possible to allocate memory for particle properties in arrays in the constructor, right?

Regards,
Sebastian

ckloss's picture

ckloss | Thu, 02/23/2012 - 17:24

Hi Sebastian,

the best way to do this is to use fix property/atom, where all the functionality that you need is available in encapulated form.
See doc/fix_property/atom.html for doc and e.g. fix_scalar_transportequation.h/cpp in conjunction with fix_heat_gran.h/cpp for how to use it

Cheers, Christoph