Salome HOME
Merge from master to omu/insitu.
[modules/paravis.git] / src / Insitu / VisualizationLibrary / visu.cxx
1 #include "visu.hxx"
2 #include "MEDFileField.hxx"
3 #include "MEDCouplingUMesh.hxx"
4 #include "MEDCouplingCMesh.hxx"
5 #include "MEDCouplingCurveLinearMesh.hxx"
6 #include "MEDFileMesh.hxx"
7 #include "MEDFileFieldRepresentationTree.hxx"
8 #include <iostream>
9 #include "mpi.h" 
10
11 #include <vtkCPDataDescription.h>
12 #include <vtkCPInputDataDescription.h>
13 #include <vtkCPPythonScriptPipeline.h>
14 #include <vtkCPProcessor.h>
15 #include "vtkDataSet.h"
16 #include "vtkCellData.h"
17
18 #include <vtkSmartPointer.h>
19 #include <vtkXMLUnstructuredGridWriter.h>
20 #include <vtkUnstructuredGrid.h>
21
22 using namespace MEDCoupling;
23
24
25 void Visualization::CatalystInitialize(const std::string& script)
26 {
27
28   if(Processor == NULL)
29     {
30     Processor = vtkCPProcessor::New();
31     Processor->Initialize();
32     }
33   else
34     {
35     Processor->RemoveAllPipelines();
36     }
37   // Add in the Python script
38   vtkCPPythonScriptPipeline* pipeline = vtkCPPythonScriptPipeline::New();
39   char *c = const_cast<char*>(script.c_str());
40   pipeline->Initialize(c);
41   Processor->AddPipeline(pipeline);
42   pipeline->Delete();
43   return;
44 }
45
46 void Visualization::CatalystFinalize()
47 {
48   if(Processor)
49     {
50     Processor->Delete();
51     Processor = NULL;
52     }
53
54   return;
55 }
56
57 void Visualization::CatalystCoProcess(vtkDataSet *VTKGrid, double time,
58                                       unsigned int timeStep)
59 {
60   vtkCPDataDescription* dataDescription = vtkCPDataDescription::New();
61   // specify the simulation time and time step for Catalyst
62   dataDescription->AddInput("input");
63   dataDescription->SetTimeData(time, timeStep);
64
65   if(Processor->RequestDataDescription(dataDescription) != 0)
66     {
67     // Make a map from input to our VTK grid so that
68     // Catalyst gets the proper input dataset for the pipeline.
69     dataDescription->GetInputDescriptionByName("input")->SetGrid(VTKGrid);
70     // Call Catalyst to execute the desired pipelines.
71     Processor->CoProcess(dataDescription);
72     }
73   dataDescription->Delete();
74 }
75
76 void Visualization::ConvertToVTK(MEDCoupling::MEDCouplingFieldDouble* field, vtkDataSet *&VTKGrid)
77 //vtkDataSet * Visualization::ConvertToVTK(MEDCoupling::MEDCouplingFieldDouble* field)
78 {
79   MEDCoupling::MEDCouplingFieldDouble *f = field;
80   MEDCoupling::MCAuto<MEDFileField1TS> ff(MEDFileField1TS::New());
81   ff->setFieldNoProfileSBT(f);
82   MCAuto<MEDFileFieldMultiTS> fmts(MEDFileFieldMultiTS::New());
83   fmts->pushBackTimeStep(ff);
84   MCAuto<MEDFileFields> fs(MEDFileFields::New());
85   fs->pushField(fmts);
86   
87   MEDCouplingMesh *m(f->getMesh());
88   MCAuto<MEDFileMesh> mm;
89   if(dynamic_cast<MEDCouplingUMesh *>(m))
90     {
91       MCAuto<MEDFileUMesh> mmu(MEDFileUMesh::New()); 
92       mmu->setMeshAtLevel(0,dynamic_cast<MEDCouplingUMesh *>(m));
93       mmu->forceComputationOfParts();
94       mm=DynamicCast<MEDFileUMesh,MEDFileMesh>(mmu);
95     }
96   else if(dynamic_cast<MEDCouplingCMesh *>(m))
97     {
98       MCAuto<MEDFileCMesh> mmc(MEDFileCMesh::New()); 
99       mmc->setMesh(dynamic_cast<MEDCouplingCMesh *>(m));
100       mm=DynamicCast<MEDFileCMesh,MEDFileMesh>(mmc);
101     }
102   else if(dynamic_cast<MEDCouplingCurveLinearMesh *>(m))
103     {
104       // MCAuto<MEDFileCLMesh> mmc(MEDFileCLMesh::New()); 
105       MCAuto<MEDFileCurveLinearMesh> mmc(MEDFileCurveLinearMesh::New()); 
106       mmc->setMesh(dynamic_cast<MEDCouplingCurveLinearMesh *>(m));
107       //mm=DynamicCast<MEDCouplingCurveLinearMesh,MEDFileMesh>(mmc);
108       mm=0;
109     }
110   else
111     {
112     throw ;
113     }
114   MCAuto<MEDFileMeshes> ms(MEDFileMeshes::New());
115   ms->pushMesh(mm);
116   ms->getMeshesNames();
117   //
118   int proc_id;
119   MPI_Comm_rank(MPI_COMM_WORLD,&proc_id);
120   //
121   MEDFileFieldRepresentationTree Tree;
122   Tree.loadInMemory(fs,ms);
123   
124   Tree.changeStatusOfAndUpdateToHaveCoherentVTKDataSet(0,true);
125   Tree.changeStatusOfAndUpdateToHaveCoherentVTKDataSet(1,false);
126   Tree.activateTheFirst();//"TS0/Fluid domain/ComSup0/TempC@@][@@P0"
127   std::string meshName;
128   TimeKeeper TK(0);
129   int tmp1,tmp2;
130   double tmp3(f->getTime(tmp1,tmp2));
131   vtkDataSet *ret(Tree.buildVTKInstance(false,tmp3,meshName,TK));
132   VTKGrid = ret;
133 }
134
135 Visualization::Visualization()
136 {
137 Processor = NULL;
138 }
139
140
141 // ajouter en parametre le fichier python qui contient le pipeline
142 // enlever le const s'il gene
143 void Visualization::run(MEDCoupling::MEDCouplingFieldDouble* field, const std::string& pathPipeline)
144 {
145   int proc_id;
146   MPI_Comm_rank(MPI_COMM_WORLD,&proc_id);
147
148   vtkDataSet *VTKGrid = 0;
149   ConvertToVTK(field, VTKGrid);
150
151   const char *fileName = pathPipeline.c_str();
152   CatalystInitialize(fileName);
153   CatalystCoProcess(VTKGrid, 0.0, 0);
154   CatalystFinalize();
155   
156   return ;
157 }
158