File: tbl_val.C

package info (click to toggle)
lorene 0.0.0~cvs20161116%2Bdfsg-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 26,472 kB
  • sloc: cpp: 212,946; fortran: 21,645; makefile: 1,750; sh: 4
file content (520 lines) | stat: -rw-r--r-- 12,216 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
/*
 * Methods for the class Tbl_val.
 *
 * See the file tbl_val.h for documentation
 *
 */

/*
 *   Copyright (c) 2001 Jerome Novak
 *
 *   This file is part of LORENE.
 *
 *   LORENE is free software; you can redistribute it and/or modify
 *   it under the terms of the GNU General Public License as published by
 *   the Free Software Foundation; either version 2 of the License, or
 *   (at your option) any later version.
 *
 *   LORENE is distributed in the hope that it will be useful,
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *   GNU General Public License for more details.
 *
 *   You should have received a copy of the GNU General Public License
 *   along with LORENE; if not, write to the Free Software
 *   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 *
 */


char TBL_VAL_C[] = "$Header: /cvsroot/Lorene/C++/Source/Valencia/tbl_val.C,v 1.7 2014/10/13 08:53:48 j_novak Exp $" ;

/*
 * $Id: tbl_val.C,v 1.7 2014/10/13 08:53:48 j_novak Exp $
 * $Log: tbl_val.C,v $
 * Revision 1.7  2014/10/13 08:53:48  j_novak
 * Lorene classes and functions now belong to the namespace Lorene.
 *
 * Revision 1.6  2014/10/06 15:13:22  j_novak
 * Modified #include directives to use c++ syntax.
 *
 * Revision 1.5  2008/02/18 13:53:48  j_novak
 * Removal of special indentation instructions.
 *
 * Revision 1.4  2007/11/02 15:45:58  j_novak
 * Added an ugly method "append_array", which substitutes the argument to the
 * main array t.
 *
 * Revision 1.3  2002/10/16 14:37:15  j_novak
 * Reorganization of #include instructions of standard C++, in order to
 * use experimental version 3 of gcc.
 *
 * Revision 1.2  2001/12/04 21:27:54  e_gourgoulhon
 *
 * All writing/reading to a binary file are now performed according to
 * the big endian convention, whatever the system is big endian or
 * small endian, thanks to the functions fwrite_be and fread_be
 *
 * Revision 1.1  2001/11/22 13:41:54  j_novak
 * Added all source files for manipulating Valencia type objects and making
 * interpolations to and from Meudon grids.
 *
 *
 * $Header: /cvsroot/Lorene/C++/Source/Valencia/tbl_val.C,v 1.7 2014/10/13 08:53:48 j_novak Exp $
 *
 */

// headers C
#include <cmath>

// headers Lorene
#include "headcpp.h"
#include "tbl_val.h"
#include "utilitaires.h"


			//---------------//
			// Constructeurs //
			//---------------//


