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