-
Notifications
You must be signed in to change notification settings - Fork 248
/
Copy pathtest_coupling_operations.py
206 lines (149 loc) · 7.92 KB
/
test_coupling_operations.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
import KratosMultiphysics as KM
import KratosMultiphysics.KratosUnittest as KratosUnittest
from KratosMultiphysics.CoSimulationApplication.coupling_interface_data import CouplingInterfaceData
from KratosMultiphysics.CoSimulationApplication.factories import coupling_operation_factory
from testing_utilities import DummySolverWrapper
from math import sqrt, pi
class TestScalingOperation(KratosUnittest.TestCase):
def setUp(self):
self.model = KM.Model()
self.model_part = self.model.CreateModelPart("default")
self.model_part.AddNodalSolutionStepVariable(KM.PRESSURE)
self.model_part.ProcessInfo[KM.TIME] = 0.0
self.model_part.ProcessInfo[KM.STEP] = 0
for i in range(5):
new_node = self.model_part.CreateNewNode(i+1, i*0.1, 0.0, 0.0)
new_node.SetSolutionStepValue(KM.PRESSURE, 0, i+1.3)
data_settings = KM.Parameters("""{
"model_part_name" : "default",
"variable_name" : "PRESSURE"
}""")
self.interface_data = CouplingInterfaceData(data_settings, self.model)
self.solver_wrappers = {"dummy_solver" : DummySolverWrapper({"data_4_testing" : self.interface_data})}
self.solver_process_info = KM.ProcessInfo()
def test_constant_scaling(self):
scaling_op_settings = KM.Parameters("""{
"type" : "scaling",
"solver" : "dummy_solver",
"data_name" : "data_4_testing",
"scaling_factor" : 1.5,
"echo_level" : 0
}""")
scaling_op = coupling_operation_factory.CreateCouplingOperation(scaling_op_settings, self.solver_wrappers, self.solver_process_info, KM.Testing.GetDefaultDataCommunicator())
factors = [1.5] * 3
self.__ExecuteTest(scaling_op, factors)
def test_constant_scaling_from_string(self):
scaling_op_settings = KM.Parameters("""{
"type" : "scaling",
"solver" : "dummy_solver",
"data_name" : "data_4_testing",
"scaling_factor" : "1.5",
"echo_level" : 0
}""")
scaling_op = coupling_operation_factory.CreateCouplingOperation(scaling_op_settings, self.solver_wrappers, self.solver_process_info, KM.Testing.GetDefaultDataCommunicator())
factors = [1.5] * 3
self.__ExecuteTest(scaling_op, factors)
def test_variable_scaling_time(self):
scaling_op_settings = KM.Parameters("""{
"type" : "scaling",
"solver" : "dummy_solver",
"data_name" : "data_4_testing",
"scaling_factor" : "1.5*t",
"echo_level" : 0
}""")
scaling_op = coupling_operation_factory.CreateCouplingOperation(scaling_op_settings, self.solver_wrappers, self.solver_process_info, KM.Testing.GetDefaultDataCommunicator())
factors = [1.5*0.25, 1.5*0.5, 1.5*0.75]
self.__ExecuteTest(scaling_op, factors)
def test_variable_scaling_step(self):
scaling_op_settings = KM.Parameters("""{
"type" : "scaling",
"solver" : "dummy_solver",
"data_name" : "data_4_testing",
"scaling_factor" : "1.5*sqrt(step)*pi",
"echo_level" : 0
}""")
scaling_op = coupling_operation_factory.CreateCouplingOperation(scaling_op_settings, self.solver_wrappers, self.solver_process_info, KM.Testing.GetDefaultDataCommunicator())
factors = [1.5*pi*sqrt(1), 1.5*pi*sqrt(2), 1.5*pi*sqrt(3), 1.5*pi*sqrt(4), 1.5*pi*sqrt(5)]
self.__ExecuteTest(scaling_op, factors)
def test_scaling_in_interval(self):
scaling_op_settings = KM.Parameters("""{
"type" : "scaling",
"solver" : "dummy_solver",
"data_name" : "data_4_testing",
"scaling_factor" : 1.22,
"interval" : [0.0, 0.3]
}""")
scaling_op = coupling_operation_factory.CreateCouplingOperation(scaling_op_settings, self.solver_wrappers, self.solver_process_info, KM.Testing.GetDefaultDataCommunicator())
factors = [1.0] * 5
factors[0] = 1.22
self.__ExecuteTest(scaling_op, factors)
def test_scaling_in_interval_2(self):
scaling_op_settings = KM.Parameters("""{
"type" : "scaling",
"solver" : "dummy_solver",
"data_name" : "data_4_testing",
"scaling_factor" : 1.22,
"interval" : [0.8, "End"]
}""")
scaling_op = coupling_operation_factory.CreateCouplingOperation(scaling_op_settings, self.solver_wrappers, self.solver_process_info, KM.Testing.GetDefaultDataCommunicator())
factors = [1.0] * 3
factors.extend([1.22] * 3)
self.__ExecuteTest(scaling_op, factors)
def __ExecuteTest(self, scaling_operation, factors):
scaling_operation.Check()
for fac in factors:
self.model_part.ProcessInfo[KM.TIME] += 0.25
self.model_part.ProcessInfo[KM.STEP] += 1
old_data = self.interface_data.GetData()
scaling_operation.Execute()
new_data = self.interface_data.GetData()
self.__CompareValues(old_data, new_data, fac)
def __CompareValues(self, old_data, new_data, factor):
for old_val, new_val in zip(old_data, new_data):
self.assertAlmostEqual(old_val*factor, new_val)
class TestConversionOperation(KratosUnittest.TestCase):
def test_elemental_to_nodal_conversion(self):
self.model = KM.Model()
self.model_part = self.model.CreateModelPart("default")
self.model_part.AddNodalSolutionStepVariable(KM.FORCE)
self.model_part.ProcessInfo[KM.DOMAIN_SIZE] = 3
props = self.model_part.CreateNewProperties(1)
self.model_part.CreateNewNode(1, 0.0, 0.0, 0.0)
self.model_part.CreateNewNode(2, 1.0, 0.0, 0.0)
self.model_part.CreateNewNode(3, 1.0, 1.0, 0.0)
self.model_part.CreateNewNode(4, 0.0, 1.0, 0.0)
new_element = self.model_part.CreateNewElement("Element2D4N", 1, [1,2,3,4], props)
new_element.SetValue(KM.FORCE, [12.0, 8.0, 0.0])
self.model_part.CreateNewNode(5, 2.0, 0.0, 0.0)
self.model_part.CreateNewNode(6, 2.0, 1.0, 0.0)
new_element = self.model_part.CreateNewElement("Element2D4N", 2, [2,5,6,3], props)
new_element.SetValue(KM.FORCE, [16.0, 4.0, 0.0])
elemental_data = KM.Parameters("""{
"model_part_name" : "default",
"location" : "element",
"variable_name" : "FORCE",
"dimension" : 3
}""")
self.interface_data = CouplingInterfaceData(elemental_data, self.model)
self.solver_wrappers = {"dummy_solver" : DummySolverWrapper({"elemental_data" : self.interface_data})}
self.solver_process_info = KM.ProcessInfo()
conversion_op_settings = KM.Parameters("""{
"type" : "elemental_data_to_nodal_data",
"solver" : "dummy_solver",
"data_name" : "elemental_data",
"echo_level" : 0
}""")
conversion_operation = coupling_operation_factory.CreateCouplingOperation(conversion_op_settings, self.solver_wrappers, self.solver_process_info, KM.Testing.GetDefaultDataCommunicator())
conversion_operation.Check()
conversion_operation.Execute()
nodal_data_output_setting = KM.Parameters("""{
"model_part_name" : "default",
"variable_name" : "FORCE",
"location" : "node_historical",
"dimension" : 3
}""")
nodal_data_output = CouplingInterfaceData(nodal_data_output_setting, self.model)
expected_nodal_values = [3, 2 ,0, 7, 3, 0, 7, 3 ,0, 3, 2 ,0, 4, 1 ,0, 4, 1 ,0 ]
self.assertVectorAlmostEqual(expected_nodal_values, nodal_data_output.GetData())
if __name__ == '__main__':
KratosUnittest.main()