-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
198 lines (150 loc) · 5.05 KB
/
main.py
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
186
187
188
189
190
191
192
193
194
195
196
197
198
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Fri Apr 27 09:36:54 2018
@author: apreziosir
"""
# Importing modules that are going to be used in the program
import numpy as np
import scipy.sparse as sp
import mapping as mp
import Lag_pder as lpd
import Analyt as AN
import matplotlib.pyplot as plt
from matplotlib import style
# ==============================================================================
# Physical variables
# ==============================================================================
# Initial x coordinate of the phenomena (m)
X0 = 0.
# Final x coordinate of the phenomena (m)
XF = 5.
# Initial time analyzed for the phenomena (s)
T0 = 1.
# Final time for the phenomena (s)
TF = 2.
# Diffusion coefficient in (m2/s)
Dx = 0.3
# Mass of tracer injected to the system
M = 10
# Injection point in the domain
xo = 2
# ==============================================================================
# Numerical parameters
# ==============================================================================
# Stability parameter Sx for diffusion when handling explicit methods
Sx = 0.1
# Crank-Nicholson ponderation factor theta for specific cases
# theta = 0.0 explicit
# theta = 1.0 implicit
theta = 1.
# Number of nodes in the domain
N = 30
# Calculating domain length
L = XF - X0
# ==============================================================================
# Start different calculations given the parameters from the model
# ==============================================================================
# Calculating the dt with the stability parameter
dT = 1e-4
# Defining number of timesteps
nT = int((TF - T0) / dT)
# Error saving
ert = np.zeros(int(nT))
# d(chi)/dx - Constant value that arose from mapping to natural coordinates
dchi_dx = 2 / (XF - X0)
# Calculating GLL points, as N - 2 points and the adding -1 and 1 as extreme
# values (this is already in natural coordinates)
[LGLP, W] = np.polynomial.legendre.leggauss(N - 2)
LGLP = np.insert(LGLP, 0, -1)
LGLP = np.append(LGLP, 1)
# Calculating real coordinates of points from natural coordinates location
xn = mp.mapping(LGLP, X0, XF)
# P coefficient for calculations
P = dchi_dx ** 2 * dT * Dx
# ==============================================================================
# Building the differentiation matrix. I know I need the second derivative
# matrix, but it is a fact that I can square the first derivative matrix to
# obtain the second derivative matrix
# ==============================================================================
# Defining diffusion matrix for the problem (as sparse matrix)
# K = sp.lil_matrix((N, N))
K = np.zeros((N,N))
# Filling each row of the matrix qith the derivatives of the Lagrange
# polynomials
for i in range(0, N):
K[:, i] = lpd.Lag_pder(LGLP, i)
# Calculating second derivative as the matrix matrix multiplication
D2 = np.matmul(K, K)
# Freeing space - take out to prove it works
del(K)
# Final matrix for solving
K = P * D2 - np.identity(N)
K[0, :] = np.zeros(N)
K[N - 1, :] = np.zeros(N)
K[0, 0] = 1.
K[N -1, N - 1] = 1.
plt.spy(K)
plt.draw()
plt.pause(1.5)
plt.clf()
# ==============================================================================
# Initial condition and start up of other parameters
# ==============================================================================
# Generating initial condition
C = AN.difuana(M, XF - X0, Dx, xn, xo, T0)
C1 = np.zeros(N)
Cmax = np.max(C)
# Plotting initial condition
plt.ion()
plt.figure(1, figsize=(11, 8.5))
style.use('ggplot')
plt.subplot(1, 1, 1)
plt.plot(xn, C)
plt.title('Initial condition')
plt.xlabel(r'Distance $(m)$')
plt.ylabel(r'Concentration $ \frac{kg}{m} $')
plt.draw()
plt.pause(1.5)
# Entering time loop
for t in range(1, nT):
# Generating analytical solution
Ca = AN.difuana(M, XF - X0, Dx, xn, xo, T0 + t * dT)
# Setting up right hand side
C[0] = -AN.difuana(M, L, Dx, X0, xo, T0 + t * dT)
C[N - 1] = -AN.difuana(M, L, Dx, XF, xo, T0 + t * dT)
# Solving system (matrix vector multiplication)
C1 = -np.linalg.solve(K, C)
# Estimating error
err = np.abs(C1 - Ca)
ert[t] = np.linalg.norm(err)
# Plotting numerical solution and comparison with analytical
plt.clf()
plt.subplot(2, 2, 1)
plt.plot(xn, C1, 'b')
plt.xlim([X0, XF])
plt.ylim([0, Cmax])
plt.ylabel(r'Concentration $ \frac{kg}{m} $')
plt.title('Numerical solution')
plt.subplot(2, 2, 2)
plt.plot(xn, Ca)
plt.xlim([X0, XF])
plt.ylim([0, Cmax])
plt.title('Analytical solution')
plt.subplot(2, 2, 3)
plt.semilogy(xn, err)
plt.xlim([X0, XF])
plt.ylim([1e-12, 1e-4])
plt.ylabel('Absolute error')
plt.title('Error')
plt.subplot(2, 2, 4)
plt.semilogy(np.linspace(T0, TF, nT), ert)
plt.xlim([T0 - 0.2, TF + 0.2])
plt.ylim([1e-12, 1e-4])
plt.title('Error evolution')
plt.draw()
titulo = 'Spectral elements method in single domain solution implicit'
plt.suptitle(titulo)
plt.pause(0.001)
# Preparing for next timestep
C = C1