9 #include <boost/algorithm/string.hpp>
10 #include <boost/algorithm/string/replace.hpp>
11 #include <boost/thread/thread.hpp>
53 Writer::Writer(
const std::string&
name )
57 .pretty_name(
"Serial Format")
58 .description(
"All processors write in 1 file")
100 std::vector<std::string> extensions;
101 extensions.push_back(
".msh");
102 extensions.push_back(
".gmsh");
111 boost::filesystem::fstream file;
113 path = path.parent_path() / boost::filesystem::path (boost::filesystem::basename(path) +
"_P" +
to_str(PE::Comm::instance().rank()) + boost::filesystem::extension(path));
114 file.open(path,std::ios_base::out);
117 throw boost::filesystem::filesystem_error( path.string() +
" failed to open",
118 boost::system::error_code() );
130 if (PE::Comm::instance().rank() == 0)
132 boost::filesystem::fstream parallel_file;
134 parallel_file.open(parallel_file_path,std::ios_base::out);
137 throw boost::filesystem::filesystem_error( parallel_file_path.string() +
" failed to open",
138 boost::system::error_code() );
141 for (
Uint r=0; r<PE::Comm::instance().size(); ++r)
144 rank_file_path = boost::filesystem::basename(rank_file_path) +
"_P" +
to_str(r) + boost::filesystem::extension(rank_file_path);
145 parallel_file <<
"Merge \"" << rank_file_path.string() <<
"\";" << std::endl;
147 parallel_file.close();
155 std::string version =
"2";
160 file <<
"$MeshFormat\n";
161 file << version <<
" " << file_type <<
" " << data_size <<
"\n";
162 file <<
"$EndMeshFormat\n";
167 Uint phys_name_counter(0);
168 std::vector< Handle<Region const> > phys_group_regions;
176 phys_group_regions.push_back(region);
183 phys_group_regions.push_back(phys_group_region.
handle<
Region>());
188 file <<
"$PhysicalNames\n";
189 file << phys_name_counter <<
"\n";
194 std::string
name = phys_group_region->uri().path();
195 boost::algorithm::replace_first(name,
m_mesh->topology().uri().path()+
"/",
"");
196 m_groupnumber[phys_group_region->uri().path()] = ++phys_name_counter;
198 Uint group_dimensionality(0);
202 file << group_dimensionality <<
" " << phys_name_counter <<
" \"" << name <<
"\"\n";
204 file <<
"$EndPhysicalNames\n";
212 Uint prec = file.precision();
220 const Uint nb_nodes = used_nodes.size();
223 file << nb_nodes <<
"\n";
231 file << geometry.
glb_idx()[node]+1 <<
" ";
232 for (
Uint d=0; d<3; d++)
235 file << coord[d] <<
" ";
242 file <<
"$EndNodes\n";
244 file.precision(prec);
264 file <<
"$Elements\n";
265 file << nb_elems <<
"\n";
266 std::string group_name(
"");
269 Uint number_of_tags=3;
270 Uint partition_number = PE::Comm::instance().rank();
272 Uint elementary_entity_index=1;
275 group_name = elements->parent()->uri().path();
277 elm_type =
m_elementTypes[elements->element_type().derived_type_name()];
278 const Connectivity& element_connectivity = elements->geometry_space().connectivity();
279 const Uint nb_elem = elements->size();
283 ghost = elements->is_ghost(
e);
286 file << elements->glb_idx()[
e]+1 <<
" " << elm_type <<
" " << number_of_tags <<
" " << group_number <<
" " << elementary_entity_index <<
" " << (ghost? -1 : partition_number);
289 file <<
" " << elements->geometry_fields().glb_idx()[node_idx]+1;
294 ++elementary_entity_index;
296 file <<
"$EndElements\n";
304 std::set< Handle<Dictionary> > dicts;
308 dicts.insert( field->dict().handle<
Dictionary>() );
312 std::vector<Uint> shape(11);
322 std::vector< Handle<ShapeFunction const> > shape_functions(11);
324 Uint nb_interpolation_schemes = 0;
328 if(
is_null( shape_functions[ shape[sf->shape()] ] ) )
330 shape_functions[ shape[sf->shape()] ] = sf;
331 ++nb_interpolation_schemes;
333 else if ( shape_functions[ shape[sf->shape()] ]->derived_type_name() != sf->derived_type_name() )
335 throw NotSupported(
FromHere(),
"Gmsh cannot support different interpolation schemes for the same element type within one field");
340 file <<
"$InterpolationScheme\n";
341 std::string space_lib_name;
344 std::string entities_path = entities->uri().path();
345 boost::erase_first(entities_path,
m_mesh->uri().path());
346 if(space_lib_name.empty())
348 std::string sf_name = entities->space(*dict).shape_function().derived_type_name();
349 std::vector<std::string> name_parts;
350 boost::split(name_parts, sf_name, boost::is_any_of(
"."));
351 space_lib_name = name_parts.front();
352 for(
Uint i = 1; i != name_parts.size()-1; ++i)
353 space_lib_name +=
"." + name_parts[i];
356 file <<
"\""<<dict->name() <<
" ["<< space_lib_name <<
"]\"\n";
357 file << nb_interpolation_schemes <<
"\n";
364 std::stringstream line;
365 line <<
"\n" << shape[ sf->shape() ] <<
"\n";
367 line << sf->mononomial_coefficients().rows() <<
" " << sf->mononomial_coefficients().rows() <<
"\n";
368 line << sf->mononomial_coefficients() <<
"\n";
369 line << sf->mononomial_exponents().rows() <<
" " << 3 <<
"\n";
370 for (
Uint i=0; i<sf->mononomial_exponents().rows(); ++i)
372 for (
Uint j=0; j<sf->mononomial_exponents().cols(); ++j)
373 line << sf->mononomial_exponents()(i,j) <<
" ";
374 for (
Uint j=sf->mononomial_exponents().cols(); j<3; ++j)
382 CFinfo <<
"Skipping not implemented interpolation scheme for shape function " << sf->derived_type_name() <<
CFendl;
386 file <<
"$EndInterpolationScheme\n";
413 Uint prec = file.precision();
419 const Field& field = *field_h;
424 const std::string field_name = field.
name();
426 std::string space_lib_name;
429 std::string entities_path = entities->uri().path();
430 boost::erase_first(entities_path,
m_mesh->uri().path());
431 if(space_lib_name.empty())
433 std::string sf_name = entities->space(field.
dict()).shape_function().derived_type_name();
434 std::vector<std::string> name_parts;
435 boost::split(name_parts, sf_name, boost::is_any_of(
"."));
436 space_lib_name = name_parts.front();
437 for(
Uint i = 1; i != name_parts.size()-1; ++i)
438 space_lib_name +=
"." + name_parts[i];
442 const std::string interpolation_scheme = field.
dict().
name() +
" ["+space_lib_name+
"]";
443 Uint nb_elements = 0;
448 nb_elements += elements_handle->size();
452 for(
Uint e=0;
e<elements_handle->size(); ++
e)
454 if (elements_handle->is_ghost(
e))
457 nb_elements -= nb_ghost;
466 std::string var_name = field.
var_name(iVar);
468 Uint datasize(var_type);
482 CFdebug <<
"Writing discontinuous field " << field.
uri() <<
" with " << nb_elements <<
" elements" <<
CFendl;
484 file <<
"$ElementNodeData\n";
488 file <<
"\"" << (var_name ==
"var" ? field_name+
to_str(iVar) : var_name) <<
"\"\n";
489 file <<
"\"" << interpolation_scheme <<
"\"\n";
490 file <<
"\"" << field_name <<
"\"\n";
492 file << 1 <<
"\n" << field_time <<
"\n";
494 file << 3 <<
"\n" << field_iter <<
"\n" << datasize <<
"\n" << nb_elements <<
"\n";
500 const Entities& elements = *elements_handle;
501 const Space& field_space = field.
space(elements);
502 Uint local_nb_elms = elements.
size();
505 RealVector field_data (static_cast<int>(var_type));
508 for (
Uint local_elm_idx = 0; local_elm_idx<local_nb_elms; ++local_elm_idx)
512 file << elements.
glb_idx()[local_elm_idx]+1 <<
" " << nb_sf_nodes <<
" ";
515 for (
Uint n=0;
n<nb_sf_nodes; ++
n)
517 for (
Uint j=0; j<var_type; ++j)
518 field_data[j] = field[field_indexes[
n]][row_idx+j];
522 data[0]=field_data[0];
523 data[1]=field_data[1];
524 data[3]=field_data[2];
525 data[4]=field_data[3];
527 file <<
" " << data[
idx];
531 for (
Uint j=0; j<var_type; ++j)
532 file <<
" " << field_data[j];
542 file <<
"$EndElementNodeData\n";
543 row_idx +=
Uint(var_type);
548 file.precision(prec);
583 Uint prec = file.precision();
590 const Field& field = *field_h;
594 const Field& field = *field_h;
597 const std::string field_name = field.
name();
598 std::string space_lib_name;
601 std::string entities_path = entities->uri().path();
602 boost::erase_first(entities_path,
m_mesh->uri().path());
603 if(space_lib_name.empty())
605 std::string sf_name = entities->space(field.
dict()).shape_function().derived_type_name();
606 std::vector<std::string> name_parts;
607 boost::split(name_parts, sf_name, boost::is_any_of(
"."));
608 space_lib_name = name_parts.front();
609 for(
Uint i = 1; i != name_parts.size()-1; ++i)
610 space_lib_name +=
"." + name_parts[i];
614 const std::string interpolation_scheme = field.
dict().
name() +
" ["+space_lib_name+
"]";
Uint nb_elements = 0;
615 std::vector< Handle<Entities const> > filtered_used_entities_by_field;
620 filtered_used_entities_by_field.push_back(elements_handle);
626 std::vector<bool> is_node_visited(
m_mesh->geometry_fields().size(),
false);
632 is_node_visited.assign(
m_mesh->geometry_fields().size(),
false);
634 std::string var_name = field.
var_name(iVar);
636 Uint datasize(var_type);
650 CFdebug <<
"Writing continuous field " << field.
uri() <<
" with " << field.
size() <<
" nodes" <<
CFendl;
652 file <<
"$NodeData\n";
656 file <<
"\"" << (var_name ==
"var" ? field_name+
to_str(iVar) : var_name) <<
"\"\n";
657 file <<
"\"" << interpolation_scheme <<
"\"\n";
658 file <<
"\"" << field_name <<
"\"\n";
660 file << 1 <<
"\n" << field_time <<
"\n";
662 file << 3 <<
"\n" << field_iter <<
"\n" << datasize <<
"\n" << used_nodes.size() <<
"\n";
666 const Space& field_space = elements_handle->space(field.
dict());
667 const Space& geom_space = elements_handle->geometry_space();
668 const Uint nb_elems = elements_handle->size();
671 RealMatrix field_data (nb_sf_nodes, static_cast<int>(var_type));
673 for (
Uint elem_idx=0; elem_idx<nb_elems; ++elem_idx)
682 for (
Uint iState=0; iState<nb_sf_nodes; ++iState)
684 for (
Uint j=0; j<var_type; ++j)
685 field_data(iState,j) = field[field_indexes[iState]][row_idx+j];
688 for (
Uint elem_node_idx=0; elem_node_idx<geom_space_nodes.size(); ++elem_node_idx)
690 const Uint geom_space_node = geom_space_nodes[elem_node_idx];
691 cf3_assert(geom_space_node < is_node_visited.size());
692 if (!is_node_visited[geom_space_node])
694 is_node_visited[geom_space_node]=
true;
706 file <<
m_mesh->geometry_fields().glb_idx()[geom_space_node]+1 <<
" ";
710 data[0]=node_data[0];
711 data[1]=node_data[1];
712 data[3]=node_data[2];
713 data[4]=node_data[3];
715 file <<
" " << data[
idx];
719 for (
Uint j=0; j<var_type; ++j)
720 file <<
" " << node_data[j];
730 file <<
"$EndNodeData\n";
731 row_idx +=
Uint(var_type);
736 file.precision(prec);
#define CFinfo
these are always defined
const ShapeFunction & shape_function() const
std::string name(ComponentWrapper &self)
void write_elem_nodal_data(std::fstream &file)
virtual std::vector< std::string > get_extensions()
std::string var_name(Uint i=0) const
const std::vector< Handle< Entities > > & entities_range() const
bool is_null(T ptr)
predicate for comparison to nullptr
Safe pointer to an object. This is the supported method for referring to components.
Helper class to create the Builder and place it in the factory.
Space & geometry_space() const
virtual const RealMatrix & local_coordinates() const =0
bool m_enable_overlap
If true, writing of overlap will be enabled.
Handle< Mesh const > m_mesh
Handle to configured mesh.
std::vector< Uint > used_nodes(const mesh::Region ®ion, const mesh::Dictionary &dict)
ElementType & element_type() const
return the elementType
URI uri() const
Construct the full path.
void write_coordinates(std::fstream &file)
bool is_ghost(const Uint idx) const
#define boost_foreach
lowercase version of BOOST_FOREACH
const std::string & name() const
Access the name of the component.
const Field & coordinates() const
bool defined_for_entities(const Handle< Entities const > &entities) const
RegionFilter m_region_filter
Filters regions.
common::URI m_file_path
File path to be configured.
std::map< std::string, Uint > m_elementTypes
Entities & support() const
Access the geometric support.
Conversions from and to std::string.
Real max(const Real a, const Real b)
Maximum between two scalars.
Real e()
Definition of the Unit charge [C].
void write_interpolation_schemes(std::fstream &file)
Dictionary & dict() const
Common_API std::string to_str(const T &v)
Converts to std::string.
Eigen::Matrix< Real, Eigen::Dynamic, Eigen::Dynamic > RealMatrix
Dynamic sized matrix of Real scalars.
void write_header(std::fstream &file)
Uint dimensionality() const
Uint size() const
return the number of elements
common::List< Uint > & glb_idx()
Mutable access to the list of nodes.
PropertyList & properties()
TYPE value(const std::string &pname) const
Eigen::Matrix< Real, Eigen::Dynamic, 1 > RealVector
Dynamic sized column vector.
std::vector< Handle< Region const > > m_regions
Handle to configured regions.
const Handle< Space const > & space(const Handle< Entities const > &entities) const
TableConstRow< ValueT >::type ConstRow
the const type of a row in the internal structure of the table
VarType var_length(const std::string &vname) const
Return the length (in number of Real values occupied in the data row) of the variable of the given na...
Top-level namespace for coolfluid.
virtual Uint nb_nodes() const =0
common::List< Uint > & glb_idx()
Return the global index of every field row.
boost::proto::terminal< SFOp< CoordinatesOp > >::type const coordinates
unsigned int Uint
typedef for unsigned int
EntitiesFilter m_entities_filter
Filters entities.
virtual RealRowVector value(const RealVector &local_coordinate) const =0
Compute the shape function values in the given local coordinate.
boost::shared_ptr< List< Uint > > build_used_nodes_list(const std::vector< Handle< Entities const > > &entities_vector, const Dictionary &dictionary, const bool include_ghost_elems, const bool follow_periodic_links)
Create a List containing unique entries of all the nodes used by a vector of entities...
Handle< Component > handle()
Get a handle to the component.
std::vector< Handle< Field const > > m_fields
Handle to configured fields.
SelectOptionType< T >::type & add(const std::string &name, const T &default_value=T())
common::ComponentBuilder< gmsh::Writer, MeshWriter, LibGmsh > aGmshWriter_Builder
std::vector< Handle< Entities const > > m_filtered_entities
Handle to selected entities.
Most basic kernel library.
Connectivity & connectivity()
connectivity table to dictionary entries
void write_nodal_data(std::fstream &file)
bool is_not_null(T ptr)
predicate for comparison to nullptr
std::map< std::string, Uint > m_groupnumber
void write_connectivity(std::fstream &file)
bool discontinuous() const