LORENE
metric.C
1 /*
2  * Definition of methods for the class Metric.
3  *
4  */
5 
6 /*
7  * Copyright (c) 2003 Eric Gourgoulhon & Jerome Novak
8  *
9  * Copyright (c) 1999-2001 Philippe Grandclement (for previous class Metrique)
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 version 2
15  * as published by the Free Software Foundation.
16  *
17  * LORENE is distributed in the hope that it will be useful,
18  * but WITHOUT ANY WARRANTY; without even the implied warranty of
19  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20  * GNU General Public License for more details.
21  *
22  * You should have received a copy of the GNU General Public License
23  * along with LORENE; if not, write to the Free Software
24  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
25  *
26  */
27 
28 char metric_C[] = "$Header: /cvsroot/Lorene/C++/Source/Metric/metric.C,v 1.13 2014/10/13 08:53:07 j_novak Exp $" ;
29 
30 /*
31  * $Id: metric.C,v 1.13 2014/10/13 08:53:07 j_novak Exp $
32  * $Log: metric.C,v $
33  * Revision 1.13 2014/10/13 08:53:07 j_novak
34  * Lorene classes and functions now belong to the namespace Lorene.
35  *
36  * Revision 1.12 2014/10/06 15:13:14 j_novak
37  * Modified #include directives to use c++ syntax.
38  *
39  * Revision 1.11 2005/03/02 15:03:46 f_limousin
40  * p_radial_vect is added in del_deriv() and set_der_0x0.
41  *
42  * Revision 1.10 2004/11/18 12:22:33 jl_jaramillo
43  * Method to compute the unit radial vector field with respect
44  * spherical surfaces
45  *
46  * Revision 1.9 2004/02/18 18:45:36 e_gourgoulhon
47  * Computation of p_ricci_scal thanks to the new method
48  * Tensor::trace(const Metric& ).
49  *
50  * Revision 1.8 2004/01/22 14:35:23 e_gourgoulhon
51  * Corrected bug in operator=(const Sym_tensor& ): adresses of deleted
52  * p_met_cov and p_met_con are now set to 0x0.
53  *
54  * Revision 1.7 2004/01/20 09:51:40 f_limousin
55  * Correction in metric::determinant() : indices of tensors go now from 1 to
56  * 3 !
57  *
58  * Revision 1.6 2003/12/30 23:06:30 e_gourgoulhon
59  * Important reorganization of class Metric:
60  * -- suppression of virtual methods fait_* : the actual computations
61  * are now performed via the virtual methods con(), cov(), connect(),
62  * ricci(), ricci_scal(), determinant()
63  * -- the member p_connect is now treated as an ordinary derived data
64  * member
65  * -- the construction of the associated connection (member p_connect)
66  * is performed thanks to the new methods Map::flat_met_spher() and
67  * Map::flat_met_cart().
68  *
69  * Revision 1.5 2003/10/28 21:23:59 e_gourgoulhon
70  * Method Tensor::contract(int, int) renamed Tensor::scontract(int, int).
71  *
72  * Revision 1.4 2003/10/06 16:17:30 j_novak
73  * Calculation of contravariant derivative and Ricci scalar.
74  *
75  * Revision 1.3 2003/10/06 13:58:47 j_novak
76  * The memory management has been improved.
77  * Implementation of the covariant derivative with respect to the exact Tensor
78  * type.
79  *
80  * Revision 1.2 2003/10/03 11:21:47 j_novak
81  * More methods for the class Metric
82  *
83  * Revision 1.1 2003/10/02 15:45:50 j_novak
84  * New class Metric
85  *
86  *
87  *
88  * $Header: /cvsroot/Lorene/C++/Source/Metric/metric.C,v 1.13 2014/10/13 08:53:07 j_novak Exp $
89  *
90  */
91 
92 // C headers
93 #include <cstdlib>
94 
95 // Lorene headers
96 #include "metric.h"
97 #include "utilitaires.h"
98 
99  //-----------------//
100  // Constructors //
101  //-----------------//
102 
103 namespace Lorene {
104 Metric::Metric(const Sym_tensor& symti) : mp(&symti.get_mp()),
105  p_met_cov(0x0),
106  p_met_con(0x0) {
107 
108  int type_index = symti.get_index_type(0) ;
109  assert (symti.get_index_type(1) == type_index) ;
110 
111  if (type_index == COV) {
112  p_met_cov = new Sym_tensor(symti) ;
113  }
114  else {
115  assert(type_index == CON) ;
116  p_met_con = new Sym_tensor(symti) ;
117  }
118 
119  set_der_0x0() ;
121 
122 }
123 
124 
125 Metric::Metric(const Metric& meti) : mp(meti.mp),
126  p_met_cov(0x0),
127  p_met_con(0x0) {
128 
129  if (meti.p_met_cov != 0x0) p_met_cov = new Sym_tensor(*meti.p_met_cov) ;
130 
131  if (meti.p_met_con != 0x0) p_met_con = new Sym_tensor(*meti.p_met_con) ;
132 
133  set_der_0x0() ;
135 
136 }
137 
138 Metric::Metric(const Map& mpi, FILE* ) : mp(&mpi),
139  p_met_cov(0x0),
140  p_met_con(0x0) {
141 
142  cout << "Metric::Metric(FILE*) : not implemented yet!" << endl ;
143 
144  abort() ;
145 }
146 
147 Metric::Metric(const Map& mpi) : mp(&mpi),
148  p_met_cov(0x0),
149  p_met_con(0x0) {
150  set_der_0x0() ;
152 
153 }
154 
155 
156  //---------------//
157  // Destructor //
158  //---------------//
159 
161 
162  if (p_met_cov != 0x0) delete p_met_cov ;
163 
164  if (p_met_con != 0x0) delete p_met_con ;
165 
166  del_deriv() ;
167 
169 
170 }
171 
172  //-------------------//
173  // Memory management //
174  //-------------------//
175 
176 void Metric::del_deriv() const {
177 
178  if (p_connect != 0x0) delete p_connect ;
179  if (p_ricci_scal != 0x0) delete p_ricci_scal ;
180  if (p_determinant != 0x0) delete p_determinant ;
181  if (p_radial_vect != 0x0) delete p_radial_vect ;
182 
183  set_der_0x0() ;
184 
185  //## call to del_tensor_depend() ?
186 }
187 
188 void Metric::set_der_0x0() const {
189 
190  p_connect = 0x0 ;
191  p_ricci_scal = 0x0 ;
192  p_determinant = 0x0 ;
193  p_radial_vect = 0x0 ;
194 
195 }
196 
198 
199  for (int i=0 ; i<N_TENSOR_DEPEND ; i++)
200  if (tensor_depend[i] != 0x0) {
201  int j = tensor_depend[i]->get_place_met(*this) ;
202  if (j!=-1) tensor_depend[i]->del_derive_met(j) ;
203  }
205 
206 }
207 
209 
210  for (int i=0 ; i<N_TENSOR_DEPEND ; i++) {
211  tensor_depend[i] = 0x0 ;
212  }
213 }
214 
215 
216  //-----------------------//
217  // Mutators / assignment //
218  //-----------------------//
219 
220 void Metric::operator=(const Metric& meti) {
221 
222  assert( mp == meti.mp) ;
223 
224  if (p_met_cov != 0x0) {
225  delete p_met_cov ;
226  p_met_cov = 0x0 ;
227  }
228 
229  if (p_met_con != 0x0) {
230  delete p_met_con ;
231  p_met_con = 0x0 ;
232  }
233 
234  if (meti.p_met_cov != 0x0) {
235  p_met_cov = new Sym_tensor(*meti.p_met_cov) ;
236  }
237 
238  if (meti.p_met_con != 0x0) {
239  p_met_con = new Sym_tensor(*meti.p_met_con) ;
240  }
241 
242  del_deriv() ;
243 
244 }
245 
246 void Metric::operator=(const Sym_tensor& symti) {
247 
248  assert(mp == &symti.get_mp()) ;
249 
250  int type_index = symti.get_index_type(0) ;
251  assert (symti.get_index_type(1) == type_index) ;
252 
253  if (p_met_cov != 0x0) {
254  delete p_met_cov ;
255  p_met_cov = 0x0 ;
256  }
257 
258  if (p_met_con != 0x0) {
259  delete p_met_con ;
260  p_met_con = 0x0 ;
261  }
262 
263  if (type_index == COV) {
264  p_met_cov = new Sym_tensor(symti) ;
265  }
266  else {
267  assert(type_index == CON) ;
268  p_met_con = new Sym_tensor(symti) ;
269  }
270 
271  del_deriv() ;
272 
273 }
274 
275  //----------------//
276  // Accessors //
277  //----------------//
278 
279 
280 const Sym_tensor& Metric::cov() const {
281 
282  if (p_met_cov == 0x0) { // a new computation is necessary
283  assert( p_met_con != 0x0 ) ;
285  }
286 
287  return *p_met_cov ;
288 }
289 
290 const Sym_tensor& Metric::con() const {
291 
292  if (p_met_con == 0x0) { // a new computation is necessary
293  assert( p_met_cov != 0x0 ) ;
295  }
296 
297  return *p_met_con ;
298 }
299 
300 
301 const Connection& Metric::connect() const {
302 
303  if (p_connect == 0x0) { // a new computation is necessary
304 
305  // The triad is obtained from the covariant or contravariant representation:
306  const Base_vect_spher* triad_s ;
307  const Base_vect_cart* triad_c ;
308  if (p_met_cov != 0x0) {
309  triad_s =
310  dynamic_cast<const Base_vect_spher*>(p_met_cov->get_triad()) ;
311  triad_c =
312  dynamic_cast<const Base_vect_cart*>(p_met_cov->get_triad()) ;
313  }
314  else {
315  assert(p_met_con != 0x0) ;
316  triad_s =
317  dynamic_cast<const Base_vect_spher*>(p_met_con->get_triad()) ;
318  triad_c =
319  dynamic_cast<const Base_vect_cart*>(p_met_con->get_triad()) ;
320  }
321 
322  // Background flat metric in spherical or Cartesian components
323  if ( triad_s != 0x0 ) {
324  p_connect = new Connection(*this, mp->flat_met_spher()) ;
325  }
326  else {
327  assert( triad_c != 0x0 ) ;
328  p_connect = new Connection(*this, mp->flat_met_cart()) ;
329  }
330 
331  }
332 
333  return *p_connect ;
334 
335 }
336 
337 
338 const Sym_tensor& Metric::ricci() const {
339 
340  const Tensor& ricci_connect = connect().ricci() ;
341 
342  // Check: the Ricci tensor of the connection associated with
343  // the metric must be symmetric:
344  assert( typeid(ricci_connect) == typeid(const Sym_tensor&) ) ;
345 
346  return dynamic_cast<const Sym_tensor&>( ricci_connect ) ;
347 }
348 
349 
350 const Scalar& Metric::ricci_scal() const {
351 
352  if (p_ricci_scal == 0x0) { // a new computation is necessary
353 
354  p_ricci_scal = new Scalar( ricci().trace(*this) ) ;
355 
356  }
357 
358  return *p_ricci_scal ;
359 
360 }
361 
362 const Vector& Metric::radial_vect() const {
363 
364  if (p_radial_vect == 0x0) { // a new computation is necessary
365 
366 
367  p_radial_vect = new Vector ((*this).get_mp(), CON, *((*this).con().get_triad()) ) ;
368 
369  Scalar prov ( sqrt((*this).con()(1,1)) ) ;
370 
371  prov.std_spectral_base() ;
372 
373 
374  p_radial_vect->set(1) = (*this).con()(1,1)/ prov ;
375 
376  p_radial_vect->set(2) = (*this).con()(1,2)/ prov ;
377 
378  p_radial_vect->set(3) = (*this).con()(1,3)/ prov ;
379 
380 
381 
382  // p_radial_vect.std_spectral_base() ;
383 
384 
385  }
386 
387  return *p_radial_vect ;
388 
389 }
390 
391 
392 const Scalar& Metric::determinant() const {
393 
394  if (p_determinant == 0x0) { // a new computation is necessary
395 
396  p_determinant = new Scalar(*mp) ;
397  *p_determinant = cov()(1, 1)*cov()(2, 2)*cov()(3, 3)
398  + cov()(1, 2)*cov()(2, 3)*cov()(3, 1)
399  + cov()(1, 3)*cov()(2, 1)*cov()(3, 2)
400  - cov()(3, 1)*cov()(2, 2)*cov()(1, 3)
401  - cov()(3, 2)*cov()(2, 3)*cov()(1, 1)
402  - cov()(3, 3)*cov()(2, 1)*cov()(1, 2) ;
403  }
404 
405  return *p_determinant ;
406 }
407 
408 
409 
410  //---------//
411  // Outputs //
412  //---------//
413 
414 void Metric::sauve(FILE* fd) const {
415 
416  // Which representation is to be saved
417  int indic ;
418  if (p_met_cov != 0x0)
419  indic = COV ;
420  else if (p_met_con != 0x0)
421  indic = CON ;
422  else indic = 0 ;
423  fwrite_be(&indic, sizeof(int), 1, fd) ;
424  switch (indic) {
425  case COV : {
426  p_met_cov->sauve(fd) ;
427  break ;
428  }
429  case CON : {
430  p_met_con->sauve(fd) ;
431  break ;
432  }
433  default : {
434  break ;
435  }
436  }
437 }
438 
439 ostream& operator<<(ostream& ost, const Metric& meti) {
440 
441  meti >> ost ;
442  return ost ;
443 }
444 
445 
446 ostream& Metric::operator>>(ostream& ost) const {
447 
448  ost << '\n' ;
449 
450  ost << "General type metric" << '\n' ;
451  ost << "-------------------" << '\n' ;
452  ost << '\n' ;
453 
454  if (p_met_cov == 0x0) {
455  ost << "Covariant representation unknown!" << '\n' ;
456  assert( p_met_con != 0x0) ;
457  ost << "CONTRA-variant representation: " << '\n' ;
458  ost << *p_met_con ;
459  }
460  else {
461  ost << "Covariant representation: " << '\n' ;
462  ost << *p_met_cov ;
463  }
464 
465 
466  if (p_connect == 0x0)
467  ost << "Associated connection not computed yet." << '\n' ;
468  else
469  ost << "Associated connection computed." << '\n' ;
470 
471  if (p_ricci_scal == 0x0)
472  ost << "Ricci scalar not computed yet." << '\n' ;
473  else
474  ost << "Ricci scalar computed." << '\n' ;
475 
476  if (p_determinant == 0x0)
477  ost << "determinant not computed yet." << '\n' ;
478  else
479  ost << "determinant computed." << '\n' ;
480 
481  ost << endl ;
482  return ost ;
483 }
484 
485 }
Metric for tensor calculation.
Definition: metric.h:90
virtual void sauve(FILE *) const
Save in a binary file.
Definition: tensor_sym.C:372
virtual const Sym_tensor & con() const
Read-only access to the contravariant representation.
Definition: metric.C:290
void set_der_0x0() const
Sets to 0x0 all the pointers on derived quantities.
Definition: metric.C:188
virtual const Tensor & ricci() const
Computes (if not up to date) and returns the Ricci tensor associated with the current connection...
Definition: connection.C:662
Cmp sqrt(const Cmp &)
Square root.
Definition: cmp_math.C:220
const Map *const mp
Reference mapping.
Definition: metric.h:95
Scalar * p_determinant
Pointer on the determinant.
Definition: metric.h:132
Sym_tensor * inverse() const
Returns a pointer on the inverse of the Sym_tensor (seen as a matrix).
Definition: sym_tensor.C:372
virtual void del_derive_met(int) const
Logical destructor of the derivatives depending on the i-th element of met_depend ...
Definition: tensor.C:414
Lorene prototypes.
Definition: app_hor.h:64
Tensor field of valence 0 (or component of a tensorial field).
Definition: scalar.h:387
Base class for coordinate mappings.
Definition: map.h:670
virtual const Vector & radial_vect() const
Returns the radial vector normal to a spherical slicing and pointing toward spatial infinity...
Definition: metric.C:362
virtual void std_spectral_base()
Sets the spectral bases of the Valeur va to the standard ones for a scalar field. ...
Definition: scalar.C:784
void set_tensor_depend_0x0() const
Sets all elements of tensor_depend to 0x0.
Definition: metric.C:208
const Sym_tensor & ricci() const
Returns the Ricci tensor (given by the Connection p_connect )
Definition: metric.C:338
void del_tensor_depend() const
Deletes all the derivative members of the Tensor contained in tensor_depend .
Definition: metric.C:197
void operator=(const Metric &met)
Assignment to another Metric.
Definition: metric.C:220
int get_place_met(const Metric &) const
Returns the position of the pointer on metre in the array met_depend .
Definition: tensor.C:443
Tensor field of valence 1.
Definition: vector.h:188
friend ostream & operator<<(ostream &, const Metric &)
Display.
Definition: metric.C:439
const Metric_flat & flat_met_cart() const
Returns the flat metric associated with the Cartesian coordinates and with components expressed in th...
Definition: map.C:331
Vector * p_radial_vect
Pointer to the radial vector normal to a spherical slicing and pointing toward spatial infinity...
Definition: metric.h:125
Sym_tensor * p_met_con
Pointer on the covariant representation.
Definition: metric.h:105
virtual void sauve(FILE *) const
Save in a file.
Definition: metric.C:414
Connection * p_connect
Connection associated with the metric.
Definition: metric.h:112
const Base_vect * get_triad() const
Returns the vectorial basis (triad) on which the components are defined.
Definition: tensor.h:866
virtual const Scalar & ricci_scal() const
Returns the Ricci scalar.
Definition: metric.C:350
Class Connection.
Definition: connection.h:113
int get_index_type(int i) const
Gives the type (covariant or contravariant) of the index number i .
Definition: tensor.h:886
virtual const Connection & connect() const
Returns the connection.
Definition: metric.C:301
int fwrite_be(const int *aa, int size, int nb, FILE *fich)
Writes integer(s) into a binary file according to the big endian convention.
Definition: fwrite_be.C:70
Tensor handling.
Definition: tensor.h:288
virtual const Scalar & determinant() const
Returns the determinant.
Definition: metric.C:392
Cartesian vectorial bases (triads).
Definition: base_vect.h:201
Spherical orthonormal vectorial bases (triads).
Definition: base_vect.h:308
virtual const Sym_tensor & cov() const
Read-only access to the covariant representation.
Definition: metric.C:280
const Tensor * tensor_depend[N_TENSOR_DEPEND]
Pointer on the dependancies, that means the array contains pointers on all the Tensor whom derivative...
Definition: metric.h:139
virtual ostream & operator>>(ostream &) const
Operator >> (virtual function called by the operator <<).
Definition: metric.C:446
Scalar * p_ricci_scal
Pointer on the Ricci scalar.
Definition: metric.h:119
Metric(const Sym_tensor &tens)
Standard constructor from a Sym_tensor .
Definition: metric.C:104
Scalar & set(int)
Read/write access to a component.
Definition: vector.C:296
void del_deriv() const
Deletes all the derived quantities.
Definition: metric.C:176
const Map & get_mp() const
Returns the mapping.
Definition: tensor.h:861
Sym_tensor * p_met_cov
Pointer on the contravariant representation.
Definition: metric.h:100
const Metric_flat & flat_met_spher() const
Returns the flat metric associated with the spherical coordinates and with components expressed in th...
Definition: map.C:321
Class intended to describe valence-2 symmetric tensors.
Definition: sym_tensor.h:223
virtual ~Metric()
Destructor.
Definition: metric.C:160