source: XIOS/trunk/src/node/field.cpp @ 436

Last change on this file since 436 was 436, checked in by ymipsl, 11 years ago

bug fix :

  • freq_op is now by default of 1 timestep (1ts)
  • once operation is now working as required

YM

File size: 18.0 KB
Line 
1#include "field.hpp"
2
3#include "attribute_template.hpp"
4#include "object_template.hpp"
5#include "group_template.hpp"
6
7#include "node_type.hpp"
8#include "calendar_util.hpp"
9#include "message.hpp"
10#include "xmlioserver_spl.hpp"
11#include "type.hpp"
12#include "context_client.hpp"
13
14namespace xios{
15   
16   /// ////////////////////// Définitions ////////////////////// ///
17
18   CField::CField(void)
19      : CObjectTemplate<CField>(), CFieldAttributes()
20      , refObject(), baseRefObject()
21      , grid(), file()
22      , freq_operation(), freq_write()
23      , nstep(0)
24      , last_Write(), last_operation()
25      , foperation()
26      { /* Ne rien faire de plus */ }
27
28   CField::CField(const StdString & id)
29      : CObjectTemplate<CField>(id), CFieldAttributes()
30      , refObject(), baseRefObject()
31      , grid(), file()
32      , freq_operation(), freq_write()
33      , nstep(0)
34      , last_Write(), last_operation()
35      , foperation()
36   { /* Ne rien faire de plus */ }
37
38   CField::~CField(void)
39   {
40//      this->grid.reset() ;
41//      this->file.reset() ;
42      this->foperation.reset() ;
43   }
44
45   //----------------------------------------------------------------
46
47   bool CField::updateDataServer
48      (const CDate & currDate,
49       const std::deque< CArray<double, 1>* > storedClient)
50   {
51      const CDate opeDate      = *last_operation + freq_operation;
52      const CDate writeDate    = *last_Write     + freq_write; 
53     
54      if (opeDate <= currDate)
55      {
56         if (this->data.numElements() != this->grid->storeIndex[0]->numElements())
57         {
58            this->data.resize(this->grid->storeIndex[0] ->numElements());
59         } 
60         CArray<double,1> input(data.numElements()) ;
61         this->grid->inputFieldServer(storedClient, input);         
62         (*this->foperation)(input);
63         *last_operation = currDate;
64      }
65      if (writeDate < (currDate + freq_operation))
66      {
67         this->foperation->final();
68         this->incrementNStep();
69         *last_Write = writeDate;
70         return (true);       
71      }
72      return (false);
73   }
74   
75   bool CField::dispatchEvent(CEventServer& event)
76  {
77     
78    if (SuperClass::dispatchEvent(event)) return true ;
79    else
80    {
81      switch(event.type)
82      {
83        case EVENT_ID_UPDATE_DATA :
84          recvUpdateData(event) ;
85          return true ;
86          break ;
87 
88        default :
89          ERROR("bool CField::dispatchEvent(CEventServer& event)",<<"Unknown Event") ;
90          return false ;
91      }
92    }
93  }
94 
95  void CField::sendUpdateData(void)
96  {
97    CContext* context = CContext::getCurrent() ;
98    CContextClient* client=context->client ;
99   
100    CEventClient event(getType(),EVENT_ID_UPDATE_DATA) ;
101   
102    map<int,CArray<int, 1>* >::iterator it ;
103    list<shared_ptr<CMessage> > list_msg ;
104    list< CArray<double,1>* > list_data ;
105   
106    for(it=grid->storeIndex_toSrv.begin();it!=grid->storeIndex_toSrv.end();it++)
107    {
108      int rank=(*it).first ;
109      CArray<int,1>& index = *(it->second) ;
110      CArray<double,1> data_tmp(index.numElements()) ;
111     
112      for(int n=0;n<data_tmp.numElements();n++) data_tmp(n)=data(index(n)) ;
113      list_msg.push_back(shared_ptr<CMessage>(new CMessage)) ;
114      list_data.push_back(new CArray<double,1>(data_tmp)) ;
115      *list_msg.back()<<getId()<<*list_data.back() ;
116      event.push(rank,grid->nbSenders[rank],*list_msg.back()) ;
117    }
118    client->sendEvent(event) ;
119   
120    for(list< CArray<double,1>* >::iterator it=list_data.begin();it!=list_data.end();it++) delete *it ;
121  }
122 
123  void CField::recvUpdateData(CEventServer& event)
124  {
125    vector<int> ranks ;
126    vector<CBufferIn*> buffers ;
127     
128    list<CEventServer::SSubEvent>::iterator it ;
129    string fieldId ;
130
131    for (it=event.subEvents.begin();it!=event.subEvents.end();++it)
132    {
133      int rank=it->rank;
134      CBufferIn* buffer=it->buffer;
135      *buffer>>fieldId ;
136      ranks.push_back(rank) ;
137      buffers.push_back(buffer) ;
138    }
139    get(fieldId)->recvUpdateData(ranks,buffers) ;   
140  }
141 
142  void  CField::recvUpdateData(vector<int>& ranks, vector<CBufferIn*>& buffers)
143  {
144   
145    if (data_srv.empty())
146    {
147      for(map<int, CArray<int, 1>* >::iterator it=grid->out_i_fromClient.begin();it!=grid->out_i_fromClient.end();it++)
148      {
149        int rank=it->first ;
150        CArray<double,1> data_tmp(it->second->numElements()) ;
151        data_srv.insert( pair<int, CArray<double,1>* >(rank, new CArray<double,1>(data_tmp) ) ) ;
152        foperation_srv.insert(pair<int,boost::shared_ptr<func::CFunctor> >(rank,boost::shared_ptr<func::CFunctor>(new func::CInstant(*data_srv[rank])))) ;
153      }
154    }
155
156    CContext* context = CContext::getCurrent() ;
157    const CDate & currDate = context->getCalendar()->getCurrentDate();
158    const CDate opeDate      = *last_operation_srv + freq_operation_srv;
159    const CDate writeDate    = *last_Write_srv     + freq_write_srv; 
160   
161
162   
163    if (opeDate <= currDate)
164    {
165      for(int n=0;n<ranks.size();n++)
166      {
167        CArray<double,1> data_tmp ;
168        *buffers[n]>>data_tmp ;
169        (*foperation_srv[ranks[n]])(data_tmp) ;
170      }
171      *last_operation_srv = currDate;
172    }
173     
174    if (writeDate < (currDate + freq_operation_srv))
175    {
176      for(int n=0;n<ranks.size();n++)
177      {
178        this->foperation_srv[ranks[n]]->final();
179      }
180     
181      *last_Write_srv = writeDate;
182      writeField() ;
183      *lastlast_Write_srv=*last_Write_srv;
184    }
185  }
186 
187  void CField::writeField(void)
188  {
189    if (! getRelFile()->allDomainEmpty )
190      if (! grid->domain->isEmpty() || getRelFile()->type == CFile::type_attr::one_file)
191      {
192        getRelFile()->checkFile();
193        this->incrementNStep();
194        getRelFile()->getDataOutput()->writeFieldData(CField::get(this));
195      }
196  }
197   //----------------------------------------------------------------
198
199   void CField::setRelFile(CFile* _file)
200   { 
201      this->file = _file; 
202   }
203
204   //----------------------------------------------------------------
205
206   StdString CField::GetName(void)   { return (StdString("field")); }
207   StdString CField::GetDefName(void){ return (CField::GetName()); }
208   ENodeType CField::GetType(void)   { return (eField); }
209
210   //----------------------------------------------------------------
211
212   CGrid* CField::getRelGrid(void) const
213   { 
214      return (this->grid); 
215   }
216
217   //----------------------------------------------------------------
218
219   CFile* CField::getRelFile(void) const
220   { 
221      return (this->file);
222   }
223   
224   StdSize CField::getNStep(void) const
225   {
226      return (this->nstep);
227   }
228   
229   void CField::incrementNStep(void)
230   {
231      this->nstep++;
232   }
233 
234   void CField::resetNStep(void)
235   {
236      this->nstep=0;
237   }
238
239   //----------------------------------------------------------------
240
241   CField* CField::getDirectFieldReference(void) const
242   {
243      if (this->field_ref.isEmpty())
244         return (this->getBaseFieldReference());
245
246      if (! CField::has(this->field_ref.getValue()))
247         ERROR("CField::getDirectFieldReference(void)",
248               << "[ ref_name = " << this->field_ref.getValue() << "]"
249               << " invalid field name !");
250
251      return (CField::get(this->field_ref.getValue()));
252   }
253
254   //----------------------------------------------------------------
255
256   CField* CField::getBaseFieldReference(void) const
257   { 
258      return (baseRefObject); 
259   }
260
261   //----------------------------------------------------------------
262
263   const std::vector<CField*>& CField::getAllReference(void) const 
264   { 
265      return (refObject);
266   }
267
268   //----------------------------------------------------------------
269
270   const StdString & CField::getBaseFieldId(void) const
271   { 
272      return (this->getBaseFieldReference()->getId());
273   }
274   
275   //----------------------------------------------------------------
276   
277   const CDuration & CField::getFreqOperation(void) const
278   {
279      return (this->freq_operation);
280   }
281   
282   //----------------------------------------------------------------
283   const CDuration & CField::getFreqWrite(void) const
284   {
285      return (this->freq_write);
286   }
287   
288   //----------------------------------------------------------------
289         
290   boost::shared_ptr<func::CFunctor> CField::getFieldOperation(void) const
291   {
292      return (this->foperation);
293   }
294
295   //----------------------------------------------------------------
296
297   bool CField::hasDirectFieldReference(void) const
298   { 
299     return (!this->field_ref.isEmpty()); 
300   }
301   
302   bool CField::isActive(void) const
303   { 
304      return (!this->refObject.empty()); 
305   }
306   //----------------------------------------------------------------
307   
308   CArray<double, 1> CField::getData(void) const
309   {
310      return(this->data);
311   }
312
313   //----------------------------------------------------------------
314
315   boost::shared_ptr<CDate> CField::getLastWriteDate(void) const
316   {
317      return(this->last_Write);
318   }
319
320   //----------------------------------------------------------------
321
322   boost::shared_ptr<CDate> CField::getLastOperationDate(void) const
323   {
324      return(this->last_operation);
325   }
326
327   //----------------------------------------------------------------
328
329   void CField::solveRefInheritance(void)
330   {
331      std::set<CField *> sset;
332      CField* refer_sptr;
333      CField * refer_ptr = this;
334     
335      this->baseRefObject = CField::get(this);
336     
337      while (refer_ptr->hasDirectFieldReference())
338      {
339         refer_sptr = refer_ptr->getDirectFieldReference();
340         refer_ptr  = refer_sptr;
341
342         if(sset.end() != sset.find(refer_ptr))
343         {
344            DEBUG (<< "Dépendance circulaire stoppée pour l'objet de type CField sur "
345                   << "\"" + refer_ptr->getId() + "\" !");
346            break;
347         }
348
349         SuperClassAttribute::setAttributes(refer_ptr);
350         sset.insert(refer_ptr);
351         baseRefObject = refer_sptr;
352//ym         refObject.push_back(refer_sptr);
353      }
354   }
355
356   //----------------------------------------------------------------
357
358   void  CField::solveOperation(void)
359   {
360      using namespace func;
361       
362      StdString id = this->getBaseFieldReference()->getId();
363      CContext* context = CContext::getCurrent();
364     
365      if (freq_op.isEmpty()) freq_op=string("1ts") ;
366     
367      if (operation.isEmpty()  || this->file->output_freq.isEmpty())
368      {
369         ERROR("CField::solveOperation(void)",
370               << "[ id = " << id << "]"
371               << "Impossible to define an operation for this field !");
372      }
373     
374      CDuration freq_offset_ = NoneDu;
375      if (!freq_offset.isEmpty())
376      {
377         freq_offset_ = CDuration::FromString(freq_offset.getValue());
378      }
379      else
380      {
381         freq_offset.setValue(NoneDu.toString());
382      } 
383
384//      if (CXIOSManager::GetStatus() == CXIOSManager::LOC_SERVER)
385      if (context->hasServer)
386      {
387         this->freq_operation_srv =
388             CDuration::FromString(this->file->output_freq.getValue());
389         this->freq_write_srv     =
390             CDuration::FromString(this->file->output_freq.getValue());
391         this->lastlast_Write_srv     = boost::shared_ptr<CDate>
392                        (new CDate(context->getCalendar()->getInitDate()));
393         this->last_Write_srv     = boost::shared_ptr<CDate>
394                        (new CDate(context->getCalendar()->getInitDate()));
395         this->last_operation_srv = boost::shared_ptr<CDate>
396                        (new CDate(context->getCalendar()->getInitDate()));
397//         this->foperation_srv     =
398//             boost::shared_ptr<func::CFunctor>(new CInstant(this->data_srv));
399             
400         const CDuration toffset = this->freq_operation_srv - freq_offset_ - context->getCalendar()->getTimeStep(); 
401         *this->last_operation_srv   = *this->last_operation_srv - toffset; 
402      }
403     
404      if (context->hasClient)
405      {                 
406         this->freq_operation = CDuration::FromString(freq_op.getValue());
407         this->freq_write     = CDuration::FromString(this->file->output_freq.getValue());
408         this->last_Write     = boost::shared_ptr<CDate>
409                        (new CDate(context->getCalendar()->getInitDate()));
410         this->last_operation = boost::shared_ptr<CDate>
411                        (new CDate(context->getCalendar()->getInitDate()));
412                       
413         const CDuration toffset = this->freq_operation - freq_offset_ - context->getCalendar()->getTimeStep(); 
414         *this->last_operation   = *this->last_operation - toffset; 
415     
416        if (operation.get()=="once") isOnceOperation=true ;
417        else isOnceOperation=false;
418        isFirstOperation=true;
419     
420        cout<<"Operation : "<<operation<<" isOnce "<<isOnceOperation<<endl;         
421#define DECLARE_FUNCTOR(MType, mtype)              \
422   if  (operation.getValue().compare(#mtype) == 0) \
423   {                                               \
424      boost::shared_ptr<func::CFunctor>            \
425            foperation_(new C##MType(this->data)); \
426      this->foperation = foperation_;              \
427      return;                                      \
428   }
429   
430#include "functor_type.conf"
431         
432         ERROR("CField::solveOperation(void)",
433               << "[ operation = " << operation.getValue() << "]"
434               << "The operation is not defined !");
435      }
436     
437
438   }
439   
440   //----------------------------------------------------------------
441/*
442   void CField::fromBinary(StdIStream & is)
443   {
444      SuperClass::fromBinary(is);
445#define CLEAR_ATT(name_)\
446      SuperClassAttribute::operator[](#name_)->reset()
447
448         CLEAR_ATT(domain_ref);
449         CLEAR_ATT(axis_ref);
450#undef CLEAR_ATT
451
452   }
453*/
454   //----------------------------------------------------------------
455
456   void CField::solveGridReference(void)
457   {
458      CDomain* domain;
459      CAxis* axis;
460
461      if (!domain_ref.isEmpty())
462      {
463         if (CDomain::has(domain_ref.getValue()))
464            domain = CDomain::get(domain_ref.getValue()) ;
465         else
466            ERROR("CField::solveGridReference(void)",
467                  << "Reference to the domain \'"
468                  << domain_ref.getValue() << "\' is wrong") ;
469      }
470
471      if (!axis_ref.isEmpty())
472      {
473         if (CAxis::has(axis_ref.getValue()))
474            axis = CAxis::get(axis_ref.getValue()) ;
475         else
476            ERROR("CField::solveGridReference(void)",
477                  << "Reference to the axis \'"
478                  << axis_ref.getValue() <<"\' is wrong") ;
479      }
480
481      if (!grid_ref.isEmpty())
482      {
483         if (CGrid::has(grid_ref.getValue()))
484            this->grid = CGrid::get(grid_ref.getValue()) ;
485         else
486            ERROR("CField::solveGridReference(void)",
487                  << "Reference to the grid \'"
488                  << grid_ref.getValue() << "\' is wrong");
489      }
490     
491      if (grid_ref.isEmpty() &&  domain_ref.isEmpty())
492      {
493            ERROR("CField::solveGridReference(void)",
494                  << "The horizontal domain for this field is not defined");
495
496     }
497     
498     CType<string> goodDomain ;
499     CType<string> goodAxis ;
500     if (!grid_ref.isEmpty())
501     {
502       if (!grid->domain_ref.isEmpty()) goodDomain=grid->domain_ref ;
503       if (!grid->axis_ref.isEmpty()) goodAxis=grid->axis_ref ;
504     }
505     if (!domain_ref.isEmpty()) goodDomain=domain_ref ;
506     if (!axis_ref.isEmpty()) goodAxis=axis_ref ;
507     
508     
509     if (goodDomain.isEmpty()) 
510     {
511       ERROR("CField::solveGridReference(void)", << "The horizontal domain for this field is not defined");
512     }
513     else 
514     {
515       if (CDomain::has(goodDomain)) domain = CDomain::get(goodDomain) ;
516       else ERROR("CField::solveGridReference(void)",<< "Reference to the domain \'"<<goodDomain.get() << "\' is wrong") ;
517     }
518 
519     if (!goodAxis.isEmpty())
520     {
521       if (CAxis::has(goodAxis))  axis = CAxis::get(goodAxis) ;
522       else  ERROR("CField::solveGridReference(void)", << "Reference to the axis \'"
523                  << goodAxis.get() <<"\' is wrong") ;
524     } 
525   
526     bool nothingToDo=false ;
527     
528     if (!grid_ref.isEmpty())
529     {
530       if (!grid->domain_ref.isEmpty() && goodDomain.get() == grid->domain_ref.get())
531         if (goodAxis.isEmpty()) nothingToDo=true ;
532         else if (!grid->axis_ref.isEmpty()) 
533                 if (grid->axis_ref.get()==goodAxis.get()) nothingToDo=true ;
534     }
535     
536     if (!nothingToDo)
537     {
538       if (!goodAxis.isEmpty())
539       {
540         this->grid = CGrid::createGrid(domain, axis) ;
541         this->grid_ref.setValue(this->grid->getId());
542       }
543       else
544       {
545         this->grid = CGrid::createGrid(domain) ;
546         this->grid_ref.setValue(this->grid->getId());
547       }   
548     }
549
550     grid->solveReference() ;
551   }
552
553
554   ///-------------------------------------------------------------------
555
556   template <>
557      void CGroupTemplate<CField, CFieldGroup, CFieldAttributes>::solveRefInheritance(void)
558   {
559      if (this->group_ref.isEmpty()) return;
560      StdString gref = this->group_ref.getValue();
561
562      if (!CFieldGroup::has(gref))
563         ERROR("CGroupTemplate<CField, CFieldGroup, CFieldAttributes>::solveRefInheritance(void)",
564               << "[ gref = " << gref << "]"
565               << " invalid group name !");
566
567      CFieldGroup* group = CFieldGroup::get(gref);
568      CFieldGroup* owner = CFieldGroup::get(boost::polymorphic_downcast<CFieldGroup*>(this));
569
570      std::vector<CField*> allChildren  = group->getAllChildren();
571      std::vector<CField*>::iterator
572         it = allChildren.begin(), end = allChildren.end();
573     
574      for (; it != end; it++)
575      {
576         CField* child = *it;
577         if (child->hasId()) owner->createChild()->field_ref.setValue(child->getId()) ;
578           
579      }
580   }
581   
582   void CField::outputField(CArray<double,3>& fieldOut)
583   {
584      map<int, CArray<double,1>* >::iterator it;
585      for(it=data_srv.begin();it!=data_srv.end();it++)
586         grid->outputField(it->first,*it->second, fieldOut) ;
587     
588   }
589   
590   void CField::outputField(CArray<double,2>& fieldOut)
591   {
592      map<int, CArray<double,1>* >::iterator it;
593
594      for(it=data_srv.begin();it!=data_srv.end();it++)
595      {
596         grid->outputField(it->first,*it->second, fieldOut) ;
597      }
598   }
599   ///-------------------------------------------------------------------
600
601} // namespace xios
Note: See TracBrowser for help on using the repository browser.