Permalink
Browse files

Merge branch 'pow_with_non_cf' into 'master'

Pow with non cf

See merge request !234
  • Loading branch information...
JSchoeberl committed Oct 17, 2017
2 parents 7c4a8ce + 0ab631a commit ef755426e8849329bfb01bd90bf6d73b9a0f8b7d
Showing with 42 additions and 0 deletions.
  1. +27 −0 fem/python_fem.cpp
  2. +15 −0 tests/pytest/test_coefficient.py
@@ -606,6 +606,33 @@ val : can be one of the following:
return c1*c2;
} )
.def ("__pow__", [] (shared_ptr<CF> c1, int p)
{
shared_ptr<CF> one = make_shared<ConstantCoefficientFunction>(1.0);
if(p==0) return one;
unsigned n = abs(p);
shared_ptr<CF> square = c1;
shared_ptr<CF> res;
// exponentiation by squaring
while(n)
{
if(n%2)
{
// check if res was not yet assigned any value
res = res ? res*square : square;
}
square = square*square;
n /= 2;
}
if(p<0)
return one/res;
else
return res;
} )
.def ("__pow__", [] (shared_ptr<CF> c1, shared_ptr<CF> c2)
{
GenericPow func;
@@ -5,6 +5,11 @@
from netgen.geom2d import SplineGeometry
def CompareCfs2D(c1, c2):
mesh = Mesh(unit_square.GenerateMesh(maxh=0.2))
c_err = c1-c2
error = Integrate(c_err*c_err, mesh)
return abs(error) < 1e-14
def test_ParameterCF():
p = Parameter(23)
@@ -35,6 +40,15 @@ def test_real():
assert cf.real(mesh(0.4,0.4)) == 1
assert cf.imag(mesh(0.2,0.6)) == 2
def test_pow():
base = (x+0.1)
for p in range(10):
c = CoefficientFunction(1.0)
for i in range(p):
c = c*base
assert CompareCfs2D(base**p,c)
assert CompareCfs2D(base**(-p),1.0/c)
def test_domainwise_cf():
geo = SplineGeometry()
geo.AddCircle ( (0, 0), r=1, leftdomain=1, rightdomain=2)
@@ -52,6 +66,7 @@ def test_domainwise_cf():
assert abs(error_true) < 1e-14
if __name__ == "__main__":
test_pow()
test_ParameterCF()
test_mesh_size_cf()
test_real()

0 comments on commit ef75542

Please sign in to comment.