-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMainKratos.py
245 lines (171 loc) · 8.02 KB
/
MainKratos.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
from __future__ import print_function, absolute_import, division #makes KratosMultiphysics backward compatible with python 2.6 and 2.7
#### TIME MONITORING START ####
# time control starts
import time as timer
print(timer.ctime())
# measure process time
t0p = timer.clock()
# measure wall time
t0w = timer.time()
#
def StartTimeMeasuring():
# measure process time
time_ip = timer.clock()
return time_ip
def StopTimeMeasuring(time_ip, process, report):
# measure process time
time_fp = timer.clock()
if( report ):
used_time = time_fp - time_ip
print("::[KSM Simulation]:: [ %.2f" % round(used_time,2),"s", process," ] ")
#### TIME MONITORING END ####
#import kratos core and applications
from KratosMultiphysics import *
from KratosMultiphysics.SolidMechanicsApplication import *
from KratosMultiphysics.StructuralMechanicsApplication import *
from KratosMultiphysics.ExternalSolversApplication import *
######################################################################################
######################################################################################
######################################################################################
#### PARSING THE PARAMETERS ####
#import define_output
parameter_file = open("ProjectParameters.json",'r')
ProjectParameters = Parameters( parameter_file.read())
#set echo level
echo_level = ProjectParameters["problem_data"]["echo_level"].GetInt()
#### model_part settings start ####
#defining the model_part
main_model_part = ModelPart(ProjectParameters["problem_data"]["model_part_name"].GetString())
main_model_part.ProcessInfo.SetValue(DOMAIN_SIZE, ProjectParameters["problem_data"]["domain_size"].GetInt())
###TODO replace this "model" for real one once available in kratos core
Model = {ProjectParameters["problem_data"]["model_part_name"].GetString() : main_model_part}
#construct the solver (main setting methods are located in the solver_module)
solver_module = __import__(ProjectParameters["solver_settings"]["solver_type"].GetString())
solver = solver_module.CreateSolver(main_model_part, ProjectParameters["solver_settings"])
#add variables (always before importing the model part) (it must be integrated in the ImportModelPart)
# if we integrate it in the model part we cannot use combined solvers
solver.AddVariables()
#read model_part (note: the buffer_size is set here) (restart can be read here)
solver.ImportModelPart()
#add dofs (always after importing the model part) (it must be integrated in the ImportModelPart)
# if we integrate it in the model part we cannot use combined solvers
solver.AddDofs()
#build sub_model_parts or submeshes (rearrange parts for the application of custom processes)
##TODO: replace MODEL for the Kratos one ASAP
##get the list of the submodel part in the object Model
for i in range(ProjectParameters["solver_settings"]["processes_sub_model_part_list"].size()):
part_name = ProjectParameters["solver_settings"]["processes_sub_model_part_list"][i].GetString()
Model.update({part_name: main_model_part.GetSubModelPart(part_name)})
#print model_part and properties
if(echo_level>1):
print("")
print(main_model_part)
for properties in main_model_part.Properties:
print(properties)
#### model_part settings end ####
#### processes settings start ####
#obtain the list of the processes to be applied
import process_factory
list_of_processes = process_factory.KratosProcessFactory(Model).ConstructListOfProcesses( ProjectParameters["constraints_process_list"] )
list_of_processes += process_factory.KratosProcessFactory(Model).ConstructListOfProcesses( ProjectParameters["loads_process_list"] )
#list_of_processes = []
#process_definition = ProjectParameters["boundary_conditions_process_list"]
#for i in range(process_definition.size()):
# item = process_definition[i]
# module = __import__(item["implemented_in_module"].GetString())
# interface_file = __import__(item["implemented_in_file"].GetString())
# p = interface_file.Factory(item, Model)
# list_of_processes.append( p )
# print("done ",i)
#print list of constructed processes
if(echo_level>1):
for process in list_of_processes:
print(process)
#TODO: decide which is the correct place to initialize the processes
for process in list_of_processes:
process.ExecuteInitialize()
#### processes settings end ####
#### START SOLUTION ####
#TODO: think if there is a better way to do this
computing_model_part = solver.GetComputeModelPart()
#### output settings start ####
problem_path = os.getcwd()
problem_name = ProjectParameters["problem_data"]["problem_name"].GetString()
# initialize GiD I/O (gid outputs, file_lists)
from gid_output_process import GiDOutputProcess
output_settings = ProjectParameters["output_configuration"]
gid_output = GiDOutputProcess(computing_model_part,
problem_name,
output_settings)
gid_output.ExecuteInitialize()
# restart write included in gid IO ??
#### output settings end ####
## Sets strategies, builders, linear solvers, schemes and solving info, and fills the buffer
solver.Initialize()
print(" ")
print("::[KSM Simulation]:: Analysis -START- ")
for process in list_of_processes:
process.ExecuteBeforeSolutionLoop()
## Set results when are written in a single file
gid_output.ExecuteBeforeSolutionLoop()
## Stepping and time settings (get from process info or solving info)
#delta time
delta_time = ProjectParameters["problem_data"]["time_step"].GetDouble()
#start step
step = 0
#start time
time = ProjectParameters["problem_data"]["start_time"].GetDouble()
#end time
end_time = ProjectParameters["problem_data"]["end_time"].GetDouble()
# monitoring info: # must be contained in the solver
#import solving_info_utility as solving_info_utils
#solving_info = solving_info_utils.SolvingInfoUtility(model_part)
# writing a initial state results file (if no restart)
# gid_io.write_results(time, computing_model_part) done in ExecuteBeforeSolutionLoop()
# solving the problem (time integration)
while(time <= end_time):
#TODO: this must be done by a solving_info utility in the solver
# store previous time step
#~ computing_model_part.ProcessInfo[PREVIOUS_DELTA_TIME] = delta_time
# set new time step ( it can change when solve is called )
#~ delta_time = computing_model_part.ProcessInfo[DELTA_TIME]
time = time + delta_time
step = step + 1
main_model_part.CloneTimeStep(time)
# print process info
##
for process in list_of_processes:
process.ExecuteInitializeSolutionStep()
gid_output.ExecuteInitializeSolutionStep()
solver.Solve()
for process in list_of_processes:
process.ExecuteFinalizeSolutionStep()
gid_output.ExecuteFinalizeSolutionStep()
#TODO: decide if it shall be done only when output is processed or not (boundary_conditions_processes ??)
for process in list_of_processes:
process.ExecuteBeforeOutputStep()
# write results and restart files: (frequency writing is controlled internally by the gid_io)
if gid_output.IsOutputStep():
gid_output.PrintOutput()
#TODO: decide if it shall be done only when output is processed or not
for process in list_of_processes:
process.ExecuteAfterOutputStep()
for process in list_of_processes:
process.ExecuteFinalize()
# ending the problem (time integration finished)
gid_output.ExecuteFinalize()
print("::[KSM Simulation]:: Analysis -END- ")
print(" ")
# check solving information for any problem
#~ solver.InfoCheck() # InfoCheck not implemented yet.
#### END SOLUTION ####
# measure process time
tfp = timer.clock()
# measure wall time
tfw = timer.time()
print("::[KSM Simulation]:: [ Computing Time = (%.2f" % (tfp - t0p)," seconds process time) ( %.2f" % (tfw - t0w)," seconds wall time) ]")
print(timer.ctime())
# to create a benchmark: add standard benchmark files and decomment next two lines
# rename the file to: run_test.py
#from run_test_benchmark_results import *
#WriteBenchmarkResults(model_part)