77 static double m_ = 1.0;
78 static double k_ = 1.0;
97 int main(
int argc,
char *argv[])
100 Mpi::Init(argc, argv);
102 int num_procs = Mpi::WorldSize();
103 int myid = Mpi::WorldRank();
104 MPI_Comm comm = MPI_COMM_WORLD;
110 bool visualization =
true;
111 bool gnuplot =
false;
115 "Time integration order.");
116 args.
AddOption(&prob_,
"-p",
"--problem-type",
118 "\t 0 - Simple Harmonic Oscillator\n"
120 "\t 2 - Gaussian Potential Well\n"
121 "\t 3 - Quartic Potential\n"
122 "\t 4 - Negative Quartic Potential");
123 args.
AddOption(&nsteps,
"-n",
"--number-of-steps",
124 "Number of time steps.");
125 args.
AddOption(&dt,
"-dt",
"--time-step",
129 args.
AddOption(&k_,
"-k",
"--spring-const",
131 args.
AddOption(&visualization,
"-vis",
"--visualization",
"-no-vis",
132 "--no-visualization",
133 "Enable or disable GLVis visualization.");
134 args.
AddOption(&gnuplot,
"-gp",
"--gnuplot",
"-no-gp",
"--no-gnuplot",
135 "Enable or disable GnuPlot visualization.");
160 q(0) =
sin(2.0*M_PI*(
double)myid/num_procs);
161 p(0) =
cos(2.0*M_PI*(
double)myid/num_procs);
168 oss <<
"ex20p_" << setfill(
'0') << setw(5) << myid <<
".dat";
169 ofs.open(oss.str().c_str());
170 ofs << t <<
"\t" << q(0) <<
"\t" <<
p(0) << endl;
174 int nverts = (visualization) ? 2*num_procs*(nsteps+1) : 0;
175 int nelems = (visualization) ? (nsteps * num_procs) : 0;
176 Mesh mesh(2, nverts, nelems, 0, 3);
178 int *part = (visualization) ? (
new int[nelems]) : NULL;
186 for (
int i = 0; i < nsteps; i++)
196 for (
int j = 0; j < num_procs; j++)
208 siaSolver.
Step(q,p,t,dt);
215 ofs << t <<
"\t" << q(0) <<
"\t" <<
p(0) <<
"\t" << e[i+1] << endl;
222 for (
int j = 0; j < num_procs; j++)
229 v[0] = 2 * num_procs * i + 2 * j;
230 v[1] = 2 * num_procs * (i + 1) + 2 * j;
231 v[2] = 2 * num_procs * (i + 1) + 2 * j + 1;
232 v[3] = 2 * num_procs * i + 2 * j + 1;
234 part[num_procs * i + j] = j;
240 e_mean /= (nsteps + 1);
242 for (
int i = 0; i <= nsteps; i++)
244 e_var +=
pow(e[i] - e_mean, 2);
246 e_var /= (nsteps + 1);
247 double e_sd =
sqrt(e_var);
251 cout << endl <<
"Mean and standard deviation of the energy" << endl;
253 for (
int i = 0; i < num_procs; i++)
257 cout << myid <<
": " << e_mean <<
"\t" << e_sd << endl;
268 ofs.open(
"gnuplot_ex20p.inp");
269 for (
int i = 0; i < num_procs; i++)
272 ossi <<
"ex20p_" << setfill(
'0') << setw(5) << i <<
".dat";
277 ofs <<
" '" << ossi.str() <<
"' using 1:2 w l t 'q" << i <<
"',"
278 <<
" '" << ossi.str() <<
"' using 1:3 w l t 'p" << i <<
"',"
279 <<
" '" << ossi.str() <<
"' using 1:4 w l t 'H" << i <<
"'";
297 ParMesh pmesh(comm, mesh, part);
304 for (
int i = 0; i <= nsteps; i++)
306 energy[2*i+0] = e[i];
307 energy[2*i+1] = e[i];
314 sock <<
"parallel " << num_procs <<
" " << myid <<
"\n"
315 <<
"solution\n" << pmesh << energy
316 <<
"window_title 'Energy in Phase Space'\n"
317 <<
"keys\n maac\n" <<
"axis_labels 'q' 'p' 't'\n"<< flush;
323 double h = 1.0 - 0.5 / m_ + 0.5 * p * p / m_;
327 h += k_ * (1.0 -
cos(q));
330 h += k_ * (1.0 -
exp(-0.5 * q * q));
333 h += 0.5 * k_ * (1.0 + q * q) * q * q;
336 h += 0.5 * k_ * (1.0 - 0.125 * q * q) * q * q;
339 h += 0.5 * k_ * q * q;
350 y(0) = - k_*
sin(x(0));
353 y(0) = - k_ * x(0) *
exp(-0.5 * x(0) * x(0));
356 y(0) = - k_ * (1.0 + 2.0 * x(0) * x(0)) * x(0);
359 y(0) = - k_ * (1.0 - 0.25 * x(0) * x(0)) * x(0);
double hamiltonian(double q, double p, double t)
virtual void Init(Operator &P, TimeDependentOperator &F)
int AddQuad(int v1, int v2, int v3, int v4, int attr=1)
Base abstract class for first order time dependent operators.
void Mult(const Table &A, const Table &B, Table &C)
C = A * B (as boolean matrices)
Abstract parallel finite element space.
FDualNumber< tbase > exp(const FDualNumber< tbase > &f)
exp([dual number])
void Step(Vector &q, Vector &p, double &t, double &dt) override
int AddVertex(double x, double y=0.0, double z=0.0)
void Parse()
Parse the command-line options. Note that this function expects all the options provided through the ...
void PrintUsage(std::ostream &out) const
Print the usage message.
FDualNumber< tbase > cos(const FDualNumber< tbase > &f)
cos([dual number])
FDualNumber< tbase > pow(const FDualNumber< tbase > &a, const FDualNumber< tbase > &b)
pow([dual number],[dual number])
double p(const Vector &x, double t)
FDualNumber< tbase > sin(const FDualNumber< tbase > &f)
sin([dual number])
void AddOption(bool *var, const char *enable_short_name, const char *enable_long_name, const char *disable_short_name, const char *disable_long_name, const char *description, bool required=false)
Add a boolean option and set 'var' to receive the value. Enable/disable tags are used to set the bool...
Vector & Set(const double a, const Vector &x)
(*this) = a * x
void FinalizeQuadMesh(int generate_edges=0, int refine=0, bool fix_orientation=true)
Finalize the construction of a quadrilateral Mesh.
FDualNumber< tbase > sqrt(const FDualNumber< tbase > &f)
sqrt([dual number])
void PrintOptions(std::ostream &out) const
Print the options.
Arbitrary order H1-conforming (continuous) finite elements.
Class for parallel grid function.
Class for parallel meshes.
Variable order Symplectic Integration Algorithm (orders 1-4)
bool Good() const
Return true if the command line options were parsed successfully.