Datasets:

Modalities:
Text
Formats:
text
ArXiv:
Libraries:
Datasets
License:
yunplus commited on
Commit
b29fc8a
·
verified ·
1 Parent(s): 17a2db1

Upload 2 files

Browse files
Files changed (2) hide show
  1. postprocess/post.py +588 -0
  2. training_samples_index.txt +0 -0
postprocess/post.py ADDED
@@ -0,0 +1,588 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ '''
2
+
3
+ graph of C-mesh
4
+ ===
5
+ _____________________
6
+ / * *__*_*_|_________|
7
+ | * / |
8
+ | * | ^ i1 |
9
+ | * | | <======-----|
10
+ | * | |____ i0 | <- j=0
11
+ | * \_________________|
12
+ \_*___*__*_|__________| <- j=NJ
13
+
14
+ * the area used to calculate average velocity field
15
+
16
+
17
+ '''
18
+
19
+
20
+
21
+ import numpy as np
22
+ import torch
23
+
24
+ from typing import List, NewType, Tuple, Dict, Union
25
+ Tensor = NewType('Tensor', torch.Tensor)
26
+
27
+ WORKCOD = {'Tinf':460.0,'Minf':0.76,'Re':5e6,'AoA':0.0,'gamma':1.4, 'x_mc':0.25, 'y_mc':0.0}
28
+
29
+ # the base rotation matrix:
30
+ # / (1, 0) (0, 1) \
31
+ # \ (0,-1) (1, 0) /
32
+ # if one want to rotate the vector (x_o, y_o) in origin coordinate (o) to the target coordinate (t),
33
+ # the rotate matrix should be the base matrix dot the origin x unit-vector in the target coordinate.
34
+ # for example: transfer force (f_x, f_y) to lift and drag
35
+ # - the target coor.(along the freestream) can be obtained by rotate the origin coor.(along the chord)
36
+ # a angle of AoA c.c.w.
37
+ # - the x unit-vector in target coor. is / cos(AoA) \
38
+ # \ -sin(AoA) /
39
+ #
40
+ # - thus, ( Drag, Lift ) = ( f_x, f_y ) . / (1, 0) (0, 1) \ . / cos(AoA) \
41
+ # \ (0,-1) (1, 0) / \ -sin(AoA) /
42
+
43
+ #* here collect the Tensor version
44
+ # original numpy version can be found in cfdpost.utils
45
+
46
+ _rot_metrix = torch.Tensor([[[1.0,0], [0,1.0]], [[0,-1.0], [1.0,0]]])
47
+
48
+ #* function to rotate x-y to aoa
49
+
50
+ def _aoa_rot_t(aoa: Tensor) -> Tensor:
51
+ '''
52
+ aoa is in size (B, )
53
+
54
+ '''
55
+ aoa = aoa * np.pi / 180
56
+ return torch.cat((torch.cos(aoa).unsqueeze(1), -torch.sin(aoa).unsqueeze(1)), dim=1)#.squeeze(-1)
57
+
58
+ def _xy_2_cl_t(dfp: Tensor, aoa: float) -> Tensor:
59
+ '''
60
+ transfer fx, fy to CD, CL
61
+
62
+ param:
63
+ dfp: (Fx, Fy), Tensor with size (2,)
64
+ aoa: angle of attack, float
65
+
66
+ return:
67
+ ===
68
+ Tensor: (CD, CL)
69
+ '''
70
+ aoa = torch.FloatTensor([aoa])
71
+ # print(dfp.size(), _rot_metrix.size(), _aoa_rot_t(aoa).size())
72
+ return torch.einsum('p,prs,s->r', dfp, _rot_metrix.to(dfp.device), _aoa_rot_t(aoa).squeeze().to(dfp.device))
73
+
74
+ def _xy_2_cl_tc(dfp: Tensor, aoa: Tensor) -> Tensor:
75
+ '''
76
+ batch version of _xy_2_cl
77
+
78
+ transfer fx, fy to CD, CL
79
+
80
+ param:
81
+ dfp: (Fx, Fy), Tensor with size (B, 2,)
82
+ aoa: angle of attack, Tensor with size (B, )
83
+
84
+ return:
85
+ ===
86
+ Tensor: (CD, CL), with size (B, 2)
87
+ '''
88
+ # print(dfp.shape, _rot_metrix.shape, aoa.shape, _aoa_rot_t(aoa).shape)
89
+ return torch.einsum('bp,prs,bs->br', dfp, _rot_metrix.to(dfp.device), _aoa_rot_t(aoa).to(dfp.device))
90
+
91
+ #* function to extract information from 2-D flowfield
92
+ def get_aoa(vel):
93
+ '''
94
+ This function is to extract the angle of attack(AoA) from the far-field velocity field
95
+
96
+ param:
97
+ ===
98
+ `vel`: the velocity field, shape: (2 x H x W), the two channels should be U and V (x and y direction velocity)
99
+ only the field at the front and farfield is used to averaged (see comments of post.py)
100
+
101
+ return:
102
+ ===
103
+ (torch.Tensor): the angle of attack
104
+
105
+ '''
106
+
107
+ # inlet_avg = torch.mean(vel[:, 3: -3, -5:-2], dim=(1, 2))
108
+ inlet_avg = torch.mean(vel[:, 100: -100, -5:-2], dim=(1, 2))
109
+ # inlet_avg = torch.mean(vel[:, 3: -3, -1], dim=1)
110
+
111
+ return torch.atan(inlet_avg[1] / inlet_avg[0]) / 3.14 * 180
112
+
113
+ def get_p_line(X, P, i0=15, i1=316):
114
+ '''
115
+ This function is to extract p values at the airfoil surface from the P field
116
+
117
+ The surface p value is obtained by averaging the four corner values on each first layer grid
118
+
119
+ param:
120
+ ===
121
+ `X`: The X field, shape: (H x W)
122
+
123
+ `P`: The P field, shape: (H x W)
124
+
125
+ `i0` and `i1`: The position of the start and end grid number of the airfoil surface
126
+
127
+ return:
128
+ ===
129
+ Tuple(torch.Tensor, list): X, P (shape of each: (i1-i0, ))
130
+ '''
131
+ p_cen = []
132
+ for j in range(i0, i1):
133
+ p_cen.append(-0.25 * (P[j, 0] + P[j, 1] + P[j+1, 0] + P[j+1, 1]))
134
+ return X[i0: i1, 0], p_cen
135
+
136
+ def get_vector(X: Tensor, Y: Tensor, i0: int, i1: int):
137
+ '''
138
+ get the geometry variables on the airfoil surface
139
+
140
+ remark:
141
+ ===
142
+ ** `should only run once at the begining, since is very slow` **
143
+
144
+ param:
145
+ ===
146
+ `X`: The X field, shape: (H x W)
147
+
148
+ `Y`: The Y field, shape: (H x W)
149
+
150
+ `i0` and `i1`: The position of the start and end grid number of the airfoil surface
151
+
152
+ return:
153
+ ===
154
+ Tuple(torch.Tensor): `_vec_sl`, `_veclen`, `_area`
155
+
156
+ `_vec_sl`: shape : (i1-i0-1, 2), the surface section vector (x2-x1, y2-y1)
157
+
158
+ `_veclen`: shape : (i1-i0-1, ), the length of the surface section vector
159
+
160
+ `area`: shape : (i1-i0-1, ), the area of the first layer grid (used to calculate tau)
161
+ '''
162
+ _vec_sl = torch.zeros((i1-i0-1, 2,))
163
+ _veclen = torch.zeros(i1-i0-1)
164
+ _area = torch.zeros(i1-i0-1)
165
+ # _sl_cen = np.zeros((i1-i0-1, 2))
166
+
167
+ for idx, j in enumerate(range(i0, i1-1)):
168
+
169
+ point1 = torch.Tensor([X[j, 0], Y[j, 0], 0]) # coordinate of surface point j
170
+ point2 = torch.Tensor([X[j, 1], Y[j, 1], 0])
171
+ point3 = torch.Tensor([X[j + 1, 0], Y[j + 1, 0], 0])
172
+ point4 = torch.Tensor([X[j + 1, 1], Y[j + 1, 1], 0])
173
+
174
+ vec_sl = point3 - point1 # surface vector sl
175
+ _veclen[idx] = torch.sqrt((vec_sl * vec_sl).sum()) # length of surface vector sl
176
+ _vec_sl[idx] = (vec_sl / _veclen[idx])[:2]
177
+ ddiag = torch.cross(point4 - point1, point3 - point2)
178
+ _area[idx] = 0.5 * torch.sqrt((ddiag * ddiag).sum())
179
+
180
+ # _sl_cen[idx] = 0.5 * (point1 + point3)
181
+
182
+ return _vec_sl, _veclen, _area
183
+
184
+ def get_force_xy(vec_sl: Tensor, veclen: Tensor, area: Tensor,
185
+ vel: Tensor, T: Tensor, P: Tensor,
186
+ i0: int, i1: int, paras: Dict, ptype: str = 'Cp'):
187
+ '''
188
+ integrate the force on x and y direction
189
+
190
+ param:
191
+ `_vec_sl`, `_veclen`, `_area`: obtained by _get_vector
192
+
193
+ `vel`: the velocity field, shape: (2 x H x W), the two channels should be U and V (x and y direction velocity)
194
+
195
+ `T`: The temperature field, shape: (H x W)
196
+
197
+ `P`: The pressure field, shape: (H x W); should be non_dimensional pressure field by CFL3D
198
+
199
+ `i0` and `i1`: The position of the start and end grid number of the airfoil surface
200
+
201
+ `paras`: the work condtion to non-dimensionalize; should include the key of (`gamma`, `Minf`, `Tinf`, `Re`)
202
+
203
+ return:
204
+ ===
205
+ Tensor: (Fx, Fy)
206
+ '''
207
+
208
+ p_cen = 0.25 * (P[i0:i1-1, 0] + P[i0:i1-1, 1] + P[i0+1:i1, 0] + P[i0+1:i1, 1])
209
+ t_cen = 0.25 * (T[i0:i1-1, 0] + T[i0:i1-1, 1] + T[i0+1:i1, 0] + T[i0+1:i1, 1])
210
+ uv_cen = 0.5 * (vel[:, i0:i1-1, 1] + vel[:, i0+1:i1, 1])
211
+
212
+ # if ptype == 'P':
213
+ # dfp_n = 1.43 / (paras['gamma'] * paras['Minf']**2) * (paras['gamma'] * p_cen - 1) * veclen
214
+ # else:
215
+ # dfp_n = p_cen * veclen
216
+ dfp_n = 1.43 / (paras['gamma'] * paras['Minf']**2) * (paras['gamma'] * p_cen - 1) * veclen
217
+ mu = t_cen**1.5 * (1 + 198.6 / paras['Tinf']) / (t_cen + 198.6 / paras['Tinf'])
218
+ dfv_t = 0.063 / (paras['Minf'] * paras['Re']) * mu * torch.einsum('kj,jk->j', uv_cen, vec_sl) * veclen**2 / area
219
+
220
+ # cx, cy
221
+ dfp = torch.einsum('lj,lpk,jk->p', torch.cat((dfv_t.unsqueeze(0), -dfp_n.unsqueeze(0)),dim=0), _rot_metrix.to(dfv_t.device), vec_sl)
222
+
223
+ return dfp
224
+
225
+ def get_force_cl(aoa: float, **kwargs):
226
+ '''
227
+ get the lift and drag
228
+
229
+ param:
230
+ `aoa`: angle of attack
231
+
232
+ `_vec_sl`, `_veclen`, `_area`: obtained by _get_vector
233
+
234
+ `vel`: the velocity field, shape: (2 x H x W), the two channels should be U and V (x and y direction velocity)
235
+
236
+ `T`: The temperature field, shape: (H x W)
237
+
238
+ `P`: The pressure field, shape: (H x W); should be non_dimensional pressure field by CFL3D
239
+
240
+ `i0` and `i1`: The position of the start and end grid number of the airfoil surface
241
+
242
+ `paras`: the work condtion to non-dimensionalize; should include the key of (`gamma`, `Minf`, `Tinf`, `Re`)
243
+
244
+ return:
245
+ ===
246
+ Tensor: (CD, CL)
247
+ '''
248
+ dfp = get_force_xy(**kwargs)
249
+ fld = _xy_2_cl(dfp, aoa)
250
+ return fld
251
+
252
+ #* function to extract pressure force from 1-d pressure profile
253
+ # numpy.ndarray version in `cfdpost.utils`
254
+ def get_dxyforce_1d_t(geom: Tensor, cp: Tensor, cf: Tensor=None) -> Tensor:
255
+ '''
256
+ integrate the force on each surface grid cell, batch data
257
+
258
+ paras:
259
+ ---
260
+ - `geom` Tensor (B, N, 2) -> (x, y)
261
+ - `cp` Tensor (B, N)
262
+ - `cf` Tensor (B, N), default is `None`
263
+
264
+ ### retrun
265
+ Tensor (B, N-1, 2) -> (dFx, dFy)
266
+
267
+ '''
268
+
269
+ dfp_n = (0.5 * (cp[:, 1:] + cp[:, :-1])).unsqueeze(1)
270
+ if cf is None:
271
+ dfv_t = torch.zeros_like(dfp_n)
272
+ else:
273
+ dfv_t = (0.5 * (cf[:, 1:] + cf[:, :-1])).unsqueeze(1)
274
+
275
+ dr = (geom[:, 1:] - geom[:, :-1])
276
+ # print(torch.cat((dfv_t, -dfp_n), dim=1).shape, dr.shape)
277
+ return torch.einsum('blj,lpk,bjk->bjp', torch.cat((dfv_t, -dfp_n), dim=1), _rot_metrix.to(dfv_t.device), dr)
278
+
279
+ def get_xyforce_1d_t(geom: Tensor, cp: Tensor, cf: Tensor=None) -> Tensor:
280
+ '''
281
+ integrate the force on x and y direction
282
+
283
+ param:
284
+ ===
285
+ - `geom` Tensor (B, N, 2) -> (x, y)
286
+ - `cp` Tensor (B, N)
287
+ The pressure profile; should be non_dimensional pressure profile by freestream condtion
288
+
289
+ `Cp = (p - p_inf) / 0.5 * rho * U^2`
290
+
291
+ - `cf` Tensor (B, N), default is `None`
292
+ The friction profile; should be non_dimensional pressure profile by freestream condtion
293
+
294
+ `Cf = tau / 0.5 * rho * U^2`
295
+
296
+ return:
297
+ ===
298
+ Tensor: (B, 2) -> (Fx, Fy)
299
+ '''
300
+
301
+ dr_tail = geom[:, 0] - geom[:, -1]
302
+ dfp_n_tail = 0.5 * (cp[:, 0] + cp[:, -1]).unsqueeze(1)
303
+ dfv_t_tail = torch.zeros_like(dfp_n_tail)
304
+
305
+ force_surface = torch.sum(get_dxyforce_1d_t(geom, cp, cf), dim=1)
306
+ force_tail = torch.einsum('bl,lpk,bk->bp', torch.cat((dfv_t_tail, -dfp_n_tail), dim=1), _rot_metrix.to(dfp_n_tail.device), dr_tail)
307
+
308
+ return force_surface + force_tail
309
+
310
+ def get_force_1d_t(geom: Tensor, aoa: Tensor, cp: Tensor, cf: Tensor=None) -> Tensor:
311
+ '''
312
+ batch version of integrate the lift and drag
313
+
314
+ param:
315
+ ===
316
+ - `geom` Tensor (B, N, 2) -> (x, y)
317
+ - `cp` Tensor (B, N)
318
+ The pressure profile; should be non_dimensional pressure profile by freestream condtion
319
+
320
+ `Cp = (p - p_inf) / 0.5 * rho * U^2`
321
+
322
+ - `cf` Tensor (B, N), default is `None`
323
+ The friction profile; should be non_dimensional pressure profile by freestream condtion
324
+
325
+ `Cf = tau / 0.5 * rho * U^2`
326
+
327
+ - `aoa` Tensor (B,), in angle degree
328
+
329
+ return:
330
+ ===
331
+ Tensor: (B, 2) -> (CD, CL)
332
+ '''
333
+
334
+ dfp = get_xyforce_1d_t(geom, cp, cf)
335
+ return _xy_2_cl_tc(dfp, aoa)
336
+
337
+ def get_flux_1d_t(geom: Tensor, pressure: Tensor, xvel: Tensor, yvel: Tensor, rho: Tensor) -> Tensor:
338
+ '''
339
+ obtain the mass and momentum flux through a line
340
+
341
+ param:
342
+ ===
343
+ `geom`: The geometry (x, y), shape: (2, N)
344
+
345
+ `pressure`: The pressure on every line points, shape: (N, ); should be dimensional pressure profile
346
+
347
+ `xvel`: x-direction velocity on every line points, shape: (N, )
348
+
349
+ `yvel`: y-direction velocity on every line points, shape: (N, )
350
+
351
+ `rho`: density on every line points, shape: (N, )
352
+
353
+ return:
354
+ ===
355
+ Tensor: (mass_flux, moment_flux)
356
+ '''
357
+
358
+ dx = (geom[0, 1:] - geom[0, :-1])
359
+ dy = (geom[1, 1:] - geom[1, :-1])
360
+ pressure = 0.5 * (pressure[1:] + pressure[:-1])
361
+ xvel = 0.5 * (xvel[1:] + xvel[:-1])
362
+ yvel = 0.5 * (yvel[1:] + yvel[:-1])
363
+ rho = 0.5 * (rho[1:] + rho[:-1])
364
+
365
+ phixx = rho * xvel**2 + pressure
366
+ phixy = rho * xvel * yvel
367
+ phiyy = rho * yvel**2 + pressure
368
+
369
+ mass_flux = torch.sum(rho * xvel * dy - rho * yvel * dx)
370
+ moment_flux = torch.zeros((2,))
371
+ moment_flux[0] = torch.sum(phixx * dy - phixy * dx)
372
+ moment_flux[1] = torch.sum(phixy * dy - phiyy * dx)
373
+
374
+ return mass_flux, moment_flux
375
+
376
+ #* functions to get force from 2-D surfaces
377
+
378
+ def get_cellinfo_2d_t(geom: Tensor) -> Tuple[Tensor]:
379
+
380
+ '''
381
+ get the normal vector and area of each surface grid cell
382
+ :param geom: The geometry (x, y, z)
383
+ :type geom: torch.Tensor (..., I, J, 3)
384
+ :return: normals and areas
385
+ :rtype: Tuple(torch.Tensor, torch.Tensor), shape (..., I-1, J-1, 3), (..., I-1, J-1)
386
+ '''
387
+
388
+ # get corner points(p0, p1, p2, p3)
389
+ p0 = geom[..., :-1, :-1, :] # SW
390
+ p1 = geom[..., :-1, 1:, :] # SE
391
+ p2 = geom[..., 1:, 1:, :] # NW
392
+ p3 = geom[..., 1:, :-1, :] # NE
393
+
394
+ # calculate two groups of normal vector and average
395
+ normals = torch.cross(p2 - p0, p3 - p1, dim=-1)
396
+ areas = 0.5 * (torch.linalg.norm(torch.cross(p1 - p0, p2 - p0, dim=-1), dim=-1) + torch.linalg.norm(torch.cross(p2 - p0, p3 - p0, dim=-1), dim=-1))
397
+
398
+ # normalization
399
+ normals = normals / (torch.linalg.norm(normals, dim=-1, keepdim=True) + 1e-20)
400
+ # print(np.sum(normals * areas[..., np.newaxis], axis=(0,1)))
401
+ return normals, areas
402
+
403
+ def get_dxyforce_2d_t(geom: Union[Tensor, List[Tensor]], cp: Tensor, cf: Tensor=None) -> Tensor:
404
+ '''
405
+ integrate forces from 2D surface data on every surface grid cell
406
+
407
+ :param geom: The geometry (x, y, z)
408
+ :type geom: torch.Tensor (..., I, J, 3)
409
+ :param cp: pressure coefficients Cp = (p - p_inf) / 0.5 * rho * U_\infty^2
410
+ :type cp: torch.Tensor (..., I-1, J-1)
411
+ :param cf: friction coefficients Cf = (tau @ n) / 0.5 * rho * U_\infty^2
412
+ :type cf: torch.Tensor (..., I-1, J-1, 3)
413
+
414
+ :return: coefficients of forces in x, y, z directions
415
+ :rtype: torch.Tensor, (dCx, dCy, dCz), shape (..., I-1, J-1, 3)
416
+
417
+ '''
418
+ # calculate normal vector
419
+ if isinstance(geom, list):
420
+ n, a = geom
421
+ else:
422
+ n, a = get_cellinfo_2d_t(geom)
423
+ dfp = cp[..., None] * n * a[..., None]
424
+
425
+ if not (cf is None or len(cf) == 0):
426
+ shear = (cf - torch.sum(cf * n, dim=-1, keepdim=True) * n) * a[..., None]
427
+ dfp = dfp + shear
428
+
429
+ return dfp
430
+
431
+ def get_xyforce_2d_t(geom: Union[Tensor, List[Tensor]], cp: Tensor, cf: Tensor=None) -> Tensor:
432
+ '''
433
+ integrate forces from 2D surface data
434
+
435
+ :param geom: The geometry (x, y, z)
436
+ :type geom: torch.Tensor (..., I, J, 3)
437
+ :param cp: pressure coefficients Cp = (p - p_inf) / 0.5 * rho * U_\infty^2
438
+ :type cp: torch.Tensor (..., I-1, J-1)
439
+ :param cf: friction coefficients Cf = (tau @ n) / 0.5 * rho * U_\infty^2
440
+ :type cf: torch.Tensor (..., I-1, J-1, 3)
441
+
442
+ :return: coefficients of forces in x, y, z directions
443
+ :rtype: torch.Tensor (CX, CY, CZ)
444
+ '''
445
+ return torch.sum(get_dxyforce_2d_t(geom, cp, cf), dim=(-3,-2))
446
+
447
+ def get_force_2d_t(geom: Union[Tensor, List[Tensor]], aoa: Tensor, cp: Tensor, cf: Tensor=None) -> Tensor:
448
+ '''
449
+ integrate lift and drag from 2D surface data
450
+
451
+ :param geom: The geometry (x, y, z)
452
+ :type geom: torch.Tensor (..., I, J, 3)
453
+ :param aoa: angle of attack in Degree
454
+ :type aoa: torch.Tensor (..., )
455
+ :param cp: pressure coefficients Cp = (p - p_inf) / 0.5 * rho * U_\infty^2
456
+ :type cp: torch.Tensor (..., I-1, J-1)
457
+ :param cf: friction coefficients Cf = (tau @ n) / 0.5 * rho * U_\infty^2
458
+ :type cf: torch.Tensor (..., I-1, J-1, 3)
459
+
460
+ :return: coefficients of drag, lift, and side force
461
+ :rtype: torch.Tensor (CD, CL, CZ)
462
+ '''
463
+ dfp = get_xyforce_2d_t(geom, cp, cf)
464
+ dfp_xy = _xy_2_cl_tc(dfp[..., :2], aoa)
465
+ dfp = torch.concatenate((dfp_xy, dfp[..., 2:]), axis=-1)
466
+ return dfp
467
+
468
+ def get_moment_2d_t(geom: torch.Tensor, cp: torch.Tensor, cf: torch.Tensor=None, ref_point: torch.Tensor=np.array([0.25, 0, 0])) -> torch.Tensor:
469
+ '''
470
+ :param geom: The geometry (x, y, z)
471
+ :type geom: torch.Tensor (..., I, J, 3)
472
+ :param cp: pressure coefficients Cp = (p - p_inf) / 0.5 * rho * U_\infty^2
473
+ :type cp: torch.Tensor (..., I-1, J-1)
474
+ :param cf: friction coefficients Cf = (tau @ n) / 0.5 * rho * U_\infty^2
475
+ :type cf: torch.Tensor (..., I-1, J-1, 3)
476
+ :param ref_point: ref point for moment calculation
477
+ :type ref_point: torch.Tensor (..., 3)
478
+
479
+ :return: moment around z-axis
480
+ :rtype: torch.Tensor (CMx, CMy, CMz)
481
+ '''
482
+
483
+ dxyforce = get_dxyforce_2d_t(geom, cp, cf)
484
+ r = 0.25 * (geom[..., :-1, :-1, :] + geom[..., :-1, 1:, :] + geom[..., 1:, 1:, :] + geom[..., 1:, :-1, :]) - ref_point.to(geom.device)
485
+
486
+ return torch.sum(torch.cross(r, dxyforce, dim=-1), dim=(-3, -2))
487
+
488
+ def get_cellinfo_1d_t(geom: torch.Tensor) -> Tuple[torch.Tensor]:
489
+ '''
490
+ :param geom: The geometry (x, y)
491
+ :type geom: torch.Tensor (..., I, 2)
492
+
493
+ :return: tangens, normals
494
+ :rtype: Tuple[torch.Tensor] (..., I-1, 2), (..., I-1, 2)
495
+ '''
496
+
497
+ # grid centric
498
+ tangens = geom[..., 1:, :] - geom[..., :-1, :]
499
+ tangens = tangens / (torch.linalg.norm(tangens, dim=-1, keepdim=True) + 1e-20)
500
+ normals = torch.concatenate((-tangens[..., [1]], tangens[..., [0]]), axis=-1)
501
+
502
+ return tangens, normals
503
+
504
+ #* functions for wings
505
+
506
+ def rotate_input(inp: torch.Tensor, cnd: torch.Tensor, root_twist: float = 6.7166) -> Tuple[torch.Tensor]:
507
+ '''
508
+ rotate the input and condition to remove the baseline twist effect
509
+
510
+ :param inp: geometric mesh input
511
+ :type inp: torch.Tensor (B, C, H, W)
512
+ :param cnd: operating condition (AoA, Mach)
513
+ :type cnd: torch.Tensor (B, 2)
514
+ :param root_twist: The root twist value to be removed
515
+ :type root_twist: float
516
+ :return: inp, cnd
517
+ :rtype: Tuple[Tensor]
518
+ '''
519
+
520
+ B, C, H, W = inp.shape
521
+
522
+ # rotate to without baseline twist ( w.r.t centerline LE (0,0,0))
523
+ inp = torch.cat([
524
+ _xy_2_cl_tc(inp[:, :2].permute(0, 2, 3, 1).reshape(-1, 2), -root_twist * torch.ones((B*H*W,))).reshape(B, H, W, 2).permute(0, 3, 1, 2),
525
+ inp[:, 2:]
526
+ ], dim = 1)
527
+
528
+ cnd = torch.cat([
529
+ cnd[:, :1] + root_twist,
530
+ cnd[:, 1:]
531
+ ], dim = 1)
532
+
533
+ return inp, cnd
534
+
535
+ def intergal_output(geom: torch.Tensor, outputs: torch.Tensor, aoa: torch.Tensor,
536
+ s: float, c: float, xref: float, yref: float) -> torch.Tensor:
537
+ '''
538
+ torch version intergal_output from cell-centric outputs to forces/moments
539
+
540
+ :param geom: geometric
541
+ :type geom: torch.Tensor (B, 3, I, J)
542
+ :param outputs: pressure and friction coefficients (cp, cf_tau, cf_z)
543
+ :type outputs: torch.Tensor (B, 3, I-1, J-1)
544
+ :param aoa: angle of attacks
545
+ :type aoa: torch.Tensor (B, )
546
+ :param s: reference area
547
+ :type s: float
548
+ :param c: reference chord
549
+ :type c: float
550
+ :param xref: x reference point
551
+ :type xref: float
552
+ :param yref: y reference point
553
+ :type yref: float
554
+ :return: lift, drag, moment_z
555
+ :rtype: torch.Tensor (B, 3)
556
+ '''
557
+
558
+ cp = outputs[:, 0]
559
+ tangens, normals2d = get_cellinfo_1d_t(geom[:, :2].permute(0, 2, 3, 1))
560
+ tangens = 0.5 * (tangens[:, 1:] + tangens[:, :-1]) # transfer to cell centre at spanwise direction
561
+
562
+ cf = torch.concatenate((outputs[:, [1]].permute(0, 2, 3, 1) * tangens / 150, outputs[:, [2]].permute(0, 2, 3, 1) / 300), axis=-1)
563
+ forces = get_force_2d_t(geom.permute(0, 2, 3, 1), aoa=aoa, cp=cp, cf=cf)[:, [1, 0]] / s
564
+ moment = get_moment_2d_t(geom.permute(0, 2, 3, 1), cp=cp, cf=cf,
565
+ ref_point=torch.Tensor([xref, yref, 0.]))[:, [2]] / s / c
566
+
567
+ return torch.cat((forces, moment), dim=-1)
568
+
569
+ def _get_xz_cf_t(geom: torch.Tensor, cf: torch.Tensor):
570
+ '''
571
+ params:
572
+ ===
573
+ `geom`: The geometry (x, y), shape: (..., Z, I, 3)
574
+ `cf`: The geometry (cft, cfz), shape: (..., Z, I, 2)
575
+
576
+ returns:
577
+ ===
578
+ `cfxyz`: shape: (..., I, J, 3)
579
+ '''
580
+
581
+ tangens, normals = get_cellinfo_1d_t(geom[..., [0,1]])
582
+ tangens = 0.5 * (tangens[..., 1:, :, :] + tangens[..., :-1, :, :]) # transfer to cell centre at spanwise direction
583
+ # normals = 0.5 * (normals[1:] + normals[:-1])
584
+ # cfn = np.zeros_like(cf[..., 0])
585
+ # print(cf[..., [0]].shape, tangens.shape)
586
+ cfxyz = torch.concatenate((cf[..., [0]] * tangens, cf[..., [1]]), axis=-1)
587
+
588
+ return cfxyz
training_samples_index.txt ADDED
The diff for this file is too large to render. See raw diff