19 #include "libmesh/libmesh_common.h" 20 #include "libmesh/libmesh_logging.h" 24 #include "libmesh/libmesh_version.h" 25 #include "libmesh/equation_systems.h" 26 #include "libmesh/mesh_base.h" 27 #include "libmesh/mesh_tools.h" 28 #include "libmesh/parallel.h" 29 #include "libmesh/xdr_cxx.h" 30 #include "libmesh/mesh_refinement.h" 44 std::string local_file_name (
const unsigned int processor_id,
45 std::string_view basename)
47 std::ostringstream returnval;
49 std::string_view suffix;
50 if (basename.size() - basename.rfind(
".bz2") == 4)
52 basename.remove_suffix(4);
55 else if (basename.size() - basename.rfind(
".gz") == 3)
57 basename.remove_suffix(3);
61 returnval << basename <<
'.';
62 returnval << std::setfill(
'0') << std::setw(4);
63 returnval << processor_id;
66 return returnval.str();
75 template <
typename InValType>
77 const unsigned int read_flags,
78 bool partition_agnostic)
81 if (
name.find(
".xdr") != std::string::npos)
83 this->
read(name, mode, read_flags, partition_agnostic);
88 template <
typename InValType>
91 const unsigned int read_flags,
92 bool partition_agnostic)
98 std::function<std::unique_ptr<Xdr>()> local_io_functor;
99 local_io_functor = [
this,&
name,&mode]() {
100 return std::make_unique<Xdr>(local_file_name(this->
processor_id(), name), mode); };
102 this->
read(io, local_io_functor, read_flags, partition_agnostic);
107 template <
typename InValType>
109 std::function<std::unique_ptr<Xdr>()> & local_io_functor,
110 const unsigned int read_flags,
111 bool partition_agnostic)
183 bool read_parallel_files =
false;
185 std::vector<std::pair<std::string, System *>> xda_systems;
192 std::string version =
"legacy";
193 if (!read_legacy_format)
200 const std::string libMesh_label =
"libMesh-";
201 std::string::size_type lm_pos = version.find(libMesh_label);
202 if (lm_pos==std::string::npos)
213 std::istringstream iss(version.substr(lm_pos + libMesh_label.size()));
214 int ver_major = 0, ver_minor = 0, ver_patch = 0;
216 iss >> ver_major >> dot >> ver_minor >> dot >> ver_patch;
217 io.
set_version(LIBMESH_VERSION_ID(ver_major, ver_minor, ver_patch));
220 read_parallel_files = (version.rfind(
" parallel") < version.size());
227 if (!(version.rfind(
" with infinite elements") < version.size()))
228 version +=
" with infinite elements";
232 libmesh_deprecated();
234 LOG_SCOPE(
"read()",
"EquationSystems");
238 unsigned int n_sys=0;
242 for (
unsigned int sys=0; sys<n_sys; sys++)
246 std::string sys_name;
252 std::string sys_type;
265 read_additional_data,
268 xda_systems.emplace_back(sys_name, &new_system);
289 std::unique_ptr<Xdr> local_io;
294 if (!read_legacy_format && partition_agnostic)
300 for (
auto & pr : xda_systems)
301 if (read_legacy_format)
303 libmesh_deprecated();
304 #ifdef LIBMESH_ENABLE_DEPRECATED 305 pr.second->read_legacy_data (io, read_additional_data);
309 if (read_parallel_files)
313 local_io = local_io_functor();
316 pr.second->read_parallel_data<InValType> (*local_io, read_additional_data);
319 pr.second->read_serialized_data<InValType> (io, read_additional_data);
323 if (!read_legacy_format && partition_agnostic)
330 #ifdef LIBMESH_ENABLE_AMR 339 const unsigned int write_flags,
340 bool partition_agnostic)
const 343 if (
name.find(
".xdr") != std::string::npos)
345 this->
write(name, mode, write_flags, partition_agnostic);
352 const unsigned int write_flags,
353 bool partition_agnostic)
const 357 std::unique_ptr<Xdr> local_io;
360 local_io = std::make_unique<Xdr>(local_file_name(this->
processor_id(),name), mode);
362 this->
write(io, write_flags, partition_agnostic, local_io.get());
368 const unsigned int write_flags,
369 bool partition_agnostic,
370 Xdr *
const local_io)
const 438 if (partition_agnostic)
450 const bool write_parallel_files =
464 if (write_parallel_files && write_data)
470 LOG_SCOPE(
"write()",
"EquationSystems");
474 unsigned int n_sys = 0;
476 if (!pr.second->hide_output())
480 io.
set_version(LIBMESH_VERSION_ID(LIBMESH_MAJOR_VERSION,
481 LIBMESH_MINOR_VERSION,
482 LIBMESH_MICRO_VERSION));
493 if (write_parallel_files) version +=
" parallel";
495 #ifdef LIBMESH_ENABLE_INFINITE_ELEMENTS 496 version +=
" with infinite elements";
498 io.
data (version,
"# File Format Identifier");
502 io.
data (n_sys,
"# No. of Equation Systems");
504 for (
auto & [sys_name, sys] :
_systems)
507 if (sys->hide_output())
continue;
512 const unsigned int sys_num = sys->number();
514 comment =
"# Name, System No. ";
515 comment += std::to_string(sys_num);
519 std::string copy = sys_name;
520 io.
data (copy, comment);
526 const unsigned int sys_num = sys->number();
527 std::string sys_type = sys->system_type();
529 comment =
"# Type, System No. ";
530 comment += std::to_string(sys_num);
532 io.
data (sys_type, comment);
537 sys->write_header (io, version, write_additional_data);
548 if (pr.second->hide_output())
continue;
551 if (write_parallel_files)
552 pr.second->write_parallel_data (*local_io,write_additional_data);
554 pr.second->write_serialized_data (io,write_additional_data);
567 if (partition_agnostic)
568 const_cast<MeshBase &
>(
_mesh).fix_broken_node_and_element_numbering();
575 template LIBMESH_EXPORT
void EquationSystems::read<Number> (
Xdr & io, std::function<std::unique_ptr<Xdr>()> & local_io_functor,
const unsigned int read_flags,
bool partition_agnostic);
576 template LIBMESH_EXPORT
void EquationSystems::read<Number> (std::string_view
name,
const unsigned int read_flags,
bool partition_agnostic);
577 template LIBMESH_EXPORT
void EquationSystems::read<Number> (std::string_view
name,
const XdrMODE mode,
const unsigned int read_flags,
bool partition_agnostic);
578 #ifdef LIBMESH_USE_COMPLEX_NUMBERS 579 template LIBMESH_EXPORT
void EquationSystems::read<Real> (
Xdr & io, std::function<std::unique_ptr<Xdr>()> & local_io_functor,
const unsigned int read_flags,
bool partition_agnostic);
580 template LIBMESH_EXPORT
void EquationSystems::read<Real> (std::string_view
name,
const unsigned int read_flags,
bool partition_agnostic);
581 template LIBMESH_EXPORT
void EquationSystems::read<Real> (std::string_view
name,
const XdrMODE mode,
const unsigned int read_flags,
bool partition_agnostic);
std::string name(const ElemQuality q)
This function returns a string containing some name for q.
void write(std::string_view name, const XdrMODE, const unsigned int write_flags=(WRITE_DATA), bool partition_agnostic=true) const
Write the systems to disk using the XDR data format.
void close()
Closes the file if it is open.
const Parallel::Communicator & comm() const
void clean_refinement_flags()
Sets the refinement flag to Elem::DO_NOTHING for each element in the mesh.
virtual void fix_broken_node_and_element_numbering()=0
There is no reason for a user to ever call this function.
The libMesh namespace provides an interface to certain functionality in the library.
const T_sys & get_system(std::string_view name) const
This is the MeshBase class.
MeshBase & _mesh
The mesh data structure.
XdrMODE
Defines an enum for read/write mode in Xdr format.
void data(T &a, std::string_view comment="")
Inputs or outputs a single value.
void set_basic_system_only()
Sets the system to be "basic only": i.e.
Implements (adaptive) mesh refinement algorithms for a MeshBase.
std::string get_io_compatibility_version()
Specifier for I/O file compatibility features.
void read_header(Xdr &io, std::string_view version, const bool read_header=true, const bool read_additional_data=true, const bool read_legacy_format=false)
Reads the basic data header for this System.
Manages consistently variables, degrees of freedom, and coefficient vectors.
void read(std::string_view name, const XdrMODE, const unsigned int read_flags=(READ_HEADER|READ_DATA), bool partition_agnostic=true)
Read & initialize the systems from disk using the XDR data format.
void broadcast(T &data, const unsigned int root_id=0, const bool identical_sizes=false) const
This class implements a C++ interface to the XDR (eXternal Data Representation) format.
void set_version(int ver)
Sets the version of the file that is being read.
const MeshBase & get_mesh() const
virtual void init()
Initialize all the systems.
virtual System & add_system(std::string_view system_type, std::string_view name)
Add the system of type system_type named name to the systems array.
processor_id_type processor_id() const
void update()
Updates local values for all the systems.
std::map< std::string, std::unique_ptr< System >, std::less<> > _systems
Data structure holding the systems.