Welcome to the forums. Please post in English or French.

You are not logged in.

#1 2011-07-21 08:24:50

m_golbs
Member
From: Germany
Registered: 2009-11-09
Posts: 126

Python Code in *.comm - full Python possible?

Hello,

what complexity of Python can I use in *.comm. For example can I use file functions of python to read *.mess, *.dat or other ASCI file in code aster run?

Greeting Markus


The superfluous chase, is to miss the essential.
Jules Saliège

Offline

#2 2011-07-21 09:53:44

AsterO'dactyle
Administrator
Registered: 2007-11-29
Posts: 421

Re: Python Code in *.comm - full Python possible?

Code_Aster IS Python

for instance, if you run asteru (the main executable)
/opt/aster/NEW11/asteru, you have:

Version séquentielle de Code_aster 
Python 2.6.6 (r266:84292, Sep 15 2010, 16:02:57) 
[GCC 4.4.5] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>>

BUT

If you need to reuse results from a Aster's command in another command, use PAR_LOT='NON':
DEBUT(PAR_LOT='NON') or POURSUITE(PAR_LOT='NON')

The files (.mess or .resu or another ones produced by aster) are known in Fortran modes, you must use the logical units and not the "real names" from .export or Astk

For instance: fort.6 for your the current mess file


Code_Asterの開発者

Offline

#3 2011-07-21 10:38:16

m_golbs
Member
From: Germany
Registered: 2009-11-09
Posts: 126

Re: Python Code in *.comm - full Python possible?

Hello,

thanks for the informations.

...Code_Aster IS Python..

I can also create every Python code in my *.comm and by asrun this Python code work?

If it possibel to find the maximal translation of a node of everey mode in a MODE_ITER_SIMULT  with a command in *.comm?

If it also possible to modify the SIEF_ELGA_DEPL with a factor by mode after finding the maximal translations by node of everey mode in MODE_ITER_SIMULT? I want to norm the SIEF_ELGA_DEPL by total translation factors.

Greeting Markus

Last edited by m_golbs (2011-07-21 10:56:55)


The superfluous chase, is to miss the essential.
Jules Saliège

Offline

#4 2011-07-21 13:47:42

Theobalt
Member
Registered: 2009-03-11
Posts: 119

Re: Python Code in *.comm - full Python possible?

Hi!

You should take a look at [U1.03.02] Méthodes Python d'accès aux objets Aster.

You can do whatever you want. For post-processing, I often extract values from Code_Aster results, treat them with python scripts, use the results as a new input for some Code_Aster functions, extract the new results, etc. One could for example write an optimisation script in python and use it in a loop with Code_Aster functions and modify your model at each loop until you get the wanted results.

Your creativity is about the only limit.

Offline

#5 2011-07-21 15:29:46

m_golbs
Member
From: Germany
Registered: 2009-11-09
Posts: 126

Re: Python Code in *.comm - full Python possible?

Hello,

thanks! I have a look at [U1.03.02]. ...Your creativity is about the only limit.... It's is a very good sound in my ears.


Greeting Markus


The superfluous chase, is to miss the essential.
Jules Saliège

Offline

#6 2011-09-22 16:09:18

mattia
Member
From: Italy
Registered: 2011-09-22
Posts: 116

Re: Python Code in *.comm - full Python possible?

Hi everybody!

I share with m_golbs the interest of writing Python Code inside a .comm file.

Following this purpose, I read the document [U1.03.02], but I'm still unable to understand how the whole process works. I explain myself better: looking at the page http://www.code-aster.org/wiki/doku.php … thon:start I found some examples of .comm file containing both Python and Code_Aster commands. But how can one run them? I have to open in Salome-Meca a model with a defined geometry and mesh, and then recall the .comm file in Aster environment? Or I can run the .comm file by command line using as_run? I tried both the ways but they aren't working.

Can you give me a hint?


Thank you,

Mattia


Mattia

Offline

#7 2011-09-22 16:58:05

Archibald Archambaud
Member
From: Clamart, France
Registered: 2007-12-03
Posts: 322

Re: Python Code in *.comm - full Python possible?

Hi.

Here is the easy way :
- launch Astk
- File > Import > testcase
- type "forma01a" or any of the 2800 test cases
- save and run!

The geek way :
- open a shell
- type "as_run --get forma01a.comm --all" (I assume that as_run is in your PATH)
- type "cd forma01a"
- type "as_run forma01a.export"

AA

Offline

#8 2011-09-23 14:14:25

mattia
Member
From: Italy
Registered: 2011-09-22
Posts: 116

Re: Python Code in *.comm - full Python possible?

Hi Archibald,

thanks for your reply.