// Constructeur a partir d'une grille de Valence
namespace Lorene {
Tbl_val::Tbl_val(const Grille_val* g) : etat(ETATNONDEF), 
  dim(g->get_dim_tbl()), gval(g), t(0x0), tzri(0x0), txti(0x0), typi(0x0) {}

// Copie
Tbl_val::Tbl_val(const Tbl_val& tc) : etat(tc.etat), dim(tc.dim), 
  gval(tc.gval)  {

  // La valeur eventuelle
  if (tc.etat == ETATQCQ) {
    t = new double[get_taille()] ;
    for (int i=0 ; i<get_taille() ; i++) {
      t[i] = tc.t[i] ;
    }
    
    tzri = new double[get_taille_i(0)] ;
    for (int i=0 ; i<get_taille_i(0) ; i++) {
      tzri[i] = tc.tzri[i] ;
    }
    
    if (get_ndim() > 1) {
      txti= new double[get_taille_i(1)] ;
      for (int i=0 ; i<get_taille_i(1) ; i++) txti[i] = tc.txti[i] ;
    }
    else txti = 0x0 ;
    if (get_ndim() > 2) {
      typi = new double[get_taille_i(2)] ;
      for (int i=0; i<get_taille_i(2); i++) typi[i] = tc.typi[i] ;
    } 
    else typi = 0x0 ;
  }
  else{
    t = 0x0 ; 
    tzri = 0x0 ;
    txti = 0x0 ;
    typi = 0x0 ;
  }   
}

// From file
Tbl_val::Tbl_val(const Grille_val* g, FILE* fd) : dim(g->get_dim_tbl()), 
  gval(g) {
  
  fread_be(&etat, sizeof(int), 1, fd) ;		// etat
  
  // Le tableau
  if (etat == ETATQCQ) {
    t = new double[get_taille()] ;
    fread_be(t, sizeof(double), get_taille(), fd) ;	    // le tableau
    tzri = new double[get_taille_i(0)] ;
    fread_be(tzri, sizeof(double), get_taille_i(0), fd) ;
    if (get_ndim() > 1) {
      txti = new double[get_taille_i(1)] ;
      fread_be(txti, sizeof(double), get_taille_i(1), fd) ; }
    else txti = 0x0 ;
    if (get_ndim() > 2) {
      typi = new double[get_taille_i(2)] ;
      fread_be(typi, sizeof(double), get_taille_i(2), fd) ; }
    else typi = 0x0 ;
  }
  else{
    t = 0x0 ; 
    tzri = 0x0 ;
    txti = 0x0 ;
    typi = 0x0 ;
  }
}

			//-------------//
			// Destructeur //
			//-------------//

Tbl_val::~Tbl_val() {
  del_t() ;
}

