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,2nlocal,1000,“setup:mx”);__
__memory->create(my,2nlocal,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!