Showing posts with label CAE automation. Show all posts
Showing posts with label CAE automation. Show all posts

Tuesday, June 8, 2021

TCL Programming Basic Practice Exercises /Examples 2

🧠 TCL Programming Basic Practice Exercises / Examples (Part 2)

Hello friends! 👋
Welcome once again!

In this post, we’ll explore a few basic TCL programming exercises that will help you build logic, strengthen your scripting skills, and gain confidence in automation coding.


🔹 1. Find the Number of Vowels in a String

set a "Rushikesh Patil" set b 0 set c [string length $a] for {set i 0} {$i < $c} {incr i} { set d [string index $a $i] if {$d == "a" || $d == "e" || $d == "i" || $d == "o" || $d == "u"} { incr b } } puts "The number of vowels are = $b"

🔹 2. Random Number Output

for {set i 0} {$i < 100} {incr i} { set random_number [expr int(rand() * 10)] puts $random_number }

🔹 3. Factorial Program (With and Without Recursion)

➤ Without Recursion

proc fact {n} { set f 1 while {$n >= 2} { set f [expr {$f * $n}] incr n -1 } return $f }

➤ With Recursion

proc recfact {n} { if {$n <= 1} { return 1 } expr {$n * [recfact [expr {$n - 1}]]} } puts "Factorial without recursion = [fact 4]" puts "Factorial with recursion = [recfact 4]"

🔹 4. Fibonacci Series

set fib0 0 set fib1 0 set fib2 1 set s "" for {set i 0} {$i < 10} {incr i} { set fib3 [expr {$fib1 + $fib2}] set fib1 $fib2 set fib2 $fib3 append s "$fib1, " } puts "$fib0, $s" proc fib {n} { return [expr {$n < 2 ? $n : [fib [expr {$n - 1]]] + [fib [expr {$n - 2]]}}] }

🔹 5. Count Letters in a String

set str "LIHAKHDBLICIHJAADFDCSDBBBDFDB" set l [string length $str] puts $l set cnt_A 0 set cnt_B 0 set cnt_C 0 set i 0 while {$i <= $l} { if {"A" == [string index $str $i]} { incr cnt_A } elseif {"B" == [string index $str $i]} { incr cnt_B } elseif {"C" == [string index $str $i]} { incr cnt_C } incr i } puts "The count of A is $cnt_A" puts "The count of B is $cnt_B" puts "The count of C is $cnt_C"

🌟 Summary

These simple TCL programs help you understand the fundamentals of loops, conditions, recursion, and string operations.
Practicing such examples builds your confidence and prepares you for automation scripting in CAE and engineering workflows.

Thank you for reading! 🙏
Keep practicing and stay tuned for more exciting TCL examples in upcoming posts.

Sunday, November 15, 2020

ANSA Scripting Examples For Beginners | Ansa Scripting 2

ANSA Scripting Examples For Beginners

Ansa scripting | Hello friends, once again I come up with some more basic ansa scripts which will be helpful for you to understand ansa scripting logic. I hope, you already go through my past post where I have mentioned their script. without wastage of time let's start. 


Basic ANSA script No. 4


If you want to highlight or delete the face which is under 5 mm area or as per your specified values of the area.

Might be this question has generated in your mind, but why? Why we should use this script?

Sometimes while we going through shell meshing or plastic meshing. first, we have to generate mid surface & due to complexity, it generates small faces. These faces are too tiny. human eyes unable to detect it or work with it properly. 

This work is tedious & time-consuming. This issue has frequently seen in complex plastic meshing. 

At the same, you can use the following script to resolve or save time & reduce complexity in the work panel temporarily.

The script is available in pink color :

import ansa 
from ansa import *

total_faces=base.CollectEntities(constants.LSDYNA, None, "FACE", False, True)

for each_face in total_faces:
area=base.GetFaceArea(each_face)
small_faces=[]
faces_area=[]
if area <10 :
faces_area.append(area)
small_faces.append(each_face)
print(small_faces)
base.DeleteFaces(small_faces)
# you can only highlight the faces which are under the 10 mm 

If you have any doubts or reverse questions, don't be hesitate to comment. I am a student & want your favor in the sense of some new ideas. 

Basic ANSA script No. 5


If you want to check which are the faces do not mesh & still have pending to mesh. for this, you can go through the following command. which is very helpful in especially plastic meshing or complex zone. 

