Permalink
Browse files

Merge branch 'real_and_imag_cf' into 'master'

add real and imag coefficientfunctions

See merge request !233
  • Loading branch information...
JSchoeberl committed Sep 27, 2017
2 parents ef89d2d + c5bfa2a commit e2b3f2167cee21c9655434c62affa1a20c33eb73
Showing with 60 additions and 1 deletion.
  1. +45 −0 fem/coefficient.cpp
  2. +5 −0 fem/coefficient.hpp
  3. +3 −1 fem/python_fem.cpp
  4. +7 −0 tests/pytest/test_coefficient.py
@@ -5146,7 +5146,52 @@ shared_ptr<CoefficientFunction> MakeCoordinateCoefficientFunction (int comp)
}
};
class RealCF : public CoefficientFunction
{
shared_ptr<CoefficientFunction> cf;
public:
RealCF(shared_ptr<CoefficientFunction> _cf) : cf(_cf), CoefficientFunction(1,false)
{ ; }
virtual double Evaluate(const BaseMappedIntegrationPoint& ip) const override
{
if(cf->IsComplex())
{
Vec<1,Complex> val;
cf->Evaluate(ip,val);
return val(0).real();
}
return cf->Evaluate(ip);
}
};
class ImagCF : public CoefficientFunction
{
shared_ptr<CoefficientFunction> cf;
public:
ImagCF(shared_ptr<CoefficientFunction> _cf) : cf(_cf), CoefficientFunction(1,false)
{ ; }
virtual double Evaluate(const BaseMappedIntegrationPoint& ip) const override
{
if(cf->IsComplex())
{
Vec<1,Complex> val;
cf->Evaluate(ip,val);
return val(0).imag();
}
throw Exception("real cf has no imag part!");
}
};
shared_ptr<CoefficientFunction> Real(shared_ptr<CoefficientFunction> cf)
{
return make_shared<RealCF>(cf);
}
shared_ptr<CoefficientFunction> Imag(shared_ptr<CoefficientFunction> cf)
{
return make_shared<ImagCF>(cf);
}
shared_ptr<CoefficientFunction> Compile (shared_ptr<CoefficientFunction> c, bool realcompile, int maxderiv, bool wait)
{
@@ -1736,6 +1736,11 @@ void ExportBinaryFunction (class pybind11::module & m, string name)
shared_ptr<CoefficientFunction> IfPos (shared_ptr<CoefficientFunction> cf_if,
shared_ptr<CoefficientFunction> cf_then,
shared_ptr<CoefficientFunction> cf_else);
NGS_DLL_HEADER
shared_ptr<CoefficientFunction> Real(shared_ptr<CoefficientFunction> cf);
NGS_DLL_HEADER
shared_ptr<CoefficientFunction> Imag(shared_ptr<CoefficientFunction> cf);
NGS_DLL_HEADER
shared_ptr<CoefficientFunction> Compile (shared_ptr<CoefficientFunction> c, bool realcompile=false, int maxderiv=2, bool wait=false);
@@ -667,6 +667,8 @@ val : can be one of the following:
return TransposeCF(coef);
},
"transpose of matrix-valued CF")
.def_property_readonly ("real", [](shared_ptr<CF> coef) { return Real(coef); }, "real part of CF")
.def_property_readonly ("imag", [](shared_ptr<CF> coef) { return Imag(coef); }, "imaginary part of CF")
.def ("Compile", [] (shared_ptr<CF> coef, bool realcompile, int maxderiv, bool wait)
{ return Compile (coef, realcompile, maxderiv, wait); },
@@ -689,7 +691,7 @@ val : can be one of the following:
ExportStdMathFunction2<GenericATan2>(m, "atan2");
ExportStdMathFunction2<GenericPow>(m, "pow");
m.def ("IfPos", [] (shared_ptr<CF> c1, py::object then_obj, py::object else_obj)
{
return IfPos(c1,
@@ -29,6 +29,12 @@ def test_mesh_size_cf():
for val in v:
assert abs(val) < 1e-14
def test_real():
mesh = Mesh(unit_square.GenerateMesh(maxh=0.2))
cf = CoefficientFunction(1+2j)
assert cf.real(mesh(0.4,0.4)) == 1
assert cf.imag(mesh(0.2,0.6)) == 2
def test_domainwise_cf():
geo = SplineGeometry()
geo.AddCircle ( (0, 0), r=1, leftdomain=1, rightdomain=2)
@@ -48,4 +54,5 @@ def test_domainwise_cf():
if __name__ == "__main__":
test_ParameterCF()
test_mesh_size_cf()
test_real()
test_domainwise_cf()

0 comments on commit e2b3f21

Please sign in to comment.