-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmappings.py
executable file
·248 lines (194 loc) · 7.03 KB
/
mappings.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
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
# -*- coding: utf-8 -*-
"""
@author: Samuel A. Maloney
"""
import numpy as np
from abc import ABCMeta, abstractmethod
class Mapping(metaclass=ABCMeta):
@property
@abstractmethod
def name(self):
raise NotImplementedError
@abstractmethod
def __call__(self, points, zeta=0.):
"""Compute mapped y-coordinates from all points to given x-coordinate
Parameters
----------
points : numpy.ndarray, shape=(n,ndim)
(x,y) coordinates of starting points.
zeta : float
x-coordinate value of plane to which points should be mapped.
Returns
-------
numpy.ndarray, shape=(n,)
Values of y-coordinate for all points mapped to given x-coordinate.
"""
raise NotImplementedError
@abstractmethod
def deriv(self, points):
"""Compute dy/dx derivative of mapping function at given points.
Parameters
----------
points : numpy.ndarray, shape=(n,ndim)
(x,y) coordinates of evaluation points.
Returns
-------
numpy.ndarray, shape=(n,)
Values of dy/dx evaluated at all points.
"""
raise NotImplementedError
def B(self, points):
"""Compute x-coordinates of boundary intersections from given points.
Only required if Dirichlet BC used in y-direction.
Parameters
----------
points : numpy.ndarray, shape=(n,ndim)
(x,y) coordinates of evaluation points.
Returns
-------
zetaBottom : numpy.ndarray, shape=(n,)
x-coordinates of mapping intersections with bottom boundary.
If no such intersection exists then np.nan is returned.
zetaTop : numpy.ndarray, shape=(n,)
x-coordinates of mapping intersections with top boundary.
If no such intersection exists then numpy.nan is returned.
"""
raise NotImplementedError
def dBtop(self, points):
"""Compute gradient of top boundary function at given points.
Only required if Dirichlet BC used in y-direction.
Parameters
----------
points : numpy.ndarray, shape=(n,ndim)
(x,y) coordinates of evaluation points.
Returns
-------
dBdx : numpy.ndarray, shape=(n,)
x-derivatives of top boundary function at given points.
dBdy : numpy.ndarray, shape=(n,)
y-derivatives of top boundary function at given points.
"""
raise NotImplementedError
def dBbottom(self, points):
"""Compute gradient of bottom boundary function at given points.
Only required if Dirichlet BC used in y-direction.
Parameters
----------
points : numpy.ndarray, shape=(n,ndim)
(x,y) coordinates of evaluation points.
Returns
-------
dBdx : numpy.ndarray, shape=(n,)
x-derivatives of bottom boundary function at given points.
dBdy : numpy.ndarray, shape=(n,)
y-derivatives of bottom boundary function at given points.
"""
raise NotImplementedError
def __repr__(self):
return f"{self.__class__.__name__}()"
class StraightMapping(Mapping):
@property
def name(self):
return 'straight'
def __init__(self):
self.dBtop = self.dB
self.dBbottom = self.dB
def __call__(self, points, zeta=0.):
y = points.reshape(-1,2)[:,1]
return y
def deriv(self, points):
nPoints = int(points.size / 2)
return np.zeros(nPoints)
def B(self, points):
nPoints = points.size // 2
return np.full(nPoints, np.nan), np.full(nPoints, np.nan)
def dB(self, points):
print('Error: Straight mapping should never intersect y-boundary.')
# this should never be used, just return dummy values
nPoints = points.size // 2
return np.ones(nPoints), np.ones(nPoints)
class LinearMapping(Mapping):
@property
def name(self):
return 'linear'
def __init__(self, slope):
self.slope = slope
self.dBtop = self.dB
self.dBbottom = self.dB
def __call__(self, points, zeta=0.):
x = points.reshape(-1,2)[:,0]
y = points.reshape(-1,2)[:,1]
return y + self.slope*(zeta - x)
def deriv(self, points):
nPoints = int(points.size / 2)
return np.repeat(self.slope, nPoints)
def B(self, points):
x = points.reshape(-1,2)[:,0]
y = points.reshape(-1,2)[:,1]
zetaBottom = x - y/self.slope
zetaTop = x + (1-y)/self.slope
return zetaBottom, zetaTop
def dB(self, points):
nPoints = points.size // 2
return np.ones(nPoints), np.full(nPoints, -1./self.slope)
def __repr__(self):
return f"{self.__class__.__name__}({self.slope})"
class QuadraticMapping(Mapping):
@property
def name(self):
return 'quadratic'
def __init__(self, a, b=0.):
self.a = a
self.b = b
def __call__(self, points, zeta=0.):
x = points.reshape(-1,2)[:,0]
y = points.reshape(-1,2)[:,1]
return y + self.a*(zeta**2 - x**2) + self.b*(zeta - x)
def deriv(self, points):
x = points.reshape(-1,2)[:,0]
return 2*self.a*x + self.b
def B(self, points):
x = points.reshape(-1,2)[:,0]
y = points.reshape(-1,2)[:,1]
a = self.a
b = self.b
zetaBottom = (-b + np.sqrt(b**2 - 4*a*(y - a*x**2 - b*x)))/(2*a)
zetaTop = (-b + np.sqrt(b**2 - 4*a*(y - a*x**2 - b*x - 1)))/(2*a)
return zetaBottom, zetaTop
def dBtop(self, points):
x = points.reshape(-1,2)[:,0]
y = points.reshape(-1,2)[:,1]
a = self.a
b = self.b
dBdy = -1 / np.sqrt(b**2 - 4*a*(y - a*x**2 - b*x - 1))
dBdx = -(2*a*x + b) * dBdy
return dBdx, dBdy
def dBbottom(self, points):
x = points.reshape(-1,2)[:,0]
y = points.reshape(-1,2)[:,1]
a = self.a
b = self.b
dBdy = -1 / np.sqrt(b**2 - 4*a*(y - a*x**2 - b*x))
dBdx = -(2*a*x + b) * dBdy
return dBdx, dBdy
def __repr__(self):
return f"{self.__class__.__name__}({self.a}, {self.b})"
class SinusoidalMapping(Mapping):
@property
def name(self):
return 'sinusoidal'
def __init__(self, amplitude, phase, xmax=2*np.pi):
self.A = amplitude
self.phase = phase
self.xmax = xmax
self.xfac = 2*np.pi/xmax
def __call__(self, points, zeta=0.):
x = points.reshape(-1,2)[:,0]
y = points.reshape(-1,2)[:,1]
offsets = y - self.A*np.sin(self.xfac*(x - self.phase))
return (self.A*np.sin(self.xfac*(zeta - self.phase)) + offsets)
def deriv(self, points):
x = points.reshape(-1,2)[:,0]
return self.A*self.xfac*np.cos(self.xfac*(x - self.phase))
def __repr__(self):
return f"{self.__class__.__name__}({self.A}, {self.phase}, {self.xmax})"