[219] | 1 | #include "field.hpp" |
---|
| 2 | |
---|
[352] | 3 | #include "attribute_template.hpp" |
---|
| 4 | #include "object_template.hpp" |
---|
| 5 | #include "group_template.hpp" |
---|
[219] | 6 | |
---|
| 7 | #include "node_type.hpp" |
---|
| 8 | #include "calendar_util.hpp" |
---|
[352] | 9 | #include "message.hpp" |
---|
| 10 | #include "xmlioserver_spl.hpp" |
---|
| 11 | #include "type.hpp" |
---|
| 12 | #include "context_client.hpp" |
---|
[459] | 13 | #include <set> |
---|
[219] | 14 | |
---|
[335] | 15 | namespace xios{ |
---|
[219] | 16 | |
---|
| 17 | /// ////////////////////// Définitions ////////////////////// /// |
---|
| 18 | |
---|
| 19 | CField::CField(void) |
---|
| 20 | : CObjectTemplate<CField>(), CFieldAttributes() |
---|
| 21 | , refObject(), baseRefObject() |
---|
| 22 | , grid(), file() |
---|
| 23 | , freq_operation(), freq_write() |
---|
[266] | 24 | , nstep(0) |
---|
[219] | 25 | , last_Write(), last_operation() |
---|
[459] | 26 | , foperation(), hasInstantData(false), hasExpression(false) |
---|
[460] | 27 | , active(false) , hasOutputFile(false),hasFieldOut(false), slotUpdateDate(NULL) |
---|
[459] | 28 | , processed(false) |
---|
[472] | 29 | { setVirtualVariableGroup() ; } |
---|
[219] | 30 | |
---|
| 31 | CField::CField(const StdString & id) |
---|
| 32 | : CObjectTemplate<CField>(id), CFieldAttributes() |
---|
| 33 | , refObject(), baseRefObject() |
---|
| 34 | , grid(), file() |
---|
| 35 | , freq_operation(), freq_write() |
---|
[266] | 36 | , nstep(0) |
---|
[219] | 37 | , last_Write(), last_operation() |
---|
[466] | 38 | , foperation(), hasInstantData(false), hasExpression(false) |
---|
[460] | 39 | , active(false), hasOutputFile(false), hasFieldOut(false), slotUpdateDate(NULL) |
---|
[459] | 40 | , processed(false) |
---|
[472] | 41 | { setVirtualVariableGroup() ; } |
---|
[219] | 42 | |
---|
| 43 | CField::~CField(void) |
---|
| 44 | { |
---|
[347] | 45 | // this->grid.reset() ; |
---|
| 46 | // this->file.reset() ; |
---|
[219] | 47 | this->foperation.reset() ; |
---|
[459] | 48 | if (hasExpression) delete expression ; |
---|
| 49 | if (slotUpdateDate!=NULL) delete slotUpdateDate ; |
---|
| 50 | |
---|
[219] | 51 | } |
---|
| 52 | |
---|
[472] | 53 | |
---|
| 54 | //---------------------------------------------------------------- |
---|
| 55 | |
---|
| 56 | void CField::setVirtualVariableGroup(CVariableGroup* newVVariableGroup) |
---|
| 57 | { |
---|
| 58 | this->vVariableGroup = newVVariableGroup; |
---|
| 59 | } |
---|
| 60 | |
---|
| 61 | void CField::setVirtualVariableGroup(void) |
---|
| 62 | { |
---|
| 63 | this->setVirtualVariableGroup(CVariableGroup::create()); |
---|
| 64 | } |
---|
| 65 | |
---|
| 66 | CVariableGroup* CField::getVirtualVariableGroup(void) const |
---|
| 67 | { |
---|
| 68 | return (this->vVariableGroup); |
---|
| 69 | } |
---|
| 70 | |
---|
| 71 | |
---|
| 72 | std::vector<CVariable*> CField::getAllVariables(void) const |
---|
| 73 | { |
---|
| 74 | return (this->vVariableGroup->getAllChildren()); |
---|
| 75 | } |
---|
| 76 | |
---|
| 77 | void CField::solveDescInheritance(bool apply, const CAttributeMap * const parent) |
---|
| 78 | { |
---|
| 79 | SuperClassAttribute::setAttributes(parent,apply); |
---|
| 80 | this->getVirtualVariableGroup()->solveDescInheritance(apply, NULL); |
---|
| 81 | } |
---|
[219] | 82 | //---------------------------------------------------------------- |
---|
[472] | 83 | //---------------------------------------------------------------- |
---|
[219] | 84 | |
---|
| 85 | bool CField::updateDataServer |
---|
[343] | 86 | (const CDate & currDate, |
---|
[369] | 87 | const std::deque< CArray<double, 1>* > storedClient) |
---|
[219] | 88 | { |
---|
[343] | 89 | const CDate opeDate = *last_operation + freq_operation; |
---|
| 90 | const CDate writeDate = *last_Write + freq_write; |
---|
[278] | 91 | |
---|
| 92 | if (opeDate <= currDate) |
---|
[219] | 93 | { |
---|
[369] | 94 | if (this->data.numElements() != this->grid->storeIndex[0]->numElements()) |
---|
[278] | 95 | { |
---|
[369] | 96 | this->data.resize(this->grid->storeIndex[0] ->numElements()); |
---|
[278] | 97 | } |
---|
[369] | 98 | CArray<double,1> input(data.numElements()) ; |
---|
[278] | 99 | this->grid->inputFieldServer(storedClient, input); |
---|
[219] | 100 | (*this->foperation)(input); |
---|
| 101 | *last_operation = currDate; |
---|
| 102 | } |
---|
[278] | 103 | if (writeDate < (currDate + freq_operation)) |
---|
[219] | 104 | { |
---|
[278] | 105 | this->foperation->final(); |
---|
| 106 | this->incrementNStep(); |
---|
| 107 | *last_Write = writeDate; |
---|
| 108 | return (true); |
---|
[219] | 109 | } |
---|
| 110 | return (false); |
---|
| 111 | } |
---|
[300] | 112 | |
---|
| 113 | bool CField::dispatchEvent(CEventServer& event) |
---|
| 114 | { |
---|
| 115 | |
---|
| 116 | if (SuperClass::dispatchEvent(event)) return true ; |
---|
| 117 | else |
---|
| 118 | { |
---|
| 119 | switch(event.type) |
---|
| 120 | { |
---|
| 121 | case EVENT_ID_UPDATE_DATA : |
---|
| 122 | recvUpdateData(event) ; |
---|
| 123 | return true ; |
---|
| 124 | break ; |
---|
[472] | 125 | |
---|
| 126 | case EVENT_ID_ADD_VARIABLE : |
---|
| 127 | recvAddVariable(event) ; |
---|
| 128 | return true ; |
---|
| 129 | break ; |
---|
| 130 | |
---|
| 131 | case EVENT_ID_ADD_VARIABLE_GROUP : |
---|
| 132 | recvAddVariableGroup(event) ; |
---|
| 133 | return true ; |
---|
| 134 | break ; |
---|
[300] | 135 | |
---|
| 136 | default : |
---|
| 137 | ERROR("bool CField::dispatchEvent(CEventServer& event)",<<"Unknown Event") ; |
---|
| 138 | return false ; |
---|
| 139 | } |
---|
| 140 | } |
---|
| 141 | } |
---|
| 142 | |
---|
| 143 | void CField::sendUpdateData(void) |
---|
| 144 | { |
---|
[347] | 145 | CContext* context = CContext::getCurrent() ; |
---|
[300] | 146 | CContextClient* client=context->client ; |
---|
| 147 | |
---|
| 148 | CEventClient event(getType(),EVENT_ID_UPDATE_DATA) ; |
---|
| 149 | |
---|
[369] | 150 | map<int,CArray<int, 1>* >::iterator it ; |
---|
[300] | 151 | list<shared_ptr<CMessage> > list_msg ; |
---|
[369] | 152 | list< CArray<double,1>* > list_data ; |
---|
[300] | 153 | |
---|
| 154 | for(it=grid->storeIndex_toSrv.begin();it!=grid->storeIndex_toSrv.end();it++) |
---|
| 155 | { |
---|
| 156 | int rank=(*it).first ; |
---|
[369] | 157 | CArray<int,1>& index = *(it->second) ; |
---|
| 158 | CArray<double,1> data_tmp(index.numElements()) ; |
---|
| 159 | |
---|
| 160 | for(int n=0;n<data_tmp.numElements();n++) data_tmp(n)=data(index(n)) ; |
---|
[300] | 161 | list_msg.push_back(shared_ptr<CMessage>(new CMessage)) ; |
---|
[369] | 162 | list_data.push_back(new CArray<double,1>(data_tmp)) ; |
---|
| 163 | *list_msg.back()<<getId()<<*list_data.back() ; |
---|
[300] | 164 | event.push(rank,grid->nbSenders[rank],*list_msg.back()) ; |
---|
| 165 | } |
---|
| 166 | client->sendEvent(event) ; |
---|
[369] | 167 | |
---|
| 168 | for(list< CArray<double,1>* >::iterator it=list_data.begin();it!=list_data.end();it++) delete *it ; |
---|
[300] | 169 | } |
---|
| 170 | |
---|
| 171 | void CField::recvUpdateData(CEventServer& event) |
---|
| 172 | { |
---|
| 173 | vector<int> ranks ; |
---|
| 174 | vector<CBufferIn*> buffers ; |
---|
| 175 | |
---|
| 176 | list<CEventServer::SSubEvent>::iterator it ; |
---|
| 177 | string fieldId ; |
---|
[219] | 178 | |
---|
[300] | 179 | for (it=event.subEvents.begin();it!=event.subEvents.end();++it) |
---|
| 180 | { |
---|
| 181 | int rank=it->rank; |
---|
| 182 | CBufferIn* buffer=it->buffer; |
---|
| 183 | *buffer>>fieldId ; |
---|
| 184 | ranks.push_back(rank) ; |
---|
| 185 | buffers.push_back(buffer) ; |
---|
| 186 | } |
---|
| 187 | get(fieldId)->recvUpdateData(ranks,buffers) ; |
---|
| 188 | } |
---|
| 189 | |
---|
| 190 | void CField::recvUpdateData(vector<int>& ranks, vector<CBufferIn*>& buffers) |
---|
| 191 | { |
---|
| 192 | |
---|
| 193 | if (data_srv.empty()) |
---|
| 194 | { |
---|
[369] | 195 | for(map<int, CArray<int, 1>* >::iterator it=grid->out_i_fromClient.begin();it!=grid->out_i_fromClient.end();it++) |
---|
[300] | 196 | { |
---|
| 197 | int rank=it->first ; |
---|
[369] | 198 | CArray<double,1> data_tmp(it->second->numElements()) ; |
---|
| 199 | data_srv.insert( pair<int, CArray<double,1>* >(rank, new CArray<double,1>(data_tmp) ) ) ; |
---|
| 200 | foperation_srv.insert(pair<int,boost::shared_ptr<func::CFunctor> >(rank,boost::shared_ptr<func::CFunctor>(new func::CInstant(*data_srv[rank])))) ; |
---|
[300] | 201 | } |
---|
| 202 | } |
---|
| 203 | |
---|
[347] | 204 | CContext* context = CContext::getCurrent() ; |
---|
[343] | 205 | const CDate & currDate = context->getCalendar()->getCurrentDate(); |
---|
| 206 | const CDate opeDate = *last_operation_srv + freq_operation_srv; |
---|
| 207 | const CDate writeDate = *last_Write_srv + freq_write_srv; |
---|
[300] | 208 | |
---|
| 209 | |
---|
| 210 | |
---|
| 211 | if (opeDate <= currDate) |
---|
| 212 | { |
---|
| 213 | for(int n=0;n<ranks.size();n++) |
---|
| 214 | { |
---|
[369] | 215 | CArray<double,1> data_tmp ; |
---|
[300] | 216 | *buffers[n]>>data_tmp ; |
---|
| 217 | (*foperation_srv[ranks[n]])(data_tmp) ; |
---|
| 218 | } |
---|
| 219 | *last_operation_srv = currDate; |
---|
| 220 | } |
---|
| 221 | |
---|
| 222 | if (writeDate < (currDate + freq_operation_srv)) |
---|
| 223 | { |
---|
| 224 | for(int n=0;n<ranks.size();n++) |
---|
| 225 | { |
---|
| 226 | this->foperation_srv[ranks[n]]->final(); |
---|
| 227 | } |
---|
| 228 | |
---|
| 229 | *last_Write_srv = writeDate; |
---|
| 230 | writeField() ; |
---|
[334] | 231 | *lastlast_Write_srv=*last_Write_srv; |
---|
[300] | 232 | } |
---|
| 233 | } |
---|
| 234 | |
---|
| 235 | void CField::writeField(void) |
---|
| 236 | { |
---|
[379] | 237 | if (! getRelFile()->allDomainEmpty ) |
---|
| 238 | if (! grid->domain->isEmpty() || getRelFile()->type == CFile::type_attr::one_file) |
---|
| 239 | { |
---|
| 240 | getRelFile()->checkFile(); |
---|
| 241 | this->incrementNStep(); |
---|
| 242 | getRelFile()->getDataOutput()->writeFieldData(CField::get(this)); |
---|
| 243 | } |
---|
[300] | 244 | } |
---|
[219] | 245 | //---------------------------------------------------------------- |
---|
| 246 | |
---|
[347] | 247 | void CField::setRelFile(CFile* _file) |
---|
[219] | 248 | { |
---|
[459] | 249 | this->file = _file; |
---|
| 250 | hasOutputFile=true ; |
---|
[219] | 251 | } |
---|
| 252 | |
---|
| 253 | //---------------------------------------------------------------- |
---|
| 254 | |
---|
| 255 | StdString CField::GetName(void) { return (StdString("field")); } |
---|
| 256 | StdString CField::GetDefName(void){ return (CField::GetName()); } |
---|
| 257 | ENodeType CField::GetType(void) { return (eField); } |
---|
| 258 | |
---|
| 259 | //---------------------------------------------------------------- |
---|
| 260 | |
---|
[347] | 261 | CGrid* CField::getRelGrid(void) const |
---|
[219] | 262 | { |
---|
| 263 | return (this->grid); |
---|
| 264 | } |
---|
| 265 | |
---|
| 266 | //---------------------------------------------------------------- |
---|
| 267 | |
---|
[347] | 268 | CFile* CField::getRelFile(void) const |
---|
[219] | 269 | { |
---|
| 270 | return (this->file); |
---|
| 271 | } |
---|
[266] | 272 | |
---|
| 273 | StdSize CField::getNStep(void) const |
---|
| 274 | { |
---|
| 275 | return (this->nstep); |
---|
| 276 | } |
---|
| 277 | |
---|
| 278 | void CField::incrementNStep(void) |
---|
| 279 | { |
---|
| 280 | this->nstep++; |
---|
| 281 | } |
---|
[321] | 282 | |
---|
| 283 | void CField::resetNStep(void) |
---|
| 284 | { |
---|
| 285 | this->nstep=0; |
---|
| 286 | } |
---|
[219] | 287 | |
---|
| 288 | //---------------------------------------------------------------- |
---|
| 289 | |
---|
[347] | 290 | CField* CField::getDirectFieldReference(void) const |
---|
[219] | 291 | { |
---|
| 292 | if (this->field_ref.isEmpty()) |
---|
| 293 | return (this->getBaseFieldReference()); |
---|
| 294 | |
---|
[346] | 295 | if (! CField::has(this->field_ref.getValue())) |
---|
[219] | 296 | ERROR("CField::getDirectFieldReference(void)", |
---|
| 297 | << "[ ref_name = " << this->field_ref.getValue() << "]" |
---|
| 298 | << " invalid field name !"); |
---|
| 299 | |
---|
[346] | 300 | return (CField::get(this->field_ref.getValue())); |
---|
[219] | 301 | } |
---|
| 302 | |
---|
| 303 | //---------------------------------------------------------------- |
---|
| 304 | |
---|
[347] | 305 | CField* CField::getBaseFieldReference(void) const |
---|
[219] | 306 | { |
---|
| 307 | return (baseRefObject); |
---|
| 308 | } |
---|
| 309 | |
---|
| 310 | //---------------------------------------------------------------- |
---|
| 311 | |
---|
[347] | 312 | const std::vector<CField*>& CField::getAllReference(void) const |
---|
[219] | 313 | { |
---|
| 314 | return (refObject); |
---|
| 315 | } |
---|
| 316 | |
---|
| 317 | //---------------------------------------------------------------- |
---|
| 318 | |
---|
| 319 | const StdString & CField::getBaseFieldId(void) const |
---|
| 320 | { |
---|
| 321 | return (this->getBaseFieldReference()->getId()); |
---|
| 322 | } |
---|
| 323 | |
---|
| 324 | //---------------------------------------------------------------- |
---|
| 325 | |
---|
[343] | 326 | const CDuration & CField::getFreqOperation(void) const |
---|
[219] | 327 | { |
---|
| 328 | return (this->freq_operation); |
---|
| 329 | } |
---|
| 330 | |
---|
| 331 | //---------------------------------------------------------------- |
---|
[343] | 332 | const CDuration & CField::getFreqWrite(void) const |
---|
[219] | 333 | { |
---|
| 334 | return (this->freq_write); |
---|
| 335 | } |
---|
| 336 | |
---|
| 337 | //---------------------------------------------------------------- |
---|
| 338 | |
---|
| 339 | boost::shared_ptr<func::CFunctor> CField::getFieldOperation(void) const |
---|
| 340 | { |
---|
| 341 | return (this->foperation); |
---|
| 342 | } |
---|
| 343 | |
---|
| 344 | //---------------------------------------------------------------- |
---|
| 345 | |
---|
| 346 | bool CField::hasDirectFieldReference(void) const |
---|
| 347 | { |
---|
[310] | 348 | return (!this->field_ref.isEmpty()); |
---|
[219] | 349 | } |
---|
| 350 | |
---|
[310] | 351 | bool CField::isActive(void) const |
---|
| 352 | { |
---|
| 353 | return (!this->refObject.empty()); |
---|
| 354 | } |
---|
[219] | 355 | //---------------------------------------------------------------- |
---|
| 356 | |
---|
[369] | 357 | CArray<double, 1> CField::getData(void) const |
---|
[219] | 358 | { |
---|
| 359 | return(this->data); |
---|
| 360 | } |
---|
| 361 | |
---|
| 362 | //---------------------------------------------------------------- |
---|
| 363 | |
---|
[343] | 364 | boost::shared_ptr<CDate> CField::getLastWriteDate(void) const |
---|
[219] | 365 | { |
---|
| 366 | return(this->last_Write); |
---|
| 367 | } |
---|
| 368 | |
---|
| 369 | //---------------------------------------------------------------- |
---|
| 370 | |
---|
[343] | 371 | boost::shared_ptr<CDate> CField::getLastOperationDate(void) const |
---|
[219] | 372 | { |
---|
| 373 | return(this->last_operation); |
---|
| 374 | } |
---|
| 375 | |
---|
| 376 | //---------------------------------------------------------------- |
---|
| 377 | |
---|
[459] | 378 | void CField::processEnabledField(void) |
---|
| 379 | { |
---|
| 380 | if (!processed) |
---|
| 381 | { |
---|
| 382 | processed=true ; |
---|
[478] | 383 | solveBaseReference() ; |
---|
[459] | 384 | solveOperation() ; |
---|
| 385 | solveGridReference() ; |
---|
| 386 | |
---|
| 387 | if (hasDirectFieldReference()) baseRefObject->processEnabledField() ; |
---|
| 388 | buildExpression(); |
---|
| 389 | active=true; |
---|
| 390 | } |
---|
| 391 | } |
---|
[478] | 392 | |
---|
[445] | 393 | void CField::solveRefInheritance(bool apply) |
---|
[219] | 394 | { |
---|
| 395 | std::set<CField *> sset; |
---|
[347] | 396 | CField* refer_sptr; |
---|
[219] | 397 | CField * refer_ptr = this; |
---|
[478] | 398 | |
---|
| 399 | while (refer_ptr->hasDirectFieldReference()) |
---|
| 400 | { |
---|
| 401 | refer_sptr = refer_ptr->getDirectFieldReference(); |
---|
| 402 | refer_ptr = refer_sptr; |
---|
| 403 | |
---|
| 404 | if(sset.end() != sset.find(refer_ptr)) |
---|
| 405 | { |
---|
| 406 | DEBUG (<< "Circular dependency stopped for field object on " |
---|
| 407 | << "\"" + refer_ptr->getId() + "\" !"); |
---|
| 408 | break; |
---|
| 409 | } |
---|
| 410 | |
---|
| 411 | SuperClassAttribute::setAttributes(refer_ptr, apply); |
---|
| 412 | sset.insert(refer_ptr); |
---|
| 413 | } |
---|
[219] | 414 | |
---|
[478] | 415 | } |
---|
| 416 | |
---|
| 417 | void CField::solveBaseReference(void) |
---|
| 418 | { |
---|
| 419 | std::set<CField *> sset; |
---|
| 420 | CField* refer_sptr; |
---|
| 421 | CField * refer_ptr = this; |
---|
| 422 | |
---|
[459] | 423 | if (this->hasDirectFieldReference()) baseRefObject = getDirectFieldReference(); |
---|
| 424 | else baseRefObject = CField::get(this); |
---|
[219] | 425 | |
---|
| 426 | while (refer_ptr->hasDirectFieldReference()) |
---|
| 427 | { |
---|
| 428 | refer_sptr = refer_ptr->getDirectFieldReference(); |
---|
[347] | 429 | refer_ptr = refer_sptr; |
---|
[219] | 430 | |
---|
| 431 | if(sset.end() != sset.find(refer_ptr)) |
---|
| 432 | { |
---|
[478] | 433 | DEBUG (<< "Circular dependency stopped for field object on " |
---|
[219] | 434 | << "\"" + refer_ptr->getId() + "\" !"); |
---|
| 435 | break; |
---|
| 436 | } |
---|
| 437 | |
---|
| 438 | sset.insert(refer_ptr); |
---|
| 439 | } |
---|
[459] | 440 | |
---|
| 441 | if (hasDirectFieldReference()) baseRefObject->addReference(this) ; |
---|
[219] | 442 | } |
---|
[478] | 443 | |
---|
[219] | 444 | //---------------------------------------------------------------- |
---|
| 445 | |
---|
| 446 | void CField::solveOperation(void) |
---|
| 447 | { |
---|
| 448 | using namespace func; |
---|
[459] | 449 | |
---|
[460] | 450 | if (!hasOutputFile && !hasFieldOut) return ; |
---|
[459] | 451 | |
---|
| 452 | StdString id ; |
---|
| 453 | if (hasId()) id=getId(); |
---|
| 454 | else if (!name.isEmpty()) id=name ; |
---|
| 455 | else if (hasDirectFieldReference()) id=baseRefObject->getId() ; |
---|
| 456 | |
---|
[347] | 457 | CContext* context = CContext::getCurrent(); |
---|
[436] | 458 | |
---|
| 459 | if (freq_op.isEmpty()) freq_op=string("1ts") ; |
---|
| 460 | |
---|
[459] | 461 | if (operation.isEmpty() ) |
---|
[219] | 462 | { |
---|
| 463 | ERROR("CField::solveOperation(void)", |
---|
| 464 | << "[ id = " << id << "]" |
---|
[421] | 465 | << "Impossible to define an operation for this field !"); |
---|
[219] | 466 | } |
---|
[278] | 467 | |
---|
| 468 | CDuration freq_offset_ = NoneDu; |
---|
| 469 | if (!freq_offset.isEmpty()) |
---|
| 470 | { |
---|
| 471 | freq_offset_ = CDuration::FromString(freq_offset.getValue()); |
---|
| 472 | } |
---|
| 473 | else |
---|
| 474 | { |
---|
| 475 | freq_offset.setValue(NoneDu.toString()); |
---|
| 476 | } |
---|
[219] | 477 | |
---|
[300] | 478 | // if (CXIOSManager::GetStatus() == CXIOSManager::LOC_SERVER) |
---|
| 479 | if (context->hasServer) |
---|
[219] | 480 | { |
---|
[460] | 481 | if (hasOutputFile) |
---|
| 482 | { |
---|
| 483 | this->freq_operation_srv =CDuration::FromString(this->file->output_freq.getValue()); |
---|
| 484 | this->freq_write_srv = CDuration::FromString(this->file->output_freq.getValue()); |
---|
| 485 | } |
---|
[343] | 486 | this->lastlast_Write_srv = boost::shared_ptr<CDate> |
---|
| 487 | (new CDate(context->getCalendar()->getInitDate())); |
---|
| 488 | this->last_Write_srv = boost::shared_ptr<CDate> |
---|
| 489 | (new CDate(context->getCalendar()->getInitDate())); |
---|
| 490 | this->last_operation_srv = boost::shared_ptr<CDate> |
---|
| 491 | (new CDate(context->getCalendar()->getInitDate())); |
---|
[300] | 492 | // this->foperation_srv = |
---|
| 493 | // boost::shared_ptr<func::CFunctor>(new CInstant(this->data_srv)); |
---|
[278] | 494 | |
---|
[460] | 495 | if (hasOutputFile) |
---|
| 496 | { |
---|
| 497 | const CDuration toffset = this->freq_operation_srv - freq_offset_ - context->getCalendar()->getTimeStep(); |
---|
| 498 | *this->last_operation_srv = *this->last_operation_srv - toffset; |
---|
| 499 | } |
---|
[219] | 500 | } |
---|
[300] | 501 | |
---|
[449] | 502 | // if (context->hasClient) |
---|
| 503 | // { |
---|
[219] | 504 | this->freq_operation = CDuration::FromString(freq_op.getValue()); |
---|
[460] | 505 | if (hasOutputFile) this->freq_write = CDuration::FromString(this->file->output_freq.getValue()); |
---|
| 506 | if (hasFieldOut) |
---|
| 507 | { |
---|
| 508 | this->freq_write = CDuration::FromString(this->fieldOut->freq_op.getValue()); |
---|
| 509 | } |
---|
[343] | 510 | this->last_Write = boost::shared_ptr<CDate> |
---|
| 511 | (new CDate(context->getCalendar()->getInitDate())); |
---|
| 512 | this->last_operation = boost::shared_ptr<CDate> |
---|
| 513 | (new CDate(context->getCalendar()->getInitDate())); |
---|
[278] | 514 | |
---|
[300] | 515 | const CDuration toffset = this->freq_operation - freq_offset_ - context->getCalendar()->getTimeStep(); |
---|
[278] | 516 | *this->last_operation = *this->last_operation - toffset; |
---|
[436] | 517 | |
---|
| 518 | if (operation.get()=="once") isOnceOperation=true ; |
---|
| 519 | else isOnceOperation=false; |
---|
| 520 | isFirstOperation=true; |
---|
[470] | 521 | |
---|
| 522 | |
---|
[219] | 523 | #define DECLARE_FUNCTOR(MType, mtype) \ |
---|
| 524 | if (operation.getValue().compare(#mtype) == 0) \ |
---|
| 525 | { \ |
---|
[470] | 526 | if (!detect_missing_value.isEmpty() && !default_value.isEmpty() && detect_missing_value==true) \ |
---|
| 527 | { \ |
---|
| 528 | boost::shared_ptr<func::CFunctor> foperation_(new C##MType(this->data,default_value)); \ |
---|
[473] | 529 | this->foperation = foperation_; \ |
---|
[470] | 530 | } \ |
---|
| 531 | else \ |
---|
| 532 | { \ |
---|
| 533 | boost::shared_ptr<func::CFunctor> foperation_(new C##MType(this->data)); \ |
---|
| 534 | this->foperation = foperation_; \ |
---|
| 535 | } \ |
---|
[219] | 536 | return; \ |
---|
| 537 | } |
---|
| 538 | |
---|
| 539 | #include "functor_type.conf" |
---|
| 540 | |
---|
| 541 | ERROR("CField::solveOperation(void)", |
---|
| 542 | << "[ operation = " << operation.getValue() << "]" |
---|
[421] | 543 | << "The operation is not defined !"); |
---|
[449] | 544 | // } |
---|
[436] | 545 | |
---|
| 546 | |
---|
[219] | 547 | } |
---|
| 548 | |
---|
| 549 | //---------------------------------------------------------------- |
---|
[369] | 550 | /* |
---|
[219] | 551 | void CField::fromBinary(StdIStream & is) |
---|
| 552 | { |
---|
| 553 | SuperClass::fromBinary(is); |
---|
| 554 | #define CLEAR_ATT(name_)\ |
---|
[369] | 555 | SuperClassAttribute::operator[](#name_)->reset() |
---|
[219] | 556 | |
---|
| 557 | CLEAR_ATT(domain_ref); |
---|
| 558 | CLEAR_ATT(axis_ref); |
---|
| 559 | #undef CLEAR_ATT |
---|
| 560 | |
---|
| 561 | } |
---|
[369] | 562 | */ |
---|
[219] | 563 | //---------------------------------------------------------------- |
---|
| 564 | |
---|
| 565 | void CField::solveGridReference(void) |
---|
| 566 | { |
---|
[347] | 567 | CDomain* domain; |
---|
| 568 | CAxis* axis; |
---|
[219] | 569 | |
---|
| 570 | if (!domain_ref.isEmpty()) |
---|
| 571 | { |
---|
[346] | 572 | if (CDomain::has(domain_ref.getValue())) |
---|
| 573 | domain = CDomain::get(domain_ref.getValue()) ; |
---|
[219] | 574 | else |
---|
| 575 | ERROR("CField::solveGridReference(void)", |
---|
[421] | 576 | << "Reference to the domain \'" |
---|
| 577 | << domain_ref.getValue() << "\' is wrong") ; |
---|
[219] | 578 | } |
---|
| 579 | |
---|
| 580 | if (!axis_ref.isEmpty()) |
---|
| 581 | { |
---|
[346] | 582 | if (CAxis::has(axis_ref.getValue())) |
---|
| 583 | axis = CAxis::get(axis_ref.getValue()) ; |
---|
[219] | 584 | else |
---|
| 585 | ERROR("CField::solveGridReference(void)", |
---|
[421] | 586 | << "Reference to the axis \'" |
---|
| 587 | << axis_ref.getValue() <<"\' is wrong") ; |
---|
[219] | 588 | } |
---|
| 589 | |
---|
| 590 | if (!grid_ref.isEmpty()) |
---|
| 591 | { |
---|
[346] | 592 | if (CGrid::has(grid_ref.getValue())) |
---|
| 593 | this->grid = CGrid::get(grid_ref.getValue()) ; |
---|
[219] | 594 | else |
---|
| 595 | ERROR("CField::solveGridReference(void)", |
---|
[421] | 596 | << "Reference to the grid \'" |
---|
| 597 | << grid_ref.getValue() << "\' is wrong"); |
---|
[219] | 598 | } |
---|
[418] | 599 | |
---|
| 600 | if (grid_ref.isEmpty() && domain_ref.isEmpty()) |
---|
[219] | 601 | { |
---|
| 602 | ERROR("CField::solveGridReference(void)", |
---|
[421] | 603 | << "The horizontal domain for this field is not defined"); |
---|
[418] | 604 | |
---|
| 605 | } |
---|
| 606 | |
---|
| 607 | CType<string> goodDomain ; |
---|
| 608 | CType<string> goodAxis ; |
---|
| 609 | if (!grid_ref.isEmpty()) |
---|
| 610 | { |
---|
| 611 | if (!grid->domain_ref.isEmpty()) goodDomain=grid->domain_ref ; |
---|
| 612 | if (!grid->axis_ref.isEmpty()) goodAxis=grid->axis_ref ; |
---|
| 613 | } |
---|
| 614 | if (!domain_ref.isEmpty()) goodDomain=domain_ref ; |
---|
| 615 | if (!axis_ref.isEmpty()) goodAxis=axis_ref ; |
---|
| 616 | |
---|
| 617 | |
---|
| 618 | if (goodDomain.isEmpty()) |
---|
| 619 | { |
---|
[421] | 620 | ERROR("CField::solveGridReference(void)", << "The horizontal domain for this field is not defined"); |
---|
[418] | 621 | } |
---|
| 622 | else |
---|
| 623 | { |
---|
| 624 | if (CDomain::has(goodDomain)) domain = CDomain::get(goodDomain) ; |
---|
[421] | 625 | else ERROR("CField::solveGridReference(void)",<< "Reference to the domain \'"<<goodDomain.get() << "\' is wrong") ; |
---|
[418] | 626 | } |
---|
| 627 | |
---|
| 628 | if (!goodAxis.isEmpty()) |
---|
| 629 | { |
---|
| 630 | if (CAxis::has(goodAxis)) axis = CAxis::get(goodAxis) ; |
---|
[421] | 631 | else ERROR("CField::solveGridReference(void)", << "Reference to the axis \'" |
---|
| 632 | << goodAxis.get() <<"\' is wrong") ; |
---|
[418] | 633 | } |
---|
| 634 | |
---|
| 635 | bool nothingToDo=false ; |
---|
| 636 | |
---|
| 637 | if (!grid_ref.isEmpty()) |
---|
| 638 | { |
---|
| 639 | if (!grid->domain_ref.isEmpty() && goodDomain.get() == grid->domain_ref.get()) |
---|
| 640 | if (goodAxis.isEmpty()) nothingToDo=true ; |
---|
| 641 | else if (!grid->axis_ref.isEmpty()) |
---|
| 642 | if (grid->axis_ref.get()==goodAxis.get()) nothingToDo=true ; |
---|
| 643 | } |
---|
| 644 | |
---|
| 645 | if (!nothingToDo) |
---|
| 646 | { |
---|
| 647 | if (!goodAxis.isEmpty()) |
---|
| 648 | { |
---|
| 649 | this->grid = CGrid::createGrid(domain, axis) ; |
---|
| 650 | this->grid_ref.setValue(this->grid->getId()); |
---|
| 651 | } |
---|
| 652 | else |
---|
| 653 | { |
---|
| 654 | this->grid = CGrid::createGrid(domain) ; |
---|
| 655 | this->grid_ref.setValue(this->grid->getId()); |
---|
| 656 | } |
---|
| 657 | } |
---|
| 658 | |
---|
| 659 | grid->solveReference() ; |
---|
[459] | 660 | |
---|
[219] | 661 | } |
---|
| 662 | |
---|
| 663 | |
---|
| 664 | ///------------------------------------------------------------------- |
---|
| 665 | |
---|
| 666 | template <> |
---|
| 667 | void CGroupTemplate<CField, CFieldGroup, CFieldAttributes>::solveRefInheritance(void) |
---|
| 668 | { |
---|
| 669 | if (this->group_ref.isEmpty()) return; |
---|
| 670 | StdString gref = this->group_ref.getValue(); |
---|
| 671 | |
---|
[346] | 672 | if (!CFieldGroup::has(gref)) |
---|
[219] | 673 | ERROR("CGroupTemplate<CField, CFieldGroup, CFieldAttributes>::solveRefInheritance(void)", |
---|
| 674 | << "[ gref = " << gref << "]" |
---|
| 675 | << " invalid group name !"); |
---|
| 676 | |
---|
[347] | 677 | CFieldGroup* group = CFieldGroup::get(gref); |
---|
| 678 | CFieldGroup* owner = CFieldGroup::get(boost::polymorphic_downcast<CFieldGroup*>(this)); |
---|
[219] | 679 | |
---|
[347] | 680 | std::vector<CField*> allChildren = group->getAllChildren(); |
---|
| 681 | std::vector<CField*>::iterator |
---|
[219] | 682 | it = allChildren.begin(), end = allChildren.end(); |
---|
| 683 | |
---|
| 684 | for (; it != end; it++) |
---|
| 685 | { |
---|
[347] | 686 | CField* child = *it; |
---|
[346] | 687 | if (child->hasId()) owner->createChild()->field_ref.setValue(child->getId()) ; |
---|
| 688 | |
---|
[219] | 689 | } |
---|
| 690 | } |
---|
[300] | 691 | |
---|
[464] | 692 | void CField::scaleFactorAddOffset(double scaleFactor, double addOffset) |
---|
| 693 | { |
---|
| 694 | map<int, CArray<double,1>* >::iterator it; |
---|
| 695 | for(it=data_srv.begin();it!=data_srv.end();it++) *it->second = (*it->second -addOffset) * 1./scaleFactor ; |
---|
| 696 | } |
---|
| 697 | |
---|
[369] | 698 | void CField::outputField(CArray<double,3>& fieldOut) |
---|
[300] | 699 | { |
---|
[369] | 700 | map<int, CArray<double,1>* >::iterator it; |
---|
[300] | 701 | for(it=data_srv.begin();it!=data_srv.end();it++) |
---|
[369] | 702 | grid->outputField(it->first,*it->second, fieldOut) ; |
---|
[300] | 703 | |
---|
| 704 | } |
---|
| 705 | |
---|
[369] | 706 | void CField::outputField(CArray<double,2>& fieldOut) |
---|
[300] | 707 | { |
---|
[369] | 708 | map<int, CArray<double,1>* >::iterator it; |
---|
[219] | 709 | |
---|
[300] | 710 | for(it=data_srv.begin();it!=data_srv.end();it++) |
---|
| 711 | { |
---|
[369] | 712 | grid->outputField(it->first,*it->second, fieldOut) ; |
---|
[300] | 713 | } |
---|
| 714 | } |
---|
[219] | 715 | ///------------------------------------------------------------------- |
---|
| 716 | |
---|
[459] | 717 | void CField::parse(xml::CXMLNode & node) |
---|
| 718 | { |
---|
| 719 | SuperClass::parse(node); |
---|
[476] | 720 | if (! node.getContent(this->content)) |
---|
[472] | 721 | { |
---|
[476] | 722 | if (node.goToChildElement()) |
---|
[472] | 723 | { |
---|
[476] | 724 | do |
---|
| 725 | { |
---|
| 726 | if (node.getElementName()=="variable" || node.getElementName()=="variable_group") this->getVirtualVariableGroup()->parseChild(node); |
---|
| 727 | } while (node.goToNextElement()) ; |
---|
| 728 | node.goToParentElement(); |
---|
| 729 | } |
---|
[472] | 730 | } |
---|
[459] | 731 | } |
---|
| 732 | |
---|
| 733 | CArray<double,1>* CField::getInstantData(void) |
---|
| 734 | { |
---|
| 735 | if (!hasInstantData) |
---|
| 736 | { |
---|
| 737 | instantData.resize(grid->storeIndex_client.numElements()) ; |
---|
| 738 | hasInstantData=true ; |
---|
| 739 | } |
---|
| 740 | return &instantData ; |
---|
| 741 | } |
---|
| 742 | |
---|
| 743 | void CField::addReference(CField* field) |
---|
| 744 | { |
---|
| 745 | refObject.push_back(field) ; |
---|
| 746 | } |
---|
| 747 | |
---|
| 748 | void CField::addDependency(CField* field, int slotId) |
---|
| 749 | { |
---|
| 750 | fieldDependency.push_back(pair<CField*,int>(field,slotId)) ; |
---|
| 751 | } |
---|
| 752 | |
---|
| 753 | void CField::buildExpression(void) |
---|
| 754 | { |
---|
| 755 | if (content.size() > 0) |
---|
| 756 | { |
---|
| 757 | CSimpleNodeExpr* simpleExpr=parseExpr(content+'\0') ; |
---|
| 758 | expression=CFieldNode::newNode(simpleExpr) ; |
---|
| 759 | delete simpleExpr ; |
---|
[460] | 760 | set<string> instantFieldIds ; |
---|
| 761 | map<string,CField*> associatedInstantFieldIds ; |
---|
| 762 | expression->getInstantFieldIds(instantFieldIds) ; |
---|
| 763 | for (set<string>::iterator it=instantFieldIds.begin() ; it!=instantFieldIds.end();++it) |
---|
| 764 | { |
---|
| 765 | if (*it!="this") |
---|
| 766 | { |
---|
| 767 | if (CField::has(*it)) |
---|
| 768 | { |
---|
| 769 | CField* field=CField::get(*it) ; |
---|
| 770 | field->processEnabledField() ; |
---|
| 771 | associatedInstantFieldIds[*it]=field ; |
---|
| 772 | } |
---|
| 773 | else ERROR("void CField::buildExpression(void)",<<" Field "<<*it<<" does not exist") ; |
---|
| 774 | } |
---|
| 775 | } |
---|
[459] | 776 | |
---|
[460] | 777 | set<string> averageFieldIds ; |
---|
| 778 | map<string,CField*> associatedAverageFieldIds ; |
---|
[459] | 779 | |
---|
[460] | 780 | expression->getAverageFieldIds(averageFieldIds) ; |
---|
| 781 | for (set<string>::iterator it=averageFieldIds.begin() ; it!=averageFieldIds.end();++it) |
---|
| 782 | { |
---|
| 783 | if (CField::has(*it)) |
---|
| 784 | { |
---|
| 785 | CFieldGroup* root=CFieldGroup::get("field_definition") ; |
---|
| 786 | CField* averageField=root->createChild() ; |
---|
| 787 | CField* instantField=root->createChild() ; |
---|
| 788 | averageField->field_ref=*it ; |
---|
| 789 | averageField->hasFieldOut=true ; |
---|
| 790 | averageField->fieldOut=instantField ; |
---|
| 791 | instantField->freq_op=freq_op ; |
---|
| 792 | averageField-> processEnabledField() ; |
---|
| 793 | cout<<" outputfreq of averageField "<< freq_op <<" "<<instantField->freq_op<<" "<< averageField->freq_write << endl ; |
---|
| 794 | instantField->SuperClassAttribute::setAttributes(averageField, true); |
---|
| 795 | instantField->field_ref.reset() ; |
---|
| 796 | instantField->operation.reset() ; |
---|
| 797 | |
---|
| 798 | instantField-> processEnabledField() ; |
---|
| 799 | associatedAverageFieldIds[*it]=instantField ; |
---|
| 800 | } |
---|
| 801 | else ERROR("void CField::buildExpression(void)",<<" Field "<<*it<<" does not exist") ; |
---|
| 802 | } |
---|
| 803 | |
---|
| 804 | expression->reduce(this,associatedInstantFieldIds,associatedAverageFieldIds) ; |
---|
| 805 | |
---|
| 806 | slots.resize(instantFieldIds.size()+averageFieldIds.size()) ; |
---|
[459] | 807 | resetSlots() ; |
---|
| 808 | int slotId=0 ; |
---|
| 809 | set<CField*> fields ; |
---|
| 810 | expression->getFields(fields) ; |
---|
| 811 | for (set<CField*>::iterator it=fields.begin() ; it!=fields.end();++it,++slotId) (*it)->addDependency(this,slotId) ; |
---|
| 812 | hasExpression=true; |
---|
| 813 | } |
---|
| 814 | } |
---|
| 815 | |
---|
| 816 | void CField::resetSlots(void) |
---|
| 817 | { |
---|
| 818 | for(vector<bool>::iterator it=slots.begin();it!=slots.end();++it) *it=false ; |
---|
| 819 | } |
---|
| 820 | |
---|
| 821 | bool CField::slotsFull(void) |
---|
| 822 | { |
---|
| 823 | bool ret=true ; |
---|
| 824 | for(vector<bool>::iterator it=slots.begin();it!=slots.end();++it) ret &= *it; |
---|
| 825 | return ret ; |
---|
| 826 | } |
---|
| 827 | |
---|
| 828 | |
---|
| 829 | void CField::setSlot(int slotId) |
---|
| 830 | { |
---|
| 831 | CContext* context = CContext::getCurrent() ; |
---|
| 832 | const CDate & currDate = context->getCalendar()->getCurrentDate(); |
---|
| 833 | if (slotUpdateDate==NULL || currDate!=*slotUpdateDate) |
---|
| 834 | { |
---|
| 835 | resetSlots() ; |
---|
| 836 | if (slotUpdateDate==NULL) slotUpdateDate=new CDate(currDate) ; |
---|
| 837 | else *slotUpdateDate=currDate ; |
---|
| 838 | } |
---|
| 839 | slots[slotId]=true ; |
---|
| 840 | if (slotsFull()) |
---|
| 841 | { |
---|
| 842 | CArray<double,1> expr(expression->compute()) ; |
---|
| 843 | |
---|
| 844 | if (hasInstantData) |
---|
| 845 | { |
---|
| 846 | instantData=expr ; |
---|
| 847 | for(list< pair<CField *,int> >::iterator it=fieldDependency.begin(); it!=fieldDependency.end(); ++it) |
---|
| 848 | if (it->first!=this) it->first->setSlot(it->second) ; |
---|
| 849 | } |
---|
| 850 | |
---|
| 851 | if (hasOutputFile) updateDataFromExpression(expr) ; |
---|
| 852 | |
---|
| 853 | } |
---|
| 854 | } |
---|
[472] | 855 | |
---|
| 856 | |
---|
| 857 | |
---|
| 858 | CVariable* CField::addVariable(const string& id) |
---|
| 859 | { |
---|
| 860 | return vVariableGroup->createChild(id) ; |
---|
| 861 | } |
---|
| 862 | |
---|
| 863 | CVariableGroup* CField::addVariableGroup(const string& id) |
---|
| 864 | { |
---|
| 865 | return vVariableGroup->createChildGroup(id) ; |
---|
| 866 | } |
---|
| 867 | |
---|
| 868 | |
---|
| 869 | void CField::sendAddVariable(const string& id) |
---|
| 870 | { |
---|
| 871 | CContext* context=CContext::getCurrent() ; |
---|
| 872 | |
---|
| 873 | if (! context->hasServer ) |
---|
| 874 | { |
---|
| 875 | CContextClient* client=context->client ; |
---|
| 876 | |
---|
| 877 | CEventClient event(this->getType(),EVENT_ID_ADD_VARIABLE) ; |
---|
| 878 | if (client->isServerLeader()) |
---|
| 879 | { |
---|
| 880 | CMessage msg ; |
---|
| 881 | msg<<this->getId() ; |
---|
| 882 | msg<<id ; |
---|
| 883 | event.push(client->getServerLeader(),1,msg) ; |
---|
| 884 | client->sendEvent(event) ; |
---|
| 885 | } |
---|
| 886 | else client->sendEvent(event) ; |
---|
| 887 | } |
---|
| 888 | |
---|
| 889 | } |
---|
| 890 | |
---|
| 891 | |
---|
| 892 | void CField::sendAddVariableGroup(const string& id) |
---|
| 893 | { |
---|
| 894 | CContext* context=CContext::getCurrent() ; |
---|
| 895 | if (! context->hasServer ) |
---|
| 896 | { |
---|
| 897 | CContextClient* client=context->client ; |
---|
| 898 | |
---|
| 899 | CEventClient event(this->getType(),EVENT_ID_ADD_VARIABLE_GROUP) ; |
---|
| 900 | if (client->isServerLeader()) |
---|
| 901 | { |
---|
| 902 | CMessage msg ; |
---|
| 903 | msg<<this->getId() ; |
---|
| 904 | msg<<id ; |
---|
| 905 | event.push(client->getServerLeader(),1,msg) ; |
---|
| 906 | client->sendEvent(event) ; |
---|
| 907 | } |
---|
| 908 | else client->sendEvent(event) ; |
---|
| 909 | } |
---|
| 910 | |
---|
| 911 | } |
---|
| 912 | |
---|
| 913 | void CField::recvAddVariable(CEventServer& event) |
---|
| 914 | { |
---|
| 915 | |
---|
| 916 | CBufferIn* buffer=event.subEvents.begin()->buffer; |
---|
| 917 | string id; |
---|
| 918 | *buffer>>id ; |
---|
| 919 | get(id)->recvAddVariable(*buffer) ; |
---|
| 920 | } |
---|
| 921 | |
---|
| 922 | |
---|
| 923 | void CField::recvAddVariable(CBufferIn& buffer) |
---|
| 924 | { |
---|
| 925 | string id ; |
---|
| 926 | buffer>>id ; |
---|
| 927 | addVariable(id) ; |
---|
| 928 | } |
---|
| 929 | |
---|
| 930 | void CField::recvAddVariableGroup(CEventServer& event) |
---|
| 931 | { |
---|
| 932 | |
---|
| 933 | CBufferIn* buffer=event.subEvents.begin()->buffer; |
---|
| 934 | string id; |
---|
| 935 | *buffer>>id ; |
---|
| 936 | get(id)->recvAddVariableGroup(*buffer) ; |
---|
| 937 | } |
---|
| 938 | |
---|
| 939 | |
---|
| 940 | void CField::recvAddVariableGroup(CBufferIn& buffer) |
---|
| 941 | { |
---|
| 942 | string id ; |
---|
| 943 | buffer>>id ; |
---|
| 944 | addVariableGroup(id) ; |
---|
| 945 | } |
---|
| 946 | |
---|
| 947 | |
---|
| 948 | |
---|
[459] | 949 | |
---|
[335] | 950 | } // namespace xios |
---|