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 "xios_spl.hpp" |
---|
11 | #include "type.hpp" |
---|
12 | #include "timer.hpp" |
---|
13 | #include "context_client.hpp" |
---|
14 | #include "context_server.hpp" |
---|
15 | #include <set> |
---|
16 | #include "garbage_collector.hpp" |
---|
17 | #include "source_filter.hpp" |
---|
18 | #include "store_filter.hpp" |
---|
19 | #include "file_writer_filter.hpp" |
---|
20 | #include "pass_through_filter.hpp" |
---|
21 | #include "filter_expr_node.hpp" |
---|
22 | #include "lex_parser.hpp" |
---|
23 | #include "temporal_filter.hpp" |
---|
24 | #include "spatial_transform_filter.hpp" |
---|
25 | |
---|
26 | namespace xios{ |
---|
27 | |
---|
28 | /// ////////////////////// Définitions ////////////////////// /// |
---|
29 | |
---|
30 | CField::CField(void) |
---|
31 | : CObjectTemplate<CField>(), CFieldAttributes() |
---|
32 | , grid(), file() |
---|
33 | , written(false) |
---|
34 | , nstep(0), nstepMax(0) |
---|
35 | , hasOutputFile(false) |
---|
36 | , domAxisScalarIds_(vector<StdString>(3,"")), areAllReferenceSolved(false), isReferenceSolved(false) |
---|
37 | , useCompressedOutput(false) |
---|
38 | , isReadDataRequestPending(false) |
---|
39 | { setVirtualVariableGroup(); } |
---|
40 | |
---|
41 | CField::CField(const StdString& id) |
---|
42 | : CObjectTemplate<CField>(id), CFieldAttributes() |
---|
43 | , grid(), file() |
---|
44 | , written(false) |
---|
45 | , nstep(0), nstepMax(0) |
---|
46 | , hasOutputFile(false) |
---|
47 | , domAxisScalarIds_(vector<StdString>(3,"")), areAllReferenceSolved(false), isReferenceSolved(false) |
---|
48 | , useCompressedOutput(false) |
---|
49 | , isReadDataRequestPending(false) |
---|
50 | { setVirtualVariableGroup(); } |
---|
51 | |
---|
52 | CField::~CField(void) |
---|
53 | {} |
---|
54 | |
---|
55 | //---------------------------------------------------------------- |
---|
56 | |
---|
57 | void CField::setVirtualVariableGroup(CVariableGroup* newVVariableGroup) |
---|
58 | { |
---|
59 | this->vVariableGroup = newVVariableGroup; |
---|
60 | } |
---|
61 | |
---|
62 | void CField::setVirtualVariableGroup(void) |
---|
63 | { |
---|
64 | this->setVirtualVariableGroup(CVariableGroup::create()); |
---|
65 | } |
---|
66 | |
---|
67 | CVariableGroup* CField::getVirtualVariableGroup(void) const |
---|
68 | { |
---|
69 | return this->vVariableGroup; |
---|
70 | } |
---|
71 | |
---|
72 | |
---|
73 | std::vector<CVariable*> CField::getAllVariables(void) const |
---|
74 | { |
---|
75 | return this->vVariableGroup->getAllChildren(); |
---|
76 | } |
---|
77 | |
---|
78 | void CField::solveDescInheritance(bool apply, const CAttributeMap* const parent) |
---|
79 | { |
---|
80 | SuperClassAttribute::setAttributes(parent, apply); |
---|
81 | this->getVirtualVariableGroup()->solveDescInheritance(apply, NULL); |
---|
82 | } |
---|
83 | |
---|
84 | //---------------------------------------------------------------- |
---|
85 | |
---|
86 | bool CField::dispatchEvent(CEventServer& event) |
---|
87 | { |
---|
88 | if (SuperClass::dispatchEvent(event)) return true; |
---|
89 | else |
---|
90 | { |
---|
91 | switch(event.type) |
---|
92 | { |
---|
93 | case EVENT_ID_UPDATE_DATA : |
---|
94 | recvUpdateData(event); |
---|
95 | return true; |
---|
96 | break; |
---|
97 | |
---|
98 | case EVENT_ID_READ_DATA : |
---|
99 | recvReadDataRequest(event); |
---|
100 | return true; |
---|
101 | break; |
---|
102 | |
---|
103 | case EVENT_ID_READ_DATA_READY : |
---|
104 | recvReadDataReady(event); |
---|
105 | return true; |
---|
106 | break; |
---|
107 | |
---|
108 | case EVENT_ID_ADD_VARIABLE : |
---|
109 | recvAddVariable(event); |
---|
110 | return true; |
---|
111 | break; |
---|
112 | |
---|
113 | case EVENT_ID_ADD_VARIABLE_GROUP : |
---|
114 | recvAddVariableGroup(event); |
---|
115 | return true; |
---|
116 | break; |
---|
117 | |
---|
118 | default : |
---|
119 | ERROR("bool CField::dispatchEvent(CEventServer& event)", << "Unknown Event"); |
---|
120 | return false; |
---|
121 | } |
---|
122 | } |
---|
123 | } |
---|
124 | |
---|
125 | void CField::sendUpdateData(const CArray<double,1>& data) |
---|
126 | { |
---|
127 | CTimer::get("XIOS Send Data").resume(); |
---|
128 | |
---|
129 | CContext* context = CContext::getCurrent(); |
---|
130 | CContextClient* client = context->client; |
---|
131 | |
---|
132 | CEventClient event(getType(), EVENT_ID_UPDATE_DATA); |
---|
133 | |
---|
134 | map<int, CArray<int,1> >::iterator it; |
---|
135 | list<CMessage> list_msg; |
---|
136 | list<CArray<double,1> > list_data; |
---|
137 | |
---|
138 | if (!grid->doGridHaveDataDistributed()) |
---|
139 | { |
---|
140 | if (0 == client->clientRank) |
---|
141 | { |
---|
142 | for (it = grid->storeIndex_toSrv.begin(); it != grid->storeIndex_toSrv.end(); it++) |
---|
143 | { |
---|
144 | int rank = it->first; |
---|
145 | CArray<int,1>& index = it->second; |
---|
146 | |
---|
147 | list_msg.push_back(CMessage()); |
---|
148 | list_data.push_back(CArray<double,1>(index.numElements())); |
---|
149 | |
---|
150 | CArray<double,1>& data_tmp = list_data.back(); |
---|
151 | for (int n = 0; n < data_tmp.numElements(); n++) data_tmp(n) = data(index(n)); |
---|
152 | |
---|
153 | list_msg.back() << getId() << data_tmp; |
---|
154 | event.push(rank, 1, list_msg.back()); |
---|
155 | } |
---|
156 | client->sendEvent(event); |
---|
157 | } else client->sendEvent(event); |
---|
158 | } |
---|
159 | else |
---|
160 | { |
---|
161 | for (it = grid->storeIndex_toSrv.begin(); it != grid->storeIndex_toSrv.end(); it++) |
---|
162 | { |
---|
163 | int rank = it->first; |
---|
164 | CArray<int,1>& index = it->second; |
---|
165 | |
---|
166 | list_msg.push_back(CMessage()); |
---|
167 | list_data.push_back(CArray<double,1>(index.numElements())); |
---|
168 | |
---|
169 | CArray<double,1>& data_tmp = list_data.back(); |
---|
170 | for (int n = 0; n < data_tmp.numElements(); n++) data_tmp(n) = data(index(n)); |
---|
171 | |
---|
172 | list_msg.back() << getId() << data_tmp; |
---|
173 | event.push(rank, grid->nbSenders[rank], list_msg.back()); |
---|
174 | } |
---|
175 | client->sendEvent(event); |
---|
176 | } |
---|
177 | |
---|
178 | CTimer::get("XIOS Send Data").suspend(); |
---|
179 | } |
---|
180 | |
---|
181 | void CField::recvUpdateData(CEventServer& event) |
---|
182 | { |
---|
183 | vector<int> ranks; |
---|
184 | vector<CBufferIn*> buffers; |
---|
185 | |
---|
186 | list<CEventServer::SSubEvent>::iterator it; |
---|
187 | string fieldId; |
---|
188 | |
---|
189 | for (it = event.subEvents.begin(); it != event.subEvents.end(); ++it) |
---|
190 | { |
---|
191 | int rank = it->rank; |
---|
192 | CBufferIn* buffer = it->buffer; |
---|
193 | *buffer >> fieldId; |
---|
194 | ranks.push_back(rank); |
---|
195 | buffers.push_back(buffer); |
---|
196 | } |
---|
197 | get(fieldId)->recvUpdateData(ranks,buffers); |
---|
198 | } |
---|
199 | |
---|
200 | void CField::recvUpdateData(vector<int>& ranks, vector<CBufferIn*>& buffers) |
---|
201 | { |
---|
202 | if (data_srv.empty()) |
---|
203 | { |
---|
204 | for (map<int, CArray<size_t, 1> >::iterator it = grid->outIndexFromClient.begin(); it != grid->outIndexFromClient.end(); ++it) |
---|
205 | { |
---|
206 | int rank = it->first; |
---|
207 | data_srv.insert(std::make_pair(rank, CArray<double,1>(it->second.numElements()))); |
---|
208 | foperation_srv.insert(pair<int,boost::shared_ptr<func::CFunctor> >(rank,boost::shared_ptr<func::CFunctor>(new func::CInstant(data_srv[rank])))); |
---|
209 | } |
---|
210 | } |
---|
211 | |
---|
212 | CContext* context = CContext::getCurrent(); |
---|
213 | const CDate& currDate = context->getCalendar()->getCurrentDate(); |
---|
214 | const CDate opeDate = last_operation_srv +freq_op + freq_operation_srv - freq_op; |
---|
215 | const CDate writeDate = last_Write_srv + freq_write_srv; |
---|
216 | |
---|
217 | if (opeDate <= currDate) |
---|
218 | { |
---|
219 | for (int n = 0; n < ranks.size(); n++) |
---|
220 | { |
---|
221 | CArray<double,1> data_tmp; |
---|
222 | *buffers[n] >> data_tmp; |
---|
223 | (*foperation_srv[ranks[n]])(data_tmp); |
---|
224 | } |
---|
225 | last_operation_srv = currDate; |
---|
226 | } |
---|
227 | |
---|
228 | if (writeDate < (currDate + freq_operation_srv)) |
---|
229 | { |
---|
230 | for (int n = 0; n < ranks.size(); n++) |
---|
231 | { |
---|
232 | this->foperation_srv[ranks[n]]->final(); |
---|
233 | } |
---|
234 | |
---|
235 | last_Write_srv = writeDate; |
---|
236 | writeField(); |
---|
237 | lastlast_Write_srv = last_Write_srv; |
---|
238 | } |
---|
239 | } |
---|
240 | |
---|
241 | void CField::writeField(void) |
---|
242 | { |
---|
243 | if (!getRelFile()->allDomainEmpty) |
---|
244 | { |
---|
245 | if (grid->doGridHaveDataToWrite() || getRelFile()->type == CFile::type_attr::one_file) |
---|
246 | { |
---|
247 | getRelFile()->checkFile(); |
---|
248 | this->incrementNStep(); |
---|
249 | getRelFile()->getDataOutput()->writeFieldData(CField::get(this)); |
---|
250 | } |
---|
251 | } |
---|
252 | } |
---|
253 | |
---|
254 | void CField::sendReadDataRequest(void) |
---|
255 | { |
---|
256 | CContext* context = CContext::getCurrent(); |
---|
257 | CContextClient* client = context->client; |
---|
258 | |
---|
259 | lastDataRequestedFromServer = context->getCalendar()->getCurrentDate(); |
---|
260 | isReadDataRequestPending = true; |
---|
261 | |
---|
262 | CEventClient event(getType(), EVENT_ID_READ_DATA); |
---|
263 | if (client->isServerLeader()) |
---|
264 | { |
---|
265 | CMessage msg; |
---|
266 | msg << getId(); |
---|
267 | const std::list<int>& ranks = client->getRanksServerLeader(); |
---|
268 | for (std::list<int>::const_iterator itRank = ranks.begin(), itRankEnd = ranks.end(); itRank != itRankEnd; ++itRank) |
---|
269 | event.push(*itRank, 1, msg); |
---|
270 | client->sendEvent(event); |
---|
271 | } |
---|
272 | else client->sendEvent(event); |
---|
273 | } |
---|
274 | |
---|
275 | /*! |
---|
276 | Send request new data read from file if need be, that is the current data is out-of-date. |
---|
277 | \return true if and only if some data was requested |
---|
278 | */ |
---|
279 | bool CField::sendReadDataRequestIfNeeded(void) |
---|
280 | { |
---|
281 | const CDate& currentDate = CContext::getCurrent()->getCalendar()->getCurrentDate(); |
---|
282 | |
---|
283 | bool requestData = (currentDate >= lastDataRequestedFromServer + file->output_freq.getValue()); |
---|
284 | |
---|
285 | if (requestData) |
---|
286 | { |
---|
287 | cout<<"currentDate : "<<currentDate<<endl ; |
---|
288 | cout<<"lastDataRequestedFromServer : "<<lastDataRequestedFromServer<<endl ; |
---|
289 | cout<<"file->output_freq.getValue() : "<<file->output_freq.getValue()<<endl ; |
---|
290 | cout<<"lastDataRequestedFromServer + file->output_freq.getValue() : "<<lastDataRequestedFromServer + file->output_freq.getValue()<<endl ; |
---|
291 | |
---|
292 | sendReadDataRequest(); |
---|
293 | } |
---|
294 | |
---|
295 | return requestData; |
---|
296 | } |
---|
297 | |
---|
298 | void CField::recvReadDataRequest(CEventServer& event) |
---|
299 | { |
---|
300 | CBufferIn* buffer = event.subEvents.begin()->buffer; |
---|
301 | StdString fieldId; |
---|
302 | *buffer >> fieldId; |
---|
303 | get(fieldId)->recvReadDataRequest(); |
---|
304 | } |
---|
305 | |
---|
306 | void CField::recvReadDataRequest(void) |
---|
307 | { |
---|
308 | CContext* context = CContext::getCurrent(); |
---|
309 | CContextClient* client = context->client; |
---|
310 | |
---|
311 | CEventClient event(getType(), EVENT_ID_READ_DATA_READY); |
---|
312 | std::list<CMessage> msgs; |
---|
313 | |
---|
314 | bool hasData = readField(); |
---|
315 | |
---|
316 | map<int, CArray<double,1> >::iterator it; |
---|
317 | for (it = data_srv.begin(); it != data_srv.end(); it++) |
---|
318 | { |
---|
319 | msgs.push_back(CMessage()); |
---|
320 | CMessage& msg = msgs.back(); |
---|
321 | msg << getId(); |
---|
322 | if (hasData) |
---|
323 | msg << getNStep() - 1 << it->second; |
---|
324 | else |
---|
325 | msg << size_t(-1); |
---|
326 | event.push(it->first, grid->nbSenders[it->first], msg); |
---|
327 | } |
---|
328 | client->sendEvent(event); |
---|
329 | } |
---|
330 | |
---|
331 | bool CField::readField(void) |
---|
332 | { |
---|
333 | if (!getRelFile()->allDomainEmpty) |
---|
334 | { |
---|
335 | if (grid->doGridHaveDataToWrite() || getRelFile()->type == CFile::type_attr::one_file) |
---|
336 | { |
---|
337 | if (data_srv.empty()) |
---|
338 | { |
---|
339 | for (map<int, CArray<size_t, 1> >::iterator it = grid->outIndexFromClient.begin(); it != grid->outIndexFromClient.end(); ++it) |
---|
340 | data_srv.insert(std::make_pair(it->first, CArray<double,1>(it->second.numElements()))); |
---|
341 | } |
---|
342 | |
---|
343 | getRelFile()->checkFile(); |
---|
344 | if (!nstepMax) |
---|
345 | { |
---|
346 | nstepMax = getRelFile()->getDataInput()->getFieldNbRecords(CField::get(this)); |
---|
347 | } |
---|
348 | |
---|
349 | this->incrementNStep(); |
---|
350 | |
---|
351 | |
---|
352 | |
---|
353 | if (getNStep() > nstepMax && (getRelFile()->cyclic.isEmpty() || !getRelFile()->cyclic) ) |
---|
354 | return false; |
---|
355 | |
---|
356 | getRelFile()->getDataInput()->readFieldData(CField::get(this)); |
---|
357 | } |
---|
358 | } |
---|
359 | |
---|
360 | return true; |
---|
361 | } |
---|
362 | |
---|
363 | void CField::recvReadDataReady(CEventServer& event) |
---|
364 | { |
---|
365 | string fieldId; |
---|
366 | vector<int> ranks; |
---|
367 | vector<CBufferIn*> buffers; |
---|
368 | |
---|
369 | list<CEventServer::SSubEvent>::iterator it; |
---|
370 | for (it = event.subEvents.begin(); it != event.subEvents.end(); ++it) |
---|
371 | { |
---|
372 | ranks.push_back(it->rank); |
---|
373 | CBufferIn* buffer = it->buffer; |
---|
374 | *buffer >> fieldId; |
---|
375 | buffers.push_back(buffer); |
---|
376 | } |
---|
377 | get(fieldId)->recvReadDataReady(ranks, buffers); |
---|
378 | } |
---|
379 | |
---|
380 | void CField::recvReadDataReady(vector<int> ranks, vector<CBufferIn*> buffers) |
---|
381 | { |
---|
382 | CContext* context = CContext::getCurrent(); |
---|
383 | StdSize record; |
---|
384 | std::map<int, CArray<double,1> > data; |
---|
385 | |
---|
386 | bool isEOF = false; |
---|
387 | |
---|
388 | for (int i = 0; i < ranks.size(); i++) |
---|
389 | { |
---|
390 | int rank = ranks[i]; |
---|
391 | *buffers[i] >> record; |
---|
392 | isEOF = (record == size_t(-1)); |
---|
393 | |
---|
394 | if (!isEOF) |
---|
395 | *buffers[i] >> data[rank]; |
---|
396 | else |
---|
397 | break; |
---|
398 | } |
---|
399 | |
---|
400 | if (isEOF) |
---|
401 | serverSourceFilter->signalEndOfStream(lastDataRequestedFromServer); |
---|
402 | else |
---|
403 | serverSourceFilter->streamDataFromServer(lastDataRequestedFromServer, data); |
---|
404 | |
---|
405 | isReadDataRequestPending = false; |
---|
406 | } |
---|
407 | |
---|
408 | //---------------------------------------------------------------- |
---|
409 | |
---|
410 | void CField::setRelFile(CFile* _file) |
---|
411 | { |
---|
412 | this->file = _file; |
---|
413 | hasOutputFile = true; |
---|
414 | } |
---|
415 | |
---|
416 | //---------------------------------------------------------------- |
---|
417 | |
---|
418 | StdString CField::GetName(void) { return StdString("field"); } |
---|
419 | StdString CField::GetDefName(void) { return CField::GetName(); } |
---|
420 | ENodeType CField::GetType(void) { return eField; } |
---|
421 | |
---|
422 | //---------------------------------------------------------------- |
---|
423 | |
---|
424 | CGrid* CField::getRelGrid(void) const |
---|
425 | { |
---|
426 | return this->grid; |
---|
427 | } |
---|
428 | |
---|
429 | //---------------------------------------------------------------- |
---|
430 | |
---|
431 | CFile* CField::getRelFile(void) const |
---|
432 | { |
---|
433 | return this->file; |
---|
434 | } |
---|
435 | |
---|
436 | StdSize CField::getNStep(void) const |
---|
437 | { |
---|
438 | return this->nstep; |
---|
439 | } |
---|
440 | |
---|
441 | func::CFunctor::ETimeType CField::getOperationTimeType() const |
---|
442 | { |
---|
443 | return operationTimeType; |
---|
444 | } |
---|
445 | |
---|
446 | //---------------------------------------------------------------- |
---|
447 | |
---|
448 | void CField::incrementNStep(void) |
---|
449 | { |
---|
450 | this->nstep++; |
---|
451 | } |
---|
452 | |
---|
453 | void CField::resetNStep(StdSize nstep /*= 0*/) |
---|
454 | { |
---|
455 | this->nstep = nstep; |
---|
456 | } |
---|
457 | |
---|
458 | void CField::resetNStepMax(void) |
---|
459 | { |
---|
460 | this->nstepMax = 0; |
---|
461 | } |
---|
462 | |
---|
463 | //---------------------------------------------------------------- |
---|
464 | |
---|
465 | bool CField::isActive(void) const |
---|
466 | { |
---|
467 | return (instantDataFilter != NULL); |
---|
468 | } |
---|
469 | |
---|
470 | //---------------------------------------------------------------- |
---|
471 | |
---|
472 | bool CField::wasWritten() const |
---|
473 | { |
---|
474 | return written; |
---|
475 | } |
---|
476 | |
---|
477 | void CField::setWritten() |
---|
478 | { |
---|
479 | written = true; |
---|
480 | } |
---|
481 | |
---|
482 | //---------------------------------------------------------------- |
---|
483 | |
---|
484 | bool CField::getUseCompressedOutput() const |
---|
485 | { |
---|
486 | return useCompressedOutput; |
---|
487 | } |
---|
488 | |
---|
489 | void CField::setUseCompressedOutput() |
---|
490 | { |
---|
491 | useCompressedOutput = true; |
---|
492 | } |
---|
493 | |
---|
494 | //---------------------------------------------------------------- |
---|
495 | |
---|
496 | boost::shared_ptr<COutputPin> CField::getInstantDataFilter() |
---|
497 | { |
---|
498 | return instantDataFilter; |
---|
499 | } |
---|
500 | |
---|
501 | //---------------------------------------------------------------- |
---|
502 | |
---|
503 | void CField::solveOnlyReferenceEnabledField(bool doSending2Server) |
---|
504 | { |
---|
505 | CContext* context = CContext::getCurrent(); |
---|
506 | if (!isReferenceSolved) |
---|
507 | { |
---|
508 | isReferenceSolved = true; |
---|
509 | |
---|
510 | if (context->hasClient) |
---|
511 | { |
---|
512 | solveRefInheritance(true); |
---|
513 | if (hasDirectFieldReference()) getDirectFieldReference()->solveOnlyReferenceEnabledField(false); |
---|
514 | } |
---|
515 | else if (context->hasServer) |
---|
516 | solveServerOperation(); |
---|
517 | |
---|
518 | solveGridReference(); |
---|
519 | |
---|
520 | if (context->hasClient) |
---|
521 | { |
---|
522 | solveGenerateGrid(); |
---|
523 | buildGridTransformationGraph(); |
---|
524 | } |
---|
525 | } |
---|
526 | } |
---|
527 | |
---|
528 | /*! |
---|
529 | Build up graph of grids which plays role of destination and source in grid transformation |
---|
530 | This function should be called before \func solveGridReference() |
---|
531 | */ |
---|
532 | void CField::buildGridTransformationGraph() |
---|
533 | { |
---|
534 | CContext* context = CContext::getCurrent(); |
---|
535 | if (context->hasClient) |
---|
536 | { |
---|
537 | if (grid && !grid->isTransformed() && hasDirectFieldReference() && grid != getDirectFieldReference()->grid) |
---|
538 | { |
---|
539 | grid->addTransGridSource(getDirectFieldReference()->grid); |
---|
540 | } |
---|
541 | } |
---|
542 | } |
---|
543 | |
---|
544 | /*! |
---|
545 | Generate a new grid destination if there are more than one grid source pointing to a same grid destination |
---|
546 | */ |
---|
547 | void CField::generateNewTransformationGridDest() |
---|
548 | { |
---|
549 | CContext* context = CContext::getCurrent(); |
---|
550 | if (context->hasClient) |
---|
551 | { |
---|
552 | std::map<CGrid*,std::pair<bool,StdString> >& gridSrcMap = grid->getTransGridSource(); |
---|
553 | if (1 < gridSrcMap.size()) |
---|
554 | { |
---|
555 | // Search for grid source |
---|
556 | CGrid* gridSrc = grid; |
---|
557 | CField* currField = this; |
---|
558 | std::vector<CField*> hieraField; |
---|
559 | |
---|
560 | while (currField->hasDirectFieldReference() && (gridSrc == grid)) |
---|
561 | { |
---|
562 | hieraField.push_back(currField); |
---|
563 | CField* tmp = currField->getDirectFieldReference(); |
---|
564 | currField = tmp; |
---|
565 | gridSrc = currField->grid; |
---|
566 | } |
---|
567 | |
---|
568 | if (gridSrcMap.end() != gridSrcMap.find(gridSrc)) |
---|
569 | { |
---|
570 | CGrid* gridTmp; |
---|
571 | std::pair<bool,StdString> newGridDest = gridSrcMap[gridSrc]; |
---|
572 | if (newGridDest.first) |
---|
573 | { |
---|
574 | StdString newIdGridDest = newGridDest.second; |
---|
575 | if (!CGrid::has(newIdGridDest)) |
---|
576 | { |
---|
577 | ERROR("CGrid* CGrid::generateNewTransformationGridDest()", |
---|
578 | << " Something wrong happened! Grid whose id " << newIdGridDest |
---|
579 | << "should exist "); |
---|
580 | } |
---|
581 | gridTmp = CGrid::get(newIdGridDest); |
---|
582 | } |
---|
583 | else |
---|
584 | { |
---|
585 | StdString newIdGridDest = CGrid::generateId(gridSrc, grid); |
---|
586 | gridTmp = CGrid::cloneGrid(newIdGridDest, grid); |
---|
587 | |
---|
588 | (gridSrcMap[gridSrc]).first = true; |
---|
589 | (gridSrcMap[gridSrc]).second = newIdGridDest; |
---|
590 | } |
---|
591 | |
---|
592 | // Update all descendants |
---|
593 | for (std::vector<CField*>::iterator it = hieraField.begin(); it != hieraField.end(); ++it) |
---|
594 | { |
---|
595 | (*it)->grid = gridTmp; |
---|
596 | (*it)->updateRef((*it)->grid); |
---|
597 | } |
---|
598 | } |
---|
599 | } |
---|
600 | } |
---|
601 | } |
---|
602 | |
---|
603 | void CField::updateRef(CGrid* grid) |
---|
604 | { |
---|
605 | if (!grid_ref.isEmpty()) grid_ref.setValue(grid->getId()); |
---|
606 | else |
---|
607 | { |
---|
608 | std::vector<CAxis*> axisTmp = grid->getAxis(); |
---|
609 | std::vector<CDomain*> domainTmp = grid->getDomains(); |
---|
610 | if ((1<axisTmp.size()) || (1<domainTmp.size())) |
---|
611 | ERROR("void CField::updateRef(CGrid* grid)", |
---|
612 | << "More than one domain or axis is available for domain_ref/axis_ref of field " << this->getId()); |
---|
613 | |
---|
614 | if ((!domain_ref.isEmpty()) && (domainTmp.empty())) |
---|
615 | ERROR("void CField::updateRef(CGrid* grid)", |
---|
616 | << "Incoherent between available domain and domain_ref of field " << this->getId()); |
---|
617 | if ((!axis_ref.isEmpty()) && (axisTmp.empty())) |
---|
618 | ERROR("void CField::updateRef(CGrid* grid)", |
---|
619 | << "Incoherent between available axis and axis_ref of field " << this->getId()); |
---|
620 | |
---|
621 | if (!domain_ref.isEmpty()) domain_ref.setValue(domainTmp[0]->getId()); |
---|
622 | if (!axis_ref.isEmpty()) axis_ref.setValue(axisTmp[0]->getId()); |
---|
623 | } |
---|
624 | } |
---|
625 | |
---|
626 | void CField::solveAllReferenceEnabledField(bool doSending2Server) |
---|
627 | { |
---|
628 | CContext* context = CContext::getCurrent(); |
---|
629 | solveOnlyReferenceEnabledField(doSending2Server); |
---|
630 | |
---|
631 | if (!areAllReferenceSolved) |
---|
632 | { |
---|
633 | areAllReferenceSolved = true; |
---|
634 | |
---|
635 | if (context->hasClient) |
---|
636 | { |
---|
637 | solveRefInheritance(true); |
---|
638 | if (hasDirectFieldReference()) getDirectFieldReference()->solveAllReferenceEnabledField(false); |
---|
639 | } |
---|
640 | else if (context->hasServer) |
---|
641 | solveServerOperation(); |
---|
642 | |
---|
643 | solveGridReference(); |
---|
644 | } |
---|
645 | |
---|
646 | solveGridDomainAxisRef(doSending2Server); |
---|
647 | |
---|
648 | if (context->hasClient) |
---|
649 | { |
---|
650 | solveTransformedGrid(); |
---|
651 | } |
---|
652 | |
---|
653 | solveCheckMaskIndex(doSending2Server); |
---|
654 | } |
---|
655 | |
---|
656 | std::map<int, StdSize> CField::getGridAttributesBufferSize() |
---|
657 | { |
---|
658 | return grid->getAttributesBufferSize(); |
---|
659 | } |
---|
660 | |
---|
661 | std::map<int, StdSize> CField::getGridDataBufferSize() |
---|
662 | { |
---|
663 | return grid->getDataBufferSize(getId()); |
---|
664 | } |
---|
665 | |
---|
666 | //---------------------------------------------------------------- |
---|
667 | |
---|
668 | void CField::solveServerOperation(void) |
---|
669 | { |
---|
670 | CContext* context = CContext::getCurrent(); |
---|
671 | |
---|
672 | if (!context->hasServer || !hasOutputFile) return; |
---|
673 | |
---|
674 | if (freq_op.isEmpty()) |
---|
675 | freq_op.setValue(TimeStep); |
---|
676 | |
---|
677 | if (freq_offset.isEmpty()) |
---|
678 | freq_offset.setValue(NoneDu); |
---|
679 | |
---|
680 | freq_operation_srv = file->output_freq.getValue(); |
---|
681 | freq_write_srv = file->output_freq.getValue(); |
---|
682 | |
---|
683 | lastlast_Write_srv = context->getCalendar()->getInitDate(); |
---|
684 | last_Write_srv = context->getCalendar()->getInitDate(); |
---|
685 | last_operation_srv = context->getCalendar()->getInitDate(); |
---|
686 | |
---|
687 | const CDuration toffset = freq_operation_srv - freq_offset.getValue() - context->getCalendar()->getTimeStep(); |
---|
688 | last_operation_srv = last_operation_srv - toffset; |
---|
689 | |
---|
690 | if (operation.isEmpty()) |
---|
691 | ERROR("void CField::solveServerOperation(void)", |
---|
692 | << "An operation must be defined for field \"" << getId() << "\"."); |
---|
693 | |
---|
694 | boost::shared_ptr<func::CFunctor> functor; |
---|
695 | CArray<double, 1> dummyData; |
---|
696 | |
---|
697 | #define DECLARE_FUNCTOR(MType, mtype) \ |
---|
698 | if (operation.getValue().compare(#mtype) == 0) \ |
---|
699 | { \ |
---|
700 | functor.reset(new func::C##MType(dummyData)); \ |
---|
701 | } |
---|
702 | |
---|
703 | #include "functor_type.conf" |
---|
704 | |
---|
705 | if (!functor) |
---|
706 | ERROR("void CField::solveServerOperation(void)", |
---|
707 | << "\"" << operation << "\" is not a valid operation."); |
---|
708 | |
---|
709 | operationTimeType = functor->timeType(); |
---|
710 | } |
---|
711 | |
---|
712 | //---------------------------------------------------------------- |
---|
713 | |
---|
714 | /*! |
---|
715 | * Constructs the graph filter for the field, enabling or not the data output. |
---|
716 | * This method should not be called more than once with enableOutput equal to true. |
---|
717 | * |
---|
718 | * \param gc the garbage collector to use when building the filter graph |
---|
719 | * \param enableOutput must be true when the field data is to be |
---|
720 | * read by the client or/and written to a file |
---|
721 | */ |
---|
722 | void CField::buildFilterGraph(CGarbageCollector& gc, bool enableOutput) |
---|
723 | { |
---|
724 | if (!areAllReferenceSolved) solveAllReferenceEnabledField(false); |
---|
725 | |
---|
726 | // Start by building a filter which can provide the field's instant data |
---|
727 | if (!instantDataFilter) |
---|
728 | { |
---|
729 | // Check if we have an expression to parse |
---|
730 | if (!content.empty()) |
---|
731 | { |
---|
732 | boost::scoped_ptr<IFilterExprNode> expr(parseExpr(content + '\0')); |
---|
733 | instantDataFilter = expr->reduce(gc, *this); |
---|
734 | } |
---|
735 | // Check if we have a reference on another field |
---|
736 | else if (!field_ref.isEmpty()) |
---|
737 | instantDataFilter = getFieldReference(gc); |
---|
738 | // Check if the data is to be read from a file |
---|
739 | else if (file && !file->mode.isEmpty() && file->mode == CFile::mode_attr::read) |
---|
740 | instantDataFilter = serverSourceFilter = boost::shared_ptr<CSourceFilter>(new CSourceFilter(grid, |
---|
741 | freq_offset.isEmpty() ? NoneDu : freq_offset)); |
---|
742 | else // The data might be passed from the model |
---|
743 | instantDataFilter = clientSourceFilter = boost::shared_ptr<CSourceFilter>(new CSourceFilter(grid)); |
---|
744 | } |
---|
745 | |
---|
746 | // If the field data is to be read by the client or/and written to a file |
---|
747 | if (enableOutput && !storeFilter && !fileWriterFilter) |
---|
748 | { |
---|
749 | if (!read_access.isEmpty() && read_access) |
---|
750 | { |
---|
751 | storeFilter = boost::shared_ptr<CStoreFilter>(new CStoreFilter(gc, CContext::getCurrent(), grid)); |
---|
752 | instantDataFilter->connectOutput(storeFilter, 0); |
---|
753 | } |
---|
754 | |
---|
755 | if (file && (file->mode.isEmpty() || file->mode == CFile::mode_attr::write)) |
---|
756 | { |
---|
757 | fileWriterFilter = boost::shared_ptr<CFileWriterFilter>(new CFileWriterFilter(gc, this)); |
---|
758 | getTemporalDataFilter(gc, file->output_freq)->connectOutput(fileWriterFilter, 0); |
---|
759 | } |
---|
760 | } |
---|
761 | } |
---|
762 | |
---|
763 | /*! |
---|
764 | * Returns the filter needed to handle the field reference. |
---|
765 | * This method should only be called when building the filter graph corresponding to the field. |
---|
766 | * |
---|
767 | * \param gc the garbage collector to use |
---|
768 | * \return the output pin corresponding to the field reference |
---|
769 | */ |
---|
770 | boost::shared_ptr<COutputPin> CField::getFieldReference(CGarbageCollector& gc) |
---|
771 | { |
---|
772 | if (instantDataFilter || field_ref.isEmpty()) |
---|
773 | ERROR("COutputPin* CField::getFieldReference(CGarbageCollector& gc)", |
---|
774 | "Impossible to get the field reference for a field which has already been parsed or which does not have a field_ref."); |
---|
775 | |
---|
776 | CField* fieldRef = CField::get(field_ref); |
---|
777 | fieldRef->buildFilterGraph(gc, false); |
---|
778 | |
---|
779 | std::pair<boost::shared_ptr<CFilter>, boost::shared_ptr<CFilter> > filters; |
---|
780 | // Check if a spatial transformation is needed |
---|
781 | if (grid && grid != fieldRef->grid && grid->hasTransform()) |
---|
782 | { |
---|
783 | double defaultValue = 0.0; |
---|
784 | if (!default_value.isEmpty()) defaultValue = this->default_value; |
---|
785 | filters = CSpatialTransformFilter::buildFilterGraph(gc, fieldRef->grid, grid, defaultValue); |
---|
786 | } |
---|
787 | |
---|
788 | else |
---|
789 | filters.first = filters.second = boost::shared_ptr<CFilter>(new CPassThroughFilter(gc)); |
---|
790 | |
---|
791 | fieldRef->getInstantDataFilter()->connectOutput(filters.first, 0); |
---|
792 | |
---|
793 | return filters.second; |
---|
794 | } |
---|
795 | |
---|
796 | /*! |
---|
797 | * Returns the filter needed to handle a self reference in the field's expression. |
---|
798 | * If the needed filter does not exist, it is created, otherwise it is reused. |
---|
799 | * This method should only be called when building the filter graph corresponding |
---|
800 | * to the field's expression. |
---|
801 | * |
---|
802 | * \param gc the garbage collector to use |
---|
803 | * \return the output pin corresponding to a self reference |
---|
804 | */ |
---|
805 | boost::shared_ptr<COutputPin> CField::getSelfReference(CGarbageCollector& gc) |
---|
806 | { |
---|
807 | if (instantDataFilter || content.empty()) |
---|
808 | ERROR("COutputPin* CField::getSelfReference(CGarbageCollector& gc)", |
---|
809 | "Impossible to add a self reference to a field which has already been parsed or which does not have an expression."); |
---|
810 | |
---|
811 | if (!selfReferenceFilter) |
---|
812 | { |
---|
813 | if (file && !file->mode.isEmpty() && file->mode == CFile::mode_attr::read) |
---|
814 | { |
---|
815 | if (!serverSourceFilter) |
---|
816 | serverSourceFilter = boost::shared_ptr<CSourceFilter>(new CSourceFilter(grid, |
---|
817 | freq_offset.isEmpty() ? NoneDu : freq_offset)); |
---|
818 | |
---|
819 | selfReferenceFilter = serverSourceFilter; |
---|
820 | } |
---|
821 | else if (!field_ref.isEmpty()) |
---|
822 | selfReferenceFilter = getFieldReference(gc); |
---|
823 | else |
---|
824 | { |
---|
825 | if (!clientSourceFilter) |
---|
826 | clientSourceFilter = boost::shared_ptr<CSourceFilter>(new CSourceFilter(grid)); |
---|
827 | |
---|
828 | selfReferenceFilter = clientSourceFilter; |
---|
829 | } |
---|
830 | } |
---|
831 | |
---|
832 | return selfReferenceFilter; |
---|
833 | } |
---|
834 | |
---|
835 | /*! |
---|
836 | * Returns the temporal filter corresponding to the field's temporal operation |
---|
837 | * for the specified operation frequency. The filter is created if it does not |
---|
838 | * exist, otherwise it is reused. |
---|
839 | * |
---|
840 | * \param gc the garbage collector to use |
---|
841 | * \param outFreq the operation frequency, i.e. the frequency at which the output data will be computed |
---|
842 | * \return the output pin corresponding to the requested temporal filter |
---|
843 | */ |
---|
844 | boost::shared_ptr<COutputPin> CField::getTemporalDataFilter(CGarbageCollector& gc, CDuration outFreq) |
---|
845 | { |
---|
846 | std::map<CDuration, boost::shared_ptr<COutputPin> >::iterator it = temporalDataFilters.find(outFreq); |
---|
847 | |
---|
848 | if (it == temporalDataFilters.end()) |
---|
849 | { |
---|
850 | if (operation.isEmpty()) |
---|
851 | ERROR("void CField::getTemporalDataFilter(CGarbageCollector& gc, CDuration outFreq)", |
---|
852 | << "An operation must be defined for field \"" << getId() << "\"."); |
---|
853 | |
---|
854 | if (freq_op.isEmpty()) |
---|
855 | freq_op.setValue(TimeStep); |
---|
856 | if (freq_offset.isEmpty()) |
---|
857 | freq_offset.setValue(NoneDu); |
---|
858 | |
---|
859 | const bool ignoreMissingValue = (!detect_missing_value.isEmpty() && !default_value.isEmpty() && detect_missing_value == true); |
---|
860 | |
---|
861 | boost::shared_ptr<CTemporalFilter> temporalFilter(new CTemporalFilter(gc, operation, |
---|
862 | CContext::getCurrent()->getCalendar()->getInitDate(), |
---|
863 | freq_op, freq_offset, outFreq, |
---|
864 | ignoreMissingValue, ignoreMissingValue ? default_value : 0.0)); |
---|
865 | instantDataFilter->connectOutput(temporalFilter, 0); |
---|
866 | |
---|
867 | it = temporalDataFilters.insert(std::make_pair(outFreq, temporalFilter)).first; |
---|
868 | } |
---|
869 | |
---|
870 | return it->second; |
---|
871 | } |
---|
872 | |
---|
873 | //---------------------------------------------------------------- |
---|
874 | /* |
---|
875 | void CField::fromBinary(StdIStream& is) |
---|
876 | { |
---|
877 | SuperClass::fromBinary(is); |
---|
878 | #define CLEAR_ATT(name_)\ |
---|
879 | SuperClassAttribute::operator[](#name_)->reset() |
---|
880 | |
---|
881 | CLEAR_ATT(domain_ref); |
---|
882 | CLEAR_ATT(axis_ref); |
---|
883 | #undef CLEAR_ATT |
---|
884 | |
---|
885 | } |
---|
886 | */ |
---|
887 | //---------------------------------------------------------------- |
---|
888 | |
---|
889 | void CField::solveGridReference(void) |
---|
890 | { |
---|
891 | if (grid_ref.isEmpty() && domain_ref.isEmpty() && axis_ref.isEmpty() && scalar_ref.isEmpty()) |
---|
892 | { |
---|
893 | ERROR("CField::solveGridReference(void)", |
---|
894 | << "A grid must be defined for field '" << getFieldOutputName() << "' ."); |
---|
895 | } |
---|
896 | else if (!grid_ref.isEmpty() && (!domain_ref.isEmpty() || !axis_ref.isEmpty() || !scalar_ref.isEmpty())) |
---|
897 | { |
---|
898 | ERROR("CField::solveGridReference(void)", |
---|
899 | << "Field '" << getFieldOutputName() << "' has both a grid and a domain/axis/scalar." << std::endl |
---|
900 | << "Please define either 'grid_ref' or 'domain_ref'/'axis_ref'/'scalar_ref'."); |
---|
901 | } |
---|
902 | |
---|
903 | if (grid_ref.isEmpty()) |
---|
904 | { |
---|
905 | std::vector<CDomain*> vecDom; |
---|
906 | std::vector<CAxis*> vecAxis; |
---|
907 | std::vector<CScalar*> vecScalar; |
---|
908 | |
---|
909 | if (!domain_ref.isEmpty()) |
---|
910 | { |
---|
911 | StdString tmp = domain_ref.getValue(); |
---|
912 | if (CDomain::has(domain_ref)) |
---|
913 | vecDom.push_back(CDomain::get(domain_ref)); |
---|
914 | else |
---|
915 | ERROR("CField::solveGridReference(void)", |
---|
916 | << "Invalid reference to domain '" << domain_ref.getValue() << "'."); |
---|
917 | } |
---|
918 | |
---|
919 | if (!axis_ref.isEmpty()) |
---|
920 | { |
---|
921 | if (CAxis::has(axis_ref)) |
---|
922 | vecAxis.push_back(CAxis::get(axis_ref)); |
---|
923 | else |
---|
924 | ERROR("CField::solveGridReference(void)", |
---|
925 | << "Invalid reference to axis '" << axis_ref.getValue() << "'."); |
---|
926 | } |
---|
927 | |
---|
928 | if (!scalar_ref.isEmpty()) |
---|
929 | { |
---|
930 | if (CScalar::has(scalar_ref)) |
---|
931 | vecScalar.push_back(CScalar::get(scalar_ref)); |
---|
932 | else |
---|
933 | ERROR("CField::solveGridReference(void)", |
---|
934 | << "Invalid reference to scalar '" << scalar_ref.getValue() << "'."); |
---|
935 | } |
---|
936 | |
---|
937 | // Warning: the gridId shouldn't be set as the grid_ref since it could be inherited |
---|
938 | StdString gridId = CGrid::generateId(vecDom, vecAxis, vecScalar); |
---|
939 | if (CGrid::has(gridId)) |
---|
940 | this->grid = CGrid::get(gridId); |
---|
941 | else |
---|
942 | this->grid = CGrid::createGrid(gridId, vecDom, vecAxis, vecScalar); |
---|
943 | } |
---|
944 | else |
---|
945 | { |
---|
946 | if (CGrid::has(grid_ref)) |
---|
947 | this->grid = CGrid::get(grid_ref); |
---|
948 | else |
---|
949 | ERROR("CField::solveGridReference(void)", |
---|
950 | << "Invalid reference to grid '" << grid_ref.getValue() << "'."); |
---|
951 | } |
---|
952 | } |
---|
953 | |
---|
954 | void CField::solveGridDomainAxisRef(bool checkAtt) |
---|
955 | { |
---|
956 | grid->solveDomainAxisRef(checkAtt); |
---|
957 | } |
---|
958 | |
---|
959 | void CField::solveCheckMaskIndex(bool doSendingIndex) |
---|
960 | { |
---|
961 | grid->checkMaskIndex(doSendingIndex); |
---|
962 | } |
---|
963 | |
---|
964 | void CField::solveTransformedGrid() |
---|
965 | { |
---|
966 | if (grid && !grid->isTransformed() && hasDirectFieldReference() && grid != getDirectFieldReference()->grid) |
---|
967 | { |
---|
968 | std::vector<CGrid*> grids; |
---|
969 | // Source grid |
---|
970 | grids.push_back(getDirectFieldReference()->grid); |
---|
971 | // Intermediate grids |
---|
972 | if (!grid_path.isEmpty()) |
---|
973 | { |
---|
974 | std::string gridId; |
---|
975 | size_t start = 0, end; |
---|
976 | |
---|
977 | do |
---|
978 | { |
---|
979 | end = grid_path.getValue().find(',', start); |
---|
980 | if (end != std::string::npos) |
---|
981 | { |
---|
982 | gridId = grid_path.getValue().substr(start, end - start); |
---|
983 | start = end + 1; |
---|
984 | } |
---|
985 | else |
---|
986 | gridId = grid_path.getValue().substr(start); |
---|
987 | |
---|
988 | if (!CGrid::has(gridId)) |
---|
989 | ERROR("void CField::solveTransformedGrid()", |
---|
990 | << "Invalid grid_path, the grid '" << gridId << "' does not exist."); |
---|
991 | |
---|
992 | grids.push_back(CGrid::get(gridId)); |
---|
993 | } |
---|
994 | while (end != std::string::npos); |
---|
995 | } |
---|
996 | // Destination grid |
---|
997 | grids.push_back(grid); |
---|
998 | |
---|
999 | for (size_t i = 0, count = grids.size() - 1; i < count; ++i) |
---|
1000 | { |
---|
1001 | CGrid *gridSrc = grids[i]; |
---|
1002 | CGrid *gridDest = grids[i + 1]; |
---|
1003 | if (!gridDest->isTransformed()) |
---|
1004 | gridDest->transformGrid(gridSrc); |
---|
1005 | } |
---|
1006 | } |
---|
1007 | } |
---|
1008 | |
---|
1009 | void CField::solveGenerateGrid() |
---|
1010 | { |
---|
1011 | if (grid && !grid->isTransformed() && hasDirectFieldReference() && grid != getDirectFieldReference()->grid) |
---|
1012 | grid->completeGrid(getDirectFieldReference()->grid); |
---|
1013 | else |
---|
1014 | grid->completeGrid(); |
---|
1015 | } |
---|
1016 | |
---|
1017 | void CField::solveGridDomainAxisBaseRef() |
---|
1018 | { |
---|
1019 | grid->solveDomainAxisRef(false); |
---|
1020 | grid->solveDomainAxisBaseRef(); |
---|
1021 | } |
---|
1022 | |
---|
1023 | ///------------------------------------------------------------------- |
---|
1024 | |
---|
1025 | template <> |
---|
1026 | void CGroupTemplate<CField, CFieldGroup, CFieldAttributes>::solveRefInheritance(void) |
---|
1027 | { |
---|
1028 | if (this->group_ref.isEmpty()) return; |
---|
1029 | StdString gref = this->group_ref.getValue(); |
---|
1030 | |
---|
1031 | if (!CFieldGroup::has(gref)) |
---|
1032 | ERROR("CGroupTemplate<CField, CFieldGroup, CFieldAttributes>::solveRefInheritance(void)", |
---|
1033 | << "[ gref = " << gref << "]" |
---|
1034 | << " invalid group name !"); |
---|
1035 | |
---|
1036 | CFieldGroup* group = CFieldGroup::get(gref); |
---|
1037 | CFieldGroup* owner = CFieldGroup::get(boost::polymorphic_downcast<CFieldGroup*>(this)); |
---|
1038 | |
---|
1039 | std::vector<CField*> allChildren = group->getAllChildren(); |
---|
1040 | std::vector<CField*>::iterator it = allChildren.begin(), end = allChildren.end(); |
---|
1041 | |
---|
1042 | for (; it != end; it++) |
---|
1043 | { |
---|
1044 | CField* child = *it; |
---|
1045 | if (child->hasId()) owner->createChild()->field_ref.setValue(child->getId()); |
---|
1046 | |
---|
1047 | } |
---|
1048 | } |
---|
1049 | |
---|
1050 | void CField::scaleFactorAddOffset(double scaleFactor, double addOffset) |
---|
1051 | { |
---|
1052 | map<int, CArray<double,1> >::iterator it; |
---|
1053 | for (it = data_srv.begin(); it != data_srv.end(); it++) it->second = (it->second - addOffset) / scaleFactor; |
---|
1054 | } |
---|
1055 | |
---|
1056 | void CField::invertScaleFactorAddOffset(double scaleFactor, double addOffset) |
---|
1057 | { |
---|
1058 | map<int, CArray<double,1> >::iterator it; |
---|
1059 | for (it = data_srv.begin(); it != data_srv.end(); it++) it->second = it->second * scaleFactor + addOffset; |
---|
1060 | } |
---|
1061 | |
---|
1062 | void CField::outputField(CArray<double,3>& fieldOut) |
---|
1063 | { |
---|
1064 | map<int, CArray<double,1> >::iterator it; |
---|
1065 | for (it = data_srv.begin(); it != data_srv.end(); it++) |
---|
1066 | { |
---|
1067 | grid->outputField(it->first, it->second, fieldOut.dataFirst()); |
---|
1068 | } |
---|
1069 | } |
---|
1070 | |
---|
1071 | void CField::outputField(CArray<double,2>& fieldOut) |
---|
1072 | { |
---|
1073 | map<int, CArray<double,1> >::iterator it; |
---|
1074 | for(it=data_srv.begin();it!=data_srv.end();it++) |
---|
1075 | { |
---|
1076 | grid->outputField(it->first, it->second, fieldOut.dataFirst()); |
---|
1077 | } |
---|
1078 | } |
---|
1079 | |
---|
1080 | void CField::outputField(CArray<double,1>& fieldOut) |
---|
1081 | { |
---|
1082 | map<int, CArray<double,1> >::iterator it; |
---|
1083 | |
---|
1084 | for (it = data_srv.begin(); it != data_srv.end(); it++) |
---|
1085 | { |
---|
1086 | grid->outputField(it->first, it->second, fieldOut.dataFirst()); |
---|
1087 | } |
---|
1088 | } |
---|
1089 | |
---|
1090 | void CField::inputField(CArray<double,3>& fieldOut) |
---|
1091 | { |
---|
1092 | map<int, CArray<double,1> >::iterator it; |
---|
1093 | for (it = data_srv.begin(); it != data_srv.end(); it++) |
---|
1094 | { |
---|
1095 | grid->inputField(it->first, fieldOut.dataFirst(), it->second); |
---|
1096 | } |
---|
1097 | } |
---|
1098 | |
---|
1099 | void CField::inputField(CArray<double,2>& fieldOut) |
---|
1100 | { |
---|
1101 | map<int, CArray<double,1> >::iterator it; |
---|
1102 | for(it = data_srv.begin(); it != data_srv.end(); it++) |
---|
1103 | { |
---|
1104 | grid->inputField(it->first, fieldOut.dataFirst(), it->second); |
---|
1105 | } |
---|
1106 | } |
---|
1107 | |
---|
1108 | void CField::inputField(CArray<double,1>& fieldOut) |
---|
1109 | { |
---|
1110 | map<int, CArray<double,1> >::iterator it; |
---|
1111 | for (it = data_srv.begin(); it != data_srv.end(); it++) |
---|
1112 | { |
---|
1113 | grid->inputField(it->first, fieldOut.dataFirst(), it->second); |
---|
1114 | } |
---|
1115 | } |
---|
1116 | |
---|
1117 | void CField::outputCompressedField(CArray<double,1>& fieldOut) |
---|
1118 | { |
---|
1119 | map<int, CArray<double,1> >::iterator it; |
---|
1120 | |
---|
1121 | for (it = data_srv.begin(); it != data_srv.end(); it++) |
---|
1122 | { |
---|
1123 | grid->outputCompressedField(it->first, it->second, fieldOut.dataFirst()); |
---|
1124 | } |
---|
1125 | } |
---|
1126 | |
---|
1127 | ///------------------------------------------------------------------- |
---|
1128 | |
---|
1129 | void CField::parse(xml::CXMLNode& node) |
---|
1130 | { |
---|
1131 | SuperClass::parse(node); |
---|
1132 | if (!node.getContent(this->content)) |
---|
1133 | { |
---|
1134 | if (node.goToChildElement()) |
---|
1135 | { |
---|
1136 | do |
---|
1137 | { |
---|
1138 | if (node.getElementName() == "variable" || node.getElementName() == "variable_group") this->getVirtualVariableGroup()->parseChild(node); |
---|
1139 | } while (node.goToNextElement()); |
---|
1140 | node.goToParentElement(); |
---|
1141 | } |
---|
1142 | } |
---|
1143 | } |
---|
1144 | |
---|
1145 | /*! |
---|
1146 | This function retrieves Id of corresponding domain_ref and axis_ref (if any) |
---|
1147 | of a field. In some cases, only domain exists but axis doesn't |
---|
1148 | \return pair of Domain and Axis id |
---|
1149 | */ |
---|
1150 | const std::vector<StdString>& CField::getRefDomainAxisIds() |
---|
1151 | { |
---|
1152 | CGrid* cgPtr = getRelGrid(); |
---|
1153 | if (NULL != cgPtr) |
---|
1154 | { |
---|
1155 | std::vector<StdString>::iterator it; |
---|
1156 | if (!domain_ref.isEmpty()) |
---|
1157 | { |
---|
1158 | std::vector<StdString> domainList = cgPtr->getDomainList(); |
---|
1159 | it = std::find(domainList.begin(), domainList.end(), domain_ref.getValue()); |
---|
1160 | if (domainList.end() != it) domAxisScalarIds_[0] = *it; |
---|
1161 | } |
---|
1162 | |
---|
1163 | if (!axis_ref.isEmpty()) |
---|
1164 | { |
---|
1165 | std::vector<StdString> axisList = cgPtr->getAxisList(); |
---|
1166 | it = std::find(axisList.begin(), axisList.end(), axis_ref.getValue()); |
---|
1167 | if (axisList.end() != it) domAxisScalarIds_[1] = *it; |
---|
1168 | } |
---|
1169 | |
---|
1170 | if (!scalar_ref.isEmpty()) |
---|
1171 | { |
---|
1172 | std::vector<StdString> scalarList = cgPtr->getScalarList(); |
---|
1173 | it = std::find(scalarList.begin(), scalarList.end(), scalar_ref.getValue()); |
---|
1174 | if (scalarList.end() != it) domAxisScalarIds_[2] = *it; |
---|
1175 | } |
---|
1176 | } |
---|
1177 | return (domAxisScalarIds_); |
---|
1178 | } |
---|
1179 | |
---|
1180 | CVariable* CField::addVariable(const string& id) |
---|
1181 | { |
---|
1182 | return vVariableGroup->createChild(id); |
---|
1183 | } |
---|
1184 | |
---|
1185 | CVariableGroup* CField::addVariableGroup(const string& id) |
---|
1186 | { |
---|
1187 | return vVariableGroup->createChildGroup(id); |
---|
1188 | } |
---|
1189 | |
---|
1190 | void CField::sendAddAllVariables() |
---|
1191 | { |
---|
1192 | if (!getAllVariables().empty()) |
---|
1193 | { |
---|
1194 | // Firstly, it's necessary to add virtual variable group |
---|
1195 | sendAddVariableGroup(getVirtualVariableGroup()->getId()); |
---|
1196 | |
---|
1197 | // Okie, now we can add to this variable group |
---|
1198 | std::vector<CVariable*> allVar = getAllVariables(); |
---|
1199 | std::vector<CVariable*>::const_iterator it = allVar.begin(); |
---|
1200 | std::vector<CVariable*>::const_iterator itE = allVar.end(); |
---|
1201 | |
---|
1202 | for (; it != itE; ++it) |
---|
1203 | { |
---|
1204 | this->sendAddVariable((*it)->getId()); |
---|
1205 | (*it)->sendAllAttributesToServer(); |
---|
1206 | (*it)->sendValue(); |
---|
1207 | } |
---|
1208 | } |
---|
1209 | } |
---|
1210 | |
---|
1211 | void CField::sendAddVariable(const string& id) |
---|
1212 | { |
---|
1213 | CContext* context = CContext::getCurrent(); |
---|
1214 | |
---|
1215 | if (!context->hasServer) |
---|
1216 | { |
---|
1217 | CContextClient* client = context->client; |
---|
1218 | |
---|
1219 | CEventClient event(this->getType(),EVENT_ID_ADD_VARIABLE); |
---|
1220 | if (client->isServerLeader()) |
---|
1221 | { |
---|
1222 | CMessage msg; |
---|
1223 | msg << this->getId(); |
---|
1224 | msg << id; |
---|
1225 | const std::list<int>& ranks = client->getRanksServerLeader(); |
---|
1226 | for (std::list<int>::const_iterator itRank = ranks.begin(), itRankEnd = ranks.end(); itRank != itRankEnd; ++itRank) |
---|
1227 | event.push(*itRank,1,msg); |
---|
1228 | client->sendEvent(event); |
---|
1229 | } |
---|
1230 | else client->sendEvent(event); |
---|
1231 | } |
---|
1232 | } |
---|
1233 | |
---|
1234 | void CField::sendAddVariableGroup(const string& id) |
---|
1235 | { |
---|
1236 | CContext* context = CContext::getCurrent(); |
---|
1237 | if (!context->hasServer) |
---|
1238 | { |
---|
1239 | CContextClient* client = context->client; |
---|
1240 | |
---|
1241 | CEventClient event(this->getType(),EVENT_ID_ADD_VARIABLE_GROUP); |
---|
1242 | if (client->isServerLeader()) |
---|
1243 | { |
---|
1244 | CMessage msg; |
---|
1245 | msg << this->getId(); |
---|
1246 | msg << id; |
---|
1247 | const std::list<int>& ranks = client->getRanksServerLeader(); |
---|
1248 | for (std::list<int>::const_iterator itRank = ranks.begin(), itRankEnd = ranks.end(); itRank != itRankEnd; ++itRank) |
---|
1249 | event.push(*itRank,1,msg); |
---|
1250 | client->sendEvent(event); |
---|
1251 | } |
---|
1252 | else client->sendEvent(event); |
---|
1253 | } |
---|
1254 | } |
---|
1255 | |
---|
1256 | void CField::recvAddVariable(CEventServer& event) |
---|
1257 | { |
---|
1258 | |
---|
1259 | CBufferIn* buffer = event.subEvents.begin()->buffer; |
---|
1260 | string id; |
---|
1261 | *buffer >> id; |
---|
1262 | get(id)->recvAddVariable(*buffer); |
---|
1263 | } |
---|
1264 | |
---|
1265 | void CField::recvAddVariable(CBufferIn& buffer) |
---|
1266 | { |
---|
1267 | string id; |
---|
1268 | buffer >> id; |
---|
1269 | addVariable(id); |
---|
1270 | } |
---|
1271 | |
---|
1272 | void CField::recvAddVariableGroup(CEventServer& event) |
---|
1273 | { |
---|
1274 | |
---|
1275 | CBufferIn* buffer = event.subEvents.begin()->buffer; |
---|
1276 | string id; |
---|
1277 | *buffer >> id; |
---|
1278 | get(id)->recvAddVariableGroup(*buffer); |
---|
1279 | } |
---|
1280 | |
---|
1281 | void CField::recvAddVariableGroup(CBufferIn& buffer) |
---|
1282 | { |
---|
1283 | string id; |
---|
1284 | buffer >> id; |
---|
1285 | addVariableGroup(id); |
---|
1286 | } |
---|
1287 | |
---|
1288 | DEFINE_REF_FUNC(Field,field) |
---|
1289 | } // namespace xios |
---|