source: XMLIO_V2/dev/common/src/node/field.cpp @ 300

Last change on this file since 300 was 300, checked in by ymipsl, 12 years ago

nouvelle version de developpement de xios

  • nouvelle interface fortran
  • recodage complet de la couche de communication
  • et bien d'autres choses...

YM

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