print(mesh.UnmeshedMacros())

Conclusion:

I introduced an effective example of ansa scripting in geometry clean-up work. I hope you have a background in python programming & understand the importance of programming/scripting. In the upcoming post, I will use data science - python libraries to explore LSDYNA output files like glstat, nodout, matsum, etc.

Thank you for your attention. 

#ansa scripting
#ansa automation
#data science in finite element analysis











. 









Thursday, November 12, 2020

HyperMesh Scripting Tcl/Tk Helpful For Beginners | Hypermesh Scripting 1

HyperMesh Scripting Tcl/Tk


HyperMesh Scripting | I want to highlight HyperMesh scripting with sharp examples. Before going to the depth, Initially, I will target basic questions which are usually generated in a beginner's mind. I hope, you will have a background in any programming language.


Hypermesh Tcl scripting 

What is a HyperMesh Macro? 

It is a scripted or command file used to automate a series of steps in HyperMesh.


But Why do we need it? 

To save time in repeated work.

 

What is Tcl/Tk? 

Tcl (Tool Command Language) is a scripting as well as a programming language.


It is invented by John Osterhout in 1989. It is easy to read, write & understand.


Tk (Tool kit) is a widget tool that is useful while creating of GUI (Graphics User Interface), in simple language, GUI means mobile application but here it is a computer application.


If you want to learn Tcl/Tk language, I will suggest the platform - Tutorialspoint


Below, I have given some initial hints which are helpful while scripting the scripting panel. 


This is an interpreted language. In HyperMesh scripting panel commands execute in line-by-line order same as the python programming scenario. If you notice well, After a run a single command it shows 1 or 0 entity it means,


1 show - command is executed.


0 shows - the command is not executed.


Unfortunately, there is no command to stop the script while running. I told you this basic because you might be facing issues like being trapped in an infinite loop. 

 

HyperMesh doesn't allow us to work with their entities like elements, nodes, sets, etc. there is a solution for this, and that is the concept available in HyperMesh scripting Mark.


Marks specify groups of entities that are transferred to the command processor. Marks can be created for entities like nodes & elements. Marks can create up to 2 times and it has their id. every time initially you have to need to call mark while scripting.


If you want to delete generated marks, you can use *clearmarkall or hm_markclear Elem 1 these commands as well as to maintain simplicity in the default scripting panel in hypermesh you can use clear command. 


If you observe well, you will find that hypermesh scripting code is not the same as Tcl scripting language so questions arise! What is that change?


Syntax of hypermesh scripting does not contain any parentheses and commas; they have been replaced with a blank space. Notice that while the parentheses and commas have been removed, each of the commands still begins with an asterisk (*) and that the command name hasn’t changed.


For example:

*setmeshlinecolor(6) = * setmeshlinecolor 6

*jpegfile() = *jpegfile

 

 

This scripting language is an interpreted language. It’s executed line by line you can compare it with python scripting. 

 

Hypemesh scripting panel

 

Basic Hyperscript Script No 1


Here, if you want to know all Component IDs in the deck you can try this script. At the initial stage, this type of script will help you to build a large logic & script. (Code is in green color. let’s execute it in the scripting panel.)

 

*createmark comp 1 all

set all_comp_id [hm_getmark comp 1]

puts $all_comp_id

 

 

Basic Hyperscript Script No 2

Here if want to know the number of nodes or elements on your screen you can execute this program, it will help you a lot with the understanding of how the program is executed in Hypermesh script.

 

*createmark elem 1 "displayed"

hm_marklength elem 1

 

or

 

*createmark node 1 "displayed"

hm_marklength node 1

 

Basic Hyperscript Script No 3

If you want to print the name as well as the component id you can try this program. Also in this program, you will get aware of how to use loops in hypermesh scripting.

 

*createmark comp 1 all

set comp_id [hm_getmark comp 1]

puts "id of all deck: $comp_id

 

foreach terms $comp_id  {

set name [hm_getvalue comp id =$terms dataname=name]

puts $name

}

 

Please check my other post I hope you will find it more interesting there. 

Thank you for your attention.


Conclusion:


I hope, you will get a basic idea of HyperMesh scripting & Tcl/Tk. In upcoming videos, I will come with more advanced scripts & I will teach how to generate buttons in the utility menu & attach a Tcl script to it with tool kit knowledge. Sorry for the English I am working on it. Thank you so much for your time.

