/*
XLiFE++ is an extended library of finite elements written in C++
Copyright (C) 2014 Lunéville, Eric; Kielbasiewicz, Nicolas; Lafranche, Yvon; Nguyen, Manh-Ha; Chambeyron, Colin
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see .
*/
/*!
\file unit_operator.cpp
\author E. Lunéville
\since 23 feb 2012
\date 11 may 2012
Low level tests of Operator class and related classes.
Almost functionalities are checked.
This function may either creates a reference file storing the results (check=false)
or compares results to those stored in the reference file (check=true)
It returns reporting informations in a string
*/
#include "xlife++-libs.h"
#include "testUtils.hpp"
#include
#include
using namespace xlifepp;
namespace unit_operator {
// scalar spectral function
Real sinBasis(const Point& P, Parameters& pa = defaultParameters)
{
Real x = P.x();
Real h = pa("h"); // get the parameter h (user definition)
Int n = pa("basis index"); // get the index of function to compute
return std::sqrt(2. / h) * std::sin(n * pi_ * x / h); //computation
}
//vector spectral function
Vector VecBasis(const Point& P, Parameters& pa = defaultParameters)
{
Real x = P.x();
Real h = pa("h"); // get the parameter h (user definition)
Int n = pa("basis index"); // get the index of function to compute
Vector res(2);
res(1) = std::sqrt(2. / h) * std::sin(n * pi_ * x / h); // computation
res(2) = std::sqrt(2. / h) * std::cos(n * pi_ * x / h);
return res;
}
//vector spectral function
Vector VecBasis3(const Point& P, Parameters& pa = defaultParameters)
{
Real x = P.x();
Real h = pa("h"); // get the parameter h (user definition)
Int n = pa("basis index"); // get the index of function to compute
Vector res(3);
res(1) = std::sqrt(2. / h) * std::sin(n * pi_ * x / h);
res(2) = std::sqrt(2. / h) * std::cos(n * pi_ * x / h);
res(3) = Complex(0,1.);
return res;
}
Real scalF(const Point& P, Parameters& pa = defaultParameters)
{
Real x = P.x();
return x; //computation
}
Vector VecF(const Point& P, Parameters& pa = defaultParameters)
{
return Vector(1,0.);
}
// matrix function
Matrix MatF(const Point& P, Parameters& pa = defaultParameters)
{
Matrix res(2, 2);
Real x = P.x();
res(1, 1) = x; res(1, 2) = 0.;
res(2, 1) = 0.5; res(2, 2) = 2 * x;
return res;
}
Matrix MatFc(const Point& P, Parameters& pa = defaultParameters)
{
Matrix res(3, 3);
Real x = P.x();
res(1, 1) = Complex(0,1); res(2, 2) = x; res(3, 3) = 1.;
return res;
}
// scalar kernel function
Real G(const Point& M, const Point& P, Parameters& pa = defaultParameters)
{
// Real r = M.distance(P);
return 1.; // computation
}
String unit_operator(bool check)
{
String rootname = "unit_operator";
trace_p->push(rootname);
std::stringstream out; // string stream receiving results
out.precision(testPrec);
// test of DifferentialOperator
DifferentialOperator* do1_p = findDifferentialOperator(_id);
out << "difop id : name=" << do1_p->name() << " order=" << do1_p->order()
<< " normal=" << words(do1_p->normalRequired())
<< " extension=" << words(do1_p->extensionRequired()) << "\n";
findDifferentialOperator(_dt);
findDifferentialOperator(_dx);
findDifferentialOperator(_dy);
findDifferentialOperator(_dz);
findDifferentialOperator(_grad);
findDifferentialOperator(_div);
findDifferentialOperator(_curl);
findDifferentialOperator(_gradS);
findDifferentialOperator(_gradG);
findDifferentialOperator(_divS);
findDifferentialOperator(_divG);
findDifferentialOperator(_curlS);
findDifferentialOperator(_curlG);
findDifferentialOperator(_epsilon);
findDifferentialOperator(_epsilonG);
findDifferentialOperator(_ntimes);
findDifferentialOperator(_timesn);
findDifferentialOperator(_ncrossntimes);
findDifferentialOperator(_timesncrossn);
findDifferentialOperator(_timesn);
findDifferentialOperator(_ndot);
findDifferentialOperator(_ndotgrad);
findDifferentialOperator(_ndiv);
findDifferentialOperator(_ncross);
findDifferentialOperator(_ncrosscurl);
findDifferentialOperator(_ncrossncross);
verboseLevel(2);
printListDiffOp(out);
// test of Value
Value::printValueTypeRTINames(out);
Real s = 1.;
Vector vr(3); Vector vc(3, i_);
Matrix mr(3, 3, 1.); Matrix mc(3, 3, i_);
Value Vs(s); out << "real Value Vs(s) :" << Vs;
Value Vi(i_); out << "complex Value Vi(i) :" << Vi;
Value Vvr(vr); out << "real vector Value Vvr(vr) :" << Vvr;
Value Vvc(vc); out << "complex vector Value Vvc(vc) :" << Vvc;
Value Vmr(mr); out << "real matrix Value Vmr(mr) :" << Vmr;
Value Vmc(mc); out << "complex matrix Value Vmc(mc) :" << Vmc;
out << "get real Value s= " << Vs.value() << "\n";
out << "get complex Value i= " << Vi.value() << "\n";
out << "get real vector Value vr= " << Vvr.value >() << "\n";
out << "get complex vector Value vc= " << Vvc.value >() << "\n";
out << "get real matrix Value mr= " << Vmr.value >() << "\n";
out << "get complex matrix Value mc= " << Vmc.value >() << "\n";
// test of OperatorOnUnknown
Mesh msh; //fake mesh
GeomDomain Omega(msh, "Omega", 3);
Parameters ps;
ps< v_val, n(2); n[0]=1.; n[1]=1.;
out << "\ntest id(F)(M) : " << id(F).eval(M,val);
out << "\ntest id(VF)(M) : " << id(VF).eval(M,v_val);
out << "\ntest (VF|_n)(M) : " << (VF|_n).eval(M,val,&n);
out << "\ntest (_n^VF)(M) : " << (_n^VF).eval(M,val,&n); //2D ->scalar result
out << "\ntest (MatF*_n)(M) : " << (MatF*_n).eval(M,v_val,&n);
out << "\ntest (_n*MatF)(M) : " << (_n*MatF).eval(M,v_val,&n);
Matrix A(2,2);A(1,1)=10;
out << "\ntest (A*_n)(M) : " << (A*_n).eval(M,v_val,&n);
Function H(VecBasis3, "(sin,cos,1)", ps);
n.resize(3);n[2]=0.;
Complex cval;
Vector cv_val;
out << "\ntest id(H)(M) : " << id(H).eval(M,cv_val);
out << "\ntest (H|_n)(M) : " << (H|_n).eval(M,cval,&n);
out << "\ntest (_n^H)(M) : " << (_n^H).eval(M,cv_val,&n);
out << "\ntest (MatFc*_n)(M) : " << (MatFc*_n).eval(M,cv_val,&n);
//test operator involving TermVector
verboseLevel(25);
Mesh msq(Square(_origin=Point(0.,0.),_length=1.,_nnodes=5,_domain_name="Omega"),_triangle,1,_structured,"msq");
Domain omega=msq.domain("Omega");
Space Vh(omega,P1,"Vh"); Unknown uh(Vh,"uh");
TermVector x1(uh,omega,_x1,"x1");
out << "test x1*uh : " << x1*uh;
out << "test x1*uh*x1 : " << x1*uh*x1;
out << "test (x1^3)*uh : " << (x1^3)*uh;
out << "test intg(omega,(x1^2)*uh) : " << intg(omega,(x1^2)*uh);
//------------------------------------------------------------------------------------
// save results in a file or compare results with some references value in a file
//------------------------------------------------------------------------------------
trace_p->pop();
if (check) { return diffResFile(out, rootname); }
else { return saveResToFile(out, rootname); }
}
}