MarmotTesting.h
Go to the documentation of this file.
1 /* ---------------------------------------------------------------------
2  * _
3  * _ __ ___ __ _ _ __ _ __ ___ ___ | |_
4  * | '_ ` _ \ / _` | '__| '_ ` _ \ / _ \| __|
5  * | | | | | | (_| | | | | | | | | (_) | |_
6  * |_| |_| |_|\__,_|_| |_| |_| |_|\___/ \__|
7  *
8  * Unit of Strength of Materials and Structural Analysis
9  * University of Innsbruck,
10  * 2020 - today
11  *
12  * festigkeitslehre@uibk.ac.at
13  *
14  * Alexander Dummer alexander.dummer@uibk.ac.at
15  * Paul Hofer Paul.Hofer@uibk.ac.at
16  *
17  * This file is part of the MAteRialMOdellingToolbox (marmot).
18  *
19  * This library is free software; you can redistribute it and/or
20  * modify it under the terms of the GNU Lesser General Public
21  * License as published by the Free Software Foundation; either
22  * version 2.1 of the License, or (at your option) any later version.
23  *
24  * The full text of the license can be found in the file LICENSE.md at
25  * the top level directory of marmot.
26  * ---------------------------------------------------------------------
27  */
28 
29 #include "Marmot/MarmotConstants.h"
31 #include "autodiff/forward/dual/dual.hpp"
32 #include "unsupported/Eigen/CXX11/Tensor"
33 #include <Eigen/Core>
34 #include <Fastor/Fastor.h>
35 #include <iostream>
36 
37 namespace Marmot::Testing {
38 
39  bool checkIfEqual( const double a, const double b, const double tol = 1e-15 );
40 
41  bool checkIfEqual( const autodiff::dual a, const autodiff::dual b, const double tol = 1e-15 );
42 
43  bool checkIfEqual( const std::complex< double > a, const std::complex< double > b, const double tol = 1e-15 );
44 
45  std::string getString( const double a );
46  std::string getString( const autodiff::dual a );
47 
48  template < typename T >
49  bool checkIfEqual( const Eigen::Matrix< T, -1, -1 >& a,
50  const Eigen::Matrix< T, -1, -1 >& b,
51  const double tol = 1e-15 )
52  {
53  if ( a.rows() != b.rows() || a.cols() != b.cols() ) {
54  return false;
55  }
56  for ( int i = 0; i < a.rows(); i++ ) {
57  for ( int j = 0; j < a.cols(); j++ ) {
58  auto cond = checkIfEqual( a( i, j ), b( i, j ), tol );
59  if ( !cond ) {
60  std::cout << " -> HINT: a(" << i << "," << j << ") = " << getString( a( i, j ) ) << " != b(" << i << ","
61  << j << ") =" << getString( b( i, j ) ) << std::endl;
62  return false;
63  }
64  }
65  }
66  return true;
67  }
68 
69  template < typename T, long int... Rest >
70  bool checkIfEqual( const Eigen::TensorFixedSize< T, Eigen::Sizes< Rest... > >& a,
71  const Eigen::TensorFixedSize< T, Eigen::Sizes< Rest... > >& b,
72  const double tol = 1e-15 )
73  {
74  const T* a_data = a.data();
75  const T* b_data = b.data();
76 
77  for ( int i = 0; i < a.size(); i++ ) {
78  auto cond = checkIfEqual( a_data[i], b_data[i], tol );
79  if ( !cond ) {
80  std::cout << " -> HINT: a(" << i << ") = " << getString( a_data[i] ) << " != b(" << i
81  << ") =" << getString( b_data[i] ) << std::endl;
82  return false;
83  }
84  }
85  return true;
86  }
87 
88  template < typename T, size_t... Rest >
89  bool checkIfEqual( const Fastor::Tensor< T, Rest... >& a,
90  const Fastor::Tensor< T, Rest... >& b,
91  const double tol = 1e-15 )
92  {
93  const T* a_data = a.data();
94  const T* b_data = b.data();
95 
96  for ( size_t i = 0; i < a.size(); i++ ) {
97  auto cond = checkIfEqual( a_data[i], b_data[i], tol );
98  if ( !cond ) {
99  std::cout << " -> HINT: a(" << i << ") = " << getString( a_data[i] ) << " != b(" << i
100  << ") =" << getString( b_data[i] ) << std::endl;
101  return false;
102  }
103  }
104  return true;
105  }
106  void throwExceptionOnFailure( const bool condition, const std::string& message = "" );
107 
108  void executeTestsAndCollectExceptions( const std::vector< std::function< void() > >& testFunctions );
109 
110  template < int N >
111  Eigen::Matrix< double, N, 2 > fibonacciLatticeHemisphere()
112  {
113  double phi;
114  double theta;
115  Eigen::Matrix< double, 1, 2 > pt;
116  Eigen::Matrix< double, N, 2 > pts;
117 
118  // i1 = 1, 2, ..., N
119  for ( int i1 = 1; i1 <= N; i1++ ) {
120  theta = acos( -( i1 - 1. ) / N );
121  phi = ( i1 - 1. ) * Constants::GoldenAngle;
122 
123  pts.row( i1 - 1 ) = Eigen::Vector2d( phi, theta );
124  }
125 
126  return pts;
127  };
128 
129  bool spinTurbokreisel( const std::unique_ptr< MarmotMaterialHypoElastic >& material,
130  double* stress,
131  double* dStress_dStrain,
132  const double* dStrain,
133  const double* timeOld,
134  const double dT,
135  double& pNewDT,
136  const double stressTol = 1e-15,
137  const double stiffnessTol = 1e-15 );
138 
139 } // namespace Marmot::Testing
Marmot::FastorIndices::i
Fastor::Index< i_ > i
Definition: MarmotFastorTensorBasics.h:137
MarmotMaterialHypoElastic.h
MarmotConstants.h
Marmot::Testing
Definition: MarmotTesting.h:37
Marmot::Materials::SolidificationTheory::phi
T phi(T xi, Parameters params)
compliance of the hardened constituent
Definition: MarmotSolidification.h:85
Marmot::Constants::GoldenAngle
constexpr double GoldenAngle
Definition: MarmotConstants.h:46
Marmot::Testing::getString
std::string getString(const double a)
Marmot::Testing::checkIfEqual
bool checkIfEqual(const double a, const double b, const double tol=1e-15)
Marmot::FastorIndices::j
Fastor::Index< j_ > j
Definition: MarmotFastorTensorBasics.h:182
Marmot::Testing::spinTurbokreisel
bool spinTurbokreisel(const std::unique_ptr< MarmotMaterialHypoElastic > &material, double *stress, double *dStress_dStrain, const double *dStrain, const double *timeOld, const double dT, double &pNewDT, const double stressTol=1e-15, const double stiffnessTol=1e-15)
Marmot::FiniteElement::Spatial1D::Bar2::N
NSized N(double xi)
Definition: MarmotFiniteElement1D.cpp:15
Marmot::Testing::throwExceptionOnFailure
void throwExceptionOnFailure(const bool condition, const std::string &message="")
Marmot::Testing::fibonacciLatticeHemisphere
Eigen::Matrix< double, N, 2 > fibonacciLatticeHemisphere()
Definition: MarmotTesting.h:111
Marmot::Testing::executeTestsAndCollectExceptions
void executeTestsAndCollectExceptions(const std::vector< std::function< void() > > &testFunctions)