CouplingFile.cpp 6.21 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36
////////////////////////////////////////////////////////////////////////////////
//
// File: CouplingFile.cpp
//
// For more information, please see: http://www.nektar.info/
//
// The MIT License
//
// Copyright (c) 2017 Kilian Lackhove
//
// License for the specific language governing rights and limitations under
// Permission is hereby granted, free of charge, to any person obtaining a
// copy of this software and associated documentation files (the "Software"),
// to deal in the Software without restriction, including without limitation
// the rights to use, copy, modify, merge, publish, distribute, sublicense,
// and/or sell copies of the Software, and to permit persons to whom the
// Software is furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included
// in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
// THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
// DEALINGS IN THE SOFTWARE.
//
// Description: CWIPI Exchange class
//
////////////////////////////////////////////////////////////////////////////////

#include "CouplingFile.h"

#include <LibUtilities/BasicUtils/ErrorUtil.hpp>
37
#include <LibUtilities/BasicUtils/ParseUtils.h>
38 39 40
#include <LibUtilities/BasicUtils/PtsField.h>
#include <LibUtilities/BasicUtils/PtsIO.h>

41 42
#include <boost/format.hpp>

43 44 45 46 47 48 49 50 51 52 53 54
namespace Nektar
{
namespace SolverUtils
{

using namespace std;

std::string CouplingFile::className =
    GetCouplingFactory().RegisterCreatorFunction(
        "File", CouplingFile::create, "File Coupling");

CouplingFile::CouplingFile(MultiRegions::ExpListSharedPtr field)
Kilian Lackhove's avatar
Kilian Lackhove committed
55
    : Coupling(field), m_lastSend(-1E6), m_lastReceive(-1E6)
56
{
Kilian Lackhove's avatar
Kilian Lackhove committed
57
    m_config["RECEIVEFUNCTION"] = "CouplingIn";
58
    m_config["SENDFILENAME"]    = "CouplingOut_%14.8E.pts";
59 60 61 62 63 64
}

CouplingFile::~CouplingFile()
{
}

Kilian Lackhove's avatar
Kilian Lackhove committed
65 66 67 68
void CouplingFile::v_Init()
{
    Coupling::v_Init();

69
    if (m_nRecvVars > 0 && m_recvSteps > 0)
Kilian Lackhove's avatar
Kilian Lackhove committed
70 71
    {
        m_inputFunction = MemoryManager<SessionFunction>::AllocateSharedPtr(
72 73 74 75
            m_evalField->GetSession(),
            m_evalField,
            m_config["RECEIVEFUNCTION"],
            true);
Kilian Lackhove's avatar
Kilian Lackhove committed
76 77 78
    }
}

79 80 81 82
void CouplingFile::v_Send(
    const int step,
    const NekDouble time,
    const Array<OneD, const Array<OneD, NekDouble> > &field,
83
    vector<string> &varNames)
84
{
Kilian Lackhove's avatar
Kilian Lackhove committed
85
    if (m_nSendVars < 1 || m_sendSteps < 1)
Kilian Lackhove's avatar
Kilian Lackhove committed
86 87 88 89 90 91 92 93 94
    {
        return;
    }

    if (step < m_lastSend + m_sendSteps)
    {
        return;
    }
    m_lastSend = step;
95

Kilian Lackhove's avatar
Kilian Lackhove committed
96 97 98
    if (m_evalField->GetComm()->GetRank() == 0 &&
        m_evalField->GetSession()->DefinesCmdLineArgument("verbose"))
    {
99
        cout << "sending fields at i = " << step << ", t = " << time << endl;
Kilian Lackhove's avatar
Kilian Lackhove committed
100 101 102
    }

    vector<int> sendVarsToVars =
103
        GenerateVariableMapping(varNames, m_sendFieldNames);
104 105 106 107 108 109

#ifdef _WIN32
    // We need this to make sure boost::format has always
    // two digits in the exponents of Scientific notation.
    unsigned int old_exponent_format;
    old_exponent_format = _set_output_format(_TWO_DIGIT_EXPONENT);
110 111
    std::string filename =
        boost::str(boost::format(m_config["SENDFILENAME"]) % time);
112 113 114
    _set_output_format(old_exponent_format);
#else
    std::string filename =
Kilian Lackhove's avatar
Kilian Lackhove committed
115
        boost::str(boost::format(m_config["SENDFILENAME"]) % time);
116 117
#endif

Kilian Lackhove's avatar
Kilian Lackhove committed
118
    Array<OneD, Array<OneD, NekDouble> > pts(m_nSendVars + 3);
119 120
    for (int i = 0; i < 3; ++i)
    {
Kilian Lackhove's avatar
Kilian Lackhove committed
121
        pts[i] = Array<OneD, NekDouble>(m_evalField->GetTotPoints(), 0.0);
122
    }
Kilian Lackhove's avatar
Kilian Lackhove committed
123
    m_evalField->GetCoords(pts[0], pts[1], pts[2]);
124

Kilian Lackhove's avatar
Kilian Lackhove committed
125
    for (int i = 0; i < m_nSendVars; ++i)
126
    {
Kilian Lackhove's avatar
Kilian Lackhove committed
127
        pts[3 + i] = field[sendVarsToVars[i]];
128 129 130 131 132
    }

    LibUtilities::PtsIO ptsIO(m_evalField->GetSession()->GetComm());
    LibUtilities::PtsFieldSharedPtr sPts =
        MemoryManager<LibUtilities::PtsField>::AllocateSharedPtr(
Kilian Lackhove's avatar
Kilian Lackhove committed
133
            3, m_sendFieldNames, pts);
134 135 136 137 138
    // we first write to a temp file and rename this to make sure the
    // receiver doesnt try to read it before we finished writing
    string tmpFn = filename + ".tmp";
    ptsIO.Write(tmpFn, sPts);
    fs::rename(tmpFn, filename);
139 140 141 142 143
}

void CouplingFile::v_Receive(const int step,
                             const NekDouble time,
                             Array<OneD, Array<OneD, NekDouble> > &field,
144
                             vector<string> &varNames)
145
{
Kilian Lackhove's avatar
Kilian Lackhove committed
146
    if (m_nRecvVars < 1 || m_recvSteps < 1)
Kilian Lackhove's avatar
Kilian Lackhove committed
147 148 149 150 151 152 153 154 155 156 157 158 159
    {
        return;
    }

    if (step < m_lastReceive + m_recvSteps)
    {
        return;
    }
    m_lastReceive = step;

    if (m_evalField->GetComm()->GetRank() == 0 &&
        m_evalField->GetSession()->DefinesCmdLineArgument("verbose"))
    {
160
        cout << "receiving fields at i = " << step << ", t = " << time << endl;
Kilian Lackhove's avatar
Kilian Lackhove committed
161 162
    }

163 164
    string filename = m_evalField->GetSession()->GetFunctionFilename(
        m_config["RECEIVEFUNCTION"], m_recvFieldNames[0]);
165 166 167 168 169 170

#ifdef _WIN32
    // We need this to make sure boost::format has always
    // two digits in the exponents of Scientific notation.
    unsigned int old_exponent_format;
    old_exponent_format = _set_output_format(_TWO_DIGIT_EXPONENT);
171
    filename            = boost::str(boost::format(filename) % time);
172 173
    _set_output_format(old_exponent_format);
#else
174
    filename = boost::str(boost::format(filename) % time);
175 176 177
#endif

    int exists = 0;
178
    while (!exists)
179 180 181 182 183
    {
        exists = fs::exists(filename);
        m_evalField->GetComm()->AllReduce(exists, LibUtilities::ReduceMin);
    }

184
    Array<OneD, Array<OneD, NekDouble> > recvFields(m_nRecvVars);
Kilian Lackhove's avatar
Kilian Lackhove committed
185
    m_inputFunction->Evaluate(m_recvFieldNames, recvFields, time);
186 187 188 189 190 191

    vector<int> recvVarsToVars =
        GenerateVariableMapping(varNames, m_recvFieldNames);
    ASSERTL1(m_nRecvVars == recvVarsToVars.size(), "field size mismatch");
    for (int i = 0; i < recvVarsToVars.size(); ++i)
    {
192 193 194 195 196
        Vmath::Vcopy(recvFields[i].num_elements(),
                     recvFields[i],
                     1,
                     field[recvVarsToVars[i]],
                     1);
197
    }
198 199 200
}
}
}