Difficulties implementing a problemtype using the customLib

Moderator: GiD Team

Post Reply
fabiopf
Posts: 9
Joined: Tue Jun 08, 2021 8:12 pm

Difficulties implementing a problemtype using the customLib

Post by fabiopf »

I am writing the problemtype for our solver using the new system (customLib). I have managed the implementation of the tree by reading the customisation manual and analysing the cmas2d_customlib.gid example provided by GiD. However, I still have a lot of basic questions that I could not find answers in the manual. For example, I want to write the elements’ connectivities to the solver input file. Reading the manual I could write the following code:

Code: Select all

    set condition_name [list "parts_1d" "parts_2d" "parts_3d"]
    set element_formats [list {"%10d" "element" "id"} {"%10d" "element" "connectivities"}] 
    customlib::WriteConnectivities $condition_name $element_formats
However, when I have different elements (beam, shells, etc) in the same model it writes the element’s connectivities not ordered as below:

Code: Select all

        87          1          2 
        88          2          3 
        89          3          4 
        90          4          5 
        91          5          6 
        92          6          7 
        93          7          8 
        94          8         12 
        95         12         19 
        96         19         25 
        97         25         32 
        98         32         39 
        99         39         46 
       100         46         53 
       101         53         60 
         1         69         65         70 
         2         15          9         10 
         3         62         68         64 
         4         64         68         72 
         5         62         64         57 
       ...
The solver requires element’s connectivities numbered sequentially from 1 to 101. How can I do that? Also, does anyone have any suggestion to learn more about implementation of problemtypes using the customLib as the customisation manual does not cover much?
User avatar
mrpeyrau
Posts: 4
Joined: Fri Oct 23, 2020 5:22 pm

Re: Difficulties implementing a problemtype using the customLib

Post by mrpeyrau »

Dear Fabio,

As an expert problem type developer, I suggest you to use the powerful command called GiD_WriteCalculationFile for advanced features instead of the reduced procedure called customlib::WriteConnectivities. See GiD reference manual, 'Help->PROBLEMTYPES SYSTEM->Writing the input file for calculation' for details about the GiD_WriteCalculationFile command.

For instance, I have edited the cmas2d_customlib.spd file, in the problemtype cmas2d_customlib.gid for adding a new condition applied on lines, called 'Beam', as follows:

<condition n="beam" pn="Beam" ov="line" ovm="element" icon="darkorange-weight-18" groups_icon="yelowish-group" help="Concentrated mass">
<value n="Weight" pn="Weight" v="0.0" unit_magnitude="M" units="kg" help="Specify the weight that you want to apply"/>
<symbol proc="gid_groups_conds::draw_symbol_image weight-18.png" orientation="global"/>
</condition>

I have also modified the procedure Cmas2d::WriteCalculationFile, to write connectivities for both shells and beams (triangle elements and linear elements) into the input file for the calculation, calling GiD_WriteCalculationFile in the following way:

