Nektar++
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
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
42namespace Nektar::SolverUtils
43{
44
45std::string CouplingFile::className =
46 GetCouplingFactory().RegisterCreatorFunction("File", CouplingFile::create,
47 "File Coupling");
48
50 : Coupling(field), m_lastSend(-1E6), m_lastReceive(-1E6)
51{
52 m_config["RECEIVEFUNCTION"] = "CouplingIn";
53 m_config["SENDFILENAME"] = "CouplingOut_%14.8E.pts";
54}
55
57{
59
60 if (m_nRecvVars > 0 && m_recvSteps > 0)
61 {
63 m_evalField->GetSession(), m_evalField, m_config["RECEIVEFUNCTION"],
64 true);
65 }
66}
67
69 const int step, const NekDouble time,
71 std::vector<std::string> &varNames)
72{
73 if (m_nSendVars < 1 || m_sendSteps < 1)
74 {
75 return;
76 }
77
78 if (step < m_lastSend + m_sendSteps)
79 {
80 return;
81 }
82 m_lastSend = step;
83
84 if (m_evalField->GetComm()->GetRank() == 0 &&
85 m_evalField->GetSession()->DefinesCmdLineArgument("verbose"))
86 {
87 std::cout << "sending fields at i = " << step << ", t = " << time
88 << std::endl;
89 }
90
91 std::vector<int> sendVarsToVars =
93
94#if (defined _WIN32 && _MSC_VER < 1900)
95 // We need this to make sure boost::format has always
96 // two digits in the exponents of Scientific notation.
97 unsigned int old_exponent_format;
98 old_exponent_format = _set_output_format(_TWO_DIGIT_EXPONENT);
99 std::string filename =
100 boost::str(boost::format(m_config["SENDFILENAME"]) % time);
101 _set_output_format(old_exponent_format);
102#else
103 std::string filename =
104 boost::str(boost::format(m_config["SENDFILENAME"]) % time);
105#endif
106
108 for (int i = 0; i < 3; ++i)
109 {
110 pts[i] = Array<OneD, NekDouble>(m_evalField->GetTotPoints(), 0.0);
111 }
112 m_evalField->GetCoords(pts[0], pts[1], pts[2]);
113
114 for (int i = 0; i < m_nSendVars; ++i)
115 {
116 pts[3 + i] = field[sendVarsToVars[i]];
117 }
118
119 LibUtilities::PtsIO ptsIO(m_evalField->GetSession()->GetComm());
122 3, m_sendFieldNames, pts);
123 // we first write to a temp file and rename this to make sure the
124 // receiver doesnt try to read it before we finished writing
125 std::string tmpFn = filename + ".tmp";
126 ptsIO.Write(tmpFn, sPts);
127 fs::rename(tmpFn, filename);
128}
129
130void CouplingFile::v_Receive(const int step, const NekDouble time,
132 std::vector<std::string> &varNames)
133{
134 if (m_nRecvVars < 1 || m_recvSteps < 1)
135 {
136 return;
137 }
138
139 if (step < m_lastReceive + m_recvSteps)
140 {
141 return;
142 }
143 m_lastReceive = step;
144
145 if (m_evalField->GetComm()->GetRank() == 0 &&
146 m_evalField->GetSession()->DefinesCmdLineArgument("verbose"))
147 {
148 std::cout << "receiving fields at i = " << step << ", t = " << time
149 << std::endl;
150 }
151
152 std::string filename = m_evalField->GetSession()->GetFunctionFilename(
153 m_config["RECEIVEFUNCTION"], m_recvFieldNames[0]);
154
155#if (defined _WIN32 && _MSC_VER < 1900)
156 // We need this to make sure boost::format has always
157 // two digits in the exponents of Scientific notation.
158 unsigned int old_exponent_format;
159 old_exponent_format = _set_output_format(_TWO_DIGIT_EXPONENT);
160 filename = boost::str(boost::format(filename) % time);
161 _set_output_format(old_exponent_format);
162#else
163 filename = boost::str(boost::format(filename) % time);
164#endif
165
166 int exists = 0;
167 while (!exists)
168 {
169 exists = fs::exists(filename);
170 m_evalField->GetComm()->AllReduce(exists, LibUtilities::ReduceMin);
171 }
172
174 m_inputFunction->Evaluate(m_recvFieldNames, recvFields, time);
175
176 std::vector<int> recvVarsToVars =
178 ASSERTL1(m_nRecvVars == recvVarsToVars.size(), "field size mismatch");
179 for (int i = 0; i < recvVarsToVars.size(); ++i)
180 {
181 Vmath::Vcopy(recvFields[i].size(), recvFields[i], 1,
182 field[recvVarsToVars[i]], 1);
183 }
184}
185} // namespace Nektar::SolverUtils
#define ASSERTL1(condition, msg)
Assert Level 1 – Debugging which is used whether in FULLDEBUG or DEBUG compilation mode....
Definition: ErrorUtil.hpp:242
void Write(const std::string &outFile, const PtsFieldSharedPtr &ptsField, const bool backup=false)
Save a pts field to a file.
Definition: PtsIO.cpp:130
static std::shared_ptr< DataType > AllocateSharedPtr(const Args &...args)
Allocate a shared pointer from the memory pool.
SessionFunctionSharedPtr m_inputFunction
Definition: CouplingFile.h:84
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
SOLVER_UTILS_EXPORT void v_Init() override
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:57
MultiRegions::ExpListSharedPtr m_evalField
Definition: Coupling.h:107
std::vector< std::string > m_sendFieldNames
Definition: Coupling.h:110
CouplingConfigMap m_config
Definition: Coupling.h:105
std::vector< std::string > m_recvFieldNames
Definition: Coupling.h:114
SOLVER_UTILS_EXPORT std::vector< int > GenerateVariableMapping(std::vector< std::string > &vars, std::vector< std::string > &transVars)
Definition: Coupling.cpp:131
std::shared_ptr< PtsField > PtsFieldSharedPtr
Definition: PtsField.h:184
std::shared_ptr< ExpList > ExpListSharedPtr
Shared pointer to an ExpList object.
CouplingFactory & GetCouplingFactory()
Declaration of the Coupling factory singleton.
Definition: Coupling.cpp:40
double NekDouble
void Vcopy(int n, const T *x, const int incx, T *y, const int incy)
Definition: Vmath.hpp:825