Skip to content
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

Fix TDS.init() and TDS._csv_step() #530

Merged
merged 5 commits into from
Apr 13, 2024
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
36 changes: 30 additions & 6 deletions andes/routines/tds.py
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
@@ -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")
Loading