G+Smo  24.08.0
Geometry + Simulation Modules
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
gsPreCICE.h
Go to the documentation of this file.
1 
14 #pragma once
15 
16 #include <gsCore/gsConfig.h>
17 #include <precice/SolverInterface.hpp>
18 
19 namespace gismo {
20 
21 template<class T>
22 class gsPreCICE
23 {
24 public:
25 
29  gsPreCICE()
30  :
31  m_interface("participantName", "configurationFileName", 0, 1)
32  {
33  // #ifdef GISMO_WITH_MPI --> rank and size
34  // m_interface = precice::SolverInterface( "participantName", "configurationFileName", 0, 1 ); // last arguments rank, size
35  }
36 
45  gsPreCICE(std::string participantName, std::string configurationFileName)
46  :
47  m_interface(participantName, configurationFileName, 0, 1) // last arguments rank, size
48  {
49  }
50 
52  bool isCouplingOngoing() const { return m_interface.isCouplingOngoing(); }
54  bool isReadDataAvailable() const { return m_interface.isReadDataAvailable(); }
56  bool isWriteDataRequired(real_t computedTimestepLength) const { return m_interface.isWriteDataRequired(computedTimestepLength); }
58  bool isTimeWindowComplete() const { return m_interface.isTimeWindowComplete(); }
60  bool isActionRequired(const std::string &action) const { return m_interface.isActionRequired(action); }
62  void markActionFulfilled(const std::string &action) { m_interface.markActionFulfilled(action); }
63 
64  // TODO: These functions are precice constants and are preferably called outside of the class
66  const std::string actionWriteInitialData() { return precice::constants::actionWriteInitialData(); }
68  const std::string actionWriteIterationCheckpoint() { return precice::constants::actionWriteIterationCheckpoint(); }
70  const std::string actionReadIterationCheckpoint() { return precice::constants::actionReadIterationCheckpoint(); }
71 
72 
80  T initialize()
81  {
82  m_precicedt = m_interface.initialize();
83  return m_precicedt;
84  }
85 
86 
93  void addMesh(const std::string & meshName, const gsMatrix<T> & points)
94  {
95  m_meshNames.push_back(meshName);
96  m_meshIDs.push_back(m_interface.getMeshID(m_meshNames.back()));
97 
98  gsMatrix<T> pointsTranspose = points;
99  pointsTranspose.blockTransposeInPlace(1);
100  m_sizes.push_back(points.cols());
101  m_positions.push_back(pointsTranspose.data());
102 
103  index_t * vertexIDs;
104  m_interface.setMeshVertices(m_meshIDs.back(),m_sizes.back(),m_positions.back(),vertexIDs);
105  m_vertexIDs.push_back(vertexIDs);
106  }
107 
118  T advance(T dt)
119  {
120  m_precicedt = m_interface.advance(dt);
121  return m_precicedt;
122  }
123 
125  void finalize()
126  {
127  m_interface.finalize();
128  }
129 
138  void readBlockScalarData(const index_t & dataID, const index_t & size, const index_t * IDs, gsMatrix<T> & values) const
139  {
140  T * values_ptr = new T[size];
141  m_interface.readBlockScalarData(dataID,size,IDs,values_ptr);
142 
143  values = Eigen::Map<typename gsMatrix<T>::Base>(values_ptr,1,size);
144  }
145 
154  void readBlockScalarData(const index_t & meshID, const index_t & dataID, const gsMatrix<T> & coords, gsMatrix<T> & values) const
155  {
156  index_t * IDs = new index_t[coords.cols()];
157  this->getMeshVertexIDsFromPositions(meshID,coords,IDs);
158 
159  this->readBlockScalarData(dataID,coords.cols(),IDs,values);
160  }
161 
162  // void readBlockVectorData(const index_t & dataID, const index_t & size, const index_t * IDs, gsMatrix<T> & values) const
163  // {
164  // T * values_ptr = new T[size];
165  // m_interface.readBlockVectorData(dataID,size,IDs,values_ptr);
166  // index_t rows =
167  // values = Eigen::Map<typename gsMatrix<T>::Base>(values_ptr,1,size);
168 
169  // // CAST VALUES TO gsMATRIX
170  // }
171 
172  // void readBlockVectorData(const index_t & meshID, const index_t & dataID, const gsMatrix<T> & coords, gsMatrix<T> & values) const
173  // {
174  // index_t * IDs = new index_t[coords.cols()];
175  // this->getMeshVertexIDsFromPositions(meshID,coords,IDs);
176 
177  // this->readBlockVectorData(dataID,coords.cols(),IDs,values);
178  // }
179 
188  void writeBlockScalarData(const index_t & dataID, const index_t & size, const index_t * IDs, const gsMatrix<T> & values)
189  {
190  m_interface.writeBlockScalarData(dataID,size,IDs,values.data());
191  }
192 
201  void writeBlockScalarData(const index_t & meshID, const index_t & dataID, const gsMatrix<T> & coords, const gsMatrix<T> & values)
202  {
203  index_t * IDs = new index_t[coords.cols()];
204  this->getMeshVertexIDsFromPositions(meshID,coords,IDs);
205 
206  this->writeBlockScalarData(dataID,coords.cols(),IDs,values);
207  }
208 
209  // void writeBlockVectorData(const index_t & dataID, const index_t & size, const index_t * IDs, const gsMatrix<T> & values) const
210  // {
211  // m_interface.writeBlockVectorData(dataID,size,IDs,values.data());
212  // }
213 
214  // void writeBlockVectorData(const index_t & meshID, const index_t & dataID, const gsMatrix<T> & coords, const gsMatrix<T> & values) const
215  // {
216  // index_t * IDs = new index_t[coords.cols()];
217  // this->getMeshVertexIDsFromPositions(meshID,coords,IDs);
218 
219  // this->writeBlockVectorData(dataID,coords.cols(),IDs,values);
220  // }
221 
229  void getMeshVertexIDsFromPositions(const index_t & meshID, const gsMatrix<T> & coords, gsMatrix<index_t> & IDs) const
230  {
231  index_t * ID_ptr;
232  this->getMeshVertexIDsFromPositions(meshID,coords,ID_ptr);
233  for (index_t k=0; k!=coords.cols(); k++)
234  gsDebugVar(ID_ptr[k]);
235 
236  IDs = Eigen::Map<typename gsMatrix<index_t>::Base>(ID_ptr,1,coords.cols());
237  }
238 
246  void getMeshVertexIDsFromPositions(const index_t & meshID, const gsMatrix<T> & coords, index_t * ID_ptr) const
247  {
248  gsMatrix<T> coordsTranspose = coords;
249  coordsTranspose.blockTransposeInPlace(1);
250  T * positions = coordsTranspose.data();
251  m_interface.getMeshVertexIDsFromPositions(meshID,coords.cols(),positions,ID_ptr);
252  }
253 
261  index_t getMeshID(const std::string &dataName) const
262  {
263  return m_interface.getMeshID(dataName);
264  }
265 
274  index_t getDataID(const std::string &dataName, int meshID) const
275  {
276  return m_interface.getDataID(dataName,meshID);
277  }
278 
280  virtual std::ostream &print(std::ostream &os) const
281  {
282  os << precice::getVersionInformation()<<"\n\n";
283  os << "Interface has the following meshes:\n";
284  for (index_t k=0; k!=m_meshNames.size(); k++)
285  gsInfo<<m_meshNames[k]<<"\t (ID="<<m_meshIDs[k]<<")\n";
286  return os;
287  }
288 
289 private:
291  std::vector<std::string> m_meshNames;
293  std::vector<index_t> m_meshIDs;
295  std::vector<index_t> m_sizes;
297  std::vector<T *> m_positions;
299  std::vector<index_t *> m_vertexIDs;
301  precice::SolverInterface m_interface;
303  T m_precicedt;
304 };
305 
306 } //namespace gismo
#define index_t
Definition: gsConfig.h:32
Provides preprocessor directives configuration of G+Smo.
#define gsInfo
Definition: gsDebug.h:43