			//-------------//
			// Affectation //
			//-------------//

// From Tbl_val
void Tbl_val::operator=(const Tbl_val& tx)
{
  // Protection
  assert( gval == tx.gval ) ;
  assert(tx.get_etat() != ETATNONDEF) ;
  
  int n = get_taille() ;
  int ndim = get_ndim() ;
  switch (tx.etat) {
  case ETATZERO:
    set_etat_zero() ;
    break ;
    
  case ETATQCQ:
    set_etat_qcq() ;
    for (int i=0 ; i<n ; i++) {
      t[i] = tx.t[i] ;
    }
    for (int i=0; i<get_taille_i(0); i++) tzri[i] = tx.tzri[i] ;
    if (ndim > 1) for(int i=0; i < get_taille_i(1); i++)
      txti[i] = tx.txti[i] ;
    if (ndim > 2) for(int i=0; i < get_taille_i(2); i++)
      typi[i] = tx.typi[i] ; 
    break ;
    
  default:
    cout << "Erreur bizarre !" << endl ;
    abort() ;
    break ;
  }
}

// From double
void Tbl_val::operator=(double a)
{
  if ( a == double(0) ) {
    set_etat_zero() ;
  }
  else {
    int n = get_taille() ;
    set_etat_qcq() ;
    for (int i=0 ; i<n ; i++) {
      t[i] = a ;
    }
    for (int i=0 ; i < get_taille_i(0) ; i++) 
      tzri[i] = a ;
    
    if (txti != 0x0) for (int i=0 ; i < get_taille_i(1) ; i++) 
      txti[i] = a ;
    
    if (typi != 0x0) for (int i=0 ; i < get_taille_i(2) ; i++) 
      typi[i] = a ;
  }
}

// From int
void Tbl_val::operator=(int m)
{
  if (m == 0) {
    set_etat_zero() ;
  }
  else {
    int n = get_taille() ;
    set_etat_qcq() ;
    for (int i=0 ; i<n ; i++) {
      t[i] = m ;
    }
    for (int i=0 ; i < get_taille_i(0) ; i++) 
      tzri[i] = m ;
    
    if (txti != 0x0) for (int i=0 ; i < get_taille_i(1) ; i++) 
      txti[i] = m ;
    
    if (typi != 0x0) for (int i=0 ; i < get_taille_i(2) ; i++) 
      typi[i] = m ;
    
  }
}

    
			//------------//
			// Sauvegarde //
			//------------//

// save in a file

void Tbl_val::sauve(FILE* fd) const {
  
  fwrite_be(&etat, sizeof(int), 1, fd) ;		    // etat
  if (etat == ETATQCQ) {
    fwrite_be(t, sizeof(double), get_taille(), fd) ;	    // le tableau
    fwrite_be(tzri, sizeof(double), get_taille_i(0), fd) ;
    if (get_ndim() > 1) 
      fwrite_be(txti, sizeof(double), get_taille_i(1), fd) ;
    if (get_ndim() > 2) 
      fwrite_be(typi, sizeof(double), get_taille_i(2), fd) ;
  }
}

//-----------------//
// Gestion memoire //
//-----------------//

// Destructeur logique
void Tbl_val::del_t() {
  if (t != 0x0) delete [] t ;
  t = 0x0 ;
  if (tzri != 0x0) delete [] tzri ;
  tzri = 0x0 ;
  if (txti != 0x0) delete [] txti ;
  txti = 0x0 ;
  if (typi != 0x0) delete [] typi ;
  typi = 0x0 ;
  etat = ETATNONDEF ;
}

// ETATZERO
void Tbl_val::set_etat_zero() {
  if (etat == ETATZERO) return ;
  del_t() ;
  etat = ETATZERO ;
}

// ETATNONDEF
void Tbl_val::set_etat_nondef() {
  if (etat == ETATNONDEF) return ;
  del_t() ;
  etat = ETATNONDEF ;
}

// ETATQCQ
void Tbl_val::set_etat_qcq() {
  if (etat == ETATQCQ) return ;
  
  // Protection
  assert( (etat == ETATZERO) || (etat == ETATNONDEF) ) ; // sinon...
  
  t = new double[get_taille()] ;
  tzri = new double[get_taille_i(0)] ;
  int ndim = get_ndim() ;
  if (ndim > 1) {txti = new double[get_taille_i(1)] ;}
  else txti = 0x0 ;    
  if (ndim > 2) {typi = new double[get_taille_i(2)] ;}
  else typi = 0x0 ;
  etat = ETATQCQ ;
}

// ZERO hard
void Tbl_val::annule_hard() {
  if (t == 0x0) {
    t = new double[get_taille()] ;
  }
  for (int i=0 ; i<get_taille() ; i++) {
    t[i] = 0. ;
  }
  tzri = new double[get_taille_i(0)] ;
  for (int i=0 ; i < get_taille_i(0) ; i++) 
    tzri[i] = 0 ;
  int ndim = get_ndim() ;
  if (ndim > 1) {
    txti = new double[get_taille_i(1)] ;
    for (int i=0 ; i < get_taille_i(1) ; i++) txti[i] = 0 ;
  }
  else txti = 0x0 ;    
  if (ndim > 2) {
    typi = new double[get_taille_i(2)] ;
    for (int i=0 ; i < get_taille_i(2) ; i++) typi[i] = 0 ;
  }
  else typi = 0x0 ;
  
  etat = ETATQCQ ;
}

void Tbl_val::append_array(double* t_in) {
    assert (t_in != 0x0) ;
    del_t() ;
    t = t_in ;
    etat = ETATQCQ ;
}

