1 // Copyright (C) 2010-2014 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"
48 #include "MEDFileFieldRepresentationTree.hxx"
56 * This class stores properties in loading state mode (pvsm) when the MED file has not been read yet.
57 * The file is not read beacause FileName has not been informed yet ! So this class stores properties of vtkMEDReader instance that
58 * owns it and wait the vtkMEDReader::SetFileName to apply properties afterwards.
63 PropertyKeeper(vtkMEDReader *master):_master(master),IsGVActivated(false),GVValue(0),IsCMActivated(false),CMValue(0) { }
64 void assignPropertiesIfNeeded();
65 bool arePropertiesOnTreeToSetAfter() const;
67 void pushFieldStatusEntry(const char* name, int status);
68 void pushGenerateVectorsValue(int value);
69 void pushChangeModeValue(int value);
70 void pushTimesFlagsStatusEntry(const char* name, int status);
72 // pool of pairs to assign in SetFieldsStatus if needed. The use case is the load using pvsm.
73 std::vector< std::pair<std::string,int> > SetFieldsStatusPairs;
81 std::vector< std::pair<std::string,int> > TimesFlagsStatusPairs;
82 vtkMEDReader *_master;
85 void PropertyKeeper::assignPropertiesIfNeeded()
87 if(!this->SetFieldsStatusPairs.empty())
89 for(std::vector< std::pair<std::string,int> >::const_iterator it=this->SetFieldsStatusPairs.begin();it!=this->SetFieldsStatusPairs.end();it++)
90 _master->SetFieldsStatus((*it).first.c_str(),(*it).second);
91 this->SetFieldsStatusPairs.clear();
93 if(!this->TimesFlagsStatusPairs.empty())
95 for(std::vector< std::pair<std::string,int> >::const_iterator it=this->TimesFlagsStatusPairs.begin();it!=this->TimesFlagsStatusPairs.end();it++)
96 _master->SetTimesFlagsStatus((*it).first.c_str(),(*it).second);
97 this->TimesFlagsStatusPairs.clear();
99 if(this->IsGVActivated)
101 _master->GenerateVectors(this->GVValue);
102 this->IsGVActivated=false;
104 if(this->IsCMActivated)
106 _master->ChangeMode(this->CMValue);
107 this->IsCMActivated=false;
111 void PropertyKeeper::pushFieldStatusEntry(const char* name, int status)
114 for(std::vector< std::pair<std::string,int> >::const_iterator it=this->SetFieldsStatusPairs.begin();it!=this->SetFieldsStatusPairs.end() && !found;it++)
115 found=(*it).first==name;
117 this->SetFieldsStatusPairs.push_back(std::pair<std::string,int>(name,status));
120 void PropertyKeeper::pushTimesFlagsStatusEntry(const char* name, int status)
123 for(std::vector< std::pair<std::string,int> >::const_iterator it=this->TimesFlagsStatusPairs.begin();it!=this->TimesFlagsStatusPairs.end() && !found;it++)
124 found=(*it).first==name;
126 this->TimesFlagsStatusPairs.push_back(std::pair<std::string,int>(name,status));
129 void PropertyKeeper::pushGenerateVectorsValue(int value)
131 this->IsGVActivated=true;
135 void PropertyKeeper::pushChangeModeValue(int value)
137 this->IsCMActivated=true;
141 bool PropertyKeeper::arePropertiesOnTreeToSetAfter() const
143 return !SetFieldsStatusPairs.empty();
146 class vtkMEDReader::vtkMEDReaderInternal
150 vtkMEDReaderInternal(vtkMEDReader *master):TK(0),IsMEDOrSauv(true),IsStdOrMode(false),GenerateVect(false),SIL(0),LastLev0(-1),FirstCall0(2),PK(master)
162 ~vtkMEDReaderInternal()
168 MEDFileFieldRepresentationTree Tree;
170 std::string FileName;
171 //when true the file is MED file. when false it is a Sauv file
173 //when false -> std, true -> mode. By default std (false).
175 //when false -> do nothing. When true cut off or extend to nbOfCompo=3 vector arrays.
177 std::string DftMeshName;
178 // Store the vtkMutableDirectedGraph that represents links between family, groups and cell types
179 vtkMutableDirectedGraph* SIL;
180 // store the lev0 id in Tree corresponding to the TIME_STEPS in the pipeline.
182 // The property keeper is usable only in pvsm mode.
185 unsigned char FirstCall0;
188 vtkStandardNewMacro(vtkMEDReader);
190 vtkMEDReader::vtkMEDReader():Internal(new vtkMEDReaderInternal(this))
192 this->SetNumberOfInputPorts(0);
193 this->SetNumberOfOutputPorts(1);
196 vtkMEDReader::~vtkMEDReader()
198 delete this->Internal;
201 void vtkMEDReader::Reload(int a)
205 std::cerr << "vtkMEDReader::Reload" << a << std::endl;
206 std::string fName((const char *)this->GetFileName());
207 delete this->Internal;
208 this->Internal=new vtkMEDReaderInternal(this);
209 this->SetFileName(fName.c_str());
212 void vtkMEDReader::GenerateVectors(int val)
214 if(this->Internal->FileName.empty())
216 this->Internal->PK.pushGenerateVectorsValue(val);
219 //not pvsm mode (general case)
220 bool val2((bool)val);
221 if(val2!=this->Internal->GenerateVect)
223 this->Internal->GenerateVect=val2;
228 void vtkMEDReader::ChangeMode(int newMode)
230 if(this->Internal->FileName.empty())
232 this->Internal->PK.pushChangeModeValue(newMode);
235 //not pvsm mode (general case)
236 this->Internal->IsStdOrMode=newMode!=0;
237 //std::cerr << "vtkMEDReader::ChangeMode : " << this->Internal->IsStdOrMode << std::endl;
241 const char *vtkMEDReader::GetSeparator()
243 return MEDFileFieldRepresentationLeavesArrays::ZE_SEP;
246 void vtkMEDReader::SetFileName(const char *fname)
250 this->Internal->FileName=fname;
251 std::size_t pos(this->Internal->FileName.find_last_of('.'));
252 if(pos!=std::string::npos)
254 std::string ext(this->Internal->FileName.substr(pos));
255 if(ext.find("sauv")!=std::string::npos)
256 this->Internal->IsMEDOrSauv=false;
258 if(this->Internal->Tree.getNumberOfLeavesArrays()==0)
260 int iPart(-1),nbOfParts(-1);
261 #ifdef MEDREADER_USE_MPI
262 MPI_Comm_rank(MPI_COMM_WORLD,&iPart);
263 MPI_Comm_size(MPI_COMM_WORLD,&nbOfParts);
265 this->Internal->Tree.loadMainStructureOfFile(this->Internal->FileName.c_str(),this->Internal->IsMEDOrSauv,iPart,nbOfParts);
266 if(!this->Internal->PK.arePropertiesOnTreeToSetAfter())
267 this->Internal->Tree.activateTheFirst();//This line manually initialize the status of server (this) with the remote client.
268 this->Internal->TK.setMaxNumberOfTimeSteps(this->Internal->Tree.getMaxNumberOfTimeSteps());
271 this->Internal->PK.assignPropertiesIfNeeded();
273 catch(INTERP_KERNEL::Exception& e)
275 delete this->Internal;
277 std::ostringstream oss;
278 oss << "Exception has been thrown in vtkMEDReader::SetFileName : " << e.what() << std::endl;
279 if(this->HasObserver("ErrorEvent") )
280 this->InvokeEvent("ErrorEvent",const_cast<char *>(oss.str().c_str()));
282 vtkOutputWindowDisplayErrorText(const_cast<char *>(oss.str().c_str()));
283 vtkObject::BreakOnError();
287 char *vtkMEDReader::GetFileName()
289 return const_cast<char *>(this->Internal->FileName.c_str());
292 int vtkMEDReader::RequestInformation(vtkInformation *request, vtkInformationVector **inputVector, vtkInformationVector *outputVector)
294 //std::cerr << "########################################## RequestInformation ##########################################" << std::endl;
299 vtkInformation *outInfo(outputVector->GetInformationObject(0));
300 outInfo->Set(vtkDataObject::DATA_TYPE_NAME(),"vtkMultiBlockDataSet");
301 this->UpdateSIL(outInfo);
304 this->PublishTimeStepsIfNeeded(outInfo,dummy);
306 catch(INTERP_KERNEL::Exception& e)
308 std::cerr << "Exception has been thrown in vtkMEDReader::RequestInformation : " << e.what() << std::endl;
314 int vtkMEDReader::RequestData(vtkInformation *request, vtkInformationVector **inputVector, vtkInformationVector *outputVector)
316 //std::cerr << "########################################## RequestData ##########################################";
321 vtkInformation *outInfo(outputVector->GetInformationObject(0));
322 vtkMultiBlockDataSet *output(vtkMultiBlockDataSet::SafeDownCast(outInfo->Get(vtkDataObject::DATA_OBJECT())));
323 bool isUpdated(false);
325 if(outInfo->Has(vtkStreamingDemandDrivenPipeline::UPDATE_TIME_STEP()))
326 reqTS=outInfo->Get(vtkStreamingDemandDrivenPipeline::UPDATE_TIME_STEP());
327 //std::cerr << reqTS << std::endl;
328 this->FillMultiBlockDataSetInstance(output,reqTS);
329 output->GetInformation()->Set(vtkDataObject::DATA_TIME_STEP(),reqTS);
330 this->UpdateSIL(outInfo);
331 //this->UpdateProgress((float) progress/((float) maxprogress-1));
333 catch(INTERP_KERNEL::Exception& e)
335 std::cerr << "Exception has been thrown in vtkMEDReader::RequestInformation : " << e.what() << std::endl;
341 void vtkMEDReader::SetFieldsStatus(const char* name, int status)
343 //std::cerr << "vtkMEDReader::SetFieldsStatus(" << name << "," << status << ") called !" << std::endl;
344 if(this->Internal->FileName.empty())
346 this->Internal->PK.pushFieldStatusEntry(name,status);
349 //not pvsm mode (general case)
352 this->Internal->Tree.changeStatusOfAndUpdateToHaveCoherentVTKDataSet(this->Internal->Tree.getIdHavingZeName(name),status);
353 if(std::string(name)==GetFieldsTreeArrayName(GetNumberOfFieldsTreeArrays()-1))
354 if(!this->Internal->PluginStart0())
357 catch(INTERP_KERNEL::Exception& e)
359 if(!this->Internal->FileName.empty())
361 std::cerr << "vtkMEDReader::SetFieldsStatus error : " << e.what() << " *** WITH STATUS=" << status << endl;
367 int vtkMEDReader::GetNumberOfFieldsTreeArrays()
371 int ret(this->Internal->Tree.getNumberOfLeavesArrays());
372 //std::cerr << "vtkMEDReader::GetNumberOfFieldsTreeArrays called ! " << ret << std::endl;
376 const char *vtkMEDReader::GetFieldsTreeArrayName(int index)
378 return this->Internal->Tree.getNameOfC(index);
379 //std::cerr << "vtkMEDReader::GetFieldsTreeArrayName(" << index << ") called ! " << ret << std::endl;
382 int vtkMEDReader::GetFieldsTreeArrayStatus(const char *name)
384 int zeId(this->Internal->Tree.getIdHavingZeName(name));
385 int ret(this->Internal->Tree.getStatusOf(zeId));
389 void vtkMEDReader::SetTimesFlagsStatus(const char *name, int status)
391 if(this->Internal->FileName.empty())
393 this->Internal->PK.pushTimesFlagsStatusEntry(name,status);
396 //not pvsm mode (general case)
398 std::istringstream iss(name); iss >> pos;
399 this->Internal->TK.getTimesFlagArray()[pos].first=(bool)status;
400 if(pos==this->Internal->TK.getTimesFlagArray().size()-1)
401 if(!this->Internal->PluginStart0())
404 //this->Internal->TK.printSelf(std::cerr);
408 int vtkMEDReader::GetNumberOfTimesFlagsArrays()
412 return (int)this->Internal->TK.getTimesFlagArray().size();
415 const char *vtkMEDReader::GetTimesFlagsArrayName(int index)
417 return this->Internal->TK.getTimesFlagArray()[index].second.c_str();
420 int vtkMEDReader::GetTimesFlagsArrayStatus(const char *name)
423 std::istringstream iss(name); iss >> pos;
424 return (int)this->Internal->TK.getTimesFlagArray()[pos].first;
427 void vtkMEDReader::UpdateSIL(vtkInformation *info)
431 vtkMutableDirectedGraph *sil(vtkMutableDirectedGraph::New());
432 std::string meshName(this->BuildSIL(sil));
433 if(meshName!=this->Internal->DftMeshName)
435 if(this->Internal->SIL)
436 this->Internal->SIL->Delete();
437 this->Internal->SIL=sil;
438 this->Internal->DftMeshName=meshName;
439 info->Set(vtkDataObject::SIL(),this->Internal->SIL);
440 //request->AppendUnique(vtkExecutive::KEYS_TO_COPY(),vtkDataObject::SIL());
449 * The returned string is the name of the mesh activated which groups and families are in \a sil.
451 std::string vtkMEDReader::BuildSIL(vtkMutableDirectedGraph* sil)
454 vtkSmartPointer<vtkVariantArray> childEdge(vtkSmartPointer<vtkVariantArray>::New());
455 childEdge->InsertNextValue(0);
456 vtkSmartPointer<vtkVariantArray> crossEdge(vtkSmartPointer<vtkVariantArray>::New());
457 crossEdge->InsertNextValue(1);
458 // CrossEdge is an edge linking hierarchies.
459 vtkUnsignedCharArray* crossEdgesArray=vtkUnsignedCharArray::New();
460 crossEdgesArray->SetName("CrossEdges");
461 sil->GetEdgeData()->AddArray(crossEdgesArray);
462 crossEdgesArray->Delete();
463 std::vector<std::string> names;
464 // Now build the hierarchy.
465 vtkIdType rootId=sil->AddVertex();
466 names.push_back("SIL");
467 // Add global fields root
468 vtkIdType fieldsRoot(sil->AddChild(rootId,childEdge));
469 names.push_back("FieldsStatusTree");
470 this->Internal->Tree.feedSIL(sil,fieldsRoot,childEdge,names);
471 vtkIdType meshesFamsGrpsRoot(sil->AddChild(rootId,childEdge));
472 names.push_back("MeshesFamsGrps");
473 std::string dftMeshName(this->Internal->Tree.feedSILForFamsAndGrps(sil,meshesFamsGrpsRoot,childEdge,names));
474 // This array is used to assign names to nodes.
475 vtkStringArray *namesArray(vtkStringArray::New());
476 namesArray->SetName("Names");
477 namesArray->SetNumberOfTuples(sil->GetNumberOfVertices());
478 sil->GetVertexData()->AddArray(namesArray);
479 namesArray->Delete();
480 std::vector<std::string>::const_iterator iter;
482 for(cc=0, iter=names.begin(); iter!=names.end(); ++iter, ++cc)
483 namesArray->SetValue(cc,(*iter).c_str());
487 double vtkMEDReader::PublishTimeStepsIfNeeded(vtkInformation *outInfo, bool& isUpdated)
490 std::vector<double> tsteps;
491 if(!this->Internal->IsStdOrMode)
492 tsteps=this->Internal->Tree.getTimeSteps(lev0,this->Internal->TK);
494 { tsteps.resize(1); tsteps[0]=0.; }
496 if(lev0!=this->Internal->LastLev0)
500 timeRange[0]=tsteps.front();
501 timeRange[1]=tsteps.back();
502 outInfo->Set(vtkStreamingDemandDrivenPipeline::TIME_STEPS(),&tsteps[0],tsteps.size());
503 outInfo->Set(vtkStreamingDemandDrivenPipeline::TIME_RANGE(),timeRange,2);
504 this->Internal->LastLev0=lev0;
506 return tsteps.front();
509 void vtkMEDReader::FillMultiBlockDataSetInstance(vtkMultiBlockDataSet *output, double reqTS)
511 std::string meshName;
512 vtkDataSet *ret(this->Internal->Tree.buildVTKInstance(this->Internal->IsStdOrMode,reqTS,meshName,this->Internal->TK));
513 if(this->Internal->GenerateVect)
515 vtkGenerateVectors::Operate(ret->GetPointData());
516 vtkGenerateVectors::Operate(ret->GetCellData());
517 vtkGenerateVectors::Operate(ret->GetFieldData());
519 output->SetBlock(0,ret);
523 void vtkMEDReader::PrintSelf(ostream& os, vtkIndent indent)
525 this->Superclass::PrintSelf(os, indent);