File: test_point.py

package info (click to toggle)
dolfin 2019.2.0~legacy20240219.1c52e83-18
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 31,700 kB
  • sloc: xml: 104,040; cpp: 102,227; python: 24,356; sh: 460; makefile: 330; javascript: 226
file content (96 lines) | stat: -rw-r--r-- 2,388 bytes parent folder | download | duplicates (4)
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
"""Unit tests for Point interface"""

# Copyright (C) 2017 Jan Blechta
#
# This file is part of DOLFIN.
#
# DOLFIN is free software: you can redistribute it and/or modify
# it under the terms of the GNU Lesser General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# DOLFIN is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public License
# along with DOLFIN. If not, see <http://www.gnu.org/licenses/>.

import pytest
from pytest import approx
import numpy as np

from dolfin import *


def test_point_getitem():
    p = Point(1, 2, 3)
    assert p[0] == approx(1.0)
    assert p[1] == approx(2.0)
    assert p[2] == approx(3.0)
    with pytest.raises(IndexError):
        p[3]
    assert np.all(p[:] == approx(np.array((1.0, 2.0, 3.0))))


def test_point_setitem():
    p = Point()

    p[0] = 6.0
    assert p[0] == approx(6.0)

    p[1] = 16.0
    p[1] += 600.0
    assert np.isclose(p[1], 616.0)

    p[2] = 111.0
    p[2] *= 12.0
    p[2] /= 2
    assert np.isclose(p[2], 666.0)

    with pytest.raises(IndexError):
        p[3] = 6666.0

    p[:] = (0, 0, 0)
    assert np.all(p[:] == 0)

    p[:] = (1, 2, 3)
    assert np.all(p[:] == (1, 2, 3))

    p[:] += np.array((1, 2, 3))
    assert np.all(p[:] == (2, 4, 6))

    p[:] /= 2
    assert np.all(p[:] == (1, 2, 3))

    p[:] *= np.array((2., 2., 2.))
    assert np.all(p[:] == (2, 4, 6))


def test_point_array():
    p = Point(1, 2, 3)
    assert np.all(p.array() == (1, 2, 3))

    # Point.array() is a copy, no in-place modification
    p.array()[:] += 1000.0
    assert np.all(p.array() == (1, 2, 3))


def test_point_equality():
    p = Point(1.23, 2, DOLFIN_PI)
    q = Point(1.23, 2, DOLFIN_PI)
    r = Point(1.23+DOLFIN_EPS, 2, DOLFIN_PI)
    assert p == q
    assert p != r


def test_point_dot():
    p = Point(1.0, 2.0, 3.0)
    q = Point(3.1, 4.5, 5.6)
    r = Point(-1.6, -2.5, 3.3)
    s = Point(152.25)

    assert p.dot(q) == approx(p[0]*q[0] + p[1]*q[1] + p[2]*q[2])
    assert p.dot(r) == approx(p[0]*r[0] + p[1]*r[1] + p[2]*r[2])
    assert p.dot(s) == approx(p[0]*s[0])