ansys-systemcoupling-core 0.5.0__py3-none-any.whl → 0.6__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Potentially problematic release.
This version of ansys-systemcoupling-core might be problematic. Click here for more details.
- ansys/systemcoupling/core/__init__.py +11 -5
- ansys/systemcoupling/core/adaptor/api_23_1/show_plot.py +75 -0
- ansys/systemcoupling/core/adaptor/api_23_1/solution_root.py +7 -1
- ansys/systemcoupling/core/adaptor/api_23_2/show_plot.py +75 -0
- ansys/systemcoupling/core/adaptor/api_23_2/solution_root.py +7 -1
- ansys/systemcoupling/core/adaptor/api_24_1/show_plot.py +75 -0
- ansys/systemcoupling/core/adaptor/api_24_1/solution_root.py +7 -1
- ansys/systemcoupling/core/adaptor/api_24_2/add_participant.py +4 -4
- ansys/systemcoupling/core/adaptor/api_24_2/setup_root.py +1 -1
- ansys/systemcoupling/core/adaptor/api_24_2/show_plot.py +75 -0
- ansys/systemcoupling/core/adaptor/api_24_2/solution_root.py +7 -1
- ansys/systemcoupling/core/adaptor/impl/injected_commands.py +215 -32
- ansys/systemcoupling/core/adaptor/impl/static_info.py +17 -0
- ansys/systemcoupling/core/adaptor/impl/syc_proxy.py +3 -0
- ansys/systemcoupling/core/adaptor/impl/syc_proxy_interface.py +4 -0
- ansys/systemcoupling/core/adaptor/impl/types.py +1 -1
- ansys/systemcoupling/core/charts/chart_datatypes.py +169 -0
- ansys/systemcoupling/core/charts/csv_chartdata.py +299 -0
- ansys/systemcoupling/core/charts/live_csv_datasource.py +87 -0
- ansys/systemcoupling/core/charts/message_dispatcher.py +84 -0
- ansys/systemcoupling/core/charts/plot_functions.py +92 -0
- ansys/systemcoupling/core/charts/plotdefinition_manager.py +303 -0
- ansys/systemcoupling/core/charts/plotter.py +343 -0
- ansys/systemcoupling/core/client/grpc_client.py +6 -1
- ansys/systemcoupling/core/participant/manager.py +25 -9
- ansys/systemcoupling/core/participant/protocol.py +1 -0
- ansys/systemcoupling/core/session.py +4 -4
- ansys/systemcoupling/core/syc_version.py +1 -1
- ansys/systemcoupling/core/util/file_transfer.py +4 -0
- {ansys_systemcoupling_core-0.5.0.dist-info → ansys_systemcoupling_core-0.6.dist-info}/METADATA +11 -10
- {ansys_systemcoupling_core-0.5.0.dist-info → ansys_systemcoupling_core-0.6.dist-info}/RECORD +33 -22
- {ansys_systemcoupling_core-0.5.0.dist-info → ansys_systemcoupling_core-0.6.dist-info}/LICENSE +0 -0
- {ansys_systemcoupling_core-0.5.0.dist-info → ansys_systemcoupling_core-0.6.dist-info}/WHEEL +0 -0
|
@@ -0,0 +1,299 @@
|
|
|
1
|
+
# Copyright (C) 2023 - 2024 ANSYS, Inc. and/or its affiliates.
|
|
2
|
+
# SPDX-License-Identifier: MIT
|
|
3
|
+
#
|
|
4
|
+
#
|
|
5
|
+
# Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
# of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
# in the Software without restriction, including without limitation the rights
|
|
8
|
+
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
# copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
# furnished to do so, subject to the following conditions:
|
|
11
|
+
#
|
|
12
|
+
# The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
# copies or substantial portions of the Software.
|
|
14
|
+
#
|
|
15
|
+
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
# SOFTWARE.
|
|
22
|
+
|
|
23
|
+
# from dataclasses import dataclass, field
|
|
24
|
+
import csv
|
|
25
|
+
from typing import TextIO, Union
|
|
26
|
+
|
|
27
|
+
from ansys.systemcoupling.core.charts.chart_datatypes import (
|
|
28
|
+
InterfaceInfo,
|
|
29
|
+
InterfaceSeriesData,
|
|
30
|
+
SeriesData,
|
|
31
|
+
SeriesType,
|
|
32
|
+
TimestepData,
|
|
33
|
+
TransferSeriesInfo,
|
|
34
|
+
)
|
|
35
|
+
|
|
36
|
+
HeaderList = list[str]
|
|
37
|
+
ChartData = list[list[float]]
|
|
38
|
+
|
|
39
|
+
|
|
40
|
+
class CsvReader:
|
|
41
|
+
def __init__(self, file_or_filename: Union[str, TextIO]):
|
|
42
|
+
self._file_or_filename = file_or_filename
|
|
43
|
+
self._headers: HeaderList = []
|
|
44
|
+
self._data: ChartData = []
|
|
45
|
+
self._started = False
|
|
46
|
+
|
|
47
|
+
def read_data(self) -> bool:
|
|
48
|
+
try:
|
|
49
|
+
if not self._started:
|
|
50
|
+
self._read_data_initial()
|
|
51
|
+
self._started = True
|
|
52
|
+
else:
|
|
53
|
+
self._read_data_incr()
|
|
54
|
+
# print("incremental data read")
|
|
55
|
+
return True # File exists - haven't necessarily read anything yet
|
|
56
|
+
except FileNotFoundError:
|
|
57
|
+
# It is expected that the file is not necessarily immediately available
|
|
58
|
+
print(f"Failed to open {self._file_or_filename}")
|
|
59
|
+
return False
|
|
60
|
+
except Exception as e:
|
|
61
|
+
# Temporary - see if anything else goes wrong
|
|
62
|
+
print(e)
|
|
63
|
+
return False
|
|
64
|
+
|
|
65
|
+
@property
|
|
66
|
+
def headers(self) -> HeaderList:
|
|
67
|
+
return self._headers
|
|
68
|
+
|
|
69
|
+
@property
|
|
70
|
+
def data(self) -> ChartData:
|
|
71
|
+
return self._data
|
|
72
|
+
|
|
73
|
+
def _read_data_initial(self):
|
|
74
|
+
f = None
|
|
75
|
+
try:
|
|
76
|
+
f = self._get_file()
|
|
77
|
+
reader = csv.reader(f)
|
|
78
|
+
header = None
|
|
79
|
+
for row in reader:
|
|
80
|
+
if header is None:
|
|
81
|
+
header = row
|
|
82
|
+
else:
|
|
83
|
+
self._append_row(row)
|
|
84
|
+
finally:
|
|
85
|
+
self._close_file(f)
|
|
86
|
+
|
|
87
|
+
self._headers = header
|
|
88
|
+
|
|
89
|
+
def _read_data_incr(self):
|
|
90
|
+
f = None
|
|
91
|
+
try:
|
|
92
|
+
f = self._get_file()
|
|
93
|
+
reader = csv.reader(f)
|
|
94
|
+
nlines = len(self._data) + 1
|
|
95
|
+
# TODO: try using seek/tell
|
|
96
|
+
for i, row in enumerate(reader):
|
|
97
|
+
if i < nlines:
|
|
98
|
+
continue
|
|
99
|
+
self._append_row(row)
|
|
100
|
+
finally:
|
|
101
|
+
self._close_file(f)
|
|
102
|
+
|
|
103
|
+
def _append_row(self, row_data: list[str]) -> None:
|
|
104
|
+
self._data.append([float(f) for f in row_data])
|
|
105
|
+
|
|
106
|
+
def _get_file(self) -> TextIO:
|
|
107
|
+
if isinstance(self._file_or_filename, str):
|
|
108
|
+
return open(self._file_or_filename, newline="")
|
|
109
|
+
else:
|
|
110
|
+
return self._file_or_filename
|
|
111
|
+
|
|
112
|
+
def _close_file(self, f: TextIO):
|
|
113
|
+
# Only close if we opened it ourself
|
|
114
|
+
if f and isinstance(self._file_or_filename, str):
|
|
115
|
+
f.close()
|
|
116
|
+
|
|
117
|
+
|
|
118
|
+
class CsvChartDataReader:
|
|
119
|
+
"""Reader of chart data and metadata from a single CSV file, which
|
|
120
|
+
contains data for a single coupling interface.
|
|
121
|
+
|
|
122
|
+
The metadata is derived from the column headings in the file.
|
|
123
|
+
"""
|
|
124
|
+
|
|
125
|
+
def __init__(self, interface_name: str, csvfile: Union[str, TextIO]) -> None:
|
|
126
|
+
self._interface_name = interface_name
|
|
127
|
+
self._csv_reader = CsvReader(csvfile)
|
|
128
|
+
self._metadata: InterfaceInfo = None
|
|
129
|
+
self._data: InterfaceSeriesData = None
|
|
130
|
+
self._timestep_data = TimestepData()
|
|
131
|
+
|
|
132
|
+
def read_metadata(self) -> bool:
|
|
133
|
+
if not self._csv_reader.read_data():
|
|
134
|
+
return False
|
|
135
|
+
self._metadata = parse_csv_metadata(
|
|
136
|
+
self._interface_name, self._csv_reader.headers
|
|
137
|
+
)
|
|
138
|
+
self._init_data()
|
|
139
|
+
return True
|
|
140
|
+
|
|
141
|
+
@property
|
|
142
|
+
def metadata(self) -> InterfaceInfo:
|
|
143
|
+
return self._metadata
|
|
144
|
+
|
|
145
|
+
@property
|
|
146
|
+
def data(self) -> InterfaceSeriesData:
|
|
147
|
+
return self._data
|
|
148
|
+
|
|
149
|
+
@property
|
|
150
|
+
def timestep_data(self) -> TimestepData:
|
|
151
|
+
return self._timestep_data
|
|
152
|
+
|
|
153
|
+
def read_new_data(self):
|
|
154
|
+
self._csv_reader.read_data()
|
|
155
|
+
self._process_curr_data()
|
|
156
|
+
|
|
157
|
+
def _init_data(self):
|
|
158
|
+
series_data_list: list[SeriesData] = []
|
|
159
|
+
for i, trans_info in enumerate(self._metadata.transfer_info):
|
|
160
|
+
if not trans_info.line_suffixes:
|
|
161
|
+
series_data_list.append(SeriesData(transfer_index=i))
|
|
162
|
+
else:
|
|
163
|
+
for j in range(len(trans_info.line_suffixes)):
|
|
164
|
+
series_data_list.append(
|
|
165
|
+
SeriesData(transfer_index=i, component_index=j)
|
|
166
|
+
)
|
|
167
|
+
self._data = InterfaceSeriesData(self._metadata, series=series_data_list)
|
|
168
|
+
|
|
169
|
+
def _process_curr_data(self):
|
|
170
|
+
raw_data = self._csv_reader.data
|
|
171
|
+
last_data_len = len(self._data.series[0].data)
|
|
172
|
+
|
|
173
|
+
has_time = self._metadata.is_transient
|
|
174
|
+
for i in range(last_data_len, len(raw_data)):
|
|
175
|
+
row = raw_data[i]
|
|
176
|
+
self._timestep_data.timestep.append(round(row[1]))
|
|
177
|
+
start_index = 2
|
|
178
|
+
if has_time:
|
|
179
|
+
self._timestep_data.time.append(row[2])
|
|
180
|
+
start_index += 1
|
|
181
|
+
for j in range(start_index, len(row)):
|
|
182
|
+
self._data.series[j - start_index].data.append(row[j])
|
|
183
|
+
|
|
184
|
+
|
|
185
|
+
def _extract_transfer_value_type(header: str) -> str:
|
|
186
|
+
for value_type in ("Sum", "Weighted Average"):
|
|
187
|
+
if f" ({value_type}):" in header:
|
|
188
|
+
return value_type
|
|
189
|
+
return None
|
|
190
|
+
|
|
191
|
+
|
|
192
|
+
def _parse_header(header: str) -> tuple[SeriesType, str, str]:
|
|
193
|
+
if header.startswith("Data Transfer Convergence (RMS Change in Target Value):"):
|
|
194
|
+
ipfx = header.find(":")
|
|
195
|
+
isep = header.find(" - ", ipfx)
|
|
196
|
+
intf_disp_name = header[ipfx + 1 : isep].strip()
|
|
197
|
+
trans_disp_name = header[isep + 3 :].strip()
|
|
198
|
+
return SeriesType.CONVERGENCE, intf_disp_name, trans_disp_name
|
|
199
|
+
elif value_type := _extract_transfer_value_type(header):
|
|
200
|
+
trans_disp_name = header[: header.find(f"({value_type})")].strip()
|
|
201
|
+
part_disp_name = header[header.find(":") + 1 :].strip()
|
|
202
|
+
part_disp_name = _remove_suffix(part_disp_name)
|
|
203
|
+
return (
|
|
204
|
+
SeriesType.SUM if value_type == "Sum" else SeriesType.WEIGHTED_AVERAGE,
|
|
205
|
+
part_disp_name,
|
|
206
|
+
trans_disp_name,
|
|
207
|
+
)
|
|
208
|
+
raise ValueError(f"Invalid column header in CSV file: {header}")
|
|
209
|
+
|
|
210
|
+
|
|
211
|
+
def _remove_suffix(label: str) -> str:
|
|
212
|
+
def remove_partial_suffix(str_in: str, suffixes: tuple[str]) -> str:
|
|
213
|
+
for suffix in suffixes:
|
|
214
|
+
suffix = f" {suffix}"
|
|
215
|
+
if str_in.endswith(suffix):
|
|
216
|
+
return str_in[: len(str_in) - len(suffix)]
|
|
217
|
+
return str_in
|
|
218
|
+
|
|
219
|
+
# Suffix has format "[ <x|y|z>][ <real|imag>]" so remove real/imag first
|
|
220
|
+
label = remove_partial_suffix(label, ("real", "imag"))
|
|
221
|
+
label = remove_partial_suffix(label, ("x", "y", "z"))
|
|
222
|
+
return label
|
|
223
|
+
|
|
224
|
+
|
|
225
|
+
def _parse_suffix(header: str, part_disp_name: str) -> str:
|
|
226
|
+
idx = header.find(f": {part_disp_name} ")
|
|
227
|
+
if idx == -1:
|
|
228
|
+
return ""
|
|
229
|
+
return header[idx + len(part_disp_name) + 3 :].strip()
|
|
230
|
+
|
|
231
|
+
|
|
232
|
+
def parse_csv_metadata(interface_name: str, headers: list[str]) -> InterfaceInfo:
|
|
233
|
+
intf_info = InterfaceInfo(name=interface_name)
|
|
234
|
+
assert headers[0] == "Iteration"
|
|
235
|
+
assert headers[1] == "Step"
|
|
236
|
+
intf_info.is_transient = headers[2] == "Time"
|
|
237
|
+
|
|
238
|
+
start_index = 3 if intf_info.is_transient else 2
|
|
239
|
+
prev_part_name = ""
|
|
240
|
+
|
|
241
|
+
transfer_disambig: dict[str, int] = {}
|
|
242
|
+
for i in range(start_index, len(headers)):
|
|
243
|
+
header = headers[i]
|
|
244
|
+
data_index = i - start_index
|
|
245
|
+
series_type, intf_or_part_disp_name, trans_disp_name = _parse_header(header)
|
|
246
|
+
if series_type == SeriesType.CONVERGENCE:
|
|
247
|
+
prev_part_name = ""
|
|
248
|
+
if trans_disp_name in transfer_disambig:
|
|
249
|
+
transfer_disambig[trans_disp_name] += 1
|
|
250
|
+
else:
|
|
251
|
+
transfer_disambig[trans_disp_name] = 0
|
|
252
|
+
|
|
253
|
+
intf_disp_name = intf_or_part_disp_name
|
|
254
|
+
if data_index == 0:
|
|
255
|
+
assert intf_info.display_name == ""
|
|
256
|
+
intf_info.display_name = intf_disp_name
|
|
257
|
+
series_info = TransferSeriesInfo(
|
|
258
|
+
data_index,
|
|
259
|
+
series_type,
|
|
260
|
+
transfer_display_name=trans_disp_name,
|
|
261
|
+
disambiguation_index=transfer_disambig[trans_disp_name],
|
|
262
|
+
)
|
|
263
|
+
intf_info.transfer_info.append(series_info)
|
|
264
|
+
else:
|
|
265
|
+
part_disp_name = intf_or_part_disp_name
|
|
266
|
+
suffix = _parse_suffix(header, part_disp_name)
|
|
267
|
+
if suffix:
|
|
268
|
+
if prev_part_name != part_disp_name:
|
|
269
|
+
# Start a new series info for a group of components
|
|
270
|
+
# Thus if there are 3 components, say, they will
|
|
271
|
+
# implicitly have data indexes, data_index, data_index+1,
|
|
272
|
+
# data_index+2, and the next TransferSeriesInfo will
|
|
273
|
+
# have index data_index+3.
|
|
274
|
+
intf_info.transfer_info.append(
|
|
275
|
+
TransferSeriesInfo(
|
|
276
|
+
data_index,
|
|
277
|
+
series_type,
|
|
278
|
+
transfer_display_name=trans_disp_name,
|
|
279
|
+
disambiguation_index=transfer_disambig[trans_disp_name],
|
|
280
|
+
participant_display_name=part_disp_name,
|
|
281
|
+
line_suffixes=[suffix],
|
|
282
|
+
)
|
|
283
|
+
)
|
|
284
|
+
prev_part_name = part_disp_name
|
|
285
|
+
else:
|
|
286
|
+
# Append component info to current series info
|
|
287
|
+
intf_info.transfer_info[-1].line_suffixes.append(suffix)
|
|
288
|
+
else:
|
|
289
|
+
prev_part_name = ""
|
|
290
|
+
intf_info.transfer_info.append(
|
|
291
|
+
TransferSeriesInfo(
|
|
292
|
+
data_index,
|
|
293
|
+
series_type,
|
|
294
|
+
transfer_display_name=trans_disp_name,
|
|
295
|
+
disambiguation_index=transfer_disambig[trans_disp_name],
|
|
296
|
+
participant_display_name=part_disp_name,
|
|
297
|
+
)
|
|
298
|
+
)
|
|
299
|
+
return intf_info
|
|
@@ -0,0 +1,87 @@
|
|
|
1
|
+
# Copyright (C) 2023 - 2024 ANSYS, Inc. and/or its affiliates.
|
|
2
|
+
# SPDX-License-Identifier: MIT
|
|
3
|
+
#
|
|
4
|
+
#
|
|
5
|
+
# Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
# of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
# in the Software without restriction, including without limitation the rights
|
|
8
|
+
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
# copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
# furnished to do so, subject to the following conditions:
|
|
11
|
+
#
|
|
12
|
+
# The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
# copies or substantial portions of the Software.
|
|
14
|
+
#
|
|
15
|
+
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
# SOFTWARE.
|
|
22
|
+
|
|
23
|
+
import threading
|
|
24
|
+
import time
|
|
25
|
+
from typing import Callable, TextIO, Union
|
|
26
|
+
|
|
27
|
+
from ansys.systemcoupling.core.charts.chart_datatypes import SeriesData
|
|
28
|
+
from ansys.systemcoupling.core.charts.csv_chartdata import CsvChartDataReader
|
|
29
|
+
from ansys.systemcoupling.core.charts.message_dispatcher import Message, MsgType
|
|
30
|
+
|
|
31
|
+
|
|
32
|
+
class LiveCsvDataSource:
|
|
33
|
+
def __init__(
|
|
34
|
+
self,
|
|
35
|
+
interface_name: str,
|
|
36
|
+
csvfile: Union[str, TextIO],
|
|
37
|
+
put_msg: Callable[[Message], None],
|
|
38
|
+
):
|
|
39
|
+
self._csv_reader = CsvChartDataReader(interface_name, csvfile)
|
|
40
|
+
self._put_msg = put_msg
|
|
41
|
+
self._is_cancelled = threading.Event()
|
|
42
|
+
self._last_data_len = []
|
|
43
|
+
|
|
44
|
+
def cancel(self):
|
|
45
|
+
self._is_cancelled.set()
|
|
46
|
+
|
|
47
|
+
def read_data(self):
|
|
48
|
+
|
|
49
|
+
while not self._is_cancelled.is_set():
|
|
50
|
+
if not self._csv_reader.read_metadata():
|
|
51
|
+
time.sleep(0.5)
|
|
52
|
+
else:
|
|
53
|
+
self._put_msg(
|
|
54
|
+
Message(type=MsgType.METADATA, data=self._csv_reader.metadata)
|
|
55
|
+
)
|
|
56
|
+
break
|
|
57
|
+
|
|
58
|
+
last_round = False
|
|
59
|
+
while True:
|
|
60
|
+
self._csv_reader.read_new_data()
|
|
61
|
+
data = self._csv_reader.data
|
|
62
|
+
if not self._last_data_len:
|
|
63
|
+
self._last_data_len = [0] * len(data.series)
|
|
64
|
+
for i, line_series in enumerate(data.series):
|
|
65
|
+
start_index = self._last_data_len[i]
|
|
66
|
+
new_data_len = len(line_series.data)
|
|
67
|
+
|
|
68
|
+
if new_data_len - start_index == 0:
|
|
69
|
+
continue
|
|
70
|
+
|
|
71
|
+
line_series_incr = SeriesData(
|
|
72
|
+
line_series.transfer_index,
|
|
73
|
+
line_series.component_index,
|
|
74
|
+
start_index=start_index,
|
|
75
|
+
data=line_series.data[start_index:],
|
|
76
|
+
)
|
|
77
|
+
self._put_msg(Message(type=MsgType.SERIES_DATA, data=line_series_incr))
|
|
78
|
+
self._last_data_len[i] = new_data_len
|
|
79
|
+
|
|
80
|
+
if last_round:
|
|
81
|
+
self._put_msg(Message(type=MsgType.END_OF_DATA))
|
|
82
|
+
break
|
|
83
|
+
elif self._is_cancelled.is_set():
|
|
84
|
+
# Allow opportunity for any trailing updates to file
|
|
85
|
+
last_round = True
|
|
86
|
+
|
|
87
|
+
time.sleep(0.5)
|
|
@@ -0,0 +1,84 @@
|
|
|
1
|
+
# Copyright (C) 2023 - 2024 ANSYS, Inc. and/or its affiliates.
|
|
2
|
+
# SPDX-License-Identifier: MIT
|
|
3
|
+
#
|
|
4
|
+
#
|
|
5
|
+
# Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
# of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
# in the Software without restriction, including without limitation the rights
|
|
8
|
+
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
# copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
# furnished to do so, subject to the following conditions:
|
|
11
|
+
#
|
|
12
|
+
# The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
# copies or substantial portions of the Software.
|
|
14
|
+
#
|
|
15
|
+
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
# SOFTWARE.
|
|
22
|
+
|
|
23
|
+
from dataclasses import dataclass
|
|
24
|
+
from enum import IntEnum
|
|
25
|
+
import queue
|
|
26
|
+
from typing import Protocol, Union
|
|
27
|
+
|
|
28
|
+
from ansys.systemcoupling.core.charts.chart_datatypes import (
|
|
29
|
+
InterfaceInfo,
|
|
30
|
+
SeriesData,
|
|
31
|
+
TimestepData,
|
|
32
|
+
)
|
|
33
|
+
|
|
34
|
+
|
|
35
|
+
class PlotterProtocol(Protocol):
|
|
36
|
+
def set_metadata(self, metadata: InterfaceInfo): ...
|
|
37
|
+
def set_timestep_data(self, timestep_data: TimestepData): ...
|
|
38
|
+
def update_line_series(self, series_data: SeriesData): ...
|
|
39
|
+
def close(self): ...
|
|
40
|
+
|
|
41
|
+
|
|
42
|
+
class MsgType(IntEnum):
|
|
43
|
+
NO_DATA_AVAILABLE = 1
|
|
44
|
+
END_OF_DATA = 2
|
|
45
|
+
CLOSE_PLOT = 3
|
|
46
|
+
METADATA = 4
|
|
47
|
+
TIMESTEP_DATA = 5
|
|
48
|
+
SERIES_DATA = 6
|
|
49
|
+
|
|
50
|
+
|
|
51
|
+
@dataclass
|
|
52
|
+
class Message:
|
|
53
|
+
type: MsgType
|
|
54
|
+
data: Union[InterfaceInfo, TimestepData, SeriesData, None] = None
|
|
55
|
+
|
|
56
|
+
|
|
57
|
+
class MessageDispatcher:
|
|
58
|
+
def __init__(self):
|
|
59
|
+
self._q = queue.Queue()
|
|
60
|
+
|
|
61
|
+
def set_plotter(self, plotter: PlotterProtocol):
|
|
62
|
+
self._plotter = plotter
|
|
63
|
+
|
|
64
|
+
def put_msg(self, msg: Message):
|
|
65
|
+
self._q.put(msg)
|
|
66
|
+
|
|
67
|
+
def dispatch_messages(self):
|
|
68
|
+
while True:
|
|
69
|
+
try:
|
|
70
|
+
msg: Message = self._q.get(timeout=0.001)
|
|
71
|
+
msg_t = msg.type
|
|
72
|
+
print(f"dispatch message of type: {msg.type.name}")
|
|
73
|
+
if msg_t == MsgType.METADATA:
|
|
74
|
+
self._plotter.set_metadata(msg.data)
|
|
75
|
+
elif msg_t == MsgType.TIMESTEP_DATA:
|
|
76
|
+
self._plotter.set_timestep_data(msg.data)
|
|
77
|
+
elif msg_t == MsgType.SERIES_DATA:
|
|
78
|
+
self._plotter.update_line_series(msg.data)
|
|
79
|
+
elif msg_t in (MsgType.END_OF_DATA, MsgType.NO_DATA_AVAILABLE):
|
|
80
|
+
return
|
|
81
|
+
elif msg_t == MsgType.CLOSE_PLOT:
|
|
82
|
+
self._plotter.close()
|
|
83
|
+
except queue.Empty:
|
|
84
|
+
return
|
|
@@ -0,0 +1,92 @@
|
|
|
1
|
+
# Copyright (C) 2023 - 2024 ANSYS, Inc. and/or its affiliates.
|
|
2
|
+
# SPDX-License-Identifier: MIT
|
|
3
|
+
#
|
|
4
|
+
#
|
|
5
|
+
# Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
# of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
# in the Software without restriction, including without limitation the rights
|
|
8
|
+
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
# copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
# furnished to do so, subject to the following conditions:
|
|
11
|
+
#
|
|
12
|
+
# The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
# copies or substantial portions of the Software.
|
|
14
|
+
#
|
|
15
|
+
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
# SOFTWARE.
|
|
22
|
+
|
|
23
|
+
import threading
|
|
24
|
+
from typing import Callable
|
|
25
|
+
|
|
26
|
+
from ansys.systemcoupling.core.charts.csv_chartdata import CsvChartDataReader
|
|
27
|
+
from ansys.systemcoupling.core.charts.live_csv_datasource import LiveCsvDataSource
|
|
28
|
+
from ansys.systemcoupling.core.charts.message_dispatcher import MessageDispatcher
|
|
29
|
+
from ansys.systemcoupling.core.charts.plotdefinition_manager import (
|
|
30
|
+
PlotDefinitionManager,
|
|
31
|
+
PlotSpec,
|
|
32
|
+
)
|
|
33
|
+
from ansys.systemcoupling.core.charts.plotter import Plotter
|
|
34
|
+
|
|
35
|
+
|
|
36
|
+
def create_and_show_plot(spec: PlotSpec, csv_list: list[str]) -> Plotter:
|
|
37
|
+
assert len(spec.interfaces) == 1, "Plots currently only support one interface"
|
|
38
|
+
assert len(spec.interfaces) == len(csv_list)
|
|
39
|
+
|
|
40
|
+
manager = PlotDefinitionManager(spec)
|
|
41
|
+
reader = CsvChartDataReader(spec.interfaces[0].name, csv_list[0])
|
|
42
|
+
plotter = Plotter(manager)
|
|
43
|
+
|
|
44
|
+
reader.read_metadata()
|
|
45
|
+
plotter.set_metadata(reader.metadata)
|
|
46
|
+
|
|
47
|
+
reader.read_new_data()
|
|
48
|
+
data = reader.data
|
|
49
|
+
if reader.metadata.is_transient:
|
|
50
|
+
plotter.set_timestep_data(reader.timestep_data)
|
|
51
|
+
|
|
52
|
+
for line_series in data.series:
|
|
53
|
+
plotter.update_line_series(line_series)
|
|
54
|
+
|
|
55
|
+
plotter.show_plot(noblock=True)
|
|
56
|
+
return plotter
|
|
57
|
+
|
|
58
|
+
|
|
59
|
+
def solve_with_live_plot(
|
|
60
|
+
spec: PlotSpec,
|
|
61
|
+
csv_list: list[str],
|
|
62
|
+
solve_func: Callable[[], None],
|
|
63
|
+
):
|
|
64
|
+
assert len(spec.interfaces) == 1, "Plots currently only support one interface"
|
|
65
|
+
assert len(spec.interfaces) == len(csv_list)
|
|
66
|
+
|
|
67
|
+
manager = PlotDefinitionManager(spec)
|
|
68
|
+
dispatcher = MessageDispatcher()
|
|
69
|
+
plotter = Plotter(manager, dispatcher.dispatch_messages)
|
|
70
|
+
dispatcher.set_plotter(plotter)
|
|
71
|
+
|
|
72
|
+
data_source = LiveCsvDataSource(
|
|
73
|
+
spec.interfaces[0].name, csv_list[0], dispatcher.put_msg
|
|
74
|
+
)
|
|
75
|
+
data_thread = threading.Thread(target=data_source.read_data)
|
|
76
|
+
|
|
77
|
+
def solve():
|
|
78
|
+
solve_func()
|
|
79
|
+
data_source.cancel()
|
|
80
|
+
|
|
81
|
+
solve_thread = threading.Thread(target=solve)
|
|
82
|
+
|
|
83
|
+
data_thread.start()
|
|
84
|
+
solve_thread.start()
|
|
85
|
+
|
|
86
|
+
plotter.show_animated()
|
|
87
|
+
data_source.cancel()
|
|
88
|
+
data_thread.join()
|
|
89
|
+
solve_thread.join()
|
|
90
|
+
|
|
91
|
+
# Show a non-blocking static plot
|
|
92
|
+
return create_and_show_plot(spec, csv_list)
|