Salome HOME
add mpi part
[modules/med.git] / src / medtool / src / ParaMEDMEM / ComponentTopology.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 "ComponentTopology.hxx"
21 #include "ProcessorGroup.hxx"
22 #include "InterpolationUtils.hxx"
23
24 namespace ParaMEDMEM
25 {
26   /* Generic constructor for \a nb_comp components equally parted
27    * in \a nb_blocks blocks
28    */
29   ComponentTopology::ComponentTopology(int nb_comp, ProcessorGroup* group):_proc_group(group)
30   {
31     int nb_blocks=group->size();
32   
33     if (nb_blocks>nb_comp)
34       throw INTERP_KERNEL::Exception("ComponentTopology Number of components must be larger than number of blocks");
35
36     _component_array.resize(nb_blocks+1);
37     _component_array[0]=0;
38     for (int i=1; i<=nb_blocks; i++)
39       {
40         _component_array[i]=_component_array[i-1]+nb_comp/nb_blocks;
41         if (i<=nb_comp%nb_blocks)
42           _component_array[i]++;
43       }
44   }
45   
46   /* Generic constructor for \a nb_comp components equally parted
47    * in \a nb_blocks blocks
48    */
49   ComponentTopology::ComponentTopology(int nb_comp, int nb_blocks):_proc_group(0)
50   {
51     if (nb_blocks>nb_comp)
52       throw INTERP_KERNEL::Exception("ComponentTopology Number of components must be larger than number of blocks");
53     
54     _component_array.resize(nb_blocks+1);
55     _component_array[0]=0;
56     for (int i=1; i<=nb_blocks; i++)
57       {
58         _component_array[i]=_component_array[i-1]+nb_comp/nb_blocks;
59         if (i<=nb_comp%nb_blocks)
60           _component_array[i]++;
61       }
62   
63   }
64   
65   //!Constructor for one block of \a nb_comp components
66   ComponentTopology::ComponentTopology(int nb_comp):_proc_group(0)
67   {
68     
69     _component_array.resize(2);
70     _component_array[0]=0;
71     _component_array[1]=nb_comp;
72   
73   }
74
75   //! Constructor for one component
76   ComponentTopology::ComponentTopology():_proc_group(0)
77   {
78     _component_array.resize(2);
79     _component_array[0]=0;
80     _component_array[1]=1;
81   
82   }
83   
84   ComponentTopology::~ComponentTopology()
85   {
86   }
87
88   int ComponentTopology::nbLocalComponents() const
89   {
90     if (_proc_group==0)
91       return nbComponents();
92   
93     int nbcomp;
94     int myrank = _proc_group->myRank();
95     if (myrank!=-1)
96       nbcomp = _component_array[myrank+1]-_component_array[myrank];
97     else 
98       nbcomp=0;
99     return nbcomp;
100   }
101
102   int ComponentTopology::firstLocalComponent() const
103   {
104     if (_proc_group==0)
105       return 0;
106   
107     int icomp;
108     int myrank = _proc_group->myRank();
109     if (myrank!=-1)
110       icomp = _component_array[myrank];
111     else 
112       icomp=-1;
113     return icomp;
114   }
115 }