1 // Copyright (C) 2010-2015 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 #ifdef MEDREADER_USE_MPI
48 #include "vtkMultiProcessController.h"
51 #include "MEDFileFieldRepresentationTree.hxx"
59 * This class stores properties in loading state mode (pvsm) when the MED file has not been read yet.
60 * The file is not read beacause FileName has not been informed yet ! So this class stores properties of vtkMEDReader instance that
61 * owns it and wait the vtkMEDReader::SetFileName to apply properties afterwards.
66 PropertyKeeper(vtkMEDReader *master):_master(master),IsGVActivated(false),GVValue(0),IsCMActivated(false),CMValue(0) { }
67 void assignPropertiesIfNeeded();
68 bool arePropertiesOnTreeToSetAfter() const;
70 void pushFieldStatusEntry(const char* name, int status);
71 void pushGenerateVectorsValue(int value);
72 void pushChangeModeValue(int value);
73 void pushTimesFlagsStatusEntry(const char* name, int status);
75 // pool of pairs to assign in SetFieldsStatus if needed. The use case is the load using pvsm.
76 std::vector< std::pair<std::string,int> > SetFieldsStatusPairs;
84 std::vector< std::pair<std::string,int> > TimesFlagsStatusPairs;
85 vtkMEDReader *_master;
88 void PropertyKeeper::assignPropertiesIfNeeded()
90 if(!this->SetFieldsStatusPairs.empty())
92 for(std::vector< std::pair<std::string,int> >::const_iterator it=this->SetFieldsStatusPairs.begin();it!=this->SetFieldsStatusPairs.end();it++)
93 _master->SetFieldsStatus((*it).first.c_str(),(*it).second);
94 this->SetFieldsStatusPairs.clear();
96 if(!this->TimesFlagsStatusPairs.empty())
98 for(std::vector< std::pair<std::string,int> >::const_iterator it=this->TimesFlagsStatusPairs.begin();it!=this->TimesFlagsStatusPairs.end();it++)
99 _master->SetTimesFlagsStatus((*it).first.c_str(),(*it).second);
100 this->TimesFlagsStatusPairs.clear();
102 if(this->IsGVActivated)
104 _master->GenerateVectors(this->GVValue);
105 this->IsGVActivated=false;
107 if(this->IsCMActivated)
109 _master->ChangeMode(this->CMValue);
110 this->IsCMActivated=false;
114 void PropertyKeeper::pushFieldStatusEntry(const char* name, int status)
117 for(std::vector< std::pair<std::string,int> >::const_iterator it=this->SetFieldsStatusPairs.begin();it!=this->SetFieldsStatusPairs.end() && !found;it++)
118 found=(*it).first==name;
120 this->SetFieldsStatusPairs.push_back(std::pair<std::string,int>(name,status));
123 void PropertyKeeper::pushTimesFlagsStatusEntry(const char* name, int status)
126 for(std::vector< std::pair<std::string,int> >::const_iterator it=this->TimesFlagsStatusPairs.begin();it!=this->TimesFlagsStatusPairs.end() && !found;it++)
127 found=(*it).first==name;
129 this->TimesFlagsStatusPairs.push_back(std::pair<std::string,int>(name,status));
132 void PropertyKeeper::pushGenerateVectorsValue(int value)
134 this->IsGVActivated=true;
138 void PropertyKeeper::pushChangeModeValue(int value)
140 this->IsCMActivated=true;
144 bool PropertyKeeper::arePropertiesOnTreeToSetAfter() const
146 return !SetFieldsStatusPairs.empty();
149 class vtkMEDReader::vtkMEDReaderInternal
153 vtkMEDReaderInternal(vtkMEDReader *master):TK(0),IsMEDOrSauv(true),IsStdOrMode(false),GenerateVect(false),SIL(0),LastLev0(-1),FirstCall0(2),PK(master)
165 ~vtkMEDReaderInternal()
171 MEDFileFieldRepresentationTree Tree;
173 std::string FileName;
174 //when true the file is MED file. when false it is a Sauv file
176 //when false -> std, true -> mode. By default std (false).
178 //when false -> do nothing. When true cut off or extend to nbOfCompo=3 vector arrays.
180 std::string DftMeshName;
181 // Store the vtkMutableDirectedGraph that represents links between family, groups and cell types
182 vtkMutableDirectedGraph* SIL;
183 // store the lev0 id in Tree corresponding to the TIME_STEPS in the pipeline.
185 // The property keeper is usable only in pvsm mode.
188 unsigned char FirstCall0;
191 vtkStandardNewMacro(vtkMEDReader);
193 vtkMEDReader::vtkMEDReader():Internal(new vtkMEDReaderInternal(this))
195 this->SetNumberOfInputPorts(0);
196 this->SetNumberOfOutputPorts(1);
199 vtkMEDReader::~vtkMEDReader()
201 delete this->Internal;
204 void vtkMEDReader::Reload(int a)
208 std::cerr << "vtkMEDReader::Reload" << a << std::endl;
209 std::string fName((const char *)this->GetFileName());
210 delete this->Internal;
211 this->Internal=new vtkMEDReaderInternal(this);
212 this->SetFileName(fName.c_str());
215 void vtkMEDReader::GenerateVectors(int val)
217 if(this->Internal->FileName.empty())
219 this->Internal->PK.pushGenerateVectorsValue(val);
222 //not pvsm mode (general case)
223 bool val2((bool)val);
224 if(val2!=this->Internal->GenerateVect)
226 this->Internal->GenerateVect=val2;
231 void vtkMEDReader::ChangeMode(int newMode)
233 if(this->Internal->FileName.empty())
235 this->Internal->PK.pushChangeModeValue(newMode);
238 //not pvsm mode (general case)
239 this->Internal->IsStdOrMode=newMode!=0;
240 //std::cerr << "vtkMEDReader::ChangeMode : " << this->Internal->IsStdOrMode << std::endl;
244 const char *vtkMEDReader::GetSeparator()
246 return MEDFileFieldRepresentationLeavesArrays::ZE_SEP;
249 void vtkMEDReader::SetFileName(const char *fname)
253 this->Internal->FileName=fname;
254 std::size_t pos(this->Internal->FileName.find_last_of('.'));
255 if(pos!=std::string::npos)
257 std::string ext(this->Internal->FileName.substr(pos));
258 if(ext.find("sauv")!=std::string::npos)
259 this->Internal->IsMEDOrSauv=false;
261 if(this->Internal->Tree.getNumberOfLeavesArrays()==0)
263 int iPart(-1),nbOfParts(-1);
264 #ifdef MEDREADER_USE_MPI
265 vtkMultiProcessController *vmpc(vtkMultiProcessController::GetGlobalController());
268 iPart=vmpc->GetLocalProcessId();
269 nbOfParts=vmpc->GetNumberOfProcesses();
272 this->Internal->Tree.loadMainStructureOfFile(this->Internal->FileName.c_str(),this->Internal->IsMEDOrSauv,iPart,nbOfParts);
273 if(!this->Internal->PK.arePropertiesOnTreeToSetAfter())
274 this->Internal->Tree.activateTheFirst();//This line manually initialize the status of server (this) with the remote client.
275 this->Internal->TK.setMaxNumberOfTimeSteps(this->Internal->Tree.getMaxNumberOfTimeSteps());
278 this->Internal->PK.assignPropertiesIfNeeded();
280 catch(INTERP_KERNEL::Exception& e)
282 delete this->Internal;
284 std::ostringstream oss;
285 oss << "Exception has been thrown in vtkMEDReader::SetFileName : " << e.what() << std::endl;
286 if(this->HasObserver("ErrorEvent") )
287 this->InvokeEvent("ErrorEvent",const_cast<char *>(oss.str().c_str()));
289 vtkOutputWindowDisplayErrorText(const_cast<char *>(oss.str().c_str()));
290 vtkObject::BreakOnError();
294 char *vtkMEDReader::GetFileName()
296 return const_cast<char *>(this->Internal->FileName.c_str());
299 int vtkMEDReader::RequestInformation(vtkInformation *request, vtkInformationVector **inputVector, vtkInformationVector *outputVector)
301 //std::cerr << "########################################## RequestInformation ##########################################" << std::endl;
306 vtkInformation *outInfo(outputVector->GetInformationObject(0));
307 outInfo->Set(vtkDataObject::DATA_TYPE_NAME(),"vtkMultiBlockDataSet");
308 this->UpdateSIL(outInfo);
311 this->PublishTimeStepsIfNeeded(outInfo,dummy);
313 catch(INTERP_KERNEL::Exception& e)
315 std::ostringstream oss;
316 oss << "Exception has been thrown in vtkMEDReader::RequestInformation : " << e.what() << std::endl;
317 if(this->HasObserver("ErrorEvent") )
318 this->InvokeEvent("ErrorEvent",const_cast<char *>(oss.str().c_str()));
320 vtkOutputWindowDisplayErrorText(const_cast<char *>(oss.str().c_str()));
321 vtkObject::BreakOnError();
327 int vtkMEDReader::RequestData(vtkInformation *request, vtkInformationVector **inputVector, vtkInformationVector *outputVector)
329 //std::cerr << "########################################## RequestData ##########################################";
334 vtkInformation *outInfo(outputVector->GetInformationObject(0));
335 vtkMultiBlockDataSet *output(vtkMultiBlockDataSet::SafeDownCast(outInfo->Get(vtkDataObject::DATA_OBJECT())));
336 bool isUpdated(false);
338 if(outInfo->Has(vtkStreamingDemandDrivenPipeline::UPDATE_TIME_STEP()))
339 reqTS=outInfo->Get(vtkStreamingDemandDrivenPipeline::UPDATE_TIME_STEP());
340 //std::cerr << reqTS << std::endl;
341 this->FillMultiBlockDataSetInstance(output,reqTS);
342 output->GetInformation()->Set(vtkDataObject::DATA_TIME_STEP(),reqTS);
343 this->UpdateSIL(outInfo);
344 //this->UpdateProgress((float) progress/((float) maxprogress-1));
346 catch(INTERP_KERNEL::Exception& e)
348 std::cerr << "Exception has been thrown in vtkMEDReader::RequestData : " << e.what() << std::endl;
354 void vtkMEDReader::SetFieldsStatus(const char* name, int status)
356 //std::cerr << "vtkMEDReader::SetFieldsStatus(" << name << "," << status << ") called !" << std::endl;
357 if(this->Internal->FileName.empty())
359 this->Internal->PK.pushFieldStatusEntry(name,status);
362 //not pvsm mode (general case)
365 this->Internal->Tree.changeStatusOfAndUpdateToHaveCoherentVTKDataSet(this->Internal->Tree.getIdHavingZeName(name),status);
366 if(std::string(name)==GetFieldsTreeArrayName(GetNumberOfFieldsTreeArrays()-1))
367 if(!this->Internal->PluginStart0())
370 catch(INTERP_KERNEL::Exception& e)
372 if(!this->Internal->FileName.empty())
374 std::cerr << "vtkMEDReader::SetFieldsStatus error : " << e.what() << " *** WITH STATUS=" << status << endl;
380 int vtkMEDReader::GetNumberOfFieldsTreeArrays()
384 int ret(this->Internal->Tree.getNumberOfLeavesArrays());
385 //std::cerr << "vtkMEDReader::GetNumberOfFieldsTreeArrays called ! " << ret << std::endl;
389 const char *vtkMEDReader::GetFieldsTreeArrayName(int index)
391 return this->Internal->Tree.getNameOfC(index);
392 //std::cerr << "vtkMEDReader::GetFieldsTreeArrayName(" << index << ") called ! " << ret << std::endl;
395 int vtkMEDReader::GetFieldsTreeArrayStatus(const char *name)
397 int zeId(this->Internal->Tree.getIdHavingZeName(name));
398 int ret(this->Internal->Tree.getStatusOf(zeId));
402 void vtkMEDReader::SetTimesFlagsStatus(const char *name, int status)
404 if(this->Internal->FileName.empty())
406 this->Internal->PK.pushTimesFlagsStatusEntry(name,status);
409 //not pvsm mode (general case)
411 std::istringstream iss(name); iss >> pos;
412 this->Internal->TK.getTimesFlagArray()[pos].first=(bool)status;
413 if(pos==this->Internal->TK.getTimesFlagArray().size()-1)
414 if(!this->Internal->PluginStart0())
417 //this->Internal->TK.printSelf(std::cerr);
421 int vtkMEDReader::GetNumberOfTimesFlagsArrays()
425 return (int)this->Internal->TK.getTimesFlagArray().size();
428 const char *vtkMEDReader::GetTimesFlagsArrayName(int index)
430 return this->Internal->TK.getTimesFlagArray()[index].second.c_str();
433 int vtkMEDReader::GetTimesFlagsArrayStatus(const char *name)
436 std::istringstream iss(name); iss >> pos;
437 return (int)this->Internal->TK.getTimesFlagArray()[pos].first;
440 void vtkMEDReader::UpdateSIL(vtkInformation *info)
444 vtkMutableDirectedGraph *sil(vtkMutableDirectedGraph::New());
445 std::string meshName(this->BuildSIL(sil));
446 if(meshName!=this->Internal->DftMeshName)
448 if(this->Internal->SIL)
449 this->Internal->SIL->Delete();
450 this->Internal->SIL=sil;
451 this->Internal->DftMeshName=meshName;
452 info->Set(vtkDataObject::SIL(),this->Internal->SIL);
453 //request->AppendUnique(vtkExecutive::KEYS_TO_COPY(),vtkDataObject::SIL());
462 * The returned string is the name of the mesh activated which groups and families are in \a sil.
464 std::string vtkMEDReader::BuildSIL(vtkMutableDirectedGraph* sil)
467 vtkSmartPointer<vtkVariantArray> childEdge(vtkSmartPointer<vtkVariantArray>::New());
468 childEdge->InsertNextValue(0);
469 vtkSmartPointer<vtkVariantArray> crossEdge(vtkSmartPointer<vtkVariantArray>::New());
470 crossEdge->InsertNextValue(1);
471 // CrossEdge is an edge linking hierarchies.
472 vtkUnsignedCharArray* crossEdgesArray=vtkUnsignedCharArray::New();
473 crossEdgesArray->SetName("CrossEdges");
474 sil->GetEdgeData()->AddArray(crossEdgesArray);
475 crossEdgesArray->Delete();
476 std::vector<std::string> names;
477 // Now build the hierarchy.
478 vtkIdType rootId=sil->AddVertex();
479 names.push_back("SIL");
480 // Add global fields root
481 vtkIdType fieldsRoot(sil->AddChild(rootId,childEdge));
482 names.push_back("FieldsStatusTree");
483 this->Internal->Tree.feedSIL(sil,fieldsRoot,childEdge,names);
484 vtkIdType meshesFamsGrpsRoot(sil->AddChild(rootId,childEdge));
485 names.push_back("MeshesFamsGrps");
486 std::string dftMeshName(this->Internal->Tree.feedSILForFamsAndGrps(sil,meshesFamsGrpsRoot,childEdge,names));
487 // This array is used to assign names to nodes.
488 vtkStringArray *namesArray(vtkStringArray::New());
489 namesArray->SetName("Names");
490 namesArray->SetNumberOfTuples(sil->GetNumberOfVertices());
491 sil->GetVertexData()->AddArray(namesArray);
492 namesArray->Delete();
493 std::vector<std::string>::const_iterator iter;
495 for(cc=0, iter=names.begin(); iter!=names.end(); ++iter, ++cc)
496 namesArray->SetValue(cc,(*iter).c_str());
500 double vtkMEDReader::PublishTimeStepsIfNeeded(vtkInformation *outInfo, bool& isUpdated)
503 std::vector<double> tsteps;
504 if(!this->Internal->IsStdOrMode)
505 tsteps=this->Internal->Tree.getTimeSteps(lev0,this->Internal->TK);
507 { tsteps.resize(1); tsteps[0]=0.; }
509 if(lev0!=this->Internal->LastLev0)
513 timeRange[0]=tsteps.front();
514 timeRange[1]=tsteps.back();
515 outInfo->Set(vtkStreamingDemandDrivenPipeline::TIME_STEPS(),&tsteps[0],tsteps.size());
516 outInfo->Set(vtkStreamingDemandDrivenPipeline::TIME_RANGE(),timeRange,2);
517 this->Internal->LastLev0=lev0;
519 return tsteps.front();
522 void vtkMEDReader::FillMultiBlockDataSetInstance(vtkMultiBlockDataSet *output, double reqTS)
524 std::string meshName;
525 vtkDataSet *ret(this->Internal->Tree.buildVTKInstance(this->Internal->IsStdOrMode,reqTS,meshName,this->Internal->TK));
526 if(this->Internal->GenerateVect)
528 vtkGenerateVectors::Operate(ret->GetPointData());
529 vtkGenerateVectors::Operate(ret->GetCellData());
530 vtkGenerateVectors::Operate(ret->GetFieldData());
532 output->SetBlock(0,ret);
536 void vtkMEDReader::PrintSelf(ostream& os, vtkIndent indent)
538 this->Superclass::PrintSelf(os, indent);