			//------------------------//
			//	Display		  //
			//------------------------//
			
//-----------			
// Operator<<
//-----------			

ostream& operator<<(ostream& o, const Tbl_val& t) {
    
  int ndim = t.get_ndim() ;
  o.precision(4);
  o.setf(ios::showpoint);
  o << "*** Tbl_val " << ndim << "D" << "   size: " ; 
  for (int i = 0; i<ndim-1; i++) {
    o << t.get_dim(ndim-1-i) ;
    if (ndim-i == 3) o << "(Y)" << " x " ;
    if (ndim-i == 2) o << "(X)" << " x " ;
  } 
  o << t.get_dim(0) << "(Z)" << " + " << t.gval->get_fantome() << 
    " hidden cells on each side =  " << t.get_taille() << endl ;
  
  if (t.get_etat() == ETATZERO) {
    o << "Identically ZERO" << endl ;
    return o ;
  }
  
  if (t.get_etat() == ETATNONDEF) {
    o << "UNDEFINED STATE" << endl ;
    return o ;
  }
  
  assert(t.etat == ETATQCQ) ;
  switch (ndim) {
    
  case 1 : {
    for (int i=0 ; i<t.get_dim(0) ; i++) {
      o << " " << t(i)  ;
    }
    o << endl ;
    break ;
  }
  
  
  case 2 : {
    for (int j=0 ; j<t.get_dim(1) ; j++) {
      o << " J_x " << j << " : " << endl ;
      for (int i=0 ; i<t.get_dim(0) ; i++) {
	o << " " << t(j, i)  ;
      }
      o << endl ;
    }
    o << endl ;
    break ;
  }
  
  case 3 : {
    for (int k=0 ; k<t.get_dim(2) ; k++) {
      o << " K_y = " << k << " : " << endl ;
      for (int j=0 ; j<t.get_dim(1) ; j++) {
	o << " J_x = " << j << " : "  ;
	for (int i=0 ; i<t.get_dim(0) ; i++) {
	  o << " " << t(k, j, i)  ;
	}
	o << endl ;
      }
      o << endl ;
    }
    o << endl ;
    break ;
  }
  
  default : {
    cout << "operator<< Tbl_val : unexpected dimension !" << endl ;
    cout << " ndim = " << ndim << endl ; 	
    abort() ;
    break ;
  }
  }
  return o ;
}

//---------------
// Affiche_seuil
//---------------

void Tbl_val::affiche_seuil(ostream& ost, int precis,  double seuil) const {

  int ndim = get_ndim() ;
  ost << "*** Tbl_val " << ndim << "D" << "   size: " ; 
  for (int i = 0; i<ndim-1; i++) {
    ost << get_dim(i) << " x " ;
  } 
  ost << get_dim(ndim-1) << " + " << gval->get_fantome() <<
    " hidden cells on each side  =  " << get_taille() << endl ; 
  
  // Cas particuliers
  //-----------------
  
  if (etat == ETATNONDEF) {
    ost << "    state: UNDEFINED" << endl ;
    return ;
  }
  
  if (etat == ETATZERO) {
    ost << "    state: ZERO" << endl ;
    return ;
  }
  
  // Affichage des elements du tableau 
  //----------------------------------
  
  ost << "    threshold for display : " << seuil << endl ; 
  ost.precision(precis);
  ost.setf(ios::showpoint);
  
  ost << "   Values on the nodes, without hidden cells:" << endl ;
  switch (get_ndim()) {
  case 1 : {			    	// cas 1-D
    
    for (int i=0; i<get_dim(0); i++) {
      ost <<  " " << setw(precis) << (*this)(i)  ;
    }
    ost << endl ;
    break ;
  }
  
  case 2 : {				// cas 2-D
    
    for (int j=0; j<get_dim(1); j++) {
      ost <<  " #j=" << j << " : "  ;
      for (int i=0; i<get_dim(0); i++){
	ost <<  " " << setw(precis) << (*this)(j, i)  ;
      }
      ost << endl;
    }
    ost << endl;
    break;
	}
  
  case 3 : {				// cas 3-D
    for (int k=0; k<get_dim(2); k++) {
      for (int j=0; j<get_dim(1); j++){
	int test_imp = 0 ;
	for (int i=0; i<get_dim(0); i++){
	  if ( fabs( (*this)(k, j, i) ) >= seuil ) 
	    test_imp = 1 ; 
	}
	if (test_imp == 1 ) {
	  ost <<  " #k=" << k <<",j=" << j << " : "  ;
	  for (int i=0; i<get_dim(0); i++){
	    ost <<  " " << setw(precis) << (*this)(k, j, i) ;
	  }
	  ost << endl ;
		    }
      }
    }
    ost << endl;
    break;
  }
  
  default : {
    cout << "Tbl_val:affiche_seuil : unexpected dimension !" << endl ;
    cout << " get_ndim() = " << get_ndim() << endl ; 	
    abort() ; 
    break;
  }               
  
  }     // fin du switch sur le nombre de dimensions
  
  // On restaure l'etat du flot ost a ses valeurs standards:
  ost.precision(6);
  ost.unsetf(ios::showpoint);
}



}