Salome HOME
83efbb6616bbed9f7d86c28b9190253471b2b215
[tools/medcoupling.git] / src / INTERP_KERNELTest / MEDMeshMaker.cxx
1 // Copyright (C) 2007-2013  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 "MEDMeshMaker.hxx"
21
22 #include "MEDMEM_Mesh.hxx"
23 #include "MEDMEM_Meshing.hxx"
24
25 MEDMEM::MESH* MEDMeshMaker(int dim, int nbedge, MED_EN::medGeometryElement type)
26 {
27   MEDMEM::MESHING* mesh=new MEDMEM::MESHING();
28   int nbnodes;
29   int nbelems;
30   switch (dim)
31     {
32     case 2: 
33       nbnodes=(nbedge+1)*(nbedge+1);
34       if(type==MED_EN::MED_QUAD4)
35         nbelems=(nbedge*nbedge);
36       else
37         throw MEDMEM::MEDEXCEPTION("MEDMeshMaker: type not impletmented");
38       break;
39     case 3:
40       nbnodes=(nbedge+1)*(nbedge+1)*(nbedge+1);
41       if (type==MED_EN::MED_HEXA8)
42         nbelems= nbedge*nbedge*nbedge;
43       else
44         throw MEDMEM::MEDEXCEPTION("MEDMeshMaker: type not impletmented");
45       break;
46     }
47   double* coords = new double[dim*nbnodes];
48   int nz;
49   if (dim==2) nz =1; else nz=nbedge+1;
50   {
51     for (int ix=0; ix < nbedge+1; ix++)
52       for (int iy=0; iy<nbedge+1; iy++)
53         for (int iz=0; iz<nz;iz++)
54           {
55             int inode=(ix*(nbedge+1)*nz+iy*nz+iz);
56             coords[inode*dim]=double(ix)/double(nbedge);
57             coords[inode*dim+1]=double(iy)/double(nbedge);
58             if (dim==3)
59               coords[inode*dim+2]=double(iz)/double(nbedge);
60           }
61   }
62   mesh->setCoordinates(dim, nbnodes,coords,"CARTESIAN",MED_EN::MED_FULL_INTERLACE);
63   delete [] coords;
64   mesh->setNumberOfTypes(1,MED_EN::MED_CELL);
65   mesh->setTypes(&type,MED_EN::MED_CELL);
66   mesh->setNumberOfElements(&nbelems,MED_EN::MED_CELL);
67   
68   int* conn = new int [nbelems*(type%100)];
69   if (dim==2)
70     {
71       for (int ix=0; ix<nbedge; ix++)
72         for (int iy=0; iy<nbedge; iy++)
73           {
74             int ielem=(ix*nbedge+iy);
75             conn [ielem*4]=ix*(nbedge+1)+iy+1;
76             conn [ielem*4+1]=ix*(nbedge+1)+iy+1+1;
77             conn [ielem*4+2]=(ix+1)*(nbedge+1)+iy+1+1;
78             conn [ielem*4+3]=(ix+1)*(nbedge+1)+iy+1;                               
79           }
80     }
81   if (dim==3)
82     {
83       for (int ix=0; ix<nbedge; ix++)
84         for (int iy=0; iy<nbedge; iy++)
85           for (int iz=0; iz<nbedge; iz++)
86             {
87               int ielem=(ix*nbedge*nbedge+iy*nbedge+iz);
88               conn [ielem*8]=ix*(nbedge+1)*(nbedge+1)+iy*(nbedge+1)+iz+1;
89               conn [ielem*8+1]=(ix+1)*(nbedge+1)*(nbedge+1)+iy*(nbedge+1)+iz+1;
90               conn [ielem*8+2]=(ix+1)*(nbedge+1)*(nbedge+1)+(iy+1)*(nbedge+1)+iz+1;
91               conn [ielem*8+3]=ix*(nbedge+1)*(nbedge+1)+(iy+1)*(nbedge+1)+iz+1;
92               conn [ielem*8+4]=ix*(nbedge+1)*(nbedge+1)+iy*(nbedge+1)+iz+1+1;
93               conn [ielem*8+5]=(ix+1)*(nbedge+1)*(nbedge+1)+iy*(nbedge+1)+iz+1+1;
94               conn [ielem*8+6]=(ix+1)*(nbedge+1)*(nbedge+1)+(iy+1)*(nbedge+1)+iz+1+1;
95               conn [ielem*8+7]=ix*(nbedge+1)*(nbedge+1)+(iy+1)*(nbedge+1)+iz+1+1;
96             }
97     }
98   mesh->setConnectivity(MED_EN::MED_CELL,type,conn);
99   delete [] conn;
100   return mesh;
101 }