Skip to content

Commit

Permalink
adjust fixtures in sim validation and add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
rmoretti9 committed Jun 7, 2024
1 parent 9a9f98f commit 161af97
Show file tree
Hide file tree
Showing 7 changed files with 106 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
get_post_process_command_lines,
get_combined_parameters,
export_simulation_json,
validate_simulation
validate_simulation,
)
from kqcircuits.util.export_helper import write_commit_reference_file
from kqcircuits.simulations.export.util import export_layers
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
get_post_process_command_lines,
get_combined_parameters,
export_simulation_json,
validate_simulation
validate_simulation,
)
from kqcircuits.simulations.export.util import export_layers
from kqcircuits.util.export_helper import write_commit_reference_file
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
from kqcircuits.util.geometry_json_encoder import GeometryJsonEncoder
from kqcircuits.simulations.export.simulation_validate import ValidateSim


def get_combined_parameters(simulation, solution):
"""Return parameters of Simulation and Solution in a combined dictionary.
In case of common keys, 'solution.' prefix is added to Solution parameter key.
Expand Down Expand Up @@ -172,4 +173,4 @@ def validate_simulation(simulation, solution):
validate_sim = ValidateSim()
validate_sim.has_no_ports_when_required(simulation, solution)
validate_sim.has_edgeport_when_forbidden(simulation, solution)
validate_sim.flux_integration_layer_exists_if_needed(simulation, solution)
validate_sim.flux_integration_layer_exists_if_needed(simulation, solution)
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@
# Please see our contribution agreements for individuals (meetiqm.com/iqm-individual-contributor-license-agreement)
# and organizations (meetiqm.com/iqm-organization-contributor-license-agreement).

class ValidateSim():

class ValidateSim:
"""Validation class that contains consistency checks."""

def __init__(self):
Expand All @@ -40,7 +41,9 @@ def has_no_ports_when_required(self, simulation, solution):
"ElmerVectorHelmholtzSolution",
"ElmerCapacitanceSolution",
]:
raise ValidateSimError(f"Simulation '{sim_name}' has no ports assigned. This is incompatible with {solution_type}")
raise ValidateSimError(
f"Simulation '{sim_name}' has no ports assigned. This is incompatible with {solution_type}"
)

def has_edgeport_when_forbidden(self, simulation, solution):
"""Validation check: ensure that if at least one "EdgePort" is present, some solution types can't be chosen.
Expand All @@ -62,7 +65,6 @@ def has_edgeport_when_forbidden(self, simulation, solution):
f"Simulation '{sim_name}' has at least one 'EdgePort'. This is incompatible with {solution_type}"
)