proc Cmas2d::WriteCalculationFile { filename } {

customlib::InitWriteFile $filename

set xp1 {container[@n="Properties"]/condition[@n="Shells"]/group}
set xp2 ".//value\[@n='material']"

# We will save a TCL dictionary with the information requested (materials & beam information)
set mat_dict ""
set material_number 0
set formats ""

set root [$::gid_groups_conds::doc documentElement]
foreach gNode [$root selectNodes $xp1] {
set valueNode [$gNode selectNodes $xp2]
set material_name [get_domnode_attribute $valueNode v]
if { ![dict exists $mat_dict $material_name] } {
incr material_number
set mid $material_number

#We also save the density of the material by checking the material database
set xp3 {/*/container[@n="Properties"]/container[@n="materials"]//}
append xp3 [format_xpath {blockdata[@n="material" and @name=%s]/value[@n="Density"]} $material_name]
set valueNode [$root selectNodes $xp3]
# All the introduced values are translated to 'm' and 'kg' with the help of this function
set density [gid_groups_conds::convert_value_to_default $valueNode]

dict set mat_dict $material_name MID $material_number
dict set mat_dict $material_name DENSITY [format "%13.5E" $density]
} else {
set mid [dict get $mat_dict $material_name MID]
}
set f {\n%10d %10d %10d %10d [format "%10d" $mid]}
set f [subst $f]
dict set formats [$gNode @n] $f
}

if {$material_number=="0"} {
error [= "Remember to define your surface/s as shell/s"]
}

# Beams applied on lines
set xp3 {condition[@n="beam"]/group}
foreach gNode [$root selectNodes $xp3] {
set valueNode [$gNode selectNodes {value[@n="Weight"]}]
set weight [gid_groups_conds::convert_value_to_default $valueNode]
set f {\n%10d %10d %10d $weight}
set f [subst $f]
dict set formats [$gNode @n] $f
}
if {[GiD_WriteCalculationFile has_elements -elemtype {"Triangle" "Linear"} $formats]} {
GiD_WriteCalculationFile connectivities -do_subst -elemtype {"Triangle" "Linear"} $formats
} else {
error [= "Only Triangle and Linear elements are allowed in this version of the problemtype"]
}
customlib::EndWriteFile ;#finish writting
}

I hope this example helps you to deal with the use of GiD_WriteCalculationFile command to write information into the input file for calculation.

Kind regards,

Maria Rosa Peyrau
Maria Rosa Peyrau Rubio
Dept. Pre and Postprocessing CIMNE
User avatar
escolano
Posts: 1915
Joined: Sun Sep 05, 1982 10:51 pm

Re: Difficulties implementing a problemtype using the customLib

Post by escolano »

You said:
The solver requires element’s connectivities numbered sequentially from 1 to 101
I don't know what this mean.
Is this numeration possible in a generic mesh (unstructured triangles+tetrahedra, etc) ?

In any case: If do you need in the calculation file a very specific sorting of the elements you cannot directly use a function like
GiD_WriteCalculationFile connectivities (or customlib::WriteConnectivities that is a simplified proc version)
These functions do in implicit loop on the mesh, and allow a limited range of possibilities (covering most common cases)
They are made for efficiency, for meshes of millions of elements. They do the loop at C++ leval and using the native inner mesh data, without require create big arrays of numbers or call millions of callback functions.

In your case do you need to get the mesh information with Tcl funcions like
GiD_Info Mesh
or others (see Help on GiD customization manual : TCL AND TK EXTENSION https://www.gidhome.com/support/gid-manuals)

reorder the information as you need with Tcl scripting (be careful to not waste too much resources in auxiliary arrays, lists, objarray, ...) and then print the exact data you want in the calculation file

you can print for exampe an arbitrary string to the calculation file with
GiD_WriteCalculationFile puts $string
(note the the id of the file to be printed is not provided, it is printed to the file previously opened by a call to GiD_WriteCalculationFile init ...)
or with GiD_File fprintf, e.g.
GiD_File fprintf $file_id "x=%15.5f y=%15.5f" $x $y
(in this case it require file_id that is the file identifier returned by GiD_WriteCalculationFile init)

You can also open the file you want and print inside with standard Tcl command (open, puts, close,...)
fabiopf
Posts: 9
Joined: Tue Jun 08, 2021 8:12 pm

Re: Difficulties implementing a problemtype using the customLib

Post by fabiopf »

Thanks guys for your help. Maybe I did not explain well what I meant with the sentence:
The solver requires element’s connectivities numbered sequentially from 1 to 101
When I used the command WriteConnectivities, as the code below:

Code: Select all

    set condition_name [list "parts_1d" "parts_2d" "parts_3d"]
    set element_formats [list {"%10d" "element" "id"} {"%10d" "element" "connectivities"}] 
    customlib::WriteConnectivities $condition_name $element_formats
The id of the element were not sorted, as you can see in my previous post. The first printed element was the element id 87, and I would was expecting the elements connectivies printed from the lowest id to the highest id. Anyway, I think this is happening because I have defined a list starting with lines elements (parts_id), then surfaces and volumes.

But I believe Maria's example will help me. Thanks!
User avatar
escolano
Posts: 1915
Joined: Sun Sep 05, 1982 10:51 pm

Re: Difficulties implementing a problemtype using the customLib

Post by escolano »

As you said, with your command you are printing the elements grouped by the condition name: "parts_1d" "parts_2d" "parts_3d" (that in fact is related by its name to a GiD group of entities)
then elements are sorted by its group (and inside each group by element id)

If you don't need to relate the element with any condition property or material, you can also use the command "GiD_WriteCalculationFile all_connectivities"

Code: Select all

GiD_WriteCalculationFile all_connectivities -elemtype Linear "id: %d connectivities: %d %d"
GiD_WriteCalculationFile all_connectivities -elemtype Triangle "id: %d connectivities: %d %d %d"
GiD_WriteCalculationFile all_connectivities -elemtype Tetrahedra "id: %d connectivities: %d %d %d %d"
But also the elements won't be printed all sorted by element id
I think that is is not possible to print all your elements with a single command like this (but I am not completely sure of this)
GiD_WriteCalculationFile all_connectivities
because I think that the format argument is compulsory and the amount of connectivities is different for each type of element

As I have said, in case of really need a special printing sorting, you must firts ask the GiD data, modify as you want and then print what you want, with something like this (assuming that the groups with names "parts_1d" "parts_2d" "parts_3d" exists)

Code: Select all

proc muy_print_sorted_decreasing { } {
  #store in an array my_data the connectivities of each element, by its id
  foreach group_name  { "parts_1d" "parts_2d" "parts_3d" } {
    set elements_group [GiD_EntitiesGroups get $group_name  elements]
    foreach id $elements_group {
       set element_connectivities [GiD_Mesh get element $id connectivities]
       set my_data($id) $element_connectivities
    }
  }

  foreach id [lsort -integer -decreasing [array names my_data]] {
    GiD_WriteCalculationFile puts "$i $my_data($id)"
  }
}

muy_print_sorted_decreasing
fabiopf
Posts: 9
Joined: Tue Jun 08, 2021 8:12 pm

Re: Difficulties implementing a problemtype using the customLib

Post by fabiopf »

Thanks guys for your help.
Post Reply