Salome HOME
Clearly identify when we execute Python code from SALOME embedded console.
[modules/paravis.git] / src / Plugins / ParaMEDCorba / vtkParaMEDCorbaSource.cxx
1 // Copyright (C) 2010-2014  CEA/DEN, EDF R&D
2 //
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.
7 //
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.
12 //
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
16 //
17 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
18 //
19
20 #include "vtkParaMEDCorbaSource.h"
21
22 #include "vtkPoints.h"
23 #include "vtkIntArray.h"
24 #include "vtkCellData.h"
25 #include "vtkCellTypes.h"
26 #include "vtkCharArray.h"
27 #include "vtkPointData.h"
28 #include "vtkDoubleArray.h"
29 #include "vtkMultiBlockDataSet.h"
30 #include "vtkUnstructuredGrid.h"
31 //
32 #include "vtkStreamingDemandDrivenPipeline.h"
33 #include "vtkGenericAttributeCollection.h"
34 #include "vtkInformationVector.h"
35 #include "vtkObjectFactory.h"
36 #include "vtkInformation.h"
37 //
38 #include "vtksys/stl/string"
39 #include "vtksys/ios/fstream"
40 #include "vtksys/stl/algorithm"
41
42 #include "VTKMEDCouplingMeshClient.hxx"
43 #include "VTKMEDCouplingFieldClient.hxx"
44 #include "VTKMEDCouplingMultiFieldsClient.hxx"
45 #include "VTKParaMEDFieldClient.hxx"
46
47 //Work with IOR.
48 #include "ParaMEDCouplingCorbaServant.hh"
49 //
50
51 vtkStandardNewMacro(vtkParaMEDCorbaSource);
52 //vtkCxxRevisionMacro(vtkParaMEDCorbaSource,"$Revision$");
53
54 void *vtkParaMEDCorbaSource::Orb=0;
55
56 vtkParaMEDCorbaSource::vtkParaMEDCorbaSource():mfieldsFetcher(0)
57 {
58   this->MyDataSet=0;
59   if(!Orb)
60   {
61     CORBA::ORB_var *OrbC=new CORBA::ORB_var;
62     int argc=0;
63     *OrbC=CORBA::ORB_init(argc,0);
64     this->Orb=OrbC;
65   }
66   this->SetNumberOfInputPorts(0);
67   this->SetNumberOfOutputPorts(1);
68 }
69
70 vtkParaMEDCorbaSource::~vtkParaMEDCorbaSource()
71 {
72   delete mfieldsFetcher;
73 }
74
75 const char *vtkParaMEDCorbaSource::GetIORCorba()
76 {
77   return &IOR[0];
78 }
79
80 void vtkParaMEDCorbaSource::SetIORCorba(char *ior)
81 {
82   if(!ior)
83     return;
84   if(ior[0]=='\0')
85     return;
86   int length=strlen(ior);
87   IOR.resize(length+1);
88   vtksys_stl::copy(ior,ior+length+1,&IOR[0]);
89   this->Modified();
90 }
91
92 void vtkParaMEDCorbaSource::SetBufferingPolicy(int pol)
93 {
94   BufferingPolicy=pol;
95 }
96
97 int vtkParaMEDCorbaSource::GetBufferingPolicy()
98 {
99   return BufferingPolicy;
100 }
101
102 //int vtkParaMEDCorbaSource::RequestUpdateExtent( vtkInformation* request, vtkInformationVector** inInfo, vtkInformationVector* outInfo )
103 //{
104 //return this->Superclass::RequestUpdateExtent(request,inInfo,outInfo);
105
106 /*vtkParaMEDCorbaDataSet* output = vtkParaMEDCorbaDataSet::SafeDownCast( info->Get( vtkDataObject::DATA_OBJECT() ) );
107   if ( ! output )
108     {
109     output = vtkParaMEDCorbaDataSet::New();
110     output->SetPipelineInformation( info );
111     output->Delete();
112     this->GetOutputPortInformation( 0 )->Set( vtkDataObject::DATA_EXTENT_TYPE(), output->GetExtentType() );
113     }*/
114
115 // return 1;
116 //}
117
118 int vtkParaMEDCorbaSource::ProcessRequest(vtkInformation* request,
119     vtkInformationVector** inputVector,
120     vtkInformationVector* outputVector)
121 {
122   // generate the data
123   if(request->Has(vtkDemandDrivenPipeline::REQUEST_DATA()))
124   {
125     return this->RequestData(request, inputVector, outputVector);
126   }
127   if(request->Has(vtkDemandDrivenPipeline::REQUEST_INFORMATION()))
128   {
129     return this->RequestInformation(request, inputVector, outputVector);
130   }
131   return this->Superclass::ProcessRequest(request, inputVector, outputVector);
132 }
133
134 int vtkParaMEDCorbaSource::FillOutputPortInformation(int vtkNotUsed(port), vtkInformation* info)
135 {
136   info->Set(vtkDataObject::DATA_TYPE_NAME(), "vtkMultiBlockDataSet");
137   return 1;
138 }
139
140 int vtkParaMEDCorbaSource::RequestInformation(vtkInformation* request, vtkInformationVector** inInfo, vtkInformationVector* outInfo)
141 {
142   vtkInformation* myInfo=outInfo->GetInformationObject(0);
143   //myInfo->Set(vtkDataObject::DATA_TYPE_NAME(),"vtkUnstructuredGrid");
144   if(!IOR.empty())
145   {
146     //myInfo->Remove(vtkDataObject::DATA_TYPE_NAME());
147     //myInfo->Remove(PORT_REQUIREMENTS_FILLED());
148     //myInfo->Set(vtkDataObject::DATA_TYPE_NAME(),"vtkUnstructuredGrid");
149     //myInfo->Set(PORT_REQUIREMENTS_FILLED(),1);
150     //vtkUnstructuredGrid *tony=vtkUnstructuredGrid::New();
151     //tony->SetInformation(myInfo);
152     //myInfo->Set(vtkDataObject::DATA_OBJECT(),tony);
153     //
154     CORBA::ORB_var *OrbC=(CORBA::ORB_var *)this->Orb;
155     CORBA::Object_var obj=(*OrbC)->string_to_object(&IOR[0]);
156     //
157     Engines::MPIObject_ptr objPara=Engines::MPIObject::_narrow(obj);
158     if(CORBA::is_nil(objPara))
159     {//sequential
160       this->TotalNumberOfPieces=1;
161       SALOME_MED::MEDCouplingMultiFieldsCorbaInterface_var multiPtr=SALOME_MED::MEDCouplingMultiFieldsCorbaInterface::_narrow(obj);
162       if(!CORBA::is_nil(multiPtr))
163       {//Request for multiFields
164         delete mfieldsFetcher;
165         mfieldsFetcher=new ParaMEDMEM2VTK::MEDCouplingMultiFieldsFetcher(BufferingPolicy,multiPtr);
166         std::vector<double> tsteps=mfieldsFetcher->getTimeStepsForPV();
167         double timeRange[2];
168         timeRange[0]=tsteps.front();
169         timeRange[1]=tsteps.back();
170         myInfo->Set(vtkStreamingDemandDrivenPipeline::TIME_STEPS(),&tsteps[0],tsteps.size());
171         myInfo->Set(vtkStreamingDemandDrivenPipeline::TIME_RANGE(),timeRange,2);
172       }
173     }
174     else
175     {
176       Engines::IORTab *iorTab=objPara->tior();
177       this->TotalNumberOfPieces=iorTab->length();
178       delete iorTab;
179       CORBA::release(objPara);
180     }
181     // This vtkInformationRequestKey is no more present in PV4.2
182     //myInfo->Set(vtkStreamingDemandDrivenPipeline::MAXIMUM_NUMBER_OF_PIECES(),this->TotalNumberOfPieces);
183   }
184   return 1;
185 }
186
187 int vtkParaMEDCorbaSource::RequestData(vtkInformation* request, vtkInformationVector** inInfo, vtkInformationVector* outputVector)
188 {
189   vtkInformation *outInfo=outputVector->GetInformationObject(0);
190   //
191   this->UpdatePiece = vtkStreamingDemandDrivenPipeline::GetUpdatePiece(outInfo);
192   this->NumberOfPieces = vtkStreamingDemandDrivenPipeline::GetUpdateNumberOfPieces(outInfo);
193   this->GhostLevel = vtkStreamingDemandDrivenPipeline::GetUpdateGhostLevel(outInfo);
194   this->StartPiece=((this->UpdatePiece*this->TotalNumberOfPieces)/this->NumberOfPieces);
195   this->EndPiece=(((this->UpdatePiece+1)*this->TotalNumberOfPieces)/this->NumberOfPieces);
196   vtkMultiBlockDataSet *ret0=vtkMultiBlockDataSet::SafeDownCast(outInfo->Get(vtkDataObject::DATA_OBJECT()));
197   double reqTS = 0;
198   if(outInfo->Has(vtkStreamingDemandDrivenPipeline::UPDATE_TIME_STEP()))
199     reqTS = outInfo->Get(vtkStreamingDemandDrivenPipeline::UPDATE_TIME_STEP());
200   //Client request on ORB.
201   CORBA::ORB_var *OrbC=(CORBA::ORB_var *)this->Orb;
202   CORBA::Object_var obj=(*OrbC)->string_to_object(&IOR[0]);
203   //
204   Engines::MPIObject_var objPara=Engines::MPIObject::_narrow(obj);
205   if(CORBA::is_nil(objPara))
206   {//sequential
207     SALOME_MED::MEDCouplingMeshCorbaInterface_var meshPtr=SALOME_MED::MEDCouplingMeshCorbaInterface::_narrow(obj);
208     if(!CORBA::is_nil(meshPtr))
209     {
210       bool dummy;//bug VTK
211       vtkDataSet *ret=ParaMEDMEM2VTK::BuildFromMEDCouplingMeshInstance(meshPtr,dummy);//bug VTK
212       if(!ret)
213         return 0;
214       ret0->SetBlock(0,ret);
215       ret->Delete();
216       return 1;
217     }
218     SALOME_MED::MEDCouplingFieldDoubleCorbaInterface_var fieldPtr=SALOME_MED::MEDCouplingFieldDoubleCorbaInterface::_narrow(obj);
219     if(!CORBA::is_nil(fieldPtr))
220     {
221       std::vector<double> ret2;
222       vtkDataSet *ret=ParaMEDMEM2VTK::BuildFullyFilledFromMEDCouplingFieldDoubleInstance(fieldPtr,ret2);
223       if(!ret)
224       {
225         vtkErrorMacro("On single field CORBA fetching an error occurs !");
226         return 0;
227       }
228       ret0->SetBlock(0,ret);
229       ret->Delete();
230       //
231       double timeRange[2];
232       timeRange[0]=ret2[0];
233       timeRange[1]=ret2[0];
234       outInfo->Set(vtkStreamingDemandDrivenPipeline::TIME_STEPS(),&ret2[0],1);
235       outInfo->Set(vtkStreamingDemandDrivenPipeline::TIME_RANGE(),timeRange,2);
236       ret0->GetInformation()->Set(vtkDataObject::DATA_TIME_STEP(),ret2[0]);
237       return 1;
238     }
239     SALOME_MED::MEDCouplingMultiFieldsCorbaInterface_var multiPtr=SALOME_MED::MEDCouplingMultiFieldsCorbaInterface::_narrow(obj);
240     if(!CORBA::is_nil(multiPtr))
241     {
242       vtkDataSet *ret=mfieldsFetcher->buildDataSetOnTime(reqTS);
243       if(!ret)
244       {
245         vtkErrorMacro("On multi fields CORBA fetching an error occurs !");
246         return 0;
247       }
248       ret0->SetBlock(0,ret);
249       ret->Delete();
250       ret0->GetInformation()->Set(vtkDataObject::DATA_TIME_STEP(),reqTS);
251       return 1;
252     }
253     vtkErrorMacro("Unrecognized sequential CORBA reference !");
254     return 0;
255   }
256   else
257   {
258     SALOME_MED::ParaMEDCouplingFieldDoubleCorbaInterface_var paraFieldCorba=SALOME_MED::ParaMEDCouplingFieldDoubleCorbaInterface::_narrow(obj);
259     if(!CORBA::is_nil(paraFieldCorba))
260     {
261       ParaMEDMEM2VTK::FillMEDCouplingParaFieldDoubleInstanceFrom(paraFieldCorba,this->StartPiece,this->EndPiece,ret0);
262       return 1;
263     }
264     vtkErrorMacro("Unrecognized parallel CORBA reference !");
265     return 0;
266   }
267 }
268
269 void vtkParaMEDCorbaSource::PrintSelf(ostream& os, vtkIndent indent)
270 {
271   this->Superclass::PrintSelf( os, indent );
272   os << "Data: " << this->MyDataSet << "\n";
273 }
274