So I am just working on a MD program but either way I am having a not so fun time making it work (properly).
So it starts out normally and I call in several modules.
<MY CODE>
int main() {
// Initializsierung entsprechend setup.h - Initializing acorrding to setup.h
int npart = npart_def;
double tnet = tnet_def;
double dt = dt_def;
</MY CODE>
So the values npart_def etc. come from the setup.h and they are defined properly and work, no issues here..
Then these variables are passed to the another module, from the main routine it looks like..
<MY CODE>
Getinitials(npart, tnet, dt);
</MY CODE>
Then in the Getinitials function it looks like..
<MY CODE>
// Subroutines
template<typename T> void query(char text[],T& input, T min,T max) {
do {
cout<<"# "<<text<<" <"<<input<<"> : ";
if (cin.peek()!='\n') {cin>>input;}
while(getchar()!='\n'); //clear buffer
} while (!((input>=min) && (input<=max)));
}
// Mainroutine
void Getinitials(int& npart, double& tnet, double& dt)
{
makechanges: // Label fur wiederholtes Einlesen (Korrektur)
// Einlesen der Systemparameter
query("Number of Particles ", npart, 1, 1000000);
query("Run Time ", tnet, 0.1, MAX_tnet);
query("Time Steps ", dt, 0.0, tnet/1000.0);
// Kontrollausgabe der eingegebenen Werte
cout<<"#"<<endl;
cout<<"# N = "<<npart<<endl;
cout<<"# tnet = "<<tnet<<endl;
cout<<"# dt= "<<dt<<endl;
cout<<"#"<<endl;
// Start oder Korrenktur?
for(char ch; {
cout<<"# [A]ccept [D]ecline <A> : "; cin.get(ch);
if (ch=='\n') break;
else while(getchar()!='\n');
if (ch=='A' || ch=='a') break;
else if (ch=='D' || ch=='d') {goto makechanges;} // Anderungen
}
}
</MY CODE>
and this works fine also.. then in the main routine (**Placeholder**)
<MY CODE>
// Set sizes of dynamic arrays based on "Get initials"
x = new double[npart];
y = new double[npart];
z = new double[npart];
v_x = new double[npart];
v_y = new double[npart];
v_z = new double[npart];
/*dxdt = new double[npart];
dydt = new double[npart];
dzdt = new double[npart];*/
dv_xdt = new double[npart];
dv_ydt = new double[npart];
dv_zdt = new double[npart];
cout<<"Energies"<<endl;
A = 0.0;
U = 0.0;
K = new double[npart];
//E = new double[npart];
N = 0.0;
// Sets the n-particles at their respective origins
// However for simplification I am setting both to a/2
cout<<"Coords"<<endl;
x[1]=-0.5;
x[2]= 0.5;
y[1]= 0;
y[2]= 0;
z[1]= 0;
z[2]= 0;
Initout(npart);
</MY CODE>
And Initout function looks like
<MY CODE>
void Initout(int npart)
{
// Header format
cout<<"Opening file"<<endl;
ofstream handler("log.dat", ios:ut); // Offnen/Opens after making the data file
cout<<"Opened"<<endl;
handler<<"# MD -- VERSION: "<<VERSION<<endl;
cout<<"Printing N"<<endl;
handler<<"# N = "<<npart<<endl;
// Creates columns
cout<<"Headers"<<endl;
handler<<"#";
for (int k=1; k<=npart; k++) {
handler<<"\t\t n = "<<k<<"\t\t\t\t";
}
handler<<endl<<"#";
for (int k=1; k<=npart; k++) {
handler<<" x\t\t y\t\t z\t\tKinetic\t\t";
}
handler<<"External, Potential, Total Energy";
handler<<endl;
handler.close();
}
</MY CODE>
What happens though, is when the Getinitials is called, if I leave all the values and just hit enter or enter the same number, the program runs fine. However, if I change any of the numbers (I don't change npart, because it isn't really designed to be changed yet) then I get a segmentation fault when it tries to open the log.dat file.. So it seems weird that it only happens when I change the values otherwise it works, baffling..
Also, if I change the Initout function to be where I said **Placeholder** then I don't get a segmentation fault, but later on when I print values they all show up as nan.
Any help is appreciated... I am no professional programmer, and C++ was never my game.. I shoulda been a plumber :loo:
So it starts out normally and I call in several modules.
<MY CODE>
int main() {
// Initializsierung entsprechend setup.h - Initializing acorrding to setup.h
int npart = npart_def;
double tnet = tnet_def;
double dt = dt_def;
</MY CODE>
So the values npart_def etc. come from the setup.h and they are defined properly and work, no issues here..
Then these variables are passed to the another module, from the main routine it looks like..
<MY CODE>
Getinitials(npart, tnet, dt);
</MY CODE>
Then in the Getinitials function it looks like..
<MY CODE>
// Subroutines
template<typename T> void query(char text[],T& input, T min,T max) {
do {
cout<<"# "<<text<<" <"<<input<<"> : ";
if (cin.peek()!='\n') {cin>>input;}
while(getchar()!='\n'); //clear buffer
} while (!((input>=min) && (input<=max)));
}
// Mainroutine
void Getinitials(int& npart, double& tnet, double& dt)
{
makechanges: // Label fur wiederholtes Einlesen (Korrektur)
// Einlesen der Systemparameter
query("Number of Particles ", npart, 1, 1000000);
query("Run Time ", tnet, 0.1, MAX_tnet);
query("Time Steps ", dt, 0.0, tnet/1000.0);
// Kontrollausgabe der eingegebenen Werte
cout<<"#"<<endl;
cout<<"# N = "<<npart<<endl;
cout<<"# tnet = "<<tnet<<endl;
cout<<"# dt= "<<dt<<endl;
cout<<"#"<<endl;
// Start oder Korrenktur?
for(char ch; {
cout<<"# [A]ccept [D]ecline <A> : "; cin.get(ch);
if (ch=='\n') break;
else while(getchar()!='\n');
if (ch=='A' || ch=='a') break;
else if (ch=='D' || ch=='d') {goto makechanges;} // Anderungen
}
}
</MY CODE>
and this works fine also.. then in the main routine (**Placeholder**)
<MY CODE>
// Set sizes of dynamic arrays based on "Get initials"
x = new double[npart];
y = new double[npart];
z = new double[npart];
v_x = new double[npart];
v_y = new double[npart];
v_z = new double[npart];
/*dxdt = new double[npart];
dydt = new double[npart];
dzdt = new double[npart];*/
dv_xdt = new double[npart];
dv_ydt = new double[npart];
dv_zdt = new double[npart];
cout<<"Energies"<<endl;
A = 0.0;
U = 0.0;
K = new double[npart];
//E = new double[npart];
N = 0.0;
// Sets the n-particles at their respective origins
// However for simplification I am setting both to a/2
cout<<"Coords"<<endl;
x[1]=-0.5;
x[2]= 0.5;
y[1]= 0;
y[2]= 0;
z[1]= 0;
z[2]= 0;
Initout(npart);
</MY CODE>
And Initout function looks like
<MY CODE>
void Initout(int npart)
{
// Header format
cout<<"Opening file"<<endl;
ofstream handler("log.dat", ios:ut); // Offnen/Opens after making the data file
cout<<"Opened"<<endl;
handler<<"# MD -- VERSION: "<<VERSION<<endl;
cout<<"Printing N"<<endl;
handler<<"# N = "<<npart<<endl;
// Creates columns
cout<<"Headers"<<endl;
handler<<"#";
for (int k=1; k<=npart; k++) {
handler<<"\t\t n = "<<k<<"\t\t\t\t";
}
handler<<endl<<"#";
for (int k=1; k<=npart; k++) {
handler<<" x\t\t y\t\t z\t\tKinetic\t\t";
}
handler<<"External, Potential, Total Energy";
handler<<endl;
handler.close();
}
</MY CODE>
What happens though, is when the Getinitials is called, if I leave all the values and just hit enter or enter the same number, the program runs fine. However, if I change any of the numbers (I don't change npart, because it isn't really designed to be changed yet) then I get a segmentation fault when it tries to open the log.dat file.. So it seems weird that it only happens when I change the values otherwise it works, baffling..
Also, if I change the Initout function to be where I said **Placeholder** then I don't get a segmentation fault, but later on when I print values they all show up as nan.
Any help is appreciated... I am no professional programmer, and C++ was never my game.. I shoulda been a plumber :loo: