Salome HOME
102443ef043afad20d29b52c42d0b7505a3e93d6
[modules/med.git] / src / ParaMEDMEMTest / ParaMEDMEMTestMPI2_2.cxx
1 // Copyright (C) 2007-2015  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 <cppunit/extensions/HelperMacros.h>
21
22 #include "MPI2Connector.hxx"
23 #include "ParaMESH.hxx"
24 #include "ParaFIELD.hxx"
25 #include "MEDCouplingUMesh.hxx"
26 #include "MEDCouplingFieldDouble.hxx"
27 #include "InterpKernelDEC.hxx"
28 #include "MPIProcessorGroup.hxx"
29 #include "CommInterface.hxx"
30
31 #include <mpi.h>
32 #include <iostream>
33 #include <stdlib.h>
34
35 class MPI2ParaMEDMEMTest : public CppUnit::TestFixture
36 {
37   CPPUNIT_TEST_SUITE( MPI2ParaMEDMEMTest );
38   CPPUNIT_TEST( testBasicMPI2_1 );
39   CPPUNIT_TEST_SUITE_END();
40 public:
41   void testBasicMPI2_1();
42 };
43
44 using namespace ParaMEDMEM;
45
46 void MPI2ParaMEDMEMTest::testBasicMPI2_1()
47 {
48   int lsize, lrank, gsize, grank;
49   MPI_Comm gcom;
50   std::string service = "SERVICE";
51   std::ostringstream meshfilename, meshname;
52   ParaMEDMEM::ParaMESH *paramesh=0;
53   ParaMEDMEM::MEDCouplingUMesh* mesh;
54   ParaMEDMEM::ParaFIELD *parafield=0;
55   ParaMEDMEM::CommInterface* interface;
56   ParaMEDMEM::MPIProcessorGroup* source, *target;
57   
58   MPI_Comm_size( MPI_COMM_WORLD, &lsize );
59   MPI_Comm_rank( MPI_COMM_WORLD, &lrank );
60   if(lsize!=3)
61     {
62       CPPUNIT_ASSERT(false);
63       return;
64     }
65
66   /* Connection to remote programm */
67   MPI2Connector *mpio = new MPI2Connector;
68   gcom = mpio->remoteMPI2Connect(service);
69   
70   MPI_Comm_size( gcom, &gsize );
71   MPI_Comm_rank( gcom, &grank );
72   if(gsize!=5)
73     {
74       CPPUNIT_ASSERT(false);
75       return;
76     }
77
78   interface = new ParaMEDMEM::CommInterface;
79   source = new ParaMEDMEM::MPIProcessorGroup(*interface,0,gsize-lsize-1,gcom);
80   target = new ParaMEDMEM::MPIProcessorGroup(*interface,gsize-lsize,gsize-1,gcom);
81
82   const double targetCoordsAll[3][16]={{0.7,1.45,0.7,1.65,0.9,1.65,0.9,1.45,  1.1,1.4,1.1,1.6,1.3,1.6,1.3,1.4},
83                                        {0.7,-0.6,0.7,0.7,0.9,0.7,0.9,-0.6,  1.1,-0.7,1.1,0.6,1.3,0.6,1.3,-0.7},
84                                        {0.7,-1.55,0.7,-1.35,0.9,-1.35,0.9,-1.55,  1.1,-1.65,1.1,-1.45,1.3,-1.45,1.3,-1.65}};
85   int conn4All[8]={0,1,2,3,4,5,6,7};
86   double targetResults[3][2]={{34.,34.},{38.333333333333336,42.666666666666664},{47.,47.}};
87
88   std::ostringstream stream; stream << "targetmesh2D proc " << grank-(gsize-lsize);
89   mesh=MEDCouplingUMesh::New(stream.str().c_str(),2);
90   mesh->allocateCells(2);
91   mesh->insertNextCell(INTERP_KERNEL::NORM_QUAD4,4,conn4All);
92   mesh->insertNextCell(INTERP_KERNEL::NORM_QUAD4,4,conn4All+4);
93   mesh->finishInsertingCells();
94   DataArrayDouble *myCoords=DataArrayDouble::New();
95   myCoords->alloc(8,2);
96   const double *targetCoords=targetCoordsAll[grank-(gsize-lsize)];
97   std::copy(targetCoords,targetCoords+16,myCoords->getPointer());
98   mesh->setCoords(myCoords);
99   myCoords->decrRef();
100   paramesh=new ParaMESH (mesh,*target,"target mesh");
101   ParaMEDMEM::ComponentTopology comptopo;
102   parafield = new ParaFIELD(ON_CELLS,NO_TIME,paramesh, comptopo);
103
104   ParaMEDMEM::InterpKernelDEC dec(*source,*target);
105   parafield->getField()->setNature(ConservativeVolumic);
106
107   dec.setMethod("P0");
108   dec.attachLocalField(parafield);
109   dec.synchronize();
110   dec.setForcedRenormalization(false);
111   dec.recvData();
112   const double *res=parafield->getField()->getArray()->getConstPointer();
113   const double *expected=targetResults[grank-(gsize-lsize)];
114   CPPUNIT_ASSERT_DOUBLES_EQUAL(expected[0],res[0],1e-13);
115   CPPUNIT_ASSERT_DOUBLES_EQUAL(expected[1],res[1],1e-13);
116   /* Deconnection of remote programm */
117   mpio->remoteMPI2Disconnect(service);
118   /* clean-up */
119   delete mpio;
120   delete parafield;
121   mesh->decrRef();
122   delete paramesh;
123   delete source;
124   delete target;
125   delete interface;
126 }
127
128 CPPUNIT_TEST_SUITE_REGISTRATION( MPI2ParaMEDMEMTest );
129
130 #include "MPIMainTest.hxx"