-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathproblems.py
More file actions
171 lines (136 loc) · 5.58 KB
/
problems.py
File metadata and controls
171 lines (136 loc) · 5.58 KB
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
import torch
import numpy as np
def problem_OAF(param, scaling = True):
'''
Function that creates matrix A and vector b for the forced harmonic oscillator.
Params:
k = spring constant
m = mass of the spring
nu = force frequency
C = external force intensity
x0 = left boundary
xq = right boundary
dt = time interval
steps = number of time intervals
- scaling, bool: If True, rescale the problem so that the matrix has norm 1.
Output:
- force, torch.Tensor: vector b
- matrix, torch.Tensor: matrix A'''
k, m, nu, C = param['k'], param['m'], param['nu'], param['C']
x0, xq, dt, steps = param['x0'], param['xq'], param['dt'], param['steps']
# Matrix
matrix = torch.eye(steps, dtype=torch.complex128) * (-2 + k/m * dt**2) # Diagonal
for i in range(steps-1): # Non-diagonal
matrix[i,i+1] = 1 # Upper diagonal
matrix[i+1,i] = 1 # Lower diagonal
# External force
force = torch.tensor( [C*np.sin(np.pi*nu*dt)-x0/dt**2] + \
[ C*np.sin(np.pi*nu*(i+1)*dt) for i in range(1, steps-1)] + \
[C*np.sin(np.pi*nu*steps*dt)-xq/dt**2], dtype=torch.complex128)
if scaling == True:
scale = torch.linalg.norm(matrix)
matrix /= scale
force /= scale
return force, matrix
def problem_OAA(param, scaling = True):
'''
Function that creates matrix A and vector b for the forced and damped harmonic oscillator.
Params:
k = spring constant
m = mass of the spring
nu = force frequency
gamma = damping
C = external force intensity
x0 = left boundary
xq = right boundary
dt = time interval
steps = number of time intervals
- scaling, bool: If True, rescale the problem so that the matrix has norm 1.'''
k, m, nu, C, gamma = param['k'], param['m'], param['nu'], param['C'], param['gamma']
x0, xq, dt, steps = param['x0'], param['xq'], param['dt'], param['steps']
# Matrix
matrix = torch.eye(steps, dtype=torch.complex128) * (-2 + k/m * dt**2) # Diagonal
for i in range(steps-1): # Non-diagonal
matrix[i,i+1] = 1+gamma*dt/2
matrix[i+1,i] = 1-gamma*dt/2
# External force
force = torch.tensor( [C*np.sin(np.pi*nu*dt)-(1-gamma*dt/2)*x0/dt**2] + \
[ C*np.sin(np.pi*nu*(i+1)*dt) for i in range(1, steps-1)] + \
[C*np.sin(np.pi*nu*steps*dt)-(1+gamma*dt/2)*xq/dt**2] )
if scaling == True:
scale = torch.linalg.norm(matrix)
matrix /= scale
force /= scale
new_matrix = torch.zeros((2*steps, 2*steps), dtype=torch.complex128)
new_matrix[:steps, steps:] = matrix
new_matrix[steps:, :steps] = matrix.T
new_force = torch.zeros(2*steps, dtype=torch.complex128)
new_force[:steps] = force
return new_force, new_matrix, force, matrix
def problem_C2D(param, scaling = True):
'''
Function that creates matrix A and vector b for the 2D convection-diffusion problem.
Params:
k = diffusion coefficient
u1x, u2x = boundary conditions in x direction
u1y, u2y = boundary conditions in y direction
dxy = spatial discretization step
nx, ny = number of grid points in x and y directions
- scaling, bool: If True, rescale the problem so that the matrix has norm 1.'''
k, u1x, u2x, u1y, u2y = param['k'], param['u1x'], param['u2x'], param['u1y'], param['u2y']
dxy, nx, ny = param['dxy'], param['nx'], param['ny']
# Matrix
matrix = torch.eye(nx*ny, dtype=torch.complex128) * (-4) # Diagonal
# First row (i=0)
matrix[0,ny] = 1
matrix[0,1] = 1
for j in range(1, ny-1): # Non-diagonal
matrix[j,ny+j] = 1
matrix[j,j+1] = 1
matrix[j,j-1] = 1
matrix[ny-1,ny+ny-1] = 1
matrix[ny-1,ny-1-1] = 1
for i in range(1, nx-1):
# First column (j=0)
matrix[i*ny,(i+1)*ny] = 1
matrix[i*ny,(i-1)*ny] = 1
matrix[i*ny,i*ny+1] = 1
for j in range(1, ny-1): # Non-diagonal
matrix[i*ny+j,(i+1)*ny+j] = 1
matrix[i*ny+j,(i-1)*ny+j] = 1
matrix[i*ny+j,i*ny+j+1] = 1
matrix[i*ny+j,i*ny+j-1] = 1
# Last column (j=ny-1)
matrix[i*ny+ny-1,(i+1)*ny+ny-1] = 1
matrix[i*ny+ny-1,(i-1)*ny+ny-1] = 1
matrix[i*ny+ny-1,i*ny+ny-1-1] = 1
# Last row (i=nx-1)
matrix[(nx-1)*ny,((nx-1)-1)*ny] = 1
matrix[(nx-1)*ny,(nx-1)*ny+1] = 1
for j in range(1, ny-1): # Non-diagonal
matrix[(nx-1)*ny+j,((nx-1)-1)*ny+j] = 1
matrix[(nx-1)*ny+j,(nx-1)*ny+j+1] = 1
matrix[(nx-1)*ny+j,(nx-1)*ny+j-1] = 1
matrix[(nx-1)*ny+ny-1,((nx-1)-1)*ny+ny-1] = 1
matrix[(nx-1)*ny+ny-1,(nx-1)*ny+ny-1-1] = 1
# External force
fuerza = torch.zeros((nx, ny), dtype=torch.complex128)
fuerza[0,0] = u1x*k/dxy**2 + u1y*k/dxy**2
for j in range(1, ny-1):
fuerza[0,j] = u1x*k/dxy**2
fuerza[0, ny-1] = u1x*k/dxy**2 + u2y*k/dxy**2
for i in range(1, nx-1):
fuerza[i,0] = u1y*k/dxy**2
for j in range(1, ny-1):
fuerza[i,j] = 10*np.sin(2*np.pi*i*j/np.sqrt(nx*ny))
fuerza[i, ny-1] = u2y*k/dxy**2
fuerza[nx-1,0] = u2x*k/dxy**2 + u1y*k/dxy**2
for j in range(1, ny-1):
fuerza[nx-1,j] = u2x*k/dxy**2
fuerza[nx-1, ny-1] = u2x*k/dxy**2 + u2y*k/dxy**2
fuerza = -fuerza.flatten()*dxy**2/k
if scaling == True:
scalado = torch.linalg.norm(matrix)
matrix /= scalado
fuerza /= scalado
return fuerza, matrix