tbl_val.C

00001 /*
00002  * Methods for the class Tbl_val.
00003  *
00004  * See the file tbl_val.h for documentation
00005  *
00006  */
00007 
00008 /*
00009  *   Copyright (c) 2001 Jerome Novak
00010  *
00011  *   This file is part of LORENE.
00012  *
00013  *   LORENE is free software; you can redistribute it and/or modify
00014  *   it under the terms of the GNU General Public License as published by
00015  *   the Free Software Foundation; either version 2 of the License, or
00016  *   (at your option) any later version.
00017  *
00018  *   LORENE is distributed in the hope that it will be useful,
00019  *   but WITHOUT ANY WARRANTY; without even the implied warranty of
00020  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00021  *   GNU General Public License for more details.
00022  *
00023  *   You should have received a copy of the GNU General Public License
00024  *   along with LORENE; if not, write to the Free Software
00025  *   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00026  *
00027  */
00028 
00029 
00030 char TBL_VAL_C[] = "$Header: /cvsroot/Lorene/C++/Source/Valencia/tbl_val.C,v 1.5 2008/02/18 13:53:48 j_novak Exp $" ;
00031 
00032 /*
00033  * $Id: tbl_val.C,v 1.5 2008/02/18 13:53:48 j_novak Exp $
00034  * $Log: tbl_val.C,v $
00035  * Revision 1.5  2008/02/18 13:53:48  j_novak
00036  * Removal of special indentation instructions.
00037  *
00038  * Revision 1.4  2007/11/02 15:45:58  j_novak
00039  * Added an ugly method "append_array", which substitutes the argument to the
00040  * main array t.
00041  *
00042  * Revision 1.3  2002/10/16 14:37:15  j_novak
00043  * Reorganization of #include instructions of standard C++, in order to
00044  * use experimental version 3 of gcc.
00045  *
00046  * Revision 1.2  2001/12/04 21:27:54  e_gourgoulhon
00047  *
00048  * All writing/reading to a binary file are now performed according to
00049  * the big endian convention, whatever the system is big endian or
00050  * small endian, thanks to the functions fwrite_be and fread_be
00051  *
00052  * Revision 1.1  2001/11/22 13:41:54  j_novak
00053  * Added all source files for manipulating Valencia type objects and making
00054  * interpolations to and from Meudon grids.
00055  *
00056  *
00057  * $Header: /cvsroot/Lorene/C++/Source/Valencia/tbl_val.C,v 1.5 2008/02/18 13:53:48 j_novak Exp $
00058  *
00059  */
00060 
00061 // headers C
00062 #include <math.h>
00063 
00064 // headers Lorene
00065 #include "headcpp.h"
00066 #include "tbl_val.h"
00067 #include "utilitaires.h"
00068 
00069 
00070             //---------------//
00071             // Constructeurs //
00072             //---------------//
00073 
00074 
00075 // Constructeur a partir d'une grille de Valence
00076 Tbl_val::Tbl_val(const Grille_val* g) : etat(ETATNONDEF), 
00077   dim(g->get_dim_tbl()), gval(g), t(0x0), tzri(0x0), txti(0x0), typi(0x0) {}
00078 
00079 // Copie
00080 Tbl_val::Tbl_val(const Tbl_val& tc) : etat(tc.etat), dim(tc.dim), 
00081   gval(tc.gval)  {
00082 
00083   // La valeur eventuelle
00084   if (tc.etat == ETATQCQ) {
00085     t = new double[get_taille()] ;
00086     for (int i=0 ; i<get_taille() ; i++) {
00087       t[i] = tc.t[i] ;
00088     }
00089     
00090     tzri = new double[get_taille_i(0)] ;
00091     for (int i=0 ; i<get_taille_i(0) ; i++) {
00092       tzri[i] = tc.tzri[i] ;
00093     }
00094     
00095     if (get_ndim() > 1) {
00096       txti= new double[get_taille_i(1)] ;
00097       for (int i=0 ; i<get_taille_i(1) ; i++) txti[i] = tc.txti[i] ;
00098     }
00099     else txti = 0x0 ;
00100     if (get_ndim() > 2) {
00101       typi = new double[get_taille_i(2)] ;
00102       for (int i=0; i<get_taille_i(2); i++) typi[i] = tc.typi[i] ;
00103     } 
00104     else typi = 0x0 ;
00105   }
00106   else{
00107     t = 0x0 ; 
00108     tzri = 0x0 ;
00109     txti = 0x0 ;
00110     typi = 0x0 ;
00111   }   
00112 }
00113 
00114 // From file
00115 Tbl_val::Tbl_val(const Grille_val* g, FILE* fd) : dim(g->get_dim_tbl()), 
00116   gval(g) {
00117   
00118   fread_be(&etat, sizeof(int), 1, fd) ;     // etat
00119   
00120   // Le tableau
00121   if (etat == ETATQCQ) {
00122     t = new double[get_taille()] ;
00123     fread_be(t, sizeof(double), get_taille(), fd) ;     // le tableau
00124     tzri = new double[get_taille_i(0)] ;
00125     fread_be(tzri, sizeof(double), get_taille_i(0), fd) ;
00126     if (get_ndim() > 1) {
00127       txti = new double[get_taille_i(1)] ;
00128       fread_be(txti, sizeof(double), get_taille_i(1), fd) ; }
00129     else txti = 0x0 ;
00130     if (get_ndim() > 2) {
00131       typi = new double[get_taille_i(2)] ;
00132       fread_be(typi, sizeof(double), get_taille_i(2), fd) ; }
00133     else typi = 0x0 ;
00134   }
00135   else{
00136     t = 0x0 ; 
00137     tzri = 0x0 ;
00138     txti = 0x0 ;
00139     typi = 0x0 ;
00140   }
00141 }
00142 
00143             //-------------//
00144             // Destructeur //
00145             //-------------//
00146 
00147 Tbl_val::~Tbl_val() {
00148   del_t() ;
00149 }
00150 
00151             //-------------//
00152             // Affectation //
00153             //-------------//
00154 
00155 // From Tbl_val
00156 void Tbl_val::operator=(const Tbl_val& tx)
00157 {
00158   // Protection
00159   assert( gval == tx.gval ) ;
00160   assert(tx.get_etat() != ETATNONDEF) ;
00161   
00162   int n = get_taille() ;
00163   int ndim = get_ndim() ;
00164   switch (tx.etat) {
00165   case ETATZERO:
00166     set_etat_zero() ;
00167     break ;
00168     
00169   case ETATQCQ:
00170     set_etat_qcq() ;
00171     for (int i=0 ; i<n ; i++) {
00172       t[i] = tx.t[i] ;
00173     }
00174     for (int i=0; i<get_taille_i(0); i++) tzri[i] = tx.tzri[i] ;
00175     if (ndim > 1) for(int i=0; i < get_taille_i(1); i++)
00176       txti[i] = tx.txti[i] ;
00177     if (ndim > 2) for(int i=0; i < get_taille_i(2); i++)
00178       typi[i] = tx.typi[i] ; 
00179     break ;
00180     
00181   default:
00182     cout << "Erreur bizarre !" << endl ;
00183     abort() ;
00184     break ;
00185   }
00186 }
00187 
00188 // From double
00189 void Tbl_val::operator=(double a)
00190 {
00191   if ( a == double(0) ) {
00192     set_etat_zero() ;
00193   }
00194   else {
00195     int n = get_taille() ;
00196     set_etat_qcq() ;
00197     for (int i=0 ; i<n ; i++) {
00198       t[i] = a ;
00199     }
00200     for (int i=0 ; i < get_taille_i(0) ; i++) 
00201       tzri[i] = a ;
00202     
00203     if (txti != 0x0) for (int i=0 ; i < get_taille_i(1) ; i++) 
00204       txti[i] = a ;
00205     
00206     if (typi != 0x0) for (int i=0 ; i < get_taille_i(2) ; i++) 
00207       typi[i] = a ;
00208   }
00209 }
00210 
00211 // From int
00212 void Tbl_val::operator=(int m)
00213 {
00214   if (m == 0) {
00215     set_etat_zero() ;
00216   }
00217   else {
00218     int n = get_taille() ;
00219     set_etat_qcq() ;
00220     for (int i=0 ; i<n ; i++) {
00221       t[i] = m ;
00222     }
00223     for (int i=0 ; i < get_taille_i(0) ; i++) 
00224       tzri[i] = m ;
00225     
00226     if (txti != 0x0) for (int i=0 ; i < get_taille_i(1) ; i++) 
00227       txti[i] = m ;
00228     
00229     if (typi != 0x0) for (int i=0 ; i < get_taille_i(2) ; i++) 
00230       typi[i] = m ;
00231     
00232   }
00233 }
00234 
00235     
00236             //------------//
00237             // Sauvegarde //
00238             //------------//
00239 
00240 // save in a file
00241 
00242 void Tbl_val::sauve(FILE* fd) const {
00243   
00244   fwrite_be(&etat, sizeof(int), 1, fd) ;            // etat
00245   if (etat == ETATQCQ) {
00246     fwrite_be(t, sizeof(double), get_taille(), fd) ;        // le tableau
00247     fwrite_be(tzri, sizeof(double), get_taille_i(0), fd) ;
00248     if (get_ndim() > 1) 
00249       fwrite_be(txti, sizeof(double), get_taille_i(1), fd) ;
00250     if (get_ndim() > 2) 
00251       fwrite_be(typi, sizeof(double), get_taille_i(2), fd) ;
00252   }
00253 }
00254 
00255 //-----------------//
00256 // Gestion memoire //
00257 //-----------------//
00258 
00259 // Destructeur logique
00260 void Tbl_val::del_t() {
00261   if (t != 0x0) delete [] t ;
00262   t = 0x0 ;
00263   if (tzri != 0x0) delete [] tzri ;
00264   tzri = 0x0 ;
00265   if (txti != 0x0) delete [] txti ;
00266   txti = 0x0 ;
00267   if (typi != 0x0) delete [] typi ;
00268   typi = 0x0 ;
00269   etat = ETATNONDEF ;
00270 }
00271 
00272 // ETATZERO
00273 void Tbl_val::set_etat_zero() {
00274   if (etat == ETATZERO) return ;
00275   del_t() ;
00276   etat = ETATZERO ;
00277 }
00278 
00279 // ETATNONDEF
00280 void Tbl_val::set_etat_nondef() {
00281   if (etat == ETATNONDEF) return ;
00282   del_t() ;
00283   etat = ETATNONDEF ;
00284 }
00285 
00286 // ETATQCQ
00287 void Tbl_val::set_etat_qcq() {
00288   if (etat == ETATQCQ) return ;
00289   
00290   // Protection
00291   assert( (etat == ETATZERO) || (etat == ETATNONDEF) ) ; // sinon...
00292   
00293   t = new double[get_taille()] ;
00294   tzri = new double[get_taille_i(0)] ;
00295   int ndim = get_ndim() ;
00296   if (ndim > 1) {txti = new double[get_taille_i(1)] ;}
00297   else txti = 0x0 ;    
00298   if (ndim > 2) {typi = new double[get_taille_i(2)] ;}
00299   else typi = 0x0 ;
00300   etat = ETATQCQ ;
00301 }
00302 
00303 // ZERO hard
00304 void Tbl_val::annule_hard() {
00305   if (t == 0x0) {
00306     t = new double[get_taille()] ;
00307   }
00308   for (int i=0 ; i<get_taille() ; i++) {
00309     t[i] = 0. ;
00310   }
00311   tzri = new double[get_taille_i(0)] ;
00312   for (int i=0 ; i < get_taille_i(0) ; i++) 
00313     tzri[i] = 0 ;
00314   int ndim = get_ndim() ;
00315   if (ndim > 1) {
00316     txti = new double[get_taille_i(1)] ;
00317     for (int i=0 ; i < get_taille_i(1) ; i++) txti[i] = 0 ;
00318   }
00319   else txti = 0x0 ;    
00320   if (ndim > 2) {
00321     typi = new double[get_taille_i(2)] ;
00322     for (int i=0 ; i < get_taille_i(2) ; i++) typi[i] = 0 ;
00323   }
00324   else typi = 0x0 ;
00325   
00326   etat = ETATQCQ ;
00327 }
00328 
00329 void Tbl_val::append_array(double* t_in) {
00330     assert (t_in != 0x0) ;
00331     del_t() ;
00332     t = t_in ;
00333     etat = ETATQCQ ;
00334 }
00335 
00336             //------------------------//
00337             //  Display       //
00338             //------------------------//
00339             
00340 //-----------           
00341 // Operator<<
00342 //-----------           
00343 
00344 ostream& operator<<(ostream& o, const Tbl_val& t) {
00345     
00346   int ndim = t.get_ndim() ;
00347   o.precision(4);
00348   o.setf(ios::showpoint);
00349   o << "*** Tbl_val " << ndim << "D" << "   size: " ; 
00350   for (int i = 0; i<ndim-1; i++) {
00351     o << t.get_dim(ndim-1-i) ;
00352     if (ndim-i == 3) o << "(Y)" << " x " ;
00353     if (ndim-i == 2) o << "(X)" << " x " ;
00354   } 
00355   o << t.get_dim(0) << "(Z)" << " + " << t.gval->get_fantome() << 
00356     " hidden cells on each side =  " << t.get_taille() << endl ;
00357   
00358   if (t.get_etat() == ETATZERO) {
00359     o << "Identically ZERO" << endl ;
00360     return o ;
00361   }
00362   
00363   if (t.get_etat() == ETATNONDEF) {
00364     o << "UNDEFINED STATE" << endl ;
00365     return o ;
00366   }
00367   
00368   assert(t.etat == ETATQCQ) ;
00369   switch (ndim) {
00370     
00371   case 1 : {
00372     for (int i=0 ; i<t.get_dim(0) ; i++) {
00373       o << " " << t(i)  ;
00374     }
00375     o << endl ;
00376     break ;
00377   }
00378   
00379   
00380   case 2 : {
00381     for (int j=0 ; j<t.get_dim(1) ; j++) {
00382       o << " J_x " << j << " : " << endl ;
00383       for (int i=0 ; i<t.get_dim(0) ; i++) {
00384     o << " " << t(j, i)  ;
00385       }
00386       o << endl ;
00387     }
00388     o << endl ;
00389     break ;
00390   }
00391   
00392   case 3 : {
00393     for (int k=0 ; k<t.get_dim(2) ; k++) {
00394       o << " K_y = " << k << " : " << endl ;
00395       for (int j=0 ; j<t.get_dim(1) ; j++) {
00396     o << " J_x = " << j << " : "  ;
00397     for (int i=0 ; i<t.get_dim(0) ; i++) {
00398       o << " " << t(k, j, i)  ;
00399     }
00400     o << endl ;
00401       }
00402       o << endl ;
00403     }
00404     o << endl ;
00405     break ;
00406   }
00407   
00408   default : {
00409     cout << "operator<< Tbl_val : unexpected dimension !" << endl ;
00410     cout << " ndim = " << ndim << endl ;    
00411     abort() ;
00412     break ;
00413   }
00414   }
00415   return o ;
00416 }
00417 
00418 //---------------
00419 // Affiche_seuil
00420 //---------------
00421 
00422 void Tbl_val::affiche_seuil(ostream& ost, int precis,  double seuil) const {
00423 
00424   int ndim = get_ndim() ;
00425   ost << "*** Tbl_val " << ndim << "D" << "   size: " ; 
00426   for (int i = 0; i<ndim-1; i++) {
00427     ost << get_dim(i) << " x " ;
00428   } 
00429   ost << get_dim(ndim-1) << " + " << gval->get_fantome() <<
00430     " hidden cells on each side  =  " << get_taille() << endl ; 
00431   
00432   // Cas particuliers
00433   //-----------------
00434   
00435   if (etat == ETATNONDEF) {
00436     ost << "    state: UNDEFINED" << endl ;
00437     return ;
00438   }
00439   
00440   if (etat == ETATZERO) {
00441     ost << "    state: ZERO" << endl ;
00442     return ;
00443   }
00444   
00445   // Affichage des elements du tableau 
00446   //----------------------------------
00447   
00448   ost << "    threshold for display : " << seuil << endl ; 
00449   ost.precision(precis);
00450   ost.setf(ios::showpoint);
00451   
00452   ost << "   Values on the nodes, without hidden cells:" << endl ;
00453   switch (get_ndim()) {
00454   case 1 : {                    // cas 1-D
00455     
00456     for (int i=0; i<get_dim(0); i++) {
00457       ost <<  " " << setw(precis) << (*this)(i)  ;
00458     }
00459     ost << endl ;
00460     break ;
00461   }
00462   
00463   case 2 : {                // cas 2-D
00464     
00465     for (int j=0; j<get_dim(1); j++) {
00466       ost <<  " #j=" << j << " : "  ;
00467       for (int i=0; i<get_dim(0); i++){
00468     ost <<  " " << setw(precis) << (*this)(j, i)  ;
00469       }
00470       ost << endl;
00471     }
00472     ost << endl;
00473     break;
00474     }
00475   
00476   case 3 : {                // cas 3-D
00477     for (int k=0; k<get_dim(2); k++) {
00478       for (int j=0; j<get_dim(1); j++){
00479     int test_imp = 0 ;
00480     for (int i=0; i<get_dim(0); i++){
00481       if ( fabs( (*this)(k, j, i) ) >= seuil ) 
00482         test_imp = 1 ; 
00483     }
00484     if (test_imp == 1 ) {
00485       ost <<  " #k=" << k <<",j=" << j << " : "  ;
00486       for (int i=0; i<get_dim(0); i++){
00487         ost <<  " " << setw(precis) << (*this)(k, j, i) ;
00488       }
00489       ost << endl ;
00490             }
00491       }
00492     }
00493     ost << endl;
00494     break;
00495   }
00496   
00497   default : {
00498     cout << "Tbl_val:affiche_seuil : unexpected dimension !" << endl ;
00499     cout << " get_ndim() = " << get_ndim() << endl ;    
00500     abort() ; 
00501     break;
00502   }               
00503   
00504   }     // fin du switch sur le nombre de dimensions
00505   
00506   // On restaure l'etat du flot ost a ses valeurs standards:
00507   ost.precision(6);
00508   ost.unsetf(ios::showpoint);
00509 }
00510 
00511 
00512 

Generated on Tue Feb 7 01:35:20 2012 for LORENE by  doxygen 1.4.6