-
Notifications
You must be signed in to change notification settings - Fork 246
/
test_coupling_interface_data.py
510 lines (379 loc) · 19.5 KB
/
test_coupling_interface_data.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
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
import KratosMultiphysics as KM
import KratosMultiphysics.KratosUnittest as KratosUnittest
from KratosMultiphysics.CoSimulationApplication.coupling_interface_data import CouplingInterfaceData
# The expected definitions are here to make the handling of the
# multiline-stings easier (no need to deal with indentation)
coupling_interface_data_str = '''CouplingInterfaceData:
Name: "default"
SolverWrapper: "default_solver"
ModelPart: "mp_4_test"
IsDistributed: False
Variable: "DISPLACEMENT" (Vector with dimension: 2)
Location: "node_historical"
Size: 10
'''
model_part_scalar_value = -61.225
model_part_vector_value = [123.5, 54.9, -92.4]
model_part_scalar_value_2 = 9745.34
model_part_vector_value_2 = [-556.3, -334.2, 65.9]
class TestCouplingInterfaceData(KratosUnittest.TestCase):
def setUp(self):
self.model = KM.Model()
self.mp = self.model.CreateModelPart("mp_4_test", 2)
self.mp.AddNodalSolutionStepVariable(KM.PRESSURE)
self.mp.AddNodalSolutionStepVariable(KM.DISPLACEMENT)
self.mp.ProcessInfo[KM.DOMAIN_SIZE] = 2
num_nodes = 5
num_elems = 7
num_conds = 3
props = self.mp.CreateNewProperties(2)
for i in range(num_nodes):
node_id = i+1
node = self.mp.CreateNewNode(node_id, 0.0, 0.0, i+1)
node.SetSolutionStepValue(KM.PRESSURE, 0, NodeScalarHistValueCurrent(node_id))
node.SetSolutionStepValue(KM.DISPLACEMENT, 0, NodeVectorHistValueCurrent(node_id))
node.SetSolutionStepValue(KM.PRESSURE, 1, NodeScalarHistValuePrevious(node_id))
node.SetSolutionStepValue(KM.DISPLACEMENT, 1, NodeVectorHistValuePrevious(node_id))
node.SetValue(KM.TEMPERATURE, NodeScalarNonHistValue(node_id))
node.SetValue(KM.VELOCITY, NodeVectorNonHistValue(node_id))
for i in range(num_elems):
elem_id = i+1
elem = self.mp.CreateNewElement("Element2D2N", elem_id, [1,2], props)
elem.SetValue(KM.DENSITY, ElementScalarValue(elem_id))
elem.SetValue(KM.FORCE, ElementVectorValue(elem_id))
for i in range(num_conds):
cond_id = i+1
cond = self.mp.CreateNewCondition("LineCondition2D2N", cond_id, [1,2], props)
cond.SetValue(KM.YOUNG_MODULUS, ConditionScalarValue(cond_id))
cond.SetValue(KM.ROTATION, ConditionVectorValue(cond_id))
self.mp[KM.NODAL_MASS] = model_part_scalar_value
self.mp[KM.TORQUE] = model_part_vector_value
def test_basics(self):
settings_scal_hist = KM.Parameters("""{
"model_part_name" : "mp_4_test",
"variable_name" : "PRESSURE"
}""")
settings_vec_elem = KM.Parameters("""{
"model_part_name" : "mp_4_test",
"variable_name" : "DISPLACEMENT",
"location" : "element",
"dimension" : 2
}""")
coupling_data_scal = CouplingInterfaceData(settings_scal_hist, self.model)
coupling_data_vec = CouplingInterfaceData(settings_vec_elem, self.model)
self.assertEqual(coupling_data_scal.GetModelPart().Name, "mp_4_test")
self.assertEqual(coupling_data_vec.GetModelPart().Name, "mp_4_test")
self.assertFalse(coupling_data_scal.GetModelPart().IsDistributed())
self.assertFalse(coupling_data_vec.GetModelPart().IsDistributed())
self.assertEqual(coupling_data_scal.Size(), 5) # 5 nodes and scalar var
self.assertEqual(coupling_data_vec.Size(), 14) # 7 elements and vector var with dim==2
self.assertEqual(coupling_data_scal.GetBufferSize(), 2)
self.assertEqual(coupling_data_vec.GetBufferSize(), 1)
def test_printing(self):
settings = KM.Parameters("""{
"model_part_name" : "mp_4_test",
"variable_name" : "DISPLACEMENT",
"dimension" : 2
}""")
coupling_data = CouplingInterfaceData(settings, self.model)
self.assertMultiLineEqual(str(coupling_data), coupling_interface_data_str)
def test_unallowed_names(self):
settings = KM.Parameters("""{
"model_part_name" : "mp_4_test",
"variable_name" : "PRESSURE"
}""")
with self.assertRaisesRegex(Exception, 'The name cannot be empty, contain whitespaces or "."!'):
CouplingInterfaceData(settings, self.model, "")
with self.assertRaisesRegex(Exception, 'The name cannot be empty, contain whitespaces or "."!'):
CouplingInterfaceData(settings, self.model, "aaa.bbbb")
with self.assertRaisesRegex(Exception, 'The name cannot be empty, contain whitespaces or "."!'):
CouplingInterfaceData(settings, self.model, "aaa bbb")
def test_var_does_not_exist(self):
settings = KM.Parameters("""{
"model_part_name" : "mp_4_test",
"variable_name" : "var_that_hopefully_none_will_ever_create_otherwise_this_test_will_be_wrong"
}""")
with self.assertRaisesRegex(Exception, 'does not exist!'):
CouplingInterfaceData(settings, self.model)
def test_wrong_input_dim_scalar(self):
settings = KM.Parameters("""{
"model_part_name" : "mp_4_test",
"variable_name" : "PRESSURE",
"dimension" : 2
}""")
with self.assertRaisesRegex(Exception, '"dimension" cannot be specifed for scalar variables!'):
CouplingInterfaceData(settings, self.model)
def test_wrong_input_no_dim_vector(self):
settings = KM.Parameters("""{
"model_part_name" : "mp_4_test",
"variable_name" : "DISPLACEMENT"
}""")
with self.assertRaisesRegex(Exception, '"dimension" has to be specifed for vector variables!'):
CouplingInterfaceData(settings, self.model)
def test_wrong_input_variable_type(self):
settings = KM.Parameters("""{
"model_part_name" : "mp_4_test",
"variable_name" : "EXTERNAL_FORCES_VECTOR"
}""")
exp_error = 'The input for "variable" "EXTERNAL_FORCES_VECTOR" is of variable type "Vector" which is not allowed, only the following variable types are allowed:\nBool, Integer, Unsigned Integer, Double, Array'
with self.assertRaisesRegex(Exception, exp_error):
CouplingInterfaceData(settings, self.model)
def test_wrong_input_location(self):
settings = KM.Parameters("""{
"model_part_name" : "mp_4_test",
"variable_name" : "PRESSURE",
"location" : "dummy"
}""")
exp_error = '"dummy" is not allowed as "location", only the following options are possible:\nnode_historical, node_non_historical, element, condition, model_part'
with self.assertRaisesRegex(Exception, exp_error):
CouplingInterfaceData(settings, self.model)
def test_wrong_input_set_data(self):
settings = KM.Parameters("""{
"model_part_name" : "mp_4_test",
"variable_name" : "PRESSURE"
}""")
settings_model_part = KM.Parameters("""{
"model_part_name" : "mp_4_test",
"variable_name" : "PRESSURE",
"location" : "model_part"
}""")
coupling_data = CouplingInterfaceData(settings, self.model)
coupling_data_mp = CouplingInterfaceData(settings_model_part, self.model)
wrong_data = [1,2,3]
correct_data = [1,2,3,4,5]
with self.assertRaisesRegex(Exception, "The sizes of the data are not matching, got: 3, expected: 5"):
coupling_data.SetData(wrong_data)
with self.assertRaisesRegex(Exception, "The buffer-size is not large enough: current buffer size: 2 | requested solution_step_index: 3"):
coupling_data.SetData(correct_data, 2)
with self.assertRaisesRegex(Exception, "accessing data from previous steps is only possible with historical nodal data!"):
coupling_data_mp.SetData(correct_data, 2)
def test_wrong_input_dim_array(self):
settings = KM.Parameters("""{
"model_part_name" : "mp_4_test",
"variable_name" : "DISPLACEMENT",
"dimension" : 4
}""")
exp_error = '"dimension" can only be 1,2,3 when using variables of type "Array"'
with self.assertRaisesRegex(Exception, exp_error):
CouplingInterfaceData(settings, self.model)
def test_wrong_input_missing_solutionstepvar(self):
settings = KM.Parameters("""{
"model_part_name" : "mp_4_test",
"variable_name" : "FORCE",
"dimension" : 2
}""")
exp_error = '"FORCE" is missing as SolutionStepVariable in ModelPart "mp_4_test"'
with self.assertRaisesRegex(Exception, exp_error):
CouplingInterfaceData(settings, self.model)
def test_wrong_input_missing_solutionstepvar_component(self):
settings = KM.Parameters("""{
"model_part_name" : "mp_4_test",
"variable_name" : "FORCE_X"
}""")
exp_error = '"FORCE_X" is missing as SolutionStepVariable in ModelPart "mp_4_test"'
with self.assertRaisesRegex(Exception, exp_error):
CouplingInterfaceData(settings, self.model)
def test_wrong_input_missing_solutionstepvar_double(self):
settings = KM.Parameters("""{
"model_part_name" : "mp_4_test",
"variable_name" : "TEMPERATURE"
}""")
exp_error = '"TEMPERATURE" is missing as SolutionStepVariable in ModelPart "mp_4_test"'
with self.assertRaisesRegex(Exception, exp_error):
CouplingInterfaceData(settings, self.model)
def test_non_existing_model_part(self):
settings = KM.Parameters("""{
"model_part_name" : "something",
"variable_name" : "PRESSURE",
"location" : "node_non_historical"
}""")
with self.assertRaisesRegex(Exception, 'The ModelPart named : "something" was not found as root-ModelPart. The total input string was "something'):
CouplingInterfaceData(settings, self.model)
def test_GetHistoricalVariableDict(self):
settings = KM.Parameters("""{
"model_part_name" : "mp_4_test",
"variable_name" : "PRESSURE"
}""")
coupling_data = CouplingInterfaceData(settings, self.model)
exp_dict = {"mp_4_test" : KM.PRESSURE}
dict_res = coupling_data.GetHistoricalVariableDict()
self.assertEqual(len(exp_dict), len(dict_res))
self.assertEqual(exp_dict["mp_4_test"].Name(), dict_res["mp_4_test"].Name())
# this should not give anything since there are no historical variables in this case
settings_2 = KM.Parameters("""{
"model_part_name" : "mp_4_test",
"variable_name" : "PRESSURE",
"location" : "element"
}""")
coupling_data_2 = CouplingInterfaceData(settings_2, self.model)
exp_dict_2 = {}
dict_res_2 = coupling_data_2.GetHistoricalVariableDict()
self.assertEqual(len(exp_dict_2), len(dict_res_2))
def test_GetSetNodalHistoricalData(self):
settings_scal = KM.Parameters("""{
"model_part_name" : "mp_4_test",
"variable_name" : "PRESSURE"
}""")
settings_vec = KM.Parameters("""{
"model_part_name" : "mp_4_test",
"variable_name" : "DISPLACEMENT",
"dimension" : 2
}""")
coupling_data_scal = CouplingInterfaceData(settings_scal, self.model)
coupling_data_vec = CouplingInterfaceData(settings_vec, self.model)
# 1. check the initial values
exp_data_scal_cur = [NodeScalarHistValueCurrent(node.Id) for node in self.mp.Nodes]
exp_data_scal_prev = [NodeScalarHistValuePrevious(node.Id) for node in self.mp.Nodes]
exp_data_vec_cur = GetVectorValues(self.mp.Nodes, NodeVectorHistValueCurrent, 2)
exp_data_vec_prev = GetVectorValues(self.mp.Nodes, NodeVectorHistValuePrevious, 2)
self.__CheckData(exp_data_scal_cur, coupling_data_scal.GetData())
self.__CheckData(exp_data_vec_cur, coupling_data_vec.GetData())
self.__CheckData(exp_data_scal_prev, coupling_data_scal.GetData(1))
self.__CheckData(exp_data_vec_prev, coupling_data_vec.GetData(1))
# 2. check setting and getting works
set_data_scal_cur = [ElementScalarValue(node.Id) for node in self.mp.Nodes]
set_data_scal_prev = [ConditionScalarValue(node.Id) for node in self.mp.Nodes]
set_data_vec_cur = GetVectorValues(self.mp.Nodes, ElementVectorValue, 2)
set_data_vec_prev = GetVectorValues(self.mp.Nodes, ConditionVectorValue, 2)
self.__CheckSetGetData(set_data_scal_cur, coupling_data_scal)
self.__CheckSetGetData(set_data_vec_cur, coupling_data_vec)
self.__CheckSetGetData(set_data_scal_prev, coupling_data_scal, 1)
self.__CheckSetGetData(set_data_vec_prev, coupling_data_vec, 1)
def test_GetSetNodalNonHistoricalData(self):
settings_scal = KM.Parameters("""{
"model_part_name" : "mp_4_test",
"location" : "node_non_historical",
"variable_name" : "TEMPERATURE"
}""")
settings_vec = KM.Parameters("""{
"model_part_name" : "mp_4_test",
"variable_name" : "VELOCITY",
"location" : "node_non_historical",
"dimension" : 3
}""")
self.mp.ProcessInfo[KM.DOMAIN_SIZE] = 3
coupling_data_scal = CouplingInterfaceData(settings_scal, self.model)
coupling_data_vec = CouplingInterfaceData(settings_vec, self.model)
# 1. check the initial values
exp_data_scal = [NodeScalarNonHistValue(node.Id) for node in self.mp.Nodes]
exp_data_vec = GetVectorValues(self.mp.Nodes, NodeVectorNonHistValue, 3)
self.__CheckData(exp_data_scal, coupling_data_scal.GetData())
self.__CheckData(exp_data_vec, coupling_data_vec.GetData())
# 2. check setting and getting works
set_data_scal = [ConditionScalarValue(node.Id) for node in self.mp.Nodes]
set_data_vec = GetVectorValues(self.mp.Nodes, ConditionVectorValue, 3)
self.__CheckSetGetData(set_data_scal, coupling_data_scal)
self.__CheckSetGetData(set_data_vec, coupling_data_vec)
def test_GetSetElementalData(self):
settings_scal = KM.Parameters("""{
"model_part_name" : "mp_4_test",
"location" : "element",
"variable_name" : "DENSITY"
}""")
settings_vec = KM.Parameters("""{
"model_part_name" : "mp_4_test",
"variable_name" : "FORCE",
"location" : "element",
"dimension" : 2
}""")
coupling_data_scal = CouplingInterfaceData(settings_scal, self.model)
coupling_data_vec = CouplingInterfaceData(settings_vec, self.model)
# 1. check the initial values
exp_data_scal = [ElementScalarValue(elem.Id) for elem in self.mp.Elements]
exp_data_vec = GetVectorValues(self.mp.Elements, ElementVectorValue, 2)
self.__CheckData(exp_data_scal, coupling_data_scal.GetData())
self.__CheckData(exp_data_vec, coupling_data_vec.GetData())
# 2. check setting and getting works
set_data_scal = [NodeScalarNonHistValue(elem.Id) for elem in self.mp.Elements]
set_data_vec = GetVectorValues(self.mp.Elements, NodeVectorNonHistValue, 2)
self.__CheckSetGetData(set_data_scal, coupling_data_scal)
self.__CheckSetGetData(set_data_vec, coupling_data_vec)
def test_GetSetConditionalData(self):
settings_scal = KM.Parameters("""{
"model_part_name" : "mp_4_test",
"location" : "condition",
"variable_name" : "YOUNG_MODULUS"
}""")
settings_vec = KM.Parameters("""{
"model_part_name" : "mp_4_test",
"variable_name" : "ROTATION",
"location" : "condition",
"dimension" : 2
}""")
coupling_data_scal = CouplingInterfaceData(settings_scal, self.model)
coupling_data_vec = CouplingInterfaceData(settings_vec, self.model)
# 1. check the initial values
exp_data_scal = [ConditionScalarValue(cond.Id) for cond in self.mp.Conditions]
exp_data_vec = GetVectorValues(self.mp.Conditions, ConditionVectorValue, 2)
self.__CheckData(exp_data_scal, coupling_data_scal.GetData())
self.__CheckData(exp_data_vec, coupling_data_vec.GetData())
# 2. check setting and getting works
set_data_scal = [NodeScalarHistValuePrevious(cond.Id) for cond in self.mp.Conditions]
set_data_vec = GetVectorValues(self.mp.Conditions, NodeVectorHistValuePrevious, 2)
self.__CheckSetGetData(set_data_scal, coupling_data_scal)
self.__CheckSetGetData(set_data_vec, coupling_data_vec)
def test_GetSetModelPartData(self):
settings_scal = KM.Parameters("""{
"model_part_name" : "mp_4_test",
"location" : "model_part",
"variable_name" : "NODAL_MASS"
}""")
settings_vec = KM.Parameters("""{
"model_part_name" : "mp_4_test",
"variable_name" : "TORQUE",
"location" : "model_part",
"dimension" : 1
}""")
self.mp.ProcessInfo[KM.DOMAIN_SIZE] = 1
coupling_data_scal = CouplingInterfaceData(settings_scal, self.model)
coupling_data_vec = CouplingInterfaceData(settings_vec, self.model)
# 1. check the initial values
self.__CheckData([model_part_scalar_value], coupling_data_scal.GetData())
self.__CheckData([model_part_vector_value[0]], coupling_data_vec.GetData())
# 2. check setting and getting works
set_data_scal = [model_part_scalar_value_2]
set_data_vec = [model_part_vector_value_2[0]]
self.__CheckSetGetData(set_data_scal, coupling_data_scal)
self.__CheckSetGetData(set_data_vec, coupling_data_vec)
def __CheckData(self, exp_data, data):
self.assertEqual(len(exp_data), len(data))
for exp_val, val in zip(exp_data, data):
self.assertAlmostEqual(exp_val, val)
def __CheckSetGetData(self, the_data, coupling_data, solution_step_index=0):
# Checking to call fct differently
if solution_step_index > 0:
coupling_data.SetData(the_data, solution_step_index)
extracted_data = coupling_data.GetData(solution_step_index)
else:
coupling_data.SetData(the_data)
extracted_data = coupling_data.GetData()
self.__CheckData(the_data, extracted_data)
def GetVectorValues(container, fct_ptr, dim):
values = []
for entity in container:
vector_vals = fct_ptr(entity.Id)
for i in range(dim):
values.append(vector_vals[i])
return values
def NodeScalarHistValueCurrent(the_id):
return the_id*1.5
def NodeScalarHistValuePrevious(the_id):
return the_id*123.7
def NodeVectorHistValueCurrent(the_id):
return [the_id*1.1, the_id*1.1+2.5, the_id*2.3]
def NodeVectorHistValuePrevious(the_id):
return [the_id*1.4-0.4, the_id*10.4+2.5, the_id*5.3]
def NodeScalarNonHistValue(the_id):
return the_id*2.1+6.2
def NodeVectorNonHistValue(the_id):
return [the_id*14.7, the_id*19.2-10.5, the_id*303.9]
def ElementScalarValue(the_id):
return the_id*6.4+1.1
def ElementVectorValue(the_id):
return [the_id*11.7, the_id*12.2-120.5, the_id*3.9]
def ConditionScalarValue(the_id):
return the_id*14.5+15.8
def ConditionVectorValue(the_id):
return [the_id*65.7, the_id*12.1, the_id+3.9]
if __name__ == '__main__':
KratosUnittest.main()