Loops and logical tests in HWC language within Hyperview 2021

Christophe H
Christophe H New Altair Community Member
edited November 2022 in Community Q&A

Hello,

I've been using the new HWC language in Hyperview and find it very useful! Is there any logical statements (if/then/else) based on variables or loops (for/foreach/while) available in that language? Or are we required to use TCL and then invoke HWC commands there? I find TCL much harder to use. I'm looking for instance for a way to loop on all time steps or Eigen modes to create plots. Today, I do it by manually copying the same code multiple times and use variables to store filename prefix/sufix similar to this:

config reset
config variable savedir "D:/Workdir"
config variable sufx "_rh_disp"
#
config variable modenum "1"
result subcase "Vibration mode %{modenum}"
result simulation 1
save image window %{savedir}/Mode0%{modenum}%{sufx}.png
animate start
save animation page %{savedir}/Mode0%{modenum}%{sufx}.gif
#
config variable modenum "2"
...

 Thanks,

Christophe

Best Answer

  • StefanE_20536
    StefanE_20536
    Altair Employee
    edited October 2022 Answer ✓

    Thanks Ben. I guess that means there are no loop or control statements in HWC but that part should be handed over to TCL. What is then the level of communication between TCL and HWC? I've read this page from the documentation: https://2021.help.altair.com/2021.2/hwdesktop/hwd/topics/reference/hwc/hwc_tcl_r.htm

    Can I use the TCL loop counter as a variable in HWC, something like this:

    set eigenmodes {1 2 3 4 5}

    foreach modenum $eigenmode {
    hwc result subcase "Vibration mode %{modenum}"
    hwc save image window Mode0%{modenum}.png
    }

    Thanks
    Christophe

    Hi Christophe,

    you need to use Tcl for this. Please be aware the you should always use "Copy to Tcl" or "Log to Tcl" when you use HWC in Tcl so that the commands are correctly formatted for the Tcl parser.  

    I added a zip file "HWC_Loop_Example.zip" which you can run in HW Version 2022 or newer. The model is included, no paths needs to be changed.

    In 2022 we introduced the postquery commands which provide easy access to the HyperView Database. Just un-zip it and run Loop_and_Export.tcl.

    This script loops over components and simulations and exports images and h3d's labeled accordingly. I added "Screenshot_of_ExportDir_after_successful_run.PNG" which shows how the directory "exportDir" will look like after a successful run.

    if you have no access to 2022 I can adapt the script that it just uses the simulation id's, just let me know.

    Hope that helps,
    Stefan

     

     

Answers

  • Ben Buchanan
    Ben Buchanan
    Altair Employee
    edited October 2022

    Yes, tcl can do the looping.  There has been talk about Python being available but I don't think it is fully available yet.
    Here is the resource I use to help me with tcl:

    https://www.tcl.tk/man/tcl8.5/

    Here are some of the looping tutorials:

    https://www.tcl.tk/man/tcl8.5/tutorial/Tcl9.html
    https://www.tcl.tk/man/tcl8.5/tutorial/Tcl10.html

    My favorite loop in tcl though is the foreach loop:

    https://www.tcl.tk/man/tcl/TclCmd/foreach.html

    Hope this helps.  Happy coding!

  • Christophe H
    Christophe H New Altair Community Member
    edited October 2022

    Thanks Ben. I guess that means there are no loop or control statements in HWC but that part should be handed over to TCL. What is then the level of communication between TCL and HWC? I've read this page from the documentation: https://2021.help.altair.com/2021.2/hwdesktop/hwd/topics/reference/hwc/hwc_tcl_r.htm

    Can I use the TCL loop counter as a variable in HWC, something like this:

    set eigenmodes {1 2 3 4 5}

    foreach modenum $eigenmode {
    hwc result subcase "Vibration mode %{modenum}"
    hwc save image window Mode0%{modenum}.png
    }

    Thanks
    Christophe

  • Ben Buchanan
    Ben Buchanan
    Altair Employee
    edited October 2022

    Thanks Ben. I guess that means there are no loop or control statements in HWC but that part should be handed over to TCL. What is then the level of communication between TCL and HWC? I've read this page from the documentation: https://2021.help.altair.com/2021.2/hwdesktop/hwd/topics/reference/hwc/hwc_tcl_r.htm

    Can I use the TCL loop counter as a variable in HWC, something like this:

    set eigenmodes {1 2 3 4 5}

    foreach modenum $eigenmode {
    hwc result subcase "Vibration mode %{modenum}"
    hwc save image window Mode0%{modenum}.png
    }

    Thanks
    Christophe

    Yes, tcl and hwc can be combined seamlessly.  The variable would use the tcl structure ($). So it would look like this:

    set eigenmodes {1 2 3 4 5}  foreach modenum $eigenmode {    hwc result subcase "Vibration mode ${modenum}"    hwc save image window Mode0${modenum}.png }
  • StefanE_20536
    StefanE_20536
    Altair Employee
    edited October 2022 Answer ✓

    Thanks Ben. I guess that means there are no loop or control statements in HWC but that part should be handed over to TCL. What is then the level of communication between TCL and HWC? I've read this page from the documentation: https://2021.help.altair.com/2021.2/hwdesktop/hwd/topics/reference/hwc/hwc_tcl_r.htm

    Can I use the TCL loop counter as a variable in HWC, something like this:

    set eigenmodes {1 2 3 4 5}

    foreach modenum $eigenmode {
    hwc result subcase "Vibration mode %{modenum}"
    hwc save image window Mode0%{modenum}.png
    }

    Thanks
    Christophe

    Hi Christophe,

    you need to use Tcl for this. Please be aware the you should always use "Copy to Tcl" or "Log to Tcl" when you use HWC in Tcl so that the commands are correctly formatted for the Tcl parser.  

    I added a zip file "HWC_Loop_Example.zip" which you can run in HW Version 2022 or newer. The model is included, no paths needs to be changed.

    In 2022 we introduced the postquery commands which provide easy access to the HyperView Database. Just un-zip it and run Loop_and_Export.tcl.

    This script loops over components and simulations and exports images and h3d's labeled accordingly. I added "Screenshot_of_ExportDir_after_successful_run.PNG" which shows how the directory "exportDir" will look like after a successful run.

    if you have no access to 2022 I can adapt the script that it just uses the simulation id's, just let me know.

    Hope that helps,
    Stefan

     

     

  • Christophe H
    Christophe H New Altair Community Member
    edited October 2022

    Thank you Stefan for such a detailed answer, this is very helpful. My laptop is running Windows 11 so unfortunately I cannot upgrade to 2022. If it's not too much work to backport it to 2021, that would be very nice but if not, I'll try and figure it out ased on your example. Thanks again