Skip to content

Commit b2e3f8e

Browse files
Add Julia API tests
1 parent 97c1c57 commit b2e3f8e

File tree

5 files changed

+155
-21
lines changed

5 files changed

+155
-21
lines changed

julia/LibCEED.jl/src/Basis.jl

+17-18
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,7 @@ Create a non tensor-product basis for $H^1$ discretizations
126126
at quadrature points.
127127
- `grad`: Array of size `(dim, nqpts, nnodes)` expressing derivatives of nodal basis
128128
functions at quadrature points.
129-
- `qref`: Array of length `nqpts` holding the locations of quadrature points on the
129+
- `qref`: Matrix of size `(dim, nqpts)` holding the locations of quadrature points on the
130130
reference element $[-1, 1]$.
131131
- `qweight`: Array of length `nqpts` holding the quadrature weights on the reference
132132
element.
@@ -145,12 +145,13 @@ function create_h1_basis(
145145
dim = getdimension(topo)
146146
@assert size(interp) == (nqpts, nnodes)
147147
@assert size(grad) == (dim, nqpts, nnodes)
148-
@assert length(qref) == nqpts
148+
@assert size(qref) == (dim, nqpts)
149149
@assert length(qweight) == nqpts
150150

151151
# Convert from Julia matrices and tensors (column-major) to row-major format
152152
interp_rowmajor = collect(interp')
153153
grad_rowmajor = permutedims(grad, [3, 2, 1])
154+
qref_rowmajor = collect(qref')
154155

155156
ref = Ref{C.CeedBasis}()
156157
C.CeedBasisCreateH1(
@@ -161,7 +162,7 @@ function create_h1_basis(
161162
nqpts,
162163
interp_rowmajor,
163164
grad_rowmajor,
164-
qref,
165+
qref_rowmajor,
165166
qweight,
166167
ref,
167168
)
@@ -183,7 +184,7 @@ Create a non tensor-product basis for H(div) discretizations
183184
at quadrature points.
184185
- `div`: Array of size `(nqpts, nnodes)` expressing divergence of basis functions at
185186
quadrature points.
186-
- `qref`: Array of length `nqpts` holding the locations of quadrature points on the
187+
- `qref`: Matrix of size `(dim, nqpts)` holding the locations of quadrature points on the
187188
reference element $[-1, 1]$.
188189
- `qweight`: Array of length `nqpts` holding the quadrature weights on the reference
189190
element.
@@ -202,12 +203,13 @@ function create_hdiv_basis(
202203
dim = getdimension(topo)
203204
@assert size(interp) == (dim, nqpts, nnodes)
204205
@assert size(div) == (nqpts, nnodes)
205-
@assert length(qref) == nqpts
206+
@assert size(qref) == (dim, nqpts)
206207
@assert length(qweight) == nqpts
207208

208209
# Convert from Julia matrices and tensors (column-major) to row-major format
209210
interp_rowmajor = permutedims(interp, [3, 2, 1])
210211
div_rowmajor = collect(div')
212+
qref_rowmajor = collect(qref')
211213

212214
ref = Ref{C.CeedBasis}()
213215
C.CeedBasisCreateHdiv(
@@ -218,7 +220,7 @@ function create_hdiv_basis(
218220
nqpts,
219221
interp_rowmajor,
220222
div_rowmajor,
221-
qref,
223+
qref_rowmajor,
222224
qweight,
223225
ref,
224226
)
@@ -240,7 +242,7 @@ Create a non tensor-product basis for H(curl) discretizations
240242
at quadrature points.
241243
- `curl`: Matrix of size `(curlcomp, nqpts, nnodes)`, `curlcomp = 1 if dim < 3 else dim`)
242244
matrix expressing curl of basis functions at quadrature points.
243-
- `qref`: Array of length `nqpts` holding the locations of quadrature points on the
245+
- `qref`: Matrix of size `(dim, nqpts)` holding the locations of quadrature points on the
244246
reference element $[-1, 1]$.
245247
- `qweight`: Array of length `nqpts` holding the quadrature weights on the reference
246248
element.
@@ -260,12 +262,13 @@ function create_hcurl_basis(
260262
curlcomp = dim < 3 ? 1 : dim
261263
@assert size(interp) == (dim, nqpts, nnodes)
262264
@assert size(curl) == (curlcomp, nqpts, nnodes)
263-
@assert length(qref) == nqpts
265+
@assert size(qref) == (dim, nqpts)
264266
@assert length(qweight) == nqpts
265267

266268
# Convert from Julia matrices and tensors (column-major) to row-major format
267269
interp_rowmajor = permutedims(interp, [3, 2, 1])
268270
curl_rowmajor = permutedims(curl, [3, 2, 1])
271+
qref_rowmajor = collect(qref')
269272

270273
ref = Ref{C.CeedBasis}()
271274
C.CeedBasisCreateHcurl(
@@ -276,7 +279,7 @@ function create_hcurl_basis(
276279
nqpts,
277280
interp_rowmajor,
278281
curl_rowmajor,
279-
qref,
282+
qref_rowmajor,
280283
qweight,
281284
ref,
282285
)
@@ -328,10 +331,9 @@ function apply(b::Basis, u::AbstractVector; nelem=1, tmode=NOTRANSPOSE, emode=EV
328331

329332
u_vec = CeedVector(c, u)
330333

331-
len_v = (tmode == TRANSPOSE) ? getnumnodes(b) : getnumqpts(b)
332-
if emode == EVAL_GRAD
333-
len_v *= getdimension(b)
334-
end
334+
qcomp = Ref{CeedInt}()
335+
C.CeedBasisGetNumQuadratureComponents(b[], emode, qcomp)
336+
len_v = (tmode == TRANSPOSE) ? getnumnodes(b) : qcomp[]*getnumqpts(b)
335337

336338
v_vec = CeedVector(c, len_v)
337339

@@ -437,11 +439,8 @@ function getqref(b::Basis)
437439
ref = Ref{Ptr{CeedScalar}}()
438440
C.CeedBasisGetQRef(b[], ref)
439441
copy(
440-
unsafe_wrap(
441-
Array,
442-
ref[],
443-
istensor[] ? getnumqpts1d(b) : (getnumqpts(b)*getdimension(b)),
444-
),
442+
istensor[] ? unsafe_wrap(Array, ref[], getnumqpts1d(b)) :
443+
unsafe_wrap(Array, ref[], (getnumqpts(b), getdimension(b)))',
445444
)
446445
end
447446

julia/LibCEED.jl/src/LibCEED.jl

+2
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,8 @@ export @interior_qf,
7979
create_elem_restriction_strided,
8080
create_evector,
8181
create_h1_basis,
82+
create_hdiv_basis,
83+
create_hcurl_basis,
8284
create_identity_qfunction,
8385
create_interior_qfunction,
8486
create_lvector,

julia/LibCEED.jl/test/buildmats.jl

+78
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
function build_mats_hdiv(qref, qweight, ::Type{T}=Float64) where {T}
2+
P, Q, dim = 4, 4, 2
3+
interp = Array{T}(undef, dim, Q, P)
4+
div = Array{T}(undef, Q, P)
5+
6+
qref[1, 1] = -1.0/sqrt(3.0)
7+
qref[1, 2] = qref[1, 1]
8+
qref[1, 3] = qref[1, 1]
9+
qref[1, 4] = -qref[1, 1]
10+
qref[2, 1] = -qref[1, 1]
11+
qref[2, 2] = -qref[1, 1]
12+
qref[2, 3] = qref[1, 1]
13+
qref[2, 4] = qref[1, 1]
14+
qweight[1] = 1.0
15+
qweight[2] = 1.0
16+
qweight[3] = 1.0
17+
qweight[4] = 1.0
18+
19+
# Loop over quadrature points
20+
for i = 1:Q
21+
x1 = qref[1, i]
22+
x2 = qref[2, i]
23+
# Interp
24+
interp[1, i, 1] = 0.0
25+
interp[2, i, 1] = 1.0 - x2
26+
interp[1, i, 2] = x1 - 1.0
27+
interp[2, i, 2] = 0.0
28+
interp[1, i, 3] = -x1
29+
interp[2, i, 3] = 0.0
30+
interp[1, i, 4] = 0.0
31+
interp[2, i, 4] = x2
32+
# Div
33+
div[i, 1] = -1.0
34+
div[i, 2] = 1.0
35+
div[i, 3] = -1.0
36+
div[i, 4] = 1.0
37+
end
38+
39+
return interp, div
40+
end
41+
42+
function build_mats_hcurl(qref, qweight, ::Type{T}=Float64) where {T}
43+
P, Q, dim = 3, 4, 2
44+
interp = Array{T}(undef, dim, Q, P)
45+
curl = Array{T}(undef, 1, Q, P)
46+
47+
qref[1, 1] = 0.2
48+
qref[1, 2] = 0.6
49+
qref[1, 3] = 1.0/3.0
50+
qref[1, 4] = 0.2
51+
qref[2, 1] = 0.2
52+
qref[2, 2] = 0.2
53+
qref[2, 3] = 1.0/3.0
54+
qref[2, 4] = 0.6
55+
qweight[1] = 25.0/96.0
56+
qweight[2] = 25.0/96.0
57+
qweight[3] = -27.0/96.0
58+
qweight[4] = 25.0/96.0
59+
60+
# Loop over quadrature points
61+
for i = 1:Q
62+
x1 = qref[1, i]
63+
x2 = qref[2, i]
64+
# Interp
65+
interp[1, i, 1] = -x2
66+
interp[2, i, 1] = x1
67+
interp[1, i, 2] = x2
68+
interp[2, i, 2] = 1.0 - x1
69+
interp[1, i, 3] = 1.0 - x2
70+
interp[2, i, 3] = x1
71+
# Curl
72+
curl[1, i, 1] = 2.0
73+
curl[1, i, 2] = -2.0
74+
curl[1, i, 3] = -2.0
75+
end
76+
77+
return interp, curl
78+
end

julia/LibCEED.jl/test/rundevtests.jl

+46-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,48 @@
11
using Test, LibCEED, LinearAlgebra, StaticArrays
22

3-
@testset "LibCEED Development Tests" begin end
3+
include("buildmats.jl")
4+
5+
@testset "LibCEED Development Tests" begin
6+
@testset "Basis" begin
7+
c = Ceed()
8+
dim = 2
9+
ncomp = 1
10+
p1 = 4
11+
q1 = 4
12+
qref1 = Array{Float64}(undef, dim, q1)
13+
qweight1 = Array{Float64}(undef, q1)
14+
interp1, div1 = build_mats_hdiv(qref1, qweight1)
15+
b1 = create_hdiv_basis(c, QUAD, ncomp, p1, q1, interp1, div1, qref1, qweight1)
16+
17+
u1 = ones(Float64, p1)
18+
v1 = apply(b1, u1)
19+
20+
for i = 1:q1
21+
@test v1[i] -1.0
22+
@test v1[q1+i] 1.0
23+
end
24+
25+
p2 = 3
26+
q2 = 4
27+
qref2 = Array{Float64}(undef, dim, q2)
28+
qweight2 = Array{Float64}(undef, q2)
29+
interp2, curl2 = build_mats_hcurl(qref2, qweight2)
30+
b2 = create_hcurl_basis(c, TRIANGLE, ncomp, p2, q2, interp2, curl2, qref2, qweight2)
31+
32+
u2 = [1.0, 2.0, 1.0]
33+
v2 = apply(b2, u2)
34+
35+
for i = 1:q2
36+
@test v2[i] 1.0
37+
end
38+
39+
u2[1] = -1.0
40+
u2[2] = 1.0
41+
u2[3] = 2.0
42+
v2 = apply(b2, u2)
43+
44+
for i = 1:q2
45+
@test v2[q2+i] 1.0
46+
end
47+
end
48+
end

julia/LibCEED.jl/test/runtests.jl

+12-2
Original file line numberDiff line numberDiff line change
@@ -174,8 +174,18 @@ else
174174
@test getgrad1d(b2) == d1d
175175
@test checkoutput(showstr(b2), "b2.out")
176176

177-
b3 = create_h1_basis(c, LINE, 1, p, q, b1d, reshape(d1d, 1, q, p), q1d, w1d)
178-
@test getqref(b3) == q1d
177+
b3 = create_h1_basis(
178+
c,
179+
LINE,
180+
1,
181+
p,
182+
q,
183+
b1d,
184+
reshape(d1d, 1, q, p),
185+
reshape(q1d, 1, q),
186+
w1d,
187+
)
188+
@test getqref(b3) == reshape(q1d, 1, q)
179189
@test getqweights(b3) == w1d
180190
@test checkoutput(showstr(b3), "b3.out")
181191

0 commit comments

Comments
 (0)