def flux_integration_layer_exists_if_needed(self, simulation, solution):
"""Validation check related to the presence of layers and magnetic flux integration.
Args:
Expand Down Expand Up @@ -115,6 +117,7 @@ def get_solution_type(solution):

class ValidateSimError(Exception):
"""Custom exception class for specific error handling."""

def __init__(self, message, errors=None):
super().__init__(message)
self.errors = errors
5 changes: 3 additions & 2 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,9 @@ def get_sim(cls, **parameters):

@pytest.fixture
def perform_test_ansys_export_produces_output_files(tmp_path, get_simulation):
def perform_test_ansys_export_produces_output_implementation(cls, **parameters):
bat_filename = export_ansys([get_simulation(cls, **parameters)], path=tmp_path)
def perform_test_ansys_export_produces_output_implementation(cls, ansys_solution=None, **parameters):
simulation = get_simulation(cls, **parameters)
bat_filename = export_ansys([(simulation, ansys_solution) if ansys_solution else simulation], path=tmp_path)
assert Path(bat_filename).exists()

return perform_test_ansys_export_produces_output_implementation
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@


from kqcircuits.simulations.empty_simulation import EmptySimulation
from kqcircuits.simulations.export.ansys.ansys_solution import AnsysSolution


def test_export_produces_output_files(perform_test_ansys_export_produces_output_files):
perform_test_ansys_export_produces_output_files(EmptySimulation)
perform_test_ansys_export_produces_output_files(EmptySimulation, ansys_solution=AnsysSolution())
106 changes: 91 additions & 15 deletions tests/simulations/simulation_validate/test_simulation_validate.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,23 +19,99 @@
from kqcircuits.simulations.export.simulation_validate import ValidateSim
from kqcircuits.simulations.simulation import Simulation
from kqcircuits.pya_resolver import pya
from kqcircuits.simulations.port import InternalPort
from kqcircuits.simulations.port import InternalPort, EdgePort
from kqcircuits.util.export_helper import (
get_active_or_new_layout,
)
from kqcircuits.simulations.export.ansys.ansys_solution import AnsysCurrentSolution, AnsysHfssSolution, AnsysQ3dSolution, AnsysEigenmodeSolution
from kqcircuits.simulations.export.elmer.elmer_solution import ElmerVectorHelmholtzSolution, ElmerCapacitanceSolution, ElmerCrossSectionSolution

class MockSimulation(Simulation):
def build(self):
pass
self.ports.append(InternalPort(0, *self.etched_line(pya.DPoint(0, 0), pya.DPoint(1, 1))))
layout = get_active_or_new_layout()
simulation = MockSimulation(layout)

def test_has_no_ports_when_required():
simulation.ports = [InternalPort(0, *simulation.etched_line(pya.DPoint(0, 0), pya.DPoint(1, 1)))]
from kqcircuits.simulations.export.ansys.ansys_solution import (
AnsysCurrentSolution,
AnsysHfssSolution,
AnsysQ3dSolution,
AnsysEigenmodeSolution,
AnsysVoltageSolution,
)
from kqcircuits.simulations.export.elmer.elmer_solution import (
ElmerVectorHelmholtzSolution,
ElmerCapacitanceSolution,
ElmerCrossSectionSolution,
)
from kqcircuits.simulations.export.simulation_validate import ValidateSimError
import pytest


@pytest.fixture
def mock_simulation():
simulation = Simulation(pya.Layout())
simulation.name = "test_sim"
solution = AnsysCurrentSolution()
return simulation


def test_has_no_ports_when_required(mock_simulation):
ports = [InternalPort(0, [0, 0, 1, 1])]
mock_simulation.ports = ports
validator = ValidateSim()
sol_types = [
AnsysCurrentSolution(),
AnsysHfssSolution(),
AnsysVoltageSolution(),
ElmerVectorHelmholtzSolution(),
ElmerCapacitanceSolution(),
]
for solution in sol_types:
validator.has_no_ports_when_required(mock_simulation, solution)


def test_raise_no_port_error_when_required(mock_simulation):
validator = ValidateSim()
sol_types = [
AnsysCurrentSolution(),
AnsysHfssSolution(),
AnsysVoltageSolution(),
ElmerVectorHelmholtzSolution(),
ElmerCapacitanceSolution(),
]
for solution in sol_types:
with pytest.raises(ValidateSimError):
validator.has_no_ports_when_required(mock_simulation, solution)


def test_has_edgeport_when_forbidden(mock_simulation):
ports = [InternalPort(0, [0, 0, 1, 1])]
mock_simulation.ports = ports
sol_types = [AnsysEigenmodeSolution(), AnsysVoltageSolution(), AnsysCurrentSolution()]
validator = ValidateSim()
for solution in sol_types:
validator.has_edgeport_when_forbidden(mock_simulation, solution)


def test_raise_edgeport_error_when_forbidden(mock_simulation):
ports = [EdgePort(0, [0, 0, 1, 1])]
mock_simulation.ports = ports
sol_types = [AnsysEigenmodeSolution(), AnsysVoltageSolution(), AnsysCurrentSolution()]
validator = ValidateSim()
for solution in sol_types:
with pytest.raises(ValidateSimError):
validator.has_edgeport_when_forbidden(mock_simulation, solution)


def test_flux_integration_layer_exists_if_needed(mock_simulation):
validator = ValidateSim()
solution = AnsysVoltageSolution()
validator.flux_integration_layer_exists_if_needed(mock_simulation, solution)
mock_simulation.layers["flux_integration_layer"] = {"z": 0.0, "thickness": 0.0, "material": "non-pec"}
solution.integrate_magnetic_flux = True
validator.flux_integration_layer_exists_if_needed(mock_simulation, solution)


def test_raise_flux_integration_error(mock_simulation):
validator = ValidateSim()
validator.has_no_ports_when_required(simulation, solution)
solution = AnsysVoltageSolution()
solution.integrate_magnetic_flux = True
with pytest.raises(ValidateSimError):
validator.flux_integration_layer_exists_if_needed(mock_simulation, solution)
mock_simulation.layers["flux_integration_layer"] = {"z": 0.0, "thickness": 0.1, "material": "non-pec"}
with pytest.raises(ValidateSimError):
validator.flux_integration_layer_exists_if_needed(mock_simulation, solution)
mock_simulation.layers["flux_integration_layer"] = {"z": 0.0, "thickness": 0.0, "material": "pec"}
with pytest.raises(ValidateSimError):
validator.flux_integration_layer_exists_if_needed(mock_simulation, solution)

0 comments on commit 161af97

Please sign in to comment.