This procedure works fine with some test cases, but not with my model. In other words, ASTK fails to realize the import procedure. Thus, I created manually a .export file using ASTK, into which I specified the necessary input files (comm and mmed) . Once I tried to run it as_run, I receive this error:

   !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
   ! <S> Exception utilisateur levee mais pas interceptee.         !
   ! Les bases sont fermees.                                       !
   ! Type de l'exception : error                                   !
   !                                                               !
   !  Vous avez demand l'affectation d'un modle sur un GROUP_MA, !
   !  or le maillage MAIL n'en contient aucun.                     !
   !  L'affectation du modle n'est donc pas possible.             !
   !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!

it is interesting to note that at the moment I still have to insert Python code into the .comm file.

Where am I wrong?

Here are attached the export and the mess file.


Thanks,

Mattia

Last edited by mattia (2011-09-23 14:14:57)


Attachments:
weld.zip, Size: 4.24 KiB, Downloads: 466

Mattia

Offline

#9 2011-09-23 14:31:49

humbert
Member
From: Seoul
Registered: 2007-11-26
Posts: 81
Website

Re: Python Code in *.comm - full Python possible?

AsterO'dactyle wrote:

...Code_Aster IS Python..

... or not.
I'm very sorry to lower your expectations, but this is wrong.
Code_Aster unfortunately breaks a LOT of useful Python functionalities, and as a result you should not assume that it is the same as Python.
In particular you should take care of return values in functions using Code_Aster commands, and also aliasing.

Example (aliasing):

list1 = DEFI_LIST_INST( ... )
list2 = list1

If you use list2 then Code_Aster complains about list2 being an alias of list1, sending a SUPERVIS_93 alarm (!), which is totally unexpected from the point of view of a Python programmer. In fact list1 and list2 are not even considered being the same type of object! This is a (bad) change from the normal Python behavior.
Try also using Python arrays to store Code_Aster concepts created by commands (e.g. "a[5] = DEFI_LIST_INST(...)") and you will end up with a variable (here "a_5") being defined automatically. And since concept names are limited to 8 characters (variable names in real Python are not limited in length) then your array name is here limited to 6 characters only (+ 1 for '_' and 1 for the index '5' = 8 characters in total).

This subjects has already been debated quite some times (in French though) on this forum.

Yet aside from this I guess the rest (say 80~90%) is similar to Python, which allows you to use "for" loops, "if" branching, etc...
Of course you can use I/O functions to read/write files but remember the current directory is the running directory (most probably in your temporary directory), not the directory where you prepare your .comm, and filenames are changed. As mentioned your .comm is renamed fort.1, the input and output directories are respectively REPE_IN and REPE_OUT (whatever may be their real name as stated in the .export file), the .mess is renamed fort.6, etc...

Example: write the values of variables "a" and "b" in the file "myresult.csv"

.comm:
fp = open("fort.55", "w")
fp.write("%f,%f" % (a,b))
fp.close()

.export:
F libr /path/to/your/project/myresult.csv R 55

Note: Alternatively you can instead write to "REPE_OUT/myresult.csv" (in the open() call above) and add a line for REPE_OUT in your .export.
R repe /path/to/your/project/results R 0
Then after the simulation you will have a file "/path/to/your/project/results/myresult.csv".
In fact Code_Aster will copy ALL files from REPE_OUT (in the temporary simulation directory) to your specified result directory (here "/path/to/your/project/results").

Last edited by humbert (2011-09-23 14:36:21)

Offline

#10 2011-09-23 14:41:09

mattia
Member
From: Italy
Registered: 2011-09-22
Posts: 116

Re: Python Code in *.comm - full Python possible?

humbert wrote:

Try also using Python arrays to store Code_Aster concepts created by commands (e.g. "a[5] = DEFI_LIST_INST(...)") and you will end up with a variable (here "a_5") being defined automatically. And since concept names are limited to 8 characters (variable names in real Python are not limited in length) then your array name is here limited to 6 characters only (+ 1 for '_' and 1 for the index '5' = 8 characters in total).

Hi humbert,

do you mean that I can't create a Python array with data calculated with Code_Aster? That would be a big problem for me, since it is actually what I was planning to do!




Mattia


Mattia

Offline

#11 2011-09-27 08:53:06

mattia
Member
From: Italy
Registered: 2011-09-22
Posts: 116

Re: Python Code in *.comm - full Python possible?

Archibald Archambaud wrote:

Hi.

Here is the easy way :
- launch Astk
- File > Import > testcase
- type "forma01a" or any of the 2800 test cases
- save and run!

The geek way :
- open a shell
- type "as_run --get forma01a.comm --all" (I assume that as_run is in your PATH)
- type "cd forma01a"
- type "as_run forma01a.export"

AA

Thank you Archibald, at last I managed to run the solution in this way:
- first I create manually a file with ASTK;
- then I create the export file writing "as_run --get_export myfile.comm > myfile.export" into the terminal;
- finally I run the export file with "as_run myfile.export".


