Salome HOME
Win32: Fix error in Debug mode
[tools/medcoupling.git] / src / ParaMEDMEM / ICoCoMEDField.cxx
1 // Copyright (C) 2007-2014  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 "ICoCoMEDField.hxx"
21 #include "ICoCoTrioField.hxx"
22 #include "ProcessorGroup.hxx"
23 #include "MEDCouplingUMesh.hxx"
24 #include "MEDCouplingFieldDouble.hxx"
25 #include "NormalizedUnstructuredMesh.hxx"
26
27 namespace ICoCo
28 {
29
30   /*! Constructor directly attaching a MEDCouplingUMesh and a MEDCouplingFieldDouble
31     the object does not take the control the objects pointed by 
32     \a mesh and \a field.
33   */
34     
35   MEDField::MEDField(ParaMEDMEM::MEDCouplingUMesh* mesh, ParaMEDMEM::MEDCouplingFieldDouble* field): 
36     _mesh(mesh),
37     _field(field)
38   {
39     if(_mesh)
40       _mesh->incrRef();
41     if(_field)
42       _field->incrRef();
43   }
44   
45   MEDField::MEDField(TrioField& triofield)
46   {
47     _mesh = ParaMEDMEM::MEDCouplingUMesh::New();
48     _mesh->setMeshDimension(triofield._space_dim);
49     ParaMEDMEM::DataArrayDouble *myCoords=ParaMEDMEM::DataArrayDouble::New();
50     myCoords->alloc(triofield._nbnodes,triofield._space_dim);
51     _mesh->setCoords(myCoords);
52     myCoords->decrRef();
53     double *ptr=myCoords->getPointer();
54     std::copy(triofield._coords,triofield._coords+triofield._space_dim*triofield._nbnodes,ptr);
55     _mesh->allocateCells(triofield._nb_elems);
56     INTERP_KERNEL::NormalizedCellType elemtype;
57     switch (triofield._mesh_dim)
58       {
59       case 1:
60         switch (triofield._nodes_per_elem)
61           {
62           case 2:
63             elemtype=INTERP_KERNEL::NORM_SEG2;
64             break;
65           default:
66             throw INTERP_KERNEL::Exception("incompatible Trio field - wrong nb of nodes per elem");
67           }
68       case 2:
69         switch (triofield._nodes_per_elem)
70           {
71           case 3:
72             elemtype=INTERP_KERNEL::NORM_TRI3;
73             break;
74           case 4 : 
75             elemtype=INTERP_KERNEL::NORM_QUAD4;
76             break;
77           default:
78             throw INTERP_KERNEL::Exception("incompatible Trio field - wrong nb of nodes per elem");
79           }
80         break;
81       case 3:
82         switch (triofield._nodes_per_elem)
83           {
84           case 4:
85             elemtype=INTERP_KERNEL::NORM_TETRA4;
86             break;
87           case 8 : 
88             elemtype=INTERP_KERNEL::NORM_HEXA8;
89             break;
90           default:
91             throw INTERP_KERNEL::Exception("incompatible Trio field - wrong nb of nodes per elem");
92           }
93         break;
94       default:
95         throw INTERP_KERNEL::Exception("incompatible Trio field - wrong mesh dimension");
96       }
97     //creating a connectivity table that complies to MED (1 indexing)
98     //and passing it to _mesh
99     int* conn=new int[triofield._nodes_per_elem];
100     _mesh->setMeshDimension(triofield._mesh_dim);
101     for (int i=0; i<triofield._nb_elems;i++)
102       {
103         for(int j=0;j<triofield._nodes_per_elem;j++)
104           {
105             conn[j]=(triofield._connectivity)[i*triofield._nodes_per_elem+j];
106           }
107         _mesh->insertNextCell(elemtype,triofield._nodes_per_elem,conn);
108       }
109     delete[] conn;
110     
111     _mesh->finishInsertingCells();
112     
113     //field on the sending end
114     int nb_case=triofield.nb_values();
115     if (triofield._type==0)
116       {
117         _field =  ParaMEDMEM::MEDCouplingFieldDouble::New(ParaMEDMEM::ON_CELLS,ParaMEDMEM::ONE_TIME);
118       }
119     else
120       {
121         _field =  ParaMEDMEM::MEDCouplingFieldDouble::New(ParaMEDMEM::ON_NODES,ParaMEDMEM::ONE_TIME ); 
122       }
123     _field->setMesh(_mesh);
124     _field->setNature(ParaMEDMEM::ConservativeVolumic);
125     ParaMEDMEM::DataArrayDouble *fieldArr=ParaMEDMEM::DataArrayDouble::New();
126     fieldArr->alloc(_field->getNumberOfTuples(),triofield._nb_field_components);
127     _field->setName(triofield.getName().c_str());
128     std::string meshName("SupportOf_"); meshName+=_field->getName();
129     _mesh->setName(meshName.c_str());
130     _field->setTime(triofield._time1,0,triofield._itnumber);
131     if (triofield._field!=0)
132       {
133         for (int i =0; i<nb_case; i++)
134           for (int j=0; j<triofield._nb_field_components; j++)
135             {
136               fieldArr->setIJ(i,j,triofield._field[i*triofield._nb_field_components+j]);
137             }
138       }
139     //field on the receiving end
140     else
141       {
142         // the trio field points to the pointer inside the MED field
143         triofield._field=const_cast<double*> (fieldArr->getPointer());
144         for (int i=0; i<triofield._nb_field_components*nb_case;i++)
145           triofield._field[i]=0.0;
146       }
147     _field->setArray(fieldArr);
148     fieldArr->decrRef();
149   }
150
151   MEDField::~MEDField()
152   {
153     if(_field)
154       _field->decrRef();
155     if(_mesh)
156       _mesh->decrRef();
157   }
158 }