File: test_csg2d.py

package info (click to toggle)
netgen 6.2.2501%2Bdfsg1-12
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 12,980 kB
  • sloc: cpp: 165,197; tcl: 6,310; python: 2,804; sh: 522; makefile: 87
file content (114 lines) | stat: -rw-r--r-- 2,623 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
from netgen.geom2d import *
import pytest
import math
from pytest import approx


def check_area(geo, area):
    if isinstance(geo, Solid2d):
        g = CSG2d()
        g.Add(geo)
        geo = g

    m = geo.GenerateMesh(maxh=0.2)
    ngs = pytest.importorskip("ngsolve")
    mesh = ngs.Mesh(m)
    mesh.Curve(5)
    assert ngs.Integrate(1.0, mesh) == approx(area)

def test_two_circles():
    c1 = Circle(center=(0,0), radius=1)
    c2 = c1.Rotate(45)
    s = c1*c2
    geo = CSG2d()
    geo.Add(s)
    m = geo.GenerateMesh()
    assert len(m.Elements2D()) > 0

    ngs = pytest.importorskip("ngsolve")
    mesh = ngs.Mesh(m)
    mesh.Curve(5)
    assert ngs.Integrate(1.0, mesh) == approx(math.pi)
    ngs.Draw(mesh)

def test_two_edge():
    s = Solid2d( [(-1,0), cp(0,1), (1,0), cp(0,2)] )
    geo = CSG2d()
    geo.Add(s)
    m = geo.GenerateMesh()
    assert len(m.Elements2D()) > 0

    ngs = pytest.importorskip("ngsolve")
    g = geo.GenerateSplineGeometry()
    ngs.Draw(g)
    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)


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)

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)
    check_area(r-c, 1-1/4*pi)


if __name__ == "__main__":
    test_two_circles()
    test_two_edge()
    test_trig_and_circle()