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"
41 #include "libmesh/elem.h"
47 const libMesh::Parallel::Communicator &comm)
50 if( input.have_section(
"mesh-options/") &&
51 input.have_section(
"Mesh/") )
53 libmesh_error_msg(
"Error: Detected illegal simulataneous use of [mesh-options] and [Mesh] in input!");
59 if( !input.have_variable(
"mesh-options/mesh_option") &&
60 !input.have_section(
"Mesh/Read/") &&
61 !input.have_section(
"Mesh/Generation/") )
63 libmesh_error_msg(
"ERROR: Must specify either Mesh/Read or Mesh/Generation in input.");
67 if( input.have_section(
"Mesh/Read/") && input.have_section(
"Mesh/Generation/") )
69 libmesh_error_msg(
"ERROR: Can only specify one of Mesh/Read and Mesh/Generation");
73 std::string mesh_build_type =
"NULL";
74 if( input.have_section(
"Mesh/Read/") )
75 mesh_build_type =
"read";
77 else if( input.have_section(
"Mesh/Generation/") )
78 mesh_build_type =
"generate";
80 this->deprecated_option<std::string>( input,
"mesh-options/mesh_option",
"Mesh/Read or Mesh/Generation",
"DIE!", mesh_build_type);
84 if( mesh_build_type != std::string(
"generate") &&
85 mesh_build_type != std::string(
"read") &&
86 mesh_build_type != std::string(
"read_mesh_from_file") &&
87 mesh_build_type != std::string(
"create_1D_mesh") &&
88 mesh_build_type != std::string(
"create_2D_mesh") &&
89 mesh_build_type != std::string(
"create_3D_mesh") )
91 std::string error =
"ERROR: Invalid value of "+mesh_build_type+
" for Mesh/type.\n";
92 error +=
" Valid values are: generate\n";
94 libmesh_error_msg(error);
98 libMesh::UnstructuredMesh* mesh;
102 std::string mesh_class = input(
"Mesh/class",
"default");
104 this->deprecated_option<std::string>( input,
"mesh-options/mesh_class",
"Mesh/class",
"default", mesh_class);
106 if (mesh_class ==
"parallel")
107 mesh =
new libMesh::ParallelMesh(comm);
108 else if (mesh_class ==
"serial")
109 mesh =
new libMesh::SerialMesh(comm);
110 else if (mesh_class ==
"default")
111 mesh =
new libMesh::Mesh(comm);
114 std::string error =
"ERROR: Invalid class "+mesh_class+
" input for Mesh/class.\n";
115 error +=
" Valid choices are: serial, parallel.\n";
116 libmesh_error_msg(error);
121 if(mesh_build_type ==
"read_mesh_from_file" ||
122 mesh_build_type ==
"read" )
125 if( !input.have_variable(
"mesh-options/mesh_filename") &&
126 !input.have_variable(
"Mesh/Read/filename") )
128 libmesh_error_msg(
"ERROR: Must specify Mesh/Read/filename for reading mesh.");
131 std::string mesh_filename = input(
"Mesh/Read/filename",
"DIE!");
133 this->deprecated_option<std::string>( input,
"mesh-options/mesh_filename",
"Mesh/Read/filename",
"DIE!", mesh_filename);
138 mesh->read(mesh_filename);
142 bool all_second_order = input(
"Mesh/all_second_order",
false);
143 if (all_second_order)
144 mesh->all_second_order();
148 else if(mesh_build_type==
"create_1D_mesh" ||
149 mesh_build_type==
"create_2D_mesh" ||
150 mesh_build_type==
"create_3D_mesh" ||
151 mesh_build_type==
"generate")
153 this->generate_mesh(mesh_build_type,input,mesh);
165 if( !input.have_variable(
"restart-options/restart_file") )
167 this->do_mesh_refinement_from_input( input, comm, *mesh );
170 return SharedPtr<libMesh::UnstructuredMesh>(mesh);
174 libMesh::UnstructuredMesh* mesh )
176 unsigned int dimension = input(
"Mesh/Generation/dimension",0);
178 if( !input.have_variable(
"mesh-options/mesh_option") &&
179 !input.have_variable(
"Mesh/Generation/dimension") )
181 libmesh_error_msg(
"ERROR: Must specify Mesh/Generation/dimension for generating mesh.");
185 if( mesh_build_type ==
"create_1D_mesh" )
188 if( mesh_build_type==
"create_2D_mesh" )
191 if( mesh_build_type==
"create_3D_mesh" )
195 mesh->set_mesh_dimension(dimension);
199 libMesh::Real x_min = input(
"Mesh/Generation/x_min", 0.0);
200 this->
deprecated_option( input,
"mesh-options/domain_x1_min",
"Mesh/Generation/x_min", 0.0, x_min );
202 libMesh::Real x_max = input(
"Mesh/Generation/x_max", 1.0);
203 this->
deprecated_option( input,
"mesh-options/domain_x1_max",
"Mesh/Generation/x_max", 1.0, x_max );
209 libMesh::Real y_min = 0.0;
210 libMesh::Real y_max = 0.0;
214 y_min = input(
"Mesh/Generation/y_min", 0.0);
215 this->
deprecated_option( input,
"mesh-options/domain_x2_min",
"Mesh/Generation/y_min", 0.0, y_min );
217 y_max = input(
"Mesh/Generation/y_max", 1.0);
218 this->
deprecated_option( input,
"mesh-options/domain_x2_max",
"Mesh/Generation/y_max", 1.0, y_max );
224 libMesh::Real z_min = 0.0;
225 libMesh::Real z_max = 0.0;
229 z_min = input(
"Mesh/Generation/z_min", 0.0);
230 this->
deprecated_option( input,
"mesh-options/domain_x3_min",
"Mesh/Generation/z_min", 0.0, z_min );
232 z_max = input(
"Mesh/Generation/z_max", 1.0);
233 this->
deprecated_option( input,
"mesh-options/domain_x3_max",
"Mesh/Generation/z_max", 1.0, z_max );
239 if( !input.have_variable(
"mesh-options/mesh_nx1") &&
240 !input.have_variable(
"Mesh/Generation/n_elems_x") )
242 libmesh_error_msg(
"ERROR: Must supply Mesh/Generation/n_elems_x for mesh generation.");
245 unsigned int n_elems_x = input(
"Mesh/Generation/n_elems_x", 0);
246 this->deprecated_option<unsigned int>( input,
"mesh-options/mesh_nx1",
"Mesh/Generation/n_elems_x", 0, n_elems_x );
251 unsigned int n_elems_y = 0;
254 if( !input.have_variable(
"mesh-options/mesh_nx2") &&
255 !input.have_variable(
"Mesh/Generation/n_elems_y") )
257 libmesh_error_msg(
"ERROR: Must supply Mesh/Generation/n_elems_y for mesh generation.");
260 n_elems_y = input(
"Mesh/Generation/n_elems_y", 0);
261 this->deprecated_option<unsigned int>( input,
"mesh-options/mesh_nx2",
"Mesh/Generation/n_elems_y", 0, n_elems_y );
267 unsigned int n_elems_z = 0;
270 if( !input.have_variable(
"mesh-options/mesh_nx3") &&
271 !input.have_variable(
"Mesh/Generation/n_elems_z") )
273 libmesh_error_msg(
"ERROR: Must supply Mesh/Generation/n_elems_z for mesh generation.");
276 n_elems_z = input(
"Mesh/Generation/n_elems_z", 0);
277 this->deprecated_option<unsigned int>( input,
"mesh-options/mesh_nx3",
"Mesh/Generation/n_elems_z", 0, n_elems_z );
282 std::string element_type = input(
"Mesh/Generation/element_type",
"default");
283 this->deprecated_option<std::string>( input,
"mesh-options/element_type",
"Mesh/Generation/element_type",
"default", element_type );
288 if(element_type==
"default")
290 element_type =
"EDGE3";
293 GRINSEnums::ElemType element_enum_type =
294 libMesh::Utility::string_to_enum<GRINSEnums::ElemType>(element_type);
296 libMesh::MeshTools::Generation::build_line(*mesh,
303 else if( dimension == 2 )
305 if(element_type==
"default")
307 element_type =
"TRI6";
310 GRINSEnums::ElemType element_enum_type =
311 libMesh::Utility::string_to_enum<GRINSEnums::ElemType>(element_type);
313 libMesh::MeshTools::Generation::build_square(*mesh,
323 else if( dimension == 3 )
325 if(element_type==
"default")
327 element_type =
"TET10";
330 GRINSEnums::ElemType element_enum_type =
331 libMesh::Utility::string_to_enum<GRINSEnums::ElemType>(element_type);
333 libMesh::MeshTools::Generation::build_cube(*mesh,
356 const libMesh::Parallel::Communicator &comm,
357 libMesh::UnstructuredMesh& mesh )
const
359 std::string redistribution_function_string =
360 input(
"Mesh/Redistribution/function", std::string(
"0"));
361 this->deprecated_option<std::string>( input,
"mesh-options/redistribute",
"Mesh/Redistribution/function",
"0", redistribution_function_string );
363 if (redistribution_function_string !=
"0")
366 redistribution_function(redistribution_function_string);
368 libMesh::MeshTools::Modification::redistribute
369 (mesh, redistribution_function);
379 const libMesh::Elem *elem = *mesh.elements_begin();
381 if (elem->default_order() != libMesh::FIRST)
383 mesh.all_first_order();
384 mesh.all_second_order();
388 int uniformly_refine = input(
"Mesh/Refinement/uniformly_refine", 0);
389 this->
deprecated_option( input,
"mesh-options/uniformly_refine",
"Mesh/Refinement/uniformly_refine", 0, uniformly_refine );
391 if( uniformly_refine > 0 )
393 libMesh::MeshRefinement(mesh).uniformly_refine(uniformly_refine);
396 std::string h_refinement_function_string =
397 input(
"Mesh/Refinement/locally_h_refine", std::string(
"0"));
398 this->deprecated_option<std::string>( input,
"mesh-options/locally_h_refine",
"Mesh/Refinement/locally_h_refine",
"0", h_refinement_function_string );
400 if (h_refinement_function_string !=
"0")
403 h_refinement_function(h_refinement_function_string);
405 libMesh::MeshRefinement mesh_refinement(mesh);
407 libMesh::dof_id_type found_refinements = 0;
409 found_refinements = 0;
410 unsigned int max_level_refining = 0;
412 libMesh::MeshBase::element_iterator elem_it =
413 mesh.active_elements_begin();
414 libMesh::MeshBase::element_iterator elem_end =
415 mesh.active_elements_end();
416 for (; elem_it != elem_end; ++elem_it)
418 libMesh::Elem *elem = *elem_it;
420 const libMesh::Real refinement_val =
421 h_refinement_function(elem->centroid());
423 const unsigned int n_refinements = refinement_val > 0 ?
426 if (elem->level() - uniformly_refine < n_refinements)
428 elem->set_refinement_flag(libMesh::Elem::REFINE);
430 max_level_refining = std::max(max_level_refining,
435 comm.max(found_refinements);
436 comm.max(max_level_refining);
438 if (found_refinements)
440 std::cout <<
"Found up to " << found_refinements <<
441 " elements to refine on each processor," << std::endl;
442 std::cout <<
"with max level " << max_level_refining << std::endl;
443 mesh_refinement.refine_and_coarsen_elements();
445 if( input.have_variable(
"restart-options/restart_file") )
447 std::cout <<
"Warning: it is known that locally_h_refine is broken when restarting." << std::endl
448 <<
" and multiple refinement passes are done. We are forcibly" << std::endl
449 <<
" limiting the refinement to one pass until this issue is resolved." << std::endl;
454 }
while(found_refinements);
SharedPtr< 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.