45 class ReducedSystemOperator;
74 ReducedSystemOperator *reduced_oper;
88 double visc,
double mu,
double K);
94 virtual void ImplicitSolve(
const double dt,
const Vector &x,
Vector &k);
101 virtual ~HyperelasticOperator();
108 class ReducedSystemOperator :
public Operator
124 void SetParameters(
double dt_,
const Vector *v_,
const Vector *x_);
132 virtual ~ReducedSystemOperator();
138 class ElasticEnergyCoefficient :
public Coefficient
147 : model(m), x(x_) { }
149 virtual ~ElasticEnergyCoefficient() { }
158 bool init_vis =
false);
161 int main(
int argc,
char *argv[])
165 MPI_Init(&argc, &argv);
166 MPI_Comm_size(MPI_COMM_WORLD, &num_procs);
167 MPI_Comm_rank(MPI_COMM_WORLD, &myid);
170 const char *mesh_file =
"../data/beam-quad.mesh";
171 int ser_ref_levels = 2;
172 int par_ref_levels = 0;
174 int ode_solver_type = 3;
175 double t_final = 300.0;
180 bool visualization =
true;
184 args.
AddOption(&mesh_file,
"-m",
"--mesh",
185 "Mesh file to use.");
186 args.
AddOption(&ser_ref_levels,
"-rs",
"--refine-serial",
187 "Number of times to refine the mesh uniformly in serial.");
188 args.
AddOption(&par_ref_levels,
"-rp",
"--refine-parallel",
189 "Number of times to refine the mesh uniformly in parallel.");
191 "Order (degree) of the finite elements.");
192 args.
AddOption(&ode_solver_type,
"-s",
"--ode-solver",
193 "ODE solver: 1 - Backward Euler, 2 - SDIRK2, 3 - SDIRK3,\n\t"
194 " 11 - Forward Euler, 12 - RK2,\n\t"
195 " 13 - RK3 SSP, 14 - RK4.");
196 args.
AddOption(&t_final,
"-tf",
"--t-final",
197 "Final time; start time is 0.");
198 args.
AddOption(&dt,
"-dt",
"--time-step",
200 args.
AddOption(&visc,
"-v",
"--viscosity",
201 "Viscosity coefficient.");
202 args.
AddOption(&mu,
"-mu",
"--shear-modulus",
203 "Shear modulus in the Neo-Hookean hyperelastic model.");
204 args.
AddOption(&K,
"-K",
"--bulk-modulus",
205 "Bulk modulus in the Neo-Hookean hyperelastic model.");
206 args.
AddOption(&visualization,
"-vis",
"--visualization",
"-no-vis",
207 "--no-visualization",
208 "Enable or disable GLVis visualization.");
209 args.
AddOption(&vis_steps,
"-vs",
"--visualization-steps",
210 "Visualize every n-th timestep.");
229 Mesh *mesh =
new Mesh(mesh_file, 1, 1);
236 switch (ode_solver_type)
244 case 12: ode_solver =
new RK2Solver(0.5);
break;
246 case 14: ode_solver =
new RK4Solver;
break;
255 cout <<
"Unknown ODE solver type: " << ode_solver_type <<
'\n';
265 for (
int lev = 0; lev < ser_ref_levels; lev++)
275 for (
int lev = 0; lev < par_ref_levels; lev++)
292 cout <<
"Number of velocity/deformation unknowns: " << glob_size << endl;
297 true_offset[1] = true_size;
298 true_offset[2] = 2*true_size;
302 v_gf.
MakeTRef(&fespace, vx, true_offset[0]);
303 x_gf.
MakeTRef(&fespace, vx, true_offset[1]);
329 HyperelasticOperator oper(fespace, ess_bdr, visc, mu, K);
334 char vishost[] =
"localhost";
336 vis_v.
open(vishost, visport);
338 visualize(vis_v, pmesh, &x_gf, &v_gf,
"Velocity",
true);
342 vis_w.
open(vishost, visport);
345 oper.GetElasticEnergyDensity(x_gf, w_gf);
347 visualize(vis_w, pmesh, &x_gf, &w_gf,
"Elastic energy density",
true);
351 double ee0 = oper.ElasticEnergy(x_gf);
352 double ke0 = oper.KineticEnergy(v_gf);
355 cout <<
"initial elastic energy (EE) = " << ee0 << endl;
356 cout <<
"initial kinetic energy (KE) = " << ke0 << endl;
357 cout <<
"initial total energy (TE) = " << (ee0 + ke0) << endl;
362 ode_solver->
Init(oper);
366 bool last_step =
false;
367 for (
int ti = 1; !last_step; ti++)
369 double dt_real = min(dt, t_final - t);
371 ode_solver->
Step(vx, t, dt_real);
373 last_step = (t >= t_final - 1e-8*dt);
375 if (last_step || (ti % vis_steps) == 0)
379 double ee = oper.ElasticEnergy(x_gf);
380 double ke = oper.KineticEnergy(v_gf);
384 cout <<
"step " << ti <<
", t = " << t <<
", EE = " << ee
385 <<
", KE = " << ke <<
", ΔTE = " << (ee+ke)-(ee0+ke0) << endl;
393 oper.GetElasticEnergyDensity(x_gf, w_gf);
407 ostringstream mesh_name, velo_name, ee_name;
408 mesh_name <<
"deformed." << setfill(
'0') << setw(6) << myid;
409 velo_name <<
"velocity." << setfill(
'0') << setw(6) << myid;
410 ee_name <<
"elastic_energy." << setfill(
'0') << setw(6) << myid;
412 ofstream mesh_ofs(mesh_name.str().c_str());
413 mesh_ofs.precision(8);
414 pmesh->
Print(mesh_ofs);
416 ofstream velo_ofs(velo_name.str().c_str());
417 velo_ofs.precision(8);
419 ofstream ee_ofs(ee_name.str().c_str());
421 oper.GetElasticEnergyDensity(x_gf, w_gf);
448 out <<
"solution\n" << *mesh << *field;
454 out <<
"window_size 800 800\n";
455 out <<
"window_title '" << field_name <<
"'\n";
462 out <<
"autoscale value\n";
469 ReducedSystemOperator::ReducedSystemOperator(
472 :
Operator(M_->ParFESpace()->TrueVSize()), M(M_), S(S_), H(H_),
473 Jacobian(NULL), dt(0.0), v(NULL), x(NULL), w(height), z(height),
474 ess_tdof_list(ess_tdof_list_)
477 void ReducedSystemOperator::SetParameters(
double dt_,
const Vector *v_,
480 dt = dt_; v = v_; x = x_;
489 M->TrueAddMult(k, y);
490 S->TrueAddMult(w, y);
494 Operator &ReducedSystemOperator::GetGradient(
const Vector &k)
const
500 localJ->
Add(dt*dt, H->GetLocalGradient(z));
501 Jacobian = M->ParallelAssemble(localJ);
508 ReducedSystemOperator::~ReducedSystemOperator()
518 M(&fespace), S(&fespace), H(&fespace),
519 viscosity(visc), M_solver(f.GetComm()), newton_solver(f.GetComm()),
522 const double rel_tol = 1e-8;
523 const int skip_zero_entries = 0;
525 const double ref_density = 1.0;
528 M.Assemble(skip_zero_entries);
529 M.Finalize(skip_zero_entries);
530 Mmat = M.ParallelAssemble();
531 fespace.GetEssentialTrueDofs(ess_bdr, ess_tdof_list);
535 M_solver.iterative_mode =
false;
536 M_solver.SetRelTol(rel_tol);
537 M_solver.SetAbsTol(0.0);
538 M_solver.SetMaxIter(30);
539 M_solver.SetPrintLevel(0);
540 M_prec.SetType(HypreSmoother::Jacobi);
541 M_solver.SetPreconditioner(M_prec);
542 M_solver.SetOperator(*Mmat);
546 H.SetEssentialTrueDofs(ess_tdof_list);
550 S.Assemble(skip_zero_entries);
551 S.Finalize(skip_zero_entries);
553 reduced_oper =
new ReducedSystemOperator(&M, &S, &H, ess_tdof_list);
556 J_hypreSmoother->
SetType(HypreSmoother::l1Jacobi);
558 J_prec = J_hypreSmoother;
569 newton_solver.SetSolver(*J_solver);
570 newton_solver.SetOperator(*reduced_oper);
571 newton_solver.SetPrintLevel(1);
572 newton_solver.SetRelTol(rel_tol);
573 newton_solver.SetAbsTol(0.0);
574 newton_solver.SetMaxIter(10);
587 if (viscosity != 0.0)
593 M_solver.Mult(z, dv_dt);
598 void HyperelasticOperator::ImplicitSolve(
const double dt,
613 reduced_oper->SetParameters(dt, &v, &x);
615 newton_solver.Mult(zero, dv_dt);
616 MFEM_VERIFY(newton_solver.GetConverged(),
"Newton solver did not converge.");
617 add(v, dt, dv_dt, dx_dt);
620 double HyperelasticOperator::ElasticEnergy(
const ParGridFunction &x)
const
622 return H.GetEnergy(x);
625 double HyperelasticOperator::KineticEnergy(
const ParGridFunction &v)
const
627 double loc_energy = 0.5*M.InnerProduct(v, v);
629 MPI_Allreduce(&loc_energy, &energy, 1, MPI_DOUBLE, MPI_SUM,
634 void HyperelasticOperator::GetElasticEnergyDensity(
637 ElasticEnergyCoefficient w_coeff(*model, x);
641 HyperelasticOperator::~HyperelasticOperator()
654 model.SetTransformation(T);
657 return model.EvalW(J)/J.Det();
671 const double s = 0.1/64.;
674 v(dim-1) = s*x(0)*x(0)*(8.0-x(0));
void visualize(ostream &out, Mesh *mesh, GridFunction *deformed_nodes, GridFunction *field, const char *field_name=NULL, bool init_vis=false)
void EliminateRowsCols(const Array< int > &rows_cols, const HypreParVector &X, HypreParVector &B)
void SetSubVector(const Array< int > &dofs, const double value)
Set the entries listed in dofs to the given value.
double Eval(ElementTransformation &T, const IntegrationPoint &ip, double t)
void Add(const int i, const int j, const double a)
void InitialDeformation(const Vector &x, Vector &y)
Conjugate gradient method.
Class for grid function - Vector with associated FE space.
void SetFromTrueVector()
Shortcut for calling SetFromTrueDofs() with GetTrueVector() as argument.
Subclass constant coefficient.
virtual void Init(TimeDependentOperator &f)
Associate a TimeDependentOperator with the ODE solver.
Base abstract class for time dependent operators.
void SwapNodes(GridFunction *&nodes, int &own_nodes_)
void Mult(const Table &A, const Table &B, Table &C)
C = A * B (as boolean matrices)
virtual void Step(Vector &x, double &t, double &dt)=0
Perform a time step from time t [in] to time t [out] based on the requested step size dt [in]...
Data type dense matrix using column-major storage.
int Size() const
Returns the size of the vector.
Abstract class for solving systems of ODEs: dx/dt = f(x,t)
virtual void Save(std::ostream &out) const
Abstract parallel finite element space.
virtual void ProjectCoefficient(Coefficient &coeff)
bool iterative_mode
If true, use the second argument of Mult() as an initial guess.
int main(int argc, char *argv[])
Backward Euler ODE solver. L-stable.
double * GetData() const
Return a pointer to the beginning of the Vector data.
void add(const Vector &v1, const Vector &v2, Vector &v)
void InitialVelocity(const Vector &x, Vector &v)
void SetPositiveDiagonal(bool pos=true)
After computing l1-norms, replace them with their absolute values.
void Add(const DenseMatrix &A, const DenseMatrix &B, double alpha, DenseMatrix &C)
C = A + alpha*B.
void SetTrueVector()
Shortcut for calling GetTrueDofs() with GetTrueVector() as argument.
virtual void SetPreconditioner(Solver &pr)
This should be called before SetOperator.
void SetPrintLevel(int print_lvl)
Mesh * GetMesh() const
Returns the mesh.
void UniformRefinement(int i, const DSTable &, int *, int *, int *)
void MakeTRef(FiniteElementSpace *f, double *tv)
Associate a new FiniteElementSpace and new true-dof data with the GridFunction.
void SetMaxIter(int max_it)
T Max() const
Find the maximal element in the array, using the comparison operator < for class T.
HYPRE_Int GlobalTrueVSize() const
Newton's method for solving F(x)=b for a given operator F.
virtual void Print(std::ostream &out=mfem::out) const
Parallel smoothers in hypre.
void PrintUsage(std::ostream &out) const
int SpaceDimension() const
The classical explicit forth-order Runge-Kutta method, RK4.
void SetAbsTol(double atol)
Array< int > bdr_attributes
A list of all unique boundary attributes used by the Mesh.
void SetRelTol(double rtol)
Base class Coefficient that may optionally depend on time.
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)
Third-order, strong stability preserving (SSP) Runge-Kutta method.
void GetVectorGradient(ElementTransformation &tr, DenseMatrix &grad) const
Implicit midpoint method. A-stable, not L-stable.
Class for integration point with weight.
void PrintOptions(std::ostream &out) const
Abstract class for hyperelastic models.
int open(const char hostname[], int port)
void GetNodes(Vector &node_coord) const
Arbitrary order H1-conforming (continuous) finite elements.
Class for parallel grid function.
OutStream out(std::cout)
Global stream used by the library for standard output. Initially it uses the same std::streambuf as s...
The classical forward Euler method.
Wrapper for hypre's ParCSR matrix class.
Class for parallel meshes.
void SetType(HypreSmoother::Type type, int relax_times=1)
Set the relaxation type and number of sweeps.
Arbitrary order "L2-conforming" discontinuous finite elements.