Metafor

ULiege - Aerospace & Mechanical Engineering

User Tools

Site Tools


doc:user:tutorials:tuto0

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revisionPrevious revision
Next revision
Previous revision
doc:user:tutorials:tuto0 [2020/09/08 12:13] bomandoc:user:tutorials:tuto0 [2020/09/08 16:26] (current) – [Conclusions] boman
Line 1: Line 1:
- 
 ====== Python in a nutshell ====== ====== Python in a nutshell ======
- 
- 
-===== Introduction ===== 
  
 This section shows how to use [[doc:user:general:glossaire#Python]], the interpretor used by [[doc:user:general:glossaire#Metafor]], in a basic way. Ideally, [[http://www.python.org/|Python official website]] should be used. This section shows how to use [[doc:user:general:glossaire#Python]], the interpretor used by [[doc:user:general:glossaire#Metafor]], in a basic way. Ideally, [[http://www.python.org/|Python official website]] should be used.
Line 10: Line 6:
  
  
-===== The interpreter ===== +===== Introduction =====
- +
-==== Introduction ====+
  
 When Metafor is started, a window appears: When Metafor is started, a window appears:
Line 56: Line 50:
   'IndentationError', 'IndexError ...   'IndentationError', 'IndexError ...
  
-==== Types ====+===== Types =====
  
 The type of a variable is obtained with the command ''type(var)''. Every variable has a type (also called a "class"), however in Python the type of a variable can change during the execution of the code, unlike in [[doc:user:general:glossaire#Fortran]] or C. The type of a variable is obtained with the command ''type(var)''. Every variable has a type (also called a "class"), however in Python the type of a variable can change during the execution of the code, unlike in [[doc:user:general:glossaire#Fortran]] or C.
Line 74: Line 68:
  
  
-==== Functions ====+===== Functions =====
  
 Simple functions can be created using the interpreter. This is quite useful to avoid any cut and paste, or simply to write algorithms as scripts. For example:  Simple functions can be created using the interpreter. This is quite useful to avoid any cut and paste, or simply to write algorithms as scripts. For example: 
Line 103: Line 97:
   myfun = lambda x, a=1, b=1, c=0 : a*x*x+b*x+c   myfun = lambda x, a=1, b=1, c=0 : a*x*x+b*x+c
  
-==== Modules ====+===== Modules =====
  
 It is quite useful to gather several functions into a file, to form a [[doc:user:general:glossaire#module]] (one module is defined with one file). In Metafor, every [[doc:user:general:glossaire#test case]] is defined in a [[doc:user:general:glossaire#module]]. For example, the test case named ''cont2'' is located in ''Metafor/apps/qs/cont2.py'' and identified by ''apps.qs.cont2'' under Python. It is quite useful to gather several functions into a file, to form a [[doc:user:general:glossaire#module]] (one module is defined with one file). In Metafor, every [[doc:user:general:glossaire#test case]] is defined in a [[doc:user:general:glossaire#module]]. For example, the test case named ''cont2'' is located in ''Metafor/apps/qs/cont2.py'' and identified by ''apps.qs.cont2'' under Python.
Line 111: Line 105:
   >> import math   >> import math
   >>> dir(math)   >>> dir(math)
-  ['__doc__', '__loader__', '__name__', '__package__', '__spec__', 'acos', 'acosh', 'asin', 'asinh', 'atan', 'atan2', 'atanh', 'ceil', 'copysign', 'cos', 'cosh', 'degrees', 'e', 'erf', 'erfc', 'exp', 'expm1', 'fabs', 'factorial', 'floor', 'fmod', 'frexp', 'fsum', 'gamma', 'gcd', 'hypot', 'inf', 'isclose', 'isfinite', 'isinf', 'isnan', 'ldexp', 'lgamma', 'log', 'log10', 'log1p', 'log2', 'modf', 'nan', 'pi', 'pow', 'radians', 'remainder', 'sin', 'sinh', 'sqrt', 'tan', 'tanh', 'tau', 'trunc']+  ['__doc__', '__loader__', '__name__', '__package__', '__spec__', 'acos', 'acosh',  
 +  'asin', 'asinh', 'atan', 'atan2', 'atanh', 'ceil', 'copysign', 'cos', 'cosh',  
 +  'degrees', 'e', 'erf', 'erfc', 'exp', 'expm1', 'fabs', 'factorial', 'floor',  
 +  ... 
 +  'sqrt', 'tan', 'tanh', 'tau', 'trunc']
   >>> math.cos(0.1)   >>> math.cos(0.1)
   0.99500416527802582   0.99500416527802582
Line 139: Line 137:
 To use mathematical functions, the module ''math'' must be imported. In the same way, the functions which are used to build an input file in Metafor must be imported from the module ''wrap'', using the command ''from wrap import *'' To use mathematical functions, the module ''math'' must be imported. In the same way, the functions which are used to build an input file in Metafor must be imported from the module ''wrap'', using the command ''from wrap import *''
  
-==== Objects ====+===== Objects =====
  
 Python is [[doc:user:general:glossaire#object-oriented]]. This means that classes can be created. To make it short, a class is a structure containing data, to which methods can be associated. For example, to compute the length of a vector, a function can be defined : Python is [[doc:user:general:glossaire#object-oriented]]. This means that classes can be created. To make it short, a class is a structure containing data, to which methods can be associated. For example, to compute the length of a vector, a function can be defined :
Line 187: Line 185:
  
  
-==== Loop - tuples - lists ====+===== Loop - tuples - lists =====
  
 In a Metafor [[doc:user:general:glossaire#input file]], the same operation may be used several times in a row (for example, meshing the curves numbered from 1 to 10). In such a case, loops are used. A loop is written as: In a Metafor [[doc:user:general:glossaire#input file]], the same operation may be used several times in a row (for example, meshing the curves numbered from 1 to 10). In such a case, loops are used. A loop is written as:
Line 212: Line 210:
  
  
-==== Conditional constructs  ====+===== Conditional statements =====
  
 It may be useful to parametrize an input file in a complex way using conditional statements. For example, a boolean ''useLinearMaterial'' can be defined, to choose whether the material behaves in an linear way. This parameter, defined in the beginning of the input file, is easy to modify and leads to a conditional definition of a linear material. Such a construct requires the command ''if'', which is used as :  It may be useful to parametrize an input file in a complex way using conditional statements. For example, a boolean ''useLinearMaterial'' can be defined, to choose whether the material behaves in an linear way. This parameter, defined in the beginning of the input file, is easy to modify and leads to a conditional definition of a linear material. Such a construct requires the command ''if'', which is used as : 
Line 226: Line 224:
    
  
-==== Important notes ====+===== Important notes =====
  
  
Line 251: Line 249:
  
 => [[doc:user:tutorials:tuto1]] => [[doc:user:tutorials:tuto1]]
 +
 +
doc/user/tutorials/tuto0.1599559983.txt.gz · Last modified: 2020/09/08 12:13 by boman

Donate Powered by PHP Valid HTML5 Valid CSS Driven by DokuWiki