xref: /libCEED/python/tests/test-2-elemrestriction.py (revision 0b454692e1b44187f5e3492150f1dd8bba6f5f17)
1# Copyright (c) 2017, Lawrence Livermore National Security, LLC. Produced at
2# the Lawrence Livermore National Laboratory. LLNL-CODE-734707. All Rights
3# reserved. See files LICENSE and NOTICE for details.
4#
5# This file is part of CEED, a collection of benchmarks, miniapps, software
6# libraries and APIs for efficient high-order finite element and spectral
7# element discretizations for exascale applications. For more information and
8# source code availability see http://github.com/ceed.
9#
10# The CEED research is supported by the Exascale Computing Project 17-SC-20-SC,
11# a collaborative effort of two U.S. Department of Energy organizations (Office
12# of Science and the National Nuclear Security Administration) responsible for
13# the planning and preparation of a capable exascale ecosystem, including
14# software, applications, hardware, advanced system engineering and early
15# testbed platforms, in support of the nation's exascale computing imperative.
16
17# @file
18# Test Ceed ElemRestriction functionality
19
20import os
21import libceed
22import numpy as np
23import check
24
25# -------------------------------------------------------------------------------
26# Test creation, use, and destruction of an element restriction
27# -------------------------------------------------------------------------------
28
29
30def test_200(ceed_resource):
31    ceed = libceed.Ceed(ceed_resource)
32
33    ne = 3
34
35    x = ceed.Vector(ne + 1)
36    a = np.arange(10, 10 + ne + 1, dtype="float64")
37    x.set_array(a, cmode=libceed.USE_POINTER)
38
39    ind = np.zeros(2 * ne, dtype="int32")
40    for i in range(ne):
41        ind[2 * i + 0] = i
42        ind[2 * i + 1] = i + 1
43    r = ceed.ElemRestriction(ne, 2, 1, 1, ne + 1, ind,
44                             cmode=libceed.USE_POINTER)
45
46    y = ceed.Vector(2 * ne)
47    y.set_value(0)
48
49    r.apply(x, y)
50
51    with y.array_read() as y_array:
52        for i in range(2 * ne):
53            assert 10 + (i + 1) // 2 == y_array[i]
54
55# -------------------------------------------------------------------------------
56# Test creation, use, and destruction of a strided element restriction
57# -------------------------------------------------------------------------------
58
59
60def test_201(ceed_resource):
61    ceed = libceed.Ceed(ceed_resource)
62
63    ne = 3
64
65    x = ceed.Vector(2 * ne)
66    a = np.arange(10, 10 + 2 * ne, dtype="float64")
67    x.set_array(a, cmode=libceed.USE_POINTER)
68
69    strides = np.array([1, 2, 2], dtype="int32")
70    r = ceed.StridedElemRestriction(ne, 2, 1, 2 * ne, strides)
71
72    y = ceed.Vector(2 * ne)
73    y.set_value(0)
74
75    r.apply(x, y)
76
77    with y.array_read() as y_array:
78        for i in range(2 * ne):
79            assert 10 + i == y_array[i]
80
81# -------------------------------------------------------------------------------
82# Test creation and destruction of a blocked element restriction
83# -------------------------------------------------------------------------------
84
85
86def test_202(ceed_resource, capsys):
87    ceed = libceed.Ceed(ceed_resource)
88
89    ne = 8
90    blksize = 5
91
92    x = ceed.Vector(ne + 1)
93    a = np.arange(10, 10 + ne + 1, dtype="float64")
94    x.set_array(a, cmode=libceed.USE_POINTER)
95
96    ind = np.zeros(2 * ne, dtype="int32")
97    for i in range(ne):
98        ind[2 * i + 0] = i
99        ind[2 * i + 1] = i + 1
100    r = ceed.BlockedElemRestriction(ne, 2, blksize, 1, 1, ne + 1, ind,
101                                    cmode=libceed.USE_POINTER)
102
103    y = ceed.Vector(2 * blksize * 2)
104    y.set_value(0)
105
106    r.apply(x, y)
107
108    print(y)
109
110    x.set_value(0)
111    r.T.apply(y, x)
112    print(x)
113
114    stdout, stderr, ref_stdout = check.output(capsys)
115    assert not stderr
116    assert stdout == ref_stdout
117
118# -------------------------------------------------------------------------------
119# Test creation, use, and destruction of a blocked element restriction
120# -------------------------------------------------------------------------------
121
122
123def test_208(ceed_resource, capsys):
124    ceed = libceed.Ceed(ceed_resource)
125
126    ne = 8
127    blksize = 5
128
129    x = ceed.Vector(ne + 1)
130    a = np.arange(10, 10 + ne + 1, dtype="float64")
131    x.set_array(a, cmode=libceed.USE_POINTER)
132
133    ind = np.zeros(2 * ne, dtype="int32")
134    for i in range(ne):
135        ind[2 * i + 0] = i
136        ind[2 * i + 1] = i + 1
137    r = ceed.BlockedElemRestriction(ne, 2, blksize, 1, 1, ne + 1, ind,
138                                    cmode=libceed.USE_POINTER)
139
140    y = ceed.Vector(blksize * 2)
141    y.set_value(0)
142
143    r.apply_block(1, x, y)
144
145    print(y)
146
147    x.set_value(0)
148    r.T.apply_block(1, y, x)
149    print(x)
150
151    stdout, stderr, ref_stdout = check.output(capsys)
152    assert not stderr
153    assert stdout == ref_stdout
154
155# -------------------------------------------------------------------------------
156# Test getting the multiplicity of the indices in an element restriction
157# -------------------------------------------------------------------------------
158
159
160def test_209(ceed_resource):
161    ceed = libceed.Ceed(ceed_resource)
162
163    ne = 3
164
165    ind = np.zeros(4 * ne, dtype="int32")
166    for i in range(ne):
167        ind[4 * i + 0] = i * 3 + 0
168        ind[4 * i + 1] = i * 3 + 1
169        ind[4 * i + 2] = i * 3 + 2
170        ind[4 * i + 3] = i * 3 + 3
171    r = ceed.ElemRestriction(ne, 4, 1, 1, 3 * ne + 1, ind,
172                             cmode=libceed.USE_POINTER)
173
174    mult = r.get_multiplicity()
175
176    with mult.array_read() as mult_array:
177        for i in range(3 * ne + 1):
178            val = 1 + (1 if (i > 0 and i < 3 * ne and i % 3 == 0) else 0)
179            assert val == mult_array[i]
180
181# -------------------------------------------------------------------------------
182# Test creation and view of an element restriction
183# -------------------------------------------------------------------------------
184
185
186def test_210(ceed_resource, capsys):
187    ceed = libceed.Ceed(ceed_resource)
188
189    ne = 3
190
191    ind = np.zeros(2 * ne, dtype="int32")
192    for i in range(ne):
193        ind[2 * i + 0] = i + 0
194        ind[2 * i + 1] = i + 1
195    r = ceed.ElemRestriction(ne, 2, 1, 1, ne + 1, ind,
196                             cmode=libceed.USE_POINTER)
197
198    print(r)
199
200    stdout, stderr, ref_stdout = check.output(capsys)
201    assert not stderr
202    assert stdout == ref_stdout
203
204# -------------------------------------------------------------------------------
205# Test creation and view of a strided element restriction
206# -------------------------------------------------------------------------------
207
208
209def test_211(ceed_resource, capsys):
210    ceed = libceed.Ceed(ceed_resource)
211
212    ne = 3
213
214    strides = np.array([1, 2, 2], dtype="int32")
215    r = ceed.StridedElemRestriction(ne, 2, 1, ne + 1, strides)
216
217    print(r)
218
219    stdout, stderr, ref_stdout = check.output(capsys)
220    assert not stderr
221    assert stdout == ref_stdout
222
223# -------------------------------------------------------------------------------
224# Test creation and view of a blocked strided element restriction
225# -------------------------------------------------------------------------------
226
227
228def test_212(ceed_resource, capsys):
229    ceed = libceed.Ceed(ceed_resource)
230
231    ne = 3
232
233    strides = np.array([1, 2, 2], dtype="int32")
234    r = ceed.BlockedStridedElemRestriction(ne, 2, 2, 1, ne + 1, strides)
235
236    print(r)
237
238    stdout, stderr, ref_stdout = check.output(capsys)
239    assert not stderr
240    assert stdout == ref_stdout
241
242# -------------------------------------------------------------------------------
243