libMesh
fem_system_ex2.C
Go to the documentation of this file.
1 // The libMesh Finite Element Library.
2 // Copyright (C) 2002-2019 Benjamin S. Kirk, John W. Peterson, Roy H. Stogner
3 
4 // This library is free software; you can redistribute it and/or
5 // modify it under the terms of the GNU Lesser General Public
6 // License as published by the Free Software Foundation; either
7 // version 2.1 of the License, or (at your option) any later version.
8 
9 // This library is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 // Lesser General Public License for more details.
13 
14 // You should have received a copy of the GNU Lesser General Public
15 // License along with this library; if not, write to the Free Software
16 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17 
18 
19 // <h1>FEMSystem Example 2</h1>
20 // \author Robert Weidlich
21 // \date 2012
22 
23 #include "libmesh/equation_systems.h"
24 #include "libmesh/getpot.h"
25 #include "libmesh/libmesh.h"
26 #include "libmesh/libmesh_logging.h"
27 #include "libmesh/mesh.h"
28 #include "libmesh/mesh_generation.h"
29 #include "libmesh/numeric_vector.h"
30 #include "libmesh/string_to_enum.h"
31 #include "libmesh/time_solver.h"
32 #include "libmesh/transient_system.h"
33 #include "libmesh/vtk_io.h"
34 #include "libmesh/auto_ptr.h" // libmesh_make_unique
35 #include "libmesh/enum_solver_package.h"
36 
37 #include <cstdio>
38 #include <ctime>
39 #include <fstream>
40 #include <iomanip>
41 #include <iostream>
42 #include <sstream>
43 #include <string>
44 #include <unistd.h>
45 
46 using namespace libMesh;
47 
48 #include "solid_system.h"
49 
50 void setup(EquationSystems & systems,
51  Mesh & mesh,
52  GetPot & args)
53 {
54  const unsigned int dim = mesh.mesh_dimension();
55  // We currently invert tensors with the assumption that they're 3x3
56  libmesh_assert (dim == 3);
57 
58  // Generating Mesh
59  ElemType eltype = Utility::string_to_enum<ElemType>(args("mesh/generation/element_type", "hex8"));
60  int nx = args("mesh/generation/num_elem", 4, 0);
61  int ny = args("mesh/generation/num_elem", 4, 1);
62  int nz = dim > 2 ? args("mesh/generation/num_elem", 4, 2) : 0;
63  double origx = args("mesh/generation/origin", -1.0, 0);
64  double origy = args("mesh/generation/origin", -1.0, 1);
65  double origz = args("mesh/generation/origin", 0.0, 2);
66  double sizex = args("mesh/generation/size", 2.0, 0);
67  double sizey = args("mesh/generation/size", 2.0, 1);
68  double sizez = args("mesh/generation/size", 2.0, 2);
70  origx, origx+sizex, origy, origy+sizey, origz, origz+sizez, eltype);
71 
72  // Creating Systems
73  SolidSystem & imms = systems.add_system<SolidSystem> ("solid");
74  imms.args = args;
75 
76  // Build up auxiliary system
77  ExplicitSystem & aux_sys = systems.add_system<TransientExplicitSystem>("auxiliary");
78 
79  // Initialize the system
80  systems.parameters.set<unsigned int>("phase") = 0;
81  systems.init();
82 
83  imms.save_initial_mesh();
84 
85  // Fill global solution vector from local ones
86  aux_sys.current_local_solution->close();
87  *aux_sys.solution = *aux_sys.current_local_solution;
88  aux_sys.solution->close();
89 }
90 
91 
92 
93 void run_timestepping(EquationSystems & systems, GetPot & args)
94 {
95  TransientExplicitSystem & aux_system = systems.get_system<TransientExplicitSystem>("auxiliary");
96 
97  SolidSystem & solid_system = systems.get_system<SolidSystem>("solid");
98 
99  std::unique_ptr<VTKIO> io = libmesh_make_unique<VTKIO>(systems.get_mesh());
100 
101  Real duration = args("duration", 1.0);
102 
103  for (unsigned int t_step = 0; t_step < duration/solid_system.deltat; t_step++)
104  {
105  // Progress in current phase [0..1]
106  Real progress = t_step * solid_system.deltat / duration;
107  systems.parameters.set<Real>("progress") = progress;
108  systems.parameters.set<unsigned int>("step") = t_step;
109 
110  // Update message
111 
112  out << "===== Time Step " << std::setw(4) << t_step;
113  out << " (" << std::fixed << std::setprecision(2) << std::setw(6) << (progress*100.) << "%)";
114  out << ", time = " << std::setw(7) << solid_system.time;
115  out << " =====" << std::endl;
116 
117  // Advance timestep in auxiliary system
118  aux_system.current_local_solution->close();
119  aux_system.old_local_solution->close();
120  *aux_system.older_local_solution = *aux_system.old_local_solution;
121  *aux_system.old_local_solution = *aux_system.current_local_solution;
122 
123  out << "Solving Solid" << std::endl;
124  solid_system.solve();
125  aux_system.current_local_solution->close();
126  *aux_system.solution = *aux_system.current_local_solution;
127  aux_system.solution->close();
128 
129  // Carry out the adaptive mesh refinement/coarsening
130  out << "Doing a reinit of the equation systems" << std::endl;
131  systems.reinit();
132 
133  if (t_step % args("output/frequency", 1) == 0)
134  {
135  std::stringstream file_name;
136  file_name << args("results_directory", "./")
137  << "fem_"
138  << std::setw(6)
139  << std::setfill('0')
140  << t_step
141  << ".pvtu";
142 
143  io->write_equation_systems(file_name.str(), systems);
144  }
145  // Advance to the next timestep in a transient problem
146  out << "Advancing to next step" << std::endl;
147  solid_system.time_solver->advance_timestep();
148  }
149 }
150 
151 
152 
153 int main(int argc, char ** argv)
154 {
155  // Initialize libMesh and any dependent libraries
156  LibMeshInit init(argc, argv);
157 
158  // This example requires a linear solver package.
159  libmesh_example_requires(libMesh::default_solver_package() != INVALID_SOLVER_PACKAGE,
160  "--enable-petsc, --enable-trilinos, or --enable-eigen");
161 
162  // Skip this example if we do not meet certain requirements
163 #ifndef LIBMESH_HAVE_VTK
164  libmesh_example_requires(false, "--enable-vtk");
165 #endif
166 
167  // Trilinos gives us an inverted element on this one...
168  libmesh_example_requires(libMesh::default_solver_package() != TRILINOS_SOLVERS, "--enable-petsc");
169 
170  // Threaded assembly doesn't currently work with the moving mesh
171  // code.
172  // We'll skip this example for now.
173  if (libMesh::n_threads() > 1)
174  {
175  libMesh::out << "We skip fem_system_ex2 when using threads.\n"
176  << std::endl;
177  return 0;
178  }
179 
180  // read simulation parameters from file
181  GetPot args = GetPot("solid.in");
182 
183  // Create System and Mesh
184  int dim = args("mesh/generation/dimension", 3);
185  libmesh_example_requires(dim <= LIBMESH_DIM, "3D support");
186 
187  // Create a mesh distributed across the default MPI communicator.
188  Mesh mesh(init.comm(), cast_int<unsigned char>(dim));
189 
190  EquationSystems systems(mesh);
191 
192  // Create and set systems up
193  setup(systems, mesh, args);
194 
195  // run the systems
196  run_timestepping(systems, args);
197 
198  out << "Finished calculations" << std::endl;
199  return 0;
200 }
libMesh::Mesh
The Mesh class is a thin wrapper, around the ReplicatedMesh class by default.
Definition: mesh.h:50
libMesh::EquationSystems::add_system
virtual System & add_system(const std::string &system_type, const std::string &name)
Add the system of type system_type named name to the systems array.
Definition: equation_systems.C:345
libMesh::EquationSystems::get_mesh
const MeshBase & get_mesh() const
Definition: equation_systems.h:637
solid_system.h
libMesh::TransientSystem
Manages storage and variables for transient systems.
Definition: transient_system.h:57
libMesh::n_threads
unsigned int n_threads()
Definition: libmesh_base.h:96
main
int main(int argc, char **argv)
Definition: fem_system_ex2.C:153
libMesh
The libMesh namespace provides an interface to certain functionality in the library.
Definition: factoryfunction.C:55
libMesh::EquationSystems::get_system
const T_sys & get_system(const std::string &name) const
Definition: equation_systems.h:757
libMesh::MeshTools::Generation::build_cube
void build_cube(UnstructuredMesh &mesh, const unsigned int nx=0, const unsigned int ny=0, const unsigned int nz=0, const Real xmin=0., const Real xmax=1., const Real ymin=0., const Real ymax=1., const Real zmin=0., const Real zmax=1., const ElemType type=INVALID_ELEM, const bool gauss_lobatto_grid=false)
Builds a (elements) cube.
Definition: mesh_generation.C:298
libMesh::default_solver_package
SolverPackage default_solver_package()
Definition: libmesh.C:993
mesh
MeshBase & mesh
Definition: mesh_communication.C:1257
libMesh::MeshBase::mesh_dimension
unsigned int mesh_dimension() const
Definition: mesh_base.C:135
dim
unsigned int dim
Definition: adaptivity_ex3.C:113
libMesh::TriangleWrapper::init
void init(triangulateio &t)
Initializes the fields of t to nullptr/0 as necessary.
libMesh::libmesh_assert
libmesh_assert(ctx)
setup
void setup(EquationSystems &systems, Mesh &mesh, GetPot &args)
Definition: fem_system_ex2.C:50
SolidSystem::args
GetPot args
Definition: solid_system.h:58
libMesh::INVALID_SOLVER_PACKAGE
Definition: enum_solver_package.h:43
libMesh::TransientSystem::older_local_solution
std::unique_ptr< NumericVector< Number > > older_local_solution
All the values I need to compute my contribution to the simulation at hand.
Definition: transient_system.h:127
libMesh::System::current_local_solution
std::unique_ptr< NumericVector< Number > > current_local_solution
All the values I need to compute my contribution to the simulation at hand.
Definition: system.h:1551
libMesh::EquationSystems::init
virtual void init()
Initialize all the systems.
Definition: equation_systems.C:96
SolidSystem
Definition: solid_system.h:30
libMesh::LibMeshInit
The LibMeshInit class, when constructed, initializes the dependent libraries (e.g.
Definition: libmesh.h:83
libMesh::EquationSystems
This is the EquationSystems class.
Definition: equation_systems.h:74
libMesh::Parameters::set
T & set(const std::string &)
Definition: parameters.h:460
libMesh::TRILINOS_SOLVERS
Definition: enum_solver_package.h:37
libMesh::ExplicitSystem
Manages consistently variables, degrees of freedom, and coefficient vectors for explicit systems.
Definition: explicit_system.h:48
libMesh::EquationSystems::reinit
virtual void reinit()
Handle any mesh changes and reinitialize all the systems on the updated mesh.
Definition: equation_systems.C:121
run_timestepping
void run_timestepping(EquationSystems &systems, GetPot &args)
Definition: fem_system_ex2.C:93
libMesh::System::solution
std::unique_ptr< NumericVector< Number > > solution
Data structure to hold solution values.
Definition: system.h:1539
SolidSystem::save_initial_mesh
void save_initial_mesh()
Definition: solid_system.C:62
libMesh::Real
DIE A HORRIBLE DEATH HERE typedef LIBMESH_DEFAULT_SCALAR_TYPE Real
Definition: libmesh_common.h:121
libMesh::TransientSystem::old_local_solution
std::unique_ptr< NumericVector< Number > > old_local_solution
All the values I need to compute my contribution to the simulation at hand.
Definition: transient_system.h:119
libMesh::out
OStreamProxy out
libMesh::ElemType
ElemType
Defines an enum for geometric element types.
Definition: enum_elem_type.h:33
libMesh::EquationSystems::parameters
Parameters parameters
Data structure holding arbitrary parameters.
Definition: equation_systems.h:557