Debian Codegraph @

src/Mod/Fem/feminout/importCcxFrdResults.py

raw symbols SCIP
1 # ***************************************************************************
2 # * Copyright (c) 2013 Joachim Zettler *
3 # * Copyright (c) 2013 Juergen Riegel <[email protected]> *
4 # * Copyright (c) 2016 Bernd Hahnebach <[email protected]> *
5 # * *
6 # * This file is part of the FreeCAD CAx development system. *
7 # * *
8 # * This program is free software; you can redistribute it and/or modify *
9 # * it under the terms of the GNU Lesser General Public License (LGPL) *
10 # * as published by the Free Software Foundation; either version 2 of *
11 # * the License, or (at your option) any later version. *
12 # * for detail see the LICENCE text file. *
13 # * *
14 # * This program is distributed in the hope that it will be useful, *
15 # * but WITHOUT ANY WARRANTY; without even the implied warranty of *
16 # * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
17 # * GNU Library General Public License for more details. *
18 # * *
19 # * You should have received a copy of the GNU Library General Public *
20 # * License along with this program; if not, write to the Free Software *
21 # * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 *
22 # * USA *
23 # * *
24 # ***************************************************************************
25
26 __title__ = "Result import for Calculix frd file format"
27 __author__ = "Juergen Riegel , Michael Hindley, Bernd Hahnebach"
28 __url__ = "https://www.freecad.org"
29
30 ## @package importCcxFrdResults
31 # \ingroup FEM
32 # \brief FreeCAD Calculix FRD Reader for FEM workbench
33
34 import os
35 import math
36
37 import FreeCAD
38 from FreeCAD import Console
39 from builtins import open as pyopen
40
41
42 # ********* generic FreeCAD import and export methods *********
43
44
45 def open(filename):
46 "called when freecad opens a file"
47 docname = os.path.splitext(os.path.basename(filename))[0]
48 insert(filename, docname)
49
50
51 def insert(filename, docname):
52 "called when freecad wants to import a file"
53 try:
54 doc = FreeCAD.getDocument(docname)
55 except NameError:
56 doc = FreeCAD.newDocument(docname)
57 FreeCAD.ActiveDocument = doc
58 importFrd(filename)
59
60
61 # ********* module specific methods *********
62 def setupPipeline(doc, analysis, results_name, result_data):
63 import ObjectsFem
64 from . import importToolsFem
65
66 if not "BUILD_FEM_VTK" in FreeCAD.__cmake__:
67 return
68
69 # create a results pipeline (dependent on user settings)
70 pipeline_name = "Pipeline_" + results_name
71 pipelines = analysis.getObjectsOfType("Fem::FemPostPipeline")
72 fem_prefs = FreeCAD.ParamGet("User parameter:BaseApp/Preferences/Mod/Fem/General")
73 keep_results_on_rerun = fem_prefs.GetBool("KeepResultsOnReRun", False)
74
75 if not pipelines or keep_results_on_rerun:
76 # needs to create a new pipeline!
77 pipeline_obj = ObjectsFem.makePostVtkResult(doc, result_data, results_name)
78 pipeline_visibility = True
79 if analysis:
80 analysis.addObject(pipeline_obj)
81 else:
82 # by default get the last one
83 pipeline_obj = pipelines[-1]
84 # maybe there is one with the correct name
85 named_pipeline = analysis.getObject(pipeline_name)
86 if named_pipeline:
87 pipeline_obj = named_pipeline
88
89 if FreeCAD.GuiUp:
90 # store pipeline visibility because pipeline_obj.load makes the
91 # pipeline always visible
92 pipeline_visibility = pipeline_obj.ViewObject.Visibility
93
94 # relabel the pipeline and load the data into it
95 pipeline_obj.Label = pipeline_name
96 pipeline_obj.load(*result_data)
97
98 # update the pipeline
99 pipeline_obj.recomputeChildren()
100 pipeline_obj.recompute()
101 if FreeCAD.GuiUp:
102 pipeline_obj.ViewObject.updateColorBars()
103 # make results mesh invisible, will be made visible
104 # later in task_solver_ccxtools.py
105 if len(result_data) == 1:
106 result_data[0].Mesh.ViewObject.Visibility = False
107 else:
108 for res in result_data[0]:
109 res.Mesh.ViewObject.Visibility = False
110 # restore pipeline visibility
111 pipeline_obj.ViewObject.Visibility = pipeline_visibility
112
113
114 def importFrd(filename, analysis=None, result_name_prefix="", result_analysis_type=""):
115 import ObjectsFem
116 from . import importToolsFem
117
118 if analysis:
119 doc = analysis.Document
120 else:
121 doc = FreeCAD.ActiveDocument
122
123 m = read_frd_result(filename)
124 result_mesh_object = None
125 res_obj = None
126
127 if len(m["Nodes"]) > 0:
128 mesh = importToolsFem.make_femmesh(m)
129 res_mesh_is_compacted = False
130 nodenumbers_for_compacted_mesh = []
131
132 number_of_increments = len(m["Results"])
133 Console.PrintLog("Increments: " + str(number_of_increments) + "\n")
134
135 def make_result_mesh(result_name):
136 res_obj = ObjectsFem.makeResultMechanical(doc, results_name)
137 # create result mesh
138 result_mesh_object = ObjectsFem.makeMeshResult(doc, results_name + "_Mesh")
139 result_mesh_object.FemMesh = mesh
140 res_obj.Mesh = result_mesh_object
141 return res_obj
142
143 multistep_result = []
144 multistep_value = []
145 if len(m["Results"]) > 0:
146 for result_set in m["Results"]:
147 if "number" in result_set:
148 eigenmode_number = result_set["number"]
149 else:
150 eigenmode_number = 0
151 step_time = result_set["time"]
152 if not math.isfinite(step_time):
153 step_time = 0
154 step_time = round(step_time, 2)
155 if eigenmode_number > 0:
156 results_name = "{}EigenMode_{}_Results".format(
157 result_name_prefix, eigenmode_number
158 )
159 elif number_of_increments > 1:
160 if result_analysis_type == "buckling":
161 results_name = "{}BucklingFactor_{}_Results".format(
162 result_name_prefix, step_time
163 )
164 else:
165 results_name = f"{result_name_prefix}Time_{step_time}_Results"
166 else:
167 results_name = f"{result_name_prefix}Results"
168
169 res_obj = make_result_mesh(results_name)
170 res_obj = importToolsFem.fill_femresult_mechanical(res_obj, result_set)
171 if analysis:
172 # need to be here, becasause later on, the analysis objs are needed
173 # see fill of principal stresses
174 analysis.addObject(res_obj)
175
176 # more result object calculations
177 from femresult import resulttools
178 from femtools import femutils
179
180 if not res_obj.MassFlowRate:
181 # information 1:
182 # only compact result if not Flow 1D results
183 # compact result object, workaround for bug 2873
184 # https://www.freecad.org/tracker/view.php?id=2873
185 # information 2:
186 # if the result data has multiple result sets there will be multiple result objs
187 # they all will use one mesh obj
188 # on the first res obj fill: the mesh obj will be compacted, thus
189 # it does not need to be compacted on further result sets
190 # but NodeNumbers need to be compacted for every result set (res object fill)
191 # example frd file: https://forum.freecad.org/viewtopic.php?t=32649#p274291
192 if res_mesh_is_compacted is False:
193 # first result set, compact FemMesh and NodeNumbers
194 res_obj = resulttools.compact_result(res_obj)
195 res_mesh_is_compacted = True
196 nodenumbers_for_compacted_mesh = res_obj.NodeNumbers
197 else:
198 # all other result sets, do not compact FemMesh, only set NodeNumbers
199 res_obj.NodeNumbers = nodenumbers_for_compacted_mesh
200
201 # fill DisplacementLengths
202 res_obj = resulttools.add_disp_apps(res_obj)
203 # fill vonMises
204 res_obj = resulttools.add_von_mises(res_obj)
205 # fill principal stress
206 # if material reinforced object use add additional values to the res_obj
207 if res_obj.getParentGroup():
208 has_reinforced_mat = False
209 for obj in res_obj.getParentGroup().Group:
210 if femutils.is_of_type(obj, "Fem::MaterialReinforced"):
211 has_reinforced_mat = True
212 Console.PrintLog(
213 "Reinforced material object detected, "
214 "reinforced principal stresses and standard principal "
215 "stresses will be added.\n"
216 )
217 resulttools.add_principal_stress_reinforced(res_obj)
218 break
219 if has_reinforced_mat is False:
220 Console.PrintLog(
221 "No reinforced material object detected, "
222 "standard principal stresses will be added.\n"
223 )
224 # fill PrincipalMax, PrincipalMed, PrincipalMin, MaxShear
225 res_obj = resulttools.add_principal_stress_std(res_obj)
226 else:
227 Console.PrintLog(
228 "No Analysis detected, standard principal stresses will be added.\n"
229 )
230 # if a pure frd file was opened no analysis and thus no parent group
231 # fill PrincipalMax, PrincipalMed, PrincipalMin, MaxShear
232 res_obj = resulttools.add_principal_stress_std(res_obj)
233 # fill Stats
234 res_obj = resulttools.fill_femresult_stats(res_obj)
235
236 # if we have multiple results we delay the pipeline creation
237 if number_of_increments == 1:
238 setupPipeline(doc, analysis, results_name, [res_obj])
239 else:
240 multistep_value.append(step_time)
241 multistep_result.append(res_obj)
242
243 # we have collected all result objects, lets create the multistep result pipeline
244 if number_of_increments > 1:
245 # figure out type and unit
246 match result_analysis_type:
247 case "frequency":
248 unit = FreeCAD.Units.Frequency
249 description = "Eigenmode"
250 case "buckling":
251 unit = FreeCAD.Units.Unit()
252 description = "Buckling factor"
253 case "thermomech":
254 unit = FreeCAD.Units.TimeSpan
255 description = "Timesteps"
256 case "static":
257 unit = FreeCAD.Units.Unit()
258 description = "Load factor"
259 case _:
260 unit = FreeCAD.Units.Unit()
261 description = "Unknown"
262
263 setupPipeline(
264 doc,
265 analysis,
266 results_name,
267 [multistep_result, multistep_value, unit, description],
268 )
269
270 elif result_analysis_type == "check":
271 results_name = f"{result_name_prefix}Check"
272 res_obj = make_result_mesh(results_name)
273 setupPipeline(doc, analysis, results_name, [res_obj])
274 if analysis:
275 analysis.addObject(res_obj)
276
277 else:
278 error_message = (
279 "Nodes, but no results found in frd file. "
280 "It means there only is a mesh but no results in frd file. "
281 "Usually this happens for: \n"
282 "- if CalculiX returned no results "
283 "(happens on nonpositive jacobian determinant in at least one element)\n"
284 "- just no frd results where requestet in input file "
285 "(neither 'node file' nor 'el file' in output section')\n"
286 )
287 Console.PrintWarning(error_message)
288
289 # create a result obj, even if we have no results but a result mesh in frd file
290 # see error message above for more information
291 if not res_obj:
292 if result_name_prefix:
293 results_name = f"{result_name_prefix}_Results"
294 else:
295 results_name = "Results"
296 res_obj = ObjectsFem.makeResultMechanical(doc, results_name)
297 res_obj.Mesh = result_mesh_object
298 setupPipeline(doc, analysis, results_name, [res_obj])
299 # TODO, node numbers in result obj could be set
300 if analysis:
301 analysis.addObject(res_obj)
302
303 if FreeCAD.GuiUp:
304 if analysis:
305 import FemGui
306
307 FemGui.setActiveAnalysis(analysis)
308 doc.recompute()
309
310 else:
311 Console.PrintError("Problem on frd file import. No nodes found in frd file.\n")
312 # None will be returned
313 # or would it be better to raise an exception if there are not even nodes in frd file?
314
315 return res_obj
316
317
318 # read a calculix result file and extract the nodes
319 # displacement vectors and stress values.
320 def read_frd_result(frd_input):
321 Console.PrintMessage(f"Read ccx results from frd file: {frd_input}\n")
322 inout_nodes = []
323 inout_nodes_file = frd_input.rsplit(".", 1)[0] + "_inout_nodes.txt"
324 if os.path.exists(inout_nodes_file):
325 Console.PrintMessage(f"Read special 1DFlow nodes data form: {inout_nodes_file}\n")
326 f = pyopen(inout_nodes_file, "r")
327 lines = f.readlines()
328 for line in lines:
329 a = line.split(",")
330 inout_nodes.append(a)
331 f.close()
332 Console.PrintMessage(f"{inout_nodes}\n")
333 frd_file = pyopen(frd_input, "r")
334 nodes = {}
335 elements_hexa8 = {}
336 elements_penta6 = {}
337 elements_tetra4 = {}
338 elements_tetra10 = {}
339 elements_penta15 = {}
340 elements_hexa20 = {}
341 elements_tria3 = {}
342 elements_tria6 = {}
343 elements_quad4 = {}
344 elements_quad8 = {}
345 elements_seg2 = {}
346 elements_seg3 = {}
347 results = []
348 mode_results = {}
349 mode_results["number"] = float("NaN")
350 mode_results["time"] = float("NaN")
351 mode_disp = {}
352 mode_stress = {}
353 mode_strain = {}
354 mode_peeq = {}
355 mode_temp = {}
356 mode_heatflux = {}
357 mode_massflow = {}
358 mode_networkpressure = {}
359
360 nodes_found = False
361 elements_found = False
362 mode_time_found = False
363 mode_disp_found = False
364 mode_stress_found = False
365 mode_strain_found = False
366 mode_peeq_found = False
367 mode_temp_found = False
368 mode_heatflux_found = False
369 mode_massflow_found = False
370 mode_networkpressure_found = False
371 end_of_section_found = False
372 end_of_frd_data_found = False
373 input_continues = False
374 mode_eigen_changed = False
375 mode_time_changed = False
376
377 eigenmode = 0
378 eigentemp = 0
379 elem = -1
380 elemType = 0
381 timestep = 0
382 timetemp = 0
383
384 for line in frd_file:
385
386 # depending on c runtime lib and possibly locale calculix may format NAN differently so we
387 # need to sanitize the file
388 line = line.replace("NAN(IND)", "NAN")
389 # Check if we found nodes section
390 if line[4:6] == "2C":
391 nodes_found = True
392 if nodes_found and (line[1:3] == "-1"):
393 # we found a nodes line, lets extract the node and coordinate data
394 elem = int(line[4:13])
395 nodes_x = float(line[13:25])
396 nodes_y = float(line[25:37])
397 nodes_z = float(line[37:49])
398 nodes[elem] = FreeCAD.Vector(nodes_x, nodes_y, nodes_z)
399
400 # Check if we found elements section
401 if line[4:6] == "3C":
402 elements_found = True
403 if elements_found and (line[1:3] == "-1"):
404 # we found a first element line, lets extract element number
405 elem = int(line[4:13])
406 elemType = int(line[14:18])
407 if elements_found and (line[1:3] == "-2"):
408 # we found a second element line, lets extract the elements
409 # node order fits with node order in writeAbaqus() in FemMesh.cpp
410 if elemType == 1:
411 # C3D8 CalculiX --> hexa8 FreeCAD
412 # N6, N7, N8, N5, N2, N3, N4, N1
413 nd1 = int(line[3:13])
414 nd2 = int(line[13:23])
415 nd3 = int(line[23:33])
416 nd4 = int(line[33:43])
417 nd5 = int(line[43:53])
418 nd6 = int(line[53:63])
419 nd7 = int(line[63:73])
420 nd8 = int(line[73:83])
421 elements_hexa8[elem] = (nd6, nd7, nd8, nd5, nd2, nd3, nd4, nd1)
422 elif elemType == 2:
423 # C3D6 Calculix --> penta6 FreeCAD
424 # N5, N6, N4, N2, N3, N1
425 nd1 = int(line[3:13])
426 nd2 = int(line[13:23])
427 nd3 = int(line[23:33])
428 nd4 = int(line[33:43])
429 nd5 = int(line[43:53])
430 nd6 = int(line[53:63])
431 elements_penta6[elem] = (nd5, nd6, nd4, nd2, nd3, nd1)
432 elif elemType == 3:
433 # C3D4 Calculix --> tetra4 FreeCAD
434 # N2, N1, N3, N4
435 nd1 = int(line[3:13])
436 nd2 = int(line[13:23])
437 nd3 = int(line[23:33])
438 nd4 = int(line[33:43])
439 elements_tetra4[elem] = (nd2, nd1, nd3, nd4)
440 elif elemType == 4 and input_continues is False:
441 # first line
442 # C3D20 Calculix --> hexa20 FreeCAD
443 # N6, N7, N8, N5, N2, N3, N4, N1, N14, N15
444 # N16, N13, N10, N11, N12, N9, N18, N19, N20, N17
445 nd1 = int(line[3:13])
446 nd2 = int(line[13:23])
447 nd3 = int(line[23:33])
448 nd4 = int(line[33:43])
449 nd5 = int(line[43:53])
450 nd6 = int(line[53:63])
451 nd7 = int(line[63:73])
452 nd8 = int(line[73:83])
453 nd9 = int(line[83:93])
454 nd10 = int(line[93:103])
455 input_continues = True
456 elif elemType == 4 and input_continues is True:
457 # second line
458 nd11 = int(line[3:13])
459 nd12 = int(line[13:23])
460 nd13 = int(line[23:33])
461 nd14 = int(line[33:43])
462 nd15 = int(line[43:53])
463 nd16 = int(line[53:63])
464 nd17 = int(line[63:73])
465 nd18 = int(line[73:83])
466 nd19 = int(line[83:93])
467 nd20 = int(line[93:103])
468 input_continues = False
469 """
470 CalculiX uses a different node order in
471 input file *.inp and result file *.frd for hexa20 (C3D20)
472 according to Guido (the developer of ccx):
473 see note in the first line of cgx manual part element types
474 ccx (and thus the *.inp) follows the ABAQUS convention
475 documented in the ccx-documentation
476 cgx (and thus the *.frd) follows the FAM2 convention
477 documented in the cgx-documentation
478 FAM32 is from the company FEGS limited
479 maybe this company does not exist any more
480 elements_hexa20[elem] = (
481 nd6, nd7, nd8, nd5, nd2, nd3, nd4, nd1, nd14, nd15,
482 nd16, nd13, nd10, nd11, nd12, nd9, nd18, nd19, nd20, nd17
483 )
484 elements_hexa20[elem] = (
485 nd6, nd7, nd8, nd5, nd2, nd3, nd4, nd1, nd14, nd15,
486 nd16, nd13, nd18, nd19, nd20, nd17, nd10, nd11, nd12, nd9
487 )
488 hexa20 import works with the following frd file node assignment
489 """
490 elements_hexa20[elem] = (
491 nd8,
492 nd5,
493 nd6,
494 nd7,
495 nd4,
496 nd1,
497 nd2,
498 nd3,
499 nd20,
500 nd17,
501 nd18,
502 nd19,
503 nd12,
504 nd9,
505 nd10,
506 nd11,
507 nd16,
508 nd13,
509 nd14,
510 nd15,
511 )
512 elif elemType == 5 and input_continues is False:
513 # first line
514 # C3D15 Calculix --> penta15 FreeCAD
515 # N5, N6, N4, N2, N3, N1, N11, N12, N10, N8, N9, N7, N14, N15, N13
516 nd1 = int(line[3:13])
517 nd2 = int(line[13:23])
518 nd3 = int(line[23:33])
519 nd4 = int(line[33:43])
520 nd5 = int(line[43:53])
521 nd6 = int(line[53:63])
522 nd7 = int(line[63:73])
523 nd8 = int(line[73:83])
524 nd9 = int(line[83:93])
525 nd10 = int(line[93:103])
526 input_continues = True
527 elif elemType == 5 and input_continues is True:
528 # second line
529 nd11 = int(line[3:13])
530 nd12 = int(line[13:23])
531 nd13 = int(line[23:33])
532 nd14 = int(line[33:43])
533 nd15 = int(line[43:53])
534 input_continues = False
535 """
536 CalculiX uses a different node order in
537 input file *.inp and result file *.frd for penta15 (C3D15)
538 see notes at hexa20
539 elements_penta15[elem] = (
540 nd5, nd6, nd4, nd2, nd3, nd1, nd11, nd12, nd10, nd8,
541 nd9, nd7, nd14, nd15, nd13
542 ) # order of the *.inp file
543 """
544 elements_penta15[elem] = (
545 nd5,
546 nd6,
547 nd4,
548 nd2,
549 nd3,
550 nd1,
551 nd14,
552 nd15,
553 nd13,
554 nd8,
555 nd9,
556 nd7,
557 nd11,
558 nd12,
559 nd10,
560 )
561 elif elemType == 6:
562 # C3D10 Calculix --> tetra10 FreeCAD
563 # N2, N1, N3, N4, N5, N7, N6, N9, N8, N10
564 nd1 = int(line[3:13])
565 nd2 = int(line[13:23])
566 nd3 = int(line[23:33])
567 nd4 = int(line[33:43])
568 nd5 = int(line[43:53])
569 nd6 = int(line[53:63])
570 nd7 = int(line[63:73])
571 nd8 = int(line[73:83])
572 nd9 = int(line[83:93])
573 nd10 = int(line[93:103])
574 elements_tetra10[elem] = (nd2, nd1, nd3, nd4, nd5, nd7, nd6, nd9, nd8, nd10)
575 elif elemType == 7:
576 # S3 Calculix --> tria3 FreeCAD
577 # N1, N2, N3
578 nd1 = int(line[3:13])
579 nd2 = int(line[13:23])
580 nd3 = int(line[23:33])
581 elements_tria3[elem] = (nd1, nd2, nd3)
582 elif elemType == 8:
583 # S6 CalculiX --> tria6 FreeCAD
584 # N1, N2, N3, N4, N5, N6
585 nd1 = int(line[3:13])
586 nd2 = int(line[13:23])
587 nd3 = int(line[23:33])
588 nd4 = int(line[33:43])
589 nd5 = int(line[43:53])
590 nd6 = int(line[53:63])
591 elements_tria6[elem] = (nd1, nd2, nd3, nd4, nd5, nd6)
592 elif elemType == 9:
593 # S4 CalculiX --> quad4 FreeCAD
594 # N1, N2, N3, N4
595 nd1 = int(line[3:13])
596 nd2 = int(line[13:23])
597 nd3 = int(line[23:33])
598 nd4 = int(line[33:43])
599 elements_quad4[elem] = (nd1, nd2, nd3, nd4)
600 elif elemType == 10:
601 # S8 CalculiX --> quad8 FreeCAD
602 # N1, N2, N3, N4, N5, N6, N7, N8
603 nd1 = int(line[3:13])
604 nd2 = int(line[13:23])
605 nd3 = int(line[23:33])
606 nd4 = int(line[33:43])
607 nd5 = int(line[43:53])
608 nd6 = int(line[53:63])
609 nd7 = int(line[63:73])
610 nd8 = int(line[73:83])
611 elements_quad8[elem] = (nd1, nd2, nd3, nd4, nd5, nd6, nd7, nd8)
612 elif elemType == 11:
613 # B31 CalculiX --> seg2 FreeCAD
614 # N1, N2
615 nd1 = int(line[3:13])
616 nd2 = int(line[13:23])
617 elements_seg2[elem] = (nd1, nd2)
618 elif elemType == 12:
619 # B32 CalculiX --> seg3 FreeCAD
620 # Also D element element number
621 # CalculiX uses a different node order in
622 # input file *.inp and result file *.frd for seg3 (B32)
623 # see notes at hexa20
624 # N1, N2 ,N3
625 nd1 = int(line[3:13])
626 nd2 = int(line[13:23])
627 nd3 = int(line[23:33])
628 if inout_nodes:
629 for i in range(len(inout_nodes)):
630 if nd1 == int(inout_nodes[i][1]):
631 # fluid inlet node numbering
632 elements_seg3[elem] = (int(inout_nodes[i][2]), nd3, nd1)
633 elif nd3 == int(inout_nodes[i][1]):
634 # fluid outlet node numbering
635 elements_seg3[elem] = (nd1, int(inout_nodes[i][2]), nd3)
636 else:
637 # normal node numbering for D, B32 elements
638 elements_seg3[elem] = (nd1, nd2, nd3)
639
640 # Check if we found new eigenmode line
641 if line[5:10] == "PMODE":
642 eigentemp = int(line[30:36])
643 if eigentemp > eigenmode:
644 eigenmode = eigentemp
645 mode_eigen_changed = True
646
647 # Check if we found new time step
648 if line[4:10] == "1PSTEP":
649 mode_time_found = True
650 if mode_time_found and (line[2:7] == "100CL"):
651 # we found the new time step line
652 # !!! be careful here, there is timetemp and timestep!
653 # TODO: use more differ names
654 timetemp = float(line[13:25])
655 if timetemp > timestep:
656 timestep = timetemp
657 mode_time_changed = True
658
659 # Check if we found displacement section
660 if line[5:9] == "DISP":
661 mode_disp_found = True
662 if mode_disp_found and (line[1:3] == "-1"):
663 # we found a displacement line
664 elem = int(line[4:13])
665 mode_disp_x = float(line[13:25])
666 mode_disp_y = float(line[25:37])
667 mode_disp_z = float(line[37:49])
668 mode_disp[elem] = FreeCAD.Vector(mode_disp_x, mode_disp_y, mode_disp_z)
669
670 # Check if we found stress section
671 if line[5:11] == "STRESS":
672 mode_stress_found = True
673 if mode_stress_found and (line[1:3] == "-1"):
674 # we found a stress line
675 elem = int(line[4:13])
676 stress_1 = float(line[13:25])
677 stress_2 = float(line[25:37])
678 stress_3 = float(line[37:49])
679 stress_4 = float(line[49:61])
680 stress_5 = float(line[61:73])
681 stress_6 = float(line[73:85])
682 # CalculiX frd files: (Sxx, Syy, Szz, Sxy, Syz, Szx)
683 # FreeCAD: (Sxx, Syy, Szz, Sxy, Sxz, Syz)
684 # thus exchange the last two entries
685 mode_stress[elem] = (stress_1, stress_2, stress_3, stress_4, stress_6, stress_5)
686
687 # Check if we found strain section
688 if line[5:13] == "TOSTRAIN":
689 mode_strain_found = True
690 if mode_strain_found and (line[1:3] == "-1"):
691 # we found a strain line in the frd file
692 elem = int(line[4:13])
693 strain_1 = float(line[13:25])
694 strain_2 = float(line[25:37])
695 strain_3 = float(line[37:49])
696 strain_4 = float(line[49:61])
697 strain_5 = float(line[61:73])
698 strain_6 = float(line[73:85])
699 # CalculiX frd files: (Exx, Eyy, Ezz, Exy, Eyz, Ezx)
700 # FreeCAD: (Exx, Eyy, Ezz, Exy, Exz, Eyz)
701 # thus exchange the last two entries
702 mode_strain[elem] = (strain_1, strain_2, strain_3, strain_4, strain_6, strain_5)
703
704 # Check if we found an equivalent plastic strain section
705 if line[5:7] == "PE":
706 mode_peeq_found = True
707 if mode_peeq_found and (line[1:3] == "-1"):
708 # we found an equivalent plastic strain line
709 elem = int(line[4:13])
710 peeq = float(line[13:25])
711 mode_peeq[elem] = peeq
712
713 # Check if we found a temperature section
714 if line[5:11] == "NDTEMP":
715 mode_temp_found = True
716 if mode_temp_found and (line[1:3] == "-1"):
717 # we found a temperature line
718 elem = int(line[4:13])
719 temperature = float(line[13:25])
720 mode_temp[elem] = temperature
721
722 # Check if we found heat flux section
723 if line[5:9] == "FLUX":
724 mode_heatflux_found = True
725 if mode_heatflux_found and (line[1:3] == "-1"):
726 # we found a heat_flux line
727 elem = int(line[4:13])
728 mode_heatflux_x = float(line[13:25])
729 mode_heatflux_y = float(line[25:37])
730 mode_heatflux_z = float(line[37:49])
731 mode_heatflux[elem] = FreeCAD.Vector(mode_heatflux_x, mode_heatflux_y, mode_heatflux_z)
732
733 # Check if we found a mass flow section
734 if line[5:11] == "MAFLOW":
735 mode_massflow_found = True
736 if mode_massflow_found and (line[1:3] == "-1"):
737 # we found a mass flow line
738 elem = int(line[4:13])
739 massflow = float(line[13:25])
740 mode_massflow[elem] = massflow * 1000 # convert units to kg/s from t/s
741 if inout_nodes:
742 for i in range(len(inout_nodes)):
743 if elem == int(inout_nodes[i][1]):
744 node = int(inout_nodes[i][2])
745 # convert units to kg/s from t/s
746 mode_massflow[node] = massflow * 1000
747
748 # Check if we found a network pressure section
749 if line[5:11] == "STPRES":
750 mode_networkpressure_found = True
751 if mode_networkpressure_found and (line[1:3] == "-1"):
752 # we found a network pressure line
753 elem = int(line[4:13])
754 networkpressure = float(line[13:25])
755 mode_networkpressure[elem] = networkpressure
756 if inout_nodes:
757 for i in range(len(inout_nodes)):
758 if elem == int(inout_nodes[i][1]):
759 node = int(inout_nodes[i][2])
760 mode_networkpressure[node] = networkpressure
761
762 # Check if we found the end of a section
763 if line[1:3] == "-3":
764 end_of_section_found = True
765
766 if nodes_found:
767 nodes_found = False
768 node_element_section = True
769
770 if elements_found:
771 elements_found = False
772 node_element_section = True
773
774 if mode_disp_found:
775 mode_results["disp"] = mode_disp
776 mode_disp = {}
777 mode_disp_found = False
778 node_element_section = False
779
780 if mode_stress_found:
781 mode_results["stress"] = mode_stress
782 mode_stress = {}
783 mode_stress_found = False
784 node_element_section = False
785
786 if mode_strain_found:
787 mode_results["strain"] = mode_strain
788
789 mode_strain = {}
790 mode_strain_found = False
791 node_element_section = False
792
793 if mode_peeq_found:
794 mode_results["peeq"] = mode_peeq
795 mode_peeq = {}
796 mode_peeq_found = False
797 node_element_section = False
798
799 if mode_temp_found:
800 mode_results["temp"] = mode_temp
801 mode_temp = {}
802 mode_temp_found = False
803 node_element_section = False
804
805 if mode_heatflux_found:
806 mode_results["heatflux"] = mode_heatflux
807 mode_heatflux = {}
808 mode_heatflux_found = False
809 node_element_section = False
810
811 if mode_massflow_found:
812 mode_results["mflow"] = mode_massflow
813 mode_massflow = {}
814 mode_massflow_found = False
815 node_element_section = False
816
817 if mode_networkpressure_found:
818 mode_results["npressure"] = mode_networkpressure
819 mode_networkpressure_found = False
820 mode_networkpressure = {}
821 node_element_section = False
822
823 """
824 print("---- End of Section --> Mode_Results may be changed ----")
825 for key in sorted(mode_results):
826 if key is "number" or key is "time":
827 print(key + " --> " + str(mode_results[key]))
828 else:
829 print(key + " --> " + str(len(mode_results[key])))
830 print("----Mode_Results----\n")
831 """
832
833 # Check if we found the end of frd data
834 if line[1:5] == "9999":
835 end_of_frd_data_found = True
836
837 if (
838 (mode_eigen_changed or mode_time_changed or end_of_frd_data_found)
839 and end_of_section_found
840 and not node_element_section
841 ):
842
843 """
844 print("\n\n----Append mode_results to results")
845 print(line)
846 for key in sorted(mode_results):
847 if key is "number" or key is "time":
848 print(key + " --> " + str(mode_results[key]))
849 else:
850 print(key + " --> " + str(len(mode_results[key])))
851 print("----Append Mode_Results----\n")
852 """
853
854 # append mode_results to results and reset mode_result
855 results.append(mode_results)
856 mode_results = {}
857 # https://forum.freecad.org/viewtopic.php?f=18&t=32649&start=10#p274686
858 mode_results["number"] = float("NaN")
859 mode_results["time"] = float("NaN")
860 end_of_section_found = False
861
862 # on changed --> write changed values in mode_result
863 # will be the first to do on an empty mode_result
864 if mode_eigen_changed:
865 mode_results["number"] = eigenmode
866 mode_eigen_changed = False
867
868 if mode_time_changed:
869 mode_results["time"] = timestep
870 # mode_results["time"] = 0 # Don't return time if static # Why?
871 mode_time_found = False
872 mode_time_changed = False
873
874 # here we are in the indent of loop for every line in frd file
875 # do not add a print here :-)
876
877 # close frd file if loop over all lines is finished
878 frd_file.close()
879
880 """
881 # debug prints and checks with the read data
882 print("\n\n----RESULTS values begin----")
883 print(len(results))
884 # print("\n")
885 # print(results)
886 print("----RESULTS values end----\n\n")
887 """
888
889 if not inout_nodes:
890 if results:
891 if "mflow" in results[0] or "npressure" in results[0]:
892 Console.PrintError("We have mflow or npressure, but no inout_nodes file.\n")
893 if not nodes:
894 Console.PrintError("FEM: No nodes found in Frd file.\n")
895
896 return {
897 "Nodes": nodes,
898 "Seg2Elem": elements_seg2,
899 "Seg3Elem": elements_seg3,
900 "Tria3Elem": elements_tria3,
901 "Tria6Elem": elements_tria6,
902 "Quad4Elem": elements_quad4,
903 "Quad8Elem": elements_quad8,
904 "Tetra4Elem": elements_tetra4,
905 "Tetra10Elem": elements_tetra10,
906 "Hexa8Elem": elements_hexa8,
907 "Hexa20Elem": elements_hexa20,
908 "Penta6Elem": elements_penta6,
909 "Penta15Elem": elements_penta15,
910 "Results": results,
911 }