1 /*
2 XLiFE++ is an extended library of finite elements written in C++
3     Copyright (C) 2014  Lunéville, Eric; Kielbasiewicz, Nicolas; Lafranche, Yvon; Nguyen, Manh-Ha; Chambeyron, Colin
4 
5     This program is free software: you can redistribute it and/or modify
6     it under the terms of the GNU General Public License as published by
7     the Free Software Foundation, either version 3 of the License, or
8     (at your option) any later version.
9     This program is distributed in the hope that it will be useful,
10     but WITHOUT ANY WARRANTY; without even the implied warranty of
11     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12     GNU General Public License for more details.
13     You should have received a copy of the GNU General Public License
14     along with this program.  If not, see <http://www.gnu.org/licenses/>.
15  */
16 
17 /*!
18   \file unit_SymbolicFunction.cpp
19   \author E. Lunéville
20   \since 11 feb 2017
21   \date 11 feb 2017
22 
23 	Low level tests of Function class.
24 	Almost functionalities are checked.
25 	This function may either creates a reference file storing the results (check=false)
26 	or compares results to those stored in the reference file (check=true)
27 	It returns reporting information in a string
28 */
29 
30 #include "xlife++-libs.h"
31 #include "testUtils.hpp"
32 
33 #include <iostream>
34 #include <fstream>
35 
36 using namespace xlifepp;
37 
38 namespace unit_SymbolicFunction {
39 
40 //============================================================================
41 // test symbolic functions
42 //============================================================================
unit_SymbolicFunction(bool check)43 String unit_SymbolicFunction(bool check)
44 {
45   String rootname = "unit_SymbolicFunction";
46   trace_p->push(rootname);
47   std::stringstream out;                  // string stream receiving results
48   out.precision(testPrec);
49 
50   //test constructors/destructor
51   SymbolicFunction fs1(x_1);
52   out<<"fs1 = "<<fs1<<eol;
53   SymbolicFunction fs2(fs1,_sqrt, 2.);
54   out<<"fs2 = "<<fs2<<eol;
55   SymbolicFunction fs3(fs1,fs2,_plus);
56   out<<"fs3 = "<<fs3<<eol;
57   SymbolicFunction fs4=fs3;
58   out<<"fs4 = "<<fs4<<eol;
59   fs1=fs4;
60   out<<"fs1=fs4, fs1 = "<<fs1<<eol;
61   SymbolicFunction* fsp= new SymbolicFunction(fs1);
62   fs2=*fsp; delete fsp;
63   out<<"copy pointer and delete pointer, fs2 = "<<fs2<<eol;
64 
65   //test syntax
66   SymbolicFunction fs;
67   fs = x_1+x_2-1;
68   out<<" fs = x_1+x_2-1; -> "<<fs <<"  fs(1,0)-f(1,0) = "<<fs(1,0)<<eol;
69 
70   fs = 2*x_1-3*x_2;
71   out<<" fs = 2*x_1-3*x_2; -> "<<fs<<"  fs(1,0)-f(1,0) = "<<fs(1,0)-2<<eol;
72 
73   fs =5*(2*x_1-3*x_2);
74   out<<" fs = 5*(2*x_1-3*x_2); -> "<<fs<<"     fs(1,0)-f(1,0) = "<<fs(1,0)-10<<eol;
75 
76   fs = 4*sqrt(2*x_1-3*x_2)/x_2;  out<<" fs = 4*sqrt(2*x_1-3*x_2)/x_2; -> "<<fs<<"  fs(2,0.5)-f(2,0.5) = "<<fs(2,0.5)-(4*std::sqrt(2.5)/0.5)<<eol;
77 
78   fs = sin(cos(sqrt(abs(log(x_1*x_1+x_2*x_2+x_3*x_3)))));
79   out<<" fs = sin(cos(sqrt(abs(log(x_1*x_1+x_2*x_2+x_3*x_3))))); -> "<<fs
80       <<"     fs(1,0,1)-f(1,0,1) = "<<fs(1,0,1)-sin(cos(sqrt(abs(log(2)))))<<eol;
81 
82   fs = (1.-sin(x_1))*(cos(1.+x_1)/5 + 2*sin(3*x_1));
83   out<<" fs = (1.-sin(x_1))*(cos(1.+x_1)/5 + 2*sin(3*x_1)); -> "<<fs
84       <<"  fs(1)-f(1) = "<<fs(1)-(1.-sin(1))*(cos(2)/5 + 2*sin(3))<<eol;
85 
86   fs = pow(squared(x_1), 0.5);
87   out<<" fs = pow(squared(x_1), 0.5); -> "<<fs<<" fs(3) - f(3) ="<<fs(3)-3<<eol;
88 
89   fs = SymbolicFunction(3);
90   out<<" fs = SymbolicFunction(3); -> "<<fs<<" fs(1) - f(1) ="<<fs(1)-3<<eol;
91 
92   fs = -sin(x_1);
93   out<<" fs = -sin(x_1); -> "<<fs<<"  fs(1) - f(1)="<<fs(1)+std::sin(1)<<eol;
94 
95   fs = +sin(-x_1);
96   out<<" fs = +sin(-x_1); -> "<<fs<<"  fs(1) - f(1) ="<<fs(1)+std::sin(1)<<eol;
97 
98   fs = cos(x_2)-2*sqrt(x_1*x_2);
99   out<<" fs = cos(x_2)-2*sqrt(x_1*x_2); -> "<<fs<<" fs(1,2) - f(1,2)="<<fs(1,2)-(std::cos(2)-2*std::sqrt(2))<<eol;
100 
101   fs= x_1 < 1;
102   out<<" fs =  x_1 < 1; -> "<<fs<<" fs(2) = "<<fs(2)<<eol;
103 
104   fs= 1 <= x_1;
105   out<<" fs = 1 <= x_1; -> "<<fs<<" fs(2) = "<<fs(2)<<eol;
106 
107   fs=(x_1+x_2)<=1 && exp(x_1)>2;
108   out<<" fs = (x_1+x_2)<1 && exp(x_1)>3; -> "<<fs<<" fs(1,0) = "<<fs(1,0)<<eol;
109 
110   fs=(x_1+x_2)^3;
111   out<<" fs = (x_1+x_2)^3; -> "<<fs<<" fs(1,2) - f(1,2) = "<<fs(1,2)-27<<eol;
112 
113 //  fs = (1-x_1+cos(x_2))^3;
114 //  out<<" fs = (1-x_1+cos(x_2))^3; -> "<<fs<<" fs(_i,0) - f(i,0) = "<<fs(i_,0)-std::pow(2-i_,3)<<eol;
115 
116   fs = (x_1<=0)*exp(x_1)+(x_1>0);
117   out<<" fs = (x_1<=0)*exp(x_1)+(x_1>0); -> "<<fs<<" fs(1) - f(1) = "<<fs(1)-1<<" fs(-1) - f(-1) = "<<fs(-1)-std::exp(-1)<<eol;
118 
119   //------------------------------------------------------------------------------------
120   // save results in a file or compare results with some references value in a file
121   //------------------------------------------------------------------------------------
122   trace_p->pop();
123   if (check) { return diffResFile(out, rootname); }
124   else { return saveResToFile(out, rootname); }
125 }
126 
127 }
128