LORENE
tbl_val_math.C
1/*
2 * Methods for making calculations with Godunov-type arrays.
3 *
4 * See the file tbl_val.h for documentation
5 *
6 */
7
8/*
9 * Copyright (c) 2001 Jerome Novak
10 *
11 * This file is part of LORENE.
12 *
13 * LORENE is free software; you can redistribute it and/or modify
14 * it under the terms of the GNU General Public License as published by
15 * the Free Software Foundation; either version 2 of the License, or
16 * (at your option) any later version.
17 *
18 * LORENE is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU General Public License for more details.
22 *
23 * You should have received a copy of the GNU General Public License
24 * along with LORENE; if not, write to the Free Software
25 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
26 *
27 */
28
29
30
31
32/*
33 * $Id: tbl_val_math.C,v 1.5 2016/12/05 16:18:20 j_novak Exp $
34 * $Log: tbl_val_math.C,v $
35 * Revision 1.5 2016/12/05 16:18:20 j_novak
36 * Suppression of some global variables (file names, loch, ...) to prevent redefinitions
37 *
38 * Revision 1.4 2014/10/13 08:53:48 j_novak
39 * Lorene classes and functions now belong to the namespace Lorene.
40 *
41 * Revision 1.3 2014/10/06 15:13:22 j_novak
42 * Modified #include directives to use c++ syntax.
43 *
44 * Revision 1.2 2002/11/12 10:03:54 j_novak
45 * The method "Tbl_val::get_gval" has been changed to "get_grid".
46 *
47 * Revision 1.1 2001/11/22 13:41:54 j_novak
48 * Added all source files for manipulating Valencia type objects and making
49 * interpolations to and from Meudon grids.
50 *
51 *
52 * $Header: /cvsroot/Lorene/C++/Source/Valencia/tbl_val_math.C,v 1.5 2016/12/05 16:18:20 j_novak Exp $
53 *
54 */
55
56// Headers C
57// ---------
58#include <cmath>
59#include <cstdlib>
60
61// Headers Lorene
62// --------------
63#include "tbl_val.h"
64
65//-------//
66// Sinus //
67//-------//
68
69namespace Lorene {
71{
72 // Protection
73 assert(ti.get_etat() != ETATNONDEF) ;
74
75 // Cas ETATZERO
76 if (ti.get_etat() == ETATZERO) {
77 return ti ;
78 }
79
80 // Cas general
81 assert(ti.get_etat() == ETATQCQ) ; // sinon...
82 Tbl_val to(ti.get_grille()) ; // Tbl_val resultat
83 to.set_etat_qcq() ;
84 int taille = ti.get_taille() ;
85 for (int i=0 ; i<taille ; i++) {
86 to.t[i] = sin(ti.t[i]) ;
87 }
88 for (int i=0; i<ti.get_taille_i(0); i++)
89 to.tzri[i] = sin(ti.tzri[i]) ;
90 if (ti.txti != 0x0) for (int i=0; i<ti.get_taille_i(1); i++)
91 to.txti[i] = sin(ti.txti[i]) ;
92 if (ti.typi != 0x0) for (int i=0; i<ti.get_taille_i(2); i++)
93 to.typi[i] = sin(ti.typi[i]) ;
94 return to ;
95}
96
97//---------//
98// Cosinus //
99//---------//
100
102{
103 // Protection
104 assert(ti.get_etat() != ETATNONDEF) ;
105
106 Tbl_val to(ti.get_grille()) ; // Tbl_val resultat
107 to.set_etat_qcq() ;
108
109 // Cas ETATZERO
110 if (ti.get_etat() == ETATZERO) {
111 to = 1 ;
112 return to ;
113 }
114
115 // Cas general
116 assert(ti.get_etat() == ETATQCQ) ; // sinon...
117 int taille = ti.get_taille() ;
118 for (int i=0 ; i<taille ; i++) {
119 to.t[i] = cos(ti.t[i]) ;
120 }
121 for (int i=0; i<ti.get_taille_i(0); i++)
122 to.tzri[i] = cos(ti.tzri[i]) ;
123 if (ti.txti != 0x0) for (int i=0; i<ti.get_taille_i(1); i++)
124 to.txti[i] = cos(ti.txti[i]) ;
125 if (ti.typi != 0x0) for (int i=0; i<ti.get_taille_i(2); i++)
126 to.typi[i] = cos(ti.typi[i]) ;
127 return to ;
128}
129
130//----------//
131// Tangente //
132//----------//
133
135{
136 // Protection
137 assert(ti.get_etat() != ETATNONDEF) ;
138
139 // Cas ETATZERO
140 if (ti.get_etat() == ETATZERO) {
141 return ti ;
142 }
143
144 // Cas general
145 assert(ti.get_etat() == ETATQCQ) ; // sinon...
146 Tbl_val to(ti.get_grille()) ; // Tbl_val resultat
147 to.set_etat_qcq() ;
148 int taille = ti.get_taille() ;
149 for (int i=0 ; i<taille ; i++) {
150 to.t[i] = tan(ti.t[i]) ;
151 }
152 for (int i=0; i<ti.get_taille_i(0); i++)
153 to.tzri[i] = tan(ti.tzri[i]) ;
154 if (ti.txti != 0x0) for (int i=0; i<ti.get_taille_i(1); i++)
155 to.txti[i] = tan(ti.txti[i]) ;
156 if (ti.typi != 0x0) for (int i=0; i<ti.get_taille_i(2); i++)
157 to.typi[i] = tan(ti.typi[i]) ;
158 return to ;
159}
160
161//----------//
162// ArcSinus //
163//----------//
164
166{
167 // Protection
168 assert(ti.get_etat() != ETATNONDEF) ;
169
170 // Cas ETATZERO
171 if (ti.get_etat() == ETATZERO) {
172 return ti ;
173 }
174
175 // Cas general
176 assert(ti.get_etat() == ETATQCQ) ; // sinon...
177 Tbl_val to(ti.get_grille()) ; // Tbl_val resultat
178 to.set_etat_qcq() ;
179 int taille = ti.get_taille() ;
180 for (int i=0 ; i<taille ; i++) {
181 to.t[i] = asin(ti.t[i]) ;
182 }
183 for (int i=0; i<ti.get_taille_i(0); i++)
184 to.tzri[i] = asin(ti.tzri[i]) ;
185 if (ti.txti != 0x0) for (int i=0; i<ti.get_taille_i(1); i++)
186 to.txti[i] = asin(ti.txti[i]) ;
187 if (ti.typi != 0x0) for (int i=0; i<ti.get_taille_i(2); i++)
188 to.typi[i] = asin(ti.typi[i]) ;
189
190 return to ;
191}
192
193//------------//
194// ArcCosinus //
195//------------//
196
198{
199 // Protection
200 assert(ti.get_etat() != ETATNONDEF) ;
201
202 Tbl_val to(ti.get_grille()) ; // Tbl_val resultat
203 to.set_etat_qcq() ;
204
205 // Cas ETATZERO
206 if (ti.get_etat() == ETATZERO) {
207 to = M_PI * .5 ;
208 return to ;
209 }
210
211 // Cas general
212 assert(ti.get_etat() == ETATQCQ) ; // sinon...
213 int taille = ti.get_taille() ;
214 for (int i=0 ; i<taille ; i++) {
215 to.t[i] = acos(ti.t[i]) ;
216 }
217 for (int i=0; i<ti.get_taille_i(0); i++)
218 to.tzri[i] = acos(ti.tzri[i]) ;
219 if (ti.txti != 0x0) for (int i=0; i<ti.get_taille_i(1); i++)
220 to.txti[i] = acos(ti.txti[i]) ;
221 if (ti.typi != 0x0) for (int i=0; i<ti.get_taille_i(2); i++)
222 to.typi[i] = acos(ti.typi[i]) ;
223 return to ;
224}
225
226//-------------//
227// ArcTangente //
228//-------------//
229
231{
232 // Protection
233 assert(ti.get_etat() != ETATNONDEF) ;
234
235 // Cas ETATZERO
236 if (ti.get_etat() == ETATZERO) {
237 return ti ;
238 }
239
240 // Cas general
241 assert(ti.get_etat() == ETATQCQ) ; // sinon...
242 Tbl_val to(ti.get_grille()) ; // Tbl_val resultat
243 to.set_etat_qcq() ;
244 int taille = ti.get_taille() ;
245 for (int i=0 ; i<taille ; i++) {
246 to.t[i] = atan(ti.t[i]) ;
247 }
248 for (int i=0; i<ti.get_taille_i(0); i++)
249 to.tzri[i] = atan(ti.tzri[i]) ;
250 if (ti.txti != 0x0) for (int i=0; i<ti.get_taille_i(1); i++)
251 to.txti[i] = atan(ti.txti[i]) ;
252 if (ti.typi != 0x0) for (int i=0; i<ti.get_taille_i(2); i++)
253 to.typi[i] = atan(ti.typi[i]) ;
254 return to ;
255}
256
257//------//
258// Sqrt //
259//------//
260
262{
263 // Protection
264 assert(ti.get_etat() != ETATNONDEF) ;
265
266 // Cas ETATZERO
267 if (ti.get_etat() == ETATZERO) {
268 return ti ;
269 }
270
271 // Cas general
272 assert(ti.get_etat() == ETATQCQ) ; // sinon...
273 Tbl_val to(ti.get_grille()) ; // Tbl_val resultat
274 to.set_etat_qcq() ;
275 int taille = ti.get_taille() ;
276 for (int i=0 ; i<taille ; i++) {
277 to.t[i] = sqrt(ti.t[i]) ;
278 }
279 for (int i=0; i<ti.get_taille_i(0); i++)
280 to.tzri[i] = sqrt(ti.tzri[i]) ;
281 if (ti.txti != 0x0) for (int i=0; i<ti.get_taille_i(1); i++)
282 to.txti[i] = sqrt(ti.txti[i]) ;
283 if (ti.typi != 0x0) for (int i=0; i<ti.get_taille_i(2); i++)
284 to.typi[i] = sqrt(ti.typi[i]) ;
285 return to ;
286}
287
288//---------------//
289// Exponentielle //
290//---------------//
291
293{
294 // Protection
295 assert(ti.get_etat() != ETATNONDEF) ;
296
297 Tbl_val to(ti.get_grille()) ; // Tbl_val resultat
298 to.set_etat_qcq() ;
299
300 // Cas ETATZERO
301 if (ti.get_etat() == ETATZERO) {
302 to = 1 ;
303 return to ;
304 }
305
306 // Cas general
307 assert(ti.get_etat() == ETATQCQ) ; // sinon...
308 int taille = ti.get_taille() ;
309 for (int i=0 ; i<taille ; i++) {
310 to.t[i] = exp(ti.t[i]) ;
311 }
312 for (int i=0; i<ti.get_taille_i(0); i++)
313 to.tzri[i] = exp(ti.tzri[i]) ;
314 if (ti.txti != 0x0) for (int i=0; i<ti.get_taille_i(1); i++)
315 to.txti[i] = exp(ti.txti[i]) ;
316 if (ti.typi != 0x0) for (int i=0; i<ti.get_taille_i(2); i++)
317 to.typi[i] = exp(ti.typi[i]) ;
318 return to ;
319}
320
321//-------------//
322// Log naturel //
323//-------------//
324
326{
327 // Protection
328 assert(ti.get_etat() != ETATNONDEF) ;
329
330 // Cas ETATZERO
331 if (ti.get_etat() == ETATZERO) {
332 cout << "Tbl_val log: log(ETATZERO) !" << endl ;
333 abort () ;
334 }
335
336 // Cas general
337 assert(ti.get_etat() == ETATQCQ) ; // sinon...
338 Tbl_val to(ti.get_grille()) ; // Tbl_val resultat
339 to.set_etat_qcq() ;
340 int taille = ti.get_taille() ;
341 for (int i=0 ; i<taille ; i++) {
342 to.t[i] = log(ti.t[i]) ;
343 }
344 for (int i=0; i<ti.get_taille_i(0); i++)
345 to.tzri[i] = log(ti.tzri[i]) ;
346 if (ti.txti != 0x0) for (int i=0; i<ti.get_taille_i(1); i++)
347 to.txti[i] = log(ti.txti[i]) ;
348 if (ti.typi != 0x0) for (int i=0; i<ti.get_taille_i(2); i++)
349 to.typi[i] = log(ti.typi[i]) ;
350 return to ;
351}
352
353//-------------//
354// Log decimal //
355//-------------//
356
358{
359 // Protection
360 assert(ti.get_etat() != ETATNONDEF) ;
361
362 // Cas ETATZERO
363 if (ti.get_etat() == ETATZERO) {
364 cout << "Tbl_val log10: log10(ETATZERO) !" << endl ;
365 abort () ;
366 }
367
368 // Cas general
369 assert(ti.get_etat() == ETATQCQ) ; // sinon...
370 Tbl_val to(ti.get_grille()) ; // Tbl_val resultat
371 to.set_etat_qcq() ;
372 int taille = ti.get_taille() ;
373 for (int i=0 ; i<taille ; i++) {
374 to.t[i] = log10(ti.t[i]) ;
375 }
376 for (int i=0; i<ti.get_taille_i(0); i++)
377 to.tzri[i] = log(ti.tzri[i]) ;
378 if (ti.txti != 0x0) for (int i=0; i<ti.get_taille_i(1); i++)
379 to.txti[i] = log(ti.txti[i]) ;
380 if (ti.typi != 0x0) for (int i=0; i<ti.get_taille_i(2); i++)
381 to.typi[i] = log(ti.typi[i]) ;
382 return to ;
383}
384
385//--------------//
386// Power entier //
387//--------------//
388
389Tbl_val pow(const Tbl_val& ti, int n)
390{
391 // Protection
392 assert(ti.get_etat() != ETATNONDEF) ;
393
394 // Cas ETATZERO
395 if (ti.get_etat() == ETATZERO) {
396 if (n > 0) {
397 return ti ;
398 }
399 else {
400 cout << "Tbl_val pow: ETATZERO^n avec n<=0 ! "<< endl ;
401 abort () ;
402 }
403 }
404
405 // Cas general
406 assert(ti.get_etat() == ETATQCQ) ; // sinon...
407 Tbl_val to(ti.get_grille()) ; // Tbl_val resultat
408 to.set_etat_qcq() ;
409 double x = n ;
410 int taille = ti.get_taille() ;
411 for (int i=0 ; i<taille ; i++) {
412 to.t[i] = pow(ti.t[i], x) ;
413 }
414 for (int i=0; i<ti.get_taille_i(0); i++)
415 to.tzri[i] = pow(ti.tzri[i], x) ;
416 if (ti.txti != 0x0) for (int i=0; i<ti.get_taille_i(1); i++)
417 to.txti[i] = pow(ti.txti[i], x) ;
418 if (ti.typi != 0x0) for (int i=0; i<ti.get_taille_i(2); i++)
419 to.typi[i] = pow(ti.typi[i], x) ;
420 return to ;
421}
422
423//--------------//
424// Power double //
425//--------------//
426
427Tbl_val pow(const Tbl_val& ti, double x)
428{
429 // Protection
430 assert(ti.get_etat() != ETATNONDEF) ;
431
432 // Cas ETATZERO
433 if (ti.get_etat() == ETATZERO) {
434 if (x > 0) {
435 return ti ;
436 }
437 else {
438 cout << "Tbl_val pow: ETATZERO^x avec x<=0 !" << endl ;
439 abort () ;
440 }
441 }
442
443 // Cas general
444 assert(ti.get_etat() == ETATQCQ) ; // sinon...
445 Tbl_val to(ti.get_grille()) ; // Tbl_val resultat
446 to.set_etat_qcq() ;
447 int taille = ti.get_taille() ;
448 for (int i=0 ; i<taille ; i++) {
449 to.t[i] = pow(ti.t[i], x) ;
450 }
451 for (int i=0; i<ti.get_taille_i(0); i++)
452 to.tzri[i] = pow(ti.tzri[i], x) ;
453 if (ti.txti != 0x0) for (int i=0; i<ti.get_taille_i(1); i++)
454 to.txti[i] = pow(ti.txti[i], x) ;
455 if (ti.typi != 0x0) for (int i=0; i<ti.get_taille_i(2); i++)
456 to.typi[i] = pow(ti.typi[i], x) ;
457 return to ;
458}
459
460//----------------//
461// Absolute value //
462//----------------//
463
465{
466 // Protection
467 assert(ti.get_etat() != ETATNONDEF) ;
468
469 // Cas ETATZERO
470 if (ti.get_etat() == ETATZERO) {
471 return ti ;
472 }
473
474 // Cas general
475 assert(ti.get_etat() == ETATQCQ) ; // sinon...
476
477 Tbl_val to(ti.get_grille()) ; // Tbl_val resultat
478 to.set_etat_qcq() ;
479
480 const double* xi = ti.t ;
481 double* xo = to.t ;
482 int taille = ti.get_taille() ;
483
484 for (int i=0 ; i<taille ; i++) {
485 xo[i] = fabs( xi[i] ) ;
486 }
487 for (int i=0; i<ti.get_taille_i(0); i++)
488 to.tzri[i] = fabs(ti.tzri[i]) ;
489 if (ti.txti != 0x0) for (int i=0; i<ti.get_taille_i(1); i++)
490 to.txti[i] = fabs(ti.txti[i]) ;
491 if (ti.typi != 0x0) for (int i=0; i<ti.get_taille_i(2); i++)
492 to.typi[i] = fabs(ti.typi[i]) ;
493
494 return to ;
495}
496//----------------//
497// Cubic //
498//----------------//
499
501{
502 // Protection
503 assert(ti.get_etat() != ETATNONDEF) ;
504
505 // Cas ETATZERO
506 if (ti.get_etat() == ETATZERO) {
507 return ti ;
508 }
509
510 // Cas general
511 assert(ti.get_etat() == ETATQCQ) ; // sinon...
512
513 Tbl_val absolute(abs(ti)) ;
514 Tbl_val res (pow(absolute, 1./3.)) ;
515
516 for (int i=0 ; i<ti.get_taille() ; i++)
517 if (ti.t[i] < 0)
518 res.t[i] *= -1 ;
519 for (int i=0; i<ti.get_taille_i(0); i++)
520 if (ti.tzri[i] < 0) res.tzri[i] *= -1 ;
521 if (ti.txti != 0x0) for (int i=0; i<ti.get_taille_i(1); i++)
522 if (ti.txti[i] < 0) res.txti[i] *= -1 ;
523 if (ti.typi != 0x0) for (int i=0; i<ti.get_taille_i(2); i++)
524 if (ti.typi[i] < 0) res.typi[i] *= -1 ;
525
526 return res ;
527}
528
529//-------------------------------//
530// max //
531//-------------------------------//
532
533double max(const Tbl_val& ti) {
534
535 // Protection
536 assert(ti.get_etat() != ETATNONDEF) ;
537
538 // Cas particulier
539 if (ti.get_etat() == ETATZERO) {
540 return double(0) ;
541 }
542
543 // Cas general
544 assert(ti.get_etat() == ETATQCQ) ; // sinon....
545
546 const double* x = ti.t ;
547 double resu = x[0] ;
548 for (int i=1; i<ti.get_taille(); i++) {
549 if ( x[i] > resu ) resu = x[i] ;
550 }
551
552 return resu ;
553}
554
555//-------------------------------//
556// min //
557//-------------------------------//
558
559double min(const Tbl_val& ti) {
560
561 // Protection
562 assert(ti.get_etat() != ETATNONDEF) ;
563
564 // Cas particulier
565 if (ti.get_etat() == ETATZERO) {
566 return double(0) ;
567 }
568
569 // Cas general
570 assert(ti.get_etat() == ETATQCQ) ; // sinon....
571
572 const double* x = ti.t ;
573 double resu = x[0] ;
574 for (int i=1; i<ti.get_taille(); i++) {
575 if ( x[i] < resu ) resu = x[i] ;
576 }
577
578 return resu ;
579}
580
581//-------------------------------//
582// norme //
583//-------------------------------//
584
585double norme(const Tbl_val& ti) {
586
587 // Protection
588 assert(ti.get_etat() != ETATNONDEF) ;
589
590 double resu = 0 ;
591
592 if (ti.get_etat() != ETATZERO) { // on n'effectue la somme que si necessaire
593
594 assert(ti.get_etat() == ETATQCQ) ; // sinon....
595 const double* x = ti.t ;
596 for (int i=0; i<ti.get_taille(); i++) {
597 resu += fabs( x[i] ) ;
598 }
599
600 }
601
602 return resu ;
603}
604
605//-------------------------------//
606// diffrel //
607//-------------------------------//
608
609double diffrel(const Tbl_val& t1, const Tbl_val& t2) {
610
611 // Protections
612 assert(t1.get_etat() != ETATNONDEF) ;
613 assert(t2.get_etat() != ETATNONDEF) ;
614
615 double norm2 = norme(t2) ;
616 double normdiff = norme(t1-t2) ;
617 double resu ;
618 if ( norm2 == double(0) ) {
619 resu = normdiff ;
620 }
621 else {
622 resu = normdiff / norm2 ;
623 }
624
625 return resu ;
626
627}
628
629//-------------------------------//
630// diffrelmax //
631//-------------------------------//
632
633double diffrelmax(const Tbl_val& t1, const Tbl_val& t2) {
634
635 // Protections
636 assert(t1.get_etat() != ETATNONDEF) ;
637 assert(t2.get_etat() != ETATNONDEF) ;
638
639 double max2 = max(abs(t2)) ;
640 double maxdiff = max(abs(t1-t2)) ;
641 double resu ;
642 if ( max2 == double(0) ) {
643 resu = maxdiff ;
644 }
645 else {
646 resu = maxdiff / max2 ;
647 }
648
649 return resu ;
650
651}
652}
Finite-difference array intended to store field values.
Definition tbl_val.h:97
const Grille_val * get_grille() const
Returns a pointer on the grid on which the Tbl_val is defined.
Definition tbl_val.h:491
int get_taille_i(int i) const
Gives the size of the interface arrays (including the hidden cells).
Definition tbl_val.h:469
double * txti
The array at x (or ) interfaces.
Definition tbl_val.h:118
double * tzri
The array at z (or r) interfaces.
Definition tbl_val.h:116
void set_etat_qcq()
Sets the logical state to ETATQCQ (ordinary state).
Definition tbl_val.C:297
double * t
The array of double at the nodes.
Definition tbl_val.h:114
int get_etat() const
Gives the logical state.
Definition tbl_val.h:459
double * typi
The array at y (or ) interfaces.
Definition tbl_val.h:120
int get_taille() const
Gives the size of the node array (including the hidden cells).
Definition tbl_val.h:462
Cmp atan(const Cmp &)
Arctangent.
Definition cmp_math.C:198
Cmp sqrt(const Cmp &)
Square root.
Definition cmp_math.C:223
Cmp log10(const Cmp &)
Basis 10 logarithm.
Definition cmp_math.C:325
Cmp exp(const Cmp &)
Exponential.
Definition cmp_math.C:273
Cmp sin(const Cmp &)
Sine.
Definition cmp_math.C:72
Tbl diffrel(const Cmp &a, const Cmp &b)
Relative difference between two Cmp (norme version).
Definition cmp_math.C:507
Tbl norme(const Cmp &)
Sums of the absolute values of all the values of the Cmp in each domain.
Definition cmp_math.C:484
Cmp acos(const Cmp &)
Arccosine.
Definition cmp_math.C:172
Cmp asin(const Cmp &)
Arcsine.
Definition cmp_math.C:147
Cmp racine_cubique(const Cmp &)
Cube root.
Definition cmp_math.C:248
Tbl min(const Cmp &)
Minimum values of a Cmp in each domain.
Definition cmp_math.C:461
Tbl max(const Cmp &)
Maximum values of a Cmp in each domain.
Definition cmp_math.C:438
Cmp pow(const Cmp &, int)
Power .
Definition cmp_math.C:351
Cmp cos(const Cmp &)
Cosine.
Definition cmp_math.C:97
Cmp abs(const Cmp &)
Absolute value.
Definition cmp_math.C:413
Tbl diffrelmax(const Cmp &a, const Cmp &b)
Relative difference between two Cmp (max version).
Definition cmp_math.C:542
Cmp tan(const Cmp &)
Tangent.
Definition cmp_math.C:123
Cmp log(const Cmp &)
Neperian logarithm.
Definition cmp_math.C:299
Lorene prototypes.
Definition app_hor.h:67
Coord x
x coordinate centered on the grid
Definition map.h:738