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_TermVector.cpp
19 	\author E. Lun�ville
20 	\since 5 oct 2012
21 	\date 21 aug 2013
22 
23 	Low level tests of TestVector class and related classes.
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 informations in a string
28 */
29 
30 #include "xlife++-libs.h"
31 #include "testUtils.hpp"
32 
33 using namespace xlifepp;
34 
35 namespace unit_TermVector {
36 
37 // scalar 2D function
one(const Point & P,Parameters & pa=defaultParameters)38 Real one(const Point& P, Parameters& pa = defaultParameters)
39 {
40 	return 1.;
41 }
xy(const Point & P,Parameters & pa=defaultParameters)42 Real xy(const Point& P, Parameters& pa = defaultParameters)
43 {
44 	return P(1)*P(2);
45 }
46 
x(const Point & P,Parameters & pa=defaultParameters)47 Real x(const Point& P, Parameters& pa = defaultParameters)
48 {
49 	return P(1);
50 }
51 
fn(const Point & P,Parameters & pa=defaultParameters)52 Real fn(const Point& P, Parameters& pa = defaultParameters)
53 {
54 	Vector<Real>& n = getN();  //new method
55 	return dot(P,Point(n));
56 }
57 
unit_TermVector(bool check)58 String unit_TermVector(bool check)
59 {
60   String rootname = "unit_TermVector";
61   trace_p->push(rootname);
62   std::stringstream out;                  // string stream receiving results
63   out.precision(testPrec);
64   verboseLevel(9);
65   //numberOfThreads(1);
66 
67  // create a mesh and Domains
68   std::vector<String> sidenames(4,"");
69   sidenames[0]="Gamma_1";
70   sidenames[3]="Gamma_2";
71   verboseLevel(10);
72   Mesh mesh2d(Rectangle(_xmin=0,_xmax=1,_ymin=0,_ymax=1,_nnodes=11,_side_names=sidenames),_triangle,1,_structured,"P1 mesh of [0,1]x[0,1]");
73   Domain omega=mesh2d.domain("Omega");
74   Domain gamma1=mesh2d.domain("Gamma_1");
75   Domain gamma2=mesh2d.domain("Gamma_2");
76 
77   //create Lagrange P1 space and unknown
78   Space V1(omega,_Lagrange,1,"V1",false);
79   Unknown u(V1,"u"); TestFunction v(u,"v");
80 
81   //create TermVector from linear forms
82   Function f1(one,"one");
83   Function fxy(xy,"xy");
84   TermVector un(v,omega,1.,"un");
85   LinearForm lf=intg(omega,f1*v);
86   TermVector B1(lf,"B");
87   out<<B1;
88   out<<"\n B1|un="<<realPart(B1|un)<<"\n\n";
89   std::cout<<"fin compute B1\n";
90   LinearForm lfxy=intg(omega,xy*v);
91   TermVector Bxy(lfxy,"Bxy");
92   out<<Bxy;
93   out<<"\n Bxy|un="<<realPart(Bxy|un)<<"\n\n";
94   std::cout<<"fin compute Bxy\n";
95   LinearForm lfb1=intg(gamma2,v);
96   TermVector C1(lfb1,"C1");
97   out<<C1;
98   //saveToFile("C1", C1, vtk);
99 
100   //test product
101   out<<TermVector(2*intg(omega,f1*v));
102   out<<TermVector(pi_*intg(omega,f1*v));
103   out<<TermVector(i_*intg(omega,f1*v));
104   out<<TermVector(Complex(3,0)*intg(omega,f1*v));
105   out<<TermVector(Complex(0,3)*intg(omega,f1*v));
106 
107   //create TermVector from function
108   TermVector F1(u,omega,fxy,"F1");
109   out<<"TermVector from Function"<<F1;
110 
111   //create TermVector from symbolic function
112   TermVector F2(u,omega,x_1*x_2,"F2");
113   out<<"TermVector from SymbolicFunction"<<F2;
114 
115   //create TermVector from function involving normal vector
116   //Parameters pa(0,"_n");
117   Function f_fn(fn);f_fn.requireNx=true;
118   TermVector Fn(u,gamma2,f_fn,"Fn");
119   out<<"TermVector from Function involving normal vector"<<Fn;
120 
121   //examples of inner product
122   TermVector ung2(v,gamma2,1.,"ung2");
123   TermVector ung1(v,gamma1,1.,"ung1");
124   out<<"\n C1|ung2="<<realPart(C1|ung2)<<"\n";
125   out<<"\n C1|ung1="<<realPart(C1|ung1)<<"\n";
126   out<<"\n C1|un="<<realPart(C1|un)<<"\n";
127   std::cout<<"fin compute C1\n";
128 
129   //create Lagrange P2 space and unknown
130   Interpolation* LagrangeP2=findInterpolation(Lagrange,standard,2,H1);
131   Space V2(omega,*LagrangeP2,"V2",false);
132   Unknown u2(V2,"u2");
133   TestFunction v2(u2,"v2");
134 
135   TermVector un2(v2,omega,1.,"un2");
136   LinearForm lf2=intg(omega,f1*v2);
137   TermVector B2(lf2,"B2");
138   out<<B2;
139   out<<"\n B2|un2="<<realPart(B2|un2)<<"\n\n";
140   std::cout<<"fin compute B2\n";
141 
142   LinearForm lfxy2=intg(omega,xy*v2);
143   TermVector Bxy2(lfxy2,"Bxy2");
144   out<<Bxy2;
145   out<<"\n Bxy2|un2="<<realPart(Bxy2|un2)<<"\n\n";
146   std::cout<<"fin compute Bxy2\n";
147 
148   LinearForm lfb2=intg(gamma2,v2);
149   TermVector C2(lfb2,"C2");
150   out<<C2;
151   std::cout<<"fin compute C2\n";
152 
153   //examples of inner product
154   TermVector ung22(v2,gamma2,1.,"ung22");
155   TermVector ung12(v2,gamma1,1.,"ung12");
156   out<<"\n C2|ung22="<<realPart(C2|ung22)<<"\n";
157   out<<"\n C2|ung12="<<realPart(C2|ung12)<<"\n";
158   out<<"\n C2|un2="<<realPart(C2|un2)<<"\n";
159 
160   //linear algebra on TermVector
161   TermVector t1(u, omega, xy), t2(u,gamma1,one);
162   TermVector t=t1+t2;t.name()="";
163   out<<"\n t1 = "<<t1<<"\n";
164   out<<"\n t2 = "<<t2<<"\n";
165   out<<"\n t1 + t2 = "<<t<<"\n";
166 
167   t=2*t1+Complex(0.,1.)*t2;
168   out<<"\n 2*t1+Complex(0.,1.)*t2 = "<<t<<"\n";
169 
170   t=2*t1-Complex(0.,1.)*t2;
171   out<<"\n 2*t1-Complex(0.,1.)*t2 = "<<t<<"\n";
172 
173   out<<"\n abs(t) = "<<abs(t)<<"\n";
174   out<<"\n imag(t) = "<<imag(t)<<"\n";
175   out<<"\n real(t) = "<<real(t)<<"\n";
176   out<<"\n conj(t) = "<<conj(t)<<"\n";
177 
178   Real dt=2.;
179   t=(t1-t2)/dt;
180   out<<"\n (t1-t2)/dt = "<<t<<"\n";
181   out<<"\n on the fly abs((t1-t2)/dt) = "<<abs((t1-t2)/dt)<<"\n";
182   out<<"\n on the fly out<<(t1-t2)/dt = "<<(t1-t2)/dt<<"\n";
183 
184   //interpolation of TermVector
185   Point p(0.5,0.5), q(0.11,0.33);
186   Real r=0.;
187   out<<"t1.subVector(u)(p,r) = "<<t1.subVector(u)(p,r)<<"\n";
188   out<<"t1.subVector(u)(q,r) = "<<t1.subVector(u)(q,r)<<"\n";
189   out<<"t1(p,r) = "<<t1(p,r)<<"\n";
190   out<<"t1(q,r) = "<<t1(q,r)<<"\n";
191   out<<"t1(u,p,r) = "<<t1(u,p,r)<<"\n";
192   out<<"t1(u,q,r) = "<<t1(u,q,r)<<"\n";
193 
194   //assign constant value to a part of a TermVector
195   TermVector ts(u2,omega,fxy);
196   ts.setValue(gamma1,1.);
197   ts.setValue(gamma2,2.);
198   Domain gamma=merge(gamma1,gamma2,"Gamma");
199   ts.setValue(gamma,0.);
200   ts.setValue(gamma1,x); //assign function values to a part of a TermVector
201   ts.setValue(gamma,0.);
202   TermVector tg(u2,gamma1,x);
203   ts.setValue(tg);      //assign TermVector values to a part of a TermVector
204   //saveToFile("ts",ts,vtu);
205 
206   //interpolation on a domain (TermVector::mapTo)
207   Mesh mdisk(Disk(_center = Point(0.5,0.5),_radius=0.5, _nnodes=11,_domain_name="Disk"),_triangle,1,_gmsh);
208   Domain disk=mdisk.domain("Disk");
209   Space Vd(disk,P1,"Vd"); Unknown vd(Vd,"vd");
210   TermVector td=t1.mapTo(disk,vd, false);
211   //saveToFile("t1",t1,_vtk);
212   //saveToFile("td",td,_vtk);
213 
214   //non linear operation on TermVector
215   out<<"by fun t1*t1 = "<<TermVector(t1,t1,fun_productR)<<eol;      //using explicit function
216   out<<"by symbolic function t1^2 = "<<TermVector(t1,x_1^2)<<eol;   //using symbolic function
217   out<<"t1*t1 = "<<t1*t1<<eol;                                      //using direct expression
218   TermVector st1 = TermVector(t1,sin(x_1));
219   out<<"st1 = "<<st1<<eol;
220   out<<"abs(st1+t1) = "<<TermVector(st1,t1,abs(x_1+x_2))<<eol;
221   out<<"abs(sin(t1)+t1)= "<<abs(sin(t1)+t1)<<eol;
222 
223   //linear form from TermVector
224   out<<"TermVector(intg(omega,F1*v)) : "<<TermVector(intg(omega,F1*v))<<eol;
225 
226 	//------------------------------------------------------------------------------------
227 	// save results in a file or compare results with some references value in a file
228 	//------------------------------------------------------------------------------------
229 	trace_p->pop();
230 	if (check) { return diffResFile(out, rootname); }
231 	else { return saveResToFile(out, rootname); }
232 }
233 
234 }
235