annotate src/SbpOperators/volumeops/laplace/laplace.jl @ 1599:37b05221beda feature/boundary_conditions

Review
author Jonatan Werpers <jonatan@werpers.com>
date Wed, 29 May 2024 22:35:08 +0200
parents 19cdec9c21cb
children b2496b001297
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
624
a85db383484f Update documentation and remove some out-commented lines
Vidar Stiernström <vidar.stiernstrom@it.uu.se>
parents: 618
diff changeset
1 """
995
1ba8a398af9c Rename types
Jonatan Werpers <jonatan@werpers.com>
parents: 962
diff changeset
2 Laplace{T, Dim, TM} <: LazyTensor{T, Dim, Dim}
667
f3a0d1f7d842 Make Laplace a type storing relevant operators used when writing a scheme, e.g. quadratures, normal derivatives etc.
Vidar Stiernström <vidar.stiernstrom@it.uu.se>
parents: 648
diff changeset
3
1329
e94ddef5e72f Clean up documentation for changed types in SbpOperatorClean up documentation for changed types in SbpOperatorss
Jonatan Werpers <jonatan@werpers.com>
parents: 1291
diff changeset
4 The Laplace operator, approximating ∑d²/xᵢ² , i = 1,...,`Dim` as a
e94ddef5e72f Clean up documentation for changed types in SbpOperatorClean up documentation for changed types in SbpOperatorss
Jonatan Werpers <jonatan@werpers.com>
parents: 1291
diff changeset
5 `LazyTensor`.
667
f3a0d1f7d842 Make Laplace a type storing relevant operators used when writing a scheme, e.g. quadratures, normal derivatives etc.
Vidar Stiernström <vidar.stiernstrom@it.uu.se>
parents: 648
diff changeset
6 """
995
1ba8a398af9c Rename types
Jonatan Werpers <jonatan@werpers.com>
parents: 962
diff changeset
7 struct Laplace{T, Dim, TM<:LazyTensor{T, Dim, Dim}} <: LazyTensor{T, Dim, Dim}
951
66e8faf4bb4b Reviwe: Some more comments
Jonatan Werpers <jonatan@werpers.com>
parents: 948
diff changeset
8 D::TM # Difference operator
989
7bf3121c6864 Add type StencilSet
Vidar Stiernström <vidar.stiernstrom@it.uu.se>
parents: 962
diff changeset
9 stencil_set::StencilSet # Stencil set of the operator
667
f3a0d1f7d842 Make Laplace a type storing relevant operators used when writing a scheme, e.g. quadratures, normal derivatives etc.
Vidar Stiernström <vidar.stiernstrom@it.uu.se>
parents: 648
diff changeset
10 end
f3a0d1f7d842 Make Laplace a type storing relevant operators used when writing a scheme, e.g. quadratures, normal derivatives etc.
Vidar Stiernström <vidar.stiernstrom@it.uu.se>
parents: 648
diff changeset
11
924
12e8e431b43c Start restructuring Laplace making it more minimal.
Vidar Stiernström <vidar.stiernstrom@it.uu.se>
parents: 922
diff changeset
12 """
1347
08f06bfacd5c Fix typos and formatting of documentation
Vidar Stiernström <vidar.stiernstrom@it.uu.se>
parents: 1329
diff changeset
13 Laplace(g::Grid, stencil_set::StencilSet)
667
f3a0d1f7d842 Make Laplace a type storing relevant operators used when writing a scheme, e.g. quadratures, normal derivatives etc.
Vidar Stiernström <vidar.stiernstrom@it.uu.se>
parents: 648
diff changeset
14
1347
08f06bfacd5c Fix typos and formatting of documentation
Vidar Stiernström <vidar.stiernstrom@it.uu.se>
parents: 1329
diff changeset
15 Creates the `Laplace` operator `Δ` on `g` given `stencil_set`.
989
7bf3121c6864 Add type StencilSet
Vidar Stiernström <vidar.stiernstrom@it.uu.se>
parents: 962
diff changeset
16
7bf3121c6864 Add type StencilSet
Vidar Stiernström <vidar.stiernstrom@it.uu.se>
parents: 962
diff changeset
17 See also [`laplace`](@ref).
924
12e8e431b43c Start restructuring Laplace making it more minimal.
Vidar Stiernström <vidar.stiernstrom@it.uu.se>
parents: 922
diff changeset
18 """
1291
356ec6a72974 Implement changes in SbpOperators
Jonatan Werpers <jonatan@werpers.com>
parents: 1128
diff changeset
19 function Laplace(g::Grid, stencil_set::StencilSet)
356ec6a72974 Implement changes in SbpOperators
Jonatan Werpers <jonatan@werpers.com>
parents: 1128
diff changeset
20 Δ = laplace(g, stencil_set)
356ec6a72974 Implement changes in SbpOperators
Jonatan Werpers <jonatan@werpers.com>
parents: 1128
diff changeset
21 return Laplace(Δ, stencil_set)
667
f3a0d1f7d842 Make Laplace a type storing relevant operators used when writing a scheme, e.g. quadratures, normal derivatives etc.
Vidar Stiernström <vidar.stiernstrom@it.uu.se>
parents: 648
diff changeset
22 end
f3a0d1f7d842 Make Laplace a type storing relevant operators used when writing a scheme, e.g. quadratures, normal derivatives etc.
Vidar Stiernström <vidar.stiernstrom@it.uu.se>
parents: 648
diff changeset
23
f3a0d1f7d842 Make Laplace a type storing relevant operators used when writing a scheme, e.g. quadratures, normal derivatives etc.
Vidar Stiernström <vidar.stiernstrom@it.uu.se>
parents: 648
diff changeset
24 LazyTensors.range_size(L::Laplace) = LazyTensors.range_size(L.D)
f3a0d1f7d842 Make Laplace a type storing relevant operators used when writing a scheme, e.g. quadratures, normal derivatives etc.
Vidar Stiernström <vidar.stiernstrom@it.uu.se>
parents: 648
diff changeset
25 LazyTensors.domain_size(L::Laplace) = LazyTensors.domain_size(L.D)
f3a0d1f7d842 Make Laplace a type storing relevant operators used when writing a scheme, e.g. quadratures, normal derivatives etc.
Vidar Stiernström <vidar.stiernstrom@it.uu.se>
parents: 648
diff changeset
26 LazyTensors.apply(L::Laplace, v::AbstractArray, I...) = LazyTensors.apply(L.D,v,I...)
f3a0d1f7d842 Make Laplace a type storing relevant operators used when writing a scheme, e.g. quadratures, normal derivatives etc.
Vidar Stiernström <vidar.stiernstrom@it.uu.se>
parents: 648
diff changeset
27
995
1ba8a398af9c Rename types
Jonatan Werpers <jonatan@werpers.com>
parents: 962
diff changeset
28 # TODO: Implement pretty printing of Laplace once pretty printing of LazyTensors is implemented.
924
12e8e431b43c Start restructuring Laplace making it more minimal.
Vidar Stiernström <vidar.stiernstrom@it.uu.se>
parents: 922
diff changeset
29 # Base.show(io::IO, L::Laplace) = ...
755
36adc15d3935 Reduce the number of type perameters used by Laplace.
Vidar Stiernström <vidar.stiernstrom@it.uu.se>
parents: 754
diff changeset
30
36adc15d3935 Reduce the number of type perameters used by Laplace.
Vidar Stiernström <vidar.stiernstrom@it.uu.se>
parents: 754
diff changeset
31 """
1291
356ec6a72974 Implement changes in SbpOperators
Jonatan Werpers <jonatan@werpers.com>
parents: 1128
diff changeset
32 laplace(g::Grid, stencil_set)
624
a85db383484f Update documentation and remove some out-commented lines
Vidar Stiernström <vidar.stiernstrom@it.uu.se>
parents: 618
diff changeset
33
1347
08f06bfacd5c Fix typos and formatting of documentation
Vidar Stiernström <vidar.stiernstrom@it.uu.se>
parents: 1329
diff changeset
34 Creates the Laplace operator operator `Δ` as a `LazyTensor` on `g`.
624
a85db383484f Update documentation and remove some out-commented lines
Vidar Stiernström <vidar.stiernstrom@it.uu.se>
parents: 618
diff changeset
35
1291
356ec6a72974 Implement changes in SbpOperators
Jonatan Werpers <jonatan@werpers.com>
parents: 1128
diff changeset
36 `Δ` approximates the Laplace operator ∑d²/xᵢ² , i = 1,...,`Dim` on `g`. The
356ec6a72974 Implement changes in SbpOperators
Jonatan Werpers <jonatan@werpers.com>
parents: 1128
diff changeset
37 approximation depends on the type of grid and the stencil set.
947
38d1752a9aff Reformat "See also:"
Jonatan Werpers <jonatan@werpers.com>
parents: 936
diff changeset
38
38d1752a9aff Reformat "See also:"
Jonatan Werpers <jonatan@werpers.com>
parents: 936
diff changeset
39 See also: [`second_derivative`](@ref).
624
a85db383484f Update documentation and remove some out-commented lines
Vidar Stiernström <vidar.stiernstrom@it.uu.se>
parents: 618
diff changeset
40 """
1291
356ec6a72974 Implement changes in SbpOperators
Jonatan Werpers <jonatan@werpers.com>
parents: 1128
diff changeset
41 function laplace end
356ec6a72974 Implement changes in SbpOperators
Jonatan Werpers <jonatan@werpers.com>
parents: 1128
diff changeset
42 function laplace(g::TensorGrid, stencil_set)
356ec6a72974 Implement changes in SbpOperators
Jonatan Werpers <jonatan@werpers.com>
parents: 1128
diff changeset
43 # return mapreduce(+, enumerate(g.grids)) do (i, gᵢ)
356ec6a72974 Implement changes in SbpOperators
Jonatan Werpers <jonatan@werpers.com>
parents: 1128
diff changeset
44 # Δᵢ = laplace(gᵢ, stencil_set)
356ec6a72974 Implement changes in SbpOperators
Jonatan Werpers <jonatan@werpers.com>
parents: 1128
diff changeset
45 # LazyTensors.inflate(Δᵢ, size(g), i)
356ec6a72974 Implement changes in SbpOperators
Jonatan Werpers <jonatan@werpers.com>
parents: 1128
diff changeset
46 # end
356ec6a72974 Implement changes in SbpOperators
Jonatan Werpers <jonatan@werpers.com>
parents: 1128
diff changeset
47
356ec6a72974 Implement changes in SbpOperators
Jonatan Werpers <jonatan@werpers.com>
parents: 1128
diff changeset
48 Δ = LazyTensors.inflate(laplace(g.grids[1], stencil_set), size(g), 1)
356ec6a72974 Implement changes in SbpOperators
Jonatan Werpers <jonatan@werpers.com>
parents: 1128
diff changeset
49 for d = 2:ndims(g)
356ec6a72974 Implement changes in SbpOperators
Jonatan Werpers <jonatan@werpers.com>
parents: 1128
diff changeset
50 Δ += LazyTensors.inflate(laplace(g.grids[d], stencil_set), size(g), d)
356
0844069ab5ff Reinclude SbpOperators and fix most of the code and tests there.
Jonatan Werpers <jonatan@werpers.com>
parents: 333
diff changeset
51 end
611
e71f2f81b5f8 NOT WORKING: Draft implementation of VolumeOperator and make SecondDerivative specialize it. Reformulate Laplace for the new SecondDerivative.
Vidar Stiernström <vidar.stiernstrom@it.uu.se>
parents: 542
diff changeset
52 return Δ
947
38d1752a9aff Reformat "See also:"
Jonatan Werpers <jonatan@werpers.com>
parents: 936
diff changeset
53 end
1291
356ec6a72974 Implement changes in SbpOperators
Jonatan Werpers <jonatan@werpers.com>
parents: 1128
diff changeset
54 laplace(g::EquidistantGrid, stencil_set) = second_derivative(g, stencil_set)
1395
bdcdbd4ea9cd Merge with default. Comment out broken tests for boundary_conditions at sat
Vidar Stiernström <vidar.stiernstrom@it.uu.se>
parents: 1136 1347
diff changeset
55
1598
19cdec9c21cb Implement and test sat_tensors for Dirichlet and Neumann conditions
Vidar Stiernström <vidar.stiernstrom@gmail.com>
parents: 1484
diff changeset
56 """
19cdec9c21cb Implement and test sat_tensors for Dirichlet and Neumann conditions
Vidar Stiernström <vidar.stiernstrom@gmail.com>
parents: 1484
diff changeset
57 sat_tensors(Δ::Laplace, g::Grid, bc::DirichletCondition, tuning)
19cdec9c21cb Implement and test sat_tensors for Dirichlet and Neumann conditions
Vidar Stiernström <vidar.stiernstrom@gmail.com>
parents: 1484
diff changeset
58
19cdec9c21cb Implement and test sat_tensors for Dirichlet and Neumann conditions
Vidar Stiernström <vidar.stiernstrom@gmail.com>
parents: 1484
diff changeset
59 The operators required to construct the SAT for imposing a Dirichlet condition.
19cdec9c21cb Implement and test sat_tensors for Dirichlet and Neumann conditions
Vidar Stiernström <vidar.stiernstrom@gmail.com>
parents: 1484
diff changeset
60 `tuning` specifies the strength of the penalty. See
19cdec9c21cb Implement and test sat_tensors for Dirichlet and Neumann conditions
Vidar Stiernström <vidar.stiernstrom@gmail.com>
parents: 1484
diff changeset
61
19cdec9c21cb Implement and test sat_tensors for Dirichlet and Neumann conditions
Vidar Stiernström <vidar.stiernstrom@gmail.com>
parents: 1484
diff changeset
62 See also: [`sat`,`DirichletCondition`, `positivity_decomposition`](@ref).
19cdec9c21cb Implement and test sat_tensors for Dirichlet and Neumann conditions
Vidar Stiernström <vidar.stiernstrom@gmail.com>
parents: 1484
diff changeset
63 """
19cdec9c21cb Implement and test sat_tensors for Dirichlet and Neumann conditions
Vidar Stiernström <vidar.stiernstrom@gmail.com>
parents: 1484
diff changeset
64 function BoundaryConditions.sat_tensors(Δ::Laplace, g::Grid, bc::DirichletCondition, tuning)
19cdec9c21cb Implement and test sat_tensors for Dirichlet and Neumann conditions
Vidar Stiernström <vidar.stiernstrom@gmail.com>
parents: 1484
diff changeset
65 id = bc.id
19cdec9c21cb Implement and test sat_tensors for Dirichlet and Neumann conditions
Vidar Stiernström <vidar.stiernstrom@gmail.com>
parents: 1484
diff changeset
66 set = Δ.stencil_set
19cdec9c21cb Implement and test sat_tensors for Dirichlet and Neumann conditions
Vidar Stiernström <vidar.stiernstrom@gmail.com>
parents: 1484
diff changeset
67 H⁻¹ = inverse_inner_product(g,set)
19cdec9c21cb Implement and test sat_tensors for Dirichlet and Neumann conditions
Vidar Stiernström <vidar.stiernstrom@gmail.com>
parents: 1484
diff changeset
68 Hᵧ = inner_product(boundary_grid(g, id), set)
19cdec9c21cb Implement and test sat_tensors for Dirichlet and Neumann conditions
Vidar Stiernström <vidar.stiernstrom@gmail.com>
parents: 1484
diff changeset
69 e = boundary_restriction(g, set, id)
19cdec9c21cb Implement and test sat_tensors for Dirichlet and Neumann conditions
Vidar Stiernström <vidar.stiernstrom@gmail.com>
parents: 1484
diff changeset
70 d = normal_derivative(g, set, id)
19cdec9c21cb Implement and test sat_tensors for Dirichlet and Neumann conditions
Vidar Stiernström <vidar.stiernstrom@gmail.com>
parents: 1484
diff changeset
71 B = positivity_decomposition(Δ, g, bc, tuning)
19cdec9c21cb Implement and test sat_tensors for Dirichlet and Neumann conditions
Vidar Stiernström <vidar.stiernstrom@gmail.com>
parents: 1484
diff changeset
72 sat_op = H⁻¹∘(d' - B*e')∘Hᵧ
19cdec9c21cb Implement and test sat_tensors for Dirichlet and Neumann conditions
Vidar Stiernström <vidar.stiernstrom@gmail.com>
parents: 1484
diff changeset
73 return sat_op, e
19cdec9c21cb Implement and test sat_tensors for Dirichlet and Neumann conditions
Vidar Stiernström <vidar.stiernstrom@gmail.com>
parents: 1484
diff changeset
74 end
19cdec9c21cb Implement and test sat_tensors for Dirichlet and Neumann conditions
Vidar Stiernström <vidar.stiernstrom@gmail.com>
parents: 1484
diff changeset
75 BoundaryConditions.sat_tensors(Δ::Laplace, g::Grid, bc::DirichletCondition) = BoundaryConditions.sat_tensors(Δ, g, bc, (1.,1.))
1106
b4ee47f2aafb Start implementing functions for boundary conditions associated with Laplace
Vidar Stiernström <vidar.stiernstrom@it.uu.se>
parents: 1040
diff changeset
76
1136
470a70a6c1e6 Reformulate boundary routines (sat tensors) for Laplace and fix export
Vidar Stiernström <vidar.stiernstrom@it.uu.se>
parents: 1133
diff changeset
77 """
1484
8d60d045c2a2 Add todo and minor edits to variable naming and docstring
Vidar Stiernström <vidar.stiernstrom@it.uu.se>
parents: 1396
diff changeset
78 sat_tensors(Δ::Laplace, g::Grid, bc::NeumannCondition)
1106
b4ee47f2aafb Start implementing functions for boundary conditions associated with Laplace
Vidar Stiernström <vidar.stiernstrom@it.uu.se>
parents: 1040
diff changeset
79
1598
19cdec9c21cb Implement and test sat_tensors for Dirichlet and Neumann conditions
Vidar Stiernström <vidar.stiernstrom@gmail.com>
parents: 1484
diff changeset
80 The operators required to construct the SAT for imposing a Neumann condition
1484
8d60d045c2a2 Add todo and minor edits to variable naming and docstring
Vidar Stiernström <vidar.stiernstrom@it.uu.se>
parents: 1396
diff changeset
81
1136
470a70a6c1e6 Reformulate boundary routines (sat tensors) for Laplace and fix export
Vidar Stiernström <vidar.stiernstrom@it.uu.se>
parents: 1133
diff changeset
82
470a70a6c1e6 Reformulate boundary routines (sat tensors) for Laplace and fix export
Vidar Stiernström <vidar.stiernstrom@it.uu.se>
parents: 1133
diff changeset
83 See also: [`sat`,`NeumannCondition`](@ref).
470a70a6c1e6 Reformulate boundary routines (sat tensors) for Laplace and fix export
Vidar Stiernström <vidar.stiernstrom@it.uu.se>
parents: 1133
diff changeset
84 """
1395
bdcdbd4ea9cd Merge with default. Comment out broken tests for boundary_conditions at sat
Vidar Stiernström <vidar.stiernstrom@it.uu.se>
parents: 1136 1347
diff changeset
85 function BoundaryConditions.sat_tensors(Δ::Laplace, g::Grid, bc::NeumannCondition)
1136
470a70a6c1e6 Reformulate boundary routines (sat tensors) for Laplace and fix export
Vidar Stiernström <vidar.stiernstrom@it.uu.se>
parents: 1133
diff changeset
86 id = bc.id
1106
b4ee47f2aafb Start implementing functions for boundary conditions associated with Laplace
Vidar Stiernström <vidar.stiernstrom@it.uu.se>
parents: 1040
diff changeset
87 set = Δ.stencil_set
b4ee47f2aafb Start implementing functions for boundary conditions associated with Laplace
Vidar Stiernström <vidar.stiernstrom@it.uu.se>
parents: 1040
diff changeset
88 H⁻¹ = inverse_inner_product(g,set)
1136
470a70a6c1e6 Reformulate boundary routines (sat tensors) for Laplace and fix export
Vidar Stiernström <vidar.stiernstrom@it.uu.se>
parents: 1133
diff changeset
89 Hᵧ = inner_product(boundary_grid(g, id), set)
1106
b4ee47f2aafb Start implementing functions for boundary conditions associated with Laplace
Vidar Stiernström <vidar.stiernstrom@it.uu.se>
parents: 1040
diff changeset
90 e = boundary_restriction(g, set, id)
b4ee47f2aafb Start implementing functions for boundary conditions associated with Laplace
Vidar Stiernström <vidar.stiernstrom@it.uu.se>
parents: 1040
diff changeset
91 d = normal_derivative(g, set, id)
1395
bdcdbd4ea9cd Merge with default. Comment out broken tests for boundary_conditions at sat
Vidar Stiernström <vidar.stiernstrom@it.uu.se>
parents: 1136 1347
diff changeset
92
1598
19cdec9c21cb Implement and test sat_tensors for Dirichlet and Neumann conditions
Vidar Stiernström <vidar.stiernstrom@gmail.com>
parents: 1484
diff changeset
93 sat_op = -H⁻¹∘e'∘Hᵧ
1484
8d60d045c2a2 Add todo and minor edits to variable naming and docstring
Vidar Stiernström <vidar.stiernstrom@it.uu.se>
parents: 1396
diff changeset
94 return sat_op, d
1106
b4ee47f2aafb Start implementing functions for boundary conditions associated with Laplace
Vidar Stiernström <vidar.stiernstrom@it.uu.se>
parents: 1040
diff changeset
95 end
1598
19cdec9c21cb Implement and test sat_tensors for Dirichlet and Neumann conditions
Vidar Stiernström <vidar.stiernstrom@gmail.com>
parents: 1484
diff changeset
96
19cdec9c21cb Implement and test sat_tensors for Dirichlet and Neumann conditions
Vidar Stiernström <vidar.stiernstrom@gmail.com>
parents: 1484
diff changeset
97 function positivity_decomposition(Δ::Laplace, g::Grid, bc::DirichletCondition, tuning)
19cdec9c21cb Implement and test sat_tensors for Dirichlet and Neumann conditions
Vidar Stiernström <vidar.stiernstrom@gmail.com>
parents: 1484
diff changeset
98 pos_prop = positivity_properties(Δ)
19cdec9c21cb Implement and test sat_tensors for Dirichlet and Neumann conditions
Vidar Stiernström <vidar.stiernstrom@gmail.com>
parents: 1484
diff changeset
99 h = spacing(orthogonal_grid(g, bc.id))
19cdec9c21cb Implement and test sat_tensors for Dirichlet and Neumann conditions
Vidar Stiernström <vidar.stiernstrom@gmail.com>
parents: 1484
diff changeset
100 θ_H = pos_prop.theta_H
19cdec9c21cb Implement and test sat_tensors for Dirichlet and Neumann conditions
Vidar Stiernström <vidar.stiernstrom@gmail.com>
parents: 1484
diff changeset
101 τ_H = tuning[1]*ndims(g)/(h*θ_H)
19cdec9c21cb Implement and test sat_tensors for Dirichlet and Neumann conditions
Vidar Stiernström <vidar.stiernstrom@gmail.com>
parents: 1484
diff changeset
102 θ_R = pos_prop.theta_R
19cdec9c21cb Implement and test sat_tensors for Dirichlet and Neumann conditions
Vidar Stiernström <vidar.stiernstrom@gmail.com>
parents: 1484
diff changeset
103 τ_R = tuning[2]/(h*θ_R)
19cdec9c21cb Implement and test sat_tensors for Dirichlet and Neumann conditions
Vidar Stiernström <vidar.stiernstrom@gmail.com>
parents: 1484
diff changeset
104 B = τ_H + τ_R
19cdec9c21cb Implement and test sat_tensors for Dirichlet and Neumann conditions
Vidar Stiernström <vidar.stiernstrom@gmail.com>
parents: 1484
diff changeset
105 return B
19cdec9c21cb Implement and test sat_tensors for Dirichlet and Neumann conditions
Vidar Stiernström <vidar.stiernstrom@gmail.com>
parents: 1484
diff changeset
106 end
19cdec9c21cb Implement and test sat_tensors for Dirichlet and Neumann conditions
Vidar Stiernström <vidar.stiernstrom@gmail.com>
parents: 1484
diff changeset
107
1599
Jonatan Werpers <jonatan@werpers.com>
parents: 1598
diff changeset
108 positivity_properties(Δ::Laplace) = parse_named_tuple(Δ.stencil_set["Positivity"]["D2"]) # REVIEW: Can this function extract theta_H from the inner product instead of storing it twice in the TOML?