Mattia


Mattia

Offline

#12 2011-09-29 09:16:43

humbert
Member
From: Seoul
Registered: 2007-11-26
Posts: 81
Website

Re: Python Code in *.comm - full Python possible?

mattia wrote:
humbert wrote:

Try also using Python arrays to store Code_Aster concepts created by commands (e.g. "a[5] = DEFI_LIST_INST(...)") and you will end up with a variable (here "a_5") being defined automatically. And since concept names are limited to 8 characters (variable names in real Python are not limited in length) then your array name is here limited to 6 characters only (+ 1 for '_' and 1 for the index '5' = 8 characters in total).

Hi humbert,

do you mean that I can't create a Python array with data calculated with Code_Aster? That would be a big problem for me, since it is actually what I was planning to do!

Mattia

No, I mean that your array must have a very short name (5~6 characters) because the number of digits of the index of your array will be counted in the 8-character limit of Code_Aster for names of concepts. For example if you have an array of 50 elements then each item in the array will be refered to in Python as "arrayname[index]" but will internally to Code_Aster be refered to as "arrayname_index", which must have a length <= 8 character. In this case if index <= 50 then you must have length(arrayname) <= 5 characters.
Note however that if the items of your array do not hold a Code_Aster concept (as returned by e.g. DEFI_xxx functions) then the array is a regular Python array with regular items, and without this limitation.

For short:

mylongarrayname[25] = DEFI_xxx()   # FAIL: "mylongarrayname_25 = DEFI_xxx()"

res[25] = DEFI_xxx()               # OK  : "res_25 = DEFI_xxx()"

Offline

#13 2011-09-29 09:50:23

mattia
Member
From: Italy
Registered: 2011-09-22
Posts: 116

Re: Python Code in *.comm - full Python possible?

humbert wrote:
mattia wrote:
humbert wrote:

Try also using Python arrays to store Code_Aster concepts created by commands (e.g. "a[5] = DEFI_LIST_INST(...)") and you will end up with a variable (here "a_5") being defined automatically. And since concept names are limited to 8 characters (variable names in real Python are not limited in length) then your array name is here limited to 6 characters only (+ 1 for '_' and 1 for the index '5' = 8 characters in total).

Hi humbert,

do you mean that I can't create a Python array with data calculated with Code_Aster? That would be a big problem for me, since it is actually what I was planning to do!

Mattia

No, I mean that your array must have a very short name (5~6 characters) because the number of digits of the index of your array will be counted in the 8-character limit of Code_Aster for names of concepts. For example if you have an array of 50 elements then each item in the array will be refered to in Python as "arrayname[index]" but will internally to Code_Aster be refered to as "arrayname_index", which must have a length <= 8 character. In this case if index <= 50 then you must have length(arrayname) <= 5 characters.
Note however that if the items of your array do not hold a Code_Aster concept (as returned by e.g. DEFI_xxx functions) then the array is a regular Python array with regular items, and without this limitation.

For short:

mylongarrayname[25] = DEFI_xxx()   # FAIL: "mylongarrayname_25 = DEFI_xxx()"

res[25] = DEFI_xxx()               # OK  : "res_25 = DEFI_xxx()"

Ok, now it's perfectly clear. Thanks for the information, I will keep it in mind smile


Mattia


Mattia

Offline

#14 2021-03-30 12:37:30

corra
Member
Registered: 2008-06-03
Posts: 154

Re: Python Code in *.comm - full Python possible?

Hello,

I re-open this old post to ask a question on the use of Python.
My question is about the visibility of the objects created in a Python function.
Let us consider a function which creates a field:

def function1():
   Y0CH=CREA_CHAMP(TYPE_CHAM='NOEU_NEUT_R',    
      OPERATION='AFFE',
      MODELE=MODEL,
      AFFE= _F(		
         GROUP_MA='all',
         NOM_CMP= ('X1','X2','X3'),	
         VALE = (0.0, 0.0, 0.0),
      ),
   );

How can I access Y0CH (including destroying it!) outside the function?
- Option 1 (regular Python): the object is not visible, unless I return it at the end of the function with return Y0CH
- Option 2 (similar to INCLUDE operator): the object is already visible, I can access it with its name without returning it
- Option 3: the object can be returned, but it will be a shallow copy. Any action on the object outside the function (including destroying it) will not change the original object, which maintains the same name as in the function. This means that it is not possible to re-use that name, because it is not possible to destroy it with DETRUIRE.

From the tests I am doing, my understanding is that the correct answer is the last one, which basically prevents the use of function to create objects that need to be destroyed.

Can anyone explain me better this point?
Thank you and best regards,

Corrado

Offline