Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,7 @@ git push origin x.x.x
- [x] Get rid of pydantic? <- Not doing it
- [x] Simplify MonitorData
- [x] remove MonitorData.monitor and MonitorData.monitor_name attributes.
- [x] remove MonitorData.load() and MonitorData.export()
- [x] remove MonitorData.from_file() and MonitorData.to_file()
- [x] Make monitordata.load_from_group aware of lists
- [x] Use shapely for geometry ops / plotting? `Geometry.geo(x=0)` -> shapely representation.
- [x] Fix all tests.
Expand Down Expand Up @@ -239,8 +239,8 @@ git push origin x.x.x
After integration is complete, need to flesh out details and make them compatible with the main package.

- [ ] Make webAPI work without conversion (1 hour)
- [ ] Use native `Simulation.export()` or `Simulation.json()` for `upload()`.
- [ ] Use native `SimulationData.load()` for `load()`.
- [ ] Use native `Simulation.to_file()` or `Simulation.json()` for `upload()`.
- [ ] Use native `SimulationData.from_file()` for `load()`.
- [ ] Flesh out Mode solver details (discussion, then implemeent in 1 hour)
- [ ] Change API?
- [ ] Flesh out Symmetry details (3 days?)
Expand Down
24 changes: 12 additions & 12 deletions tests/test_IO.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@
@clear_tmp
def test_simulation_load_export():
path = "tests/tmp/simulation.json"
SIM.export(path)
SIM2 = Simulation.load(path)
SIM.to_file(path)
SIM2 = Simulation.from_file(path)
assert SIM == SIM2, "original and loaded simulations are not the same"


Expand Down Expand Up @@ -59,8 +59,8 @@ def test_simulation_preserve_types():
)

path = "tests/tmp/simulation.json"
sim_all.export(path)
sim_2 = Simulation.load(path)
sim_all.to_file(path)
sim_2 = Simulation.from_file(path)
assert sim_all == sim_2

M_types = [type(s.medium) for s in sim_2.structures]
Expand All @@ -82,8 +82,8 @@ def test_simulation_preserve_types():

def test_1a_simulation_load_export2():
path = "tests/tmp/simulation.json"
SIM2.export(path)
SIM3 = Simulation.load(path)
SIM2.to_file(path)
SIM3 = Simulation.from_file(path)
assert SIM2 == SIM3, "original and loaded simulations are not the same"


Expand All @@ -108,9 +108,9 @@ def test_validation_speed():
new_structures.append(new_structure)
S.structures = new_structures

S.export(path)
S.to_file(path)
time_start = time()
_S = Simulation.load(path)
_S = Simulation.from_file(path)
time_validate = time() - time_start
times_sec.append(time_validate)
assert S == _S
Expand All @@ -124,9 +124,9 @@ def test_validation_speed():
@clear_tmp
def test_yaml():
path = "tests/tmp/simulation.json"
SIM.export(path)
sim = Simulation.load(path)
SIM.to_file(path)
sim = Simulation.from_file(path)
path1 = "tests/tmp/simulation.yaml"
sim.export_yaml(path1)
sim1 = Simulation.load_yaml(path1)
sim.to_yaml(path1)
sim1 = Simulation.from_yaml(path1)
assert sim1 == sim
12 changes: 6 additions & 6 deletions tests/test_components.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ def _test_version():
grid_size=(0.1, 0.1, 0.1),
)
path = "tests/tmp/simulation.json"
sim.export("tests/tmp/simulation.json")
sim.to_file("tests/tmp/simulation.json")
with open(path, "r") as f:
s = f.read()
assert '"version": ' in s
Expand Down Expand Up @@ -196,15 +196,15 @@ def test_medium_conversions():
freq = 3.0

# test medium creation
medium = nk_to_medium(n, k, freq)
medium = Medium.from_nk(n, k, freq)

