Back to index

salome-med  6.5.0
ICoCoMEDField.cxx
Go to the documentation of this file.
00001 // Copyright (C) 2007-2012  CEA/DEN, EDF R&D
00002 //
00003 // This library is free software; you can redistribute it and/or
00004 // modify it under the terms of the GNU Lesser General Public
00005 // License as published by the Free Software Foundation; either
00006 // version 2.1 of the License.
00007 //
00008 // This library is distributed in the hope that it will be useful,
00009 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00010 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00011 // Lesser General Public License for more details.
00012 //
00013 // You should have received a copy of the GNU Lesser General Public
00014 // License along with this library; if not, write to the Free Software
00015 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
00016 //
00017 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
00018 //
00019 
00020 #include "ICoCoMEDField.hxx"
00021 #include "ICoCoTrioField.hxx"
00022 #include "ProcessorGroup.hxx"
00023 #include "MEDCouplingUMesh.hxx"
00024 #include "MEDCouplingFieldDouble.hxx"
00025 #include "NormalizedUnstructuredMesh.hxx"
00026 
00027 namespace ICoCo
00028 {
00029 
00035   MEDField::MEDField(ParaMEDMEM::MEDCouplingUMesh* mesh, ParaMEDMEM::MEDCouplingFieldDouble* field): 
00036     _mesh(mesh),
00037     _field(field)
00038   {
00039     if(_mesh)
00040       _mesh->incrRef();
00041     if(_field)
00042       _field->incrRef();
00043   }
00044   
00045   MEDField::MEDField(TrioField& triofield)
00046   {
00047     _mesh = ParaMEDMEM::MEDCouplingUMesh::New();
00048     _mesh->setMeshDimension(triofield._space_dim);
00049     ParaMEDMEM::DataArrayDouble *myCoords=ParaMEDMEM::DataArrayDouble::New();
00050     myCoords->alloc(triofield._nbnodes,triofield._space_dim);
00051     _mesh->setCoords(myCoords);
00052     myCoords->decrRef();
00053     double *ptr=myCoords->getPointer();
00054     std::copy(triofield._coords,triofield._coords+triofield._space_dim*triofield._nbnodes,ptr);
00055     _mesh->allocateCells(triofield._nb_elems);
00056     INTERP_KERNEL::NormalizedCellType elemtype;
00057     switch (triofield._mesh_dim)
00058       {
00059       case 2:
00060         switch (triofield._nodes_per_elem)
00061           {
00062           case 3:
00063             elemtype=INTERP_KERNEL::NORM_TRI3;
00064             break;
00065           case 4 : 
00066             elemtype=INTERP_KERNEL::NORM_QUAD4;
00067             break;
00068           default:
00069             throw INTERP_KERNEL::Exception("incompatible Trio field - wrong nb of nodes per elem");
00070           }
00071         break;
00072       case 3:
00073         switch (triofield._nodes_per_elem)
00074           {
00075           case 4:
00076             elemtype=INTERP_KERNEL::NORM_TETRA4;
00077             break;
00078           case 8 : 
00079             elemtype=INTERP_KERNEL::NORM_HEXA8;
00080             break;
00081           default:
00082             throw INTERP_KERNEL::Exception("incompatible Trio field - wrong nb of nodes per elem");
00083           }
00084         break;
00085       default:
00086         throw INTERP_KERNEL::Exception("incompatible Trio field - wrong mesh dimension");
00087       }
00088     //creating a connectivity table that complies to MED (1 indexing)
00089     //and passing it to _mesh
00090     int* conn=new int[triofield._nodes_per_elem];
00091     _mesh->setMeshDimension(triofield._mesh_dim);
00092     for (int i=0; i<triofield._nb_elems;i++)
00093       {
00094         for(int j=0;j<triofield._nodes_per_elem;j++)
00095           {
00096             conn[j]=(triofield._connectivity)[i*triofield._nodes_per_elem+j];
00097           }
00098         _mesh->insertNextCell(elemtype,triofield._nodes_per_elem,conn);
00099       }
00100     delete[] conn;
00101     
00102     _mesh->finishInsertingCells();
00103     
00104     //field on the sending end
00105     int nb_case=triofield.nb_values();
00106     if (triofield._type==0)
00107       {
00108         _field =  ParaMEDMEM::MEDCouplingFieldDouble::New(ParaMEDMEM::ON_CELLS,ParaMEDMEM::ONE_TIME);
00109       }
00110     else
00111       {
00112         _field =  ParaMEDMEM::MEDCouplingFieldDouble::New(ParaMEDMEM::ON_NODES,ParaMEDMEM::ONE_TIME ); 
00113       }
00114     _field->setMesh(_mesh);
00115     _field->setNature(ParaMEDMEM::ConservativeVolumic);
00116     ParaMEDMEM::DataArrayDouble *fieldArr=ParaMEDMEM::DataArrayDouble::New();
00117     fieldArr->alloc(_field->getNumberOfTuples(),triofield._nb_field_components);
00118     _field->setName(triofield.getName().c_str());
00119     std::string meshName("SupportOf_"); meshName+=_field->getName();
00120     _mesh->setName(meshName.c_str());
00121     _field->setTime(triofield._time1,0,triofield._itnumber);
00122     if (triofield._field!=0)
00123       {
00124         for (int i =0; i<nb_case; i++)
00125           for (int j=0; j<triofield._nb_field_components; j++)
00126             {
00127               fieldArr->setIJ(i,j,triofield._field[i*triofield._nb_field_components+j]);
00128             }
00129       }
00130     //field on the receiving end
00131     else
00132       {
00133         // the trio field points to the pointer inside the MED field
00134         triofield._field=const_cast<double*> (fieldArr->getPointer());
00135         for (int i=0; i<triofield._nb_field_components*nb_case;i++)
00136           triofield._field[i]=0.0;
00137       }
00138     _field->setArray(fieldArr);
00139     fieldArr->decrRef();
00140   }
00141 
00142   MEDField::~MEDField()
00143   {
00144     if(_field)
00145       _field->decrRef();
00146     if(_mesh)
00147       _mesh->decrRef();
00148   }
00149 }