Nektar++
Coupling.cpp
Go to the documentation of this file.
1 ////////////////////////////////////////////////////////////////////////////////
2 //
3 // File: Coupling.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: Coupling
30 //
31 ////////////////////////////////////////////////////////////////////////////////
32 
33 #include "Coupling.h"
34 
36 
37 namespace Nektar
38 {
39 namespace SolverUtils
40 {
41 
42 using namespace std;
43 
45 {
46  static CouplingFactory instance;
47  return instance;
48 }
49 
51  : m_couplingName(""), m_evalField(field), m_nSendVars(0), m_sendSteps(0),
52  m_nRecvVars(0), m_recvSteps(0)
53 {
54  m_config["RECEIVESTEPS"] = "0";
55  m_config["RECEIVEVARIABLES"] = "";
56 
57  m_config["SENDSTEPS"] = "0";
58  m_config["SENDVARIABLES"] = "";
59 }
60 
62 {
63  LibUtilities::SessionReaderSharedPtr session = m_evalField->GetSession();
64 
65  TiXmlElement *vCoupling = session->GetElement("Nektar/Coupling");
66  ASSERTL0(vCoupling, "Invalid Coupling config");
67 
68  vCoupling->QueryStringAttribute("NAME", &m_couplingName);
69  ASSERTL0(m_couplingName.size(), "No Coupling NAME attribute set");
70 
71  TiXmlElement *element = vCoupling->FirstChildElement("I");
72  while (element)
73  {
74  std::stringstream tagcontent;
75  tagcontent << *element;
76  // read the property name
77  ASSERTL0(element->Attribute("PROPERTY"),
78  "Missing PROPERTY attribute in Coupling section "
79  "XML element: \n\t'" +
80  tagcontent.str() + "'");
81  std::string property = element->Attribute("PROPERTY");
82  ASSERTL0(!property.empty(),
83  "PROPERTY attribute must be non-empty in XML "
84  "element: \n\t'" +
85  tagcontent.str() + "'");
86 
87  // make sure that solver property is capitalised
88  std::string propertyUpper = boost::to_upper_copy(property);
89 
90  CouplingConfigMap::const_iterator x = m_config.find(propertyUpper);
91  ASSERTL0(x != m_config.end(),
92  "Invalid PROPERTY attribute in Coupling section "
93  "XML element: \n\t'" +
94  tagcontent.str() + "'");
95 
96  // read the value
97  ASSERTL0(element->Attribute("VALUE"),
98  "Missing VALUE attribute in Coupling section "
99  "XML element: \n\t'" +
100  tagcontent.str() + "'");
101  std::string value = element->Attribute("VALUE");
102  ASSERTL0(!value.empty(),
103  "VALUE attribute must be non-empty in XML "
104  "element: \n\t'" +
105  tagcontent.str() + "'");
106 
107  // Set Variable
108  m_config[propertyUpper] = value;
109 
110  element = element->NextSiblingElement("I");
111  }
112 
113  // mangle config into variables. This is ugly
115  m_nRecvVars = m_recvFieldNames.size();
116 
118  m_nSendVars = m_sendFieldNames.size();
119 
120  m_recvSteps = boost::lexical_cast<int>(m_config["RECEIVESTEPS"]);
121  m_sendSteps = boost::lexical_cast<int>(m_config["SENDSTEPS"]);
122 
123  if (session->GetComm()->GetRank() == 0 &&
124  session->DefinesCmdLineArgument("verbose") && m_config.size() > 0)
125  {
126  cout << "Coupling Config:" << endl;
127  CouplingConfigMap::iterator x;
128  for (x = m_config.begin(); x != m_config.end(); ++x)
129  {
130  cout << "\t" << x->first << " = '" << x->second << "'" << endl;
131  }
132  }
133 }
134 
135 vector<int> Coupling::GenerateVariableMapping(vector<string> &vars,
136  vector<string> &transVars)
137 {
138  vector<int> transToVars;
139  Array<OneD, Array<OneD, NekDouble> > sendField(transVars.size());
140  for (int i = 0; i < transVars.size(); ++i)
141  {
142  auto it2 = find(vars.begin(), vars.end(), transVars[i]);
143  ASSERTL0(it2 != vars.end(),
144  "send variable " + transVars[i] + " not found");
145  int id = distance(vars.begin(), it2);
146 
147  transToVars.push_back(id);
148  }
149 
150  return transToVars;
151 }
152 }
153 }
#define ASSERTL0(condition, msg)
Definition: ErrorUtil.hpp:216
static bool GenerateVector(const std::string &str, std::vector< T > &out)
Takes a comma-separated string and converts it to entries in a vector.
Definition: ParseUtils.cpp:135
virtual SOLVER_UTILS_EXPORT void v_Init()
Definition: Coupling.cpp:61
MultiRegions::ExpListSharedPtr m_evalField
Definition: Coupling.h:115
std::vector< std::string > m_sendFieldNames
Definition: Coupling.h:118
CouplingConfigMap m_config
Definition: Coupling.h:113
std::vector< std::string > m_recvFieldNames
Definition: Coupling.h:122
SOLVER_UTILS_EXPORT Coupling(MultiRegions::ExpListSharedPtr field)
Definition: Coupling.cpp:50
SOLVER_UTILS_EXPORT std::vector< int > GenerateVariableMapping(std::vector< std::string > &vars, std::vector< std::string > &transVars)
Definition: Coupling.cpp:135
std::shared_ptr< SessionReader > SessionReaderSharedPtr
std::shared_ptr< ExpList > ExpListSharedPtr
Shared pointer to an ExpList object.
CouplingFactory & GetCouplingFactory()
Declaration of the Coupling factory singleton.
Definition: Coupling.cpp:44
SOLVER_UTILS_EXPORT typedef LibUtilities::NekFactory< std::string, Coupling, MultiRegions::ExpListSharedPtr > CouplingFactory
Declaration of the Coupling factory.
Definition: Coupling.h:52
InputIterator find(InputIterator first, InputIterator last, InputIterator startingpoint, const EqualityComparable &value)
Definition: StdRegions.hpp:362
The above copyright notice and this permission notice shall be included.
Definition: CoupledSolver.h:1