netgen/tests/pytest/test_csg2d.py

115 lines
2.6 KiB
Python
Raw Normal View History

2020-08-28 20:28:35 +05:00
from netgen.geom2d import *
import pytest
import math
from pytest import approx
2021-01-13 12:40:52 +05:00
from pytest_check import check
2020-08-28 20:28:35 +05:00
2021-01-12 22:07:58 +05:00
def check_area(geo, area):
if isinstance(geo, Solid2d):
g = CSG2d()
g.Add(geo)
geo = g
m = geo.GenerateMesh()
ngs = pytest.importorskip("ngsolve")
mesh = ngs.Mesh(m)
mesh.Curve(5)
2021-01-13 12:40:52 +05:00
with check: assert ngs.Integrate(1.0, mesh) == approx(area)
2021-01-12 22:07:58 +05:00
2020-08-28 20:28:35 +05:00
def test_two_circles():
c1 = Circle(center=(0,0), radius=1)
2020-11-06 15:51:15 +05:00
c2 = c1.Rotate(45)
2020-08-28 20:28:35 +05:00
s = c1*c2
geo = CSG2d()
geo.Add(s)
m = geo.GenerateMesh()
2020-08-28 21:35:35 +05:00
assert len(m.Elements2D()) > 0
2020-08-28 20:28:35 +05:00
ngs = pytest.importorskip("ngsolve")
mesh = ngs.Mesh(m)
mesh.Curve(5)
assert ngs.Integrate(1.0, mesh) == approx(math.pi)
2020-08-28 21:35:35 +05:00
ngs.Draw(mesh)
2020-08-28 20:28:35 +05:00
def test_two_edge():
s = Solid2d( [(-1,0), cp(0,1), (1,0), cp(0,2)] )
geo = CSG2d()
geo.Add(s)
m = geo.GenerateMesh()
2020-08-28 21:35:35 +05:00
assert len(m.Elements2D()) > 0
2020-08-28 20:28:35 +05:00
ngs = pytest.importorskip("ngsolve")
g = geo.GenerateSplineGeometry()
ngs.Draw(g)
2020-08-28 21:35:35 +05:00
mesh = ngs.Mesh(m)
mesh.Curve(5)
ngs.Draw(mesh)
def test_trig_and_circle():
g = CSG2d()
trig = Solid2d( [(0,0), (1,1), (-1,1) ] ).BC("diamond")
circle = Circle( center=(0,0.101), radius=0.1).BC("circle") # TODO: Failing with center=(0,0.1)
d = trig-circle
g.Add(d)
g.Add(circle)
m = g.GenerateMesh(maxh=0.1)
assert len(m.Elements2D()) > 0
ngs = pytest.importorskip("ngsolve")
geo = g.GenerateSplineGeometry()
ngs.Draw(geo)
mesh = ngs.Mesh(m)
mesh.Curve(3)
ngs.Draw(mesh)
2020-08-28 20:28:35 +05:00
def test_circle_plus_rect():
circle = Circle( center=(0,0), radius=1 )
rect = Rectangle( pmin=(-0.5,0.0), pmax=(0.5,0.5) )
geo = CSG2d()
geo.Add(circle+rect)
m = geo.GenerateMesh(maxh=0.2)
ngs = pytest.importorskip("ngsolve")
mesh = ngs.Mesh(m)
mesh.Curve(5)
assert ngs.Integrate(1.0, mesh) == approx(math.pi)
def test_circle_plus_rect1():
circle = Circle( center=(0,0), radius=1 )
rect = Rectangle( pmin=(-0.5,-0.5), pmax=(0.5,0.5) )
geo = CSG2d()
geo.Add(circle+rect)
m = geo.GenerateMesh(maxh=0.2)
ngs = pytest.importorskip("ngsolve")
mesh = ngs.Mesh(m)
mesh.Curve(5)
assert ngs.Integrate(1.0, mesh) == approx(math.pi)
2021-01-12 22:07:58 +05:00
def test_circle_and_rect():
c = Circle(center=(0,0),radius=1)
r = Rectangle((0,0),(1,1))
pi = math.pi
check_area(c-r, 3/4*pi)
check_area(c*r, 1/4*pi)
check_area(c+r, 3/4*pi+1)
check_area(r*c, 1/4*pi)
check_area(r+c, 3/4*pi+1)
2020-08-28 20:28:35 +05:00
if __name__ == "__main__":
test_two_circles()
test_two_edge()
2020-08-28 21:35:35 +05:00
test_trig_and_circle()