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

PR #530 follow up #533

Merged
merged 6 commits into from
Apr 16, 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
61 changes: 31 additions & 30 deletions andes/routines/tds.py
Original file line number Diff line number Diff line change
Expand Up @@ -237,16 +237,7 @@ def init(self):

# discard initialized values and use that from CSV if provided
if self.data_csv is not None:
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._csv_data_to_dae()

# connect to data streaming server
if system.streaming.dimec is None:
Expand Down Expand Up @@ -348,17 +339,17 @@ def run(self, no_summary=False, from_csv=None, **kwargs):
system.exit_code += 1
return succeed

# load from csv is provided
if no_summary is False and (system.dae.t == 0):
self.summary()

# load from csv if provided
if from_csv is not None:
self.from_csv = from_csv
else:
self.from_csv = system.options.get("from_csv")
if self.from_csv is not None:
self.data_csv = self._load_csv(self.from_csv)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

_load_csv() after init(), to ensure smooth init when from_csv is given in TDS.run()

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What was the issue with the previous order?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The issue is when CSV is given at "andes.load(from_csv='xx.csv')", TDS.init() run into error for the incompatible data size. It is because _load_csv() is called after TDS.init() in TDS.run().

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We can forget this as the introduced helper function TDS._csv_output() addressed it.

if no_summary is False and (system.dae.t == 0):
self.summary()

# only initializing at t<0 allows to continue when `run` is called again.
if system.dae.t < 0:
self.init()
Expand Down Expand Up @@ -549,19 +540,7 @@ def _csv_step(self):
while the remaining variables are set to zero.
"""

system = self.system
if self.data_csv is not None:
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._csv_data_to_dae()

self.converged = True
return self.converged
Expand Down Expand Up @@ -943,13 +922,16 @@ def _load_csv(self, csv_file):
raise ValueError("Data from CSV is not 2-dimensional (time versus variable)")
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):
if self.system.Output.n < 1:

system = self.system
if data.shape[1] < (system.dae.m + system.dae.n):
if 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)):
# NOTE: data has first column as time, so the rest should be `n+m` from `Output`
if data.shape[1] - 1 < (len(system.Output.xidx + 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.")

Expand Down Expand Up @@ -1108,3 +1090,22 @@ def check_criteria(self):
res = deltadelta(self.system.dae.x[self.system.SynGen.delta_addr],
self.config.ddelta_limit)
return res

def _csv_data_to_dae(self):
"""
Helper function to fetch data when replaying from CSV file.

When loading from a partial CSV file, the recorded data is loaded
while the rest of the variables remain to be initial values.
"""
system = self.system
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:
xidx = system.Output.xidx
system.dae.x[xidx] = self.data_csv[self.k_csv, 1:len(xidx) + 1]
yidx = system.Output.yidx
system.dae.y[yidx] = self.data_csv[self.k_csv, len(xidx) + 1:len(xidx) + len(yidx) + 1]

system.vars_to_models()
1 change: 1 addition & 0 deletions docs/source/release-notes.rst
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ v1.9.3 (2024-04-XX)
services that are specified as complex. This will allow generating simplified
expressions.
- Adjust `BusFreq.Tw.default` to 0.1.
- Add parameter from_csv=None in TDS.run() to allow loading data from CSV files at TDS begining.
- Fix `TDS.init()` and `TDS._csv_step()` to fit loading from CSV when `Output` exists.

v1.9.2 (2024-03-25)
Expand Down
Loading