34 #include "libmesh/string_to_enum.h"
35 #include "libmesh/mesh_generation.h"
36 #include "libmesh/mesh_modification.h"
37 #include "libmesh/mesh_refinement.h"
38 #include "libmesh/parallel_mesh.h"
39 #include "libmesh/parsed_function.h"
40 #include "libmesh/serial_mesh.h"
58 const libMesh::Parallel::Communicator &comm)
61 if( input.have_section(
"mesh-options/") &&
62 input.have_section(
"Mesh/") )
64 libmesh_error_msg(
"Error: Detected illegal simulataneous use of [mesh-options] and [Mesh] in input!");
70 if( !input.have_variable(
"mesh-options/mesh_option") &&
71 !input.have_section(
"Mesh/Read/") &&
72 !input.have_section(
"Mesh/Generation/") )
74 libmesh_error_msg(
"ERROR: Must specify either Mesh/Read or Mesh/Generation in input.");
78 if( input.have_section(
"Mesh/Read/") && input.have_section(
"Mesh/Generation/") )
80 libmesh_error_msg(
"ERROR: Can only specify one of Mesh/Read and Mesh/Generation");
84 std::string mesh_build_type =
"NULL";
85 if( input.have_section(
"Mesh/Read/") )
86 mesh_build_type =
"read";
88 else if( input.have_section(
"Mesh/Generation/") )
89 mesh_build_type =
"generate";
91 this->deprecated_option<std::string>( input,
"mesh-options/mesh_option",
"Mesh/Read or Mesh/Generation",
"DIE!", mesh_build_type);
95 if( mesh_build_type != std::string(
"generate") &&
96 mesh_build_type != std::string(
"read") &&
97 mesh_build_type != std::string(
"read_mesh_from_file") &&
98 mesh_build_type != std::string(
"create_1D_mesh") &&
99 mesh_build_type != std::string(
"create_2D_mesh") &&
100 mesh_build_type != std::string(
"create_3D_mesh") )
102 std::string error =
"ERROR: Invalid value of "+mesh_build_type+
" for Mesh/type.\n";
103 error +=
" Valid values are: generate\n";
105 libmesh_error_msg(error);
109 libMesh::UnstructuredMesh* mesh;
113 std::string mesh_class = input(
"Mesh/class",
"default");
115 this->deprecated_option<std::string>( input,
"mesh-options/mesh_class",
"Mesh/class",
"default", mesh_class);
117 if (mesh_class ==
"parallel")
118 mesh =
new libMesh::ParallelMesh(comm);
119 else if (mesh_class ==
"serial")
120 mesh =
new libMesh::SerialMesh(comm);
121 else if (mesh_class ==
"default")
122 mesh =
new libMesh::Mesh(comm);
125 std::string error =
"ERROR: Invalid class "+mesh_class+
" input for Mesh/class.\n";
126 error +=
" Valid choices are: serial, parallel.\n";
127 libmesh_error_msg(error);
132 if(mesh_build_type ==
"read_mesh_from_file" ||
133 mesh_build_type ==
"read" )
136 if( !input.have_variable(
"mesh-options/mesh_filename") &&
137 !input.have_variable(
"Mesh/Read/filename") )
139 libmesh_error_msg(
"ERROR: Must specify Mesh/Read/filename for reading mesh.");
142 std::string mesh_filename = input(
"Mesh/Read/filename",
"DIE!");
144 this->deprecated_option<std::string>( input,
"mesh-options/mesh_filename",
"Mesh/Read/filename",
"DIE!", mesh_filename);
149 mesh->read(mesh_filename);
153 else if(mesh_build_type==
"create_1D_mesh" ||
154 mesh_build_type==
"create_2D_mesh" ||
155 mesh_build_type==
"create_3D_mesh" ||
156 mesh_build_type==
"generate")
158 this->generate_mesh(mesh_build_type,input,mesh);
170 if( !input.have_variable(
"restart-options/restart_file") )
172 this->do_mesh_refinement_from_input( input, comm, *mesh );
175 return std::tr1::shared_ptr<libMesh::UnstructuredMesh>(mesh);
179 libMesh::UnstructuredMesh* mesh )
181 unsigned int dimension = input(
"Mesh/Generation/dimension",0);
183 if( !input.have_variable(
"mesh-options/mesh_option") &&
184 !input.have_variable(
"Mesh/Generation/dimension") )
186 libmesh_error_msg(
"ERROR: Must specify Mesh/Generation/dimension for generating mesh.");
190 if( mesh_build_type ==
"create_1D_mesh" )
193 if( mesh_build_type==
"create_2D_mesh" )
196 if( mesh_build_type==
"create_3D_mesh" )
200 mesh->set_mesh_dimension(dimension);
204 libMesh::Real x_min = input(
"Mesh/Generation/x_min", 0.0);
205 this->
deprecated_option( input,
"mesh-options/domain_x1_min",
"Mesh/Generation/x_min", 0.0, x_min );
207 libMesh::Real x_max = input(
"Mesh/Generation/x_max", 1.0);
208 this->
deprecated_option( input,
"mesh-options/domain_x1_max",
"Mesh/Generation/x_max", 1.0, x_max );
214 libMesh::Real y_min = 0.0;
215 libMesh::Real y_max = 0.0;
219 y_min = input(
"Mesh/Generation/y_min", 0.0);
220 this->
deprecated_option( input,
"mesh-options/domain_x2_min",
"Mesh/Generation/y_min", 0.0, y_min );
222 y_max = input(
"Mesh/Generation/y_max", 1.0);
223 this->
deprecated_option( input,
"mesh-options/domain_x2_max",
"Mesh/Generation/y_max", 1.0, y_max );
229 libMesh::Real z_min = 0.0;
230 libMesh::Real z_max = 0.0;
234 z_min = input(
"Mesh/Generation/z_min", 0.0);
235 this->
deprecated_option( input,
"mesh-options/domain_x3_min",
"Mesh/Generation/z_min", 0.0, z_min );
237 z_max = input(
"Mesh/Generation/z_max", 1.0);
238 this->
deprecated_option( input,
"mesh-options/domain_x3_max",
"Mesh/Generation/z_max", 1.0, z_max );
244 if( !input.have_variable(
"mesh-options/mesh_nx1") &&
245 !input.have_variable(
"Mesh/Generation/n_elems_x") )
247 libmesh_error_msg(
"ERROR: Must supply Mesh/Generation/n_elems_x for mesh generation.");
250 unsigned int n_elems_x = input(
"Mesh/Generation/n_elems_x", 0);
251 this->deprecated_option<unsigned int>( input,
"mesh-options/mesh_nx1",
"Mesh/Generation/n_elems_x", 0, n_elems_x );
256 unsigned int n_elems_y = 0;
259 if( !input.have_variable(
"mesh-options/mesh_nx2") &&
260 !input.have_variable(
"Mesh/Generation/n_elems_y") )
262 libmesh_error_msg(
"ERROR: Must supply Mesh/Generation/n_elems_y for mesh generation.");
265 n_elems_y = input(
"Mesh/Generation/n_elems_y", 0);
266 this->deprecated_option<unsigned int>( input,
"mesh-options/mesh_nx2",
"Mesh/Generation/n_elems_y", 0, n_elems_y );
272 unsigned int n_elems_z = 0;
275 if( !input.have_variable(
"mesh-options/mesh_nx3") &&
276 !input.have_variable(
"Mesh/Generation/n_elems_z") )
278 libmesh_error_msg(
"ERROR: Must supply Mesh/Generation/n_elems_z for mesh generation.");
281 n_elems_z = input(
"Mesh/Generation/n_elems_z", 0);
282 this->deprecated_option<unsigned int>( input,
"mesh-options/mesh_nx3",
"Mesh/Generation/n_elems_z", 0, n_elems_z );
287 std::string element_type = input(
"Mesh/Generation/element_type",
"default");
288 this->deprecated_option<std::string>( input,
"mesh-options/element_type",
"Mesh/Generation/element_type",
"default", element_type );
293 if(element_type==
"default")
295 element_type =
"EDGE3";
298 GRINSEnums::ElemType element_enum_type =
299 libMesh::Utility::string_to_enum<GRINSEnums::ElemType>(element_type);
301 libMesh::MeshTools::Generation::build_line(*mesh,
308 else if( dimension == 2 )
310 if(element_type==
"default")
312 element_type =
"TRI6";
315 GRINSEnums::ElemType element_enum_type =
316 libMesh::Utility::string_to_enum<GRINSEnums::ElemType>(element_type);
318 libMesh::MeshTools::Generation::build_square(*mesh,
328 else if( dimension == 3 )
330 if(element_type==
"default")
332 element_type =
"TET10";
335 GRINSEnums::ElemType element_enum_type =
336 libMesh::Utility::string_to_enum<GRINSEnums::ElemType>(element_type);
338 libMesh::MeshTools::Generation::build_cube(*mesh,
361 const libMesh::Parallel::Communicator &comm,
362 libMesh::UnstructuredMesh& mesh )
const
364 std::string redistribution_function_string =
365 input(
"Mesh/Redistribution/function", std::string(
"0"));
366 this->deprecated_option<std::string>( input,
"mesh-options/redistribute",
"Mesh/Redistribution/function",
"0", redistribution_function_string );
368 if (redistribution_function_string !=
"0")
370 libMesh::ParsedFunction<libMesh::Real>
371 redistribution_function(redistribution_function_string);
373 libMesh::MeshTools::Modification::redistribute
374 (mesh, redistribution_function);
384 const libMesh::Elem *elem = *mesh.elements_begin();
386 if (elem->default_order() != libMesh::FIRST)
388 mesh.all_first_order();
389 mesh.all_second_order();
393 int uniformly_refine = input(
"Mesh/Refinement/uniformly_refine", 0);
394 this->
deprecated_option( input,
"mesh-options/uniformly_refine",
"Mesh/Refinement/uniformly_refine", 0, uniformly_refine );
396 if( uniformly_refine > 0 )
398 libMesh::MeshRefinement(mesh).uniformly_refine(uniformly_refine);
401 std::string h_refinement_function_string =
402 input(
"Mesh/Refinement/locally_h_refine", std::string(
"0"));
403 this->deprecated_option<std::string>( input,
"mesh-options/locally_h_refine",
"Mesh/Refinement/locally_h_refine",
"0", h_refinement_function_string );
405 if (h_refinement_function_string !=
"0")
407 libMesh::ParsedFunction<libMesh::Real>
408 h_refinement_function(h_refinement_function_string);
410 libMesh::MeshRefinement mesh_refinement(mesh);
412 libMesh::dof_id_type found_refinements = 0;
414 found_refinements = 0;
415 unsigned int max_level_refining = 0;
417 libMesh::MeshBase::element_iterator elem_it =
418 mesh.active_elements_begin();
419 libMesh::MeshBase::element_iterator elem_end =
420 mesh.active_elements_end();
421 for (; elem_it != elem_end; ++elem_it)
423 libMesh::Elem *elem = *elem_it;
425 const libMesh::Real refinement_val =
426 h_refinement_function(elem->centroid());
428 const unsigned int n_refinements = refinement_val > 0 ?
431 if (elem->level() - uniformly_refine < n_refinements)
433 elem->set_refinement_flag(libMesh::Elem::REFINE);
435 max_level_refining = std::max(max_level_refining,
440 comm.max(found_refinements);
441 comm.max(max_level_refining);
443 if (found_refinements)
445 std::cout <<
"Found up to " << found_refinements <<
446 " elements to refine on each processor," << std::endl;
447 std::cout <<
"with max level " << max_level_refining << std::endl;
448 mesh_refinement.refine_and_coarsen_elements();
450 if( input.have_variable(
"restart-options/restart_file") )
452 std::cout <<
"Warning: it is known that locally_h_refine is broken when restarting." << std::endl
453 <<
" and multiple refinement passes are done. We are forcibly" << std::endl
454 <<
" limiting the refinement to one pass until this issue is resolved." << std::endl;
459 }
while(found_refinements);
MeshBuilder()
This Object handles building a libMesh::UnstructuredMesh subclass.
std::tr1::shared_ptr< libMesh::UnstructuredMesh > build(const GetPot &input, const libMesh::Parallel::Communicator &comm LIBMESH_CAN_DEFAULT_TO_COMMWORLD)
Builds the libMesh::Mesh according to input options.
void generate_mesh(const std::string &mesh_build_type, const GetPot &input, libMesh::UnstructuredMesh *mesh)
void do_mesh_refinement_from_input(const GetPot &input, const libMesh::Parallel::Communicator &comm, libMesh::UnstructuredMesh &mesh) const
Refine the mesh based on user input parameters.
void deprecated_option(const GetPot &input, const std::string &old_option, const std::string &new_option, const T &default_value, T &option_value) const
Helper function for displaying deprecated warnings.