-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathrun_sie.py
More file actions
executable file
·1543 lines (1369 loc) · 66.7 KB
/
Copy pathrun_sie.py
File metadata and controls
executable file
·1543 lines (1369 loc) · 66.7 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
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
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
run_sie.py — Orchestrate HELIOS workflows.
This script provides a subcommand-based CLI that prepares simulations,
runs the C++ solvers, and post-processes fields for visualization. It is
designed to work with the HELIOS project layout and with C++11 builds of
SIENano / SIENanoPP.
───────────────────────────────────────────────────────────────────────────────
Project layout (relative to repository root)
───────────────────────────────────────────────────────────────────────────────
sim_data/<sim>/ # user inputs (config.txt, mesh.mphtxt)
sim_res/<sim>/ # generated outputs for this simulation
├─ jobs/ # job.N.xml files (one per wavelength)
├─ mesh.mesh # converted mesh (from COMSOL *.mphtxt or Gmsh *.msh)
├─ out/ # solver outputs
│ ├─ csc/ # normalized location of *.csc files (gathered here)
│ └─ fields/ # field files produced by SIENanoPP (.ein,.esc,.hin,.hsc)
├─ points/ # sampling point sets (*.pos) produced by make-points
└─ logs/ # one log per solver run
Optional materials/tables:
- materials/ # ε(λ) tables used by jobwriter
- erfc.bin # periodic erfc lookup table (auto-resolved if needed)
───────────────────────────────────────────────────────────────────────────────
Subcommands (high level)
───────────────────────────────────────────────────────────────────────────────
prepare Generate jobs/ from config.txt and convert mesh if needed.
solve Run the SIENano app over selected jobs.
post Run the SIENanoPP app on produced *.sol using user-supplied points.
make-points Create regular sampling grids; avoids layers' interfaces.
all prepare -> solve (-> post if --points/-p is given).
───────────────────────────────────────────────────────────────────────────────
Typical workflow
───────────────────────────────────────────────────────────────────────────────
1) Put inputs in sim_data/<sim>/:
- config.txt (see project examples)
- Mesh file:
* COMSOL text mesh (*.mphtxt), or
* Gmsh MSH v4 ASCII (*.msh).
NOTE: COMSOL and Gmsh inputs support 3D tetrahedral meshes only.
2) Prepare jobs and mesh:
run_sie.py prepare <sim> [--mode 0|1|2] [--meshfile 0|1]
3) Solve:
run_sie.py solve <sim> [-a] [-l LEVEL] [-th THREADS] [--jobs 1,3,5-8]
[--table PATH]
4) Post-process fields:
run_sie.py post <sim> -p points/<file.pos> [-a] [-th THREADS]
[--table PATH]
───────────────────────────────────────────────────────────────────────────────
Periodic simulation awareness
───────────────────────────────────────────────────────────────────────────────
- Jobs are treated as periodic if their job XML contains a <periodic> block.
- For periodic jobs, this script resolves an erfc lookup table and passes both:
* environment: ERFC_TABLE=/abs/path/to/erfc.bin
* CLI flag: -t /abs/path/to/erfc.bin
You may override with --table PATH. If unset, common locations are tried
under sim_res/<sim>/ and the project root.
───────────────────────────────────────────────────────────────────────────────
make-points behavior (interface-safe sampling)
───────────────────────────────────────────────────────────────────────────────
- Generates plane grids (xz, xy, yz) with independent steps per axis.
- Loads interface z-levels from sim_res/<sim>/config.txt (if present) and
nudges any point that would land exactly on an interface by 1e-2 (display
units unchanged). This avoids some degenerate evaluations in SIENanoPP.
───────────────────────────────────────────────────────────────────────────────
I/O conventions
───────────────────────────────────────────────────────────────────────────────
- Points files (*.pos): plain text, one "x y z" per line (no header).
- Solutions (*.sol): produced by SIENano and kept directly under
sim_res/<sim>/out/ so SIENanoPP can consume them without shims.
- Cross sections (*.csc): produced by SIENano; this script moves them into
sim_res/<sim>/out/csc/ so visualization can find them.
- Field files: SIENanoPP writes .ein/.esc/.hin/.hsc into out/; this script
organizes them under out/fields/<points_name>/ per input points set.
───────────────────────────────────────────────────────────────────────────────
CLI synopsis (selected options)
───────────────────────────────────────────────────────────────────────────────
Common:
--root ROOT Project root (auto-detected in most setups)
--launcher CMD Prefix external runner, e.g. "srun -N 1 -n 16"
-a, --accurate Enable solver accuracy options (passes -a)
-th, --threads N Threads for SIENano/SIENanoPP
--table PATH erfc table path for periodic jobs
--jobs SPEC Job selection: "1,3,5-8" (1-based)
prepare:
--mode {0,1,2} 0: isolated homogeneous, 1: periodic homogeneous,
2: isolated layered
--meshfile {0,1} 1 converts the unique *.mphtxt or *.msh into *.mesh
make-points:
--plane {xz,xy,yz} Target plane
--step H Uniform step (overridden by per-axis)
--stepx/--stepy/--stepz Per-axis steps
--x0 --x1 --y0 --y1 --z0 --z1 Bounds in native units
-p, --points NAME Output basename under sim_res/<sim>/points/
post:
-p, --points PATH|NAME Points file or name under sim_res/<sim>/points/
───────────────────────────────────────────────────────────────────────────────
Environment variables
───────────────────────────────────────────────────────────────────────────────
NO_BAR=1 Disable progress bars/spinners (CI-friendly)
ERFC_TABLE=/path/file Default periodic table (overridden by --table)
───────────────────────────────────────────────────────────────────────────────
Exit codes & logging
───────────────────────────────────────────────────────────────────────────────
- Non-zero exit codes indicate user-facing errors (bad paths/flags/inputs).
- All external tool invocations are logged under sim_res/<sim>/logs/.
- On non-interactive TTYs, spinners are suppressed to keep logs clean.
"""
from __future__ import annotations
import argparse
import os
import re
import shutil
import sys, time
from tqdm import tqdm
from pathlib import Path
from typing import List, Optional, Tuple
import subprocess
import shlex
# ------------------------------- paths ---------------------------------------
ROOT = Path(__file__).resolve().parent
APPS = ROOT / "apps"
PYTOOLS = ROOT / "pytools"
SIENANO = APPS / "SIENano"
SIENANOPP = APPS / "SIENanoPP"
SIETABLE = APPS / "SIETable"
# ----------------------------- utilities -------------------------------------
def die(msg: str, code: int = 2) -> None:
"""
Terminate the program with a short, prefixed error message and given exit code.
Use for user-facing, expected failure modes (bad paths/flags/missing files).
"""
print(f"Error: {msg}", file=sys.stderr)
sys.exit(code)
def ensure_exists(p: Path, kind: str = "path") -> None:
"""Guard helper: exit with a clear message if a required file/folder is missing."""
if not p.exists():
die(f"Cannot find {kind}: {p}")
def list_jobs(jobs_dir: Path) -> List[Path]:
"""Return job.*.xml files sorted by numeric index (job.<N>.xml -> N asc)."""
return sorted(
(p for p in jobs_dir.glob("job.*.xml") if p.is_file()),
key=lambda p: int(p.stem.split(".")[1])
)
def rel_or_abs(p: Path, base: Path) -> Path:
"""Interpret 'p' relative to 'base' if not absolute; return a Path."""
return p if p.is_absolute() else (base / p)
def find_unique_meshfile(sim_data_dir: Path) -> Optional[Path]:
"""
Prefer a single mesh source under sim_data/<sim>: one of
- *.msh (Gmsh, v4 ASCII recommended) or
- *.mphtxt (COMSOL)
If multiple are present, error out for explicit user choice.
"""
cands = sorted(list(sim_data_dir.glob("*.msh")) + list(sim_data_dir.glob("*.mphtxt")))
if not cands:
return None
if len(cands) > 1:
die(f"Found multiple mesh sources in {sim_data_dir}. Use --meshfile to disambiguate.")
return cands[0]
def tee_run(cmd: List[str], cwd: Path, log_file: Path, show_prefix: str = "") -> int:
"""
Run a command, tee stdout/stderr to a log file, and (optionally) show a
minimal progress indication on TTY. On non-TTY (e.g., ssh/CI), we suppress
spinners to avoid noisy output.
Notes:
- Appends to the log file (does not truncate). Each call writes a '$ cmd' header.
- Respect SIE_NO_SPINNER=1 to disable the lightweight TTY prefix line.
"""
log_file.parent.mkdir(parents=True, exist_ok=True)
is_tty = sys.stdout.isatty()
no_spinner = os.environ.get("SIE_NO_SPINNER", "0") == "1"
# Append instead of write: keeps any header callers added before tee_run()
with log_file.open("a") as lf:
lf.write(f"$ {' '.join(cmd)}\n\n")
lf.flush()
proc = subprocess.Popen(
cmd, cwd=str(cwd),
stdout=lf, stderr=subprocess.STDOUT,
universal_newlines=True
)
# Non-interactive context (CI/ssh) or user disabled spinner -> no TTY noise.
if not is_tty or no_spinner:
# Just wait quietly; avoid carriage returns/garbage in non-TTY
ret = proc.wait()
if is_tty and show_prefix:
# one clean line after completion
print(f"{show_prefix} done (rc={ret})")
return ret
# Light TTY indicator (no animated spinner flood)
if show_prefix:
# Minimal on-tty progress (single-line prefix) instead of an animated spinner.
print(f"{show_prefix} ...", end="", flush=True)
ret = proc.wait()
if show_prefix:
print(f"\r{show_prefix} done (rc={ret}){' ' * 10}")
return ret
_bars = {}
def progress_bar(i: int, n: int, prefix: str = "", width: int = 40) -> None:
"""
tqdm-based progress bar that:
- shows in interactive terminals
- renders on stderr (so stdout prints remain intact)
- persists at the bottom like tqdm, even while logs scroll
Keeps the same call signature as before: progress_bar(i, n, prefix).
"""
global _bars
is_tty = sys.stdout.isatty()
no_bar = bool(os.environ.get("NO_BAR", False))
# Fallback to a simple completion line when not interactive or disabled
if no_bar or not is_tty or n <= 1:
if i == n:
print(f"{prefix}{i}/{n} done.")
return
key = prefix.strip() or "default"
bar = _bars.get(key)
if bar is None:
# Leave=True keeps the final bar on screen; dynamic_ncols adapts to terminal width.
bar = tqdm(
total=n,
desc=key,
dynamic_ncols=True,
leave=True,
file=sys.stderr,
bar_format="{l_bar}{bar}| {n_fmt}/{total_fmt}",
)
_bars[key] = bar
# Update only the delta from current count
delta = max(0, i - bar.n)
if delta:
bar.update(delta)
if i >= n:
bar.close()
_bars.pop(key, None)
# ensure a clean line after the closed bar
print("", file=sys.stderr)
def parse_launcher(launcher: str | None) -> list[str]:
"""
Parse an optional launcher prefix (e.g., 'srun -N 1 -n 16').
Uses shlex.split to respect quotes. Validates the binary exists on PATH.
"""
if not launcher:
return []
parts = shlex.split(launcher)
if not parts:
return []
exe = parts[0]
if shutil.which(exe) is None:
raise FileNotFoundError(
f"Launcher executable not found: '{exe}'. "
f"Install it, load its module, provide an absolute path, "
f"or drop --launcher to run locally."
)
return parts
# ---- points (.pos) helpers ---------------------------------------------------
def _frange(a: float, b: float, h: float, eps: float = 1e-9) -> list[float]:
"""
Inclusive float range [a, b] with fixed step h.
Mitigates FP drift by rounding step count and tolerating a small epsilon near 'b'.
"""
if h <= 0:
die(f"step must be > 0, got {h}")
# Round step count so tiny FP discrepancies don't drop the last point.
n = int(round((b - a) / h)) + 1
vals = []
for i in range(max(0, n)):
v = a + i * h
if v > b + eps:
break
vals.append(v)
# If 'b' is still meaningfully beyond the last value, force-append it.
if vals and (b - vals[-1]) > (h * 0.5) and (b - vals[-1]) > eps:
vals.append(b)
return vals
def write_pos_grid(sim: str, plane: str,
x: tuple[float, float] | None,
y: tuple[float, float] | None,
z: tuple[float, float] | None,
x0: float | None, y0: float | None, z0: float | None,
out_name: str,
*, # keyword-only extras
stepx: float | None = None,
stepy: float | None = None,
stepz: float | None = None,
interfaces: set[float] = frozenset(),
append: bool = False) -> None:
"""
Generate a regular grid of points on a named plane and save to
sim_res/<sim>/points/<out_name>.pos as plain text:
x y z
x y z
...
"""
res_dir = ROOT / "sim_res" / sim
ensure_exists(res_dir, "sim_res/<sim> folder")
pts_dir = res_dir / "points"
pts_dir.mkdir(parents=True, exist_ok=True)
out_path = pts_dir / out_name
mesh_path = res_dir / "mesh.mesh"
z_limits = get_mesh_z_limits(mesh_path)
pts: list[tuple[float, float, float]] = []
if plane == "xz":
if x is None or z is None or y0 is None:
die("xz requires --x xmin xmax --z zmin zmax --y0 value")
xs = _frange(x[0], x[1], stepx)
zs = _frange(z[0], z[1], stepz)
# Load nodes on this y-plane (axis 1)
bad_2d = load_planar_nodes(mesh_path, 1, float(y0))
bad_xs = {p[0] for p in bad_2d}
bad_zs = {p[1] for p in bad_2d}
x_mid = (x[0] + x[1]) * 0.5
z_mid = (z[0] + z[1]) * 0.5
for xi in xs:
if round(xi, 4) in bad_xs:
if xi >= x_mid: xi -= 0.5 * stepx
else: xi += 0.5 * stepx
for zi in zs:
zp = nudge_z_if_interface(zi, interfaces, z_limits, 0.5 * stepz)
pts.append((xi, float(y0), zp))
elif plane == "xy":
if x is None or y is None or z0 is None:
die("xy requires --x xmin xmax --y ymin ymax --z0 value")
xs = _frange(x[0], x[1], stepx)
ys = _frange(y[0], y[1], stepy)
# Load nodes on this z-plane (axis 2)
bad_2d = load_planar_nodes(mesh_path, 2, float(z0))
bad_xs = {p[0] for p in bad_2d}
bad_ys = {p[1] for p in bad_2d}
x_mid = (x[0] + x[1]) * 0.5
y_mid = (y[0] + y[1]) * 0.5
z_fixed = nudge_z_if_interface(float(z0), interfaces, z_limits)
if abs(z_fixed) < 1e-2: z_fixed = z_fixed + 1e-2
for xi in xs:
if round(xi, 4) in bad_xs:
if xi >= x_mid: xi -= 0.5 * stepx
else: xi += 0.5 * stepx
for yi in ys:
if round(yi, 4) in bad_ys:
if yi >= y_mid: yi -= 0.5 * stepy
else: yi += 0.5 * stepy
pts.append((xi, yi, z_fixed))
elif plane == "yz":
if y is None or z is None or x0 is None:
die("yz requires --y ymin ymax --z zmin zmax --x0 value")
ys = _frange(y[0], y[1], stepy)
zs = _frange(z[0], z[1], stepz)
# Load nodes on this x-plane (axis 0)
bad_2d = load_planar_nodes(mesh_path, 0, float(x0))
bad_ys = {p[0] for p in bad_2d}
bad_zs = {p[1] for p in bad_2d}
y_mid = (y[0] + y[1]) * 0.5
z_mid = (z[0] + z[1]) * 0.5
for yi in ys:
if round(yi, 4) in bad_ys:
if yi >= y_mid: yi -= 0.5 * stepy
else: yi += 0.5 * stepy
for zi in zs:
zp = nudge_z_if_interface(zi, interfaces, z_limits, 0.5 * stepz)
pts.append((float(x0), yi, zp))
else:
die(f"Unknown plane '{plane}' (choose from xz, xy, yz)")
with out_path.open("a" if append else "w") as f:
for xp, yp, zp in pts:
f.write(f"{xp:.6f} {yp:.6f} {zp:.6f}\n")
print(f"[points] Wrote {len(pts)} points -> {out_path}")
def parse_job_selection(spec: str, max_job: int) -> list[int]:
"""
Parse job selection strings (e.g., '1,3,5-8') into sorted, 1-based indices.
Clips ranges to [1, max_job] and rejects empty/invalid specs.
"""
sel: set[int] = set()
for tok in spec.split(","):
tok = tok.strip()
if not tok:
continue
# Range token (A-B): accept reversed bounds and clip to valid job interval.
if "-" in tok:
a, b = tok.split("-", 1)
a, b = int(a), int(b)
lo, hi = min(a, b), max(a, b)
for k in range(max(1, lo), min(max_job, hi) + 1):
sel.add(k)
else:
k = int(tok)
if 1 <= k <= max_job:
sel.add(k)
out = sorted(sel)
if not out:
die(f"No valid jobs in selection '{spec}' (1..{max_job}).")
return out
# ---- solution file helpers (per job, with subjobs) --------------------------
SOL_RE = re.compile(r"^sim(\d+)_inc_(\d+)\.sol$")
def list_job_solutions(sol_dir: Path, job_number: int) -> list[Path]:
"""
Return all solutions for a given job_number as a list of Paths, sorted by subjob index.
Only files matching sim<i>_inc_<j>.sol are considered, and only those with i==job_number.
"""
found: list[tuple[int, Path]] = []
if not sol_dir.exists():
return []
for p in sol_dir.glob("*.sol"):
m = SOL_RE.match(p.name)
if not m:
continue
i = int(m.group(1))
sj = int(m.group(2))
if i == job_number:
found.append((sj, p))
return [p for _, p in sorted(found)]
# -------------------------- periodic helpers ---------------------------------
PERIODIC_TAG_RE = re.compile(r"<\s*periodic\b", re.IGNORECASE)
def job_is_periodic(job_xml: Path) -> bool:
"""
Heuristic: treat a job as periodic if '<periodic' appears in its XML (case-insensitive).
"""
try:
txt = job_xml.read_text(errors="ignore")
except Exception:
return False
return PERIODIC_TAG_RE.search(txt) is not None
def extract_periodic_params_from_config(cfg: Path) -> Optional[List[float]]:
"""
Scan config.txt for a line with five numerics: px py cx cy zcut (commas/spaces allowed).
Returns [px, py, cx, cy, zcut] as floats if found, else None.
"""
params = None
# use UTF-8 with BOM support and ignore stray bytes
with cfg.open("r", encoding="utf-8-sig", errors="ignore") as f:
for line in f:
s = line.strip()
if not s or s.startswith("#"):
continue
parts = re.split(r"[,\s]+", s)
if len(parts) == 5:
try:
vals = list(map(float, parts))
except ValueError:
continue
params = vals
return params
# ---- table resolution (works with or without --table) -----------------------
def resolve_table_path_optional(sim: str, res_dir: Path, table_opt: Optional[str]) -> Optional[str]:
"""
Resolve a periodic table path to an absolute path, or None.
Priority:
1) --table value, interpreted as:
- absolute path, or
- relative to sim_res/<sim>, or
- relative to project ROOT, or
- relative to current working directory
2) ERFC_TABLE environment variable (absolute or any of the above)
3) sim_res/<sim>/erfc.bin
4) ROOT/third_party/erfc.bin
5) ROOT/erfc.bin
Returns an absolute string path if found; otherwise None.
"""
candidates: list[Path] = []
# Treat user paths as either absolute or relative to sim_res/<sim>, project ROOT, or CWD.
def add_candidates_from(s: Optional[str]):
if not s:
return
p = Path(s)
if p.is_absolute():
candidates.append(p)
else:
candidates.extend([res_dir / p, ROOT / p, Path.cwd() / p])
# Common placements used in this project (local per-sim or shared under project tree):
# 1) CLI
add_candidates_from(table_opt)
# 2) ENV
add_candidates_from(os.environ.get("ERFC_TABLE"))
# 3) Common local placements
candidates.append(res_dir / "erfc.bin")
candidates.append(ROOT / "third_party" / "erfc.bin")
candidates.append(ROOT / "erfc.bin")
for c in candidates:
try:
if c.exists() and c.is_file():
return str(c.resolve())
except Exception:
continue
return None
def ensure_table_for_periodic(sim: str, res_dir: Path, table_opt: Optional[str]) -> str:
"""
Ensure we have a valid table path for periodic jobs. If found, export ERFC_TABLE
and return absolute path. If not found, error.
"""
tab_abs = resolve_table_path_optional(sim, res_dir, table_opt)
if not tab_abs:
die(
"Periodic job detected but no periodic lookup table was found.\n"
"Provide one via:\n"
" --table /abs/path/to/erfc.bin (or path relative to project or sim_res/<sim>)\n"
"or set the environment variable:\n"
" export ERFC_TABLE=/abs/path/to/erfc.bin"
)
# Export for child processes (SIENano/SIENanoPP) even if user didn't set it.
os.environ["ERFC_TABLE"] = tab_abs # make it visible to children
return tab_abs
# --- Interfaces parsing from sim_res/<sim>/config.txt ------------------------
def load_interface_z(res_dir: Path) -> set[float]:
"""
Parse the 'Layered media interfaces' section in sim_res/<sim>/config.txt.
Collect numeric z-values on subsequent non-empty, non-comment lines
(stops on blank/comment).
"""
zs: set[float] = set()
cfg = res_dir / "config.txt"
if not cfg.exists():
return zs
lines = cfg.read_text(errors="ignore").splitlines()
for i, line in enumerate(lines):
# Start collecting immediately after this marker line;
# accept comma- or space-separated numbers.
if "Layered media interfaces" in line:
j = i + 2
while j < len(lines):
s = lines[j].strip()
if not s or s.startswith("#"):
break
for tok in re.split(r"[,\s]+", s):
if not tok:
continue
try:
zs.add(float(tok))
except ValueError:
pass
j += 1
break
return zs
def nudge_z_if_interface(z: float, interfaces: set[float],
z_limits: tuple[float, float] | None = None,
offset: float = 1e-2,
tol: float = 1e-6) -> float:
"""
If z coincides with an interface (within 'tol'), shift it to avoid singular sampling.
Logic:
- If <= z_mid (mesh mid): shift up (+1e-2)
- If >= z_mid (mesh mid): shift down (-1e-2)
- Otherwise: shift down (-1e-2)
"""
for zi in interfaces:
if z_limits and abs(z - zi) < tol:
z_min, z_max = z_limits
z_mid = (z_min + z_max) * 0.5
if zi <= z_mid:
return z + offset
# Default to negative shift
return z - offset
return z
def load_planar_nodes(mesh_path: Path, plane_idx: int, plane_val: float, tol: float = 1e-4) -> set[tuple[float, float]]:
"""
Reads mesh.mesh to find nodes lying on the specific plane (axis[plane_idx] ~= plane_val).
Returns a set of (u, v) coordinates (rounded to 4 decimals) for the other two axes.
Assumes simple mesh format: lines with exactly 3 floats are nodes.
"""
bad_pts = set()
if not mesh_path.exists():
return bad_pts
try:
with mesh_path.open("r") as f:
for line in f:
parts = line.split()
if len(parts) == 3: # Heuristic: 3 columns = x y z
try:
c = [float(p) for p in parts]
if abs(c[plane_idx] - plane_val) < tol:
# Extract the other two coordinates in index order
others = [c[i] for i in range(3) if i != plane_idx]
bad_pts.add((round(others[0], 4), round(others[1], 4)))
except ValueError:
pass
except Exception:
pass
return bad_pts
def get_mesh_z_limits(mesh_path: Path) -> tuple[float, float] | None:
"""
Scan mesh.mesh for global min/max Z coordinates (3rd column).
Returns (z_min, z_max) or None if failed.
"""
if not mesh_path.exists():
return None
zmin, zmax = float('inf'), -float('inf')
try:
with mesh_path.open("r") as f:
for line in f:
parts = line.split()
if len(parts) == 3:
try:
z = float(parts[2])
if z < zmin: zmin = z
if z > zmax: zmax = z
except ValueError:
pass
except Exception:
return None
if zmin == float('inf'):
return None
return zmin, zmax
# ---- wavelength helpers (lambdalist.txt and job.N.xml parsing) ---------------
LAMBDA_TAG_RE = re.compile(r"<\s*(wavelength|lambda|lambda0)[^>]*>([^<]+)<", re.IGNORECASE)
NUM_UNIT_RE = re.compile(r"([-+]?\d*\.?\d+(?:[eE][-+]?\d+)?)\s*(nm|um|µm|micron|m|ev|hz|khz|mhz|ghz|thz)?", re.IGNORECASE)
def parse_lambda_list(expr: str) -> List[float]:
"""
Parse a wavelength list expression into sorted unique nm values.
Accepted forms (comma/semicolon separated):
690
690nm
0.69um
690-696
690:2:696 (start:step:end)
Tokens can mix units; all are converted to nm.
Returns a sorted list of unique wavelengths in nm.
"""
if not expr:
return []
toks = [t.strip() for t in expr.replace(";", ",").split(",") if t.strip()]
vals: List[float] = []
def parse_one(s: str) -> Optional[float]:
m = NUM_UNIT_RE.fullmatch(s)
if not m:
return None
v = float(m.group(1))
u = (m.group(2) or "nm")
return _to_nm(v, u)
for t in toks:
# step range a:s:b ?
if ":" in t:
parts = [p.strip() for p in t.split(":")]
if len(parts) == 3:
a = _to_nm(*NUM_UNIT_RE.fullmatch(parts[0]).groups()) if NUM_UNIT_RE.fullmatch(parts[0]) else None
s = _to_nm(*NUM_UNIT_RE.fullmatch(parts[1]).groups()) if NUM_UNIT_RE.fullmatch(parts[1]) else None
b = _to_nm(*NUM_UNIT_RE.fullmatch(parts[2]).groups()) if NUM_UNIT_RE.fullmatch(parts[2]) else None
# step given in wavelength units (nm after conversion)
if a is not None and s is not None and b is not None and s != 0:
cur = a
# handle direction via sign of step
step = s if b >= a else -abs(s)
# include end if it lands within epsilon
eps = abs(step) * 1e-9 + 1e-9
if step > 0:
while cur <= b + eps:
vals.append(cur)
cur += step
else:
while cur >= b - eps:
vals.append(cur)
cur += step
continue
# simple range a-b ?
if "-" in t and not t.strip().startswith("-"):
a_str, b_str = [x.strip() for x in t.split("-", 1)]
a = parse_one(a_str)
b = parse_one(b_str)
if a is not None and b is not None:
step = 1.0 # default 1 nm
if b >= a:
cur = a
while cur <= b + 1e-9:
vals.append(cur)
cur += step
else:
cur = a
while cur >= b - 1e-9:
vals.append(cur)
cur -= step
continue
# single value
v = parse_one(t)
if v is not None:
vals.append(v)
# normalize: sort + unique (within 1e-6 nm tolerance)
vals.sort()
out: List[float] = []
for v in vals:
if not out or abs(out[-1] - v) > 1e-6:
out.append(v)
return out
def _to_nm(val: float, unit: str | None) -> float | None:
if not unit or unit.lower() == "nm":
return val
u = unit.lower()
if u in ("um", "µm", "micron"): return val * 1e3
if u == "m": return val * 1e9
if u == "ev": return 1239.841984 / val if val != 0 else None
fact = {"hz":1, "khz":1e3, "mhz":1e6, "ghz":1e9, "thz":1e12}.get(u)
if fact:
f = val * fact
c = 299_792_458.0
return (c / f) * 1e9 if f != 0 else None
return None
def _parse_lambda_from_xml(xml: Path) -> float | None:
try:
txt = xml.read_text(errors="ignore")
except Exception:
return None
m = LAMBDA_TAG_RE.search(txt)
def first_nm(s: str) -> float | None:
m2 = NUM_UNIT_RE.search(s)
if not m2: return None
val, unit = float(m2.group(1)), (m2.group(2) or "nm")
return _to_nm(val, unit)
if m:
nm = first_nm(m.group(2))
if nm is not None: return nm
m3 = NUM_UNIT_RE.search(txt)
if m3:
return _to_nm(float(m3.group(1)), m3.group(2) or "nm")
return None
def ensure_lambdalist(res_dir: Path) -> dict[int, float]:
"""Return {job_index -> λ_nm}. Writes lambdalist.txt if missing."""
lf = res_dir / "lambdalist.txt"
idx2wl: dict[int, float] = {}
if lf.exists():
for i, line in enumerate(lf.read_text().splitlines(), 1):
s = line.strip()
if not s or s[0] in "#%/": continue
parts = s.split()
if len(parts) == 1:
idx2wl[i] = float(parts[0])
elif len(parts) >= 2:
# 'job.N λ' or similar
try:
n = int(parts[0].split(".")[-1])
idx2wl[n] = float(parts[1])
except Exception:
pass
if idx2wl:
return idx2wl
# Build from jobs/*.xml
jobs_dir = res_dir / "jobs"
jobs = list_jobs(jobs_dir)
if not jobs:
die(f"No jobs in {jobs_dir} to infer wavelengths from.")
for p in jobs:
n = int(p.stem.split(".")[1]) # job.N.xml -> N
nm = _parse_lambda_from_xml(p)
if nm is not None:
idx2wl[n] = nm
if not idx2wl:
die("Could not infer wavelengths from job XMLs.")
with lf.open("w") as f:
for n in sorted(idx2wl):
f.write(f"job.{n} {idx2wl[n]:.9g}\n")
return idx2wl
def lambdas_to_job_indices(res_dir: Path, lam_expr: str) -> List[int]:
"""Map lambda expression (supports numbers, a-b, a:s:b, mixed units) to nearest job indices."""
idx2wl = ensure_lambdalist(res_dir)
if not idx2wl:
die("No wavelengths found to map --lambdas.")
idxs = sorted(idx2wl.keys())
wls = [idx2wl[i] for i in idxs]
asks = parse_lambda_list(lam_expr)
if not asks:
die("Empty --lambdas list.")
chosen: set[int] = set()
for lam in asks:
j = min(range(len(idxs)), key=lambda k: abs(wls[k] - lam))
chosen.add(idxs[j])
return sorted(chosen)
# ------------------------ prepare logic (with prompt) ------------------------
def _confirm_delete(res_dir: Path) -> bool:
"""Interactive safeguard before deleting an existing sim_res/<sim> folder."""
print(f"Folder already exists: {res_dir}")
ans = input("Delete it and proceed? [y/N]: ").strip().lower()
return ans in ("y", "yes")
def _maybe_reset_res(sim: str, overwrite: bool, keep: bool) -> Path:
"""Create/refresh sim_res/<sim> depending on --overwrite/--keep and TTY interactivity."""
res_dir = ROOT / "sim_res" / sim
if not res_dir.exists():
return res_dir
if overwrite:
shutil.rmtree(res_dir)
return res_dir
if keep:
return res_dir
# if non-interactive (stdin not a TTY), default to keep to avoid EOF
try:
import sys
# In non-interactive mode (e.g., batch), default to KEEP to avoid EOF on input().
if not sys.stdin.isatty():
return res_dir
except Exception:
return res_dir
if _confirm_delete(res_dir):
shutil.rmtree(res_dir)
return res_dir
else:
die("Aborted by user (kept existing sim_res).")
def prepare_sim(sim: str, mode: int, meshfile: Optional[str], overwrite: bool, keep: bool,
materials: str = "eps", spline: bool = False) -> Tuple[Path, Path]:
"""
Ensure sim_res/<sim> structure exists and is populated:
- Copy config.txt for provenance.
- If jobs/ is empty, generate jobs via pytools/jobwriter.py (mode, meshfile, sim).
- If meshfile==1 and mesh.mesh is missing, convert the lone *.mphtxt via pytools/meshconvert.py.
When mode==1 (periodic) and px py cx cy zcut exist in config, pass them to meshconvert.
"""
sim_data = ROOT / "sim_data" / sim
ensure_exists(sim_data, "sim_data folder")
cfg = sim_data / "config.txt"
ensure_exists(cfg, "config.txt")
res_dir = _maybe_reset_res(sim, overwrite, keep)
jobs_dir = res_dir / "jobs"
out_dir = res_dir / "out"
logs_dir = res_dir / "logs"
for d in (res_dir, jobs_dir, out_dir, logs_dir):
d.mkdir(parents=True, exist_ok=True)
# Keep a snapshot of the config next to results to make runs self-contained/reproducible.
cfg_dest = res_dir / "config.txt"
try:
# copy if missing or source is newer
shutil.copy2(str(cfg), str(cfg_dest))
print(f"[prepare] Copied config.txt -> {cfg_dest}")
except Exception as e:
print(f"[prepare] Warning: could not copy config.txt: {e}")
# 1) JOBS via jobwriter.py (if empty)
jobs = list_jobs(jobs_dir)
if not jobs:
print(f"[prepare] Generating jobs from {cfg} ...")
jobwriter = PYTOOLS / "jobwriter.py"
ensure_exists(jobwriter, "jobwriter.py")
cmd = [sys.executable, str(jobwriter), str(mode), "0", sim]
if spline: cmd.append("--spline")
env = os.environ.copy()
if materials == "nk":
env["MATERIALS_COLUMNS"] = "nk"
else:
# ensure explicit eps mode (unset to let jobwriter default to eps)
env.pop("MATERIALS_COLUMNS", None)
ret = subprocess.run(cmd, cwd=str(PYTOOLS), env=env).returncode
if ret != 0:
die("jobwriter failed.")
jobs = list_jobs(jobs_dir)
if not jobs:
die("jobwriter finished but no jobs were produced.")
# 2) MESH: reuse existing *.mesh if present, otherwise call meshconvert.py
mesh_out = res_dir / "mesh.mesh"
if not mesh_out.exists():
# If a .mesh already exists in sim_data/<sim>, just move it
existing_meshes = sorted(sim_data.glob("*.mesh"))
if existing_meshes:
if len(existing_meshes) > 1:
die(f"Multiple .mesh files found in {sim_data}; not sure which to use.")
src = existing_meshes[0]
print(f"[prepare] Found existing mesh {src.name}; moving to {mesh_out.name} ...")
shutil.copy(str(src), str(mesh_out))
print(f"[prepare] Done. jobs: {len(jobs)} mesh: {mesh_out.exists()}")
return res_dir, jobs_dir
# otherwise fall back to converting a .msh / .mphtxt via meshconvert.py
meshconvert = PYTOOLS / "meshconvert.py"
ensure_exists(meshconvert, "meshconvert.py")
# Resolve mesh source
src = None
if meshfile:
# accept absolute or relative to sim_data/<sim> or project root
cand = Path(meshfile)
if not cand.is_absolute():
for base in (sim_data, ROOT):
p = base / meshfile
if p.exists():
cand = p; break
if not cand.exists():
die(f"--meshfile not found: {meshfile}")
src = cand
else:
src = find_unique_meshfile(sim_data)
if src is None:
die(f"No mesh file found in {sim_data}. Provide --meshfile or place a *.msh / *.mphtxt there.")
# Copy the source mesh next to results
src_dest = res_dir / src.name
if src_dest.resolve() != src.resolve():
try: shutil.copy2(str(src), str(src_dest))
except Exception: shutil.copyfile(str(src), str(src_dest))
# Apply periodic cropping (mode==1) for both COMSOL and Gmsh sources
if mode == 1:
pp = extract_periodic_params_from_config(cfg)
if pp and len(pp) == 5:
print(f"[prepare] Converting mesh with periodic crop {pp}: {src_dest.name} -> mesh.mesh ...")
cmd = [sys.executable, str(meshconvert), "-o", str(mesh_out), str(src_dest)] + list(map(str, pp))
else:
print("[prepare] Periodic mode but params not found in config; converting as isolated.")
cmd = [sys.executable, str(meshconvert), "-o", str(mesh_out), str(src_dest)]
else:
print(f"[prepare] Converting mesh: {src_dest.name} -> mesh.mesh ...")
cmd = [sys.executable, str(meshconvert), "-o", str(mesh_out), str(src_dest)]
ret = subprocess.run(cmd, cwd=str(PYTOOLS)).returncode
if ret != 0:
die("meshconvert failed.")
print(f"[prepare] Done. jobs: {len(jobs)} mesh: {mesh_out.exists()}")
return res_dir, jobs_dir
# --------------------------------- solve -------------------------------------
def run_solve(sim: str, mode: int, accurate: bool,
level: int, threads: int, launcher: Optional[str],
etm: int, table_opt: Optional[str],
jobs_spec: Optional[str] = None) -> None:
"""
Run SIENano over selected jobs (or all) under sim_res/<sim>/jobs/.
- Adds -a/-l/-th as requested.
- Detects periodic jobs via XML and appends -etm and -t <table>.
- Moves produced sim<i>_inc_<j>.sol into out/sol/ and keeps logs per job.
"""
res_dir = ROOT / "sim_res" / sim
jobs_dir = res_dir / "jobs"