Salome HOME
Copyright update 2020
[tools/medcoupling.git] / src / ParaMEDMEMTest / ParaMEDMEMTest_BlockTopology.cxx
1 // Copyright (C) 2007-2020  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 "ParaMEDMEMTest.hxx"
21 #include <cppunit/TestAssert.h>
22
23 #include "InterpolationUtils.hxx"
24 #include "CommInterface.hxx"
25 #include "ProcessorGroup.hxx"
26 #include "MPIProcessorGroup.hxx"
27 #include "Topology.hxx"
28 #include "BlockTopology.hxx"
29
30 #include <string>
31
32 // use this define to enable lines, execution of which leads to Segmentation Fault
33 #define ENABLE_FAULTS
34
35 // use this define to enable CPPUNIT asserts and fails, showing bugs
36 #define ENABLE_FORCED_FAILURES
37
38
39 using namespace std;
40 using namespace MEDCoupling;
41  
42 /*
43  * Check methods defined in BlockTopology.hxx
44  *
45   BlockTopology(){};
46   BlockTopology(const ProcessorGroup& group, const MEDMEM::GRID& grid); 
47   BlockTopology(const BlockTopology& geom_topo, const ComponentTopology& comp_topo);
48   (+) BlockTopology(const ProcessorGroup& group, int nb_elem);
49   virtual ~BlockTopology();
50   (+) inline int getNbElements()const;
51   (+) inline int getNbLocalElements() const;
52   const ProcessorGroup* getProcGroup()const {return _proc_group;};
53   (+) inline std::pair<int,int> globalToLocal (const int) const ;
54   (+) inline int localToGlobal (const std::pair<int,int>) const;
55   (+) std::vector<std::pair<int,int> > getLocalArrayMinMax() const ;
56   (+) int getDimension() const {return _dimension;};
57   (+) void serialize(int* & serializer, int& size) const ;
58   (+) void unserialize(const int* serializer, const CommInterface& comm_interface);
59   
60  */
61  
62 void ParaMEDMEMTest::testBlockTopology_constructor()
63 {
64   //test constructor
65   int size;
66   MPI_Comm_size(MPI_COMM_WORLD,&size);
67   int rank;
68   MPI_Comm_rank(MPI_COMM_WORLD,&rank);
69   CommInterface interface;
70   MPIProcessorGroup group(interface);
71   BlockTopology blocktopo(group,1);
72   CPPUNIT_ASSERT_EQUAL(ToIdType(1),blocktopo.getNbLocalElements());
73   CPPUNIT_ASSERT_EQUAL(ToIdType(size),blocktopo.getNbElements());
74   CPPUNIT_ASSERT_EQUAL(1,blocktopo.getDimension());
75   
76   //checking access methods
77   BlockTopology blocktopo2(group,2);
78   std::pair<int,int> local= blocktopo2.globalToLocal(0);
79   CPPUNIT_ASSERT_EQUAL(local.first,0);
80   CPPUNIT_ASSERT_EQUAL(local.second,0);
81   int global=blocktopo2.localToGlobal(local);
82   CPPUNIT_ASSERT_EQUAL(global,0);
83   
84   local = blocktopo2.globalToLocal(1);
85   CPPUNIT_ASSERT_EQUAL(local.first,0);
86   CPPUNIT_ASSERT_EQUAL(local.second,1);
87   global=blocktopo2.localToGlobal(local);
88   CPPUNIT_ASSERT_EQUAL(global,1);
89   
90   local = blocktopo2.globalToLocal(2*size-1);
91   CPPUNIT_ASSERT_EQUAL(local.first,size-1);
92   CPPUNIT_ASSERT_EQUAL(local.second,1);
93   global=blocktopo2.localToGlobal(local);
94   CPPUNIT_ASSERT_EQUAL(global,2*size-1);
95
96   std::vector<std::pair<int,mcIdType> > bounds = blocktopo2.getLocalArrayMinMax();
97   int vecsize = bounds.size();
98   CPPUNIT_ASSERT_EQUAL(1,vecsize);
99   CPPUNIT_ASSERT_EQUAL(2*rank, (bounds[0]).first);
100   CPPUNIT_ASSERT_EQUAL(ToIdType(2*rank+2), (bounds[0]).second);
101  }
102  
103 void ParaMEDMEMTest::testBlockTopology_serialize()
104 {
105
106   int size;
107   MPI_Comm_size(MPI_COMM_WORLD,&size);
108   int rank;
109   MPI_Comm_rank(MPI_COMM_WORLD,&rank);
110   CommInterface interface;
111   MPIProcessorGroup group(interface);
112   BlockTopology blocktopo(group,3);
113
114 //testing the serialization process that is used to transfer a
115 //block topology via a MPI_Send/Recv comm  
116   BlockTopology blocktopo_recv;
117   mcIdType* serializer;
118   mcIdType sersize;
119   blocktopo.serialize(serializer,sersize);
120   blocktopo_recv.unserialize(serializer,interface);
121   CPPUNIT_ASSERT_EQUAL(blocktopo.getNbElements(),blocktopo_recv.getNbElements());
122   delete [] serializer;
123 }