27 #include "headcpp.hpp"
30 #include "tensor_impl.hpp"
32 #include "metric_tensor.hpp"
36 int std_position_array (
const Array<int>& idx,
int ndim) {
37 assert (idx.get_ndim() == 1) ;
38 int valence =idx.get_size(0) ;
40 for (
int i=0 ; i<valence ; i++)
41 assert ((idx(i)>=1) && (idx(i)<=ndim)) ;
43 for (
int i=0 ; i<valence ; i++)
44 res = ndim*res+(idx(i)-1) ;
49 int std_position_index (
const Index& idx,
int ndim) {
50 int valence =idx.get_ndim() ;
51 for (
int i=0 ; i<valence ; i++)
52 assert ((idx(i)>=0) && (idx(i)<ndim)) ;
54 for (
int i=0 ; i<valence ; i++)
55 res = ndim*res+(idx(i)) ;
60 Array<int> std_indices (
int place,
int valence,
int ndim) {
61 Array<int> res(valence) ;
62 for (
int i=valence-1 ; i>=0 ; i--) {
63 res.set(i) = div(place, ndim).rem ;
64 place = int((place-res(i))/ndim) ;
78 : espace(sp), ndom(espace.get_nbr_domains()), ndim(espace.get_ndim()),
79 valence(val), basis(bb), type_indice(tipe), name_affected{false}, name_indice{valence},
80 n_comp(int(pow(espace.get_ndim(), val))), cmp{n_comp}, parameters{}
87 assert ((tipe(i) == COV) || (tipe(i) == CON)) ;
98 : espace(sp), ndom(espace.get_nbr_domains()), ndim(espace.get_ndim()),
99 valence(val), basis(bb), type_indice(val), name_affected{false}, name_indice{valence},
100 n_comp(int(pow(espace.get_ndim(), val))), cmp{n_comp}, parameters{}
103 assert ((tipe == COV) || (tipe == CON)) ;
114 : espace(sp), ndom(espace.get_nbr_domains()), ndim(dim),
115 valence(val), basis(bb), type_indice(tipe), name_affected{false}, name_indice{valence},
116 n_comp(int(pow(ndim, val))), cmp{n_comp}, parameters{}
122 for (
int i=0 ; i<
valence ; i++)
123 assert ((tipe(i) == COV) || (tipe(i) == CON)) ;
134 : espace(sp), ndom(espace.get_nbr_domains()), ndim(dim),
135 valence(val), basis(bb), type_indice(val), name_affected{false}, name_indice{valence},
136 n_comp(int(pow(ndim, val))), cmp{n_comp}, parameters{}
139 assert ((tipe == COV) || (tipe == CON)) ;
154 espace(source.espace), ndom(source.ndom), ndim(source.ndim), valence(source.valence), basis(source.basis),
155 type_indice(source.type_indice), name_affected{copy && source.name_affected}, name_indice{valence},
156 n_comp (source.n_comp), cmp{n_comp}, parameters{source.parameters}
160 for (
int i=0 ; i<
valence ; i++)
174 valence(0), basis(sp), type_indice(0), name_affected{false}, name_indice{},
175 n_comp{1}, cmp{1}, parameters{}
186 : espace(sp), ndom(espace.get_nbr_domains()), ndim(espace.get_ndim()),
187 valence(val), basis(bb), type_indice(val), name_affected{false}, name_indice{valence},
188 n_comp(ncompi), cmp{n_comp}, parameters{}
191 assert ((tipe == COV) || (tipe == CON)) ;
197 : espace(sp), ndom(espace.get_nbr_domains()), ndim(espace.get_ndim()),
198 valence(val), basis(bb), type_indice(tipe), name_affected{false}, name_indice{valence},
199 n_comp(ncompi), cmp{n_comp}, parameters{}
206 for (
int i=0 ; i<
valence ; i++)
207 assert ((tipe(i) == COV) || (tipe(i) == CON)) ;
213 : espace(sp), ndom(espace.get_nbr_domains()), ndim(dim),
214 valence(val), basis(bb), type_indice(val), name_affected{false}, name_indice{valence},
215 n_comp(ncompi), cmp{n_comp}, parameters{}
218 assert ((tipe == COV) || (tipe == CON)) ;
224 : espace(sp), ndom(espace.get_nbr_domains()), ndim(dim),
225 valence(val), basis(bb), type_indice(tipe), name_affected{false}, name_indice{valence},
226 n_comp(ncompi), cmp{n_comp}, parameters{}
233 for (
int i=0 ; i<
valence ; i++)
234 assert ((tipe(i) == COV) || (tipe(i) == CON)) ;
241 espace(sp), ndom(espace.get_nbr_domains()), ndim(espace.get_ndim()),
242 basis(sp, fd), type_indice(fd), name_affected{false}, name_indice{valence}, n_comp{}, cmp{}, parameters{}
244 fread_be (&
valence,
sizeof(
int), 1, fd) ;
246 fread_be (&
n_comp,
sizeof(
int), 1, fd) ;
256 espace(sp), ndom(espace.get_nbr_domains()), ndim(dim),
257 basis(sp, fd), type_indice(fd), name_affected{false}, name_indice{valence}, n_comp{}, cmp{}, parameters{}
259 fread_be (&
valence,
sizeof(
int), 1, fd) ;
261 fread_be (&
n_comp,
sizeof(
int), 1, fd) ;
271 assert(&espace == &so.espace);
272 std::swap(ndom,so.ndom);
273 std::swap(ndim,so.ndim);
274 std::swap(valence,so.valence);
275 basis.swap(so.basis);
276 type_indice.swap(so.type_indice);
277 std::swap(name_affected,so.name_affected);
278 name_indice.swap(so.name_indice);
279 std::swap(n_comp,so.n_comp);
281 parameters.swap(so.parameters);
282 std::swap(give_place_array,so.give_place_array);
283 std::swap(give_place_index,so.give_place_index);
284 std::swap(give_indices,so.give_indices);
287 #ifdef TENSOR_MOVE_SEMANTIC
288 Tensor::Tensor(
Tensor&& so) noexcept: espace{so.espace}, ndom{so.ndom}, ndim{so.ndim}, valence{so.valence},
289 basis{std::move(so.basis)}, type_indice{std::move(so.type_indice)}, name_affected{so.name_affected},
290 name_indice{std::move(so.name_indice)}, n_comp{so.n_comp}, cmp{std::move(so.cmp)},
291 parameters{std::move(so.parameters)}, give_place_array{so.give_place_array},
292 give_place_index{so.give_place_index}, give_indices{so.give_indices}
295 void Tensor::do_move(Tensor &&so,
bool move_cmp) noexcept
297 std::swap(ndom,so.ndom);
299 valence = so.valence;
300 basis = std::move(so.basis);
301 type_indice = std::move(so.type_indice);
302 name_affected = so.name_affected;
303 std::swap(name_indice,so.name_indice);
304 std::swap(n_comp,so.n_comp);
305 if(move_cmp) cmp = std::move(so.cmp);
306 std::swap(parameters,so.parameters);
307 give_place_array = so.give_place_array;
308 give_place_index = so.give_place_index;
309 give_indices = so.give_indices;
314 this->do_move(std::move(so),
true);
324 for(
auto & v :
cmp) safe_delete(v);
330 fwrite_be (&
valence,
sizeof(
int), 1, fd) ;
332 fwrite_be (&
n_comp,
sizeof(
int), 1, fd) ;
333 for (
int i=0 ; i<
n_comp ; i++)
339 ostream& operator<<(ostream& flux,
const Tensor &source ) {
342 flux <<
"Class : " <<
typeid(source).name()
343 <<
" Valence : " << source.
valence <<
'\n' ;
350 flux <<
"Type of the indices : " ;
351 for (
int i=0 ; i<source.
valence ; i++) {
352 flux <<
"index " << i <<
" : " ;
354 flux <<
" contravariant." <<
'\n' ;
356 flux <<
" covariant." <<
'\n' ;
357 if ( i < source.
valence-1 ) flux <<
" " ;
362 for (
int i=0 ; i<source.
n_comp ; i++) {
366 "===================== Scalar field ========================= \n" ;
369 flux <<
"================ Component " ;
371 for (
int j=0 ; j<source.
valence ; j++) {
372 flux <<
" " << num_indices(j) ;
374 flux <<
" ================ \n" ;
378 flux << *source.
cmp[i] <<
'\n' ;
387 for (
int d=0 ; d<
ndom ; d++) {
393 cmp[0]->std_base_domain(d) ;
401 cmp[0]->std_base_x_cart_domain(d) ;
402 cmp[1]->std_base_y_cart_domain(d) ;
403 cmp[2]->std_base_z_cart_domain(d) ;
407 cmp[0]->std_base_r_spher_domain(d) ;
408 cmp[1]->std_base_t_spher_domain(d) ;
409 cmp[2]->std_base_p_spher_domain(d) ;
413 cmp[0]->std_base_r_mtz_domain(d) ;
414 cmp[1]->std_base_t_mtz_domain(d) ;
415 cmp[2]->std_base_p_mtz_domain(d) ;
418 #ifndef REMOVE_ALL_CHECKS
420 cerr <<
"Tensor::std_base not yet implemented for " <<
basis << endl ;
430 for (
int cc=0 ; cc<
n_comp ; cc++) {
433 for (
int i=1 ; i<
valence ; i++)
435 cmp[cc]->set_domain(d).set_base() = result ;
451 for (
int ncmp=0 ; ncmp<
valence ; ncmp++) {
453 bool finloop = false ;
456 found.
set(pos) = true ;
457 perm.
set(ncmp) = pos ;
464 if ((pos ==
valence) && (!finloop)) {
469 while ((!finloop) && (res));
reference set(const Index &pos)
Read/write of an element.
int get_ndim() const
Returns the number of dimensions.
int get_size(int i) const
Returns the size of a given dimension.
void save(FILE *fd, Array_ordering order=last_index) const
Save in a file.
Class for storing the basis of decompositions of a field.
Describes the tensorial basis used by the various tensors.
int get_basis(int nd) const
Read only the basis in a given domain.
void save(FILE *fd) const
Saving function.
virtual Base_spectral mult(const Base_spectral &, const Base_spectral &) const
Method for the multiplication of two Base_spectral.
virtual Tensor change_basis_cart_to_spher(int dd, const Tensor &so) const
Changes the tensorial basis from Cartsian to spherical in a given domain.
virtual Tensor change_basis_spher_to_cart(int dd, const Tensor &so) const
Changes the tensorial basis from spherical to Cartesian in a given domain.
Class that gives the position inside a multi-dimensional Array.
bool inc(int increm, int var=0)
Increments the position of the Index.
int get_m_quant() const
Returns .
The class Scalar does not really implements scalars in the mathematical sense but rather tensorial co...
Val_domain & set_domain(int)
Read/write of a particular Val_domain.
The Space class is an ensemble of domains describing the whole space of the computation.
const Domain * get_domain(int i) const
returns a pointer on the domain.
int get_nbr_domains() const
Returns the number of Domains.
virtual ~Tensor()
Destructor.
bool name_affected
Indicator that states if the indices have been given names.
Memory_mapped_array< char > name_indice
If the indices haves names they are stored here.
void swap(Tensor &) noexcept
Swap method.
virtual Tensor & operator=(const Tensor &)
Assignment to a Tensor.
Base_tensor basis
Tensorial basis with respect to which the tensor components are defined.
void change_basis_spher_to_cart()
Changes the tensorial basis from orthonormal spherical to Cartesian.
bool find_indices(const Tensor &tt, Array< int > &output_ind) const
Checks whether the current Tensor and tt have compatible indices (i.e.
int ndom
The number of Domain.
Tensor(const Space &sp, int val, const Array< int > &tipe, const Base_tensor &ba)
Constructor.
int valence
Valence of the tensor (0 = scalar, 1 = vector, etc...)
int(* give_place_index)(const Index &, int)
Pointer on the function that gives the storage location corresponding to a set of indices values....
Array< int > type_indice
1D array of integers of size valence containing the type of each index: COV for a covariant one and C...
Param_tensor parameters
Possible additional parameters relevant for the current Tensor.
Array< int >(* give_indices)(int, int, int)
Pointer on the function that gives the indices corresponding to a give storage location.
virtual void std_base()
Sets the standard spectal bases of decomposition for each component.
Memory_mapped_array< Scalar * > cmp
Array of size n_comp of pointers onto the components.
Array< int > get_index_type() const
void change_basis_cart_to_spher()
Changes the tensorial basis from Cartesian to orthonormal spherical.
const Base_tensor & get_basis() const
Returns the vectorial basis (triad) on which the components are defined.
virtual void save(FILE *) const
Saving operator.
Scalar & set()
Read/write for a Scalar.
virtual Array< int > indices(int pos) const
Gives the values of the indices corresponding to a location in the array used for storage of the comp...
int n_comp
Number of stored components, depending on the symmetry.
const Space & espace
The Space.
int get_valence() const
Returns the valence.
bool is_m_quant_affected() const
Checks whether the additional parameter is affected (used for boson stars for instance).
int(* give_place_array)(const Array< int > &, int)
Pointer on the function that gives the storage location corresponding to a set of indices values....
int & set_basis(int dd)
Assigns a new tensorial basis in a given domain.
A class derived from Tensor to deal specificaly with objects of valence 1 (and so also 1-forms).