Mercurial > repos > public > sbplib_julia
comparison test/BoundaryConditions/boundary_condition_test.jl @ 1164:d26aef8a5987 feature/boundary_conditions
Add types for different kinds of boundary data functions to discretize the data on the grid. Add tests
author | Vidar Stiernström <vidar.stiernstrom@it.uu.se> |
---|---|
date | Wed, 07 Dec 2022 21:39:07 +0100 |
parents | |
children | bdcdbd4ea9cd |
comparison
equal
deleted
inserted
replaced
1163:c9fdfb1efba8 | 1164:d26aef8a5987 |
---|---|
1 using Test | |
2 | |
3 using Sbplib.BoundaryConditions | |
4 using Sbplib.Grids | |
5 | |
6 grid_1D = EquidistantGrid(11, 0.0, 1.0) | |
7 grid_2D = EquidistantGrid((11,15), (0.0, 0.0), (1.0,1.0)) | |
8 grid_3D = EquidistantGrid((11,15,13), (0.0, 0.0, 0.0), (1.0,1.0, 1.0)) | |
9 (id_l,_) = boundary_identifiers(grid_1D) | |
10 (_,_,_,id_n) = boundary_identifiers(grid_2D) | |
11 (_,_,_,_,id_b,_) = boundary_identifiers(grid_3D) | |
12 | |
13 @testset "BoundaryData" begin | |
14 | |
15 @testset "ConstantBoundaryData" begin | |
16 c = float(pi) | |
17 @test ConstantBoundaryData(c) isa BoundaryData | |
18 g_1D = discretize(ConstantBoundaryData(c),boundary_grid(grid_1D, id_l)) | |
19 g_2D = discretize(ConstantBoundaryData(c),boundary_grid(grid_2D, id_n)) | |
20 @test g_1D isa Function | |
21 @test g_2D isa Function | |
22 @test g_1D(0.) == fill(c) | |
23 @test g_2D(2.) == c*ones(11) | |
24 @test_throws MethodError g_1D(0.,0.) | |
25 @test_throws MethodError g_2D(0.,0.) | |
26 end | |
27 | |
28 @testset "TimeDependentBoundaryData" begin | |
29 f(t) = 1. /(t+0.1) | |
30 @test TimeDependentBoundaryData(f) isa BoundaryData | |
31 g_1D = discretize(TimeDependentBoundaryData(f),boundary_grid(grid_1D, id_l)) | |
32 g_2D = discretize(TimeDependentBoundaryData(f),boundary_grid(grid_2D, id_n)) | |
33 @test g_1D isa Function | |
34 @test g_2D isa Function | |
35 @test g_1D(0.) == f(0.)*fill(1) | |
36 @test g_2D(2.) == f(2.)*ones(11) | |
37 @test_throws MethodError g_1D(0.,0.) | |
38 @test_throws MethodError g_2D(0.,0.) | |
39 end | |
40 | |
41 #TBD: Is it reasoanble to have SpaceDependentBoundaryData for 1D-grids? It would then be a constant | |
42 # which then may be represented by ConstantBoundaryData. | |
43 @testset "SpaceDependentBoundaryData" begin | |
44 f0() = 2 | |
45 f1(x) = x.^2 | |
46 f2(x,y) = x.^2 - y | |
47 @test SpaceDependentBoundaryData(f1) isa BoundaryData | |
48 g_1D = discretize(SpaceDependentBoundaryData(f0),boundary_grid(grid_1D, id_l)) | |
49 g_2D = discretize(SpaceDependentBoundaryData(f1),boundary_grid(grid_2D, id_n)) | |
50 g_3D = discretize(SpaceDependentBoundaryData(f2),boundary_grid(grid_3D, id_n)) | |
51 @test g_1D isa Function | |
52 @test g_2D isa Function | |
53 @test g_3D isa Function | |
54 @test_broken g_1D(1.) == fill(f0()) # Does not work since evalOn for f0 returns (). | |
55 @test g_2D(2.) ≈ f1.(range(0., 1., 11)) rtol=1e-14 | |
56 @test g_3D(0.) ≈ evalOn(boundary_grid(grid_3D, id_n),f2) rtol=1e-14 | |
57 @test_throws MethodError g_1D(0.,0.) | |
58 @test_throws MethodError g_2D(0.,0.) | |
59 @test_throws MethodError g_3D(0.,0.) | |
60 end | |
61 | |
62 # TBD: Include tests for 1D-grids? See TBD above | |
63 @testset "SpaceTimeDependentBoundaryData" begin | |
64 fx1(x) = x.^2 | |
65 fx2(x,y) = x.^2 - y | |
66 ft(t) = exp(t) | |
67 ftx1(t,x) = ft(t)*fx1(x) | |
68 ftx2(t,x,y) = ft(t)*fx2(x,y) | |
69 @test SpaceTimeDependentBoundaryData(ftx1) isa BoundaryData | |
70 g_2D = discretize(SpaceTimeDependentBoundaryData(ftx1),boundary_grid(grid_2D, id_n)) | |
71 g_3D = discretize(SpaceTimeDependentBoundaryData(ftx2),boundary_grid(grid_3D, id_b)) | |
72 @test g_2D isa Function | |
73 @test g_3D isa Function | |
74 @test g_2D(2.) ≈ ft(2.)*fx1.(range(0., 1., 11)) rtol=1e-14 | |
75 @test g_3D(3.14) ≈ ft(3.14)*evalOn(boundary_grid(grid_3D, id_b),fx2) rtol=1e-14 | |
76 @test_throws MethodError g_2D(0.,0.) | |
77 @test_throws MethodError g_3D(0.,0.) | |
78 end | |
79 | |
80 @testset "ZeroBoundaryData" begin | |
81 @test ZeroBoundaryData() isa BoundaryData | |
82 g_2D = discretize(ZeroBoundaryData(), boundary_grid(grid_2D, id_n)) | |
83 g_3D = discretize(ZeroBoundaryData(), boundary_grid(grid_3D, id_b)) | |
84 @test g_2D isa Function | |
85 @test g_3D isa Function | |
86 @test g_2D(2.) ≈ 0.0*range(0., 1., 11) rtol=1e-14 | |
87 f(x,y) = 0 | |
88 @test g_3D(3.14) ≈ 0.0*evalOn(boundary_grid(grid_3D, id_b), f) rtol=1e-14 | |
89 @test_throws MethodError g_2D(0.,0.) | |
90 @test_throws MethodError g_3D(0.,0.) | |
91 end | |
92 end | |
93 | |
94 @testset "BoundaryCondition" begin | |
95 g = ConstantBoundaryData(1.0) | |
96 NeumannCondition(g,id_n) isa BoundaryCondition{ConstantBoundaryData} | |
97 DirichletCondition(g,id_n) isa BoundaryCondition{ConstantBoundaryData} | |
98 @test data(NeumannCondition(g,id_n)) == g | |
99 end |