# test consistency
eps_z = nk_to_eps_complex(n, k)
eps, sig = nk_to_eps_sigma(n, k, freq)
eps_z_ = eps_sigma_to_eps_complex(eps, sig, freq)
eps_z = AbstractMedium.nk_to_eps_complex(n, k)
eps, sig = AbstractMedium.nk_to_eps_sigma(n, k, freq)
eps_z_ = AbstractMedium.eps_sigma_to_eps_complex(eps, sig, freq)
assert np.isclose(eps_z, eps_z_)

n_, k_ = eps_complex_to_nk(eps_z)
n_, k_ = AbstractMedium.eps_complex_to_nk(eps_z)
assert np.isclose(n, n_)
assert np.isclose(k, k_)

Expand Down
6 changes: 4 additions & 2 deletions tests/test_material_library.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import pytest
import numpy as np

from tidy3d.material_library import material_library
Expand All @@ -8,6 +7,9 @@ def test_library():
"""for each member of material library, ensure that it evaluates eps_model correctly"""
for material_name, variants in material_library.items():
for _, variant in variants.items():
fmin, fmax = variant.frequency_range
if variant.frequency_range:
fmin, fmax = variant.frequency_range
else:
fmin, fmax = 100e12, 300e12
freqs = np.linspace(fmin, fmax, 10011)
eps_complex = variant.eps_model(freqs)
6 changes: 3 additions & 3 deletions tests/test_plugins.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,17 +80,17 @@ def test_dispersion():
fitter = DispersionFitter(wvls, n_data)
medium, rms = fitter.fit_single()
medium, rms = fitter.fit(num_tries=2)
medium.export("tests/tmp/medium_fit.json")
medium.to_file("tests/tmp/medium_fit.json")


def test_dispersion_load():
"""loads dispersion model from nk data file"""
fitter = DispersionFitter.load("tests/data/nk_data.csv", skiprows=1, delimiter=",")
fitter = DispersionFitter.from_file("tests/data/nk_data.csv", skiprows=1, delimiter=",")
medium, rms = fitter.fit(num_tries=20)


def test_dispersion_plot():
"""plots a medium fit from file"""
fitter = DispersionFitter.load("tests/data/nk_data.csv", skiprows=1, delimiter=",")
fitter = DispersionFitter.from_file("tests/data/nk_data.csv", skiprows=1, delimiter=",")
medium, rms = fitter.fit(num_tries=20)
fitter.plot(medium)
6 changes: 3 additions & 3 deletions tests/test_web.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ def test_webapi_5_download():
def test_webapi_6_load():
"""load the results into sim_data"""
task_id = _get_gloabl_task_id()
sim_data = web.load_data(task_id, simulation=sim_original, path=PATH_SIM_DATA)
sim_data = web.load(task_id, simulation=sim_original, path=PATH_SIM_DATA)
first_monitor_name = list(sim_original.monitors.keys())[0]
_ = sim_data[first_monitor_name]

Expand Down Expand Up @@ -130,7 +130,7 @@ def test_job_5_download():
def test_job_6_load():
"""load the results into sim_data"""
job = _get_gloabl_job()
sim_data = job.load_data(path=PATH_SIM_DATA)
sim_data = job.load(path=PATH_SIM_DATA)
first_monitor_name = list(sim_original.monitors.keys())[0]
_ = sim_data[first_monitor_name]

