-
Notifications
You must be signed in to change notification settings - Fork 248
/
Copy pathtest_co_sim_io_py_exposure.py
366 lines (275 loc) · 16.5 KB
/
test_co_sim_io_py_exposure.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
import KratosMultiphysics as KM
import KratosMultiphysics.KratosUnittest as KratosUnittest
from KratosMultiphysics.CoSimulationApplication import CoSimIO
from KratosMultiphysics import kratos_utilities as kratos_utils
from KratosMultiphysics.testing.utilities import GetPython3Command
import os
import subprocess
class TestCoSimIOPyExposure_aux_tests(KratosUnittest.TestCase):
def test_InfoFromParameters(self):
params = KM.Parameters("""{
"some_bool" : true,
"the_string" : "file",
"another_string" : "i25mmk",
"int_for_it" : 12,
"double_val" : 0.223,
"array_val_ign" : [0.923, 1.8],
"sub_param" : {
"tol" : 0.01,
"is_converged" : false,
"echo_lvl" : 5
}
}""")
info = CoSimIO.InfoFromParameters(params)
self.assertEqual(info.Size(), 6)
self.assertTrue(info.Has("some_bool"))
self.assertTrue(info.Has("the_string"))
self.assertTrue(info.Has("another_string"))
self.assertTrue(info.Has("int_for_it"))
self.assertTrue(info.Has("double_val"))
self.assertTrue(info.Has("sub_param"))
# only int, bool, double, string and sub-param can be converted, others are ignored
self.assertFalse(info.Has("array_val_ign"))
self.assertTrue(info.GetBool("some_bool"))
self.assertEqual(info.GetString("the_string"), "file")
self.assertEqual(info.GetString("another_string"), "i25mmk")
self.assertEqual(info.GetInt("int_for_it"), 12)
self.assertAlmostEqual(info.GetDouble("double_val"), 0.223)
sub_info = info.GetInfo("sub_param")
self.assertEqual(sub_info.Size(), 3)
self.assertAlmostEqual(sub_info.GetDouble("tol"), 0.01)
self.assertFalse(sub_info.GetBool("is_converged"))
self.assertEqual(sub_info.GetInt("echo_lvl"), 5)
class TestCoSimIOPyExposure(KratosUnittest.TestCase):
def test_Connect_Disconnect(self):
p = self.__RunPythonInSubProcess("connect_disconnect")
connection_settings = CoSimIO.Info()
connection_settings.SetString("my_name", "partner_a")
connection_settings.SetString("connect_to", "partner_b")
connection_settings.SetInt("echo_level", 0)
info = CoSimIO.Connect(connection_settings)
connection_name = info.GetString("connection_name")
self.assertEqual(info.GetInt("connection_status"), CoSimIO.ConnectionStatus.Connected)
disconnect_settings = CoSimIO.Info()
disconnect_settings.SetString("connection_name", connection_name)
info = CoSimIO.Disconnect(disconnect_settings)
self.assertEqual(info.GetInt("connection_status"), CoSimIO.ConnectionStatus.Disconnected)
self.__CheckSubProcess(p)
def test_Export_Import_Data_raw_values(self):
p = self.__RunPythonInSubProcess("import_export_data")
connection_settings = CoSimIO.Info()
connection_settings.SetString("my_name", "ExpImp")
connection_settings.SetString("connect_to", "impExp")
connection_settings.SetInt("echo_level", 0)
info = CoSimIO.Connect(connection_settings)
connection_name = info.GetString("connection_name")
self.assertEqual(info.GetInt("connection_status"), CoSimIO.ConnectionStatus.Connected)
values = CoSimIO.DoubleVector([1.0, 2.5, 3.3, -9.4])
export_info = CoSimIO.Info()
export_info.SetString("connection_name", connection_name)
export_info.SetString("identifier", "data_exchange_1")
CoSimIO.ExportData(export_info, values)
import_info = CoSimIO.Info()
import_info.SetString("connection_name", connection_name)
import_info.SetString("identifier", "data_exchange_2")
imported_values = CoSimIO.DoubleVector()
CoSimIO.ImportData(import_info, imported_values)
disconnect_settings = CoSimIO.Info()
disconnect_settings.SetString("connection_name", connection_name)
info = CoSimIO.Disconnect(disconnect_settings)
self.assertEqual(info.GetInt("connection_status"), CoSimIO.ConnectionStatus.Disconnected)
# checking the values after disconnecting to avoid deadlock
self.assertVectorAlmostEqual(values, imported_values)
self.__CheckSubProcess(p)
def test_Export_Import_Data_ModelPart_scalar_node_historical(self):
model = KM.Model()
model_part = model.CreateModelPart("for_test")
model_part.AddNodalSolutionStepVariable(KM.PRESSURE)
model_part.AddNodalSolutionStepVariable(KM.TEMPERATURE)
for i in range(5):
node = model_part.CreateNewNode(i+1, 0.0, 0.0, 0.0) # using same coord, doesn't matter for this test
node.SetSolutionStepValue(KM.PRESSURE, i*1.7)
self.__ExportImportDataOnModelPart(model_part, KM.PRESSURE, KM.TEMPERATURE, KM.Globals.DataLocation.NodeHistorical)
# checking the values after disconnecting to avoid deadlock
for i, node in enumerate(model_part.Nodes):
self.assertAlmostEqual(node.GetSolutionStepValue(KM.TEMPERATURE), i*1.7)
def test_Export_Import_Data_ModelPart_vector_node_historical(self):
model = KM.Model()
model_part = model.CreateModelPart("for_test")
model_part.AddNodalSolutionStepVariable(KM.DISPLACEMENT)
model_part.AddNodalSolutionStepVariable(KM.VELOCITY)
for i in range(5):
node = model_part.CreateNewNode(i+1, 0.0, 0.0, 0.0) # using same coord, doesn't matter for this test
node.SetSolutionStepValue(KM.DISPLACEMENT, [i*1.7, i+1.1, i**1.2])
self.__ExportImportDataOnModelPart(model_part, KM.DISPLACEMENT, KM.VELOCITY, KM.Globals.DataLocation.NodeHistorical)
# checking the values after disconnecting to avoid deadlock
for i, node in enumerate(model_part.Nodes):
self.assertVectorAlmostEqual(node.GetSolutionStepValue(KM.VELOCITY), KM.Vector([i*1.7, i+1.1, i**1.2]))
def test_Export_Import_Data_ModelPart_scalar_node_non_historical(self):
model = KM.Model()
model_part = model.CreateModelPart("for_test")
model_part.AddNodalSolutionStepVariable(KM.PRESSURE)
model_part.AddNodalSolutionStepVariable(KM.TEMPERATURE)
for i in range(5):
node = model_part.CreateNewNode(i+1, 0.0, 0.0, 0.0) # using same coord, doesn't matter for this test
node.SetValue(KM.PRESSURE, i*1.6)
self.__ExportImportDataOnModelPart(model_part, KM.PRESSURE, KM.TEMPERATURE, KM.Globals.DataLocation.NodeNonHistorical)
# checking the values after disconnecting to avoid deadlock
for i, node in enumerate(model_part.Nodes):
self.assertAlmostEqual(node.GetValue(KM.TEMPERATURE), i*1.6)
def test_Export_Import_Data_ModelPart_vector_node_non_historical(self):
model = KM.Model()
model_part = model.CreateModelPart("for_test")
for i in range(5):
node = model_part.CreateNewNode(i+1, 0.0, 0.0, 0.0) # using same coord, doesn't matter for this test
node.SetValue(KM.DISPLACEMENT, [i*1.7, i+1.99, i**1.2])
self.__ExportImportDataOnModelPart(model_part, KM.DISPLACEMENT, KM.VELOCITY, KM.Globals.DataLocation.NodeNonHistorical)
# checking the values after disconnecting to avoid deadlock
for i, node in enumerate(model_part.Nodes):
self.assertVectorAlmostEqual(node.GetValue(KM.VELOCITY), KM.Vector([i*1.7, i+1.99, i**1.2]))
def test_Export_Import_Data_ModelPart_scalar_element(self):
model = KM.Model()
model_part = model.CreateModelPart("for_test")
for i in range(5):
model_part.CreateNewNode(i+1, 0.0, 0.0, 0.0) # using same coord, doesn't matter for this test
props = model_part.CreateNewProperties(0)
for i in range(4):
element = model_part.CreateNewElement("Element2D2N", i+1, [i+1, i+2], props)
element.SetValue(KM.PRESSURE, i*1.6)
self.__ExportImportDataOnModelPart(model_part, KM.PRESSURE, KM.TEMPERATURE, KM.Globals.DataLocation.Element)
# checking the values after disconnecting to avoid deadlock
for i, elem in enumerate(model_part.Elements):
self.assertAlmostEqual(elem.GetValue(KM.TEMPERATURE), i*1.6)
def test_Export_Import_Data_ModelPart_vector_element(self):
model = KM.Model()
model_part = model.CreateModelPart("for_test")
for i in range(5):
model_part.CreateNewNode(i+1, 0.0, 0.0, 0.0) # using same coord, doesn't matter for this test
props = model_part.CreateNewProperties(0)
for i in range(4):
element = model_part.CreateNewElement("Element2D2N", i+1, [i+1, i+2], props)
element.SetValue(KM.DISPLACEMENT, [i*1.7, i+1.99, i**1.2])
self.__ExportImportDataOnModelPart(model_part, KM.DISPLACEMENT, KM.VELOCITY, KM.Globals.DataLocation.Element)
# checking the values after disconnecting to avoid deadlock
for i, elem in enumerate(model_part.Elements):
self.assertVectorAlmostEqual(elem.GetValue(KM.VELOCITY), KM.Vector([i*1.7, i+1.99, i**1.2]))
def test_Export_Import_Data_ModelPart_scalar_condition(self):
model = KM.Model()
model_part = model.CreateModelPart("for_test")
for i in range(5):
model_part.CreateNewNode(i+1, 0.0, 0.0, 0.0) # using same coord, doesn't matter for this test
props = model_part.CreateNewProperties(0)
for i in range(5):
condition = model_part.CreateNewCondition("PointCondition2D1N", i+1, [i+1], props)
condition.SetValue(KM.PRESSURE, i*(-11.6))
self.__ExportImportDataOnModelPart(model_part, KM.PRESSURE, KM.TEMPERATURE, KM.Globals.DataLocation.Condition)
# checking the values after disconnecting to avoid deadlock
for i, cond in enumerate(model_part.Conditions):
self.assertAlmostEqual(cond.GetValue(KM.TEMPERATURE), i*(-11.6))
def test_Export_Import_Data_ModelPart_vector_condition(self):
model = KM.Model()
model_part = model.CreateModelPart("for_test")
for i in range(5):
model_part.CreateNewNode(i+1, 0.0, 0.0, 0.0) # using same coord, doesn't matter for this test
props = model_part.CreateNewProperties(0)
for i in range(5):
condition = model_part.CreateNewCondition("PointCondition2D1N", i+1, [i+1], props)
condition.SetValue(KM.DISPLACEMENT, [i*1.7, i+1.25, i**1.2])
self.__ExportImportDataOnModelPart(model_part, KM.DISPLACEMENT, KM.VELOCITY, KM.Globals.DataLocation.Condition)
# checking the values after disconnecting to avoid deadlock
for i, cond in enumerate(model_part.Conditions):
self.assertVectorAlmostEqual(cond.GetValue(KM.VELOCITY), KM.Vector([i*1.7, i+1.25, i**1.2]))
def test_Export_Import_Data_ModelPart_scalar_model_part(self):
model = KM.Model()
model_part = model.CreateModelPart("for_test")
model_part.SetValue(KM.PRESSURE, 333.896)
self.__ExportImportDataOnModelPart(model_part, KM.PRESSURE, KM.TEMPERATURE, KM.Globals.DataLocation.ModelPart)
# checking the values after disconnecting to avoid deadlock
self.assertAlmostEqual(model_part.GetValue(KM.TEMPERATURE), 333.896)
def test_Export_Import_Data_ModelPart_vector_model_part(self):
model = KM.Model()
model_part = model.CreateModelPart("for_test")
model_part.SetValue(KM.DISPLACEMENT, [14.3, -333.896, 987.2])
self.__ExportImportDataOnModelPart(model_part, KM.DISPLACEMENT, KM.VELOCITY, KM.Globals.DataLocation.ModelPart)
# checking the values after disconnecting to avoid deadlock
self.assertVectorAlmostEqual(model_part.GetValue(KM.VELOCITY), KM.Vector([14.3, -333.896, 987.2]))
def test_Export_Import_Mesh(self):
p = self.__RunPythonInSubProcess("import_export_mesh")
model = KM.Model()
model_part = model.CreateModelPart("for_test")
model_part_returned = model.CreateModelPart("for_test_2")
for i in range(15):
model_part.CreateNewNode(i+1, i*1.1, 1.1**i, 0.0)
props = model_part.CreateNewProperties(0)
for i in range(9): # this leaves some hanging nodes
model_part.CreateNewElement("Element2D2N", i+1, [i+1, i+2], props)
connection_settings = CoSimIO.Info()
connection_settings.SetString("my_name", "ExpImpMesh")
connection_settings.SetString("connect_to", "impExpMesh")
connection_settings.SetInt("echo_level", 0)
info = CoSimIO.Connect(connection_settings)
connection_name = info.GetString("connection_name")
self.assertEqual(info.GetInt("connection_status"), CoSimIO.ConnectionStatus.Connected)
export_info = CoSimIO.Info()
export_info.SetString("connection_name", connection_name)
export_info.SetString("identifier", "mesh_exchange_1")
CoSimIO.ExportMesh(export_info, model_part)
import_info = CoSimIO.Info()
import_info.SetString("connection_name", connection_name)
import_info.SetString("identifier", "mesh_exchange_2")
CoSimIO.ImportMesh(import_info, model_part_returned, KM.Testing.GetDefaultDataCommunicator())
disconnect_settings = CoSimIO.Info()
disconnect_settings.SetString("connection_name", connection_name)
info = CoSimIO.Disconnect(disconnect_settings)
self.assertEqual(info.GetInt("connection_status"), CoSimIO.ConnectionStatus.Disconnected)
self.__CheckSubProcess(p)
# checking the values after disconnecting to avoid deadlock
self.assertEqual(model_part.NumberOfNodes(), model_part_returned.NumberOfNodes())
self.assertEqual(model_part.NumberOfElements(), model_part_returned.NumberOfElements())
for node_orig, node_exchanged in zip(model_part.Nodes, model_part_returned.Nodes):
self.assertEqual(node_orig.Id, node_exchanged.Id)
self.assertAlmostEqual(node_orig.X0, node_exchanged.X0)
self.assertAlmostEqual(node_orig.Y0, node_exchanged.Y0)
self.assertAlmostEqual(node_orig.Z0, node_exchanged.Z0)
for elem_orig, elem_exchanged in zip(model_part.Elements, model_part_returned.Elements):
self.assertEqual(len(elem_orig.GetNodes()), len(elem_exchanged.GetNodes()))
for node_orig, node_exchanged in zip(elem_orig.GetNodes(), elem_exchanged.GetNodes()):
self.assertEqual(node_orig.Id, node_exchanged.Id)
self.assertAlmostEqual(node_orig.X0, node_exchanged.X0)
self.assertAlmostEqual(node_orig.Y0, node_exchanged.Y0)
self.assertAlmostEqual(node_orig.Z0, node_exchanged.Z0)
def __ExportImportDataOnModelPart(self, model_part, var_export, var_import, data_location):
p = self.__RunPythonInSubProcess("import_export_data")
connection_settings = CoSimIO.Info()
connection_settings.SetString("my_name", "ExpImp")
connection_settings.SetString("connect_to", "impExp")
connection_settings.SetInt("echo_level", 0)
info = CoSimIO.Connect(connection_settings)
connection_name = info.GetString("connection_name")
self.assertEqual(info.GetInt("connection_status"), CoSimIO.ConnectionStatus.Connected)
export_info = CoSimIO.Info()
export_info.SetString("connection_name", connection_name)
export_info.SetString("identifier", "data_exchange_1")
CoSimIO.ExportData(export_info, model_part, var_export, data_location)
import_info = CoSimIO.Info()
import_info.SetString("connection_name", connection_name)
import_info.SetString("identifier", "data_exchange_2")
CoSimIO.ImportData(import_info, model_part, var_import, data_location)
disconnect_settings = CoSimIO.Info()
disconnect_settings.SetString("connection_name", connection_name)
info = CoSimIO.Disconnect(disconnect_settings)
self.assertEqual(info.GetInt("connection_status"), CoSimIO.ConnectionStatus.Disconnected)
self.__CheckSubProcess(p)
def __RunPythonInSubProcess(self, script_name):
if not script_name.endswith(".py"):
script_name += ".py"
return subprocess.Popen([GetPython3Command(), os.path.join("co_sim_io_py_exposure_aux_files", script_name)], stdout=subprocess.PIPE)
def __CheckSubProcess(self, proc):
try:
p_out = proc.communicate(timeout=5)
except subprocess.TimeoutExpired: # Timeout reached
proc.kill()
p_out = proc.communicate()
self.assertEqual(proc.returncode, 0, msg=p_out[0].decode('ascii'))
if __name__ == '__main__':
KratosUnittest.main()