*
    Stephan Blum, October 2007
    (C) DYNARDO Weimar 2007
    All rights reserved.

    10-bar truss example
    SLang subroutine to calculate the response
/

* For background information see "../../doc/tenBarTruss.pdf" /

control echo, off, , /
    * NODE DATA/

    node allocate, replace, 8, /
    node create, two_d, 1 720 360 0, /
    node create, two_d, 2 720 0 0, /
    node create, two_d, 3 360 360 0, /
    node create, two_d, 4 360 0 0, /
    node create, two_d, 5 0 360 0, /
    node create, two_d, 6 0 0 0, /
    node create, reference, 7 720 370 0, /
    node create, reference, 8 730 0 0, /

    group allocate, replace, 2, /
    group create, list nodes, 1 4
        1 2 3 4, /

    node modify, restraints, 5 2
        0 1 0 0
        0 0 1 0, /

    node modify, restraints, 6 2
        0 1 0 0
        0 0 1 0, /

    node modify, total available_dof, 1 1 0 0 0 0, /

    section allocate, replace, 10, /
    section create, truss, 1, /
    section create, truss, 2, /
    section create, truss, 3, /
    section create, truss, 4, /
    section create, truss, 5, /
    section create, truss, 6, /
    section create, truss, 7, /
    section create, truss, 8, /
    section create, truss, 9, /
    section create, truss, 10, /

    material allocate, replace, 1, /
    material create, linear_elastic, 1, first_material /
    object create, real vector replace, 3, material_table /
    object read, , material_table 1.000e+7 0.2000 0.1000, /
    material modify, material_data, first_material material_table, /

    * ELEMENT DATA/

    element allocate, replace, 10, /
    element create, truss2n, 1 5 3 7, /
    element create, truss2n, 2 3 1 7, /
    element create, truss2n, 3 6 4 7, /
    element create, truss2n, 4 4 2 7, /
    element create, truss2n, 5 3 4 8, /
    element create, truss2n, 6 1 2 8, /
    element create, truss2n, 7 5 4 7, /
    element create, truss2n, 8 3 6 8, /
    element create, truss2n, 9 3 2 7, /
    element create, truss2n, 10 1 4 8, /

    * CROSS SECTIONS /

    object create, replace real vector, 1, physical_table /
    object modify, set, physical_table 1  20.00000 , /
    section modify, physical_data, 1 physical_table, /
    object modify, set, physical_table 1  20.00000 , /
    section modify, physical_data, 2 physical_table, /
    object modify, set, physical_table 1  20.00000 , /
    section modify, physical_data, 3 physical_table, /
    object modify, set, physical_table 1  20.00000 , /
    section modify, physical_data, 4 physical_table, /
    object modify, set, physical_table 1  20.00000 , /
    section modify, physical_data, 5 physical_table, /
    object modify, set, physical_table 1  20.00000 , /
    section modify, physical_data, 6 physical_table, /
    object modify, set, physical_table 1  20.00000 , /
    section modify, physical_data, 7 physical_table, /
    object modify, set, physical_table 1  20.00000 , /
    section modify, physical_data, 8 physical_table, /
    object modify, set, physical_table 1  20.00000 , /
    section modify, physical_data, 9 physical_table, /
    object modify, set, physical_table 1  20.00000 , /
    section modify, physical_data, 10 physical_table, /


    element modify, material total, 1 , /


    element modify, section, 1 1, /
    element modify, section, 2 2, /
    element modify, section, 3 3, /
    element modify, section, 4 4, /
    element modify, section, 5 5, /
    element modify, section, 6 6, /
    element modify, section, 7 7, /
    element modify, section, 8 8, /
    element modify, section, 9 9, /
    element modify, section, 10 10, /


    * FE-Analysis /

    element build, stiffness total, , /
    global restraints, , , /
    global matrix, stiffness, , glob_k /
    compact factorize, replace, glob_k, sky_k /

    element build, mass total, , /

    load allocate, replace, 1, /
    load build, nodes list, 1 2
        4 0 -100000.00 0 0 0 0
        2 0 -100000.00 0 0 0 0, /
    global vector, load, 1 1 1, glob_l /

    compact solve, , sky_k glob_l, disp /
    node merge, displacements, disp, /

    stress allocate, replace, 1, /
    *stress create, all mises_stress, 1 1 truss2n, /
    stress create, all local stress_tensor, 1 1 truss2n, /
    stress build, total fem, 1, /


    * VARIABLE RUNTIME FOR PARALLELIZING /

    *object create, , , i /
    *#label SleepAgain
        object modify, add, i 1, /
        *control system, , "sleep 1", /
    *control if, less goto, i 10 SleepAgain, /


    * RESPONSES /

    * MASS /
    element mass, replace total, , tmp /
    object extract, row, tmp 1, mass /
    string append, replace, 1 "TOTAL MASS:", szTmp /
    output file, text plain string, szTmp, ten_bar_response.out /
    output file, append text plain  freeformat, mass "%16.6f", ten_bar_response.out /

    * DISPLACEMENT /
    object extract, row, disp 4, disp2 /
    object operate, \chs, disp2, /
    string append, replace, 1 "DISPLACEMENT NODE 2:", szTmp /
    output file, append text plain string, szTmp, ten_bar_response.out /
    output file, append text plain freeformat, disp2 "%16.6f", ten_bar_response.out /

    object extract, row, disp 8, disp4 /
    object operate, \chs, disp4, /
    string append, replace, 1 "DISPLACEMENT NODE 4:", szTmp /
    output file, append text plain string, szTmp, ten_bar_response.out /
    output file, append text plain freeformat, disp4 "%16.6f", ten_bar_response.out /

    * STRESS /
    stress extract, single fem, 1 1 1, stress1 /
    string append, replace, 1 "STRESS ELEMENT 1:", szTmp /
    output file, append text plain string, szTmp, ten_bar_response.out /
    output file, append text plain freeformat, stress1 "%16.6f", ten_bar_response.out /

    stress extract, single fem, 1 2 1, stress2 /
    string append, replace, 1 "STRESS ELEMENT 2:", szTmp /
    output file, append text plain string, szTmp, ten_bar_response.out /
    output file, append text plain freeformat, stress2 "%16.6f", ten_bar_response.out /

    stress extract, single fem, 1 3 1, stress3 /
    string append, replace, 1 "STRESS ELEMENT 3:", szTmp /
    output file, append text plain string, szTmp, ten_bar_response.out /
    output file, append text plain freeformat, stress3 "%16.6f", ten_bar_response.out /

    stress extract, single fem, 1 4 1, stress4 /
    string append, replace, 1 "STRESS ELEMENT 4:", szTmp /
    output file, append text plain string, szTmp, ten_bar_response.out /
    output file, append text plain freeformat, stress4 "%16.6f", ten_bar_response.out /

    stress extract, single fem, 1 5 1, stress5 /
    string append, replace, 1 "STRESS ELEMENT 5:", szTmp /
    output file, append text plain string, szTmp, ten_bar_response.out /
    output file, append text plain freeformat, stress5 "%16.6f", ten_bar_response.out /

    stress extract, single fem, 1 6 1, stress6 /
    string append, replace, 1 "STRESS ELEMENT 6:", szTmp /
    output file, append text plain string, szTmp, ten_bar_response.out /
    output file, append text plain freeformat, stress6 "%16.6f", ten_bar_response.out /

    stress extract, single fem, 1 7 1, stress7 /
    string append, replace, 1 "STRESS ELEMENT 7:", szTmp /
    output file, append text plain string, szTmp, ten_bar_response.out /
    output file, append text plain freeformat, stress7 "%16.6f", ten_bar_response.out /

    stress extract, single fem, 1 8 1, stress8 /
    string append, replace, 1 "STRESS ELEMENT 8:", szTmp /
    output file, append text plain string, szTmp, ten_bar_response.out /
    output file, append text plain freeformat, stress8 "%16.6f", ten_bar_response.out /

    stress extract, single fem, 1 9 1, stress9 /
    string append, replace, 1 "STRESS ELEMENT 9:", szTmp /
    output file, append text plain string, szTmp, ten_bar_response.out /
    output file, append text plain freeformat, stress9 "%16.6f", ten_bar_response.out /

    stress extract, single fem, 1 10 1, stress10 /
    string append, replace, 1 "STRESS ELEMENT 10:", szTmp /
    output file, append text plain string, szTmp, ten_bar_response.out /
    output file, append text plain freeformat, stress10 "%16.6f", ten_bar_response.out /

    * 2nd objective: max deflection of the 4 unconstrained bar intersections /
    node extract, replace group displacements trans_dof matrix, 1, node_disp /
    object extract, replace column, node_disp 2, tmp /
    object sort, ascending column, tmp, /
    object operate, \abs, tmp, /
    object extract, replace row, tmp 1, max_disp /
    string append, replace, 1 "MAXIMUM DISPLACEMENT", szTmp /
    output file, append text plain string, szTmp, ten_bar_response.out /
    output file, append text plain freeformat, max_disp  "%16.6f", ten_bar_response.out /

    * constraint response /