Expand Down Expand Up @@ -201,7 +201,7 @@ def test_batch_5_download():
def test_batch_6_load():
"""load the results into sim_data"""
batch = _get_gloabl_batch()
sim_data_dict = batch.load_data(path_dir=PATH_DIR_SIM_DATA)
sim_data_dict = batch.load(path_dir=PATH_DIR_SIM_DATA)
first_monitor_name = list(sim_original.monitors.keys())[0]
for _, sim_data in sim_data_dict.items():
_ = sim_data[first_monitor_name]
Expand Down
2 changes: 1 addition & 1 deletion tests/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ def prepend_tmp(path):
structures=[
td.Structure(
geometry=td.Box(center=[0, 0, 0], size=[1.5, 1.5, 1.5]),
medium=td.nk_to_medium(n=2, k=0, freq=3e14),
medium=td.Medium.from_nk(n=2, k=0, freq=3e14),
)
],
sources=[
Expand Down
4 changes: 1 addition & 3 deletions tidy3d/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,6 @@
# medium
from .components import Medium, PoleResidue, AnisotropicMedium, PEC
from .components import Sellmeier, Debye, Drude, Lorentz
from .components import nk_to_eps_complex, nk_to_eps_sigma, eps_complex_to_nk
from .components import nk_to_medium, eps_sigma_to_eps_complex

# structures
from .components import Structure
Expand All @@ -41,7 +39,7 @@

# data
from .components import SimulationData, FieldData, FluxData, ModeData, FluxTimeData
from .components import data_type_map, ScalarFieldData, ScalarFieldTimeData
from .components import DATA_TYPE_MAP, ScalarFieldData, ScalarFieldTimeData

# constants imported as `C_0 = td.C_0` or `td.constants.C_0`
from .constants import inf, C_0, ETA_0
Expand Down
6 changes: 3 additions & 3 deletions tidy3d/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,9 +61,9 @@

# load the simulation
if ".yaml" in sim_file or ".yml" in sim_file:
simulation = Simulation.load_yaml(sim_file)
simulation = Simulation.from_yaml(sim_file)
else:
simulation = Simulation.load(sim_file)
simulation = Simulation.from_file(sim_file)

# inspect the simulation
if inspect_sim:
Expand Down Expand Up @@ -92,7 +92,7 @@
# run the simulation and load results
job.start()
job.monitor()
sim_data = job.load_data(path=out_file)
sim_data = job.load(path=out_file)

# visualize results
if viz_results:
Expand Down
8 changes: 4 additions & 4 deletions tidy3d/components/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -150,10 +150,10 @@ The `AbstractMedium()` base class define the properties of the medium of which t
A Dispersionless medium is created with `Medium(permittivity, conductivity)`.

The following functions are useful for defining a dispersionless medium using other possible inputs:
- `nk_to_eps_sigma` (convert refractive index parameters (n, k) to permittivity and conductivity).
- `nk_to_medium` (convert refractive index parameters (n, k) to a `Medium()` directly).
- `nk_to_eps_complex` (convert refractive index parameters (n, k) to a complex-valued permittivity).
- `eps_sigma_to_eps_complex` (convert permittivity and conductivity to complex-valued permittiviy)
- `AbstractMedium.nk_to_eps_sigma` (convert refractive index parameters (n, k) to permittivity and conductivity).
- `Medium.from_nk` (convert refractive index parameters (n, k) to a `Medium()` directly).
- `AbstractMedium.nk_to_eps_complex` (convert refractive index parameters (n, k) to a complex-valued permittivity).
- `AbstractMedium.eps_sigma_to_eps_complex` (convert permittivity and conductivity to complex-valued permittiviy)

### Dispersive Media

Expand Down
5 changes: 2 additions & 3 deletions tidy3d/components/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,7 @@

# medium
from .medium import Medium, PoleResidue, Sellmeier, Debye, Drude, Lorentz, AnisotropicMedium, PEC
from .medium import nk_to_eps_complex, nk_to_eps_sigma, eps_complex_to_nk
from .medium import nk_to_medium, eps_sigma_to_eps_complex
from .medium import AbstractMedium

# structure
from .structure import Structure
Expand All @@ -36,4 +35,4 @@

# data
from .data import SimulationData, FieldData, FluxData, ModeData, FluxTimeData
from .data import ScalarFieldData, ScalarFieldTimeData, data_type_map
from .data import ScalarFieldData, ScalarFieldTimeData, DATA_TYPE_MAP
34 changes: 17 additions & 17 deletions tidy3d/components/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ def help(self, methods: bool = False) -> None:
rich.inspect(self, methods=methods)

@classmethod
def load(cls, fname: str):
def from_file(cls, fname: str):
"""Loads a :class:`Tidy3dBaseModel` from .yaml or .json file.

Parameters
Expand All @@ -82,15 +82,15 @@ def load(cls, fname: str):

Example
-------
>>> simulation = Simulation.load(fname='folder/sim.json')
>>> simulation = Simulation.from_file(fname='folder/sim.json')
"""
if ".json" in fname:
return cls.load_json(fname=fname)
return cls.from_json(fname=fname)
if ".yaml" in fname:
return cls.load_yaml(fname=fname)
return cls.from_yaml(fname=fname)
raise FileError(f"File must be .json or .yaml, given {fname}")

def export(self, fname: str) -> None:
def to_file(self, fname: str) -> None:
"""Exports :class:`Tidy3dBaseModel` instance to .yaml or .json file

Parameters
Expand All @@ -100,16 +100,16 @@ def export(self, fname: str) -> None:

Example
-------
>>> simulation.export(fname='folder/sim.json')
>>> simulation.to_file(fname='folder/sim.json')
"""
if ".json" in fname:
return self.export_json(fname=fname)
return self.to_json(fname=fname)
if ".yaml" in fname:
return self.export_yaml(fname=fname)
return self.to_yaml(fname=fname)
raise FileError(f"File must be .json or .yaml, given {fname}")

@classmethod
def load_json(cls, fname: str):
def from_json(cls, fname: str):
"""Load a :class:`Tidy3dBaseModel` from .json file.

Parameters
Expand All @@ -124,11 +124,11 @@ def load_json(cls, fname: str):

Example
-------
>>> simulation = Simulation.load_json(fname='folder/sim.json')
>>> simulation = Simulation.from_json(fname='folder/sim.json')
"""
return cls.parse_file(fname)

def export_json(self, fname: str) -> None:
def to_json(self, fname: str) -> None:
"""Exports :class:`Tidy3dBaseModel` instance to .json file

Parameters
Expand All @@ -138,14 +138,14 @@ def export_json(self, fname: str) -> None:

Example
-------
>>> simulation.export_json(fname='folder/sim.json')
>>> simulation.to_json(fname='folder/sim.json')
"""
json_string = self._json_string()
with open(fname, "w", encoding="utf-8") as file_handle:
file_handle.write(json_string)

@classmethod
def load_yaml(cls, fname: str):
def from_yaml(cls, fname: str):
"""Loads :class:`Tidy3dBaseModel` from .yaml file.

Parameters
Expand All @@ -156,18 +156,18 @@ def load_yaml(cls, fname: str):
Returns
-------
:class:`Tidy3dBaseModel`
An instance of the component class calling `load_yaml`.
An instance of the component class calling `from_yaml`.

Example
-------
>>> simulation = Simulation.load_yaml(fname='folder/sim.yaml')
>>> simulation = Simulation.from_yaml(fname='folder/sim.yaml')
"""
with open(fname, "r", encoding="utf-8") as yaml_in:
json_dict = yaml.safe_load(yaml_in)
json_raw = json.dumps(json_dict, indent=INDENT)
return cls.parse_raw(json_raw)

def export_yaml(self, fname: str) -> None:
def to_yaml(self, fname: str) -> None:
"""Exports :class:`Tidy3dBaseModel` instance to .yaml file.

Parameters
Expand All @@ -177,7 +177,7 @@ def export_yaml(self, fname: str) -> None:

Example
-------
>>> simulation.export_yaml(fname='folder/sim.yaml')
>>> simulation.to_yaml(fname='folder/sim.yaml')
"""
json_string = self._json_string()
json_dict = json.loads(json_string)
Expand Down
Loading