source: XIOS/dev/dev_ym/XIOS_COUPLING/src/node/scalar.cpp @ 1875

Last change on this file since 1875 was 1875, checked in by ymipsl, 4 years ago

XIOS coupling branch
Some updates.

First coupling test is beginning to work...

YM

File size: 7.3 KB
Line 
1#include "scalar.hpp"
2
3#include "attribute_template.hpp"
4#include "object_template.hpp"
5#include "group_template.hpp"
6#include "object_factory.hpp"
7#include "xios_spl.hpp"
8#include "type.hpp"
9
10namespace xios {
11
12   /// ////////////////////// Définitions ////////////////////// ///
13
14   CScalar::CScalar(void)
15      : CObjectTemplate<CScalar>()
16      , CScalarAttributes()
17      , relFiles()
18   { /* Ne rien faire de plus */ }
19
20   CScalar::CScalar(const StdString & id)
21      : CObjectTemplate<CScalar>(id)
22      , CScalarAttributes()
23      , relFiles()
24   { /* Ne rien faire de plus */ }
25
26   CScalar::~CScalar(void)
27   { /* Ne rien faire de plus */ }
28
29   std::map<StdString, ETranformationType> CScalar::transformationMapList_ = std::map<StdString, ETranformationType>();
30   bool CScalar::dummyTransformationMapList_ = CScalar::initializeTransformationMap(CScalar::transformationMapList_);
31   bool CScalar::initializeTransformationMap(std::map<StdString, ETranformationType>& m)
32   {
33     m["reduce_axis"]   = TRANS_REDUCE_AXIS_TO_SCALAR;
34     m["extract_axis"]  = TRANS_EXTRACT_AXIS_TO_SCALAR;
35     m["reduce_domain"] = TRANS_REDUCE_DOMAIN_TO_SCALAR;
36     m["reduce_scalar"] = TRANS_REDUCE_SCALAR_TO_SCALAR;
37   }
38
39   StdString CScalar::GetName(void)   { return (StdString("scalar")); }
40   StdString CScalar::GetDefName(void){ return (CScalar::GetName()); }
41   ENodeType CScalar::GetType(void)   { return (eScalar); }
42
43   CScalar* CScalar::createScalar()
44   {
45     CScalar* scalar = CScalarGroup::get("scalar_definition")->createChild();
46     return scalar;
47   }
48
49   bool CScalar::IsWritten(const StdString & filename) const
50   {
51      return (this->relFiles.find(filename) != this->relFiles.end());
52   }
53
54   void CScalar::addRelFile(const StdString& filename)
55   {
56      this->relFiles.insert(filename);
57   }
58
59   void CScalar::checkAttributes(void)
60   {
61      if (checkAttributes_done_) return ;
62
63      checkAttributes_done_ = true ; 
64   }
65
66  void CScalar::checkAttributesOnClient()
67  {
68
69  }
70
71  /*!
72    Compare two scalar objects.
73    They are equal if only if they have identical attributes as well as their values.
74    Moreover, they must have the same transformations.
75  \param [in] scalar Compared scalar
76  \return result of the comparison
77  */
78  bool CScalar::isEqual(CScalar* obj)
79  {
80    vector<StdString> excludedAttr;
81    excludedAttr.push_back("scalar_ref");
82    bool objEqual = SuperClass::isEqual(obj, excludedAttr);
83    if (!objEqual) return objEqual;
84
85    TransMapTypes thisTrans = this->getAllTransformations();
86    TransMapTypes objTrans  = obj->getAllTransformations();
87
88    TransMapTypes::const_iterator it, itb, ite;
89    std::vector<ETranformationType> thisTransType, objTransType;
90    for (it = thisTrans.begin(); it != thisTrans.end(); ++it)
91      thisTransType.push_back(it->first);
92    for (it = objTrans.begin(); it != objTrans.end(); ++it)
93      objTransType.push_back(it->first);
94
95    if (thisTransType.size() != objTransType.size()) return false;
96    for (int idx = 0; idx < thisTransType.size(); ++idx)
97      objEqual &= (thisTransType[idx] == objTransType[idx]);
98
99    return objEqual;
100  }
101
102  CTransformation<CScalar>* CScalar::addTransformation(ETranformationType transType, const StdString& id)
103  {
104    transformationMap_.push_back(std::make_pair(transType, CTransformation<CScalar>::createTransformation(transType,id)));
105    return transformationMap_.back().second;
106  }
107
108  bool CScalar::hasTransformation()
109  {
110    return (!transformationMap_.empty());
111  }
112
113  void CScalar::setTransformations(const TransMapTypes& scalarTrans)
114  {
115    transformationMap_ = scalarTrans;
116  }
117
118  CScalar::TransMapTypes CScalar::getAllTransformations(void)
119  {
120    return transformationMap_;
121  }
122
123  void CScalar::duplicateTransformation(CScalar* src)
124  {
125    if (src->hasTransformation())
126    {
127      this->setTransformations(src->getAllTransformations());
128    }
129  }
130
131  /*!
132   * Go through the hierarchy to find the scalar from which the transformations must be inherited
133   */
134  void CScalar::solveInheritanceTransformation()
135  {
136    if (hasTransformation() || !hasDirectScalarReference())
137      return;
138
139    CScalar* scalar = this;
140    std::vector<CScalar*> refScalar;
141    while (!scalar->hasTransformation() && scalar->hasDirectScalarReference())
142    {
143      refScalar.push_back(scalar);
144      scalar = scalar->getDirectScalarReference();
145    }
146
147    if (scalar->hasTransformation())
148      for (size_t i = 0; i < refScalar.size(); ++i)
149        refScalar[i]->setTransformations(scalar->getAllTransformations());
150  }
151 
152  void CScalar::sendScalarToFileServer(CContextClient* client)
153  {
154    if (sendScalarToFileServer_done_.count(client)!=0) return ;
155    else sendScalarToFileServer_done_.insert(client) ;
156    StdString scalarDefRoot("scalar_definition");
157    CScalarGroup* scalarPtr = CScalarGroup::get(scalarDefRoot);
158    this->sendAllAttributesToServer(client);
159  }
160
161  void CScalar::sendScalarToCouplerOut(CContextClient* client, const string& fieldId, int posInGrid)
162  {
163    if (sendScalarToCouplerOut_done_.count(client)!=0) return ;
164    else sendScalarToCouplerOut_done_.insert(client) ;
165
166    string scalarId="_scalar["+std::to_string(posInGrid)+"]_of_"+fieldId ;
167   
168    if (!scalar_ref.isEmpty())
169    {
170      auto scalar_ref_tmp=scalar_ref.getValue() ;
171      scalar_ref.reset() ; // remove the reference, find an other way to do that more cleanly
172      this->sendAllAttributesToServer(client, scalarId)  ;
173      scalar_ref = scalar_ref_tmp ;
174    }
175    else this->sendAllAttributesToServer(client, scalarId)  ;
176
177
178    this->sendAllAttributesToServer(client, scalarId);
179  } 
180
181  void CScalar::makeAliasForCoupling(const string& fieldId, int posInGrid)
182  {
183    const string scalarId = "_scalar["+std::to_string(posInGrid)+"]_of_"+fieldId ;
184    this->createAlias(scalarId) ;
185  }
186
187  void CScalar::setContextClient(CContextClient* contextClient)
188  TRY
189  {
190    if (clientsSet.find(contextClient)==clientsSet.end())
191    {
192      clients.push_back(contextClient) ;
193      clientsSet.insert(contextClient);
194    }
195  }
196  CATCH_DUMP_ATTR
197  /*!
198    Parse children nodes of a scalar in xml file.
199    \param node child node to process
200  */
201  void CScalar::parse(xml::CXMLNode & node)
202  {
203    SuperClass::parse(node);
204
205    if (node.goToChildElement())
206    {
207      StdString nodeElementName;
208      do
209      {
210        StdString nodeId("");
211        if (node.getAttributes().end() != node.getAttributes().find("id"))
212        { nodeId = node.getAttributes()["id"]; }
213
214        nodeElementName = node.getElementName();
215        std::map<StdString, ETranformationType>::const_iterator ite = transformationMapList_.end(), it;
216        it = transformationMapList_.find(nodeElementName);
217        if (ite != it)
218        {
219          transformationMap_.push_back(std::make_pair(it->second, CTransformation<CScalar>::createTransformation(it->second,
220                                                                                                                 nodeId,
221                                                                                                                 &node)));
222        }
223        else
224        {
225          ERROR("void CScalar::parse(xml::CXMLNode & node)",
226                << "The transformation " << nodeElementName << " has not been supported yet.");
227        }
228      } while (node.goToNextElement()) ;
229      node.goToParentElement();
230    }
231  }
232
233  // Definition of some macros
234  DEFINE_REF_FUNC(Scalar,scalar)
235
236} // namespace xios
Note: See TracBrowser for help on using the repository browser.