Skip to content

Commit

Permalink
Merge branch 'develop' of github.com:curent/andes into develop
Browse files Browse the repository at this point in the history
  • Loading branch information
cuihantao committed Apr 13, 2024
2 parents 213896a + 2a24a17 commit 7f56cc9
Show file tree
Hide file tree
Showing 4 changed files with 87 additions and 7 deletions.
2 changes: 1 addition & 1 deletion andes/models/measurement/busfreq.py
Expand Up @@ -29,7 +29,7 @@ def __init__(self, system, config):
unit="sec",
tex_name='T_f',
)
self.Tw = NumParam(default=0.02,
self.Tw = NumParam(default=0.1,
info="washout time const",
unit="sec",
tex_name='T_w',
Expand Down
36 changes: 30 additions & 6 deletions andes/routines/tds.py
Expand Up @@ -237,8 +237,15 @@ def init(self):

# discard initialized values and use that from CSV if provided
if self.data_csv is not None:
system.dae.x[:] = self.data_csv[0, 1:system.dae.n + 1]
system.dae.y[:] = self.data_csv[0, system.dae.n + 1:system.dae.n + system.dae.m + 1]
if system.Output.n < 1:
system.dae.x[:] = self.data_csv[self.k_csv, 1:system.dae.n + 1]
system.dae.y[:] = self.data_csv[self.k_csv, system.dae.n + 1:system.dae.n + system.dae.m + 1]
else:
xyidx = system.Output.xidx + [yidx+system.dae.n for yidx in system.Output.yidx]
_xy = np.zeros(system.dae.n + system.dae.m)
_xy[xyidx] = self.data_csv[self.k_csv, 1:]
system.dae.x[:] = _xy[:system.dae.n]
system.dae.y[:] = _xy[system.dae.n:]
system.vars_to_models()

# connect to data streaming server
Expand Down Expand Up @@ -537,12 +544,23 @@ def itm_step(self):
def _csv_step(self):
"""
Fetch data for the next step from ``data_csv``.
When `Output` exists, the target variables `x` and `y` are filled with the data,
while the remaining variables are set to zero.
"""

system = self.system
if self.data_csv is not None:
system.dae.x[:] = self.data_csv[self.k_csv, 1:system.dae.n + 1]
system.dae.y[:] = self.data_csv[self.k_csv, system.dae.n + 1:system.dae.n + system.dae.m + 1]
if system.Output.n < 1:
system.dae.x[:] = self.data_csv[self.k_csv, 1:system.dae.n + 1]
system.dae.y[:] = self.data_csv[self.k_csv, system.dae.n + 1:system.dae.n + system.dae.m + 1]
else:
xyidx = system.Output.xidx + [yidx+system.dae.n for yidx in system.Output.yidx]
_xy = np.zeros(system.dae.n + system.dae.m)
_xy[xyidx] = self.data_csv[self.k_csv, 1:]
system.dae.x[:] = _xy[:system.dae.n]
system.dae.y[:] = _xy[system.dae.n:]

system.vars_to_models()

self.converged = True
Expand Down Expand Up @@ -926,8 +944,14 @@ def _load_csv(self, csv_file):
if data.shape[0] < 2:
logger.warning("CSV data does not contain more than one time step.")
if data.shape[1] < (self.system.dae.m + self.system.dae.n):
logger.warning("CSV data contains fewer variables than required.")
logger.warning("Check if the CSV data file is generated from the test case.")
if self.system.Output.n < 1:
logger.warning("CSV data contains fewer variables than required.")
logger.warning("Check if the CSV data file is generated from the test case.")
else:
logger.info("Output selection detected.")
if data.shape[1] - 1 < (len(self.system.Output.xidx + self.system.Output.yidx)):
logger.warning("CSV data contains fewer variables than required.")
logger.warning("Check if the CSV data file is generated with selected output.")

# set start and end times from data
self.config.t0 = data[0, 0]
Expand Down
2 changes: 2 additions & 0 deletions docs/source/release-notes.rst
Expand Up @@ -14,6 +14,8 @@ v1.9.3 (2024-04-XX)
- In symbolic processor, most variables are assumed to be real, except some
services that are specified as complex. This will allow generating simplified
expressions.
- Adjust `BusFreq.Tw.default` to 0.1.
- Fix `TDS.init()` and `TDS._csv_step()` to fit loading from CSV when `Output` exists.

v1.9.2 (2024-03-25)
-------------------
Expand Down
54 changes: 54 additions & 0 deletions tests/test_output.py
@@ -1,6 +1,7 @@
"""
Test output selection.
"""
import os

import numpy as np
import unittest
Expand Down Expand Up @@ -73,3 +74,56 @@ def test_output_xyname(self):
np.testing.assert_equal(
ss.dae.ts.get_data(ss.TG2.pout, a=None).shape[1],
0)

def test_from_csv(self):
"""
Test from_csv when loading selected output from csv file.
"""
case = andes.get_case("5bus/pjm5bus.json")
ss = andes.load(case,
no_output=True,
setup=False,
default_config=True)

ss.add("Output", {"model": "Bus", "varname": "v"})

ss.setup()

ss.PFlow.run()
ss.TDS.config.tf = 0.1
ss.TDS.run()
ss.TDS.load_plotter()

ss.TDS.plt.export_csv("pjm5bus_selec_out.csv")

# Test assign CSV in TDS.run()
ss2 = andes.load(case,
no_output=True,
setup=False,
default_config=True)

ss2.add("Output", {"model": "Bus", "varname": "v"})

ss2.setup()

ss2.PFlow.run()
ss2.TDS.run(from_csv="pjm5bus_selec_out.csv")

self.assertTrue(ss2.TDS.converged)

# Test assign CSV in andes.load()
ss3 = andes.load(case,
no_output=True,
setup=False,
default_config=True,
from_csv="pjm5bus_selec_out.csv")
ss3.add("Output", {"model": "Bus", "varname": "v"})

ss3.setup()

ss3.PFlow.run()
ss3.TDS.run()

self.assertTrue(ss3.TDS.converged)

os.remove("pjm5bus_selec_out.csv")

0 comments on commit 7f56cc9

Please sign in to comment.