MAST3
Multidisciplinary-design Adaptation and Sensitivity Toolkit (MAST)
utility.hpp
Go to the documentation of this file.
1 /*
2 * MAST: Multidisciplinary-design Adaptation and Sensitivity Toolkit
3 * Copyright (C) 2013-2020 Manav Bhatia and MAST authors
4 *
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2.1 of the License, or (at your option) any later version.
9 *
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
14 *
15 * You should have received a copy of the GNU Lesser General Public
16 * License along with this library; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18 */
19 
20 #ifndef __mast_numerics_utility_h__
21 #define __mast_numerics_utility_h__
22 
23 // C++ includes
24 #include <vector>
25 #include <type_traits>
26 
27 // MAST includes
29 #include <mast/base/exceptions.hpp>
30 
31 // libMesh includes
32 #include <libmesh/numeric_vector.h>
33 #include <libmesh/sparse_matrix.h>
34 #include <libmesh/parallel.h>
35 #include <libmesh/system.h>
36 
37 namespace MAST {
38 namespace Numerics {
39 namespace Utility {
40 
41 
42 template <typename ValType>
43 inline void
44 setZero(ValType& m) { m.setZero();}
45 
46 
47 inline void
48 setZero(libMesh::NumericVector<real_t>& v) { v.zero();}
49 
50 
51 inline void
52 setZero(libMesh::SparseMatrix<real_t>& m) { m.zero();}
53 
54 template <typename ScalarType>
55 inline void
56 setZero(std::vector<ScalarType>& v) { std::fill(v.begin(), v.end(), ScalarType());}
57 
58 
59 template <typename VecType>
60 inline typename
61 std::enable_if<std::is_same<typename Eigen::internal::traits<VecType>::Scalar,
62  real_t>::value, real_t>::type
63 real_norm(const VecType& v) {
64  return v.norm();
65 }
66 
67 
68 template <typename VecType>
69 inline typename
70 std::enable_if<std::is_same<typename Eigen::internal::traits<VecType>::Scalar,
71  complex_t>::value, real_t>::type
72 real_norm(const VecType& v) {
73  return v.norm();
74 }
75 
76 
77 #if MAST_ENABLE_ADOLC == 1
78 template <typename VecType>
79 inline typename
80 std::enable_if<std::is_same<typename Eigen::internal::traits<VecType>::Scalar,
81  adouble_tl_t>::value, real_t>::type
82 real_norm(const VecType& v) {
83  return v.norm().getValue();
84 }
85 #endif
86 
87 
91 inline real_t
92 real_minimum(const std::vector<real_t> &vec) {
93 
94  return *std::min_element(vec.begin(), vec.end());
95 }
96 
97 
101 inline complex_t
102 real_minimum(const std::vector<complex_t> &vec) {
103 
104  complex_t
105  v = vec[0];
106 
107  for (uint_t i=1; i<vec.size(); i++) {
108  if (vec[i].real() < v.real())
109  v = vec[i];
110  }
111 
112  return v;
113 }
114 
115 
116 #if MAST_ENABLE_ADOLC == 1
117 
120 inline adouble_tl_t
121 real_minimum(const std::vector<adouble_tl_t> &vec) {
122 
123  adouble_tl_t
124  v = vec[0];
125 
126  for (uint_t i=1; i<vec.size(); i++) {
127  if (vec[i] < v)
128  v = vec[i];
129  }
130 
131  return v;
132 }
133 #endif
134 
135 
136 
140 inline real_t
141 real_maximum(const std::vector<real_t> &vec) {
142 
143  return *std::max_element(vec.begin(), vec.end());
144 }
145 
146 
150 inline complex_t
151 real_maximum(const std::vector<complex_t> &vec) {
152 
153  complex_t
154  v = vec[0];
155 
156  for (uint_t i=1; i<vec.size(); i++) {
157  if (v.real() < vec[i].real())
158  v = vec[i];
159  }
160 
161  return v;
162 }
163 
164 
165 #if MAST_ENABLE_ADOLC == 1
166 
169 inline adouble_tl_t
170 real_maximum(const std::vector<adouble_tl_t> &vec) {
171 
172  adouble_tl_t
173  v = vec[0];
174 
175  for (uint_t i=1; i<vec.size(); i++) {
176  if (v < vec[i])
177  v = vec[i];
178  }
179 
180  return v;
181 }
182 #endif
183 
184 
185 
186 template <typename ScalarType>
187 inline void
188 add(std::vector<ScalarType>& v, uint_t i, ScalarType s) {
189  v[i] += s;
190 }
191 
192 
193 inline void
194 add(libMesh::NumericVector<real_t>& v, uint_t i, real_t s) {
195  v.add(i, s);
196 }
197 
198 
199 template <typename ScalarType>
200 inline void
201 add(Eigen::Matrix<ScalarType, Eigen::Dynamic, 1>& v,
202  uint_t i,
203  ScalarType s) {
204  v(i) += s;
205 }
206 
207 
208 template <typename ScalarType>
209 inline void
210 set(std::vector<ScalarType>& v, uint_t i, ScalarType s) {
211  v[i] = s;
212 }
213 
214 
215 inline void
216 set(libMesh::NumericVector<real_t>& v, uint_t i, real_t s) {
217  v.set(i, s);
218 }
219 
220 
221 template <typename ScalarType>
222 inline void
223 set(Eigen::Matrix<ScalarType, Eigen::Dynamic, 1>& v,
224  uint_t i,
225  ScalarType s) {
226  v(i) = s;
227 }
228 
229 
230 template <typename ScalarType>
231 inline ScalarType
232 get(const std::vector<ScalarType>& v, uint_t i) {
233  return v[i];
234 }
235 
236 
237 inline real_t
238 get(const libMesh::NumericVector<real_t>& v, uint_t i) {
239  return v.el(i);
240 }
241 
242 
243 template <typename ScalarType>
244 inline ScalarType
245 get(const Eigen::Matrix<ScalarType, Eigen::Dynamic, 1>& v, uint_t i) {
246  return v(i);
247 }
248 
249 
250 template <typename ValType>
251 inline void
252 finalize(ValType& m) { }
253 
254 
255 inline void
256 finalize(libMesh::NumericVector<real_t>& v) { v.close();}
257 
258 
259 inline void
260 finalize(libMesh::SparseMatrix<real_t>& m) { m.close();}
261 
262 
263 template <typename P1, int P2, typename P3>
264 inline void
265 finalize(Eigen::SparseMatrix<P1, P2, P3>& m) { m.makeCompressed();}
266 
267 
268 template <typename ScalarType>
269 inline void resize(std::vector<ScalarType>& v, uint_t n) {
270 
271  v.resize(n, ScalarType());
272 }
273 
274 
275 template <typename ScalarType>
276 inline void resize(Eigen::Matrix<ScalarType, Eigen::Dynamic, 1>& v, uint_t n) {
277 
278  v = Eigen::Matrix<ScalarType, Eigen::Dynamic, 1>::Zero(n);
279 }
280 
281 
282 template <typename ValType>
283 inline std::unique_ptr<ValType>
284 build(const libMesh::System& sys) {
285 
286  std::unique_ptr<ValType> rval(new ValType);
287  MAST::Numerics::Utility::resize(*rval, sys.n_dofs());
288 
289  return rval;
290 }
291 
292 
293 template <>
294 inline std::unique_ptr<libMesh::NumericVector<real_t>>
295 build(const libMesh::System& sys) {
296 
297  return std::unique_ptr<libMesh::NumericVector<real_t>>
298  (sys.solution->zero_clone().release());
299 }
300 
301 
302 
303 template <typename ScalarType, typename VecType>
304 inline void
305 copy(const VecType& v_from,
306  libMesh::DenseVector<ScalarType>& v_to) {
307 
308  v_to.resize(v_from.size());
309 
310  for (uint_t i=0; i<v_from.size(); i++)
311  v_to(i) = v_from(i);
312 }
313 
314 template <typename ScalarType, typename MatType>
315 inline void
316 copy(const MatType& m_from,
317  libMesh::DenseMatrix<ScalarType>& m_to) {
318 
319  m_to.resize(m_from.rows(), m_from.cols());
320 
321  for (uint_t i=0; i<m_from.cols(); i++)
322  for (uint_t j=0; j<m_from.cols(); j++)
323  m_to(i, j) = m_from(i, j);
324 }
325 
326 
327 inline void
328 comm_sum(const libMesh::Parallel::Communicator& comm,
329  real_t& v) {
330  comm.sum(v);
331 }
332 
333 
334 inline void
335 comm_sum(const libMesh::Parallel::Communicator& comm,
336  complex_t& v) {
337  real_t
338  v_re = v.real(),
339  v_im = v.imag();
340 
341  comm.sum(v_re);
342  comm.sum(v_im);
343 
344  v.real(v_re);
345  v.imag(v_im);
346 }
347 
348 
349 inline void
350 comm_sum(const libMesh::Parallel::Communicator& comm,
351  std::vector<real_t>& v) {
352  comm.sum(v);
353 }
354 
355 
356 inline void
357 comm_sum(const libMesh::Parallel::Communicator& comm,
358  std::vector<complex_t>& v) {
359 
360  std::vector<real_t>
361  v_re(v.size()),
362  v_im(v.size());
363 
364  for (uint_t i=0; i<v.size(); i++) {
365 
366  v_re[i] = v[i].real();
367  v_im[i] = v[i].imag();
368  }
369 
370  comm.sum(v_re);
371  comm.sum(v_im);
372 
373  for (uint_t i=0; i<v.size(); i++) {
374 
375  v[i].real(v_re[i]);
376  v[i].imag(v_im[i]);
377  }
378 }
379 
380 
381 inline real_t
382 comm_min(const libMesh::Parallel::Communicator& comm,
383  const std::vector<real_t>& v) {
384 
385  real_t
386  v_min = *std::min_element(v.begin(), v.end());
387 
388  comm.min(v_min);
389 
390  return v_min;
391 }
392 
393 
394 inline real_t
395 comm_min(const libMesh::Parallel::Communicator &comm,
396  real_t v) {
397 
398  comm.min(v);
399 
400  return v;
401 }
402 
403 
404 inline complex_t
405 comm_min(const libMesh::Parallel::Communicator& comm,
406  const std::vector<complex_t>& v) {
407  Error(false, "Not currently implemented for complex_t");
408  return 0.;
409 }
410 
411 
412 inline complex_t
413 comm_min(const libMesh::Parallel::Communicator &comm,
414  complex_t v) {
415 
416  Error(false, "Not currently implemented for complex_t");
417  return 0.;
418 }
419 
420 
421 inline real_t
422 comm_max(const libMesh::Parallel::Communicator& comm,
423  const std::vector<real_t>& v) {
424 
425  real_t
426  v_max = *std::max_element(v.begin(), v.end());
427 
428  comm.max(v_max);
429 
430  return v_max;
431 }
432 
433 
434 inline real_t
435 comm_max(const libMesh::Parallel::Communicator &comm,
436  real_t v) {
437 
438  comm.max(v);
439 
440  return v;
441 }
442 
443 inline complex_t
444 comm_max(const libMesh::Parallel::Communicator& comm,
445  const std::vector<complex_t>& v) {
446  Error(false, "Not currently implemented for complex_t");
447  return 0.;
448 }
449 
450 
451 inline complex_t
452 comm_max(const libMesh::Parallel::Communicator &comm,
453  complex_t v) {
454  Error(false, "Not currently implemented for complex_t");
455  return 0.;
456 }
457 
458 
459 #if MAST_ENABLE_ADOLC == 1
460 inline void
461 comm_sum(const libMesh::Parallel::Communicator& comm,
462  adouble_tl_t& v) {
463  Error(false, "Not currently implemented for adouble_tl_t");
464 }
465 
466 inline void
467 comm_sum(const libMesh::Parallel::Communicator& comm,
468  std::vector<adouble_tl_t>& v) {
469 
470  Error(false, "Not currently implemented for adouble_tl_t");
471 }
472 
473 inline adouble_tl_t
474 comm_min(const libMesh::Parallel::Communicator& comm,
475  const std::vector<adouble_tl_t>& v) {
476 
477  Error(false, "Not currently implemented for adouble_tl_t");
478  return 0.;
479 }
480 
481 inline adouble_tl_t
482 comm_min(const libMesh::Parallel::Communicator &comm,
483  adouble_tl_t v) {
484 
485  Error(false, "Not currently implemented for adouble_tl_t");
486  return 0.;
487 }
488 
489 
490 inline adouble_tl_t
491 comm_max(const libMesh::Parallel::Communicator& comm,
492  const std::vector<adouble_tl_t>& v) {
493 
494  Error(false, "Not currently implemented for adouble_tl_t");
495  return 0.;
496 }
497 
498 inline adouble_tl_t
499 comm_max(const libMesh::Parallel::Communicator &comm,
500  adouble_tl_t v) {
501 
502  Error(false, "Not currently implemented for adouble_tl_t");
503  return 0.;
504 }
505 #endif
506 
507 
508 } // namespace Utility
509 } // namespace Numerics
510 } // namespace MAST
511 
512 #endif // __mast_numerics_utility_h__
#define Error(cond, msg)
Definition: exceptions.hpp:166
std::enable_if< std::is_same< typename Eigen::internal::traits< VecType >::Scalar, real_t >::value, real_t >::type real_norm(const VecType &v)
Definition: utility.hpp:63
real_t comm_max(const libMesh::Parallel::Communicator &comm, const std::vector< real_t > &v)
Definition: utility.hpp:422
void setZero(ValType &m)
Definition: utility.hpp:44
real_t real_minimum(const std::vector< real_t > &vec)
computes minimum of vector
Definition: utility.hpp:92
void finalize(ValType &m)
Definition: utility.hpp:252
std::complex< real_t > complex_t
std::enable_if< Dim< 3, ScalarType >::typesource_load_multiplier(const SourceLoadFieldType *f, const SectionAreaType *s, ContextType &c) { Assert0(f, "Invalid pointer");Assert0(s, "Invalid pointer");return f-> value(c) *s -> value(c)
real_t real_maximum(const std::vector< real_t > &vec)
computes maximum of vector
Definition: utility.hpp:141
real_t comm_min(const libMesh::Parallel::Communicator &comm, const std::vector< real_t > &v)
Definition: utility.hpp:382
unsigned int uint_t
void add(std::vector< ScalarType > &v, uint_t i, ScalarType s)
Definition: utility.hpp:188
void comm_sum(const libMesh::Parallel::Communicator &comm, real_t &v)
Definition: utility.hpp:328
void copy(const VecType &v_from, libMesh::DenseVector< ScalarType > &v_to)
Definition: utility.hpp:305
double real_t
std::unique_ptr< ValType > build(const libMesh::System &sys)
Definition: utility.hpp:284
void resize(std::vector< ScalarType > &v, uint_t n)
Definition: utility.hpp:269