MFEM  v4.1.0 Finite element discretization library
ex10.cpp
Go to the documentation of this file.
1 // MFEM Example 10
2 // SUNDIALS Modification
3 //
4 // Compile with: make ex10
5 //
6 // Sample runs:
7 // ex10 -m ../../data/beam-quad.mesh -r 2 -o 2 -s 12 -dt 0.15 -vs 10
8 // ex10 -m ../../data/beam-tri.mesh -r 2 -o 2 -s 16 -dt 0.3 -vs 5
9 // ex10 -m ../../data/beam-hex.mesh -r 1 -o 2 -s 12 -dt 0.2 -vs 5
10 // ex10 -m ../../data/beam-tri.mesh -r 2 -o 2 -s 2 -dt 3 -nls kinsol
11 // ex10 -m ../../data/beam-quad.mesh -r 2 -o 2 -s 2 -dt 3 -nls kinsol
12 // ex10 -m ../../data/beam-hex.mesh -r 1 -o 2 -s 2 -dt 3 -nls kinsol
13 // ex10 -m ../../data/beam-quad.mesh -r 2 -o 2 -s 14 -dt 0.15 -vs 10
14 // ex10 -m ../../data/beam-tri.mesh -r 2 -o 2 -s 17 -dt 0.01 -vs 30
15 // ex10 -m ../../data/beam-hex.mesh -r 1 -o 2 -s 14 -dt 0.15 -vs 10
16 // ex10 -m ../../data/beam-quad-amr.mesh -r 2 -o 2 -s 12 -dt 0.15 -vs 10
17 //
18 // Description: This examples solves a time dependent nonlinear elasticity
19 // problem of the form dv/dt = H(x) + S v, dx/dt = v, where H is a
20 // hyperelastic model and S is a viscosity operator of Laplacian
21 // type. The geometry of the domain is assumed to be as follows:
22 //
23 // +---------------------+
24 // boundary --->| |
25 // attribute 1 | |
26 // (fixed) +---------------------+
27 //
28 // The example demonstrates the use of nonlinear operators (the
29 // class HyperelasticOperator defining H(x)), as well as their
30 // implicit time integration using a Newton method for solving an
31 // associated reduced backward-Euler type nonlinear equation
32 // (class ReducedSystemOperator). Each Newton step requires the
33 // inversion of a Jacobian matrix, which is done through a
34 // (preconditioned) inner solver. Note that implementing the
35 // method HyperelasticOperator::ImplicitSolve is the only
36 // requirement for high-order implicit (SDIRK) time integration.
37 //
38 // We recommend viewing examples 2 and 9 before viewing this
39 // example.
40
41 #include "mfem.hpp"
42 #include <memory>
43 #include <iostream>
44 #include <fstream>
45 #include <string>
46 #include <map>
47
48 #ifndef MFEM_USE_SUNDIALS
49 #error This example requires that MFEM is built with MFEM_USE_SUNDIALS=YES
50 #endif
51
52 using namespace std;
53 using namespace mfem;
54
55 class ReducedSystemOperator;
56
57 /** After spatial discretization, the hyperelastic model can be written as a
58  * system of ODEs:
59  * dv/dt = -M^{-1}*(H(x) + S*v)
60  * dx/dt = v,
61  * where x is the vector representing the deformation, v is the velocity field,
62  * M is the mass matrix, S is the viscosity matrix, and H(x) is the nonlinear
63  * hyperelastic operator.
64  *
65  * Class HyperelasticOperator represents the right-hand side of the above
66  * system of ODEs. */
67 class HyperelasticOperator : public TimeDependentOperator
68 {
69 protected:
70  FiniteElementSpace &fespace;
71
72  BilinearForm M, S;
73  NonlinearForm H;
74  double viscosity;
75  HyperelasticModel *model;
76
77  CGSolver M_solver; // Krylov solver for inverting the mass matrix M
78  DSmoother M_prec; // Preconditioner for the mass matrix M
79
80  /** Nonlinear operator defining the reduced backward Euler equation for the
81  velocity. Used in the implementation of method ImplicitSolve. */
82  ReducedSystemOperator *reduced_oper;
83
84  /// Newton solver for the reduced backward Euler equation
85  NewtonSolver *newton_solver;
86
87  /// Solver for the Jacobian solve in the Newton method
88  Solver *J_solver;
89  /// Preconditioner for the Jacobian solve in the Newton method
90  Solver *J_prec;
91
92  mutable Vector z; // auxiliary vector
93
95  SparseMatrix *Jacobian;
96
97  double saved_gamma; // saved gamma value from implicit setup
98
99 public:
100  /// Solver type to use in the ImplicitSolve() method, used by SDIRK methods.
101  enum NonlinearSolverType
102  {
103  NEWTON = 0, ///< Use MFEM's plain NewtonSolver
104  KINSOL = 1 ///< Use SUNDIALS' KINSOL (through MFEM's class KINSolver)
105  };
106
107  HyperelasticOperator(FiniteElementSpace &f, Array<int> &ess_bdr,
108  double visc, double mu, double K,
109  NonlinearSolverType nls_type);
110
111  /// Compute the right-hand side of the ODE system.
112  virtual void Mult(const Vector &vx, Vector &dvx_dt) const;
113
114  /** Solve the Backward-Euler equation: k = f(x + dt*k, t), for the unknown k.
115  This is the only requirement for high-order SDIRK implicit integration.*/
116  virtual void ImplicitSolve(const double dt, const Vector &x, Vector &k);
117
118
119  /// Custom Jacobian system solver for the SUNDIALS time integrators.
120  /** For the ODE system represented by HyperelasticOperator
121
122  M dv/dt = -(H(x) + S*v)
123  dx/dt = v,
124
125  this class facilitates the solution of linear systems of the form
126
127  (M + γS) yv + γJ yx = M bv, J=(dH/dx)(x)
128  - γ yv + yx = bx
129
130  for given bv, bx, x, and γ = GetTimeStep(). */
131
132  /** Linear solve applicable to the SUNDIALS format.
133  Solves (Mass - dt J) y = Mass b, where in our case:
134  Mass = | M 0 | J = | -S -grad_H | y = | v_hat | b = | b_v |
135  | 0 I | | I 0 | | x_hat | | b_x |
136  The result replaces the rhs b.
137  We substitute x_hat = b_x + dt v_hat and solve
138  (M + dt S + dt^2 grad_H) v_hat = M b_v - dt grad_H b_x. */
139
140  /** Setup the linear system. This method is used by the implicit
141  SUNDIALS solvers. */
142  virtual int SUNImplicitSetup(const Vector &y, const Vector &fy,
143  int jok, int *jcur, double gamma);
144
145  /** Solve the linear system. This method is used by the implicit
146  SUNDIALS solvers. */
147  virtual int SUNImplicitSolve(const Vector &b, Vector &x, double tol);
148
149  double ElasticEnergy(const Vector &x) const;
150  double KineticEnergy(const Vector &v) const;
151  void GetElasticEnergyDensity(const GridFunction &x, GridFunction &w) const;
152
153  virtual ~HyperelasticOperator();
154 };
155
156 /** Nonlinear operator of the form:
157  k --> (M + dt*S)*k + H(x + dt*v + dt^2*k) + S*v,
158  where M and S are given BilinearForms, H is a given NonlinearForm, v and x
159  are given vectors, and dt is a scalar. */
160 class ReducedSystemOperator : public Operator
161 {
162 private:
163  BilinearForm *M, *S;
164  NonlinearForm *H;
165  mutable SparseMatrix *Jacobian;
166  double dt;
167  const Vector *v, *x;
168  mutable Vector w, z;
169
170 public:
171  ReducedSystemOperator(BilinearForm *M_, BilinearForm *S_, NonlinearForm *H_);
172
173  /// Set current dt, v, x values - needed to compute action and Jacobian.
174  void SetParameters(double dt_, const Vector *v_, const Vector *x_);
175
176  /// Compute y = H(x + dt (v + dt k)) + M k + S (v + dt k).
177  virtual void Mult(const Vector &k, Vector &y) const;
178
179  /// Compute J = M + dt S + dt^2 grad_H(x + dt (v + dt k)).
180  virtual Operator &GetGradient(const Vector &k) const;
181
182  virtual ~ReducedSystemOperator();
183 };
184
185
186 /** Function representing the elastic energy density for the given hyperelastic
187  model+deformation. Used in HyperelasticOperator::GetElasticEnergyDensity. */
188 class ElasticEnergyCoefficient : public Coefficient
189 {
190 private:
191  HyperelasticModel &model;
192  const GridFunction &x;
193  DenseMatrix J;
194
195 public:
196  ElasticEnergyCoefficient(HyperelasticModel &m, const GridFunction &x_)
197  : model(m), x(x_) { }
198  virtual double Eval(ElementTransformation &T, const IntegrationPoint &ip);
199  virtual ~ElasticEnergyCoefficient() { }
200 };
201
202 void InitialDeformation(const Vector &x, Vector &y);
203
204 void InitialVelocity(const Vector &x, Vector &v);
205
206 void visualize(ostream &out, Mesh *mesh, GridFunction *deformed_nodes,
207  GridFunction *field, const char *field_name = NULL,
208  bool init_vis = false);
209
210
211 int main(int argc, char *argv[])
212 {
213  // 1. Parse command-line options.
214  const char *mesh_file = "../../data/beam-quad.mesh";
215  int ref_levels = 2;
216  int order = 2;
217  int ode_solver_type = 3;
218  double t_final = 300.0;
219  double dt = 3.0;
220  double visc = 1e-2;
221  double mu = 0.25;
222  double K = 5.0;
223  bool visualization = true;
224  const char *nls = "newton";
225  int vis_steps = 1;
226
227  // Relative and absolute tolerances for CVODE and ARKODE.
228  const double reltol = 1e-1, abstol = 1e-1;
229  // Since this example uses the loose tolerances defined above, it is
230  // necessary to lower the linear solver tolerance for CVODE which is relative
231  // to the above tolerances.
232  const double cvode_eps_lin = 1e-4;
233  // Similarly, the nonlinear tolerance for ARKODE needs to be tightened.
234  const double arkode_eps_nonlin = 1e-6;
235
236  OptionsParser args(argc, argv);
238  "Mesh file to use.");
240  "Number of times to refine the mesh uniformly.");
242  "Order (degree) of the finite elements.");
244  "ODE solver:\n\t"
245  "1 - Backward Euler,\n\t"
246  "2 - SDIRK2, L-stable\n\t"
247  "3 - SDIRK3, L-stable\n\t"
248  "4 - Implicit Midpoint,\n\t"
249  "5 - SDIRK2, A-stable,\n\t"
250  "6 - SDIRK3, A-stable,\n\t"
251  "7 - Forward Euler,\n\t"
252  "8 - RK2,\n\t"
253  "9 - RK3 SSP,\n\t"
254  "10 - RK4,\n\t"
255  "11 - CVODE implicit BDF, approximate Jacobian,\n\t"
256  "12 - CVODE implicit BDF, specified Jacobian,\n\t"
257  "13 - CVODE implicit ADAMS, approximate Jacobian,\n\t"
258  "14 - CVODE implicit ADAMS, specified Jacobian,\n\t"
259  "15 - ARKODE implicit, approximate Jacobian,\n\t"
260  "16 - ARKODE implicit, specified Jacobian,\n\t"
261  "17 - ARKODE explicit, 4th order.");
263  "Nonlinear systems solver: "
264  "\"newton\" (plain Newton) or \"kinsol\" (KINSOL).");
266  "Final time; start time is 0.");
268  "Time step.");
270  "Viscosity coefficient.");
272  "Shear modulus in the Neo-Hookean hyperelastic model.");
274  "Bulk modulus in the Neo-Hookean hyperelastic model.");
276  "--no-visualization",
277  "Enable or disable GLVis visualization.");
279  "Visualize every n-th timestep.");
280  args.Parse();
281  if (!args.Good())
282  {
283  args.PrintUsage(cout);
284  return 1;
285  }
286  args.PrintOptions(cout);
287
288  // check for vaild ODE solver option
289  if (ode_solver_type < 1 || ode_solver_type > 17)
290  {
291  cout << "Unknown ODE solver type: " << ode_solver_type << '\n';
292  return 1;
293  }
294
295  // 2. Read the mesh from the given mesh file. We can handle triangular,
296  // quadrilateral, tetrahedral and hexahedral meshes with the same code.
297  Mesh *mesh = new Mesh(mesh_file, 1, 1);
298  int dim = mesh->Dimension();
299
300  // 3. Setup the nonlinear solver
301  map<string,HyperelasticOperator::NonlinearSolverType> nls_map;
302  nls_map["newton"] = HyperelasticOperator::NEWTON;
303  nls_map["kinsol"] = HyperelasticOperator::KINSOL;
304  if (nls_map.find(nls) == nls_map.end())
305  {
306  cout << "Unknown type of nonlinear solver: " << nls << endl;
307  return 4;
308  }
309
310  // 4. Refine the mesh to increase the resolution. In this example we do
311  // 'ref_levels' of uniform refinement, where 'ref_levels' is a
312  // command-line parameter.
313  for (int lev = 0; lev < ref_levels; lev++)
314  {
315  mesh->UniformRefinement();
316  }
317
318  // 5. Define the vector finite element spaces representing the mesh
319  // deformation x, the velocity v, and the initial configuration, x_ref.
320  // Define also the elastic energy density, w, which is in a discontinuous
321  // higher-order space. Since x and v are integrated in time as a system,
322  // we group them together in block vector vx, with offsets given by the
323  // fe_offset array.
324  H1_FECollection fe_coll(order, dim);
325  FiniteElementSpace fespace(mesh, &fe_coll, dim);
326
327  int fe_size = fespace.GetTrueVSize();
328  cout << "Number of velocity/deformation unknowns: " << fe_size << endl;
329  Array<int> fe_offset(3);
330  fe_offset[0] = 0;
331  fe_offset[1] = fe_size;
332  fe_offset[2] = 2*fe_size;
333
334  BlockVector vx(fe_offset);
335  GridFunction v, x;
336  v.MakeTRef(&fespace, vx.GetBlock(0), 0);
337  x.MakeTRef(&fespace, vx.GetBlock(1), 0);
338
339  GridFunction x_ref(&fespace);
340  mesh->GetNodes(x_ref);
341
342  L2_FECollection w_fec(order + 1, dim);
343  FiniteElementSpace w_fespace(mesh, &w_fec);
344  GridFunction w(&w_fespace);
345
346  // 6. Set the initial conditions for v and x, and the boundary conditions on
347  // a beam-like mesh (see description above).
349  v.ProjectCoefficient(velo);
350  v.SetTrueVector();
352  x.ProjectCoefficient(deform);
353  x.SetTrueVector();
354
355  Array<int> ess_bdr(fespace.GetMesh()->bdr_attributes.Max());
356  ess_bdr = 0;
357  ess_bdr[0] = 1; // boundary attribute 1 (index 0) is fixed
358
359  // 7. Initialize the hyperelastic operator, the GLVis visualization and print
360  // the initial energies.
361  HyperelasticOperator oper(fespace, ess_bdr, visc, mu, K, nls_map[nls]);
362
363  socketstream vis_v, vis_w;
364  if (visualization)
365  {
366  char vishost[] = "localhost";
367  int visport = 19916;
368  vis_v.open(vishost, visport);
369  vis_v.precision(8);
371  visualize(vis_v, mesh, &x, &v, "Velocity", true);
372  vis_w.open(vishost, visport);
373  if (vis_w)
374  {
375  oper.GetElasticEnergyDensity(x, w);
376  vis_w.precision(8);
377  visualize(vis_w, mesh, &x, &w, "Elastic energy density", true);
378  }
379  }
380
381  double ee0 = oper.ElasticEnergy(x.GetTrueVector());
382  double ke0 = oper.KineticEnergy(v.GetTrueVector());
383  cout << "initial elastic energy (EE) = " << ee0 << endl;
384  cout << "initial kinetic energy (KE) = " << ke0 << endl;
385  cout << "initial total energy (TE) = " << (ee0 + ke0) << endl;
386
387  // 8. Define the ODE solver used for time integration. Several implicit
388  // singly diagonal implicit Runge-Kutta (SDIRK) methods, as well as
389  // explicit Runge-Kutta methods are available.
390  double t = 0.0;
391  oper.SetTime(t);
392
393  ODESolver *ode_solver = NULL;
394  CVODESolver *cvode = NULL;
395  ARKStepSolver *arkode = NULL;
396  switch (ode_solver_type)
397  {
398  // Implicit L-stable methods
399  case 1: ode_solver = new BackwardEulerSolver; break;
400  case 2: ode_solver = new SDIRK23Solver(2); break;
401  case 3: ode_solver = new SDIRK33Solver; break;
402  // Implicit A-stable methods (not L-stable)
403  case 4: ode_solver = new ImplicitMidpointSolver; break;
404  case 5: ode_solver = new SDIRK23Solver; break;
405  case 6: ode_solver = new SDIRK34Solver; break;
406  // Explicit methods
407  case 7: ode_solver = new ForwardEulerSolver; break;
408  case 8: ode_solver = new RK2Solver(0.5); break; // midpoint method
409  case 9: ode_solver = new RK3SSPSolver; break;
410  case 10: ode_solver = new RK4Solver; break;
411  // CVODE BDF
412  case 11:
413  case 12:
414  cvode = new CVODESolver(CV_BDF);
415  cvode->Init(oper);
416  cvode->SetSStolerances(reltol, abstol);
417  CVodeSetEpsLin(cvode->GetMem(), cvode_eps_lin);
418  cvode->SetMaxStep(dt);
419  if (ode_solver_type == 11)
420  {
421  cvode->UseSundialsLinearSolver();
422  }
423  ode_solver = cvode; break;
425  case 13:
426  case 14:
428  cvode->Init(oper);
429  cvode->SetSStolerances(reltol, abstol);
430  CVodeSetEpsLin(cvode->GetMem(), cvode_eps_lin);
431  cvode->SetMaxStep(dt);
432  if (ode_solver_type == 13)
433  {
434  cvode->UseSundialsLinearSolver();
435  }
436  ode_solver = cvode; break;
437  // ARKStep Implicit methods
438  case 15:
439  case 16:
440  arkode = new ARKStepSolver(ARKStepSolver::IMPLICIT);
441  arkode->Init(oper);
442  arkode->SetSStolerances(reltol, abstol);
443  ARKStepSetNonlinConvCoef(arkode->GetMem(), arkode_eps_nonlin);
444  arkode->SetMaxStep(dt);
445  if (ode_solver_type == 15)
446  {
447  arkode->UseSundialsLinearSolver();
448  }
449  ode_solver = arkode; break;
450  // ARKStep Explicit methods
451  case 17:
452  arkode = new ARKStepSolver(ARKStepSolver::EXPLICIT);
453  arkode->Init(oper);
454  arkode->SetSStolerances(reltol, abstol);
455  arkode->SetMaxStep(dt);
456  ode_solver = arkode; break;
457  }
458
459  // Initialize MFEM integrators, SUNDIALS integrators are initialized above
460  if (ode_solver_type < 11) { ode_solver->Init(oper); }
461
462  // 9. Perform time-integration (looping over the time iterations, ti, with a
463  // time-step dt).
464  bool last_step = false;
465  for (int ti = 1; !last_step; ti++)
466  {
467  double dt_real = min(dt, t_final - t);
468
469  ode_solver->Step(vx, t, dt_real);
470
471  last_step = (t >= t_final - 1e-8*dt);
472
473  if (last_step || (ti % vis_steps) == 0)
474  {
475  double ee = oper.ElasticEnergy(x.GetTrueVector());
476  double ke = oper.KineticEnergy(v.GetTrueVector());
477
478  cout << "step " << ti << ", t = " << t << ", EE = " << ee << ", KE = "
479  << ke << ", ΔTE = " << (ee+ke)-(ee0+ke0) << endl;
480
481  if (cvode) { cvode->PrintInfo(); }
482  else if (arkode) { arkode->PrintInfo(); }
483
484  if (visualization)
485  {
487  visualize(vis_v, mesh, &x, &v);
488  if (vis_w)
489  {
490  oper.GetElasticEnergyDensity(x, w);
491  visualize(vis_w, mesh, &x, &w);
492  }
493  }
494  }
495  }
496
497  // 10. Save the displaced mesh, the velocity and elastic energy.
498  {
500  GridFunction *nodes = &x;
501  int owns_nodes = 0;
502  mesh->SwapNodes(nodes, owns_nodes);
503  ofstream mesh_ofs("deformed.mesh");
504  mesh_ofs.precision(8);
505  mesh->Print(mesh_ofs);
506  mesh->SwapNodes(nodes, owns_nodes);
507  ofstream velo_ofs("velocity.sol");
508  velo_ofs.precision(8);
509  v.Save(velo_ofs);
510  ofstream ee_ofs("elastic_energy.sol");
511  ee_ofs.precision(8);
512  oper.GetElasticEnergyDensity(x, w);
513  w.Save(ee_ofs);
514  }
515
516  // 11. Free the used memory.
517  delete ode_solver;
518  delete mesh;
519
520  return 0;
521 }
522
523
524 void visualize(ostream &out, Mesh *mesh, GridFunction *deformed_nodes,
525  GridFunction *field, const char *field_name, bool init_vis)
526 {
527  if (!out)
528  {
529  return;
530  }
531
532  GridFunction *nodes = deformed_nodes;
533  int owns_nodes = 0;
534
535  mesh->SwapNodes(nodes, owns_nodes);
536
537  out << "solution\n" << *mesh << *field;
538
539  mesh->SwapNodes(nodes, owns_nodes);
540
541  if (init_vis)
542  {
543  out << "window_size 800 800\n";
544  out << "window_title '" << field_name << "'\n";
545  if (mesh->SpaceDimension() == 2)
546  {
547  out << "view 0 0\n"; // view from top
548  out << "keys jl\n"; // turn off perspective and light
549  }
550  out << "keys cm\n"; // show colorbar and mesh
551  out << "autoscale value\n"; // update value-range; keep mesh-extents fixed
552  out << "pause\n";
553  }
554  out << flush;
555 }
556
557
558 ReducedSystemOperator::ReducedSystemOperator(
560  : Operator(M_->Height()), M(M_), S(S_), H(H_), Jacobian(NULL),
561  dt(0.0), v(NULL), x(NULL), w(height), z(height)
562 { }
563
564 void ReducedSystemOperator::SetParameters(double dt_, const Vector *v_,
565  const Vector *x_)
566 {
567  dt = dt_; v = v_; x = x_;
568 }
569
570 void ReducedSystemOperator::Mult(const Vector &k, Vector &y) const
571 {
572  // compute: y = H(x + dt*(v + dt*k)) + M*k + S*(v + dt*k)
575  H->Mult(z, y);
578 }
579
580 Operator &ReducedSystemOperator::GetGradient(const Vector &k) const
581 {
582  delete Jacobian;
583  Jacobian = Add(1.0, M->SpMat(), dt, S->SpMat());
588  return *Jacobian;
589 }
590
591 ReducedSystemOperator::~ReducedSystemOperator()
592 {
593  delete Jacobian;
594 }
595
596
597 HyperelasticOperator::HyperelasticOperator(FiniteElementSpace &f,
598  Array<int> &ess_bdr, double visc,
599  double mu, double K,
600  NonlinearSolverType nls_type)
601  : TimeDependentOperator(2*f.GetTrueVSize(), 0.0), fespace(f),
602  M(&fespace), S(&fespace), H(&fespace),
603  viscosity(visc), z(height/2),
605 {
606  const double rel_tol = 1e-8;
607  const int skip_zero_entries = 0;
608
609  const double ref_density = 1.0; // density in the reference configuration
610  ConstantCoefficient rho0(ref_density);
612  M.Assemble(skip_zero_entries);
613  Array<int> ess_tdof_list;
614  fespace.GetEssentialTrueDofs(ess_bdr, ess_tdof_list);
615  SparseMatrix tmp;
616  M.FormSystemMatrix(ess_tdof_list, tmp);
617
618  M_solver.iterative_mode = false;
619  M_solver.SetRelTol(rel_tol);
620  M_solver.SetAbsTol(0.0);
621  M_solver.SetMaxIter(30);
622  M_solver.SetPrintLevel(0);
623  M_solver.SetPreconditioner(M_prec);
624  M_solver.SetOperator(M.SpMat());
625
626  model = new NeoHookeanModel(mu, K);
628  H.SetEssentialTrueDofs(ess_tdof_list);
629
630  ConstantCoefficient visc_coeff(viscosity);
632  S.Assemble(skip_zero_entries);
633  S.FormSystemMatrix(ess_tdof_list, tmp);
634
635  reduced_oper = new ReducedSystemOperator(&M, &S, &H);
636
637 #ifndef MFEM_USE_SUITESPARSE
638  J_prec = new DSmoother(1);
639  MINRESSolver *J_minres = new MINRESSolver;
640  J_minres->SetRelTol(rel_tol);
641  J_minres->SetAbsTol(0.0);
642  J_minres->SetMaxIter(300);
643  J_minres->SetPrintLevel(-1);
644  J_minres->SetPreconditioner(*J_prec);
645  J_solver = J_minres;
646 #else
647  J_solver = new UMFPackSolver;
648  J_prec = NULL;
649 #endif
650
651  if (nls_type == KINSOL)
652  {
653  KINSolver *kinsolver = new KINSolver(KIN_NONE, true);
654  newton_solver = kinsolver;
655  newton_solver->SetOperator(*reduced_oper);
656  newton_solver->SetMaxIter(200);
657  newton_solver->SetRelTol(rel_tol);
658  newton_solver->SetPrintLevel(0);
659  kinsolver->SetMaxSetupCalls(4);
660  }
661  else
662  {
663  newton_solver = new NewtonSolver();
664  newton_solver->SetOperator(*reduced_oper);
665  newton_solver->SetMaxIter(10);
666  newton_solver->SetRelTol(rel_tol);
667  newton_solver->SetPrintLevel(-1);
668  }
669  newton_solver->SetSolver(*J_solver);
670  newton_solver->iterative_mode = false;
671 }
672
673 void HyperelasticOperator::Mult(const Vector &vx, Vector &dvx_dt) const
674 {
675  // Create views to the sub-vectors v, x of vx, and dv_dt, dx_dt of dvx_dt
676  int sc = height/2;
677  Vector v(vx.GetData() + 0, sc);
678  Vector x(vx.GetData() + sc, sc);
679  Vector dv_dt(dvx_dt.GetData() + 0, sc);
680  Vector dx_dt(dvx_dt.GetData() + sc, sc);
681
682  H.Mult(x, z);
683  if (viscosity != 0.0)
684  {
686  }
687  z.Neg(); // z = -z
688  M_solver.Mult(z, dv_dt);
689
690  dx_dt = v;
691 }
692
693 void HyperelasticOperator::ImplicitSolve(const double dt,
694  const Vector &vx, Vector &dvx_dt)
695 {
696  int sc = height/2;
697  Vector v(vx.GetData() + 0, sc);
698  Vector x(vx.GetData() + sc, sc);
699  Vector dv_dt(dvx_dt.GetData() + 0, sc);
700  Vector dx_dt(dvx_dt.GetData() + sc, sc);
701
702  // By eliminating kx from the coupled system:
703  // kv = -M^{-1}*[H(x + dt*kx) + S*(v + dt*kv)]
704  // kx = v + dt*kv
705  // we reduce it to a nonlinear equation for kv, represented by the
706  // reduced_oper. This equation is solved with the newton_solver
707  // object (using J_solver and J_prec internally).
708  reduced_oper->SetParameters(dt, &v, &x);
709  Vector zero; // empty vector is interpreted as zero r.h.s. by NewtonSolver
710  newton_solver->Mult(zero, dv_dt);
711  MFEM_VERIFY(newton_solver->GetConverged(),
712  "Nonlinear solver did not converge.");
713 #ifdef MFEM_DEBUG
714  cout << " num nonlin sol iters = " << newton_solver->GetNumIterations()
715  << ", final norm = " << newton_solver->GetFinalNorm() << '\n';
716 #endif
718 }
719
720 int HyperelasticOperator::SUNImplicitSetup(const Vector &y,
721  const Vector &fy, int jok, int *jcur,
722  double gamma)
723 {
724  int sc = y.Size() / 2;
725  const Vector x(y.GetData() + sc, sc);
726
727  // J = M + dt*(S + dt*grad(H))
728  if (Jacobian) { delete Jacobian; }
729  Jacobian = Add(1.0, M.SpMat(), gamma, S.SpMat());
732
733  // Set Jacobian solve operator
734  J_solver->SetOperator(*Jacobian);
735
736  // Indicate that the Jacobian was updated
737  *jcur = 1;
738
739  // Save gamma for use in solve
740  saved_gamma = gamma;
741
742  // Return success
743  return 0;
744 }
745
746 int HyperelasticOperator::SUNImplicitSolve(const Vector &b, Vector &x,
747  double tol)
748 {
749  int sc = b.Size() / 2;
750  Vector b_v(b.GetData() + 0, sc);
751  Vector b_x(b.GetData() + sc, sc);
752  Vector x_v(x.GetData() + 0, sc);
753  Vector x_x(x.GetData() + sc, sc);
754  Vector rhs(sc);
755
756  // rhs = M b_v - dt*grad(H) b_x
758  rhs *= -saved_gamma;
760
761  J_solver->iterative_mode = false;
762  J_solver->Mult(rhs, x_v);
763
765
766  return 0;
767 }
768
769 double HyperelasticOperator::ElasticEnergy(const Vector &x) const
770 {
771  return H.GetEnergy(x);
772 }
773
774 double HyperelasticOperator::KineticEnergy(const Vector &v) const
775 {
776  return 0.5*M.InnerProduct(v, v);
777 }
778
779 void HyperelasticOperator::GetElasticEnergyDensity(
780  const GridFunction &x, GridFunction &w) const
781 {
782  ElasticEnergyCoefficient w_coeff(*model, x);
783  w.ProjectCoefficient(w_coeff);
784 }
785
786 HyperelasticOperator::~HyperelasticOperator()
787 {
788  delete Jacobian;
789  delete newton_solver;
790  delete J_solver;
791  delete J_prec;
792  delete reduced_oper;
793  delete model;
794 }
795
796
798  const IntegrationPoint &ip)
799 {
800  model.SetTransformation(T);
802  // return model.EvalW(J); // in reference configuration
803  return model.EvalW(J)/J.Det(); // in deformed configuration
804 }
805
806
807 void InitialDeformation(const Vector &x, Vector &y)
808 {
809  // set the initial configuration to be the same as the reference, stress
810  // free, configuration
811  y = x;
812 }
813
814 void InitialVelocity(const Vector &x, Vector &v)
815 {
816  const int dim = x.Size();
817  const double s = 0.1/64.;
818
819  v = 0.0;
820  v(dim-1) = s*x(0)*x(0)*(8.0-x(0));
821  v(0) = -s*x(0)*x(0);
822 }
void Init(TimeDependentOperator &f_)
Initialize CVODE: calls CVodeCreate() to create the CVODE memory and set some defaults.
Definition: sundials.cpp:151
void visualize(ostream &out, Mesh *mesh, GridFunction *deformed_nodes, GridFunction *field, const char *field_name=NULL, bool init_vis=false)
Definition: ex10.cpp:379
double Eval(ElementTransformation &T, const IntegrationPoint &ip, double t)
Evaluate the coefficient in the element described by T at the point ip at time t. ...
Definition: coefficient.hpp:55
void Add(const int i, const int j, const double a)
Definition: sparsemat.cpp:2281
void InitialDeformation(const Vector &x, Vector &y)
Definition: ex10.cpp:591
virtual void Print(std::ostream &out=mfem::out) const
Definition: mesh.hpp:1188
Definition: solvers.hpp:152
Class for grid function - Vector with associated FE space.
Definition: gridfunc.hpp:27
Data type for scaled Jacobi-type smoother of sparse matrix.
void SetSStolerances(double reltol, double abstol)
Set the scalar relative and scalar absolute tolerances.
Definition: sundials.cpp:342
A class to handle Vectors in a block fashion.
Definition: blockvector.hpp:30
void SetFromTrueVector()
Shortcut for calling SetFromTrueDofs() with GetTrueVector() as argument.
Definition: gridfunc.hpp:139
Subclass constant coefficient.
Definition: coefficient.hpp:67
Base abstract class for first order time dependent operators.
Definition: operator.hpp:259
void SwapNodes(GridFunction *&nodes, int &own_nodes_)
Definition: mesh.cpp:6241
void Mult(const Table &A, const Table &B, Table &C)
C = A * B (as boolean matrices)
Definition: table.cpp:476
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]...
virtual void Init(TimeDependentOperator &f)
Associate a TimeDependentOperator with the ODE solver.
Definition: ode.cpp:18
void * GetMem() const
Access the SUNDIALS memory structure.
Definition: sundials.hpp:80
Data type dense matrix using column-major storage.
Definition: densemat.hpp:23
int Size() const
Returns the size of the vector.
Definition: vector.hpp:157
Abstract class for solving systems of ODEs: dx/dt = f(x,t)
Definition: ode.hpp:22
void SetMaxStep(double dt_max)
Set the maximum time step.
Definition: sundials.cpp:835
MINRES method.
Definition: solvers.hpp:262
int main(int argc, char *argv[])
Definition: ex1.cpp:62
Backward Euler ODE solver. L-stable.
Definition: ode.hpp:360
double * GetData() const
Return a pointer to the beginning of the Vector data.
Definition: vector.hpp:166
void add(const Vector &v1, const Vector &v2, Vector &v)
Definition: vector.cpp:238
void SetMaxSetupCalls(int max_calls)
Set maximum number of nonlinear iterations without a Jacobian update.
Definition: sundials.cpp:1225
void InitialVelocity(const Vector &x, Vector &v)
Definition: ex10.cpp:598
Interface to ARKode&#39;s ARKStep module – additive Runge-Kutta methods.
Definition: sundials.hpp:197
Direct sparse solver using UMFPACK.
Definition: solvers.hpp:580
virtual void Save(std::ostream &out) const
Save the GridFunction to an output stream.
Definition: gridfunc.cpp:2731
void Add(const DenseMatrix &A, const DenseMatrix &B, double alpha, DenseMatrix &C)
C = A + alpha*B.
Definition: densemat.cpp:1926
void SetTrueVector()
Shortcut for calling GetTrueDofs() with GetTrueVector() as argument.
Definition: gridfunc.hpp:133
virtual void SetPreconditioner(Solver &pr)
This should be called before SetOperator.
Definition: solvers.hpp:275
void SetPrintLevel(int print_lvl)
Definition: solvers.cpp:70
Data type sparse matrix.
Definition: sparsemat.hpp:40
Mesh * GetMesh() const
Returns the mesh.
Definition: fespace.hpp:295
Interface to the CVODE library – linear multi-step methods.
Definition: sundials.hpp:92
double b
Definition: lissajous.cpp:42
Interface to the KINSOL library – nonlinear solver methods.
Definition: sundials.hpp:370
void UniformRefinement(int i, const DSTable &, int *, int *, int *)
Definition: mesh.cpp:7982
void MakeTRef(FiniteElementSpace *f, double *tv)
Associate a new FiniteElementSpace and new true-dof data with the GridFunction.
Definition: gridfunc.cpp:207
void SetMaxIter(int max_it)
Definition: solvers.hpp:65
T Max() const
Find the maximal element in the array, using the comparison operator &lt; for class T.
Definition: array.cpp:68
void SetMaxStep(double dt_max)
Set the maximum time step.
Definition: sundials.cpp:348
Newton&#39;s method for solving F(x)=b for a given operator F.
Definition: solvers.hpp:300
virtual int GetTrueVSize() const
Return the number of vector true (conforming) dofs.
Definition: fespace.hpp:384
int Dimension() const
Definition: mesh.hpp:744
void PrintUsage(std::ostream &out) const
Definition: optparser.cpp:434
const Vector & GetTrueVector() const
Definition: gridfunc.hpp:121
int SpaceDimension() const
Definition: mesh.hpp:745
The classical explicit forth-order Runge-Kutta method, RK4.
Definition: ode.hpp:145
void SetAbsTol(double atol)
Definition: solvers.hpp:64
Array< int > bdr_attributes
A list of all unique boundary attributes used by the Mesh.
Definition: mesh.hpp:189
void SetRelTol(double rtol)
Definition: solvers.hpp:63
Class FiniteElementSpace - responsible for providing FEM view of the mesh, mainly managing the set of...
Definition: fespace.hpp:87
virtual void SetOperator(const Operator &op)
Set the nonlinear Operator of the system and initialize KINSOL.
Definition: sundials.cpp:1052
Base class Coefficient that may optionally depend on time.
Definition: coefficient.hpp:31
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)
Definition: optparser.hpp:76
Third-order, strong stability preserving (SSP) Runge-Kutta method.
Definition: ode.hpp:132
Definition: gridfunc.cpp:1138
Implicit midpoint method. A-stable, not L-stable.
Definition: ode.hpp:373
Class for integration point with weight.
Definition: intrules.hpp:25
void PrintInfo() const
Print various ARKStep statistics.
Definition: sundials.cpp:871
int dim
Definition: ex24.cpp:43
void PrintOptions(std::ostream &out) const
Definition: optparser.cpp:304
virtual void ProjectCoefficient(Coefficient &coeff)
Definition: gridfunc.cpp:1685
Abstract class for hyperelastic models.
int open(const char hostname[], int port)
Vector data type.
Definition: vector.hpp:48
void GetNodes(Vector &node_coord) const
Definition: mesh.cpp:6203
Arbitrary order H1-conforming (continuous) finite elements.
Definition: fe_coll.hpp:83
void PrintInfo() const
Print various CVODE statistics.
Definition: sundials.cpp:360
Base class for solvers.
Definition: operator.hpp:500
OutStream out(std::cout)
Global stream used by the library for standard output. Initially it uses the same std::streambuf as s...
Definition: globals.hpp:66
The classical forward Euler method.
Definition: ode.hpp:99
Abstract operator.
Definition: operator.hpp:24
void Init(TimeDependentOperator &f_)
Initialize ARKode: calls ARKStepCreate() to create the ARKStep memory and set some defaults...
Definition: sundials.cpp:565
void SetSStolerances(double reltol, double abstol)
Set the scalar relative and scalar absolute tolerances.
Definition: sundials.cpp:829
void UseSundialsLinearSolver()
Attach a SUNDIALS GMRES linear solver to ARKode.
Definition: sundials.cpp:757
void UseSundialsLinearSolver()
Attach SUNDIALS GMRES linear solver to CVODE.
Definition: sundials.cpp:322
Vector & GetBlock(int i)
Get the i-th vector in the block.
Definition: blockvector.hpp:84
Arbitrary order &quot;L2-conforming&quot; discontinuous finite elements.
Definition: fe_coll.hpp:143
void Neg()
(*this) = -(*this)
Definition: vector.cpp:230
bool Good() const
Definition: optparser.hpp:125