@@ -81,11 +81,6 @@ void EquationSystems::read (std::string_view name,
8181 if (name .find (".xdr" ) != std ::string ::npos )
8282 mode = DECODE ;
8383 this -> read (name , mode , read_flags , partition_agnostic );
84-
85- #ifdef LIBMESH_ENABLE_AMR
86- MeshRefinement mesh_refine (_mesh );
87- mesh_refine .clean_refinement_flags ();
88- #endif
8984}
9085
9186
@@ -96,58 +91,24 @@ void EquationSystems::read (std::string_view name,
9691 const unsigned int read_flags ,
9792 bool partition_agnostic )
9893{
99- // If we have exceptions enabled we can be considerate and try
100- // to read old restart files which contain infinite element
101- // information but do not have the " with infinite elements"
102- // string in the version information.
103-
104- // First try the read the user requested
105- libmesh_try
106- {
107- this -> _read_impl < InValType > (name , mode , read_flags , partition_agnostic );
108- }
109-
110- // If that fails, try it again but explicitly request we look for infinite element info
111- libmesh_catch (...)
112- {
113- libMesh ::out << "\n*********************************************************************\n"
114- << "READING THE FILE \"" << name << "\" FAILED.\n"
115- << "It is possible this file contains infinite element information,\n"
116- << "but the version string does not contain \" with infinite elements\"\n"
117- << "Let's try this again, but looking for infinite element information...\n"
118- << "*********************************************************************\n"
119- << std ::endl ;
120-
121- libmesh_try
122- {
123- this -> _read_impl < InValType > (name , mode , read_flags | EquationSystems ::TRY_READ_IFEMS , partition_agnostic );
124- }
94+ // This will unzip a file with .bz2 as the extension, otherwise it
95+ // simply returns the name if the file need not be unzipped.
96+ Xdr io ((this -> processor_id () == 0 ) ? std ::string (name ) : "" , mode );
12597
126- // If all that failed, we are out of ideas here...
127- libmesh_catch (...)
128- {
129- libMesh ::out << "\n*********************************************************************\n"
130- << "Well, at least we tried!\n"
131- << "Good Luck!!\n"
132- << "*********************************************************************\n"
133- << std ::endl ;
134- libmesh_error ();
135- }
136- }
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 ); };
137101
138- #ifdef LIBMESH_ENABLE_AMR
139- MeshRefinement mesh_refine (_mesh );
140- mesh_refine .clean_refinement_flags ();
141- #endif
102+ this -> read (io , local_io_functor , read_flags , partition_agnostic );
142103}
143104
144105
145106
146107template < typename InValType >
147- void EquationSystems ::_read_impl ( std :: string_view name ,
148- const XdrMODE mode ,
149- const unsigned int read_flags ,
150- bool partition_agnostic )
108+ void EquationSystems ::read ( Xdr & io ,
109+ std :: function < std :: unique_ptr < Xdr > ( ) > & local_io_functor ,
110+ const unsigned int read_flags ,
111+ bool partition_agnostic )
151112{
152113 /**
153114 * This program implements the output of an
@@ -223,9 +184,6 @@ void EquationSystems::_read_impl (std::string_view name,
223184
224185 std ::vector < std ::pair < std ::string , System * >> xda_systems ;
225186
226- // This will unzip a file with .bz2 as the extension, otherwise it
227- // simply returns the name if the file need not be unzipped.
228- Xdr io ((this -> processor_id () == 0 ) ? std ::string (name ) : "" , mode );
229187 libmesh_assert (io .reading ());
230188
231189 {
@@ -247,7 +205,7 @@ void EquationSystems::_read_impl (std::string_view name,
247205
248206 // Recursively call this read() function but with the
249207 // EquationSystems::READ_LEGACY_FORMAT bit set.
250- this -> read (name , mode , (read_flags | EquationSystems ::READ_LEGACY_FORMAT ), partition_agnostic );
208+ this -> read (io , local_io_functor , (read_flags | EquationSystems ::READ_LEGACY_FORMAT ), partition_agnostic );
251209 return ;
252210 }
253211
@@ -328,6 +286,8 @@ void EquationSystems::_read_impl (std::string_view name,
328286 // Read and set the numeric vector values
329287 if (read_data )
330288 {
289+ std ::unique_ptr < Xdr > local_io ;
290+
331291 // the EquationSystems::read() method should look constant from the mesh
332292 // perspective, but we need to assign a temporary numbering to the nodes
333293 // and elements in the mesh, which requires that we abuse const_cast
@@ -337,8 +297,6 @@ void EquationSystems::_read_impl (std::string_view name,
337297 MeshTools ::Private ::globally_renumber_nodes_and_elements (mesh );
338298 }
339299
340- Xdr local_io (read_parallel_files ? local_file_name (this -> processor_id (),name ) : "", mode );
341-
342300 for (auto & pr : xda_systems )
343301 if (read_legacy_format )
344302 {
@@ -349,7 +307,14 @@ void EquationSystems::_read_impl (std::string_view name,
349307 }
350308 else
351309 if (read_parallel_files )
352- pr .second -> read_parallel_data < InValType > (local_io , read_additional_data );
310+ {
311+ if (!local_io )
312+ {
313+ local_io = local_io_functor ();
314+ libmesh_assert (local_io -> reading ());
315+ }
316+ pr .second -> read_parallel_data < InValType > (* local_io , read_additional_data );
317+ }
353318 else
354319 pr .second -> read_serialized_data < InValType > (io , read_additional_data );
355320
@@ -361,6 +326,11 @@ void EquationSystems::_read_impl (std::string_view name,
361326
362327 // Localize each system's data
363328 this -> update ();
329+
330+ #ifdef LIBMESH_ENABLE_AMR
331+ MeshRefinement mesh_refine (_mesh );
332+ mesh_refine .clean_refinement_flags ();
333+ #endif
364334}
365335
366336
@@ -381,6 +351,23 @@ void EquationSystems::write(std::string_view name,
381351 const XdrMODE mode ,
382352 const unsigned int write_flags ,
383353 bool partition_agnostic ) const
354+ {
355+ Xdr io ((this -> processor_id ()== 0 ) ? std ::string (name ) : "" , mode );
356+
357+ std ::unique_ptr < Xdr > local_io ;
358+ // open a parallel buffer if warranted
359+ if (write_flags & EquationSystems ::WRITE_PARALLEL_FILES && write_flags & EquationSystems ::WRITE_DATA )
360+ local_io = std ::make_unique < Xdr > (local_file_name (this -> processor_id (),name ), mode );
361+
362+ this -> write (io , write_flags , partition_agnostic , local_io .get ());
363+ }
364+
365+
366+
367+ void EquationSystems ::write (Xdr & io ,
368+ const unsigned int write_flags ,
369+ bool partition_agnostic ,
370+ Xdr * const local_io ) const
384371{
385372 /**
386373 * This program implements the output of an
@@ -474,9 +461,10 @@ void EquationSystems::write(std::string_view name,
474461 // !this->get_mesh().is_serial())
475462 ;
476463
477- // New scope so that io will close before we try to zip the file
464+ if (write_parallel_files && write_data )
465+ libmesh_assert (local_io );
466+
478467 {
479- Xdr io ((this -> processor_id ()== 0 ) ? std ::string (name ) : "" , mode );
480468 libmesh_assert (io .writing ());
481469
482470 LOG_SCOPE ("write()" , "EquationSystems" );
@@ -554,21 +542,23 @@ void EquationSystems::write(std::string_view name,
554542 // to write vectors to disk, if wanted
555543 if (write_data )
556544 {
557- // open a parallel buffer if warranted.
558- Xdr local_io (write_parallel_files ? local_file_name (this -> processor_id (),name ) : "" , mode );
559-
560545 for (auto & pr : _systems )
561546 {
562547 // Ignore this system if it has been marked as hidden
563548 if (pr .second -> hide_output ()) continue ;
564549
565550 // 10.) + 11.)
566551 if (write_parallel_files )
567- pr .second -> write_parallel_data (local_io ,write_additional_data );
552+ pr .second -> write_parallel_data (* local_io ,write_additional_data );
568553 else
569554 pr .second -> write_serialized_data (io ,write_additional_data );
570555 }
556+
557+ if (local_io )
558+ local_io -> close ();
571559 }
560+
561+ io .close ();
572562 }
573563
574564 // the EquationSystems::write() method should look constant,
@@ -582,13 +572,13 @@ void EquationSystems::write(std::string_view name,
582572
583573// template specialization
584574
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 );
585576template LIBMESH_EXPORT void EquationSystems ::read < Number > (std ::string_view name , const unsigned int read_flags , bool partition_agnostic );
586577template LIBMESH_EXPORT void EquationSystems ::read < Number > (std ::string_view name , const XdrMODE mode , const unsigned int read_flags , bool partition_agnostic );
587- template LIBMESH_EXPORT void EquationSystems ::_read_impl < Number > (std ::string_view name , const XdrMODE mode , const unsigned int read_flags , bool partition_agnostic );
588578#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 );
589580template LIBMESH_EXPORT void EquationSystems ::read < Real > (std ::string_view name , const unsigned int read_flags , bool partition_agnostic );
590581template LIBMESH_EXPORT void EquationSystems ::read < Real > (std ::string_view name , const XdrMODE mode , const unsigned int read_flags , bool partition_agnostic );
591- template LIBMESH_EXPORT void EquationSystems ::_read_impl < Real > (std ::string_view name , const XdrMODE mode , const unsigned int read_flags , bool partition_agnostic );
592582#endif
593583
594584} // namespace libMesh
0 commit comments