Skip to content

Commit e002a95

Browse files
Merge pull request #667 from KratosMultiphysics/feature-optimization-mdpa
Feature optimization mdpa
2 parents 827cfec + 2f2ba3c commit e002a95

10 files changed

Lines changed: 103 additions & 23 deletions

File tree

kratos.gid/apps/Structural/write/write.tcl

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -228,6 +228,9 @@ proc Structural::write::writeLocalAxes { } {
228228

229229
# This is the kind of code I hate
230230
proc Structural::write::writeHinges { } {
231+
232+
# format for writing ids
233+
set id_f [dict get $write::formats_dict ID]
231234

232235
# Preprocess old_conditions. Each mesh linear element remembers the origin line in geometry
233236
set match_dict [dict create]
@@ -282,11 +285,11 @@ proc Structural::write::writeHinges { } {
282285
set end [::tcl::mathfunc::max {*}$linear_elements]
283286
if {[llength $first_list] > 0} {
284287
set value [join $first_list ,]
285-
write::WriteString [format "%5d \[%d\] (%s)" $first [llength $first_list] $value]
288+
write::WriteString [format "$id_f \[%d\] (%s)" $first [llength $first_list] $value]
286289
}
287290
if {[llength $last_list] > 0} {
288291
set value [join $last_list ,]
289-
write::WriteString [format "%5d \[%d\] (%s)" $end [llength $last_list] $value]
292+
write::WriteString [format "$id_f \[%d\] (%s)" $end [llength $last_list] $value]
290293
}
291294
}
292295
# Write the tail

kratos.gid/kratos.tcl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ proc Kratos::RegisterGiDEvents { } {
8484
proc Kratos::Event_InitProblemtype { dir } {
8585
variable kratos_private
8686

87-
# Init Kratos problemtype global vars
87+
# Init Kratos problemtype global vars with default values
8888
Kratos::InitGlobalVariables $dir
8989

9090
# Load all common tcl files (not the app ones)
@@ -102,7 +102,7 @@ proc Kratos::Event_InitProblemtype { dir } {
102102
# Problemtype libraries as CustomLib
103103
Kratos::LoadProblemtypeLibraries
104104

105-
# Load the user environment (stored preferences)
105+
# Load the Kratos problemtype global and user environment (stored preferences)
106106
Kratos::LoadEnvironment
107107

108108
# Customize GiD menus to add the Kratos entry

kratos.gid/scripts/Controllers/Preferences.xml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,10 @@
44
<option value='dev' label='Developer'/>
55
<option value='release' label='Release'/>
66
</combobox>
7+
<combobox name='mdpa_format' variable='mdpa_format' variablemanager='Kratos::ManagePreferences' label='MDPA Optimization' help=''>
8+
<option value='0' label='Optimized'/>
9+
<option value='1' label='Readable'/>
10+
</combobox>
711
<combobox name='echo_level' variable='echo_level' variablemanager='Kratos::ManagePreferences' label='Echo level' help='Show more/less message'>
812
<option value='0' label='0'/>
913
<option value='1' label='1'/>

kratos.gid/scripts/Controllers/PreferencesWindow.tcl

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,20 +3,27 @@ proc Kratos::ManagePreferences { cmd name {value ""}} {
33
set ret ""
44
switch $cmd {
55
"GetValue" {
6-
set ret $::Kratos::kratos_private($name)
6+
if {[info exists ::Kratos::kratos_private($name)]} {
7+
set ret $::Kratos::kratos_private($name)
8+
} else {
9+
set ret [Kratos::ManagePreferences GetDefaultValue $name]
10+
}
711
}
812
"SetValue" {
913
set ::Kratos::kratos_private($name) $value
1014
}
1115
"GetDefaultValue" {
1216
# same as GetValue
1317
switch $name {
14-
"DevMode" {
15-
set ret "release"
16-
}
17-
"echo_level" {
18-
set ret 0
19-
}
18+
"DevMode" {
19+
set ret "release"
20+
}
21+
"echo_level" {
22+
set ret 0
23+
}
24+
"mdpa_format" {
25+
set ret 1
26+
}
2027
}
2128
}
2229
}

kratos.gid/scripts/Writing/WriteConditions.tcl

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,9 @@ proc write::writeGroupCondition {groupid kname nnodes iter} {
5656

5757
# Get the entities to print
5858
if {$nnodes == 1} {
59-
set formats [dict create $groupid "%10d \n"]
59+
variable formats_dict
60+
set id_f [dict get $formats_dict ID]
61+
set formats [dict create $groupid "${s}$id_f \n"]
6062
set obj [GiD_EntitiesGroups get $groupid nodes]
6163
} else {
6264
set formats [write::GetFormatDict $groupid 0 $nnodes]

kratos.gid/scripts/Writing/WriteConditionsByGiDId.tcl

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,9 @@ proc write::writeGroupConditionByGiDId {groupid kname nnodes} {
4747

4848
# Get the entities to print
4949
if {$nnodes == 1} {
50-
set formats [dict create $groupid "${s}%10d \n"]
50+
variable formats_dict
51+
set id_f [dict get $formats_dict ID]
52+
set formats [dict create $groupid "${s}$id_f \n"]
5153
GiD_WriteCalculationFile nodes $formats
5254
} else {
5355
set formats [write::GetFormatDict $groupid 0 $nnodes]
@@ -64,6 +66,9 @@ proc write::writeGroupConditionByGiDId {groupid kname nnodes} {
6466
# what can be: nodal, Elements, Conditions or Elements&Conditions
6567
proc write::writeGroupSubModelPartByGiDId { cid group {what "Elements"} {tableid_list ""} } {
6668
variable submodelparts
69+
variable formats_dict
70+
71+
set id_f [dict get $formats_dict ID]
6772

6873
set mid ""
6974
set what [split $what "&"]
@@ -81,7 +86,7 @@ proc write::writeGroupSubModelPartByGiDId { cid group {what "Elements"} {tableid
8186
incr ::write::current_mdpa_indent_level 2
8287
set s2 [mdpaIndent]
8388
set gdict [dict create]
84-
set f "${s2}%5i\n"
89+
set f "${s2}$id_f\n"
8590
set f [subst $f]
8691
dict set gdict $group $f
8792
incr ::write::current_mdpa_indent_level -2

kratos.gid/scripts/Writing/WriteConditionsByUniqueId.tcl

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,9 @@ proc write::writeGroupConditionByUniqueId {groupid kname nnodes iter ConditionMa
8585

8686
# Get the entities to print
8787
if {$nnodes == 1} {
88-
set formats [dict create $groupid "%10d \n"]
88+
variable formats_dict
89+
set id_f [dict get $formats_dict ID]
90+
set formats [dict create $groupid "${s}$id_f \n"]
8991
set obj [GiD_EntitiesGroups get $groupid nodes]
9092
} else {
9193
set formats [write::GetFormatDict $groupid 0 $nnodes]
@@ -152,6 +154,9 @@ proc write::writeConditionGroupedSubmodelPartsByUniqueId {cid groups_dict condit
152154
# what can be: nodal, Elements, Conditions or Elements&Conditions
153155
proc write::writeGroupSubModelPartByUniqueId { cid group ConditionsMap {what "Elements"} {tableid_list ""} } {
154156
variable submodelparts
157+
variable formats_dict
158+
159+
set id_f [dict get $formats_dict ID]
155160

156161
set mid ""
157162
set what [split $what "&"]
@@ -169,7 +174,7 @@ proc write::writeGroupSubModelPartByUniqueId { cid group ConditionsMap {what "El
169174
incr ::write::current_mdpa_indent_level 2
170175
set s2 [mdpaIndent]
171176
set gdict [dict create]
172-
set f "${s2}%5i\n"
177+
set f "${s2}$id_f\n"
173178
set f [subst $f]
174179
dict set gdict $group $f
175180
incr ::write::current_mdpa_indent_level -2
@@ -199,7 +204,7 @@ proc write::writeGroupSubModelPartByUniqueId { cid group ConditionsMap {what "El
199204
set elems [GiD_WriteCalculationFile elements -sorted -return $gdict]
200205
for {set i 0} {$i <[llength $elems]} {incr i} {
201206
set eid [objarray get $ConditionsMap [lindex $elems $i]]
202-
WriteString "${s2}[format %10d $eid]"
207+
WriteString "${s2}[format $id_f $eid]"
203208
}
204209
}
205210
WriteString "${s1}End SubModelPartConditions"

kratos.gid/scripts/Writing/WriteNodes.tcl

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,19 @@
11

22
proc write::writeNodalCoordinatesOnGroups { groups } {
3+
# Write the nodal coordinates block for a list of groups
4+
# Nodes block format
5+
# Begin Nodes
6+
# // id X Y Z
7+
# End Nodes
8+
variable formats_dict
9+
set id_f [dict get $formats_dict ID]
10+
set coord_f [dict get $formats_dict COORDINATE]
311
set formats [dict create]
412
set s [mdpaIndent]
513
WriteString "${s}Begin Nodes"
614
incr ::write::current_mdpa_indent_level
715
foreach group $groups {
8-
dict set formats $group "${s}%5d %14.5f %14.5f %14.5f\n"
16+
dict set formats $group "${s}$id_f $coord_f $coord_f $coord_f\n"
917
}
1018
GiD_WriteCalculationFile nodes $formats
1119
incr ::write::current_mdpa_indent_level -1
@@ -23,10 +31,13 @@ proc write::writeNodalCoordinates { } {
2331
# Begin Nodes
2432
# // id X Y Z
2533
# End Nodes
34+
variable formats_dict
35+
set id_f [dict get $formats_dict ID]
36+
set coord_f [dict get $formats_dict COORDINATE]
2637
set s [mdpaIndent]
2738
WriteString "${s}Begin Nodes"
2839
incr ::write::current_mdpa_indent_level
29-
customlib::WriteCoordinates "${s}%5d %14.10f %14.10f %14.10f\n"
40+
customlib::WriteCoordinates "${s}$id_f $coord_f $coord_f $coord_f\n"
3041
incr ::write::current_mdpa_indent_level -1
3142
WriteString "${s}End Nodes"
3243
WriteString "\n"

kratos.gid/scripts/Writing/WriteSubModelPart.tcl

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,9 @@
22
# what can be: nodal, Elements, Conditions or Elements&Conditions
33
proc write::writeGroupSubModelPart { cid group {what "Elements"} {iniend ""} {tableid_list ""} } {
44
variable submodelparts
5+
variable formats_dict
56

7+
set id_f [dict get $formats_dict ID]
68
set mid ""
79
set what [split $what "&"]
810
set group [GetWriteGroupName $group]
@@ -19,7 +21,7 @@ proc write::writeGroupSubModelPart { cid group {what "Elements"} {iniend ""} {ta
1921
incr ::write::current_mdpa_indent_level 2
2022
set s2 [mdpaIndent]
2123
set gdict [dict create]
22-
set f "${s2}%5i\n"
24+
set f "${s2}$id_f\n"
2325
set f [subst $f]
2426
dict set gdict $group $f
2527
incr ::write::current_mdpa_indent_level -2
@@ -51,7 +53,7 @@ proc write::writeGroupSubModelPart { cid group {what "Elements"} {iniend ""} {ta
5153
#W $iniend
5254
foreach {ini end} $iniend {
5355
for {set i $ini} {$i<=$end} {incr i} {
54-
WriteString "${s2}[format %5d $i]"
56+
WriteString "${s2}[format $id_f $i]"
5557
}
5658
}
5759
}

kratos.gid/scripts/Writing/Writing.tcl

Lines changed: 43 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ namespace eval write {
1010
variable MDPA_loop_control
1111
variable current_configuration
1212
variable current_mdpa_indent_level
13+
variable formats_dict
1314
}
1415

1516
proc write::Init { } {
@@ -33,6 +34,10 @@ proc write::Init { } {
3334
set MDPA_loop_control 0
3435

3536
set current_mdpa_indent_level 0
37+
38+
39+
variable formats_dict
40+
set formats_dict [dict create]
3641
}
3742

3843
proc write::initWriteConfiguration {configuration} {
@@ -92,6 +97,11 @@ proc write::writeEvent { filename } {
9297
return 1
9398
}
9499
if {$time_monitor} {set inittime [clock seconds]}
100+
101+
# Set write formats depending on the user's configuration
102+
InitWriteFormats
103+
104+
# Current active app
95105
set activeapp [::apps::getActiveApp]
96106
set appid [::apps::getActiveAppId]
97107

@@ -246,8 +256,12 @@ proc write::transformGroupName {groupid} {
246256

247257
# Warning! Indentation must be set before calling here!
248258
proc write::GetFormatDict { groupid mid num} {
259+
variable formats_dict
260+
set id_f [dict get $formats_dict ID]
261+
set mid_f [dict get $formats_dict MAT_ID]
262+
249263
set s [mdpaIndent]
250-
set f "${s}%5d [format "%10d" $mid] [string repeat "%10d " $num]\n"
264+
set f "${s}$id_f [format $mid_f $mid] [string repeat "$id_f " $num]\n"
251265
return [dict create $groupid $f]
252266
}
253267

@@ -398,6 +412,9 @@ proc write::writePartSubModelPart { } {
398412
}
399413

400414
proc write::writeLinearLocalAxesGroup {group} {
415+
variable formats_dict
416+
set id_f [dict get $formats_dict ID]
417+
set coord_f [dict get $formats_dict COORDINATE]
401418
set elements [GiD_EntitiesGroups get $group elements -element_type linear]
402419
set num_elements [objarray length $elements]
403420
if {$num_elements} {
@@ -408,7 +425,7 @@ proc write::writeLinearLocalAxesGroup {group} {
408425
set y0 [lindex $raw 1]
409426
set y1 [lindex $raw 4]
410427
set y2 [lindex $raw 7]
411-
write::WriteString [format "%5d \[3\](%14.10f, %14.10f, %14.10f)" $line $y0 $y1 $y2]
428+
write::WriteString [format "$id_f \[3\]($coord_f, $coord_f, $coord_f)" $line $y0 $y1 $y2]
412429
}
413430
write::WriteString "End ElementalData"
414431
write::WriteString ""
@@ -577,6 +594,30 @@ proc write::mdpaIndent { {b 4} } {
577594
string repeat [string repeat " " $b] $current_mdpa_indent_level
578595
}
579596

597+
# Sets the precission for the diffetent entities written in the mdpa
598+
# To customize the formats and precissions for your mdpa.
599+
# You can edit in your write mdpa event script using write::SetWriteFormatFor
600+
proc write::InitWriteFormats { } {
601+
if {$::Kratos::kratos_private(mdpa_format) == 1} {
602+
# Readable
603+
write::SetWriteFormatFor ID "%5d"
604+
write::SetWriteFormatFor CONNECTIVITY "%10d"
605+
write::SetWriteFormatFor MAT_ID "%10d"
606+
write::SetWriteFormatFor COORDINATE "%14.10f"
607+
} else {
608+
# Optimized
609+
write::SetWriteFormatFor ID "%d"
610+
write::SetWriteFormatFor CONNECTIVITY "%d"
611+
write::SetWriteFormatFor MAT_ID "%d"
612+
write::SetWriteFormatFor COORDINATE "%.10f"
613+
}
614+
}
615+
616+
proc write::SetWriteFormatFor { what format } {
617+
variable formats_dict
618+
dict set formats_dict $what $format
619+
}
620+
580621
proc write::CopyFileIntoModel { filepath } {
581622
set dir [GetConfigurationAttribute dir]
582623
set activeapp [::apps::getActiveApp]

0 commit comments

Comments
 (0)