Nektar++
CouplingFile.cpp
Go to the documentation of this file.
1 ////////////////////////////////////////////////////////////////////////////////
2 //
3 // File: CouplingFile.cpp
4 //
5 // For more information, please see: http://www.nektar.info/
6 //
7 // The MIT License
8 //
9 // Copyright (c) 2017 Kilian Lackhove
10 //
11 // Permission is hereby granted, free of charge, to any person obtaining a
12 // copy of this software and associated documentation files (the "Software"),
13 // to deal in the Software without restriction, including without limitation
14 // the rights to use, copy, modify, merge, publish, distribute, sublicense,
15 // and/or sell copies of the Software, and to permit persons to whom the
16 // Software is furnished to do so, subject to the following conditions:
17 //
18 // The above copyright notice and this permission notice shall be included
19 // in all copies or substantial portions of the Software.
20 //
21 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
22 // OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
23 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
24 // THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
25 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
26 // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
27 // DEALINGS IN THE SOFTWARE.
28 //
29 // Description: CWIPI Exchange class
30 //
31 ////////////////////////////////////////////////////////////////////////////////
32 
33 #include "CouplingFile.h"
34 
39 
40 #include <boost/format.hpp>
41 
42 namespace Nektar
43 {
44 namespace SolverUtils
45 {
46 
47 using namespace std;
48 
49 std::string CouplingFile::className =
50  GetCouplingFactory().RegisterCreatorFunction("File", CouplingFile::create,
51  "File Coupling");
52 
54  : Coupling(field), m_lastSend(-1E6), m_lastReceive(-1E6)
55 {
56  m_config["RECEIVEFUNCTION"] = "CouplingIn";
57  m_config["SENDFILENAME"] = "CouplingOut_%14.8E.pts";
58 }
59 
61 {
62 }
63 
65 {
67 
68  if (m_nRecvVars > 0 && m_recvSteps > 0)
69  {
71  m_evalField->GetSession(), m_evalField, m_config["RECEIVEFUNCTION"],
72  true);
73  }
74 }
75 
77  const int step, const NekDouble time,
78  const Array<OneD, const Array<OneD, NekDouble>> &field,
79  vector<string> &varNames)
80 {
81  if (m_nSendVars < 1 || m_sendSteps < 1)
82  {
83  return;
84  }
85 
86  if (step < m_lastSend + m_sendSteps)
87  {
88  return;
89  }
90  m_lastSend = step;
91 
92  if (m_evalField->GetComm()->GetRank() == 0 &&
93  m_evalField->GetSession()->DefinesCmdLineArgument("verbose"))
94  {
95  cout << "sending fields at i = " << step << ", t = " << time << endl;
96  }
97 
98  vector<int> sendVarsToVars =
100 
101 #if (defined _WIN32 && _MSC_VER < 1900)
102  // We need this to make sure boost::format has always
103  // two digits in the exponents of Scientific notation.
104  unsigned int old_exponent_format;
105  old_exponent_format = _set_output_format(_TWO_DIGIT_EXPONENT);
106  std::string filename =
107  boost::str(boost::format(m_config["SENDFILENAME"]) % time);
108  _set_output_format(old_exponent_format);
109 #else
110  std::string filename =
111  boost::str(boost::format(m_config["SENDFILENAME"]) % time);
112 #endif
113 
115  for (int i = 0; i < 3; ++i)
116  {
117  pts[i] = Array<OneD, NekDouble>(m_evalField->GetTotPoints(), 0.0);
118  }
119  m_evalField->GetCoords(pts[0], pts[1], pts[2]);
120 
121  for (int i = 0; i < m_nSendVars; ++i)
122  {
123  pts[3 + i] = field[sendVarsToVars[i]];
124  }
125 
126  LibUtilities::PtsIO ptsIO(m_evalField->GetSession()->GetComm());
129  3, m_sendFieldNames, pts);
130  // we first write to a temp file and rename this to make sure the
131  // receiver doesnt try to read it before we finished writing
132  string tmpFn = filename + ".tmp";
133  ptsIO.Write(tmpFn, sPts);
134  fs::rename(tmpFn, filename);
135 }
136 
137 void CouplingFile::v_Receive(const int step, const NekDouble time,
139  vector<string> &varNames)
140 {
141  if (m_nRecvVars < 1 || m_recvSteps < 1)
142  {
143  return;
144  }
145 
146  if (step < m_lastReceive + m_recvSteps)
147  {
148  return;
149  }
150  m_lastReceive = step;
151 
152  if (m_evalField->GetComm()->GetRank() == 0 &&
153  m_evalField->GetSession()->DefinesCmdLineArgument("verbose"))
154  {
155  cout << "receiving fields at i = " << step << ", t = " << time << endl;
156  }
157 
158  string filename = m_evalField->GetSession()->GetFunctionFilename(
159  m_config["RECEIVEFUNCTION"], m_recvFieldNames[0]);
160 
161 #if (defined _WIN32 && _MSC_VER < 1900)
162  // We need this to make sure boost::format has always
163  // two digits in the exponents of Scientific notation.
164  unsigned int old_exponent_format;
165  old_exponent_format = _set_output_format(_TWO_DIGIT_EXPONENT);
166  filename = boost::str(boost::format(filename) % time);
167  _set_output_format(old_exponent_format);
168 #else
169  filename = boost::str(boost::format(filename) % time);
170 #endif
171 
172  int exists = 0;
173  while (!exists)
174  {
175  exists = fs::exists(filename);
176  m_evalField->GetComm()->AllReduce(exists, LibUtilities::ReduceMin);
177  }
178 
180  m_inputFunction->Evaluate(m_recvFieldNames, recvFields, time);
181 
182  vector<int> recvVarsToVars =
184  ASSERTL1(m_nRecvVars == recvVarsToVars.size(), "field size mismatch");
185  for (int i = 0; i < recvVarsToVars.size(); ++i)
186  {
187  Vmath::Vcopy(recvFields[i].size(), recvFields[i], 1,
188  field[recvVarsToVars[i]], 1);
189  }
190 }
191 } // namespace SolverUtils
192 } // namespace Nektar
#define ASSERTL1(condition, msg)
Assert Level 1 – Debugging which is used whether in FULLDEBUG or DEBUG compilation mode....
Definition: ErrorUtil.hpp:249
void Write(const std::string &outFile, const PtsFieldSharedPtr &ptsField, const bool backup=false)
Save a pts field to a file.
Definition: PtsIO.cpp:132
static std::shared_ptr< DataType > AllocateSharedPtr(const Args &...args)
Allocate a shared pointer from the memory pool.
virtual SOLVER_UTILS_EXPORT void v_Send(const int step, const NekDouble time, const Array< OneD, const Array< OneD, NekDouble >> &field, std::vector< std::string > &varNames) override
SessionFunctionSharedPtr m_inputFunction
Definition: CouplingFile.h:84
virtual SOLVER_UTILS_EXPORT void v_Init() override
virtual SOLVER_UTILS_EXPORT ~CouplingFile()
virtual SOLVER_UTILS_EXPORT void v_Receive(const int step, const NekDouble time, Array< OneD, Array< OneD, NekDouble >> &field, std::vector< std::string > &varNames) override
SOLVER_UTILS_EXPORT CouplingFile(MultiRegions::ExpListSharedPtr field)
static SOLVER_UTILS_EXPORT CouplingSharedPtr create(MultiRegions::ExpListSharedPtr field)
Creates an instance of this class.
Definition: CouplingFile.h:54
virtual SOLVER_UTILS_EXPORT void v_Init()
Definition: Coupling.cpp:61
MultiRegions::ExpListSharedPtr m_evalField
Definition: Coupling.h:113
std::vector< std::string > m_sendFieldNames
Definition: Coupling.h:116
CouplingConfigMap m_config
Definition: Coupling.h:111
std::vector< std::string > m_recvFieldNames
Definition: Coupling.h:120
SOLVER_UTILS_EXPORT std::vector< int > GenerateVariableMapping(std::vector< std::string > &vars, std::vector< std::string > &transVars)
Definition: Coupling.cpp:134
std::shared_ptr< PtsField > PtsFieldSharedPtr
Definition: PtsField.h:190
std::shared_ptr< ExpList > ExpListSharedPtr
Shared pointer to an ExpList object.
CouplingFactory & GetCouplingFactory()
Declaration of the Coupling factory singleton.
Definition: Coupling.cpp:44
The above copyright notice and this permission notice shall be included.
Definition: CoupledSolver.h:2
double NekDouble
void Vcopy(int n, const T *x, const int incx, T *y, const int incy)
Definition: Vmath.cpp:1255