KADATH
term_eq.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 "term_eq.hpp"
21 #include "scalar.hpp"
22 #include "tensor_impl.hpp"
23 #include "space.hpp"
24 namespace Kadath {
25 void affecte_one_dom (int, Tensor*, const Tensor*) ;
26 
27 
28 Term_eq::Term_eq (int dd, const Tensor& vx) : dom{dd}, val_d{nullptr}, der_d{nullptr}, der_t{nullptr}, type_data {TERM_T} {
29  val_t = new Tensor{vx, false} ;
30  for (int i=0 ; i<val_t->get_n_comp() ; i++) {
31  Array<int> id (val_t->indices(i)) ;
32  val_t->set(id).set_domain(dom) = vx(id)(dom) ;
33  }
34  if (vx.is_name_affected()) {
36  for (int i=0 ; i<val_t->get_valence() ; i++)
37  val_t->set_name_ind(i, vx.get_name_ind()[i]) ;
38  }
39 }
40 
41 Term_eq::Term_eq (int dd, const Tensor& vx, const Tensor& dx) :
42  dom{dd}, val_d{nullptr}, der_d{nullptr}, val_t{new Tensor{vx, false}}, der_t{new Tensor{dx, false}},
43  type_data {TERM_T}
44 {
45  for (int i=0 ; i<val_t->get_n_comp() ; i++) {
46  Array<int> id (val_t->indices(i)) ;
47  val_t->set(id).set_domain(dom) = vx(id)(dom) ;
48  }
49 
50  if (vx.is_name_affected()) {
52  for (int i=0 ; i<val_t->get_valence() ; i++)
53  val_t->set_name_ind(i, vx.get_name_ind()[i]) ;
54  }
55 
56  for (int i=0 ; i<der_t->get_n_comp() ; i++) {
57  Array<int> id (der_t->indices(i)) ;
58  der_t->set(id).set_domain(dom) = dx(id)(dom) ;
59  }
60 
61  if (dx.is_name_affected()) {
63  for (int i=0 ; i<der_t->get_valence() ; i++)
64  der_t->set_name_ind(i, dx.get_name_ind()[i]) ;
65  }
66 }
67 
68 Term_eq::Term_eq (const Term_eq& so) : dom{so.dom}, val_d{nullptr}, der_d{nullptr}, val_t{nullptr}, der_t{nullptr},
69  type_data{so.type_data} {
70 
71  if (so.val_d!=nullptr)
72  val_d = new double{*so.val_d} ;
73  if (so.der_d!=nullptr)
74  der_d = new double{*so.der_d} ;
75  if (so.val_t!=nullptr) {
76  val_t = new Tensor {*so.val_t, false} ;
77  for (int i=0 ; i<val_t->get_n_comp() ; i++) {
78  Array<int> id (val_t->indices(i)) ;
79  val_t->set(id).set_domain(dom) = (*so.val_t)(id)(dom) ;
80  }
81 
82  if (so.val_t->is_name_affected()) {
84  for (int i=0 ; i<val_t->get_valence() ; i++)
85  val_t->set_name_ind(i, so.val_t->get_name_ind()[i]) ;
86  }
87  }
88 
89  if (so.der_t!=nullptr) {
90  der_t = new Tensor {*so.der_t, false} ;
91  for (int i=0 ; i<der_t->get_n_comp() ; i++) {
92  Array<int> id (der_t->indices(i)) ;
93  der_t->set(id).set_domain(dom) = (*so.der_t)(id)(dom) ;
94  }
95 
96  if (so.der_t->is_name_affected()) {
98  for (int i=0 ; i<der_t->get_valence() ; i++)
99  der_t->set_name_ind(i, so.der_t->get_name_ind()[i]) ;
100  }
101  }
102 }
103 
105  if (val_d!=nullptr)
106  delete val_d ;
107  if (der_d!=nullptr)
108  delete der_d ;
109  if (val_t!=nullptr)
110  delete val_t ;
111  if (der_t!=nullptr)
112  delete der_t ;
113 }
114 
115 
116 void Term_eq::operator= (const Term_eq& so) {
117 
118  assert (dom==so.dom) ;
119 
120  if (type_data!=so.type_data) {
121  cerr << "Wrong type of data in Term_eq" << endl ;
122  abort() ;
123  }
124 
125  if (type_data==TERM_D) {
126  if (so.val_d!=nullptr) {
127  if (val_d!=nullptr)
128  delete val_d ;
129  val_d = new double(*so.val_d) ;
130  }
131  if (so.der_d!=nullptr) {
132  if (der_d!=nullptr)
133  delete der_d ;
134  der_d = new double(*so.der_d) ;
135  }
136  }
137 
138  if (type_data==TERM_T) {
139 
140  if (so.val_t!=nullptr) {
141  if (val_t==nullptr)
142  val_t = new Tensor(*so.val_t) ;
143  else
144  affecte_one_dom (dom, val_t, so.val_t) ;
145  }
146  if (so.der_t!=nullptr) {
147  if (der_t==nullptr)
148  der_t = new Tensor(*so.der_t) ;
149  else
150  affecte_one_dom(dom, der_t, so.der_t) ;
151  }
152  }
153 }
154 
156 #ifndef REMOVE_ALL_CHECKS
157  if (type_data!=TERM_T) {
158  cerr << "Wrong type of data in Term_eq" << endl ;
159  abort() ;
160  }
161 #endif
162  if (val_t!=nullptr)
163  delete val_t ;
164  val_t = new Tensor(so, false) ;
165  for (int i=0 ; i<val_t->get_n_comp() ; i++) {
166  Array<int> id (val_t->indices(i)) ;
167  val_t->set(id).set_domain(dom) = so(id)(dom) ;
168  }
169 }
170 
172 #ifndef REMOVE_ALL_CHECKS
173  if (type_data!=TERM_T) {
174  cerr << "Wrong type of data in Term_eq" << endl ;
175  abort() ;
176  }
177 #endif
178  if (der_t!=nullptr)
179  delete der_t ;
180  der_t = new Tensor(so, false) ;
181  for (int i=0 ; i<val_t->get_n_comp() ; i++) {
182  Array<int> id (der_t->indices(i)) ;
183  der_t->set(id).set_domain(dom) = so(id)(dom) ;
184  }
185 }
186 
188 
189  switch (type_data) {
190  case (TERM_D) :
191  if (der_d!=nullptr)
192  delete der_d ;
193  der_d = new double(0.) ;
194  break ;
195  case (TERM_T) :
196  assert (val_t!=nullptr) ;
197  if (der_t==nullptr)
198  der_t = new Tensor(*val_t, false) ;
199  for (int i=0 ; i<der_t->get_n_comp() ; i++)
201  break;
202  default :
203  cerr << "Wrong type of data in Term_eq" << endl ;
204  abort() ;
205  }
206 }
207 
208 ostream& operator<< (ostream& flux, const Term_eq& so) {
209  flux << "Data defined in domain = " << so.dom << endl ;
210  switch (so.type_data) {
211  case (TERM_D) :
212  flux << "double data" << endl ;
213  if (so.val_d !=nullptr)
214  flux << "val = " << *so.val_d << endl ;
215  else
216  flux << "val undefined" << endl ;
217  if (so.der_d !=nullptr)
218  flux << "der = " << *so.der_d << endl ;
219  else
220  flux << "der undefined" << endl ;
221  break ;
222  case (TERM_T) :
223  flux << "tensorial data" << endl ;
224  if (so.val_t !=nullptr)
225  flux << "val = " << *so.val_t << endl ;
226  else
227  flux << "val undefined" << endl ;
228  if (so.der_t !=nullptr)
229  flux << "der = " << *so.der_t << endl ;
230  else
231  flux << "der undefined" << endl ;
232  break ;
233  default:
234  cerr << "Unknown data type in Term_eq" << endl ;
235  abort() ;
236  }
237  return flux ;
238 }}
Val_domain & set_domain(int)
Read/write of a particular Val_domain.
Definition: scalar.hpp:555
Tensor handling.
Definition: tensor.hpp:149
void set_name_ind(int dd, char name)
Sets the name of one index ; the names must have been affected first.
void set_name_affected()
Affects the name of the indices.
Definition: tensor.hpp:435
Scalar & set(const Array< int > &ind)
Returns the value of a component (read/write version).
Definition: tensor_impl.hpp:91
char const * get_name_ind() const
Definition: tensor.hpp:424
int get_n_comp() const
Returns the number of stored components.
Definition: tensor.hpp:514
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...
Definition: tensor.hpp:484
int get_valence() const
Returns the valence.
Definition: tensor.hpp:509
bool is_name_affected() const
Check whether the names of the indices have been affected.
Definition: tensor.hpp:429
This class is intended to describe the manage objects appearing in the equations.
Definition: term_eq.hpp:62
Tensor * set_val_t()
Read/write accessor to the tensorial value.
Definition: term_eq.hpp:147
Tensor * der_t
Pointer on the variation, if the Term_eq is a Tensor.
Definition: term_eq.hpp:69
const int type_data
Flag describing the type of data :
Definition: term_eq.hpp:75
double * val_d
Pointer on the value, if the Term_eq is a double.
Definition: term_eq.hpp:66
Term_eq & operator=(Term_eq &&) noexcept
Move assignment operator.
Definition: term_eq.hpp:317
void set_der_zero()
Sets the variation of the approriate type to zero.
Definition: term_eq.cpp:187
const int dom
Index of the Domain where the Term_eq is defined.
Definition: term_eq.hpp:65
double * der_d
Pointer on the variation if the Term_eq is a double.
Definition: term_eq.hpp:67
Term_eq(int dom, int val)
Constructor for a double type Term_eq.
Definition: term_eq.hpp:289
~Term_eq()
Destructor.
Definition: term_eq.cpp:104
Tensor * set_der_t()
Read/write accessor to the tensorial derivative.
Definition: term_eq.hpp:151
Tensor * val_t
Pointer on the value, if the Term_eq is a Tensor.
Definition: term_eq.hpp:68
void set_zero()
Sets the Val_domain to zero (logical state to zero and arrays destroyed).
Definition: val_domain.cpp:223