Skip to content

Commit

Permalink
Merge pull request #485 from zaidmahmood/fix_Teye
Browse files Browse the repository at this point in the history
Fixed Teye
  • Loading branch information
cuihantao committed Sep 27, 2023
2 parents f161574 + da70a29 commit 095ae59
Show file tree
Hide file tree
Showing 4 changed files with 68 additions and 5 deletions.
7 changes: 7 additions & 0 deletions andes/core/model/model.py
Original file line number Diff line number Diff line change
Expand Up @@ -514,6 +514,13 @@ def set(self, src, idx, attr, value):
for state in self.states.values():
if (state.t_const is instance) and len(state.a) > 0:
self.system.dae.Tf[state.a[uid]] = instance.v[uid]
# convert scalar `uid` to list
if isinstance(uid, (float, int, str, np.integer, np.floating)):
uid = [uid]
# set diagonal elements of `Teye` for each element in `uid``
uid_int = state.a.tolist()
for ii in uid:
self.system.TDS.Teye[uid_int[ii], uid_int[ii]] = instance.v[ii]

return True

Expand Down
6 changes: 3 additions & 3 deletions andes/io/streaming.py
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,7 @@ def _find_pos(self, model, fkey, src_col=0):
"""Find the positions of foreign keys in the source model index list"""
if isinstance(fkey, ndarray):
fkey = fkey.tolist()
elif type(fkey) in (int, float):
elif isinstance(fkey, (int, float)):
fkey = [fkey]

ret = []
Expand Down Expand Up @@ -254,7 +254,7 @@ def send_init(self, recepient='all'):
)
sleep(0.5)
else:
if type(recepient) != list:
if not isinstance(recepient, list):
recepient = [recepient]
for item in recepient:
self.dimec.send_r(item, Varheader=self.Varheader)
Expand Down Expand Up @@ -292,7 +292,7 @@ def record_module_init(self, name, init_var):

@staticmethod
def transpose_matlab_row(a):
if type(a) is ndarray:
if isinstance(a, ndarray):
if a.shape[0] == 1:
a = a[0]
return a
Expand Down
4 changes: 2 additions & 2 deletions tests/test_discrete.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,8 +73,8 @@ def test_sorted_limiter(self):
cmp.zu.tolist())

# test when no `n_select` is over range
rcmp_noselect = SortedLimiter(self.u, self.lower, self.upper,
n_select=999)
rcmp_noselect = SortedLimiter(self.u, self.lower, self.upper, n_select=999)

rcmp_noselect.list2array(len(self.u.v))
rcmp_noselect.check_var()

Expand Down
56 changes: 56 additions & 0 deletions tests/test_model_set.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
import unittest
import numpy as np

import andes


class TestModelMethods(unittest.TestCase):
"""
Test methods of Model.
"""

def test_model_set(self):
"""
Test `Model.set()` method.
"""

ss = andes.run(
andes.get_case("ieee14/ieee14.json"),
default_config=True,
no_output=True,
)
ss.TDS.init()

omega_addr = ss.GENROU.omega.a.tolist()

# set a single value
ss.GENROU.set("M", "GENROU_1", "v", 2.0)
self.assertEqual(ss.GENROU.M.v[0], 2.0)
self.assertEqual(ss.TDS.Teye[omega_addr[0], omega_addr[0]], 2.0)

# set a list of values
ss.GENROU.set("M", ["GENROU_1", "GENROU_2"], "v", [2.0, 3.5])
np.testing.assert_equal(ss.GENROU.M.v[[0, 1]], [2.0, 3.5])
self.assertEqual(ss.TDS.Teye[omega_addr[0], omega_addr[0]], 2.0)
self.assertEqual(ss.TDS.Teye[omega_addr[1], omega_addr[1]], 3.5)

# set a list of values
ss.GENROU.set("M", ["GENROU_3", "GENROU_5"], "v", [2.0, 3.5])
np.testing.assert_equal(ss.GENROU.M.v[[2, 4]], [2.0, 3.5])
self.assertEqual(ss.TDS.Teye[omega_addr[2], omega_addr[2]], 2.0)
self.assertEqual(ss.TDS.Teye[omega_addr[4], omega_addr[4]], 3.5)

# set a list of idxes with a single element to an array of values
ss.GENROU.set("M", ["GENROU_4"], "v", np.array([4.0]))
np.testing.assert_equal(ss.GENROU.M.v[3], 4.0)
self.assertEqual(ss.TDS.Teye[omega_addr[3], omega_addr[3]], 4.0)

# set an array of idxes with a single element to an array of values
ss.GENROU.set("M", np.array(["GENROU_4"]), "v", np.array([5.0]))
np.testing.assert_equal(ss.GENROU.M.v[3], 5.0)
self.assertEqual(ss.TDS.Teye[omega_addr[3], omega_addr[3]], 5.0)

# set an array of idxes with a list of single value
ss.GENROU.set("M", np.array(["GENROU_4"]), "v", 6.0)
np.testing.assert_equal(ss.GENROU.M.v[3], 6.0)
self.assertEqual(ss.TDS.Teye[omega_addr[3], omega_addr[3]], 6.0)

0 comments on commit 095ae59

Please sign in to comment.