Back to index

salome-med  6.5.0
ICoCoTrioField.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 // ICoCo file common to several codes
00021 // ICoCoTrioField.cxx
00022 // version 1.2 10/05/2010
00023 
00024 #include <ICoCoTrioField.hxx>
00025 #include <string.h>
00026 #include <iostream>
00027 #include <iomanip>
00028 
00029 using namespace ICoCo;
00030 using namespace std;
00031 
00032 // Default constructor
00033 TrioField::TrioField() :
00034   _type(0),
00035   _mesh_dim(0),
00036   _space_dim(0),
00037   _nbnodes(0),
00038   _nodes_per_elem(0),
00039   _nb_elems(0),
00040   _itnumber(0),
00041   _connectivity(0),
00042   _coords(0),
00043   _time1(0.),
00044   _time2(0.),
00045   _nb_field_components(0),
00046   _field(0),
00047   _has_field_ownership(false) { }
00048 
00049 // Copy constructor
00050 TrioField::TrioField(const TrioField& OtherField):_connectivity(0),_coords(0),_field(0) {
00051   (*this)=OtherField;
00052 }
00053 
00054 // Destructor
00055 TrioField::~TrioField() {
00056   clear();
00057 }
00058 
00059 // After the call to clear(), all pointers are null and field ownership is false.
00060 // Arrays are deleted if necessary
00061 void TrioField::clear() {
00062   if (_connectivity)
00063     delete[] _connectivity;
00064   if (_coords)
00065     delete[] _coords;
00066   if (_field && _has_field_ownership)
00067     delete[] _field;
00068   _connectivity=0;
00069   _coords=0;
00070   _field=0;
00071   _has_field_ownership=false;
00072 }
00073 
00074 // Returns the number of value locations
00075 // The size of field is nb_values()*_nb_field_components
00076 int TrioField::nb_values() const {
00077   if (_type==0)
00078     return _nb_elems;
00079   else if (_type==1)
00080     return _nbnodes;
00081   throw 0;
00082   return -1;
00083 }
00084 
00085 // Save field to a .field file (loadable by visit!)
00086 void TrioField::save(ostream& os) const{
00087 
00088   os << setprecision(12);
00089   os << getName() << endl;
00090   os << _type << endl;
00091   os << _mesh_dim << endl;
00092   os << _space_dim << endl;
00093   os << _nbnodes << endl;
00094   os << _nodes_per_elem << endl;
00095   os << _nb_elems << endl;
00096     
00097   os<< _itnumber<<endl;
00098   for (int i=0;i<_nb_elems;i++) {
00099     for (int j=0;j<_nodes_per_elem;j++)
00100       os << " " << _connectivity[i*_nodes_per_elem+j];
00101     os<<endl;
00102   }
00103   
00104   for (int i=0;i<_nbnodes;i++) {
00105     for (int j=0;j<_space_dim;j++)
00106       os << " " << _coords[i*_space_dim+j] ;
00107     os << endl;
00108   }
00109   
00110   os << _time1 << endl;
00111   os << _time2 << endl;
00112   os << _nb_field_components << endl;
00113 
00114   if (_field) {
00115     os << 1 << endl;
00116     for (int i=0;i<nb_values();i++) {
00117       for (int j=0;j<_nb_field_components;j++)
00118         os << " " << _field[i*_nb_field_components+j];
00119       os << endl;
00120     }
00121   }
00122   else
00123     os << 0 << endl;
00124 
00125   os << _has_field_ownership << endl;
00126 
00127 }
00128 
00129 // Restore field from a .field file
00130 void TrioField::restore(istream& in) {
00131 
00132   string name;
00133   in >> name; 
00134   setName(name);
00135   in >> _type;
00136   in >> _mesh_dim;
00137   in >> _space_dim;
00138   in >> _nbnodes;
00139   in >> _nodes_per_elem;
00140   in >> _nb_elems;
00141     
00142   in >> _itnumber;
00143   if (_connectivity)
00144     delete [] _connectivity;
00145   _connectivity=new int[_nodes_per_elem*_nb_elems];
00146   for (int i=0;i<_nb_elems;i++) {
00147     for (int j=0;j<_nodes_per_elem;j++)
00148       in >> _connectivity[i*_nodes_per_elem+j];
00149   }
00150   if (_coords)
00151     delete [] _coords;
00152   _coords=new double[_nbnodes*_space_dim];
00153   for (int i=0;i<_nbnodes;i++) {
00154     for (int j=0;j<_space_dim;j++)
00155       in >> _coords[i*_space_dim+j];
00156   }
00157   
00158   in >> _time1;
00159   in >> _time2;
00160   in >> _nb_field_components;
00161   int test;
00162   in >> test;
00163   if (test) {
00164     if (_field)
00165       delete [] _field;
00166     _field=new double[_nb_field_components*nb_values()];
00167     for (int i=0;i<nb_values();i++) {
00168       for (int j=0;j<_nb_field_components;j++)
00169         in>> _field[i*_nb_field_components+j];
00170     }
00171   }
00172   else
00173     _field=0;
00174 
00175   in >> _has_field_ownership;
00176 }
00177 
00178 
00179 // After the call to set_standalone(), field ownership is true and field is allocated
00180 // to the size _nb_field_components*nb_values().
00181 // The values of the field have been copied if necessary.
00182 void TrioField::set_standalone() {
00183   if (!_field) {
00184     _field=new double[_nb_field_components*nb_values()];
00185     _has_field_ownership=true;
00186     
00187   }
00188   else if (!_has_field_ownership) {
00189     double *tmp_field=new double[_nb_field_components*nb_values()];
00190     memcpy(tmp_field,_field,_nb_field_components*nb_values()*sizeof(double));
00191     _field=tmp_field;
00192     _has_field_ownership=true;
00193   }
00194 }
00195 
00196 // Used to simulate a 0D geometry (Cathare/Trio for example).
00197 void TrioField::dummy_geom() {
00198   _type=0;
00199   _mesh_dim=2;
00200   _space_dim=2;
00201   _nbnodes=3;
00202   _nodes_per_elem=3;
00203   _nb_elems=1;
00204   _itnumber=0;
00205   if (_connectivity)
00206     delete[] _connectivity;
00207   _connectivity=new int[3];
00208   _connectivity[0]=0;
00209   _connectivity[1]=1;
00210   _connectivity[2]=2;
00211   if (_coords)
00212     delete[] _coords;
00213   _coords=new double[6];
00214   _coords[0]=0;
00215   _coords[1]=0;
00216   _coords[2]=1;
00217   _coords[3]=0;
00218   _coords[4]=0;
00219   _coords[5]=1;
00220   _time1=0;
00221   _time2=1;
00222   _nb_field_components=1;
00223   if (_field && _has_field_ownership)
00224     delete[] _field;
00225   _has_field_ownership=false;
00226   _field=0;
00227 }
00228 
00229 // Overloading operator = for TrioField
00230 // This becomes an exact copy of NewField.
00231 // If NewField._has_field_ownership is false, they point to the same values.
00232 // Otherwise the values are copied.
00233 TrioField& TrioField::operator=(const TrioField& NewField){
00234 
00235   clear();
00236 
00237   _type=NewField._type;
00238   _mesh_dim=NewField._mesh_dim;
00239   _space_dim=NewField._space_dim;
00240   _nbnodes=NewField._nbnodes;
00241   _nodes_per_elem=NewField._nodes_per_elem;
00242   _nb_elems=NewField._nb_elems;
00243   _itnumber=NewField._itnumber;
00244   _time1=NewField._time1;
00245   _time2=NewField._time2;
00246   _nb_field_components=NewField._nb_field_components;
00247 
00248   if (!NewField._connectivity)
00249     _connectivity=0;
00250   else {
00251     _connectivity=new int[_nodes_per_elem*_nb_elems];
00252     memcpy( _connectivity,NewField._connectivity,_nodes_per_elem*_nb_elems*sizeof(int));
00253   }
00254 
00255   if (!NewField._coords)
00256     _coords=0;
00257   else {
00258     _coords=new double[_nbnodes*_space_dim];
00259     memcpy( _coords,NewField._coords,_nbnodes*_space_dim*sizeof(double));
00260   }
00261 
00262   //Copie des valeurs du champ
00263   _has_field_ownership=NewField._has_field_ownership;
00264   if (_has_field_ownership) {
00265     _field=new double[nb_values()*_nb_field_components];
00266     memcpy(_field,NewField._field,nb_values()*_nb_field_components*sizeof(double));
00267   }
00268   else
00269     _field=NewField._field;
00270 
00271   return(*this);
00272 
00273 }
00274 
00275 
00276