forked from flexcompute/tidy3d
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmake_script.py
74 lines (50 loc) · 1.87 KB
/
make_script.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
"""Generates Tidy3d python script from a simulation file.
Usage:
$ python make_script.py simulation.json simulation.py
to turn existing `simulation.json` into a script `simulation.py`
"""
import tidy3d as td
import argparse
import sys
import re
from black import format_str, FileMode
def main(args):
parser = argparse.ArgumentParser(description="Generate tidy3d script from a simulation file.")
parser.add_argument(
"simulation_file",
type=str,
default="simulation.json",
help="path to the simulation file (.json, .yaml, .hdf5) to generate script from.",
)
parser.add_argument(
"script_file", type=str, default="simulation.py", help="path to the .py script to write to."
)
args = parser.parse_args(args)
sim_file = args.simulation_file
out_file = args.script_file
sim = td.Simulation.from_file(sim_file)
# add header
sim_string = "from tidy3d import *\n"
sim_string += "from tidy3d.components.grid.mesher import GradedMesher\n\n"
# add the simulation body itself
sim_string += sim.__repr__()
# new we need to get rid of all the "type" info that isnt needed
# remove type='...', in middle
pattern = r"type='([A-Za-z0-9_\./\\-]*)', "
sim_string = re.sub(pattern, "", sim_string)
# remove , type='...')
pattern = r", type='([A-Za-z0-9_\./\\-]*)'\)"
sim_string = re.sub(pattern, ")", sim_string)
# remove (type='...'),
pattern = r"\(type='([A-Za-z0-9_\./\\-]*)'\)"
sim_string = re.sub(pattern, "()", sim_string)
# remove (type='...',
pattern = r"\(type='([A-Za-z0-9_\./\\-]*)', "
sim_string = re.sub(pattern, "(", sim_string)
# black to format string
sim_string = format_str(sim_string, mode=FileMode())
# write to file
with open(out_file, "w+") as f:
f.write(sim_string)
if __name__ == "__main__":
main(sys.argv[1:])