#hypermesh scripting 

#hypermesh scripting for beginners.



Tuesday, November 10, 2020

HyperMesh Scripting Tcl/Tk Helpful For Beginners | hypermesh scripting 2

HyperMesh Scripting Tcl/Tk Helpful For Beginners | Hypermesh scripting | 

In the last hypermesh scripting post I had introduced to you 3 basic scripts of hypermesh, here in this post, I have come with 4 new hypermesh scripts that will be helpful while doing work on hypermesh. So without wastage of time, let's see. 

Hypermesh scripting
Source: Unsplash


Basic Hypermesh Script No. 4

If you want to detach all elements in the deck at the same time you can use the below script. It will detach all elements which are shown on the desktop. 

Scripts are available in pink color.

set a 1

while {$a<100} {

    *createmark elements 1 $a

    *detachelements 1 0

    incr a

}


Basic Hypermesh Script No. 5 

If you want to print all part id or node id or elements id you can go through the following Tcl code. Don’t forget to use clear command while using the new script, otherwise, it might be showing you the error.

 

For component id:

*clearmark comp 1; *clearmark comp 2 

hm_createmark comp 1 "advanced" "displayed"

set comp_id_list [hm_getmark comp 1]

puts "$comp_id_list"

 

 For elements id: 

 *clearmark elem 1; *clearmark elem 2 

hm_createmark elem 1 "advanced" "displayed"

set comp_id_list [hm_getmark elem 1]

puts "$comp_id_list"

 

For nodes id:

*clearmark node 1; *clearmark node 2 

hm_createmark node 1 "advanced" "displayed"

set comp_id_list [hm_getmark node 1]

puts "$comp_id_list"


Basic Hypermesh Script No. 6

If you want to remove particular elements submitted into the mark then you can use the following code to delete the elements as you want.

*createmark elem 1 38931 38951 38972

hm_marklength elem 1

hm_markremove elem 1 38931

hm_marklength elem 1


Basic Hypermesh Script No. 7

If you want to delete all the face which are under 5 mm area then you can use the following code which is very useful while going through geometry clean up in plastic meshing or 2D meshing. 


proc delete_face_under {} {

*clearmarkall 1; *clearmarkall 2

*createmark face 1 all

set face_entity [hm_getmark face 1]


foreach entity $face_entity {

    puts $entity

}

}

delete_face_under;


Conclusion:

Once upon we learn 4 basic scripts which is the base for complex scripting. I will attach some basics of Tcl/Tk programming language which directly helps to understand and resolve the complexity level of understanding. In an upcoming post I will aware you about if you don't know the syntax of Tcl scripting then how can you code in hypermesh scripting panel of usage of the command. Tcl & much more with simple language.


 

 

 

 

 

Friday, February 16, 2018

Use of programming tool - python in Ls Dyna postprocessing activities | Computer Aided Engineering

Use of programming tool - python in Ls Dyna postprocessing | Computer Aided Engineering| 

Hello friends, In this post, I will show you how you can use data science to explore or to generate more data while lsdyna post-processing of computer-aided engineering because it is a general definition that, more data means more information, more clarity & it is easy for interpolation, prediction & helps to reach a final decision while going through optimization work or any developments process.

Lsdyna python automation
Use of python in crash & safety. 

So, before going towards details of data science & implementation of it into finite element analysis, I hope you have a background in python programming & know lsdyna software generally used for crash analysis, drop analysis, etc.

And, I hope, you are aware of the output file of lsdyna, which is matsum which shows us energy absorbed by components while crashing. In simple words internal energy vs. time graph for a particular component.

It’s no big deal to open it into hyperview or meta post-processor & check output energies but wait.

If you want to go one step up so this blog is for you. So, let’s start & read carefully.

Below I have given a sample python program that reads the matsum file & puts out all PID contained in the matsum file. I know it looks too easy & might be useless at this moment, but This is an initial-level program. In my upcoming post, I will try to go into detail & explore this topic more & more in advance.


Sample Code: Python 

Output:

Description - I opened a matsum file in notepad and saved it as a .txt extension. Then paste this file into pycharm IDE & after that use this code to print all part IDs contained in the deck. 

 

In upcoming posts, I will show you how you can use different libraries of python to explore more and more finite element analysis. Also, I will show how you can generate your own application with the help of python for your work with Tkinter. 

 

