| 1 |
/* |
| 2 |
* Simple tensor manipulations |
| 3 |
* |
| 4 |
*/ |
| 5 |
|
| 6 |
/* |
| 7 |
* Copyright (c) 2005 Eric Gourgoulhon |
| 8 |
* |
| 9 |
* This file is part of LORENE. |
| 10 |
* |
| 11 |
* LORENE is free software; you can redistribute it and/or modify |
| 12 |
* it under the terms of the GNU General Public License version 2 |
| 13 |
* as published by the Free Software Foundation. |
| 14 |
* |
| 15 |
* LORENE is distributed in the hope that it will be useful, |
| 16 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 17 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 18 |
* GNU General Public License for more details. |
| 19 |
* |
| 20 |
* You should have received a copy of the GNU General Public License |
| 21 |
* along with LORENE; if not, write to the Free Software |
| 22 |
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
| 23 |
* |
| 24 |
*/ |
| 25 |
|
| 26 |
/* |
| 27 |
* $Id: demo_tensor.C,v 1.6 2014/10/13 08:54:07 j_novak Exp $ |
| 28 |
* $Log: demo_tensor.C,v $ |
| 29 |
* Revision 1.6 2014/10/13 08:54:07 j_novak |
| 30 |
* Lorene classes and functions now belong to the namespace Lorene. |
| 31 |
* |
| 32 |
* Revision 1.5 2014/10/06 15:09:48 j_novak |
| 33 |
* Modified #include directives to use c++ syntax. |
| 34 |
* |
| 35 |
* Revision 1.4 2006/05/30 13:22:20 j_novak |
| 36 |
* Back to previous versions... |
| 37 |
* |
| 38 |
* Revision 1.2 2005/11/16 17:11:59 e_gourgoulhon |
| 39 |
* Added fij.std_spectral_base() (although not necessary in the |
| 40 |
* present case) |
| 41 |
* Modified some comments. |
| 42 |
* |
| 43 |
* Revision 1.1 2005/11/16 09:46:30 e_gourgoulhon |
| 44 |
* Added demo_tensor.C |
| 45 |
* |
| 46 |
* |
| 47 |
* $Header: /cvsroot/Lorene/School05/Wednesday/demo_tensor.C,v 1.6 2014/10/13 08:54:07 j_novak Exp $ |
| 48 |
* |
| 49 |
*/ |
| 50 |
|
| 51 |
// C headers |
| 52 |
#include <cstdlib> |
| 53 |
#include <cassert> |
| 54 |
#include <cmath> |
| 55 |
|
| 56 |
// Lorene headers |
| 57 |
#include "headcpp.h" // standard input/output C++ headers (iostream, fstream) |
| 58 |
#include "metric.h" // classes Metric, Tensor, etc... |
| 59 |
#include "nbr_spx.h" // defines infinity as an ordinary number: __infinity |
| 60 |
#include "graphique.h" // for graphical outputs |
| 61 |
#include "utilitaires.h" // utilities |
| 62 |
|
| 63 |
using namespace Lorene ; |
| 64 |
|
| 65 |
int main() { |
| 66 |
|
| 67 |
// Setup of a multi-domain grid (Lorene class Mg3d) |
| 68 |
// ------------------------------------------------ |
| 69 |
|
| 70 |
int nz = 3 ; // Number of domains |
| 71 |
int nr = 17 ; // Number of collocation points in r in each domain |
| 72 |
int nt = 9 ; // Number of collocation points in theta in each domain |
| 73 |
int np = 8 ; // Number of collocation points in phi in each domain |
| 74 |
int symmetry_theta = SYM ; // symmetry with respect to the equatorial plane |
| 75 |
int symmetry_phi = NONSYM ; // no symmetry in phi |
| 76 |
bool compact = true ; // external domain is compactified |
| 77 |
|
| 78 |
// Multi-domain grid construction: |
| 79 |
|
| 80 |
Mg3d mgrid(nz, nr, nt, np, symmetry_theta, symmetry_phi, compact) ; |
| 81 |
|
| 82 |
cout << mgrid << endl ; |
| 83 |
|
| 84 |
|
| 85 |
// Setup of an affine mapping : grid --> physical space (Lorene class Map_af) |
| 86 |
// -------------------------------------------------------------------------- |
| 87 |
|
| 88 |
// radial boundaries of each domain: |
| 89 |
double r_limits[] = {0., 1., 2., __infinity} ; |
| 90 |
assert( nz == 3 ) ; // since the above array describes only 3 domains |
| 91 |
|
| 92 |
Map_af map(mgrid, r_limits) ; // Mapping construction |
| 93 |
|
| 94 |
cout << map << endl ; |
| 95 |
|
| 96 |
// Denomination of various coordinates associated with the mapping |
| 97 |
// --------------------------------------------------------------- |
| 98 |
|
| 99 |
const Coord& r = map.r ; |
| 100 |
const Coord& x = map.x ; |
| 101 |
const Coord& y = map.y ; |
| 102 |
|
| 103 |
// Some scalar field to be used as a conformal factor |
| 104 |
// -------------------------------------------------- |
| 105 |
|
| 106 |
Scalar psi4(map) ; |
| 107 |
|
| 108 |
psi4 = 1 + 5*x*y*exp(-r*r) ; |
| 109 |
|
| 110 |
psi4.set_outer_boundary(nz-1, 1.) ; // 1 at spatial infinity |
| 111 |
// (instead of NaN !) |
| 112 |
|
| 113 |
psi4.std_spectral_base() ; // Standar polynomial bases will be used |
| 114 |
// to perform the spectral expansions |
| 115 |
|
| 116 |
// Graphical outputs: |
| 117 |
// ----------------- |
| 118 |
|
| 119 |
// 1D view via PGPLOT |
| 120 |
des_profile(psi4, 0., 4., 1, M_PI/4, M_PI/4, "r", "\\gq\\u4") ; |
| 121 |
|
| 122 |
// 2D view of the slice z=0 via PGPLOT |
| 123 |
des_coupe_z(psi4, 0., -3., 3., -3., 3., "\\gq\\u4") ; |
| 124 |
|
| 125 |
// 3D view of the same slice via OpenDX |
| 126 |
psi4.visu_section('z', 0., -3., 3., -3., 3.) ; |
| 127 |
|
| 128 |
cout << "Coefficients of the spectral expansion of Psi^4:" << endl ; |
| 129 |
psi4.spectral_display() ; |
| 130 |
|
| 131 |
arrete() ; // pause (waiting for return) |
| 132 |
|
| 133 |
// Components of the flat metric in an orthonormal |
| 134 |
// spherical frame |
| 135 |
|
| 136 |
Sym_tensor fij(map, COV, map.get_bvect_spher()) ; |
| 137 |
fij.set(1,1) = 1 ; |
| 138 |
fij.set(1,2) = 0 ; |
| 139 |
fij.set(1,3) = 0 ; |
| 140 |
fij.set(2,2) = 1 ; |
| 141 |
fij.set(2,3) = 0 ; |
| 142 |
fij.set(3,3) = 1 ; |
| 143 |
|
| 144 |
fij.std_spectral_base() ; // Standar polynomial bases will be used |
| 145 |
// to perform the spectral expansions |
| 146 |
|
| 147 |
|
| 148 |
// Components of the physical metric in an orthonormal |
| 149 |
// spherical frame |
| 150 |
|
| 151 |
Sym_tensor gij = psi4 * fij ; |
| 152 |
|
| 153 |
// Construction of the metric from the covariant components: |
| 154 |
|
| 155 |
Metric gam(gij) ; |
| 156 |
|
| 157 |
// Construction of a Vector : V^i = (Psi^4)^{;i} |
| 158 |
|
| 159 |
Vector vv = psi4.derive_con(gam) ; // this is spherical components |
| 160 |
// (same triad as gam) |
| 161 |
|
| 162 |
vv.dec_dzpuis(2) ; // the dzpuis flag (power of r in the CED) |
| 163 |
// is set to 0 (= 2 - 2) |
| 164 |
|
| 165 |
// Cartesian components of the vector : |
| 166 |
Vector vv_cart = vv ; |
| 167 |
vv_cart.change_triad( map.get_bvect_cart() ) ; |
| 168 |
|
| 169 |
// Plot of the vector field : |
| 170 |
|
| 171 |
des_coupe_vect_z(vv_cart, 0., -4., 1., -2., 2., -2., 2., "Vector V") ; |
| 172 |
|
| 173 |
// A symmetric tensor of valence 2 : the Ricci tensor associated |
| 174 |
// with the metric gam : |
| 175 |
|
| 176 |
Sym_tensor tens1 = gam.ricci() ; |
| 177 |
|
| 178 |
const Sym_tensor& tens2 = gam.ricci() ; // same as before except that |
| 179 |
// no memory is allocated for a |
| 180 |
// new tensor: tens2 is merely |
| 181 |
// a non-modifiable reference to |
| 182 |
// the Ricci tensor of gam |
| 183 |
|
| 184 |
// Plot of tens1 |
| 185 |
|
| 186 |
des_meridian(tens1, 0., 4., "Ricci (x r\\u3\\d in last domain)", 10) ; |
| 187 |
|
| 188 |
// Another valence 2 tensor : the covariant derivative of V with respect to |
| 189 |
// the metric gam : |
| 190 |
|
| 191 |
Tensor tens3 = vv.derive_cov(gam) ; |
| 192 |
|
| 193 |
const Tensor& tens4 = vv.derive_cov(gam) ; |
| 194 |
|
| 195 |
// the reference tens4 is preferable over the new object tens3 if you do |
| 196 |
// not intend to modify tens4 or vv, because it does not perform any |
| 197 |
// memory allocation for a tensor. |
| 198 |
|
| 199 |
// Raising an index with the metric gam : |
| 200 |
|
| 201 |
Tensor tens5 = tens3.up(1, gam) ; // 1 = second index |
| 202 |
// (index j in the covariant derivative |
| 203 |
// V^i_{;j}) |
| 204 |
|
| 205 |
Tensor diff1 = tens5 - vv.derive_con(gam) ; // this should be zero |
| 206 |
|
| 207 |
// Check: |
| 208 |
cout << "Maximum value of diff1 in each domain : " << endl ; |
| 209 |
Tbl tdiff1 = max(diff1) ; |
| 210 |
|
| 211 |
// Another valence 2 tensor : Lie_V R_{ij} |
| 212 |
|
| 213 |
Sym_tensor tens6 = tens1.derive_lie(vv) ; |
| 214 |
|
| 215 |
// Contracting two tensors : |
| 216 |
|
| 217 |
Tensor tens7 = contract(tens1, 1, tens5, 0) ; // contracting the last index |
| 218 |
// of tens1 with the first one |
| 219 |
// of tens5 |
| 220 |
|
| 221 |
// self contraction of a tensor : |
| 222 |
|
| 223 |
Scalar scal1 = contract(tens3, 0, 1) ; // 0 = first index, 1 = second index |
| 224 |
|
| 225 |
// Each of these fields should be zero: |
| 226 |
|
| 227 |
Scalar diff2 = scal1 - vv.divergence(gam) ; // divergence |
| 228 |
|
| 229 |
Scalar diff3 = scal1 - tens3.trace() ; // trace |
| 230 |
|
| 231 |
// Check : |
| 232 |
cout << "Maximum value of diff2 in each domain : " |
| 233 |
<< max(abs(diff2)) << endl ; |
| 234 |
|
| 235 |
cout << "Maximum value of diff3 in each domain : " |
| 236 |
<< max(abs(diff3)) << endl ; |
| 237 |
|
| 238 |
arrete() ; |
| 239 |
|
| 240 |
// Tensorial product : |
| 241 |
|
| 242 |
Tensor_sym tens8 = tens1 * tens3 ; // tens1 = R_{ij} |
| 243 |
// tens3 = V^k_{;l} |
| 244 |
// tens8 = (T8)_{ij}^k_l = R_ij V^k_{;l} |
| 245 |
|
| 246 |
cout << "Valence of tens8 : " << tens8.get_valence() << endl ; |
| 247 |
|
| 248 |
arrete() ; |
| 249 |
|
| 250 |
cout << "Spectral coefficients of the component (2,3,1,1) of tens8 : " |
| 251 |
<< endl ; |
| 252 |
|
| 253 |
tens8(2,3,1,1).spectral_display() ; |
| 254 |
|
| 255 |
//////////////////////////////////////////////////////////////////////// |
| 256 |
// // |
| 257 |
// To see more functions, please have a look to Lorene documentation // |
| 258 |
// at http://www.lorene.obspm.fr/Refguide/ // |
| 259 |
// // |
| 260 |
//////////////////////////////////////////////////////////////////////// |
| 261 |
|
| 262 |
return EXIT_SUCCESS ; |
| 263 |
|
| 264 |
} |