-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsolver.jl
185 lines (120 loc) · 4.9 KB
/
solver.jl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
using Plots: plot, plot!
const G = 6.67e-11 # Gravitational proportionality constant can be ommitted (set to 1.0) to see a paraboloidal solution shape
const integral_steps = 1000 # Number of steps in integral() function
# Structure representing n-node 1-D Lagrange symplectic basis
struct Basis
a::Float64 # Start point
b::Float64 # End point
n::Integer # Number of nodes, not added division points. In fact only n-2 points are added.
h::Float64 # Size of a single element
basic_funs::Dict{Integer, Function} # Lazy construction of basic functions
nodes::Array{Float64, 1} # All the n nodes, including start and end given
# Constructor function
Basis(start_point::Float64, end_point::Float64, n_divisions::Int) = new(
start_point,
end_point,
n_divisions,
(end_point - start_point) / (n_divisions-1),
Dict{Integer, Function}(),
range(start_point, end_point, length = n_divisions)
)
end
# Convention: Basis[i] is an i-th basic function
function Base.getindex(basis::Basis, i::Int64)::Function
if get(basis.basic_funs, i, nothing) === nothing
basis.basic_funs[i] = basic_i(basis, i)
end
return basis.basic_funs[i]
end
# Technical safety improvement
function safeget(array::Array{Float64, 1}, i::Int64)::Number
if i ≤ 0
return array[1] - 1.0
elseif i ≥ length(array) + 1
return array[length(array)] + 1.0
else
return array[i]
end
end
# Create a lambda representing i-th basic function of Lagrange symplectic basis
function basic_i(basis::Basis, i::Int64)::Function
left = safeget(basis.nodes, i-1)
right = safeget(basis.nodes, i+1)
return x ->
if left ≤ x ≤ basis.nodes[i]
return 1/basis.h * (x - left)
elseif basis.nodes[i] ≤ x ≤ right
return 1/basis.h * (-x + right)
else
return 0
end
end
# Construct an interpolation by taking a dot product of basic functions and node values
function linear_combination(basis::Basis, coefficients::Array{Float64, 1}, shift::Function, result_length::Int64)::Tuple{Array{Float64, 1}, Array{Float64, 1}}
domain = range(basis.a, basis.b, length = result_length)
image = zeros(result_length)
for i in eachindex(coefficients)
image += map(basis[i], domain) .* coefficients[i]
end
image += map(shift, domain)
return domain, image
end
# Arrange lhs bilinear form matrix of Galerkin system
function lhs_matrix(basis::Basis)::Matrix{Float64}
n = basis.n
lhs = zeros(n - 2, n - 2)
diagonal = 2.0 / basis.h
offset = -1.0 / basis.h
for i in 2:n-3
lhs[i, i] = diagonal
lhs[i, i-1] = offset
lhs[i, i+1] = offset
end
lhs[1, 1] = diagonal
lhs[1, 2] = offset
lhs[n-2, n-2] = diagonal
lhs[n-2, n-3] = offset
return lhs
end
# Simple rectangular method integration (Riemannian sum)
function integral(start_point::Float64, end_point::Float64, integrand::Function)::Float64
integral_width = (end_point - start_point) / integral_steps
return sum(map(integrand, range(start_point, end_point, length = integral_steps)) .* integral_width)
end
# Arrange rhs functional values of Galerkin system
function rhs_vector(basis::Basis, mass_distribution::Function, shift_slope::Float64)::Array{Float64, 1}
return [
(
-4π * G *integral(basis.nodes[i-1], basis.nodes[i+1], x -> basis[i](x) * mass_distribution(x)) -
integral(basis.nodes[i-1], basis.nodes[i+1], x -> (
if basis.nodes[i-1] ≤ x ≤ basis.nodes[i]
return shift_slope / basis.h
elseif basis.nodes[i] < x ≤ basis.nodes[i+1]
return -shift_slope / basis.h
else
return 0.0
end
)
)
) for i in 2:basis.n-1
]
end
# Set up the basis, arrange lhs bilinear form and rhs functional, solve Galerkin equations and construct a dense, plot-ready solution
function solve_FEM(a::Float64, b::Float64, val_a::Float64, val_b::Float64, n_nodes::Int64, mass_distribution::Function, result_length)::Tuple{Array{Float64, 1}, Array{Float64, 1}}
basis::Basis = Basis(a, b, n_nodes)
shift_slope::Float64 = (val_b - val_a) / (b - a)
shift = x -> shift_slope * (x - a) + val_a
node_values = [0.0; lhs_matrix(basis) \ rhs_vector(basis, mass_distribution, shift_slope); 0.0]
return linear_combination(basis, node_values, shift, result_length)
end
# Main function providing solution to the task
function main(n_nodes::Int64 = 100)
n_points = 1000
mass_distribution = x -> (1.0 ≤ x ≤ 2.0 ? 1.0 : 0.0)
a = 0.0
b = 3.0
val_a = 5.0
val_b = 4.0
domain, image = solve_FEM(a, b, val_a, val_b, n_nodes, mass_distribution, n_points)
plot(domain, image, legend = false)
end