Conclusion:

 
We got a basic overview of how we can use data science in the mechanical sector, especially computer-aided engineering to explore more and more about data generation. I aim to push mechanical engineers toward data science. Here I gave a start and it totally depends on you, how can you use data science for the welfare of mechanical society. In the end, I will show you how to plot graphs of the Matsum file of lsdyna through the use of the matplotlib library & as well as how to compare two matsum files in a single click with the generation of all graphs contained inside it. Thank you. 




Sunday, January 10, 2016

ANSA Scripting Examples For Beginners | Ansa Scripting 1

ANSA Scripting Basics

Here, my intention to focus on the basics of ANSA scripting. So, let’s start, before going to ANSA scripting, I hope, you have a background in python programming especially it’s subdomain object-oriented programming & functional programming. I am expected at least you have beginner level knowledge in programming. 

Computer aided engineering ansa scripting
Source: Unsplash

Here I don’t want to share the basics of python programming, it’s free & available everywhere. Without a wastage of time, let’s start.


  • ANSA scripting totally depends upon python programming & beta solutions developed their own libraries which are useful for while ANSA scripting. 
  • If you want to build your personal GUI you can do it as you wish with the Tkinter widget tool available default inbuilt in ANSA. (GUI means small application or window the same as a mobile application.

  • Sometimes, a question arises in a beginner's mind, what is this Tkinter? Don’t worry, in common language, its tool kit available for python by which you can develop your own application or software.

  • Most of the details like how to open a scripting window in ANSA, how to execute a code, or basic tool knowledge is available on YouTube please go through it. Here I can’t share any photos & Videos of any software because it might be created copyright issues for me, unfortunately after that I unable to continue all this stuff.

  • Yes, I will share some basic script, please execute it on your ANSA platform. It will help you a lot to understand & build logic behind the ANSA scripting, after that, you will able to build a script for your own problem statements.
Ansa python scripting
Source: Unsplash

Basic ANSA script No. 1

If you want to print or see the all PID in lsdyna deck. You can go through this basic script & I know it’s not a big deal to see all PID in ANSA. Initially, this type of scripts plays an important role to build logic in complex scenarios.

Code available in green color & copy & execute it on ANSA scripting window.

 

import ansa

from ansa import *

rushi=base.CollectEntities(constants.LSDYNA, None, "__PROPERTIES__", )

print(len(rushi))

for i in rushi:

                print(i._name)

               

Here ansa is a python library developed by beta solutions. base.CollectEntities is function in library & constants.LSDYNA shows you are using the LSDYANA deck.

 

Basic ANSA script No. 2

If you have lots of PID & want to see the name of the exact PID you can go through this script. Here default PID is 25, you can check the title of it easily.

 

import ansa

from ansa import *

def main():

 name_of_part= base.GetEntity(constants.LSDYNA, "__PROPERTIES__", 25,)

 print(name_of_part._name)

main()

if __name__ == '__main__':

                main()

 

In the search panel of the Ansa script if you search any function and double click on it you will get a help option at the base it helps a lot while scripting in ANSA.

 
Basic ANSA script No. 3

If you want to print all PID & name In front of it you can go through this script. I am increasing the complexity level step by step I hope you will understand.

 

import ansa

from ansa import *

id_collector=[]

name_collector=[]

def main():

                part_name=base.CollectEntities(constants.LSDYNA, None, "__PROPERTIES__", False, True)

                for i in part_name:

                    id_collector.append(i._id)

                    name_collector.append(i._name)

                    pass

main()

zip_list=list(zip(id_collector, name_collector))

for points in zip_list:      

    print(points)

if __name__ == '__main__':

                main()

 

Here I use a zip function to add to the list together & if you search on google about the details of the zip function you will get it easily, so I will not go in-depth of python terms do its own place. & if __name__ == '__main__':  this code is useful to call main function & to stop looping.

 

ANSA scripting
Source: Unsplash 

Conclusion

I hope you will get the basic idea about ANSA scripting, in an upcoming post, I will use real examples that will be useful while meshing work as well as deck preparation work. Also, I will share how can we use python & its different libraries in finite element analysis & design. Thank you. Excuse me for English grammar I already start working on it. 


 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 


 

 

 

 

Thank you !

Search This Blog

Contact Form

Name

Email *

Message *