From dfeea4d5210882d6e952178e3d83ab8b7201eb91 Mon Sep 17 00:00:00 2001 From: Johann Rudloff Date: Wed, 6 Feb 2019 22:15:04 +0100 Subject: [PATCH 1/7] Fix input parsing and netlist generation for piecewise-linear (PWL) sources. --- PySpice/Spice/HighLevelElement.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/PySpice/Spice/HighLevelElement.py b/PySpice/Spice/HighLevelElement.py index b8dade899..c46a0fcb5 100644 --- a/PySpice/Spice/HighLevelElement.py +++ b/PySpice/Spice/HighLevelElement.py @@ -361,7 +361,7 @@ def __init__(self, values, repeate_time=0, delay_time=.0): # Fixme: default - self.values = [self.__as_unit__(x) for x in values] + self.values = sum(([as_s(t), self.__as_unit__(x)] for (t, x) in values), []) self.repeate_time = as_s(repeate_time) self.delay_time = as_s(delay_time) @@ -371,7 +371,7 @@ def format_spice_parameters(self): # Fixme: to func? return ('PWL(' + - join_list(self.values) + + join_list(self.values) + ' ' + join_dict({'r':self.repeate_time, 'td':self.delay_time}) + # OrderedDict( ')') From d15c07892ec0f0d1eef01b4aaad77879fdf8e549 Mon Sep 17 00:00:00 2001 From: Johann Rudloff Date: Wed, 6 Feb 2019 22:15:47 +0100 Subject: [PATCH 2/7] Update documentation to explain usage of PieceWiseLinear sources. --- PySpice/Spice/HighLevelElement.py | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/PySpice/Spice/HighLevelElement.py b/PySpice/Spice/HighLevelElement.py index c46a0fcb5..ee47a455d 100644 --- a/PySpice/Spice/HighLevelElement.py +++ b/PySpice/Spice/HighLevelElement.py @@ -353,6 +353,11 @@ class PieceWiseLinearMixin(SourceMixinAbc): delay time time = td. The current source still needs to be patched, td and r are not yet available. + `values` should be given as a list of (`Time`, `Value`)-tuples, e.g.:: + + PieceWiseLinearVoltageSource(Circuit(''), 'inpwl1', '1', '0', + values=[(0, 0), (10@u_ms, 0), (11@u_ms, 5@u_V), (20@u_ms, 5@u_V)]) + """ ############################################## From a39c78f61f6845ce949b7fc1240bcfed2d2532db Mon Sep 17 00:00:00 2001 From: Johann Rudloff Date: Wed, 6 Feb 2019 22:17:02 +0100 Subject: [PATCH 3/7] Add unit test for PieceWiseLinearVoltageSource. --- .travis.yml | 1 + unit-test/Spice/test_HighLevelElement.py | 53 ++++++++++++++++++++++++ 2 files changed, 54 insertions(+) create mode 100644 unit-test/Spice/test_HighLevelElement.py diff --git a/.travis.yml b/.travis.yml index 771b87520..8538a58b6 100644 --- a/.travis.yml +++ b/.travis.yml @@ -8,3 +8,4 @@ install: script: - python unit-test/Spice/test_BasicElement.py + - python unit-test/Spice/test_HighLevelElement.py diff --git a/unit-test/Spice/test_HighLevelElement.py b/unit-test/Spice/test_HighLevelElement.py new file mode 100644 index 000000000..fce41c96c --- /dev/null +++ b/unit-test/Spice/test_HighLevelElement.py @@ -0,0 +1,53 @@ +#################################################################################################### +# +# PySpice - A Spice Package for Python +# Copyright (C) 2014 Fabrice Salvaire +# +# This program is free software: you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program. If not, see . +# +#################################################################################################### + +#################################################################################################### + +import unittest + +#################################################################################################### + +from PySpice.Spice.HighLevelElement import * +from PySpice.Spice.Netlist import Circuit +from PySpice.Unit import * + +#################################################################################################### + +class TestHighLevelElement(unittest.TestCase): + + ############################################## + + def _test_spice_declaration(self, element, spice_declaration): + + self.assertEqual(str(element), spice_declaration) + + ############################################## + + def test(self): + + self._test_spice_declaration(PieceWiseLinearVoltageSource(Circuit(''), 'inpwl1', '1', '0', + values=[(0, 0), (10@u_ms, 0), (11@u_ms, 5@u_V), (20@u_ms, 5@u_V)]), + 'Vinpwl1 1 0 PWL(0s 0V 10ms 0V 11ms 5V 20ms 5V r=0s td=0.0s)') + +#################################################################################################### + +if __name__ == '__main__': + + unittest.main() From 4378568535ed49d220d43a7b985aaf2c3ce109ec Mon Sep 17 00:00:00 2001 From: Johann Rudloff Date: Thu, 7 Feb 2019 19:21:56 +0100 Subject: [PATCH 4/7] Convert path to str before open to fix install with Python 3.5 (should fix Travis build) --- setup_data.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/setup_data.py b/setup_data.py index 6012e0ad6..cb5aa38d3 100644 --- a/setup_data.py +++ b/setup_data.py @@ -32,7 +32,7 @@ else: pyspice_path = pyspice_path.parent init_path = pyspice_path.joinpath('PySpice', '__init__.py') -with open(init_path) as fh: +with open(str(init_path)) as fh: try: version = re.findall(r"^__version__ = '([^']+)'\r?$", fh.read(), re.M)[0] except IndexError: From 3d211ee3892f88c32a9190ae07ca887f41bb1fae Mon Sep 17 00:00:00 2001 From: Johann Rudloff Date: Thu, 7 Feb 2019 19:43:33 +0100 Subject: [PATCH 5/7] Sort items in join_dict, to guarantee a stable order for unit tests. Since Python 3.7, dict.items() guarantees insertion order, but before, the order is undefined. To have reproducible output for tests, the params are now sorted lexicographically. --- PySpice/Tools/StringTools.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/PySpice/Tools/StringTools.py b/PySpice/Tools/StringTools.py index ac11fea63..b10baa9df 100644 --- a/PySpice/Tools/StringTools.py +++ b/PySpice/Tools/StringTools.py @@ -65,5 +65,5 @@ def join_list(items): def join_dict(d): return ' '.join(["{}={}".format(key, str_spice(value)) - for key, value in d.items() + for key, value in sorted(d.items()) if value is not None]) From e7fa4d53d4e93da93d2c02dd449daab0e43d4739 Mon Sep 17 00:00:00 2001 From: Johann Rudloff Date: Thu, 7 Feb 2019 19:21:56 +0100 Subject: [PATCH 6/7] Convert path to str before open to fix install with Python 3.5 (should fix Travis build) --- setup_data.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/setup_data.py b/setup_data.py index 6012e0ad6..cb5aa38d3 100644 --- a/setup_data.py +++ b/setup_data.py @@ -32,7 +32,7 @@ else: pyspice_path = pyspice_path.parent init_path = pyspice_path.joinpath('PySpice', '__init__.py') -with open(init_path) as fh: +with open(str(init_path)) as fh: try: version = re.findall(r"^__version__ = '([^']+)'\r?$", fh.read(), re.M)[0] except IndexError: From 91e174489914295320db42d7341074af31a2150e Mon Sep 17 00:00:00 2001 From: Johann Rudloff Date: Thu, 7 Feb 2019 19:43:33 +0100 Subject: [PATCH 7/7] Sort items in join_dict, to guarantee a stable order for unit tests. Since Python 3.7, dict.items() guarantees insertion order, but before, the order is undefined. To have reproducible output for tests, the params are now sorted lexicographically. --- PySpice/Tools/StringTools.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/PySpice/Tools/StringTools.py b/PySpice/Tools/StringTools.py index ac11fea63..b10baa9df 100644 --- a/PySpice/Tools/StringTools.py +++ b/PySpice/Tools/StringTools.py @@ -65,5 +65,5 @@ def join_list(items): def join_dict(d): return ' '.join(["{}={}".format(key, str_spice(value)) - for key, value in d.items() + for key, value in sorted(d.items()) if value is not None])