Salome HOME
Tests again and again
[tools/medcoupling.git] / src / ParaMEDMEM / ICoCoMEDField.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 "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 2:
60         switch (triofield._nodes_per_elem)
61           {
62           case 3:
63             elemtype=INTERP_KERNEL::NORM_TRI3;
64             break;
65           case 4 : 
66             elemtype=INTERP_KERNEL::NORM_QUAD4;
67             break;
68           default:
69             throw INTERP_KERNEL::Exception("incompatible Trio field - wrong nb of nodes per elem");
70           }
71         break;
72       case 3:
73         switch (triofield._nodes_per_elem)
74           {
75           case 4:
76             elemtype=INTERP_KERNEL::NORM_TETRA4;
77             break;
78           case 8 : 
79             elemtype=INTERP_KERNEL::NORM_HEXA8;
80             break;
81           default:
82             throw INTERP_KERNEL::Exception("incompatible Trio field - wrong nb of nodes per elem");
83           }
84         break;
85       default:
86         throw INTERP_KERNEL::Exception("incompatible Trio field - wrong mesh dimension");
87       }
88     //creating a connectivity table that complies to MED (1 indexing)
89     //and passing it to _mesh
90     int* conn=new int[triofield._nodes_per_elem];
91     _mesh->setMeshDimension(triofield._mesh_dim);
92     for (int i=0; i<triofield._nb_elems;i++)
93       {
94         for(int j=0;j<triofield._nodes_per_elem;j++)
95           {
96             conn[j]=(triofield._connectivity)[i*triofield._nodes_per_elem+j];
97           }
98         _mesh->insertNextCell(elemtype,triofield._nodes_per_elem,conn);
99       }
100     delete[] conn;
101     
102     _mesh->finishInsertingCells();
103     
104     //field on the sending end
105     int nb_case=triofield.nb_values();
106     if (triofield._type==0)
107       {
108         _field =  ParaMEDMEM::MEDCouplingFieldDouble::New(ParaMEDMEM::ON_CELLS,ParaMEDMEM::ONE_TIME);
109       }
110     else
111       {
112         _field =  ParaMEDMEM::MEDCouplingFieldDouble::New(ParaMEDMEM::ON_NODES,ParaMEDMEM::ONE_TIME ); 
113       }
114     _field->setMesh(_mesh);
115     _field->setNature(ParaMEDMEM::ConservativeVolumic);
116     ParaMEDMEM::DataArrayDouble *fieldArr=ParaMEDMEM::DataArrayDouble::New();
117     fieldArr->alloc(_field->getNumberOfTuples(),triofield._nb_field_components);
118     _field->setName(triofield.getName().c_str());
119     std::string meshName("SupportOf_"); meshName+=_field->getName();
120     _mesh->setName(meshName.c_str());
121     _field->setTime(triofield._time1,0,triofield._itnumber);
122     if (triofield._field!=0)
123       {
124         for (int i =0; i<nb_case; i++)
125           for (int j=0; j<triofield._nb_field_components; j++)
126             {
127               fieldArr->setIJ(i,j,triofield._field[i*triofield._nb_field_components+j]);
128             }
129       }
130     //field on the receiving end
131     else
132       {
133         // the trio field points to the pointer inside the MED field
134         triofield._field=const_cast<double*> (fieldArr->getPointer());
135         for (int i=0; i<triofield._nb_field_components*nb_case;i++)
136           triofield._field[i]=0.0;
137       }
138     _field->setArray(fieldArr);
139     fieldArr->decrRef();
140   }
141
142   MEDField::~MEDField()
143   {
144     if(_field)
145       _field->decrRef();
146     if(_mesh)
147       _mesh->decrRef();
148   }
149 }