How does "memory->create()" work ?

Hi Steve ,

I added two commands , abcd and efg , which are belong to different classes separately , input-command , and modify-fix . A two-dimensional array was defined as public member of abcd’s new class , which is derived from input-command , and I initialize it after allocate memory using “memory->create()” just like :
memory->create(matrix_1, nlocal,1000,“abcd:matrix_1”); // matrix_1 is the array

But when I want to use this array in the other command , efg , by writting some codes in source file like this :
double *matrix_1 = ((Abcd)input)->matrix_1;
though it will be fine with compilng , means I can get the lmp_serial.exe , the program will terminate when “setting up run…” , saying :
Segmentation Fault
I think there are some bugs in my code which probably relates to memory allocation and pointer usage .

Is it possible to ask you for some instructions ?
With my heartfelt gratitude ! Thanks very much .

Hi Steve ,

I added two commands , abcd and efg , which are belong to different classes
separately , input-command , and modify-fix . A two-dimensional array was
defined as public member of abcd's new class , which is derived from
input-command , and I initialize it after allocate memory using
"memory->create()" just like :
memory->create(matrix_1, nlocal,1000,"abcd:matrix_1"); // matrix_1 is the
array

But when I want to use this array in the other command , efg , by writting
some codes in source file like this :
double **matrix_1 = ((Abcd*)input)->matrix_1;
though it will be fine with compilng , means I can get the lmp_serial.exe ,
the program will terminate when "setting up run..." , saying :
Segmentation Fault
I think there are some bugs in my code which probably relates to memory
allocation and pointer usage .

which nobody can debug without seeing the code.
there are a gazillion ways in which you can have screwed up.
the best way to find out is to use a debugger and see
what is wrong exactly.

Is it possible to ask you for some instructions ?

the code for memory->create() is in memory.h
and should be fairly self-explanatory. if you have
problems with that, you probably need to stick
your nose into your c++ books for a bit.

cheers,
    axel.

Thank you Axel, my C++ knowledge is not enough, so more eagerly I want to cite where the problem is and enhance my C++ skill accordingly.

The basic issue is I wrote two commands, parts of their codes I pasted as below:
The first command:
head file:
#ifdef COMMAND_CLASS
CommandStyle(setup,SetUp)
#else
#ifndef LMP_SETUP_H
#define LMP_SETUP_H
#include “pointers.h”
namespace LAMMPS_NS {
class SetUp : protected Pointers {
public:
SetUp(class LAMMPS *);
void command(int, char **);
double **mx,**my,**mz; // Three two-dimensional arrays I want to use in both commands
private:
int xvar;
char *xstr;
};
}
#endif
#endif

source file:
using namespace LAMMPS_NS;
SetUp::SetUp(LAMMPS lmp) : Pointers(lmp) {}
void SetUp::command(int narg, char **arg)
{
double **x = atom->x;
double **f = atom->f;
int mask = atom->mask;
int nlocal = atom->nlocal;
__memory->create(mx,2
nlocal,1000,“setup:mx”);__
__memory->create(my,2
nlocal,1000,"__setup**:my");**
memory->create(mz,2*nlocal,1000,"setup**:mz");** // I thought this would allocate memory for them.
for (int i = 0; i < nlocal; i++){

} // Then I initialize them by using this loop.
}

The second command: ( which I wrote according to fix_addforce.cpp )
source file:

void FixAddMatrix::post_force(int vflag)
{
force_flag = 0;
double **x = atom->x;
double **f = atom->f;
int *mask = atom->mask;
int nlocal = atom->nlocal;

double *mx = ((SetUp)input)->mx;
double *my = ((SetUp)input)->my;
double *mz = ((SetUp)input)->mz; // I want to use and modify the same arrays which were set up and initialized in “setup” command. Maybe you can see my lack of knowledge of pointer usage for I can’t figure out whether it is right to write like this and whether it has relationship with the Segmentation Fault. If you tell me it is wrong, I’ll go back to my C++ books.

if (xstyle == EQUAL) {
modify->clearstep_compute();
ng = input->variable->compute_equal(xvar);
modify->addstep_compute(update->ntimestep + 1);
foriginal[1] = foriginal[2] = foriginal[3] = 0.0;

for (int i = 0; i < nlocal; i++){
if (mask[i] & groupbit) {
if (iregion >= 0 &&
!domain->regions[iregion]->match(x[i][0],x[i][1],x[i][2]))
continue;
foriginal[1] += f[i][0];
foriginal[2] += f[i][1];
foriginal[3] += f[i][2];
}
}

for (int ig = 0; ig < ng; ig++){

} // In this loop I continue to modify those three arrays.
}
}

And in input script, the first command will be written before the second one, fix_addmatrix.
When I debug the executable file “lmp_serial” with my input script, the GDB said:

Program received signal SIGSEGV, Segmentation fault.
0x080e1f4b in LAMMPS_NS::FixAddMatrix::post_force(int) ()
(gdb) where
#0 0x080e1f4b in LAMMPS_NS::FixAddMatrix::post_force(int) ()
#1 0x080e220a in LAMMPS_NS::FixAddMatrix::setup(int) ()
#2 0x0816537c in LAMMPS_NS::Modify::setup(int) ()
#3 0x0823159d in LAMMPS_NS::Verlet::setup() ()
#4 0x08213874 in LAMMPS_NS::Run::command(int, char**) ()
#5 0x08151e18 in LAMMPS_NS::Input::execute_command() ()
#6 0x08152979 in LAMMPS_NS::Input::file() ()
#7 0x0815ad80 in main ()
(gdb) ^CQuit

Is it possible to give me some instructions on where this Segmentation Fault comes from? I could not find anybody around me who know something about C++ and also Lammps to help me, so I turn into Maillist with these so simple questions.
Heartful gratitude!

memory->create(mx,2*nlocal,1000,"setup:mx");

This will allocate a 2d array and store the ptr for it
in mx, wherever you have defined mx.

If you want mx to be permanent, e.g. in some class,
then it can't be in a Command class, unless you are
only going to use it in the Command class. That is b/c
commands you add to LAMMPS via that class (like
the built-in velocity command), only exist while the
one line (e.g. velocity) in the input script is processed
and destroyed right after.

I don't know what you are attempting to do, but
you can't add permanent arrays to LAMMPS that way,
that can be accessed by some other piece of code
you write.

Steve