*   group create, list elements, 2 10 1 2 3 4 5 6 7 8 9 10, /
    group create, list elements, 2 6 1 3 4 7 8 9, /
    stress extract, replace maximum_stress group, 1 2 1 , maximum /
    stress extract, replace minimum_stress group, 1 2 1 , minimum /
    object operate, \abs, minimum, /
    object append, add_rows, maximum minimum, /
    object extract, replace maximum, maximum, max_stress  tmp1 /
    string append, replace, 1 "MAXIMUM STRESS", szTmp /
    output file, append text plain string, szTmp, ten_bar_response.out /
    output file, append text plain freeformat, max_stress  "%16.6f", ten_bar_response.out /


    monitor allocate, replace, 1, /
    monitor create, , 1 0.1 0.1 0.5 0.4, structure /
    *structure stress, deformed edges detailed precise, 1 0 0 0 10 1 1, /
    *structure image, deformed edges detailed precise, 1 0 0 0 10,/
    *monitor file, , 1, structure.eps  /

********** write success string randomly ***********************/
    * set mean1 0.0 - 1.0   [no succes - total succes] /
    #macro  mean1   1.0
    ranvar create, uniform, 1 mean1 \sqr \pow 12 -1, schreibnOderNet /
    ranvar simulate, replace, 1 schreibnOderNet, tmp /
    control if, real greater gosub, tmp(1) .5 WriteSuccessString, /

********** random produce error ***********************/
    * set mean2 0.0 - 1.0   [no error - total error] /
    #macro  mean2   0.0
    ranvar create, uniform, 2 mean2 \sqr \pow 12 -1, errorOderNet /
    ranvar simulate, replace, 1 errorOderNet, tmp /
    control if, real greater goto, tmp(1) .5 ProduceError, /
control exit,,,/


#label WriteSuccessString
    string append, replace, 1 "calculation completed successfully", szTmp /
    output file, plain text string append, szTmp, ten_bar_response.out /
    *output print, string, szTmp, /
control return,,,/


#label ProduceError
    control core, test, , /
control quit,,,/
