20 #ifndef __mast_numerics_utility_h__ 21 #define __mast_numerics_utility_h__ 25 #include <type_traits> 32 #include <libmesh/numeric_vector.h> 33 #include <libmesh/sparse_matrix.h> 34 #include <libmesh/parallel.h> 35 #include <libmesh/system.h> 42 template <
typename ValType>
48 setZero(libMesh::NumericVector<real_t>& v) { v.zero();}
52 setZero(libMesh::SparseMatrix<real_t>& m) { m.zero();}
54 template <
typename ScalarType>
56 setZero(std::vector<ScalarType>& v) { std::fill(v.begin(), v.end(), ScalarType());}
59 template <
typename VecType>
61 std::enable_if<std::is_same<typename Eigen::internal::traits<VecType>::Scalar,
68 template <
typename VecType>
70 std::enable_if<std::is_same<typename Eigen::internal::traits<VecType>::Scalar,
77 #if MAST_ENABLE_ADOLC == 1 78 template <
typename VecType>
80 std::enable_if<std::is_same<typename Eigen::internal::traits<VecType>::Scalar,
83 return v.norm().getValue();
94 return *std::min_element(vec.begin(), vec.end());
107 for (
uint_t i=1; i<vec.size(); i++) {
108 if (vec[i].real() < v.real())
116 #if MAST_ENABLE_ADOLC == 1 126 for (
uint_t i=1; i<vec.size(); i++) {
143 return *std::max_element(vec.begin(), vec.end());
156 for (
uint_t i=1; i<vec.size(); i++) {
157 if (v.real() < vec[i].real())
165 #if MAST_ENABLE_ADOLC == 1 175 for (
uint_t i=1; i<vec.size(); i++) {
186 template <
typename ScalarType>
188 add(std::vector<ScalarType>& v,
uint_t i, ScalarType s) {
199 template <
typename ScalarType>
201 add(Eigen::Matrix<ScalarType, Eigen::Dynamic, 1>& v,
208 template <
typename ScalarType>
210 set(std::vector<ScalarType>& v,
uint_t i, ScalarType s) {
221 template <
typename ScalarType>
223 set(Eigen::Matrix<ScalarType, Eigen::Dynamic, 1>& v,
230 template <
typename ScalarType>
232 get(
const std::vector<ScalarType>& v,
uint_t i) {
238 get(
const libMesh::NumericVector<real_t>& v,
uint_t i) {
243 template <
typename ScalarType>
245 get(
const Eigen::Matrix<ScalarType, Eigen::Dynamic, 1>& v,
uint_t i) {
250 template <
typename ValType>
256 finalize(libMesh::NumericVector<real_t>& v) { v.close();}
260 finalize(libMesh::SparseMatrix<real_t>& m) { m.close();}
263 template <
typename P1,
int P2,
typename P3>
265 finalize(Eigen::SparseMatrix<P1, P2, P3>& m) { m.makeCompressed();}
268 template <
typename ScalarType>
271 v.resize(n, ScalarType());
275 template <
typename ScalarType>
276 inline void resize(Eigen::Matrix<ScalarType, Eigen::Dynamic, 1>& v,
uint_t n) {
278 v = Eigen::Matrix<ScalarType, Eigen::Dynamic, 1>::Zero(n);
282 template <
typename ValType>
283 inline std::unique_ptr<ValType>
286 std::unique_ptr<ValType> rval(
new ValType);
294 inline std::unique_ptr<libMesh::NumericVector<real_t>>
297 return std::unique_ptr<libMesh::NumericVector<real_t>>
298 (sys.solution->zero_clone().release());
303 template <
typename ScalarType,
typename VecType>
306 libMesh::DenseVector<ScalarType>& v_to) {
308 v_to.resize(v_from.size());
310 for (
uint_t i=0; i<v_from.size(); i++)
314 template <
typename ScalarType,
typename MatType>
317 libMesh::DenseMatrix<ScalarType>& m_to) {
319 m_to.resize(m_from.rows(), m_from.cols());
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);
328 comm_sum(
const libMesh::Parallel::Communicator& comm,
335 comm_sum(
const libMesh::Parallel::Communicator& comm,
350 comm_sum(
const libMesh::Parallel::Communicator& comm,
351 std::vector<real_t>& v) {
357 comm_sum(
const libMesh::Parallel::Communicator& comm,
358 std::vector<complex_t>& v) {
364 for (
uint_t i=0; i<v.size(); i++) {
366 v_re[i] = v[i].real();
367 v_im[i] = v[i].imag();
373 for (
uint_t i=0; i<v.size(); i++) {
382 comm_min(
const libMesh::Parallel::Communicator& comm,
383 const std::vector<real_t>& v) {
386 v_min = *std::min_element(v.begin(), v.end());
395 comm_min(
const libMesh::Parallel::Communicator &comm,
405 comm_min(
const libMesh::Parallel::Communicator& comm,
406 const std::vector<complex_t>& v) {
407 Error(
false,
"Not currently implemented for complex_t");
413 comm_min(
const libMesh::Parallel::Communicator &comm,
416 Error(
false,
"Not currently implemented for complex_t");
422 comm_max(
const libMesh::Parallel::Communicator& comm,
423 const std::vector<real_t>& v) {
426 v_max = *std::max_element(v.begin(), v.end());
435 comm_max(
const libMesh::Parallel::Communicator &comm,
444 comm_max(
const libMesh::Parallel::Communicator& comm,
445 const std::vector<complex_t>& v) {
446 Error(
false,
"Not currently implemented for complex_t");
452 comm_max(
const libMesh::Parallel::Communicator &comm,
454 Error(
false,
"Not currently implemented for complex_t");
459 #if MAST_ENABLE_ADOLC == 1 461 comm_sum(
const libMesh::Parallel::Communicator& comm,
463 Error(
false,
"Not currently implemented for adouble_tl_t");
467 comm_sum(
const libMesh::Parallel::Communicator& comm,
468 std::vector<adouble_tl_t>& v) {
470 Error(
false,
"Not currently implemented for adouble_tl_t");
474 comm_min(
const libMesh::Parallel::Communicator& comm,
475 const std::vector<adouble_tl_t>& v) {
477 Error(
false,
"Not currently implemented for adouble_tl_t");
482 comm_min(
const libMesh::Parallel::Communicator &comm,
485 Error(
false,
"Not currently implemented for adouble_tl_t");
491 comm_max(
const libMesh::Parallel::Communicator& comm,
492 const std::vector<adouble_tl_t>& v) {
494 Error(
false,
"Not currently implemented for adouble_tl_t");
499 comm_max(
const libMesh::Parallel::Communicator &comm,
502 Error(
false,
"Not currently implemented for adouble_tl_t");
512 #endif // __mast_numerics_utility_h__
std::enable_if< std::is_same< typename Eigen::internal::traits< VecType >::Scalar, real_t >::value, real_t >::type real_norm(const VecType &v)
real_t comm_max(const libMesh::Parallel::Communicator &comm, const std::vector< real_t > &v)
real_t real_minimum(const std::vector< real_t > &vec)
computes minimum of vector
void finalize(ValType &m)
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
real_t comm_min(const libMesh::Parallel::Communicator &comm, const std::vector< real_t > &v)
void add(std::vector< ScalarType > &v, uint_t i, ScalarType s)
void comm_sum(const libMesh::Parallel::Communicator &comm, real_t &v)
void copy(const VecType &v_from, libMesh::DenseVector< ScalarType > &v_to)
std::unique_ptr< ValType > build(const libMesh::System &sys)
void resize(std::vector< ScalarType > &v, uint_t n)