comparison src/BoundaryConditions/boundary_condition.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 667e9c588f23
children bdcdbd4ea9cd
comparison
equal deleted inserted replaced
1163:c9fdfb1efba8 1164:d26aef8a5987
1 # TODO: Should BoundaryData just be used for traits
2 # of the BoundaryConditions? Seems like one then could move the
3 # the boundary data value val directly to BoundaryCondition
4 # Not sure how one would do this tho.
1 """ 5 """
2 BoundaryDataType 6 BoundaryData
3 7
4 A type for storing boundary data, e.g. constant, space-dependent, time-dependent etc. 8 A type for storing boundary data, e.g. constant, space-dependent, time-dependent etc.
5 Subtypes of `BoundaryDataType` should store the boundary data in a field `val`, i.e. 9 Subtypes of `BoundaryData` should store the boundary data in a field `val`. The exception
6 `struct MyBoundaryDataType{T} <: BoundaryDataType val::T end`. 10 to this is ZeroBoundaryData.
7 """ 11 """
8 abstract type BoundaryDataType end 12 abstract type BoundaryData end
9 13
10 struct ConstantBoundaryData{T} <: BoundaryDataType 14 """
15 ConstantBoundaryData
16
17 `val` is a scalar value of type T
18 """
19 struct ConstantBoundaryData{T<:Number} <: BoundaryData
11 val::T 20 val::T
12 end 21 end
13 22
14 struct SpaceDependentBoundaryData{T} <: BoundaryDataType 23 """
24 SpaceDependentBoundaryData
25
26 `val` is a function of dimensionality equal to the boundary
27 """
28 struct SpaceDependentBoundaryData{T<:Function} <: BoundaryData
15 val::T 29 val::T
16 end 30 end
17 31
18 struct TimeDependentBoundaryData{T} <: BoundaryDataType 32 """
33 TimeDependentBoundaryData
34
35 `val` is a scalar function val(t)
36 """
37 struct TimeDependentBoundaryData{T<:Function} <: BoundaryData
19 val::T 38 val::T
20 end 39 end
21 40
22 struct SpaceTimeDependentBoundaryData{T} <: BoundaryDataType 41 """
42 SpaceTimeDependentBoundaryData
43
44 `val` is a timedependent function returning the spacedependent
45 boundary data at a specific time. For instance, if f(t,x)
46 is the function describing the spacetimedependent boundary data then
47 val(t*) returns the function g(x) = f(t*,x...)
48 """
49 struct SpaceTimeDependentBoundaryData{T<:Function} <: BoundaryData
23 val::T 50 val::T
51
52 function SpaceTimeDependentBoundaryData(f::Function)
53 val(t) = (args...) -> f(t,args...)
54 return new{typeof(val)}(val)
55 end
56 end
57
58 """
59 ZeroBoundaryData
60 """
61 struct ZeroBoundaryData <: BoundaryData end
62
63
64 """
65 discretize(::BoundaryData, boundary_grid)
66
67 Returns an anonymous time-dependent function f, such that f(t) is
68 a `LazyArray` holding the `BoundaryData` discretized on `boundary_grid`.
69 """
70 # TODO: Is the return type of discretize really a good interface
71 # for the boundary data?
72 # Moreover, instead of explicitly converting to a LazyArray here
73 # should we defer this to evalOn (and extend evalOn for scalars as well)?
74 # I.e. if evalOn returns a LazyArray, the boundary data is lazy. Otherwise
75 # it is preallocated.
76
77 function discretize(bd::ConstantBoundaryData, boundary_grid)
78 return t -> LazyTensors.LazyConstantArray(bd.val, size(boundary_grid))
79 end
80
81 function discretize(bd::TimeDependentBoundaryData, boundary_grid)
82 return t -> LazyTensors.LazyConstantArray(bd.val(t), size(boundary_grid))
83 end
84
85 function discretize(bd::SpaceDependentBoundaryData, boundary_grid)
86 return t -> evalOn(boundary_grid, bd.val)
87 end
88
89 function discretize(bd::SpaceTimeDependentBoundaryData, boundary_grid)
90 return t -> evalOn(boundary_grid, bd.val(t))
91 end
92
93 function discretize(::ZeroBoundaryData, boundary_grid)
94 return t -> LazyTensors.LazyConstantArray(zero(eltype(boundary_grid)), size(boundary_grid))
24 end 95 end
25 96
26 """ 97 """
27 BoundaryCondition 98 BoundaryCondition
28 99
29 A type for implementing data needed in order to impose a boundary condition. 100 A type for implementing data needed in order to impose a boundary condition.
30 Subtypes refer to perticular types of boundary conditions, e.g. Neumann conditions. 101 Subtypes refer to perticular types of boundary conditions, e.g. Neumann conditions.
31 """ 102 """
32 # TODO: Parametrize the boundary id as well? 103 abstract type BoundaryCondition{T<:BoundaryData} end
33 abstract type BoundaryCondition{T<:BoundaryDataType} end
34 104
35 data(bc::BoundaryCondition) = bc.data.val 105 """
106 data(::BoundaryCondition)
36 107
37 struct NeumannCondition{BDT<:BoundaryDataType} <: BoundaryCondition{BDT} 108 Returns the data stored by the `BoundaryCondition`.
38 id::BoundaryIdentifier 109 """
39 data::BDT 110 data(bc::BoundaryCondition) = bc.data
111
112
113 struct NeumannCondition{BD<:BoundaryData} <: BoundaryCondition{BD}
114 data::BD
115 id::BoundaryIdentifier
40 end 116 end
41 117
42 struct DirichletCondition{BDT<:BoundaryDataType} <: BoundaryCondition{BDT} 118 struct DirichletCondition{BD<:BoundaryData} <: BoundaryCondition{BD}
119 data::BD
43 id::BoundaryIdentifier 120 id::BoundaryIdentifier
44 data::BDT
45 end 121 end
46
47 struct RobinCondition{BDT<:BoundaryDataType,T<:Real} <: BoundaryCondition{BDT}
48 id::BoundaryIdentifier
49 data::BDT
50 α::T
51 β::T
52 end