Salome HOME
Merge from V6_main_20120808 08Aug12
[modules/paravis.git] / src / Plugins / ParaMEDCorba / vtkParaMEDCorbaSource.cxx
1 // Copyright (C) 2010-2012  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.
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 "vtkstd/string"
39 #include "vtksys/ios/fstream"
40 #include "vtkstd/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   vtkstd::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       myInfo->Set(vtkStreamingDemandDrivenPipeline::MAXIMUM_NUMBER_OF_PIECES(),this->TotalNumberOfPieces);
182     }
183   return 1;
184 }
185
186 int vtkParaMEDCorbaSource::RequestData(vtkInformation* request, vtkInformationVector** inInfo, vtkInformationVector* outputVector)
187 {
188   vtkInformation *outInfo=outputVector->GetInformationObject(0);
189   //
190   this->UpdatePiece = this->GetOutputDataObject(0)->GetUpdatePiece();//emulation vtkUnstructuredGrid::GetUpdateExtent(int&,int&,int&)
191   this->NumberOfPieces = this->GetOutputDataObject(0)->GetUpdateNumberOfPieces();//emulation vtkUnstructuredGrid::GetUpdateExtent(int&,int&,int&)
192   this->GhostLevel = this->GetOutputDataObject(0)->GetUpdateGhostLevel();//emulation vtkUnstructuredGrid::GetUpdateExtent(int&,int&,int&)
193   this->StartPiece=((this->UpdatePiece*this->TotalNumberOfPieces)/this->NumberOfPieces);
194   this->EndPiece=(((this->UpdatePiece+1)*this->TotalNumberOfPieces)/this->NumberOfPieces);
195   vtkMultiBlockDataSet *ret0=vtkMultiBlockDataSet::SafeDownCast(outInfo->Get(vtkDataObject::DATA_OBJECT()));
196   double *reqTS = 0;
197   if(outInfo->Has(vtkStreamingDemandDrivenPipeline::UPDATE_TIME_STEPS()))
198     reqTS = outInfo->Get(vtkStreamingDemandDrivenPipeline::UPDATE_TIME_STEPS());
199   //Client request on ORB.
200   CORBA::ORB_var *OrbC=(CORBA::ORB_var *)this->Orb;
201   CORBA::Object_var obj=(*OrbC)->string_to_object(&IOR[0]);
202   //
203   Engines::MPIObject_var objPara=Engines::MPIObject::_narrow(obj);
204   if(CORBA::is_nil(objPara))
205     {//sequential
206        SALOME_MED::MEDCouplingMeshCorbaInterface_var meshPtr=SALOME_MED::MEDCouplingMeshCorbaInterface::_narrow(obj);
207        if(!CORBA::is_nil(meshPtr))
208          {
209            bool dummy;//bug VTK
210            vtkDataSet *ret=ParaMEDMEM2VTK::BuildFromMEDCouplingMeshInstance(meshPtr,dummy);//bug VTK
211            if(!ret)
212              return 0;
213            ret0->SetBlock(0,ret);
214            ret->Delete();
215            return 1;
216          }
217        SALOME_MED::MEDCouplingFieldDoubleCorbaInterface_var fieldPtr=SALOME_MED::MEDCouplingFieldDoubleCorbaInterface::_narrow(obj);
218        if(!CORBA::is_nil(fieldPtr))
219          {
220            std::vector<double> ret2;
221            vtkDataSet *ret=ParaMEDMEM2VTK::BuildFullyFilledFromMEDCouplingFieldDoubleInstance(fieldPtr,ret2);
222            if(!ret)
223              {
224                vtkErrorMacro("On single field CORBA fetching an error occurs !");
225                return 0;
226              }
227            ret0->SetBlock(0,ret);
228            ret->Delete();
229            //
230            double timeRange[2];
231            timeRange[0]=ret2[0];
232            timeRange[1]=ret2[0];
233            outInfo->Set(vtkStreamingDemandDrivenPipeline::TIME_STEPS(),&ret2[0],1);
234            outInfo->Set(vtkStreamingDemandDrivenPipeline::TIME_RANGE(),timeRange,2);
235            ret0->GetInformation()->Set(vtkDataObject::DATA_TIME_STEPS(), &ret2[0], 1);
236            return 1;
237          }
238        SALOME_MED::MEDCouplingMultiFieldsCorbaInterface_var multiPtr=SALOME_MED::MEDCouplingMultiFieldsCorbaInterface::_narrow(obj);
239        if(!CORBA::is_nil(multiPtr))
240          {
241            vtkDataSet *ret=mfieldsFetcher->buildDataSetOnTime(reqTS[0]);
242            if(!ret)
243              {
244                vtkErrorMacro("On multi fields CORBA fetching an error occurs !");
245                return 0;
246              }
247            ret0->SetBlock(0,ret);
248            ret->Delete();
249            ret0->GetInformation()->Set(vtkDataObject::DATA_TIME_STEPS(),reqTS, 1);
250            return 1;
251          }
252        vtkErrorMacro("Unrecognized sequential CORBA reference !");
253        return 0;
254     }
255   else
256     {
257       SALOME_MED::ParaMEDCouplingFieldDoubleCorbaInterface_var paraFieldCorba=SALOME_MED::ParaMEDCouplingFieldDoubleCorbaInterface::_narrow(obj);
258       if(!CORBA::is_nil(paraFieldCorba))
259         {
260           ParaMEDMEM2VTK::FillMEDCouplingParaFieldDoubleInstanceFrom(paraFieldCorba,this->StartPiece,this->EndPiece,ret0);
261           return 1;
262         }
263       vtkErrorMacro("Unrecognized parallel CORBA reference !");
264       return 0;
265     }
266 }
267
268 void vtkParaMEDCorbaSource::PrintSelf(ostream& os, vtkIndent indent)
269 {
270   this->Superclass::PrintSelf( os, indent );
271   os << "Data: " << this->MyDataSet << "\n";
272 }
273