Skip to content

added create_component_from_spicemodel method in Circuit #947

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Mar 14, 2022
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
2 changes: 1 addition & 1 deletion _unittest/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@
test_project_name = "test_primitives"

sys.path.append(local_path)
from .launch_desktop_tests import run_desktop_tests
from _unittest.launch_desktop_tests import run_desktop_tests

# set scratch path and create it if necessary
scratch_path = tempfile.gettempdir()
Expand Down
19 changes: 19 additions & 0 deletions _unittest/example_models/test.lib
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
*----------------------------------------------------------------------
* PSPICE Model generated by PyAEDT
* Property : C = 0.22 [uF]
*----------------------------------------------------------------------
* Applicable Conditions:
* Frequency Range = 100Hz - 6GHz
* Temperature = -55 degC - 125 degC
* DC Bias Voltage = 0V - 100V
* Small Signal Operation
*----------------------------------------------------------------------
* Encrypted Netlist
*----------------------------------------------------------------------
.subckt GRM1234 Port1 Port2 PARAMS: temperature=25
C1 Port1 Port2 1e-12
.ends

.subckt GRM2345 Port1 Port2
C1 Port1 Port2 1e-12
.ends
6 changes: 6 additions & 0 deletions _unittest/test_21_Circuit.py
Original file line number Diff line number Diff line change
Expand Up @@ -320,3 +320,9 @@ def test_28_load_and_save_diff_pair_file(self):
with open(diff_file2, "r") as fh:
lines = fh.read().splitlines()
assert len(lines) == 3

def test_29_create_circuit_from_spice(self):
model = os.path.join(local_path, "example_models", "test.lib")
assert self.aedtapp.modeler.schematic.create_component_from_spicemodel(model)
assert self.aedtapp.modeler.schematic.create_component_from_spicemodel(model, "GRM2345")
assert not self.aedtapp.modeler.schematic.create_component_from_spicemodel(model, "GRM2346")
12 changes: 10 additions & 2 deletions pyaedt/circuit.py
Original file line number Diff line number Diff line change
Expand Up @@ -264,7 +264,11 @@ def create_schematic_from_netlist(self, file_to_import):
already_exist = True
if not already_exist:
self.modeler.schematic.create_new_component_from_symbol(
parameter, pins, fields[0][0], parameter_list, parameter_value
parameter,
pins,
refbase=fields[0][0],
parameter_list=parameter_list,
parameter_value=parameter_value,
)
mycomp = self.modeler.schematic.create_component(
fields[0],
Expand Down Expand Up @@ -295,7 +299,11 @@ def create_schematic_from_netlist(self, file_to_import):
already_exist = True
if not already_exist:
self.modeler.schematic.create_new_component_from_symbol(
parameter, pins, fields[0][0], parameter_list, parameter_value
parameter,
pins,
refbase=fields[0][0],
parameter_list=parameter_list,
parameter_value=parameter_value,
)
mycomp = self.modeler.schematic.create_component(
fields[0],
Expand Down
63 changes: 57 additions & 6 deletions pyaedt/modeler/PrimitivesNexxim.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import os
import re
import warnings

from pyaedt.generic.general_methods import generate_unique_name
Expand Down Expand Up @@ -928,7 +929,7 @@ def create_new_component_from_symbol(
pin_lists,
time_stamp=1591858313,
description="",
refbase="U",
refbase="x",
parameter_list=[],
parameter_value=[],
gref="",
Expand Down Expand Up @@ -1032,13 +1033,12 @@ def create_new_component_from_symbol(
arg2 = ["NAME:Parameters"]

for el, val in zip(parameter_list, parameter_value):
if isinstance(val, str):
if "MOD" in el:
arg2.append("TextValueProp:=")
arg2.append([el, "D", "", val])

else:
arg2.append("ValueProp:=")
arg2.append([el, "D", "", str(val), False, ""])
arg2.append("ValuePropNU:=")
arg2.append([el, "D", "", str(val), 0, ""])

arg2.append("ButtonProp:=")
arg2.append(["CosimDefinition", "D", "", "Edit", "Edit", 40501, "ButtonPropClientData:=", []])
Expand All @@ -1051,7 +1051,7 @@ def create_new_component_from_symbol(
while id < len(pin_lists):
spicesintax += "%" + str(id) + " "
id += 1
spicesintax += symbol_name + " "
# spicesintax += symbol_name + " "
for el, val in zip(parameter_list, parameter_value):
if "MOD" in el:
spicesintax += "@{} ".format(el)
Expand Down Expand Up @@ -1750,3 +1750,54 @@ def assign_sin_excitation2ports(self, ports, settings):
DeprecationWarning,
)
return self._app.assign_voltage_sinusoidal_excitation_to_ports(ports, settings)

@pyaedt_function_handler()
def _parse_spice_model(self, model_path):
models = []
with open(model_path, "r") as f:
for line in f:
if ".subckt" in line.lower():
pinNames = [i.strip() for i in re.split(" |\t", line) if i]
models.append(pinNames[1])
return models

@pyaedt_function_handler()
def create_component_from_spicemodel(self, model_path, model_name=None, location=None):
"""Create and place a new component based on a spice .lib file.

Parameters
----------
model_path : str
Path to .lib file.
model_name : str, optional
Model name to import. If `None` the first subckt in the lib file will be placed.
location : list, optional
Position in the schematic of the new component.

Returns
-------
:class:`pyaedt.modeler.Object3d.CircuitComponent`
Circuit Component Object.
"""
models = self._parse_spice_model(model_path)
if not model_name and models:
model_name = models[0]
elif model_name not in models:
return False
arg = ["NAME:Options", "Mode:=", 2, "Overwrite:=", False, "SupportsSimModels:=", False, "LoadOnly:=", False]
arg2 = ["NAME:Models"]
for el in models:
arg2.append(el + ":=")
if el == model_name:
arg2.append([True, "", "", False])
else:
arg2.append([False, "", "", False])
arg.append(arg2)
self.o_component_manager.ImportModelsFromFile(model_path, arg)

return self.create_component(
None,
component_library=None,
component_name=model_name,
location=location,
)
2 changes: 1 addition & 1 deletion requirements_test.txt
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
pyvista
matplotlib
numpy
pytest
pytest==7.0.0
pytest-cov
codecov