1 // Copyright (C) 2010-2016 CEA/DEN, EDF R&D
3 // This library is free software; you can redistribute it and/or
4 // modify it under the terms of the GNU Lesser General Public
5 // License as published by the Free Software Foundation; either
6 // version 2.1 of the License, or (at your option) any later version.
8 // This library is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 // Lesser General Public License for more details.
13 // You should have received a copy of the GNU Lesser General Public
14 // License along with this library; if not, write to the Free Software
15 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
19 // Author : Anthony Geay
21 #include "vtkMEDReader.h"
22 #include "vtkGenerateVectors.h"
24 #include "vtkMultiBlockDataSet.h"
25 #include "vtkInformation.h"
26 #include "vtkDataSetAttributes.h"
27 #include "vtkStringArray.h"
28 #include "vtkMutableDirectedGraph.h"
29 #include "vtkInformationStringKey.h"
31 #include "vtkUnsignedCharArray.h"
32 #include "vtkInformationVector.h"
33 #include "vtkSmartPointer.h"
34 #include "vtkVariantArray.h"
35 #include "vtkExecutive.h"
36 #include "vtkStreamingDemandDrivenPipeline.h"
37 #include "vtkMultiTimeStepAlgorithm.h"
38 #include "vtkUnstructuredGrid.h"
39 #include "vtkInformationQuadratureSchemeDefinitionVectorKey.h"
40 #include "vtkQuadratureSchemeDefinition.h"
41 #include "vtkPointData.h"
42 #include "vtkCellData.h"
43 #include "vtkCellType.h"
44 #include "vtkCellArray.h"
45 #include "vtkDoubleArray.h"
46 #include "vtkObjectFactory.h"
47 #include "vtkInformationDataObjectMetaDataKey.h"
49 #ifdef MEDREADER_USE_MPI
50 #include "vtkMultiProcessController.h"
51 #include "vtkPUnstructuredGridGhostCellsGenerator.h"
54 #include "MEDFileFieldRepresentationTree.hxx"
63 * This class stores properties in loading state mode (pvsm) when the MED file has not been read yet.
64 * The file is not read beacause FileName has not been informed yet ! So this class stores properties of vtkMEDReader instance that
65 * owns it and wait the vtkMEDReader::SetFileName to apply properties afterwards.
70 PropertyKeeper(vtkMEDReader *master):_master(master),IsGVActivated(false),GVValue(0),IsCMActivated(false),CMValue(0),IsGhostActivated(false),GCGCP(1) { }
71 void assignPropertiesIfNeeded();
72 bool arePropertiesOnTreeToSetAfter() const;
74 void pushFieldStatusEntry(const char* name, int status);
75 void pushGenerateVectorsValue(int value);
76 void pushChangeModeValue(int value);
77 void pushTimesFlagsStatusEntry(const char* name, int status);
78 void pushGhost(int value);
80 // pool of pairs to assign in SetFieldsStatus if needed. The use case is the load using pvsm.
81 std::vector< std::pair<std::string,int> > SetFieldsStatusPairs;
89 bool IsGhostActivated;
92 std::vector< std::pair<std::string,int> > TimesFlagsStatusPairs;
93 vtkMEDReader *_master;
96 void PropertyKeeper::assignPropertiesIfNeeded()
98 if(!this->SetFieldsStatusPairs.empty())
100 for(std::vector< std::pair<std::string,int> >::const_iterator it=this->SetFieldsStatusPairs.begin();it!=this->SetFieldsStatusPairs.end();it++)
101 _master->SetFieldsStatus((*it).first.c_str(),(*it).second);
102 this->SetFieldsStatusPairs.clear();
104 if(!this->TimesFlagsStatusPairs.empty())
106 for(std::vector< std::pair<std::string,int> >::const_iterator it=this->TimesFlagsStatusPairs.begin();it!=this->TimesFlagsStatusPairs.end();it++)
107 _master->SetTimesFlagsStatus((*it).first.c_str(),(*it).second);
108 this->TimesFlagsStatusPairs.clear();
110 if(this->IsGVActivated)
112 _master->GenerateVectors(this->GVValue);
113 this->IsGVActivated=false;
115 if(this->IsCMActivated)
117 _master->ChangeMode(this->CMValue);
118 this->IsCMActivated=false;
120 if(this->IsGhostActivated)
122 _master->GhostCellGeneratorCallForPara(this->GCGCP);
123 this->IsGhostActivated=false;
127 void PropertyKeeper::pushFieldStatusEntry(const char* name, int status)
130 for(std::vector< std::pair<std::string,int> >::const_iterator it=this->SetFieldsStatusPairs.begin();it!=this->SetFieldsStatusPairs.end() && !found;it++)
131 found=(*it).first==name;
133 this->SetFieldsStatusPairs.push_back(std::pair<std::string,int>(name,status));
136 void PropertyKeeper::pushTimesFlagsStatusEntry(const char* name, int status)
139 for(std::vector< std::pair<std::string,int> >::const_iterator it=this->TimesFlagsStatusPairs.begin();it!=this->TimesFlagsStatusPairs.end() && !found;it++)
140 found=(*it).first==name;
142 this->TimesFlagsStatusPairs.push_back(std::pair<std::string,int>(name,status));
145 void PropertyKeeper::pushGenerateVectorsValue(int value)
147 this->IsGVActivated=true;
151 void PropertyKeeper::pushChangeModeValue(int value)
153 this->IsCMActivated=true;
157 void PropertyKeeper::pushGhost(int value)
159 this->IsGhostActivated=true;
163 bool PropertyKeeper::arePropertiesOnTreeToSetAfter() const
165 return !SetFieldsStatusPairs.empty();
168 class vtkMEDReader::vtkMEDReaderInternal
172 vtkMEDReaderInternal(vtkMEDReader *master):TK(0),IsMEDOrSauv(true),IsStdOrMode(false),GenerateVect(false),SIL(0),LastLev0(-1),FirstCall0(2),PK(master),MyMTime(0),GCGCP(true)
178 return false; // TODO Useless and buggy
185 ~vtkMEDReaderInternal()
191 MEDFileFieldRepresentationTree Tree;
193 std::string FileName;
194 //when true the file is MED file. when false it is a Sauv file
196 //when false -> std, true -> mode. By default std (false).
198 //when false -> do nothing. When true cut off or extend to nbOfCompo=3 vector arrays.
200 std::string DftMeshName;
201 // Store the vtkMutableDirectedGraph that represents links between family, groups and cell types
202 vtkMutableDirectedGraph* SIL;
203 // store the lev0 id in Tree corresponding to the TIME_STEPS in the pipeline.
205 // The property keeper is usable only in pvsm mode.
208 std::set<std::string> _wonderful_set;// this set is used by SetFieldsStatus method to detect the fact that SetFieldsStatus has been called for all items ! Great Items are not sorted ! Why ?
209 std::map<std::string,bool> _wonderful_ref;// this map stores the state before a SetFieldsStatus status.
213 unsigned char FirstCall0;
216 vtkStandardNewMacro(vtkMEDReader);
218 // vtkInformationKeyMacro(vtkMEDReader, META_DATA, DataObjectMetaData); // Here we need to customize vtkMEDReader::META_DATA method
219 // start of overload of vtkInformationKeyMacro
220 static vtkInformationDataObjectMetaDataKey *vtkMEDReader_META_DATA=new vtkInformationDataObjectMetaDataKey("META_DATA","vtkMEDReader");
222 vtkInformationDataObjectMetaDataKey *vtkMEDReader::META_DATA()
224 static const char ZE_KEY[]="vtkMEDReader::META_DATA";
225 vtkInformationDataObjectMetaDataKey *ret(vtkMEDReader_META_DATA);
226 MEDCoupling::GlobalDict *gd(MEDCoupling::GlobalDict::GetInstance());
227 if(!gd->hasKey(ZE_KEY))
228 {// here META_DATA is put on global var to be exchanged with other filters without dependancy of MEDReader. Please do not change ZE_KEY !
229 std::ostringstream oss; oss << ret;
230 gd->setKeyValue(ZE_KEY,oss.str());
234 // end of overload of vtkInformationKeyMacro
236 vtkMEDReader::vtkMEDReader():Internal(new vtkMEDReaderInternal(this))
238 this->SetNumberOfInputPorts(0);
239 this->SetNumberOfOutputPorts(1);
242 vtkMEDReader::~vtkMEDReader()
244 delete this->Internal;
248 void vtkMEDReader::Reload()
250 std::string fName((const char *)this->GetFileName());
251 delete this->Internal;
252 this->Internal=new vtkMEDReaderInternal(this);
253 this->SetFileName(fName.c_str());
256 int vtkMEDReader::GetServerModifTime()
258 if( !this->Internal )
260 return this->Internal->MyMTime;
263 void vtkMEDReader::GenerateVectors(int val)
265 if ( !this->Internal )
268 if(this->Internal->FileName.empty())
270 this->Internal->PK.pushGenerateVectorsValue(val);
273 //not pvsm mode (general case)
274 bool val2((bool)val);
275 if(val2!=this->Internal->GenerateVect)
277 this->Internal->GenerateVect=val2;
282 void vtkMEDReader::ChangeMode(int newMode)
284 if ( !this->Internal )
287 if(this->Internal->FileName.empty())
289 this->Internal->PK.pushChangeModeValue(newMode);
292 //not pvsm mode (general case)
293 this->Internal->IsStdOrMode=newMode!=0;
297 void vtkMEDReader::GhostCellGeneratorCallForPara(int gcgcp)
299 if ( !this->Internal )
302 if(this->Internal->FileName.empty())
304 this->Internal->PK.pushGhost(gcgcp);
307 bool newVal(gcgcp!=0);
308 if(newVal!=this->Internal->GCGCP)
310 this->Internal->GCGCP=newVal;
315 const char *vtkMEDReader::GetSeparator()
317 return MEDFileFieldRepresentationLeavesArrays::ZE_SEP;
320 void vtkMEDReader::SetFileName(const char *fname)
326 this->Internal->FileName=fname;
327 std::size_t pos(this->Internal->FileName.find_last_of('.'));
328 if(pos!=std::string::npos)
330 std::string ext(this->Internal->FileName.substr(pos));
331 if(ext.find("sauv")!=std::string::npos)
332 this->Internal->IsMEDOrSauv=false;
334 if(this->Internal->Tree.getNumberOfLeavesArrays()==0)
336 int iPart(-1),nbOfParts(-1);
337 #ifdef MEDREADER_USE_MPI
338 vtkMultiProcessController *vmpc(vtkMultiProcessController::GetGlobalController());
341 iPart=vmpc->GetLocalProcessId();
342 nbOfParts=vmpc->GetNumberOfProcesses();
345 this->Internal->Tree.loadMainStructureOfFile(this->Internal->FileName.c_str(),this->Internal->IsMEDOrSauv,iPart,nbOfParts);
346 if(!this->Internal->PK.arePropertiesOnTreeToSetAfter())
347 this->Internal->Tree.activateTheFirst();//This line manually initialize the status of server (this) with the remote client.
348 this->Internal->TK.setMaxNumberOfTimeSteps(this->Internal->Tree.getMaxNumberOfTimeSteps());
351 this->Internal->PK.assignPropertiesIfNeeded();
353 catch(INTERP_KERNEL::Exception& e)
355 delete this->Internal;
357 std::ostringstream oss;
358 oss << "Exception has been thrown in vtkMEDReader::SetFileName : " << e.what() << std::endl;
359 if(this->HasObserver("ErrorEvent") )
360 this->InvokeEvent("ErrorEvent",const_cast<char *>(oss.str().c_str()));
362 vtkOutputWindowDisplayErrorText(const_cast<char *>(oss.str().c_str()));
363 vtkObject::BreakOnError();
367 char *vtkMEDReader::GetFileName()
371 return const_cast<char *>(this->Internal->FileName.c_str());
374 int vtkMEDReader::RequestInformation(vtkInformation *request, vtkInformationVector **inputVector, vtkInformationVector *outputVector)
376 // std::cout << "########################################## vtkMEDReader::RequestInformation ##########################################" << std::endl;
381 // request->Print(cout);
382 vtkInformation *outInfo(outputVector->GetInformationObject(0));
383 outInfo->Set(vtkDataObject::DATA_TYPE_NAME(),"vtkMultiBlockDataSet");
384 this->UpdateSIL(request, outInfo);
386 // Set the meta data graph as a meta data key in the information
387 // That's all that is needed to transfer it along the pipeline
388 outInfo->Set(vtkMEDReader::META_DATA(),this->Internal->SIL);
391 this->PublishTimeStepsIfNeeded(outInfo,dummy);
393 catch(INTERP_KERNEL::Exception& e)
395 std::ostringstream oss;
396 oss << "Exception has been thrown in vtkMEDReader::RequestInformation : " << e.what() << std::endl;
397 if(this->HasObserver("ErrorEvent") )
398 this->InvokeEvent("ErrorEvent",const_cast<char *>(oss.str().c_str()));
400 vtkOutputWindowDisplayErrorText(const_cast<char *>(oss.str().c_str()));
401 vtkObject::BreakOnError();
407 int vtkMEDReader::RequestData(vtkInformation *request, vtkInformationVector **inputVector, vtkInformationVector *outputVector)
409 // std::cout << "########################################## vtkMEDReader::RequestData ##########################################" << std::endl;
414 // request->Print(cout);
415 vtkInformation *outInfo(outputVector->GetInformationObject(0));
416 vtkMultiBlockDataSet *output(vtkMultiBlockDataSet::SafeDownCast(outInfo->Get(vtkDataObject::DATA_OBJECT())));
417 bool isUpdated(false);
419 if(outInfo->Has(vtkStreamingDemandDrivenPipeline::UPDATE_TIME_STEP()))
420 reqTS=outInfo->Get(vtkStreamingDemandDrivenPipeline::UPDATE_TIME_STEP());
421 #ifndef MEDREADER_USE_MPI
422 this->FillMultiBlockDataSetInstance(output,reqTS);
424 if(this->Internal->GCGCP)
426 vtkSmartPointer<vtkPUnstructuredGridGhostCellsGenerator> gcg(vtkSmartPointer<vtkPUnstructuredGridGhostCellsGenerator>::New());
428 vtkDataSet *ret(RetrieveDataSetAtTime(reqTS));
429 gcg->SetInputData(ret);
432 gcg->SetUseGlobalPointIds(true);
433 gcg->SetBuildIfRequired(false);
435 output->SetBlock(0,gcg->GetOutput());
438 this->FillMultiBlockDataSetInstance(output,reqTS);
440 output->GetInformation()->Set(vtkDataObject::DATA_TIME_STEP(),reqTS);
441 // Is it really needed ? TODO
442 this->UpdateSIL(request, outInfo);
444 catch(INTERP_KERNEL::Exception& e)
446 std::cerr << "Exception has been thrown in vtkMEDReader::RequestData : " << e.what() << std::endl;
452 void vtkMEDReader::SetFieldsStatus(const char* name, int status)
454 if( !this->Internal )
457 //this->Internal->_wonderful_set.insert(name);
458 if(this->Internal->FileName.empty())
460 this->Internal->PK.pushFieldStatusEntry(name,status);
463 if(this->Internal->_wonderful_set.empty())
464 this->Internal->_wonderful_ref=this->Internal->Tree.dumpState();// start of SetFieldsStatus serie -> store ref to compare at the end of the SetFieldsStatus serie.
465 this->Internal->_wonderful_set.insert(name);
466 //not pvsm mode (general case)
469 this->Internal->Tree.changeStatusOfAndUpdateToHaveCoherentVTKDataSet(this->Internal->Tree.getIdHavingZeName(name),status);
470 if(this->Internal->_wonderful_set.size()==GetNumberOfFieldsTreeArrays())
472 if(this->Internal->_wonderful_ref!=this->Internal->Tree.dumpState())
474 if(!this->Internal->PluginStart0())
478 this->Internal->MyMTime++;
480 this->Internal->_wonderful_set.clear();
483 catch(INTERP_KERNEL::Exception& e)
485 if(!this->Internal->FileName.empty())
487 std::cerr << "vtkMEDReader::SetFieldsStatus error : " << e.what() << " *** WITH STATUS=" << status << endl;
493 int vtkMEDReader::GetNumberOfFieldsTreeArrays()
497 return this->Internal->Tree.getNumberOfLeavesArrays();
500 const char *vtkMEDReader::GetFieldsTreeArrayName(int index)
504 return this->Internal->Tree.getNameOfC(index);
507 int vtkMEDReader::GetFieldsTreeArrayStatus(const char *name)
512 int zeId(this->Internal->Tree.getIdHavingZeName(name));
513 int ret(this->Internal->Tree.getStatusOf(zeId));
517 void vtkMEDReader::SetTimesFlagsStatus(const char *name, int status)
522 if(this->Internal->FileName.empty())
524 this->Internal->PK.pushTimesFlagsStatusEntry(name,status);
527 //not pvsm mode (general case)
529 std::istringstream iss(name); iss >> pos;
530 this->Internal->TK.getTimesFlagArray()[pos].first=(bool)status;
531 if(pos==this->Internal->TK.getTimesFlagArray().size()-1)
532 if(!this->Internal->PluginStart0())
535 //this->Internal->TK.printSelf(std::cerr);
539 int vtkMEDReader::GetNumberOfTimesFlagsArrays()
543 return (int)this->Internal->TK.getTimesFlagArray().size();
546 const char *vtkMEDReader::GetTimesFlagsArrayName(int index)
548 return this->Internal->TK.getTimesFlagArray()[index].second.c_str();
551 int vtkMEDReader::GetTimesFlagsArrayStatus(const char *name)
556 std::istringstream iss(name); iss >> pos;
557 return (int)this->Internal->TK.getTimesFlagArray()[pos].first;
560 void vtkMEDReader::UpdateSIL(vtkInformation* request, vtkInformation *info)
564 std::string meshName(this->Internal->Tree.getActiveMeshName());
565 if(!this->Internal->SIL || meshName!=this->Internal->DftMeshName)
567 vtkMutableDirectedGraph *sil(vtkMutableDirectedGraph::New());
569 if(this->Internal->SIL)
570 this->Internal->SIL->Delete();
571 this->Internal->SIL=sil;
572 this->Internal->DftMeshName=meshName;
577 * The returned string is the name of the mesh activated which groups and families are in \a sil.
579 std::string vtkMEDReader::BuildSIL(vtkMutableDirectedGraph* sil)
582 return std::string();
584 vtkSmartPointer<vtkVariantArray> childEdge(vtkSmartPointer<vtkVariantArray>::New());
585 childEdge->InsertNextValue(0);
586 vtkSmartPointer<vtkVariantArray> crossEdge(vtkSmartPointer<vtkVariantArray>::New());
587 crossEdge->InsertNextValue(1);
588 // CrossEdge is an edge linking hierarchies.
589 vtkUnsignedCharArray* crossEdgesArray=vtkUnsignedCharArray::New();
590 crossEdgesArray->SetName("CrossEdges");
591 sil->GetEdgeData()->AddArray(crossEdgesArray);
592 crossEdgesArray->Delete();
593 std::vector<std::string> names;
594 // Now build the hierarchy.
595 vtkIdType rootId=sil->AddVertex();
596 names.push_back("SIL");
597 // Add global fields root
598 vtkIdType fieldsRoot(sil->AddChild(rootId,childEdge));
599 names.push_back("FieldsStatusTree");
600 this->Internal->Tree.feedSIL(sil,fieldsRoot,childEdge,names);
601 vtkIdType meshesFamsGrpsRoot(sil->AddChild(rootId,childEdge));
602 names.push_back("MeshesFamsGrps");
603 std::string dftMeshName(this->Internal->Tree.feedSILForFamsAndGrps(sil,meshesFamsGrpsRoot,childEdge,names));
604 // This array is used to assign names to nodes.
605 vtkStringArray *namesArray(vtkStringArray::New());
606 namesArray->SetName("Names");
607 namesArray->SetNumberOfTuples(sil->GetNumberOfVertices());
608 sil->GetVertexData()->AddArray(namesArray);
609 namesArray->Delete();
610 std::vector<std::string>::const_iterator iter;
612 for(cc=0, iter=names.begin(); iter!=names.end(); ++iter, ++cc)
613 namesArray->SetValue(cc,(*iter).c_str());
617 double vtkMEDReader::PublishTimeStepsIfNeeded(vtkInformation *outInfo, bool& isUpdated)
623 std::vector<double> tsteps;
624 if(!this->Internal->IsStdOrMode)
625 tsteps=this->Internal->Tree.getTimeSteps(lev0,this->Internal->TK);
627 { tsteps.resize(1); tsteps[0]=0.; }
629 if(lev0!=this->Internal->LastLev0)
633 timeRange[0]=tsteps.front();
634 timeRange[1]=tsteps.back();
635 outInfo->Set(vtkStreamingDemandDrivenPipeline::TIME_STEPS(),&tsteps[0],tsteps.size());
636 outInfo->Set(vtkStreamingDemandDrivenPipeline::TIME_RANGE(),timeRange,2);
637 this->Internal->LastLev0=lev0;
639 return tsteps.front();
642 void vtkMEDReader::FillMultiBlockDataSetInstance(vtkMultiBlockDataSet *output, double reqTS)
644 if( !this->Internal )
646 vtkDataSet *ret(RetrieveDataSetAtTime(reqTS));
647 output->SetBlock(0,ret);
651 vtkDataSet *vtkMEDReader::RetrieveDataSetAtTime(double reqTS)
653 if( !this->Internal )
655 std::string meshName;
656 vtkDataSet *ret(this->Internal->Tree.buildVTKInstance(this->Internal->IsStdOrMode,reqTS,meshName,this->Internal->TK));
657 if(this->Internal->GenerateVect)
659 vtkGenerateVectors::Operate(ret->GetPointData());
660 vtkGenerateVectors::Operate(ret->GetCellData());
661 vtkGenerateVectors::Operate(ret->GetFieldData());
666 void vtkMEDReader::PrintSelf(ostream& os, vtkIndent indent)
668 this->Superclass::PrintSelf(os, indent);