KADATH
base_spectral.cpp
1 /*
2  Copyright 2017 Philippe Grandclement
3 
4  This file is part of Kadath.
5 
6  Kadath is free software: you can redistribute it and/or modify
7  it under the terms of the GNU General Public License as published by
8  the Free Software Foundation, either version 3 of the License, or
9  (at your option) any later version.
10 
11  Kadath is distributed in the hope that it will be useful,
12  but WITHOUT ANY WARRANTY; without even the implied warranty of
13  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  GNU General Public License for more details.
15 
16  You should have received a copy of the GNU General Public License
17  along with Kadath. If not, see <http://www.gnu.org/licenses/>.
18 */
19 
20 #include "headcpp.hpp"
21 #include "base_spectral.hpp"
22 #include "array.hpp"
23 
24 namespace Kadath{
25 //Base_spectral::Base_spectral (int dimensions) : def{false}, ndim{dimensions}, bases_1d(ndim) {
27 // for (int i=0 ; i<ndim ; i++) bases_1d[i] = nullptr ;
28 //}
29 
30 //Base_spectral::Base_spectral(const Base_spectral& so) : def{so.def}, ndim{so.ndim}, bases_1d(ndim) {
32 // for (int l=0 ; l<ndim ; l++)
33 // bases_1d[l] = (so.bases_1d[l] == nullptr) ? nullptr : new Array<int>(*so.bases_1d[l]) ;
34 //}
35 
36 Base_spectral::Base_spectral (FILE* fd) : def{}, ndim{}, bases_1d() {
37  int indic ;
38  fread_be (&indic, sizeof(int), 1, fd) ;
39  def = (indic==0) ? true : false ;
40  fread_be (&ndim, sizeof(int), 1, fd) ;
41  bases_1d.resize(ndim) ;
42  if (def)
43  for (int i=0 ; i<ndim ; i++)
44  bases_1d[i] = new Array<int>(fd) ;
45  else
46  for (int i=0 ; i<ndim ; i++)
47  bases_1d[i] = nullptr ;
48 }
49 
50 // Base_spectral::Base_spectral(Base_spectral && so) : def{so.def}, ndim{0}, bases_1d{}
51 // {
52 // std::swap(ndim,so.ndim);
53 // std::swap(bases_1d,so.bases_1d);
54 // so.def = false;
55 // }
56 
57 // Base_spectral& Base_spectral::operator=(Base_spectral &&so)
58 // {
59 // std::swap(def,so.def);
60 // ndim = so.ndim;
61 // std::swap(bases_1d,so.bases_1d);
62 // return *this;
63 // }
64 
65 //Base_spectral::~Base_spectral() {
66 // for (int l = 0; l < ndim; l++)
67 // if (bases_1d[l] != nullptr) delete bases_1d[l];
68 //}
69 
70 
71 void Base_spectral::save (FILE* fd) const {
72  int indic = (def) ? 0 : 1 ;
73  fwrite_be (&indic, sizeof(int), 1, fd) ;
74  fwrite_be (&ndim, sizeof(int), 1, fd) ;
75  if (def)
76  for (int i=0 ; i<ndim ; i++)
77  bases_1d[i]->save(fd) ;
78 }
79 
80 
81 
82 void Base_spectral::allocate(const Dim_array& nbr_coef) {
83 
84  for (int i=0 ; i<ndim ; i++)
85  if (bases_1d[i] != nullptr) delete bases_1d[i] ;
86 
87  bases_1d[ndim-1] = new Array<int>(1) ;
88  for (int i=ndim-2 ; i>=0 ; i--) {
89  Dim_array sizes{ndim - 1 - i} ;
90  for (int k=0 ; k<ndim-1-i ; k++)
91  sizes.set(k) = nbr_coef(i+k+1) ;
92  bases_1d[i] = new Array<int>{sizes} ;
93  }
94 }
95 
96 ostream& operator<< (ostream& o, const Base_spectral& so) {
97  o << so.ndim << "-dimensional spectral base" << endl ;
98  if (so.def) {
99  for (int l=0 ; l<so.ndim ; l++) {
100  o << "Variable " << l << endl ;
101  o << *so.bases_1d[l] << endl ;
102  }
103  }
104  else
105  o << "Base not defined" << endl ;
106 return o ;
107 }
108 
109 void Base_spectral::set(Dim_array const& nbr_coefs, int BASEPHI, int BASETHETA, int BASER)
110 {
111  assert (nbr_coefs.get_ndim()==3) ;
112 
113  allocate(nbr_coefs);
114  def = true;
115  bases_1d[2]->set(0) = BASEPHI;
116  Index index(bases_1d[0]->get_dimensions());
117  for (int k(0) ; k < nbr_coefs(2) ; ++k)
118  {
119  bases_1d[1]->set(k) = BASETHETA;
120  for (int j(0) ; j < nbr_coefs(1) ; ++j)
121  {
122  index.set(0) = j ; index.set(1) = k;
123  bases_1d[0]->set(index) = BASER;
124  }
125  }
126 }
127 }
Class for storing the basis of decompositions of a field.
Base_spectral(int dimensions)
Standard constructor, the Base_spectral is not defined.
void save(FILE *) const
Saving function.
Bases_container bases_1d
Arrays containing the various basis of decomposition.
void set(Dim_array const &nbr_coefs, int basephi, int basetheta, int baser)
Allocates the various arrays, for a given number of coefficients and sets basis to some values (same ...
void allocate(const Dim_array &nbr_coefs)
Allocates the various arrays, for a given number of coefficients.
bool def
true if the Base_spectral is defined and false otherwise.
int ndim
Number of dimensions.
Class for storing the dimensions of an array.
Definition: dim_array.hpp:34
int get_ndim() const
Returns the number of dimensions.
Definition: dim_array.hpp:63
Class that gives the position inside a multi-dimensional Array.
Definition: index.hpp:38
int & set(int i)
Read/write of the position in a given dimension.
Definition: index.hpp:72