xref: /libCEED/julia/LibCEED.jl/test/rundevtests.jl (revision caee03026e6576cbf7a399c2fc51bb918c77f451)
1using Test, LibCEED, LinearAlgebra, StaticArrays
2
3@testset "LibCEED Development Tests" begin
4    @testset "QFunction" begin
5        c = Ceed()
6        @test showstr(create_interior_qfunction(c, "Poisson3DApply")) == """
7             Gallery CeedQFunction - Poisson3DApply
8               2 input fields:
9                 Input field 0:
10                   Name: "du"
11                   Size: 3
12                   EvalMode: "gradient"
13                 Input field 1:
14                   Name: "qdata"
15                   Size: 6
16                   EvalMode: "none"
17               1 output field:
18                 Output field 0:
19                   Name: "dv"
20                   Size: 3
21                   EvalMode: "gradient\""""
22    end
23
24    @testset "QFunction" begin
25        c = Ceed()
26
27        id = create_identity_qfunction(c, 1, EVAL_INTERP, EVAL_INTERP)
28        Q = 10
29        v = rand(CeedScalar, Q)
30        v1 = CeedVector(c, v)
31        v2 = CeedVector(c, Q)
32        apply!(id, Q, [v1], [v2])
33        @test @witharray(a = v2, a == v)
34
35        @interior_qf id2 = (c, (a, :in, EVAL_INTERP), (b, :out, EVAL_INTERP), b .= a)
36        v2[] = 0.0
37        apply!(id2, Q, [v1], [v2])
38        @test @witharray(a = v2, a == v)
39
40        ctxdata = CtxData(IOBuffer(), rand(CeedScalar, 3))
41        ctx = Context(c, ctxdata)
42        dim = 3
43        @interior_qf qf = (
44            c,
45            dim=dim,
46            ctxdata::CtxData,
47            (a, :in, EVAL_GRAD, dim),
48            (b, :in, EVAL_NONE),
49            (c, :out, EVAL_INTERP),
50            begin
51                c[] = b*sum(a)
52                show(ctxdata.io, MIME("text/plain"), ctxdata.x)
53            end,
54        )
55        set_context!(qf, ctx)
56        in_sz, out_sz = LibCEED.get_field_sizes(qf)
57        @test in_sz == [dim, 1]
58        @test out_sz == [1]
59        v1 = rand(CeedScalar, dim)
60        v2 = rand(CeedScalar, 1)
61        cv1 = CeedVector(c, v1)
62        cv2 = CeedVector(c, v2)
63        cv3 = CeedVector(c, 1)
64        apply!(qf, 1, [cv1, cv2], [cv3])
65        @test String(take!(ctxdata.io)) == showstr(ctxdata.x)
66        @test @witharray_read(v3 = cv3, v3[1] == v2[1]*sum(v1))
67
68        @test QFunctionNone()[] == LibCEED.C.CEED_QFUNCTION_NONE[]
69    end
70
71    @testset "Operator" begin
72        c = Ceed()
73        @interior_qf id = (
74            c,
75            (input, :in, EVAL_INTERP),
76            (output, :out, EVAL_INTERP),
77            begin
78                output[] = input
79            end,
80        )
81        b = create_tensor_h1_lagrange_basis(c, 3, 1, 3, 3, GAUSS_LOBATTO)
82        n = getnumnodes(b)
83        offsets = Vector{CeedInt}(0:n-1)
84        r = create_elem_restriction(c, 1, n, 1, 1, n, offsets)
85        op = Operator(
86            c;
87            qf=id,
88            fields=[
89                (:input, r, b, CeedVectorActive()),
90                (:output, r, b, CeedVectorActive()),
91            ],
92        )
93        @test showstr(op) == """
94             CeedOperator
95               1 elements with 27 quadrature points each
96               2 fields
97               1 input field:
98                 Input field 0:
99                   Name: "input"
100                   Size: 1
101                   EvalMode: interpolation
102                   Active vector
103               1 output field:
104                 Output field 0:
105                   Name: "output"
106                   Size: 1
107                   EvalMode: interpolation
108                   Active vector"""
109    end
110end
111