File: test_difference.py

package info (click to toggle)
python-dmsh 0.2.19-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 480 kB
  • sloc: python: 1,893; makefile: 5
file content (79 lines) | stat: -rw-r--r-- 2,263 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
import numpy as np
from helpers import assert_norm_equality

import dmsh


def generate_difference(show=False):
    geo = dmsh.Circle([-0.5, 0.0], 1.0) - dmsh.Circle([+0.5, 0.0], 1.0)
    X, cells = dmsh.generate(geo, 0.1, show=show, max_steps=100)
    geo.plot()
    return X, cells


def test_difference(show=False):
    X, cells = generate_difference(show)

    ref_norms = [2.9409044729708609e02, 1.5855488859739937e01, 1.5000000000000000e00]
    assert_norm_equality(X.flatten(), ref_norms, 1.0e-6)


def test_boundary_step():
    geo = dmsh.Difference(dmsh.Circle([-0.5, 0.0], 1.0), dmsh.Circle([+0.5, 0.0], 1.0))
    pts = np.array(
        [
            [-2.1, 0.0],
            [0.1, 0.0],
            [-1.4, 0.0],
            [-0.6, 0.0],
        ]
    )
    pts = geo.boundary_step(pts.T).T
    ref = np.array([[-1.5, 0.0], [-0.5, 0.0], [-1.5, 0.0], [-0.5, 0.0]])
    assert np.all(np.abs(pts - ref) < 1.0e-10)


def test_boundary_step2():
    geo = dmsh.Difference(dmsh.Circle([-0.5, 0.0], 1.0), dmsh.Circle([+0.5, 0.0], 1.0))
    np.random.seed(0)
    pts = np.random.uniform(-2.0, 2.0, (2, 100))
    pts = geo.boundary_step(pts)
    # geo.plot()
    # import matplotlib.pyplot as plt
    # plt.plot(pts[0], pts[1], "xk")
    # plt.show()
    assert np.all(np.abs(geo.dist(pts)) < 1.0e-12)


def test_boundary_step_pacman():
    geo = dmsh.Difference(
        dmsh.Circle([0.0, 0.0], 1.0),
        dmsh.Polygon([[0.0, 0.0], [1.5, 0.4], [1.5, -0.4]]),
    )
    # np.random.seed(0)
    # pts = np.random.uniform(-2.0, 2.0, (2, 100))
    # pts = np.array([[-2.0, 0.0]])
    # pts = np.array([[-0.1, 0.0]])
    # pts = np.array([[0.0, 2.0]])
    # pts = np.array([[0.0, 0.9]])
    # pts = np.array([[2.0, 0.1]])
    # pts = np.array([[0.1, 0.1]])
    # pts = np.array([[0.7, 0.1]])
    pts = np.array([[0.5, 0.1]])
    pts = pts.T
    print(pts.T.shape)
    pts = geo.boundary_step(pts)
    geo.plot()
    import matplotlib.pyplot as plt

    plt.plot(pts[0], pts[1], "xk")
    plt.show()
    # assert np.all(np.abs(geo.dist(pts)) < 1.0e-12)


if __name__ == "__main__":
    # from helpers import save
    test_difference(show=False)
    X, cells = generate_difference(show=True)
    # save("difference.png", X, cells)
    # test_boundary_step_pacman()