Skip to content

Commit 636a4d1

Browse files
authored
Merge pull request #124 from ToFuProject/Issue123_BetterSample
[#123] get_sample_mesh_3d_slice(adjust_phi=bool) implemented + minor …
2 parents 6dfe14b + ca5c411 commit 636a4d1

File tree

4 files changed

+75
-28
lines changed

4 files changed

+75
-28
lines changed

bsplines2d/_class01_Mesh2D.py

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,10 +25,10 @@
2525
__all__ = ['Mesh2D']
2626

2727

28-
# #############################################################################
29-
# #############################################################################
30-
#
31-
# #############################################################################
28+
# #############################################################
29+
# #############################################################
30+
# Class
31+
# #############################################################
3232

3333

3434
class Mesh2D(ds.DataStock):
@@ -487,6 +487,7 @@ def get_sample_mesh(
487487
mode=None,
488488
x0=None,
489489
x1=None,
490+
# options
490491
Dx0=None,
491492
Dx1=None,
492493
imshow=None,
@@ -505,6 +506,7 @@ def get_sample_mesh(
505506
mode=mode,
506507
x0=x0,
507508
x1=x1,
509+
# options
508510
Dx0=Dx0,
509511
Dx1=Dx1,
510512
imshow=imshow,
@@ -558,6 +560,7 @@ def get_sample_mesh_3d_slice(
558560
Dphi=None,
559561
# option
560562
reshape_2d=None,
563+
adjust_phi=None,
561564
# plot
562565
plot=None,
563566
dax=None,
@@ -586,6 +589,7 @@ def get_sample_mesh_3d_slice(
586589
Dphi=Dphi,
587590
# option
588591
reshape_2d=reshape_2d,
592+
adjust_phi=adjust_phi,
589593
# plot
590594
plot=plot,
591595
dax=dax,

bsplines2d/_class01_cropping.py

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,10 @@
1414
from . import _class01_select as _select
1515

1616

17-
# #############################################################################
18-
# #############################################################################
17+
# ##################################################################
18+
# ##################################################################
1919
# crop rect mesh
20-
# #############################################################################
20+
# ##################################################################
2121

2222

2323
def crop(
@@ -58,7 +58,10 @@ def crop(
5858
# ------------
5959

6060
key, mtype, cropbool, thresh_in, remove_isolated = _crop_check(
61-
coll=coll, key=key, crop=crop, thresh_in=thresh_in,
61+
coll=coll,
62+
key=key,
63+
crop=crop,
64+
thresh_in=thresh_in,
6265
remove_isolated=remove_isolated,
6366
)
6467

@@ -206,21 +209,18 @@ def _crop_check(
206209

207210
cropbool = crop.dtype == np.bool_
208211

212+
# --------------------
209213
# thresh_in and maxth
210-
if thresh_in is None:
211-
thresh_in = 3
214+
# --------------------
215+
212216
maxth = 5 if coll.dobj[wm][key]['type'] == 'rect' else 4
213217

214-
c0 = (
215-
isinstance(thresh_in, (int, np.integer))
216-
and (1 <= thresh_in <= maxth)
217-
)
218-
if not c0:
219-
msg = (
220-
f"Arg thresh_in must be a int in 1 <= thresh_in <= {maxth}\n"
221-
f"Provided: {thresh_in}"
222-
)
223-
raise Exception(msg)
218+
thresh_in = int(ds._generic_check._check_var(
219+
thresh_in, 'thresh_in',
220+
default=3,
221+
types=(int, float),
222+
sign=['>=1', f"<={maxth}"],
223+
))
224224

225225
# ----------------
226226
# remove_isolated
@@ -328,4 +328,4 @@ def _get_cropbs_from_crop(coll=None, crop=None, keybs=None):
328328
for ii in range(shapebs[0])
329329
], dtype=bool)
330330

331-
return cropbs
331+
return cropbs

bsplines2d/_class01_sample.py

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -457,8 +457,8 @@ def _get_res(
457457
)
458458
if not c0:
459459
msg = (
460-
f"Arg res must be a list of 2 positive floats!\n"
461-
"Provided: {res}"
460+
"Arg res must be a list of 2 positive floats!\n"
461+
f"Provided: {res}"
462462
)
463463
raise Exception(msg)
464464

@@ -525,17 +525,25 @@ def _sample_2d(
525525

526526
# --------
527527
# compute
528+
# --------
528529

529530
min0, max0 = knots0.min(), knots0.max()
530531
min1, max1 = knots1.min(), knots1.max()
531532

533+
# --------
534+
# absolute
535+
532536
if mode == 'abs':
533537
if x0 is None:
534538
n0 = int(np.ceil((max0 - min0) / res[0]))
535539
x0 = np.linspace(min0, max0, n0)
536540
if x1 is None:
537541
n1 = int(np.ceil((max1 - min1) / res[1]))
538542
x1 = np.linspace(min1, max1, n1)
543+
544+
# --------
545+
# relative
546+
539547
else:
540548
if x0 is None:
541549
n0 = int(np.ceil(1./res[0]))
@@ -552,13 +560,17 @@ def _sample_2d(
552560
knots1[-1:],
553561
))
554562

563+
# --------
564+
# scalar
565+
555566
if np.isscalar(x0):
556567
x0 = np.full(x1.shape, x0)
557568
if np.isscalar(x1):
558569
x1 = np.full(x0.shape, x1)
559570

560571
# -----------
561572
# prepare ind
573+
# -----------
562574

563575
# x0, x1
564576
if grid or in_mesh or Dx0 is not None:
@@ -688,7 +700,6 @@ def _store_2d(
688700
kx1=None,
689701
):
690702

691-
692703
# -----------
693704
# check key
694705

bsplines2d/_class01_slice3d.py

Lines changed: 36 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ def main(
2727
Dphi=None,
2828
# option
2929
reshape_2d=None,
30+
adjust_phi=None,
3031
# plot
3132
plot=None,
3233
dax=None,
@@ -38,7 +39,7 @@ def main(
3839
# --------------
3940

4041
(
41-
res, Z, phi, domain, reshape_2d, plot,
42+
res, Z, phi, domain, reshape_2d, adjust_phi, plot,
4243
) = _check(
4344
res=res,
4445
# slice
@@ -50,6 +51,7 @@ def main(
5051
Dphi=Dphi,
5152
# option
5253
reshape_2d=reshape_2d,
54+
adjust_phi=adjust_phi,
5355
plot=plot,
5456
)
5557

@@ -107,6 +109,13 @@ def main(
107109
indphi=indphi,
108110
)
109111

112+
# ------------
113+
# adjust phi
114+
# ------------
115+
116+
if adjust_phi is True:
117+
pts_phi = phi
118+
110119
# --------------
111120
# output
112121
# --------------
@@ -171,6 +180,7 @@ def _check(
171180
Dphi=None,
172181
# option
173182
reshape_2d=None,
183+
adjust_phi=None,
174184
# plot
175185
plot=None,
176186
color=None,
@@ -258,6 +268,18 @@ def _check(
258268
default=True,
259269
)
260270

271+
# ---------
272+
# adjust_phi
273+
# ---------
274+
275+
adjust_phi = ds._generic_check._check_var(
276+
adjust_phi, 'adjust_phi',
277+
types=bool,
278+
default=False,
279+
)
280+
if phi is None:
281+
adjust_phi = False
282+
261283
# ---------
262284
# plot
263285
# ---------
@@ -268,7 +290,7 @@ def _check(
268290
default=False,
269291
)
270292

271-
return res, Z, phi, domain, reshape_2d, plot
293+
return res, Z, phi, domain, reshape_2d, adjust_phi, plot
272294

273295

274296
# ##############################################
@@ -337,7 +359,7 @@ def _poloidal_slice(
337359
# domain Z
338360
# -----------
339361

340-
dphi = np.pi/8
362+
dphi = np.pi/12
341363
Dphi = (phi - dphi, phi + dphi)
342364

343365
indr, indz, indphi = func_ind_from_domain(
@@ -370,9 +392,19 @@ def _poloidal_slice(
370392
# ---------
371393
# reshape
372394
# ---------
395+
373396
ir = np.concatenate(indr_new)
374397
iz = np.concatenate(indz_new)
375398
iphi = np.concatenate(indphi_new)
399+
400+
# ------------
401+
# safety check
402+
403+
assert np.unique([ir, iz], axis=1).shape[1] == ir.size
404+
405+
# ----------------
406+
# optional reshape
407+
376408
if reshape_2d is True:
377409
i0u = np.unique(ir)
378410
i1u = np.unique(iz)
@@ -386,7 +418,7 @@ def _poloidal_slice(
386418

387419
indsz = np.argsort(iz[ind])
388420
iiz = np.searchsorted(i1u, np.sort(iz[ind]))
389-
sli = (iri, iiz)
421+
sli = (ii, iiz)
390422

391423
indr[sli] = iri
392424
indz[sli] = iz[ind][indsz]

0 commit comments

Comments
 (0)