1 /*!
2  * \file   tests/Math/st2tost2/ComputeDeviatorDeterminantSecondDerivativeTest.cxx
3  * \brief
4  * \author Thomas Helfer
5  * \date   14/11/2016
6  * \copyright Copyright (C) 2006-2018 CEA/DEN, EDF R&D. All rights
7  * reserved.
8  * This project is publicly released under either the GNU GPL Licence
9  * or the CECILL-A licence. A copy of thoses licences are delivered
10  * with the sources of TFEL. CEA or EDF may also distribute this
11  * project under specific licensing conditions.
12  */
13 
14 #include<cstdlib>
15 #include<iostream>
16 
17 #include"TFEL/Tests/Test.hxx"
18 #include"TFEL/Tests/TestCase.hxx"
19 #include"TFEL/Tests/TestProxy.hxx"
20 #include"TFEL/Tests/TestManager.hxx"
21 #include"TFEL/Math/tensor.hxx"
22 #include"TFEL/Math/st2tost2.hxx"
23 
24 struct ComputeDeviatorDeterminantSecondDerivativeTest final
25   : public tfel::tests::TestCase
26 {
ComputeDeviatorDeterminantSecondDerivativeTestComputeDeviatorDeterminantSecondDerivativeTest27   ComputeDeviatorDeterminantSecondDerivativeTest()
28     : tfel::tests::TestCase("TFEL/Math","ComputeDeviatorDeterminantSecondDerivative")
29   {} // end of ComputeDeviatorDeterminantSecondDerivativeTest
30 
executeComputeDeviatorDeterminantSecondDerivativeTest31   tfel::tests::TestResult execute() override
32   {
33     const double sqrt2 = tfel::math::Cste<double>::sqrt2;
34     const double v1[6] = {8.2e-2,-4.5e-2,7.2e-2,2.3e-2*sqrt2,0,0.};
35     const double v2[6] = {-8.2e-2,4.5e-2,7.2e-2,0e-2,2.3e-2*sqrt2,0.};
36     const double v3[6] = {8.2e-2,4.5e-2,-7.2e-2,0e-2,0.e-2,2.3e-2*sqrt2};
37     const double v4[6] = {8.2e-2,4.5e-2,-7.2e-2,3.14e-2*sqrt2,-6.42e-2*sqrt2,2.3e-2*sqrt2};
38     const double v5[6]  = {1.27453824166446,0.77207083708966,0.24525337568425,
39 			  -1.7790370858361e-4,3.613971630283e-3,-1.7873236537153e-02};
40     for(const auto v: {v1,v2,v3,v4,v5}){
41       test<1u>(v);
42       test<2u>(v);
43       test<3u>(v);
44     }
45     return this->result;
46   } // end of execute
47  private:
48   template<unsigned short N>
testComputeDeviatorDeterminantSecondDerivativeTest49   void test(const double* values){
50     this->template test2<N>(values);
51     this->template test3<N>(values);
52   }
53   template<unsigned short N>
test2ComputeDeviatorDeterminantSecondDerivativeTest54   void test2(const double* values){
55     using namespace tfel::math;
56     using Stensor   = stensor<N,double>;
57     using size_type = typename stensor<N,double>::size_type;
58     const double eps  = 1.e-5;
59     const double prec = 1.e-10;
60     const Stensor s(values);
61     const auto dJ3 = computeDeviatorDeterminantSecondDerivative(s);
62     st2tost2<N,double> ndJ3;
63     for(size_type i=0;i!=s.size();++i){
64       stensor<N> s_e(s);
65       s_e[i] += eps;
66       const stensor<N> dJp = computeDeviatorDeterminantDerivative(s_e);
67       s_e[i] -= 2*eps;
68       const stensor<N> dJm = computeDeviatorDeterminantDerivative(s_e);
69       for(size_type j=0;j!=s.size();++j){
70     	ndJ3(j,i) = (dJp(j)-dJm(j))/(2*eps);
71       }
72     }
73     for(size_type i=0;i!=s.size();++i){
74       for(size_type j=0;j!=s.size();++j){
75     	TFEL_TESTS_ASSERT(std::abs(ndJ3(i,j)-dJ3(i,j))<prec);
76     	if(std::abs(ndJ3(i,j)-dJ3(i,j))>prec){
77     	  std::cout << "Error " << N << " (" << i << ", " << j << ") "
78     		    << "[" << i*StensorDimeToSize<N>::value+j << "]: "
79     		    << ndJ3(i,j) << " vs " << dJ3(i,j) << " "
80     		    << std::abs(ndJ3(i,j)-dJ3(i,j)) << std::endl;
81     	}
82       }
83     }
84   }
85   template<unsigned short N>
test3ComputeDeviatorDeterminantSecondDerivativeTest86   void test3(const double* values){
87     using namespace tfel::math;
88     using Stensor  = stensor<N,double>;
89     using Stensor4 = st2tost2<N,double>;
90     using size_type = typename stensor<N,double>::size_type;
91     const auto id = Stensor::Id();
92     const double prec = 1.e-10;
93     const stensor<N,double> s(values);
94     const auto i1 = trace(s);
95     const auto di2  = i1*id-s;
96     const auto d2i2 = (id^id)-Stensor4::Id();
97     const auto d2i3 = computeDeterminantSecondDerivative(s);
98     const auto dJ3  = computeDeviatorDeterminantSecondDerivative(s);
99     const st2tost2<N,double> dJ3_2 =
100       (4*i1/9)*(id^id)-((id^di2)+(di2^id)+i1*d2i2)/3+d2i3;
101     for(size_type i=0;i!=s.size();++i){
102       for(size_type j=0;j!=s.size();++j){
103     	TFEL_TESTS_ASSERT(std::abs(dJ3_2(i,j)-dJ3(i,j))<prec);
104     	if(std::abs(dJ3_2(i,j)-dJ3(i,j))>prec){
105     	  std::cout << "Error " << N << " (" << i << ", " << j << ") "
106     		    << "[" << i*StensorDimeToSize<N>::value+j << "]: "
107     		    << dJ3_2(i,j) << " vs " << dJ3(i,j) << " "
108     		    << std::abs(dJ3_2(i,j)-dJ3(i,j)) << std::endl;
109     	}
110       }
111     }
112    }
113 }; // end of ComputeDeviatorDeterminantSecondDerivativeTest
114 
115 TFEL_TESTS_GENERATE_PROXY(ComputeDeviatorDeterminantSecondDerivativeTest,
116 			  "ComputeDeviatorDeterminantSecondDerivativeTest");
117 
main()118 int main()
119 {
120   auto& m = tfel::tests::TestManager::getTestManager();
121   m.addTestOutput(std::cout);
122   m.addXMLTestOutput("ComputeDeviatorDeterminantSecondDerivative.xml");
123   return m.execute().success() ? EXIT_SUCCESS : EXIT_FAILURE;
124 }
125