MFEM  v4.4.0
Finite element discretization library
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Pages
adios2stream.hpp
Go to the documentation of this file.
1 // Copyright (c) 2010-2022, Lawrence Livermore National Security, LLC. Produced
2 // at the Lawrence Livermore National Laboratory. All Rights reserved. See files
3 // LICENSE and NOTICE for details. LLNL-CODE-806117.
4 //
5 // This file is part of the MFEM library. For more information and source code
6 // availability visit https://mfem.org.
7 //
8 // MFEM is free software; you can redistribute it and/or modify it under the
9 // terms of the BSD-3 license. We welcome feedback and contributions, see file
10 // CONTRIBUTING.md for details.
11 
12 // Created on: Jan 22, 2019
13 // Author: William F Godoy godoywf@ornl.gov
14 // adios2: Adaptable Input/Output System https://github.com/ornladios/ADIOS2
15 
16 #ifndef MFEM_ADIOS2STREAM
17 #define MFEM_ADIOS2STREAM
18 
19 #include "../config/config.hpp"
20 
21 #ifdef MFEM_USE_ADIOS2
22 
23 #include <map>
24 #include <memory> // std::unique_ptr
25 #include <string>
26 #include <set>
27 
28 #ifdef MFEM_USE_MPI
29 #include <mpi.h>
30 #endif
31 
32 #include <adios2.h>
33 
34 namespace mfem
35 {
36 
37 // forward declaring classes to avoid circular references
38 class Vector;
39 class GridFunction;
40 class Mesh;
41 class ADIOS2DataCollection;
42 
43 template <class T>
44 class Array;
45 class Element;
46 
48 {
49  friend class Vector;
50  friend class GridFunction;
51  friend class Mesh;
52  friend class ADIOS2DataCollection;
53 
54 public:
55  /**
56  * Open modes for adios2stream (from std::fstream)
57  * out: write
58  * in: read
59  * app: append
60  */
61  enum class openmode { out, in, app };
62 
63  /** Print and Save modes, deferred is done at Close or EndStep, sync is immediate */
64  enum class mode {sync, deferred};
65 
67 
68 #ifdef MFEM_USE_MPI
69 
70  /**
71  * adios2stream MPI constructor, allows for passing parameters in source
72  * code (compile-time) only.
73  * @param name stream name
74  * @param mode adios2stream::openmode::in (Read), adios2stream::openmode::out
75  * (Write)
76  * @param comm MPI communicator establishing domain for fstream
77  * @param engine_type available adios2 engine, default is BPFile
78  * see https://adios2.readthedocs.io/en/latest/engines/engines.html
79  * @throws std::invalid_argument (user input error) or std::runtime_error
80  * (system error)
81  */
82  adios2stream(const std::string& name, const openmode mode, MPI_Comm comm,
83  const std::string engine_type = "BPFile");
84 #else
85  /**
86  * adios2stream Non-MPI serial constructor, allows for passing parameters in
87  * source code (compile-time) only.
88  * @param name stream name
89  * @param mode adios2stream::openmode::in (Read), adios2stream::openmode::out
90  * (Write)
91  * @param engine_type available adios2 engine, default is BPFile
92  * @throws std::invalid_argument (user input error) or std::runtime_error
93  * (system error)
94  */
95  adios2stream(const std::string& name, const openmode mode,
96  const std::string engine_type = "BPFile");
97 #endif
98 
99  /** calls Close if stream is valid basically follows C++ RAII **/
100  virtual ~adios2stream();
101 
102  /**
103  * Set parameters for a particular adios2stream Engine
104  * See https://adios2.readthedocs.io/en/latest/engines/engines.html
105  * @param parameters map of key/value string elements
106  */
107  void SetParameters(const std::map<std::string, std::string>& parameters =
108  std::map<std::string, std::string>());
109 
110  /**
111  * Single parameter version of SetParameters passing a key/value pair
112  * See https://adios2.readthedocs.io/en/latest/engines/engines.html
113  * @param key input parameter key
114  * @param value input parameter value
115  */
116  void SetParameter(const std::string key, const std::string value) noexcept;
117 
118  /** Begins an I/O step */
119  void BeginStep();
120 
121  /** Ends the current step, by default transports the data */
122  void EndStep();
123 
124  /**
125  * Associates a physical time with the current I/O step as TIME variable
126  * @param time input physical time
127  */
128  void SetTime(const double time);
129 
130  /**
131  * Associates a current time step (cycle) with the current I/O step as CYCLE variable
132  * @param cycle physical time
133  */
134  void SetCycle(const int cycle);
135 
136  /**
137  * Input to the Global Geometry Refiner
138  * @param level input level
139  */
140  void SetRefinementLevel(const int level) noexcept;
141 
142  /** Return the current step between BeginStep and EndStep */
143  size_t CurrentStep() const;
144 
145  /** Finished interaction with adios2stream and flushes the data */
146  void Close();
147 
148 protected:
149 
150  /**
151  * Called from friend class Mesh (which is called from ParMesh)
152  * @param mesh input Mesh object to print
153  * @param print_mode sync: one at a time, deferred: collected (pre-fetch)
154  */
155  void Print(const Mesh& mesh, const adios2stream::mode print_mode = mode::sync);
156 
157  void Save(const GridFunction& grid_function, const std::string& variable_name,
158  const data_type type);
159 
160 private:
161  /** placeholder for engine name */
162  const std::string name;
163 
164  /** placeholder for engine openmode */
165  const openmode adios2_openmode;
166 
167  /** main adios2 object that owns all the io and engine components */
168  std::unique_ptr<adios2::ADIOS> adios;
169 
170  /** io object to set parameters, variables and engines */
171  adios2::IO io;
172 
173  /** heavy object doing system-level I/O operations */
174  adios2::Engine engine;
175 
176  /** true: transient problem (SetTime is called) */
177  bool transient = false;
178 
179  /** true : engine step is active after engine.BeginStep(),
180  * false: inactive after engine.EndStep() */
181  bool active_step = false;
182 
183  /** true: mesh is defined, false: not yet */
184  bool is_mesh_defined = false;
185 
186  /** ordering of the nodes to be passed to the schema as an attribute
187  * true: XXX YYY ZZZ, false: XYZ, XYZ, XYZ
188  * if true it must swap the vertices to Ordering::byDIM*/
189  bool ordering_by_node = false;
190 
191  /** true: refine solution at Save */
192  bool refine = true;
193 
194  /** refinement level at Save and Print */
195  int refinement_level = 1;
196 
197  /** save for point data */
198  size_t refined_mesh_nvertices = 0;
199 
200  /** save for cell data */
201  size_t refined_mesh_nelements = 0;
202 
203  /** saves the variable names representing point data */
204  std::set<std::string> point_data_variables;
205 
206  /**
207  * Map glvis element types to VTK element types
208  * @param glvisType input
209  * @return VTK element type
210  */
211  int32_t GLVISToVTKType(const int glvisType) const noexcept;
212 
213  /** sets the current vtk_schema from point data arrays to be parsed
214  * in VTK for Paraview visualization */
215  std::string VTKSchema() const noexcept;
216 
217  /**
218  * Checks if array of elements contains only constant types
219  * @param elements array input to check
220  * @return true: types are constant, false: mixed types
221  */
222  bool IsConstantElementType(const Array<Element*>& elements ) const noexcept;
223 
224  /**
225  * Maps to appropriate adios2::Mode from out, in to write, read
226  * @param mode
227  * @return
228  */
229  adios2::Mode ToADIOS2Mode(const adios2stream::openmode mode) const noexcept;
230 
231 };
232 
233 } // end namespace mfem
234 
235 #endif // MFEM_USE_ADIOS2
236 
237 #endif /* MFEM_ADIOS2STREAM */
void Save(const GridFunction &grid_function, const std::string &variable_name, const data_type type)
Class for grid function - Vector with associated FE space.
Definition: gridfunc.hpp:30
void SetTime(const double time)
void Print(const Mesh &mesh, const adios2stream::mode print_mode=mode::sync)
void SetParameter(const std::string key, const std::string value) noexcept
void SetRefinementLevel(const int level) noexcept
void SetCycle(const int cycle)
size_t CurrentStep() const
void SetParameters(const std::map< std::string, std::string > &parameters=std::map< std::string, std::string >())
Vector data type.
Definition: vector.hpp:60
adios2stream(const std::string &name, const openmode mode, MPI_Comm comm, const std::string engine_type="BPFile")
Abstract data type element.
Definition: element.hpp:28