KADATH
scalar.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 "scalar.hpp"
22 #include "tensor_impl.hpp"
23 
24 namespace Kadath {
25 
26  void Scalar::save (FILE* fd) const {
27  for (int i=0 ; i<get_nbr_domains() ; i++)
28  val_zones[i]->save(fd) ;
29  }
30 
32  assert (&espace==&so.espace) ;
33  for (int i=0 ; i<ndom ; i++) {
34  if (val_zones[i] != nullptr) delete val_zones[i] ;
35  val_zones[i] = new Val_domain{*so.val_zones[i]} ;
36  }
37  return *this;
38  }
39 
41  assert (&espace==&so.espace) ;
42  assert (so.valence==0) ;
43  for (int i=0 ; i<ndom ; i++) {
44  if (val_zones[i] != nullptr) delete val_zones[i] ;
45  val_zones[i] = new Val_domain{*so.cmp[0]->val_zones[i]} ;
46  }
47  return *this;
48  }
49 
50 #ifdef TENSOR_MOVE_SEMANTIC
51  Scalar & Scalar::operator=(Tensor && so) noexcept
52  {
53  assert(so.valence==0);
54  this->do_move(std::move(so),false);
55  val_zones.swap(so.cmp[0]->val_zones);
56  so.cmp[0] = nullptr;
57  assert(cmp[0] == this);
58  return *this;
59  }
60 #endif
61 
62  double Scalar::val_point(const Point& xx, int sens) const {
63  assert ((sens==+1) || (sens==-1)) ;
64 
65  bool* inside = new bool[ndom] ;
66  for (int l=ndom-1 ; l>=0 ; l--)
67  inside[l] = get_domain(l)->is_in(xx) ;
68  // First domain in which the point is :
69  int ld = -1 ;
70  if (sens == -1) {
71  for (int l=ndom-1 ; l>=0 ; l--)
72  if ((ld==-1) && (inside[l])) ld = l ;
73  }
74  else {
75  for (int l=0 ; l<ndom ; l++)
76  if ((ld==-1) && (inside[l])) ld = l ;
77  }
78 #ifndef REMOVE_ALL_CHECKS
79  if (ld==-1) {
80  cout << "Point " << xx << "not found in the computational space..." << endl ;
81  abort() ;
82  }
83 #endif
84  if (val_zones[ld]->check_if_zero())
85  return 0. ;
86  else {
87  Point num(get_domain(ld)->absol_to_num(xx)) ;
88 
89  coef() ;
90  delete [] inside ;
91  return val_zones[ld]->base.summation(num, *val_zones[ld]->cf) ;
92  }
93 
94  }
95 
96  double Scalar::val_point_zeronotdef(const Point& xx, int sens) const {
97  assert ((sens==+1) || (sens==-1)) ;
98 
99  bool* inside = new bool[ndom] ;
100  for (int l=ndom-1 ; l>=0 ; l--)
101  inside[l] = get_domain(l)->is_in(xx) ;
102  // First domain in which the point is :
103  int ld = -1 ;
104  if (sens == -1) {
105  for (int l=ndom-1 ; l>=0 ; l--)
106  if ((ld==-1) && (inside[l])) ld = l ;
107  }
108  else {
109  for (int l=0 ; l<ndom ; l++)
110  if ((ld==-1) && (inside[l])) ld = l ;
111  }
112 
113  if (ld==-1) {
114  return 0 ;
115  }
116  else {
117  if (val_zones[ld]->check_if_zero())
118  return 0. ;
119  else {
120  Point num(get_domain(ld)->absol_to_num(xx)) ;
121 
122  coef() ;
123  delete [] inside ;
124  return val_zones[ld]->base.summation(num, *val_zones[ld]->cf) ;
125  }
126  }
127  }
128 
129 
130  void Scalar::filter_phi (int dom, int ncf) {
131  coef() ;
132  Index pcf (get_space().get_domain(dom)->get_nbr_coefs()) ;
133  int np = get_space().get_domain(dom)->get_nbr_coefs()(2) ;
134  do {
135  if (pcf(2)+ncf > np-1)
136  set_domain(dom).set_coef(pcf) = 0 ;
137  }
138  while (pcf.inc()) ;
139  }
140 
141 
142  Scalar Scalar::der_var(int var) const {
143  Scalar res (*this, false) ;
144  for (int dom=0 ; dom<ndom ; dom++)
145  res.set_domain(dom) = val_zones[dom]->der_var(var) ;
146  return res ;
147  }
148 
149  Scalar Scalar::der_abs(int var) const {
150  Scalar res (*this, false) ;
151  for (int dom=0 ; dom<ndom ; dom++)
152  res.set_domain(dom) = val_zones[dom]->der_abs(var) ;
153 
154  return res ;
155  }
156 
157  Scalar Scalar::der_spher(int var) const
158  {
159  Scalar res(*this, false) ;
160  for (int dom(0) ; dom < ndom ; ++dom)
161  res.set_domain(dom) = val_zones[dom]->der_spher(var) ;
162  return res ;
163  }
164 
166  Scalar res (*this, false) ;
167  for (int dom=0 ; dom<ndom ; dom++)
168  res.set_domain(dom) = val_zones[dom]->der_r() ;
169 
170  return res ;
171  }
172 
173  double Scalar::integrale() const {
174  double res = 0 ;
175  for (int dom=0 ; dom<ndom ; dom++)
176  res += val_zones[dom]->integrale() ;
177 
178  return res ;
179  }
180 
181  ostream& operator<< (ostream& o, const Scalar& so) {
182 
183  o << "Scalar" << endl ;
184  for (int l=0 ; l<so.ndom ; l++) {
185  o << "Domain : " << l << endl ;
186  o << *so.val_zones[l] << endl ;
187  }
188  return o ;
189  }
190 
191  double Scalar::integ_volume() const {
192  double res = 0 ;
193  for (int dom=0 ; dom<ndom ; dom++)
194  res += val_zones[dom]->integ_volume() ;
195 
196  return res ;
197  }
198 
199 }
Dim_array const & get_nbr_coefs() const
Returns the number of coefficients.
Definition: space.hpp:94
virtual bool is_in(const Point &xx, double prec=1e-13) const
Check whether a point lies inside Domain.
Definition: domain.cpp:942
Class that gives the position inside a multi-dimensional Array.
Definition: index.hpp:38
bool inc(int increm, int var=0)
Increments the position of the Index.
Definition: index.hpp:99
The class Point is used to store the coordinates of a point.
Definition: point.hpp:30
The class Scalar does not really implements scalars in the mathematical sense but rather tensorial co...
Definition: scalar.hpp:67
const Domain * get_domain(int i) const
Definition: scalar.hpp:122
Scalar der_r() const
Returns the radial derivative.
Definition: scalar.cpp:165
Val_domain & set_domain(int)
Read/write of a particular Val_domain.
Definition: scalar.hpp:555
const Space & get_space() const
Definition: scalar.hpp:126
Scalar & operator=(const Scalar &)
Assignement to another Scalar.
Definition: scalar.cpp:31
void filter_phi(int dom, int ncf)
Sets to zero all the coefficients above a given order, for the coefficients, in a gicen Domain.
Definition: scalar.cpp:130
double integrale() const
Returns the integral in the whole space.
Definition: scalar.cpp:173
Memory_mapped_array< Val_domain * > val_zones
Pointers on the various Val_domain describing the field in each Domain.
Definition: scalar.hpp:70
Scalar der_abs(int) const
Returns the derivative with respect to one particular absolute Cartesian coordinate.
Definition: scalar.cpp:149
Scalar der_spher(int) const
Returns the derivative with respect to one particular absolute Cartesian coordinate.
Definition: scalar.cpp:157
int get_nbr_domains() const
Definition: scalar.hpp:117
double val_point_zeronotdef(const Point &xxx, int sens=-1) const
Computes the value of the field at a given point, by doing the spectral summation.
Definition: scalar.cpp:96
void coef() const
Computes the coefficients.
Definition: scalar.hpp:568
double val_point(const Point &xxx, int sens=-1) const
Computes the value of the field at a given point, by doing the spectral summation.
Definition: scalar.cpp:62
double integ_volume() const
Definition: scalar.cpp:191
Scalar der_var(int) const
Returns the derivative with respect to one particular numerical coordinate.
Definition: scalar.cpp:142
virtual void save(FILE *) const
Saving function.
Definition: scalar.cpp:26
const Domain * get_domain(int i) const
returns a pointer on the domain.
Definition: space.hpp:1385
Tensor handling.
Definition: tensor.hpp:149
int ndom
The number of Domain.
Definition: tensor.hpp:155
int valence
Valence of the tensor (0 = scalar, 1 = vector, etc...)
Definition: tensor.hpp:157
Memory_mapped_array< Scalar * > cmp
Array of size n_comp of pointers onto the components.
Definition: tensor.hpp:179
const Space & espace
The Space.
Definition: tensor.hpp:154
Class for storing the basis of decompositions of a field and its values on both the configuration and...
Definition: val_domain.hpp:69
double & set_coef(const Index &pos)
Read/write the value of the field in the coefficient space.
Definition: val_domain.cpp:177