From CAELinuxWiki
Revision as of 23:14, 7 September 2009 by Projectionist (Talk | contribs)

Jump to: navigation, search

Doing an automated study with Salome and CodeAster

This example may seem a little artificial. However this was part of my work this summer for a research partner of my university. I am not allowed to place the original problem here because conflicts with the companies secrets would be inevitable. So I have to construct a new problem around the expertise I gained from my task. Even if you do not have a similar task you may learn something.


If there is the need to analyze lots of topological likewise geometries then automation cam make life easier. This paragraph describes automatable analysis of a 2D part section. Given the case the geometry comes as list of vertices in a known order we can read this list with a script and create a TUI script.

The input data

The input data for the analysis is stored in a text file containing information about the geometry and the load. The geometry, a 2D part section, is given by a list of vertices. The point where the concentrated load acts on the structure is also given. Distributed load in 3D results in concentrated load in the 2D section. The text file may come from a CAD program. At least in my work this was the case.

# data for part sect01

name = 'sect01'

case1.fx = 120.0;
case1.fy = 21.5;

case2.fx = -54.0;
case2.fy = 97.2;

vrtcs = [
12.0 100.0
... ...
... ...

Processing the input data

We process the data file with a python script. This script generated the TUI script which will be processed by Salome. We could also directly import the geometry in the TUI script via exchange file format but then we would not know where to apply the load and where to constrain the degrees of freedom. At least I would not know.

Running the TUI script

Creating the FE study

This part was taken from CAELinux. It is the core of the script that uses a GUI for the major input. Here the original script is shown in action.

View asterHelpers createFeStudy detail.png

I removed the things related to the GUI. Crude work I have to admit. This script receives the filename of the data file as argument.

   #!/usr/bin/env python
   # Create a new FE analysis job from a template
   # J.Cugnoni,, 2005
   # edited by G. Holzinger, JKU Linz, 2009
   import os
   import os.path
   import sys
   fileName = sys.argv[1]
   name = fileName[0:fileName.rfind(".")]
   etude,fich,3,FR F
   opt_val,rep_dex _VIDE
   etude,fich,3,UL 8
   etude,fich,2,type mess
   etude,fich,4,donnee 0
   option,nbmaxnook 1
   etude,fich,2,resultat 1
   nom_fich_export _VIDE
   option,rep_dex 0
   etude,fich,4,compress 0
   etude oui
   debug 0
   opt_val,cpresok RESNOOK
   forlib_delete non
   etude,fich,4,serv Local
   etude,fich,3,donnee 0
   serv_fich_export -1
   surcharge,nbfic 0
   etude,fich,6,FR F
   path_etude [__prjdir__]
   option,rep_outils 0
   etude,fich,6,UL 80
   etude,fich,3,resultat 1
   option,cpresok 1
   etude,fich,5,compress 0
   etude,fich,1,FR F
   etude,fich,1,serv Local
   consult_supprimer non
   memoire 128
   etude,fich,1,UL 20
   onglet_actif etude
   asquit non
   etude,fich,2,donnee 0
   option,classe 1
   etude,fich,4,type erre
   consult_a_corriger non
   ident non
   etude,fich,4,resultat 1
   etude,fich,6,compress 0
   etude,fich,1,donnee 1
   rex non
   etude,fich,1,type libr
   etude,fich,1,nom ./[__prjname__]
   suivi_interactif 1
   path_sources _VIDE
   pre_eda non
   etude,fich,4,FR F
   forlib_create non
   etude,fich,4,UL 9
   etude,fich,3,nom ./[__prjname__].resu
   serv_tests -1
   etude,fich,6,serv Local
   etude,fich,5,nom ./[__prjname__].base
   make_etude run
   option,depart 1
   nom_profil [__astkfile__]
   args _VIDE
   etude,fich,5,resultat 1
   etude,fich,0,donnee 1
   opt_val,mem_aster _VIDE
   etude,fich,3,serv Local
   etude,fich,0,compress 0
   option,mem_aster 1
   asdeno non
   serv_surcharge -1
   serv_sources -1
   surcharge non
   etude,fich,6,type rmed
   etude,fich,0,serv Local
   M_1 oui
   serv_etude -1
   M_2 non
   etude,fich,6,resultat 1
   etude,fich,2,FR F
   opt_val,rep_outils _VIDE
   M_3 non
   etude,fich,2,UL 6
   path_surcharge _VIDE
   consult non
   M_4 non
   asno non
   etude,fich,3,type resu
   etude,fich,1,compress 0
   opt_val,ncpus 1
   emis_sans non
   serveur localhost
   opt_val,classe _VIDE
   opt_val,dbgjeveux _VIDE
   option,ncpus 1
   etude,fich,0,type comm
   opt_val,facmtps 1
   etude,fich,0,resultat 0
   opt_val,rep_mat _VIDE
   special _VIDE
   tests,nbfic 0
   temps 240
   option,dbgjeveux 0
   etude,fich,5,FR R
   etude,fich,5,serv Local
   asrest non
   batch 0
   etude,fich,5,UL 0
   etude,fich,2,compress 0
   option,facmtps 1
   serv_profil -1
   etude,fich,6,donnee 0
   etude,fich,0,FR F
   option,rep_mat 0
   etude,fich,0,UL 1
   etude,fich,2,serv Local
   etude,fich,0,nom ./[__prjname__].comm
   asverif non
   etude,fich,2,nom ./[__prjname__].mess
   opt_val,depart _VIDE
   etude,fich,1,resultat 0
   sources,nbfic 0
   etude,fich,5,donnee 0
   etude,fich,4,nom ./[__prjname__].erre
   tests non
   noeud localhost
   etude,fich,5,type base
   etude,fich,3,compress 0
   emis_prof non
   etude,fich,6,nom ./[__prjname__]
   etude,nbfic 7
   agla non
   opt_val,nbmaxnook 5
   version STA8.1
   path_tests _VIDE
   nom_profil %s
   path_etude %s
   etude,fich,0,nom ./%s.comm
   etude,fich,1,nom ./
   etude,fich,2,nom ./%s.mess
   etude,fich,3,nom ./%s.resu
   etude,fich,4,nom ./%s.erre
   etude,fich,5,nom ./%s.base
   etude,fich,6,nom ./
   def copyfile(filein,fileout):
   def validate():
       if os.path.exists(prjdir):
           print "project name allready exists in base directory"
           # project directory
           # file names
           commfile=os.path.join(prjdir,prjname + ".comm")
           messfile=os.path.join(prjdir,prjname + ".mess")
           errefile=os.path.join(prjdir,prjname + ".erre")
           resufile=os.path.join(prjdir,prjname + ".resu")
           mmedfile=os.path.join(prjdir,prjname + "")
           rmedfile=os.path.join(prjdir,prjname + "")
           basefile=os.path.join(prjdir,prjname + ".base")
           astkfile=os.path.join(prjdir,prjname + ".astk")
           # copy files
           # create ASTK profile
           fd.write(templateASTK % ((astkfile,prjdir,) + (prjname,)*7))
           # show message and exit
           print "New FE analysis project created successfully."
           return 0

   vars["basedirname"]=os.getcwd() + "/"
   vars["meshname"]="mesh_" + name + ".med"

Create the *.export file

Run the FE study with Code Aster

Doing the study

Here is where all the magic happens. Don't get confused with all the boxes, this source code belongs to one file. However these boxes subdividing the code into blocks may improve readability.

   #!/usr/bin/env python
   # Perform a FE analysis using Salome and CodeAster
   # G. Holzinger, JKU Linz, 2009
   # The text file containing the geometry data must reside in the same directory as this file.
   # Provide the filename as argument!
   # If the filename contains dots ('.') then the file must have a file extension (i.e. sect01.txt or sect01.v02.txt)
   # Files without file extension are also allowed (i.e. sect01)
   # Not allowed are files with filenames containing dots but without file extension (i.e. sect01.v02) for 
   # everything left of the rightmost dot excluding this dot is used as projectname and is used to build 
   # the file paths too. This would not work in this case!
   import os
   import sys
   from subprocess import *
   def usage():
   	print "\tusage: ./ FILENAME"
   	print ""
   	print "\texample: ./ sect01.txt"
   # retrieve number of arguments
   nargs = len(sys.argv)
   # no argument was passed
   # nargs == 1 because the filename of the file itself is automatically passed as argument
   if nargs == 1:
   	print "no arguments passed!"
   # one argument was passed
   if nargs == 2:
   	argument = sys.argv[1]
   	# remove file extension
   	index = sys.argv[1].rfind(".")
   	if index == -1:
   		name = sys.argv[1]
   		name = sys.argv[1][0:index]
   	print name
   	# initialize status variables
   	sts0 = (0, 1)
   	sts1 = (0, 1)
   	sts2 = (0, 1)
   	sts3 = (0, 1)
   	sts4 = (0, 1)
   	sts5 = (0, 1)
   	# create TUI script for salome
   	print "calling: ./ " + argument
   	p0 = Popen("./ " + argument, shell=True)
   	sts0 = os.waitpid(, 0)
   	if sts0[1] == 0:
   		# create mesh
   		p1 = Popen("source /SALOME-ROOT/salome_5.1.2/KERNEL_5.1.2/", shell=True)
   		sts1 = os.waitpid(, 0)
   		p2 = Popen("/SALOME-ROOT/salome_5.1.2/KERNEL_5.1.2/bin/salome/runSalome -t -u ./", shell=True)
   		sts2 = os.waitpid(, 0)
   		if sts2[1] == 0:
   			# create finite element study
   			p3 = Popen("./ " + argument, shell=True)
   			sts3 = os.waitpid(, 0)
   			if sts3[1] == 0:
   				# create the export file
   				p4 = Popen("./ " + name, shell=True)
   				sts4 = os.waitpid(, 0)
   				# run the study	
   				exportFullPath = os.getcwd() + ("/" + name)*2 + ".export"
   				resuFullPath = os.getcwd() + ("/" + name)*2 + ".resu"
   				p5 = Popen("/opt/SALOME-MECA-2009.1-GPL/aster/outils/as_run --run " + exportFullPath, shell=True)
   				sts5 = os.waitpid(, 0)
   				if sts5[1] == 0:
   					# show results
   					p6 = Popen("cat " + resuFullPath, shell=True)
   					sts6 = os.waitpid(, 0)
   				# end if
   			# end if
   		# end if
   	# end if
   	# print status report
   	print "return values of called scripts and programs\n\n"
   	print " " + str(sts0)
   	if sts0[1] == 0:
   		print "\tOk\n"
   		print "\tFailed\n"
   	print " " + str(sts1)
   	if sts1[1] == 0:
   		print "\tOk\n"
   		print "\tFailed\n"
   	print "runSalome: " + str(sts2)
   	if sts2[1] == 0:
   		print "\tOk\n"
   		print "\tFailed\n"
   	print " " + str(sts3)
   	if sts3[1] == 0:
   		print "\tOk\n"
   		print "\tFailed\n"
   	print " " + str(sts4)
   	if sts4[1] == 0:
   		print "\tOk\n"
   		print "\tFailed\n"
   	print "as_run: " + str(sts5)
   	if sts5[1] == 0:
   		print "\tOk\n"
   		print "\tFailed\n"
   if nargs > 2:
   	print "too many arguments passed!"

to be continued ...