Book of Examples
2011
University of Duisburg-Essen
Faculty of Engineering
Department of Civil Engineering
Structural Analysis and Construction
Dr. E. Baeck
2.12.2011
Contents
2 Basics in Python 13
2.1 Reserved Words . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13
2.2 Packages and Modules . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13
2.2.1 Import of a whole Module or Package . . . . . . . . . . . . . . . . . . . . . . . 14
2.2.2 Selective Import of Module Names . . . . . . . . . . . . . . . . . . . . . . . . 14
2.2.3 Import with new Names . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14
2.3 Operators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14
2.3.1 Unary Operators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15
2.3.2 Arithmetic Operators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15
2.3.3 Bit Operators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15
2.3.4 Manipulating Bits and Hexadecimal Numbering System . . . . . . . . . . . . . 16
2.3.5 Comparison Operators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 18
2.3.6 Membership Operators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19
2.3.7 Identity Operators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19
2.4 Print and Output Formats . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19
2.5 Basic Data Types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 20
2.6 Code Blocks . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 21
2.7 Loop for Repetitions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 22
2.8 Branches for Decisions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 25
2.9 Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 28
2.10 Data Sequences . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 32
2.10.1 Working with Tuples . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 32
2.10.2 Working with Lists . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 33
2.10.3 Working with Dictionaries . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 34
2.11 Random Numbers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 35
iii
Page iv Analysis of Structures - WS10/11
3 Python Projects 47
3.1 Newton, Step2 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 47
3.2 Profiles, Thin Walled Approach . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 51
3.2.1 Implementation of a general Profile Classes . . . . . . . . . . . . . . . . . . . . 51
3.2.2 Implementation of basic Classes for a Thin Walled Model . . . . . . . . . . . . 56
3.2.3 Implementation of a little Profile Database . . . . . . . . . . . . . . . . . . . . 62
III Appendix 95
A Some Theory 97
A.1 Section Properties . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 97
A.1.1 The Area of a Profile Section . . . . . . . . . . . . . . . . . . . . . . . . . . . . 97
A.1.2 First Moments of an Area . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 97
A.1.3 Second Moments of an Area . . . . . . . . . . . . . . . . . . . . . . . . . . . . 98
A.1.4 Center of Mass . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 99
E. Baeck
CONTENTS Page 1
2.12.2011
Page 2 Analysis of Structures - WS10/11
E. Baeck
Part I
3
1
If we start with Python, we should think about the choice of the Python version. Because we will use
some additional Python packages, we should be sure, that this packages are available for the desired
Python version. In the case of our lecture we will select the Python version 2.6, which is properly stable
and furthermore all the needed add-on packages are available.
To start from the beginning, we have to download the following packages for windows first. It is rec-
ommended to download the windows installer version, if available because this is the easiest kind of
installation. The installation procedure should start with the installation of the kernel package.
• python-2.6.4.msi
The installer of the python kernel system 2.6.
• comtypes-0.6.2.win32.exe
The installer of Windows types, which are necessary to use Windows API-calls.
• numpy-1.4.1-win32-superpack-python2.6.exe
The installer of the NumPy package for numerical programming.
• scipy-0.8.0-win32-superpack-python2.6.exe
The installer of the SciPy package for sientific programming.
• matplotlib-0.99.3.win32-py2.6.exe
The installer of the MatPlotLib package.
• pywin32-214.win32-py2.6.exe
The installer of a little nice Python IDE.
The python kernel should be the first package, which is to install, because this installation sets up the
Python base folder. Within this folder you can find the folder Lib, which contents a lot of libraries and
additional packages and besides that a further sub folder called site-packages. Add ons are copied into
this folder by their installers or by the install Python script.
5
Page 6 Analysis of Structures - WS10/11
The start screen of the Python installer shows the Python version. You can select, whether the setup
should make Python available for all users or not. After clicking next you’ll get within the second form
the possibility to select the base folder of Python. By default Python uses the folder C:\Python26.
We overwrite the default and select the Windows standard program folder as starting folder, so we write
c:\Programme\Python261 .
The figures 1.1 and 1.2 show the input forms installing the Python kernel.
Figure 1.1: Start Screen of the Python Installer and Choice of Base Folder
If you want to use the Python language on a Windows system, it’s recommended to install the ComTypes
package. This package will give you a simple access to the Windows resources. It can be understood as
a wrapper layer, an interface layer for the access to Windows DLL-modules. ComTypes can be a help to
develop a software with a proper Windows look and feel.
The installation of the most Python packages will run very simular to the following installation. The
figures 1.3 show the first and second form of the installation procedure. The first form gives a few
1
The discussed installation was performed on a German system
E. Baeck
1.1. PYTHON, PACKAGES, UTILITIES Page 7
information to the package. The second form is usually used to select the Python version. Each installed
and supported Python version will be listed in the list box of the second form. You can select the desired
Python version and can go on with the installation procedure clicking the next button.
Figure 1.3: Start Screen of the Package Installer and Choice of the installed Python Version
NumPy [2] is the fundamental package for scientific computing in Python. It is a Python library that pro-
vides a multidimensional array object, various derived objects (such as masked arrays and matrices), and
an assortment of routines for fast operations on arrays, including mathematical, logical, shape manipula-
tion, sorting, selecting, I/O, discrete Fourier transforms, basic linear algebra, basic statistical operations,
random simulation and much more.
At the core of the NumPy package, is the ndarray object. This encapsulates n-dimensional arrays of
homogeneous data types, with many operations being performed in compiled code for performance.2
The installation runs like the installation of the ComTypes package (see figure 1.4).
Figure 1.4: Start Screen of the Package Installer and Choice of the installed Python Version
SciPy [3] is a collection of mathematical algorithms and convenience functions built on the Numpy ex-
tension for Python. It adds significant power to the interactive Python session by exposing the user to
2
For more details see NumPy User Guide available on the http://info.baustatik.uni-due.de
2.12.2011
Page 8 Analysis of Structures - WS10/11
high-level commands and classes for the manipulation and visualization of data. With SciPy, an inter-
active Python session becomes a data-processing and system-prototyping environment rivaling sytems
such as Matlab, IDL, Octave, R-Lab, and SciLab.
The additional power of using SciPy within Python, however, is that a powerful programming language
is also available for use in developing sophisticated programs and specialized applications. Scientific
applications written in SciPy benefit from the development of additional modules in numerous niche’s
of the software landscape by developers across the world. Everything from parallel programming to web
and data-base subroutines and classes have been made available to the Python programmer. All of this
power is available in addition to the mathematical libraries in SciPy.3
The installation runs like the installation of the ComTypes package (see figure 1.5).
Figure 1.5: Start Screen of the Package Installer and Choice of the installed Python Version
There are lot of IDEs available for the development of Python software. Most of them are commercial.
One very nice IDE especially for large development projects with a lot of python files is called Aptana.
Apatana is a special version of the free Eclipse IDE. For this IDE there is a plugin available, which is
called PyDev. To use this IDE first you have to install the basis version of Aptana and then you should
install the plugin and select the desired Python version, which should be installed before. An example
project within the Aptana is shown in figure 1.6.
PythonWin is very much simpler than Aptana and has the advantage, that really only the feature are
available that are nessesary to get started with small Python programs. The installation runs like the
installation of the ComTypes package (see figure 1.7).
After having installed the PythonWin IDE it’s recommended to set up a link onto the desktop (see figure
1.8).
3
For more details see SciPy Reference Guide available on the http://info.baustatik.uni-due.de
E. Baeck
1.1. PYTHON, PACKAGES, UTILITIES Page 9
Figure 1.7: Start Screen of the PythonWin IDE Installer and Choice of the installed Python Version
2.12.2011
Page 10 Analysis of Structures - WS10/11
Like in every computer language there is a Hello World application in Python also possible. We start
dhe PythonWin IDE and create a new file. We save this file as HelloWorld.py. With Ctrl-R the execution
form is shown and the execution mode should be selected (see figure 1.9).
• No debugging,
execution without debugging.
If the HelloWorld.py script is executed, the output is written into the Interactive Window, see figure 1.10.
One of Python’s advantages is the feature to execute only one statement within the Python shell. The
Python shell within the PythonWin IDE is given by the Interactive Window, see figure 1.10.
If we want to calculate the vertical position of a ball thrown up in the air with an initial velocity v0 after
a time t we get from the Newton’s law
1
y(t) = v0 · t − · g · t2 (1.1)
2
E. Baeck
1.3. PYTHON CALCULATOR Page 11
So, if we want to calculate the vertical position for an initial velocity v0 = 5m/s after 0,6 seconds, we
can calculate the y(t = 0, 6) with one simple Python call.
A second version of the calculation can be performed introducing and using variables as follows. So we
can check every Python command within the Interactive Window.
>>> v0 = 5
>>> g = 9.81
>>> t = 0.6
>>> y = v0*t - 0.5*g*t**2
>>> print y
1.2342
>>>
A third version of our calculation of the ball’s height could be a Python script, which we can load and
executed within the PythonWin IDE. To comment the code we insert some comments, which starts with
the # character. Characters at the right side of the # character are ignored by the Python interpreter.
2.12.2011
Page 12 Analysis of Structures - WS10/11
E. Baeck
2
Basics in Python
We have seen in section 1.3, starting with a Python calculation within the Python shell is very easy. We
can use simple formulas with numbers or we can use symbolic names to make it more readable. In
Python like in other programming languages there are some reserved word, which are used to build up
the language. This words can not be used as variable names.
If you want to use such a word it’s recommended to extent the name with an underscore, like vor example
break_ instead of break.
A package within Python is a container of software objects, global variables, functions and objects. In C
or Fortran, a package could be termed as library.
Packages should be imported into the Python session or into the Python source file, if external feature
should be used. A typical package import is the import of the mathematical package. This is necessary
if you want to call some basic mathematical functions like trigonometric functions or the square root. If
such a package is not imported, it’s objects, especially it’s functions, are unknown and can not be used.
Packages are like header includes within the C language. In C as well, external functions, whose headers
are not included, are unknown and can not be called.
13
Page 14 Analysis of Structures - WS10/11
A whole package is imported with the statement "import" module. The following example shows
the import of the mathematic package to apply the square root function. With the import statement the
module math will be linked. The square root function sqrt will be then available with the usual dot access
math.sqrt.
If we only want to import a symbolic name of a module (package), then we can import in a selective
way. The nex example shows the selective import of the function sqrt from the module math. If we do
this, then the function can be used without the prefixing module name.
If some names of of module should be imported with new names, the statement as can be used within
the import statement. The following example shows the import of the trigonometric functions sin and
cos with the new names s and c and the constant pi with it’s original name, to calculate the Cartesian
ordinates of a 45◦ point with radius 10.
You see, that we change the original name of the trigonometric functions with the as key word. Within
the formulas the functions can be used with it’s new names.
2.3 Operators
We have already seen, that Python also has it’s operators calculation the height of a vertical thrown
ball. Python uses the same precedence as we know form the mathematics. The power operation has the
strongest binding followed by the point operators (products and divisions) followed by the line operators
E. Baeck
2.3. OPERATORS Page 15
(plus and minus). Unary operators will always be applied first. To change the standard precedence of the
operators we use like in mathematics parenthesis to dictate the way a formula should be evaluated.
Unary operators are working only on one value, therefor unary. In Python there are three unary operators
available.
The bitwise inversion shows the internal representation of negative numbers. A negative number is
represented by the so called b-complement of a number. This is the complement, i.e. the bitwise inverted
number plus 1. So we get
−a =∼ a + 1 or ∼ a = −(a + 1) (2.1)
Python offers the following arithmetic operators. You should be careful with the usage of data types
especially within divisions. If you use integers, the result generally will be truncated.1
Like in C bit operators can be easily be used to manipulate a number’s bits. The following operators are
available2
1
The exception of the power operator all the arithmetic operators are used with the same symbol like in C. In C there is no
power operator available.
2
Python’s bit operators are exactly the some as the C bit operators.
2.12.2011
Page 16 Analysis of Structures - WS10/11
The left shift of a numbers bit by 1 is equal to a multiplication by 2. The right shift by one is the same as
a division by 2. The bitwise AND and OR operator are usually used to set or to clear a number’s bits.
The following example shows how to apply the shift operator. We start with the bit 0, which has the
value 1. Within a for loop (see section 2.7) the bit is shiftet subsequently to the left. So we create the bits
in the range from n to m. After shifting the bit, it’s value is printed into the console window.
If we want to manipulate a number’s bits it is obvious more clearly to use the hexadecimal representation
of a number as using the elsewhere usual decimal representation. Hexadecimal numbers starts with the
literal 0x3 and uses the digits 0-9 and A-F. F is with 15 the largest digit of the hexadecimal numbering
system. The hexadecimal numbering system has the advantage, that it packs 4 bits of a number into one
hexadecimal digit. So a byte can be represented by 2 hexadecimal digits. If we now be able to translate a
hexadecimal digit into a binary number, then we can see even the bits in the largest number without any
calculation.
In the following example we want to analyze the arbitrary number 27563. The bits are obviously very
hidden using the decimal representation. To get a hexadecimal representation we can simple print the
number using the X formating. We can see that we obviously use two bytes for this number, because wie
get 4 digits (6BAB). Furthermore we can see, that the leading bit is not set, because the largest digit is 6
and the highest bit in a digit has the value 8.
3
A binary number starts with the literal 0b and uses the digits 0 and 1, like 0b1000 = 810 .
E. Baeck
2.3. OPERATORS Page 17
>>> a = 27563
>>> "%X" %a
’6BAB’
>>>
The binary number representation is easily available from the hexadecimal representation, if we know
the binary representation of the hexadecimal digits4 .
So we get assembling the binary digits of 6BAB the following bit sequence.
If we now want to set the highest bit of the discussed number, we can use the bitwise OR operator | (see
section 2.3.3). A number with only the highest bit set we can obtain by shifting the first bit to the desired
position within the 2 bytes, i.e. we shift the bit 15 times. Now we can see that we get a hexadecimal
number with only the highest digit non vanishing. Within the digit of 8 the forth bit is set, which is the
highest of a have byte5 .
>>> b = 1
>>> b = b<<15
>>> b
32768
>>> "%X" % b
’8000’
If we now want to set the highest bit of our original number 27563, we simple can overlay it with the last
number 8000.
>>> a = 27563
>>> b = a | (1<<15)
>>> b
60331
>>> "%X" % b
’EBAB’
After having set the highest bit, we see that the decimal number has changed totally. However the
hexadecimal number only changes in the first digit. Instead of 6 we have now E. And E is represented
binary with
so we get
2.12.2011
Page 18 Analysis of Structures - WS10/11
Comparing the binary result with the binary result of equation 2.2 we see that obiously only the first bit
is set as wanted.
How we can now clear a bit of a number? Clearing a bit of a number uses two steps. First we have to
create the inverse of the filtering number, having set only the desired bit. And within a second step we
use the AND operator & to overlay bitwise the inverse of the filtering number and the number, whose bit
should be cleared. In our example we want to clear the highest bit of the first byte. The filtering number
we get shifting the 1st bit 7 times.
>>> a = 27563
>>> b = a & (˜(1<<7))
>>> b
27435
>>> "%X" % b
’6B2B’
We also notice, that the decimal representation has changed widely after the clearing of the bit on the
contrary to the hexadecimal.
Boolean operators are used to branch and to make decisions. The comparing operators are identical to
the C comparing operators.6
The result of a boolean expression like above are the boolean values False or True. To combine
comparing expressions the following logical operators can be used.7
E. Baeck
2.4. PRINT AND OUTPUT FORMATS Page 19
With the membership operators you can check whether a value or an object is part of sequence of objects.
With the identity operators you can check the identity of two objects.
If you want to print data into the console window, you have to think about formating. The formating
sequences are very similar to the formating sequences of the C printf function. The formating is a so
called escape sequence within a string, which is started with the % operator.
• formating an integer
An integer (independent of the data type) is formated by the escape %d for decimal representation
and %x or %X for hexadecimal representation.
• formating a float
A float is formated by the escapes %f, %e, %E, %g and %G
2.12.2011
Page 20 Analysis of Structures - WS10/11
• formating a string
A string is formated by the escapes %s
A leading number n within a format %nT, with T the type of the format, sets up the width of the output.
The following example shows the formating of an integer in decimal and hexadecimal mode. At the
hexadecimal format a lower x sets lower digit letter, the capital X sets capital digit letters.
Formating floats there are two different formats available, the fixed format and the exponential format,
which is also called scientific format. The f format sets a non exponential representation. The e or E
format sets a exponential format. e uses a small e letter, and E uses a capital E letter. The g or G format
sets an optimized representation, i.e. a fixed or an exponential format, depending on the outputs length.
The number after the dot sets the number of digits after the comma for f and e format, it sets the number
of significant digits for the g format.
Recording to the available data types, Python is very different comparing it with common languages like
C, Fortran and Basic. Most of the languages offers the programmer data types, which are one by one
related to the underlaying hardware.
So for example Fortran and C offer 2 and 4 byte integers on 32 bit operating systems by default8 On a
64 bit operating platform a long integer of 8 bytes will be available. On the other hand there are 4 and 8
byte floats available.
Python however offers on 32 bit platforms a normal integer of 4 bytes, which is directly related to the
hardware, for example 11234, and furthermore a so called long integer, for example 1234L, which is
handled by the Python software. The long integer, which is marked by a succeeding L, is only restricted
by the computers memory, that means that a really incredible number of digits can be considered. Later
we will calculate the factorial of a incredible high number.
Furthermore Python as already mentioned offers only one float data type with 8 bytes. The standardized
4 byte float is not supported, for example 1.23 or 1.23e+2.
Python also supports a complex arithmetic with an complex data type, consisting of two floats for real
and imaginary part of the complex number. The complex unit in Python is called j. Therefor the complex
number 1 + 4i will be represented in Python with 1 + 4j .
8
That means without applying provider depended tricks.
E. Baeck
2.6. CODE BLOCKS Page 21
The last data type used in Python is a string consisting of one or more characters.
The data type of a variable can be determined using the build in function type, as shown in the following
example. Within a first step different variables were created by a simple assignment. The content of the
variable determines the type of the variable, no explicit declaration is needed or available, like in C. After
having created the variables the type of the variables will be determined by subsequent type calls.
To check the data type within a program the following tests can be made.
1. if type(d).__name__ == ’int’
You can check the type with the types __name__ member.
2. if type(d) == int
... or you can check the type with the type class name (discussed later).
>>> a = 2
>>> b = 3L
>>> c = 4.5
>>> d = 6 + 7j
>>> e = "Hello World"
>>> type (a)
<type ’int’>
>>> type (b)
<type ’long’>
>>> type (c)
<type ’float’>
>>> type(d)
<type ’complex’>
>>> type(e)
<type ’str’>
You see, ’int’ is integer, ’long’ is long integer, ’float’ is float, ’complex’ is complex and
’str’ is string data type.
Furthermore there are some sequences in Python available, which combines the mentioned data types in
a more or less sophisticated mode. More about that later.
One very imported feature of Python is, that code blocks are bracketed by an unique indent. The most
programming languages uses there specific code parenthesis. There is one opening parenthesis which
starts the code block and there is one closing parenthesis, which closes the code block.
The following example shows a code block in C.
if (a > b)
{
c = a + b
d = a - b
Further Lines of C-Code
}
2.12.2011
Page 22 Analysis of Structures - WS10/11
if (a .gt. b) then
c = a + b
d = a - b
Further Lines of Fortran-Code
endif
Compared with this in Python the code block is bracketed by indent as follows.
if a > b:
c = a + b
d = a - b
Further Lines of Python-Code
a = b
One statement which uses a code block, in this case an if statement, is closed by a colon. After the colon
an unique indent for the lines of the code block must be used. If not, it will be a syntax error. The code
block is closed, if the last line of the code block is the last line of the whole code, or is closed by a line
of code which is indented like the opening statement. In our example the assignment a=b has the same
indent as the if statement and so this line will be the first line of code outside our code block.
Like all programming languages, which make sense, Python also has some implementations of repe-
titions, of loops. Like in C an explicit loop is available - the for loop - as well as an implicit loop is
available - the while loop.
The for loop is controlled by an iterated set. One very common variant is the for loop, which is controlled
by an iteration counter. The iteration counter will be configured by a range object. The range object has
3 parameters9 . The first parameter sets the start value of the iteration counter, the second parameter sets
up the iteration value, which will be the first value that is not performed. The third parameter sets up the
increment.
The following typical example for the usage of an iterative for loop implements the calculation of the
factorial.
n
Y
n! = i (2.7)
i=1
The implementation of the factorial is given below. Note the importance of the indent, see section 2.6.
9
A parameter is a information unit, which is passed to the called object. If more then one parameter is passed, the parameters
are separated by commas.
E. Baeck
2.7. LOOP FOR REPETITIONS Page 23
n = 10 # factorial input
p = 1 # result variable must be initalized by 1
for i in range(2,n+1): # the counter runs from 2 up to n
p *= i # here we perform the product
print "%3d! = %10d" % (n,p) # write the result into the console
The second loop type, the while loop is working implicit with a boolean expression, which controls the
break condition. If we want to implement the factorial using the while loop we get the following code.
n = 10 # factorial input
p = 1 # result variable must be initalized by 1
i = 2 # the counter runs from 2 up to n
while i <=n: # loop with break condition
p *= i # perform the product
i += 1 # explicit incrementation
print "%3d! = %10d" % (n,p)
The next example shows a nested loop. Very important is the correct code block indent.
For the detailed controlling of the loops cycles two statements are available.
• continue
If the continue statement is used a cycle is immediately stopped and the next cycle is started.
• break
If the break statement is used a cycle is immediately stopped and the loop is exited.
The next example shows an application of the continue statement. A loop is performed with the values
0 · · · 4. The cycle with the counter 2 is prematurely canceld.
2.12.2011
Page 24 Analysis of Structures - WS10/11
One very interesting feature of Python is the long integer arithmetic. So we can calculate incredible large
factorials. Figure 2.1 shows the code in the upper window. The console window shows the result. A
number with a lot of digits and every digit is exact.
The next example shows the calculation of the factorial using a float. The float factorial can only be
evaluated up to 170! = 7.25742e+306. In the case of 400! we will get an overflow, because the
exponent exceeds the available memory in 8 bytes (see figure 2.2).
E. Baeck
2.8. BRANCHES FOR DECISIONS Page 25
Decisions are made by the usage of the if statement. The if statement is defined as follows.
...
else
code block else
The calculation of roots of a quadratic equation is a nice and simple example to show the application of
the if statement. The quadratic equation is given by
a · x2 + b · x + c = 0 (2.8)
If we want to solve the quadratic equation, we have to analyze the available cases. A general solution
must handle the following cases.
• constant case
a=b=c=0
• linear case
a = 0, b 6= 0
• quadratic case
a 6= 0
• no solution case
a = b = 0 and c 6= 0
2.12.2011
Page 26 Analysis of Structures - WS10/11
The implementation uses a real arithmetic importing the module math. The solver of the quadratic
equation is implemented in a function called QuadSolve. The function analyzes the different cases and
returns in the constant case only a comment, in the linear case the solution value and a comment. In the
quadratic real case 2 values and a comment were returned and in the complex quadratic case there are 4
return values and a comment. All return values are packed into a tuple. The case can be identified form
the calling program using the function len, which returns the number of items of a tuple. To avoid the
testing for zero, which would produce numerical problems in principle, we set the relative precision to
10−15 . An absolute value less then this relative precision is treated as zero value.
’’’
solution of a quadratic equation
- handling all special cases -
’’’
# import the used functions
from math import fabs as abs, sqrt
def QuadSolve(a,b,c):
p = 1.e-15 # precision of float, is used to
# avoid to test for zero with ==
# case: a=0
if abs(a) < p:
# case: b=0
if abs(b) < p:
# case: c=0
if abs(c) < p:
return ("Infinit number of solutions.",)
# case: c!=0
else:
return ("No solution possible.",)
# real case
if d >= 0.:
E. Baeck
2.8. BRANCHES FOR DECISIONS Page 27
s = sqrt(d)
x1 = 1./(2.*a)*(-b +s)
x2 = 1./(2.*a)*(-b -s)
return (x1,x2,"Real case of the quadratic problem.")
# complex case
else:
s = sqrt(-d)
x1r= -b/(2.*a)
x2r= x1r
x1i= s/(2.*a)
x2i=-x1i
return (x1r,x1i,x2r,x2i,"Complex case of the quadratic problem.")
# linear case
elif values == 2:
print "x = %f, info: %s" % result # (result[0],result[1])
2.12.2011
Page 28 Analysis of Structures - WS10/11
The return values are packed into a tuple (see section 2.10.1). The tuple is used in the set order. So we
can use it for the string format. Reordering is not needed. The output into the console window is the
following for the selected test example.
2.9 Functions
A function is a callable type. A function starts with the def command. The parameter list succeeds the
function name and consists of names separated by commas. The return of return values is optional. A
nice example to study cases of a solution and their specific return values is discussed in section 2.8.
The following code shows a special version of the abs function. The data type is checked first. Only int,
long or float type are senseful supported. We check the type with the type function. The type function
returns a type object. Within the function we check the object member __name__. If the type is not
supported, a error string is returned. If the type is supported, the return value is return. The calling
program checks the return values type using the object name (int, long and float).
# input section
y = -4 # test 1
# y = "hello" # test 2
# function call
z = MyAbs(y)
E. Baeck
2.9. FUNCTIONS Page 29
t = type(z)
if t == str: # second version to check the type
print z # print error message
else:
print "Absolute Value of %f = %f" % (y,z) # print result
End of Coding
There are three possible situations to handle within the iteration loop.
• The function value is vanishing with respect to our selected precision. The iteration loop will be
broken and the found result is passed back to the caller.
• The slope of the function is vanishing. This situation can not be handled by the simple iteration
scheme. The iteration will be broken with an error message.
• During the iteration each cycle is counted. So the iteration loop will be broken, if the maximum
available iterations are reached. The actual values and an error message is passed bake to the caller.
• fs, the function which calculates the slope of a given function numerically.
2.12.2011
Page 30 Analysis of Structures - WS10/11
i = 1 # iteration counter
while True:
E. Baeck
2.9. FUNCTIONS Page 31
2.12.2011
Page 32 Analysis of Structures - WS10/11
• Tuples are fixed sequences, which are only extensible. The advantage of tuples is the performance.
The following example shows the creation of a tuple. An empty tuple is declared. We extend the tuple
with a one element tuple - note the comma. The second extension extends the tuple with a two element
tuple.
Note, that tuples are input data for complex string formatings.
With the function tuple() a tuple can be created from an iterable object like string or list.
>>> T =tuple([1.,2.])
>>> T
(1.0, 2.0)
>>> L = list(T)
>>> L
[1.0, 2.0]
>>> T = tuple("Hello World")
>>> T
(’H’, ’e’, ’l’, ’l’, ’o’, ’ ’, ’W’, ’o’, ’r’, ’l’, ’d’)
E. Baeck
2.10. DATA SEQUENCES Page 33
Note, that the data of a list can be converted into a tuple using the function tuple() and reverse into a
list using the function list().
Lists are more flexible as tuples. Creating and extending lists can be coded like in the case of tuples.
The only difference is, that lists uses in their declaration the [..] parenthesis. So using lists the code
example of the section 2.10.1 can be coded like follows.
You can read somewhere, that tuples are much more faster than lists. This is surely not valid for every
problem. So it’s recommended to check the performance explicitly before you decide to use tuples or
lists.
The list object offers a wide set of methods. Some of them are discussed in the following table.
2.12.2011
Page 34 Analysis of Structures - WS10/11
Now, how can we implement a stack with basic Python? We simply need a list instance with the two
functions append(), which implements the Push and pop() which implements the Pop of a stack.
A dictionary is a powerful and very general container, it’s also called map, because a key value is mapped
onto the pointer of the stored item. In Python an instance pointers is stored in a dictionary using an
arbitrary key strings. So a dictionary is like a list container with a more general access. Because the
dictionary commonly hashes the key information onto some smaller index lists, the dictionary commonly
has a better performance as a linked list container. The dictionary therefore is used in the abaqus class
library (we will discuss it later) to store named instances.
A dictionary can be created like a list using curled parenthesis. Each key-value pair is separated by a
comma. The key is separated from the value by a colon.
10
If we save data on a stack, it’s called push data onto the stack. If we take data from the stack, it’s called pop data from the
stack.
E. Baeck
2.11. RANDOM NUMBERS Page 35
Because a computer is system, which works systematically reproducible, a computer will not be able to
create real random numbers. If we start a program twice with exact the same input values, the result also
will be the same. But with a little trick, we can produce so called pseudo random numbers. If we use
the time as input for the initialization of our random number generator, we will be sure, that it will be
extremely improbable, to get twice the same random number set.
So every programing language has it’s own random number generator. Python offers us a build-in pack-
age, which is called random. Like in C the random number generator is to initialize with a function
seed(). This function uses the computer time information to make varying starting conditions.
The following example will discuss two features of the Python library.
• How can we use the random package to create random numbers or to create a random order of
lists, i.e. shuffle the list’s data.
• How can we create simple 2d diagram plots using the matplotlib package.
• At the beginning the lists xPos and yPos are filled with random numbers. This random x and y
values are interpreted as point coordinates. Two subsequent points are joint by a line.
• Within a second step the list’s element are sorted. The result is plotted again. The result is an
increasing function.
• Within the third step the sorted lists are shuffled by the random package. The random order of the
random numbers is plotted again.
Figure 2.6 shows the walk for random points (blue line), for sorted points (red line) and for shuffled
points (green line). The left figure shows the content of the created png file, the right figure shows the
plot within the viewers window.
2.12.2011
Page 36 Analysis of Structures - WS10/11
numbers = []
for i in range(count):
r = lower + (upper-lower)*random.random()
numbers.append(r)
return numbers
# random positions
xPos = GetRandomNumbers(L,U,xN) # ...in x-direction
yPos = GetRandomNumbers(L,U,xN) # ...in y-direction
E. Baeck
2.11. RANDOM NUMBERS Page 37
2.12.2011
Page 38 Analysis of Structures - WS10/11
Date and time functions in common are very important and therefor Python supports this with a stan-
dard package datetime. Within this package you will find some very simple and powerful objects and
functions.
The package is loaded with the following statement. We load from datetime the subpackage datetime
and set the alias name time. The second line shows, how to get the actual time. The datetime object
contents from the left to the right the year, the month, the day, the hour, the minutes, the seconds and
the microseconds. Because the computer does not support microseconds but milliseconds the measured
milliseconds are multiplied with the factor 1000.
To implement some performance checking code it is very useful to have a timespan, which is the dif-
ference of two datetime objects. The timespan object is called timedelta and is as well an object of the
datetime package.
If we now want to check the performance of a code snippet, we will implement the following code.
First we have to import the datetime package. Then we get the actual time and assign this object to the
variable t1. Then the code to check follows. Within a last step we again create a datetime object calling
the time.now() function and assigning the object to the variable t2. If we subtract t1 from t2 we
will get a timedelta object. The timedelta objects contents the number of days, the number of seconds
and the number of microseconds of the time difference.
If we want to have the time difference in seconds, we have to write a little function, which adds all this
time parts in the unit second. If we suppose, that T is a valid timedelta object, we extract the seconds with
the seconds attribute and assign it to our seconds variable. The next part handles the microseconds.
We divide them by 106 and add the part to the seconds variable. The last part calculates the seconds of
the given hours and add this value to our second variable. The value is returned to the caller.
def GetTimeSpanInSeconds(T):
secs = float(T.seconds)
secs += float(T.microseconds)/1.e6
secs += float(t.days)*24.*60.*60.
return secs
One very important application of the timedelta object is the measurement of a program’s performance.
The next example shows how to investigate the performance of the creation of a tuple and the creation of
a list. Here we apply the above discussed function GetTimeSpanInSeconds.
Investigating the Creating Time of Tuples and Lists using a Function Approch
E. Baeck
2.12. DATE, TIME AND TIMESPAN Page 39
End of Coding
The console output on a one year old double processor notebook is given below. You see that it is very
wasteful to create a tuple with a lot of items concatenating them with a += operator. On the other hand a
list with the some number of items is created in no time. And the conversion of a list into a tuple costs
nothing. So we can resume the the creation of a very large tuple should be done by creating a list and
2.12.2011
Page 40 Analysis of Structures - WS10/11
c:\CM\Cm-AoS\WS1011\Python>Timecheck1.py
Creation of a tuple with 100000 items: 33.297s
creation of a list with 100000 items: 0.063s
Create tuple from list: 0.000s
A file is a sequence of bytes written to an external media like a hard disk, a CD or an USB stick. A file’s
bytes are indexed with an integer. So the the fist byte of a file has the index 0 and the second byte the
index 1 and so on. A text file is structured with a line break character \n. If a text line should be read,
the io11 system reads all the bytes up to the next line break.
Python as a lot of functions which are able to handle every situation concerning files. So files can be
created. We can read data from a file or write data into a file. We can close a file. We can delete a file or
change it’s permissions.
Within Python a file is an object an has a lot of attributes and methods (see also the next section). The
access to a file is initialized with the file constructor. The file constructor needs the name of the file and
the type of access. The file access type is like C’s access type of the function fopen12 . The type ’r’ is
used to read from a file, the type ’w’ is used to create a new file for writing. An old still existing file
will be deleted. The type ’a’ is used to write appending to an existing or a new file. If we want to read
from and write into a file the type ’w+’ can be used.
f = file(<filename>,<access-type>)
...
... example ...
...
f = file("myfile.txt", "w")
The file method write writes a stream of bytes into a file. In contrast to the print function the write method
adds no carriage return to the file. If a line of text should be written, the text should be closed with an \n
character.
f.write(<byte-stream>)
...
... example ...
...
f.write("This is my first line\n")
11
io means input output.
12
The function fopen is C’s classical function to open a file. The return will be a pointer to a FILE structure.
E. Baeck
2.14. OOP WITH CLASSES Page 41
f.close()
...
... example ...
...
f.open("log.txt","a")
f.write("Write a little log line\n")
f.close()
An instance of a class, that is the realization of the class in memory, is created simply by assigning the
class’s name followed by the constructors parameter list to a symbolic name, which than is the pointer or
the reference to this instance. Within the class the self pointer to the actual instance is called self. To
access member attributes or methods of a class the dot notation is used, i.e. <instance>.<member>.
We can remove an instance of a class by calling the del operator (del example). The references
to an instance are handled by the python runtime system. If an instance has lost all it’s references, the
instance is removed from the memory.15
UML structure diagrams of the emphasize the things that must be present in the system being modeled.
Since structure diagrams represent the structure they are used extensively in documenting the architec-
ture of software systems. In our description of the examples we want to implement we use the Class
Diagram which describes the structure of a system by showing the system’s classes, their attributes, and
the relationships among the classes.
A UML class diagram (see figure 2.7) consists of a rectangular box, which is di- Class Name
vided into three sections. The fist section contents the class’s name. This name attribute 1
attribute 2
is written centered in bold letters. The second section contents the attribute’s method 1
names of the class and the third section contents the method’s names. method 2
Figure 2.7: A UML
Class Diagram
13
Object orientated Programming is often used with the abbreviation OOP.
14
The Unified Modeling Language includes a set of graphic notation techniques to create visual models of software-intensive
systems. The Unified Modeling Language is an international standard see [4], UML 2.3 was formally released in May 2010.
15
This is also called Garbage Collector. In contrast to this in poor C or C++ an once allocated object has to be removed
explicitly from the memory to avoid so called memory leaks, this are blocked parts of the applications memory, which remain
unaccessible until the application is closed.
2.12.2011
Page 42 Analysis of Structures - WS10/11
A UML note diagram (see figure 2.8) consists of a stylized note sheet which is This is only
filled with some information. a simple Note
At the ends of the connecting lines the multiplicities are noted. The multiplicity gives the range of
referenced instances in the form from..to. For the Class A we have 2 up to infinite instances in an
composition, therefor at the end of the line we can not have a multiplicity of zero. In our example
we have exactly one instance of the class 1. On the other hand Class B is referred to Class 1 within
an aggregation. In our example on instance of Class B can be reverenced by an undefined number of
instances of Class 1. This is shown by the * icon. On the other hand the class 1 references at least on
instance of the Class B. Otherwise the number of references is arbitrary. This is also shown by the *
icon.
E. Baeck
2.14. OOP WITH CLASSES Page 43
In section 2.12 we have discussed a little program which implements a time checker using a stack con-
tainer. We can implement this problem in a much more clearer version, if we do it in terms of the OOP
approach. The class TimeCheck, which is introduced, encapsulates the time stack list and the log files
name as attributes and the following methods.
• Set reads the actual time with the datetime.now() and pushes it onto the time stack.
• Get reads the actual time with the datetime.now(), pops the the last time object from the stack and
calculates the timespan. The timespan is optionally printed into the console window or into a log
file.
• GetTime calculates the seconds in floats form the passed timedelta object.
The main program, which is used to discuss the class follows the steps of the classical implementation
of the problem discussed in section 2.12. We first create an empty tuple and an empty list. Than we
create the TimeCheck instance16 simple by assigning it’s name to a variable (s = TimeCheck()). To
push a time object onto the stack, we call the class’s member function Set. So, if we want to check the
performance of a code, we first call the Set method and after having executed the code, we call the Get
method. The Get method will print the comment and will return the used seconds of execution time in
floats. The simplest kind of an implementation is the implementation in the file of the main program.
This you can see in the following example.
TimeCheck
The class TimeCheck contents
self. log: name of the log file all the data and functions to
self. stack: stack list use a time stack and to get the
self. init (..): contructor time-span information in a proper
self.Set(..): push a time float format. The class is able to
self.GetTime(..): calculate seconds present a formated output of the
self.Get(..): pop a time measured time-spans.
’’’
Class to investigate the timespan, to measure the performance
by implementing a time stack container
’’’
# - module - member alias
from datetime import datetime as time # we need the datetime package
# constructor
16
A physical created object in memory of a class is called instance of a class.
2.12.2011
Page 44 Analysis of Structures - WS10/11
tStart = self. stack.pop() # pop the time object from the stack
tEnd = time.now() # get the end time
tDel = self.GetTime(tEnd -tStart) # calculate the timedelta in floats
E. Baeck
2.14. OOP WITH CLASSES Page 45
If we run the program, we see that two time stamps are indented with one dot. This timestamps are set
for inner performance measurements, that is the time-stamp for the creation of the tuple and after that
the time-stamp for the the creation of the list. The last time-stamp shows the total execution time and is
created by the first push, i.e. the first Set call and the last pop, i.e. the last Get call.
>> Start..
. 8.282 : Creating 50000 item tuple.
. 0.016 : Creating 50000 item list.
8.329 : total time
2.12.2011
Page 46 Analysis of Structures - WS10/11
E. Baeck
3
Python Projects
We have discussed the newton’s scheme in section 2.9. Within this section the project’s code will be
extended by plot features. The function and it’s derivative as well as the path of iteration will be plotted
using the PyLab package.
2. The function GetFunctionValues creates a list of the function values for the plot routine.
3. The function GetFunctionDerivativeValues creates a list of the function’ derivative values for the
plot routine.
4. The function Newton will be extended by two lists, which should store the values of the iteration
path. After each iteration step, the x-value and it’s function value are stored for later drawings.
47
Page 48 Analysis of Structures - WS10/11
def newton(f,x0,e=1.e-10,h=1.e-6,imax=100):
i = 1 # iteration counter
while True:
if abs(f0) < e:
break
E. Baeck
3.1. NEWTON, STEP2 Page 49
return yL
return yL
2.12.2011
Page 50 Analysis of Structures - WS10/11
After the output lists are created the lists are passed to the plot postprocessor by the plot method of
PyLab. After enabling the grid the plot data are written in a graphic file and are visualized in the PyLab
viewer application.
Figure 3.1 shows the iteration path for the equation f (x ) = x 2 − 1 with an starting value of 4 with red
dots.
E. Baeck
3.2. PROFILES, THIN WALLED APPROACH Page 51
In this section we will implement a class hierarchy to model a profile’s data. We will implement logging
into file and screen in a common base class and we will implement the thin walled approach to analyze
and calculate the profile’s section properties.
So if we want to develop a software, which is able to calculate the profile’s section properties like it’s
area, moment of inertia and so on, we can consequently split up the problem in general and specific parts.
So we start with the data and the functions of a general object. This data is collected in a class which
we call Base. So if we want to describe a wide range of profile types, then we should think about the
profile’s common features. Every profile has a name and therefore this name is a common feature. This
common features are collected in a general profile class which is called Profiles. If we want to handle a
specific profile type like an U-profile ore a tube profile, we collect the specific type dependent features
in the classes UProfile and TubeProfile.
Base
log: global log file name The class Base contents
count: global instance counter common features which are used
self. init (..): constructor in every class.
self.AppendLog(..): write to log
Profile
self. name: The profile’s name
self. PL: Point container
self. LL: Line container
self. init (..): constructor The class Profile contents
self.List(..): List profiles values all common features of a profile
especially the containers for the
self.AddPoint(..): insert a new Point Points and Lines of shape modell
self.AddLine(..): insert a new Line
self.GetLength(..): distance between to points
self.GetArea(..): calculate profile’s area
self.View(..): views a profile plot
UProfile
self. h: profile’s height
self. w: profile’s with The class UProfile contents
self. t: profile’s thickness the special features of a U-Profile,
self. init (..): constructor especially the geometric parameters.
self.List(..): list profiles values
self.Create(..): create thin walled model
TubeProfile
self. d: profile’s diameter The class TubeProfile contents the
self. t: profile’s thickness special features of a Tube-Profile,
self. init (..): constructor especially the geometric parameters
self.List(..): list profiles values
Figure 3.2 shows in the left column the UML class diagrams, which starts with the name of the class.
The second box contents the classes attributes and the third box contents the classes methods. The right
column contents note diagrams. A note diagram is like a help page which gives some comments and
2.12.2011
Page 52 Analysis of Structures - WS10/11
elucidations.
If we want to develop reusable code, it’s recommended to create a separate file for every class and put
them into a package. A package is a folder with the package’s name. So we introduce a folder called
Profiles. To initialize a package we have to create a file named __init__.py in this folder. If the
package is loaded, the __init__.py is executed.
In our Profiles class Base we implement the common features of our package classes. The class Base
counts the instances using the global class attribute __count. A common logging method is imple-
mented. So the name of the log file is implemented as a class method (not an instance method) __log.
The method AppendLog is implemented as a general logging function. The actual time is called an
copied as heading for our logging comment. Then the completed logging text is written into the log file
and into the screen.
’’’
Base class for general attributes and methodes of the packages’s classes.
Every class of this package will be derivated from this class
’’’
# we need the datetime for the log’s timestamp
from datetime import datetime as time
class Base:
E. Baeck
3.2. PROFILES, THIN WALLED APPROACH Page 53
End of Coding
If we want to use the code of an existing class within a new class, we can inherit this old class and use
it as a base class. That means, that the new class contents also the attributes and the methods of the old
one. A base class is used in the class statement as an argument. A class can inherit more than one class.
Because we implement the new class Profile within the Profiles package, we only have to import the
module base, i.e. the file base.py. From this module we import the class Base. So we can write the
import statement as follows.
The class profile now contents common profile features. One of them is the profile’s name. Note,
if we inherit a class from a base class, we have to call the base class’s constructor method. Every
attribute and every method of the base class is now available. A second feature of the Profile class is the
implementation of the thin walled model. Therefore we introduce a line and a point container using the
AList class. With a specific method we insert point and line instances into the container. To calculate the
section values of the created thin walled model we introduce a method to calculate the distance between
to points and a further method to calculate the line’s area, which is given by the product of line length and
line thickness. At least we introduce a little viewing method, which should create a png picture file of the
profile and should optionally start the pylab viewer tool. To be sure, that the environment supports the
Tkinter package1 , which is used by the viewer, we import the package using a try: ... except:
handler.
’’’
General Profile class, derivated from the package’s Base class
’’’
from Base import Base # the profile’s base class
from AList import AList # container for points/lines
from Point import Point # a point class
from Line import Line # a line class
1
We will see later trying to apply our package Profiles in the frame of the Abaqus scripting, that we will get problems,
because the Abaqus environment is not developed with Tkinter.
2.12.2011
Page 54 Analysis of Structures - WS10/11
def AddPoint(self,No,y,z):
p = Point(No,y,z) # create a new point
self. PL.Add(No,p) # add the point to list
def AddLine(self,No,nA,nB,t):
E. Baeck
3.2. PROFILES, THIN WALLED APPROACH Page 55
except:
self.AppendLog("*** Error: yplab could not be loaded!")
return
# plot point
for line in lines:
pylab.plot(line[0],line[1],’rp’)
if viewer: pylab.show()
End of Coding
If we now want to use the package we have to create a new main program. The main program to show
the usage of our new classes is given below.
u100 = prof("U100")
2.12.2011
Page 56 Analysis of Structures - WS10/11
u120 = prof("U120")
End of Coding
Base
log: global log file name
count: global instance counter
self. init (..): constructor
self.AppendLog(..): write to log
Profile
self. name: The profile’s name
self. PL: Point container
self. LL: Line container
self. init (..): constructor
self.List(..): List profiles values
self.AddPoint(..): insert a new Point
self.AddLine(..): insert a new Line
self.GetLength(..): distance between to points
self.GetArea(..): calculate profile’s area
self.View(..): views a profile plot
UProfile
self. h: profile’s height
TubeProfile
self. w: profile’s with
self. d: profile’s diameter
self. t: profile’s thickness
self. t: profile’s thickness
self. init (..): constructor
self. init (..): constructor
self.List(..): list profiles values
self.List(..): list profiles values
self.Create(..): create thin walled model
The simplest approach to calculate the profile’s cross section values (area, moments of inertia, the center
of mass, etc.) is given by the thin walled model, a one dimension approximation of the profiles geometry.
The profile is described only by points and lines like in the finite element approach. Therefore we
introduce a point as well as a line class (see figure 3.4). To hold the desired point and line information
we implement a slightly modified list class, which should work like an array with a well defined index
access. If we use a standard list class, we will get problems if we want to insert an object at a position
outside the list range. Then a standard list object simply appends this object as a new item at the tail
of the list and we would loose the correct index position. So we implement a new Add method, which
checks the list length and if necessary enlarges the list up to the desired index position.
Figure 3.5 shows the profile class’s UML diagram with the composition of the point and line class. The
UML diagram shows, that thin-walled model consists of minimally 2 points and one line. The code of
the Point class is given below.
E. Baeck
3.2. PROFILES, THIN WALLED APPROACH Page 57
Point
n: point number The class Point contents
x[]: List for point coordinates the coordinates and the number of
self. init (..): constructor a profile’s point.
self.List(..): print point data
Line
n: line number
p[]: List of point numbers The class Line contents
t: line’s thickness the line’s point data and the line’s
L: line’s length thickness.
self. init (..): constructor
self.List(..): print line data
AList
no attributes The class AList is based on a
self. init (..): constructor a list. The Add method adds an object
self.Add(..): Add by index with a given index value.
self.List(..): print instance’s data
Profile
self. name: The profile’s name
self. PL: Point container
self. LL: Line container
self. init (..): constructor
1
self.List(..): List profiles values
self.AddPoint(..): insert a new Point
self.AddLine(..): insert a new Line
self.GetLength(..): distance between to points
self.GetArea(..): calculate profile’s area
self.View(..): views a profile plot
1
1..*
Line
2..*
n: line number
Point p[]: List of point numbers
n: point number t: line’s thickness
x[]: List for point coordinates L: line’s length
self. init (..): constructor self. init (..): constructor
self.List(..): print point data self.List(..): print line data
2.12.2011
Page 58 Analysis of Structures - WS10/11
from Base import Base # the Point class is inherited from the Base
class Point(Base):
def init (self,no,y,z): # The Point is created passing the point number
Base. init (self) # and the coordinates. The constructor of the
# Base class is called.
self. no = no
self. x = [y,z] # the coordinates are stored in the lis
End of Coding
class Line(Base):
self. No = No
self. p = [nA,nB] # the point numbers are store in a list
self. t = t
self. L = L # the line length is calculated according to
# the thin walled approach
def List(self): # the list method calls the base class’s AppendLog
self.AppendLog(" Line: %3d, A= %3d, B= %3d, t= %7.3f" % \
(self. No,self. p[0],self. p[1],self. t))
End of Coding
The following code shows the code of the container class ALast. In a first version the class AList inherits
the content of the class Base. The python buildin container class list is used as an attribute _L. The
access to the container is handled with access methods.
E. Baeck
3.2. PROFILES, THIN WALLED APPROACH Page 59
class AList(Base):
End of Coding
The following code shows the code of the container class ALast. In a second version the class AList
inherits besides the class Base the buildin class list. The advantage comparing this version with the
member implementation of a list is, that the class AList will have exactly the same interface a the buildin
class list.
from Base import Base # the AList class should inherit the Base class
2.12.2011
Page 60 Analysis of Structures - WS10/11
To check the package code, we create an instance of the Point and the Line class. The instances’ data
are printed with their list method. Then a AList instance is created and the Point and Line instances are
inserted into to AList instance. Then the AList’s data are printed.
Within a second step a Profile instance is created. We add two Point instances and one Line instance to
Profile instance and print the Profile instance’s data.
’’’
Test program for Point, Line and AList class
’’’
# -package-module class
from Profiles.Point import Point
from Profiles.Line import Line
from Profiles.AList2 import AList
from Profiles.Profile import Profile
E. Baeck
3.2. PROFILES, THIN WALLED APPROACH Page 61
End of Coding
The following output will be written to screen and into a file, if the check script will be executed. The
output shows, that first the package is loaded with the control string of the __init__.py module. If
an instance is created the instance counter is logged.
2.12.2011
Page 62 Analysis of Structures - WS10/11
To next script implements a little testing environment for the usage of the classes UProfile and Profile
of the Profiles package. A UProfile instance will be created with the data of an U100 profile. The
thin walled model will be created automatically within the constructor of the UProfile instance. Then
the whole data of the profile will be written to the screen and into the log file using the base classes
AppendLog method. Then the area of the profile model will be created and printed. At last using the
View method of the inherited class Profile a picture file is written and the PyLab viewer is started.
Figure 3.6 shows the viewers window as well as the picture file’s content. The blue lines are the center-
lines of the profile parts, i.e. the lines. The red dots are the points of the thin walled model. We see that
height and width are reduced by the thickness values.
’’’
The test environment implements the creation
of a U100 profile, the listing of it’s data
and the creation of it’s model picture
’’’
# package module class
from Profiles.UProfile import UProfile
# and check it
prof.List()
To implement a simple profile data base for arbitrary profile instances, we can simply use the Python
buildin object dict 2 and extend it by inheritance with a specific list method (see figure 3.7). The list
iterates the dictionary getting the keys of the stored instances. With the given key we get the instance
pointer from the dictionary. Then we can use the mechanism of the polymorphism and call the instances
specific list method. If there is an error occurring, the try/except error handler will do his job and
executes the except branch code. So we can avoid program crashing due to invalid ore missing pointers.
2
A dictionary is used to store instance pointers with an arbitrary access key.
E. Baeck
3.2. PROFILES, THIN WALLED APPROACH Page 63
ProfDB
no attributes The class ProfDB is based on a
a dictionary. Profile instances are
self. init (..): constructor are stored like in a database.
self.List(..): print instance’s data
from Base import Base # the ProfDB class should inherit the Base class
2.12.2011
Page 64 Analysis of Structures - WS10/11
To check the database class ProfDB we write a little script, which first creates a ProfDB instance. Then
we create a UProfile instance with the values of the U100. This instance is stored in the dictionary using
the key of it’s name U100. Then we create a second UProfile instance with the values of the U140 and
store it as well in the dictionary using it’s name U140 as it’s key. After that we simple call the ProfDB
instance method list. The data of the two profiles are listed on the screen and into the log file. The code
of the testing environment is given below.
’’’
This testing example implements a little profile
database. Two U-profiles are created and stored in
the database. Then the whole content of the database
is printed using the AppendLog method of the base class
’’’
# package module class
from Profiles.UProfile import UProfile
from Profiles.ProfDB import ProfDB
E. Baeck
Part II
65
4
In this chapter we talk about the Abaqus Student Edition Licence 6.10.. A main aspect will be the
development of Python programs, which should automate the creation of FE models and the subsequent
calculation and preprocessing.
In this chapter we talk about the Abaqus Student Edition Licence 6.10. GUI1 .
Figure 4.1 shows the Abaqus GUI. A very important item is the combo box to select the module. The
selected module loads the specific menu items. The left window shows the Abaqus object tree. The
bottom window contents an output window for the output messages and the command window for the
Python commands.
1
Graphical User Interface is a window driven program which contents all commands in terms of menu items, buttons, boxes
and so on which are commonly called widgets.
67
Page 68 Analysis of Structures - WS10/11
• The Python command window offers the possibility to execute single Python commands.
• The GUI offers the possibility to record interactive actions in a Python script format. So we can
record and replay everything we do within the GUI.
The Python interpreter creates the input for the CAE kernel. The CAE kernel creates the input stream for
the Abaqus FE-Module.2
2
The input file for the Abaqus FE-Module is a simple Text file, the classical input file, which can also be written ’by hand’.
E. Baeck
4.3. A MODELING CHAIN Page 69
Figure 4.2 shows how to create a Finite Element model Create a database
in Abaqus/CAE. It is not possible to create elements and
nodes directly. Element and nodes are only created by Module Sketch
Draw a sketch
the mesh generator, which works an the geometric objects,
which are created drawing a sketch.
Module Part
The only FE model parameter, which are created di- Create a part and
rectly, are the material properties and the section values. assign the sketch
After the mesh is created, the complete model can be as- Module Job
Create a Job and submit
signed to a job. To calculate the results the job has to be
submitted. Figure 4.2: Modeling Chain Diagram
2.12.2011
Page 70 Analysis of Structures - WS10/11
Ly
• Ly vertical length 1500 mm
Lx
Figure 4.3: 3 Trusses
4.4.1 Create a Database
Module: Sketch
Sketch => Create => Name: ’Truss-Example’ => Continue
Add=> Point
=> enter coordinates (0,0), (1000,0), (1000,1500), (0,1500)
Add => Line => Connected Line
=> select (0,0) node with mouse, then (1000,0) node,
right click => Cancel
Add => Line
=> Connected Line
=> select (0,0) node with mouse, then (1000,1500) node,
right click => Cancel
Add => Line
=> Connected Line
=> select (0,0) node with mouse, then (0,1500) node,
right click => Cancel
=> Done
Module: Part
Part => Create => Name: ’Part-1’,=> select 2D Planar, Deformable, Wire
=> Continue
Add => Sketch => select ’Truss-Example’ => Done => Done
E. Baeck
4.4. A LITTLE INTERACTIVE WARM UP EXAMPLE Page 71
Module: Property
Material => Create => Name: ’Steel’, Mechanical, Elasticity, Elastic
=> set Young’s modulus = 210000, Poisson’s ratio = 0.3 => OK
Section => Create => Name: ’Truss-Section’, Beam, Truss => Continue
=> set Material: ’Material-1’,
Cross-sectional area: 2
Assign Section => select all elements by dragging mouse => Done
=> ’Truss-Section’ => OK => Done
Module: Assembly
Instance => Create => ’Part-1’ => Independent (mesh on instance) => OK
Module: Step
Step => Create => Name: ’Step-1’, Initial, Static, General => Continue
=> accept default settings
=> OK
Module: Load
Load => Create => Name: ’Step-1’, Step: ’Step 1’, Mechanical,
Concentrated Force => Continue => select node at (0,0) => Done
=> set CF1: -10000 set CF2: -10000 => OK
Module: Mesh
Seed => Edge by Number => select entire truss by dragging mouse
=> Done => Number of elements along edges: 1 => press Enter => Done
Mesh => Element Type => select entire truss by dragging mouse => Done
=> Element Library: Standard,
Geometric Order: Linear: Family: Truss => OK => Done
2.12.2011
Page 72 Analysis of Structures - WS10/11
Module: Job
Job => Create => Name: Job-1, Model: Model-1 => Continue
=> Job Type: Full analysis, Run Mode: Background,
Submit Time: Immediately => OK
E. Baeck
5
In this chapter we implement to example scripts, to discuss the automation in Abaqus using the Python
script language.
1. 3 Trusses
The first script gives the implementation of the interactive example, a little 3 trusses system with
one concentrated load. The calculation is performed automatically for a linear analysis.
2. U Profile
The second script gives the implementation of the automated model creation of U Profile using the
thin-walled approach (see also section 3.2 and A.1). The calculation is performed automatically
for a linear analysis and a buckling analysis.
Within this section the implementation of a script is shown, which automates the example of section
4.4. With the change of the parameter’s values, every system can be calculated with the execution of the
script.
Creating the calculation model we follow our outlines discussed in section 4.3. To avoid problems with
existing project files, we first delete an old database, if exist. Then we set the work directory by the use
of Python’s standard chdir function.1
To get the Python code for the necessary steps, we can simple run the macro recorder and record the
interactive actions. If you do this, then it’s recommended to save the macro file into the work directory
and not into the home directory. The recorded macros are saved into the file abaqusMacros.py.
Note, that the work directory will be set, if the script is executed.
Within the script code the following steps are numbered in the same way.
73
Page 74 Analysis of Structures - WS10/11
After having selected the lines the truss section will be assigned by the part’s method
SectionAssignment. We have to specify the region and the name of the section.
E. Baeck
5.1. 3 TRUSSES SCRIPT Page 75
– synchronous analysis
In this case the script halts for completion of the calculation. This can be obtained by the
usage of the job classes’ method waitForCompletion. The next commands of the script
are executed after the calculation is completed.
– asynchronous analysis
In this case the script does not halt for completion and the next instructions are executed
immediately. To automate the analysis of the results we have to set up a call back function.
This function is executed automatically after the calculation is done.
2.12.2011
Page 76 Analysis of Structures - WS10/11
# copy the next line into the abaqus command window and execute
# execfile("c:\\CM\\Cm-AoS\\WS1011\Abaqus\\3Trusses.py")
modelname = ’3Trusses’
import os # os tools
os.chdir(r’c:\\cm\\Cm-AoS\\WS1011\\Abaqus’)
# set up parameters
dx = 1000. # length in x
dy = 1500. # length in z
A = 13.5*100. # U100 area
Fx = -100000. # horzontal load
Fy = -100000. # vertikal load
EMod = 210000. # Young’s module
nue = 0.3 # Poisson’s ratio
E. Baeck
5.1. 3 TRUSSES SCRIPT Page 77
2.12.2011
Page 78 Analysis of Structures - WS10/11
E. Baeck
5.1. 3 TRUSSES SCRIPT Page 79
myJob.submit(consistencyChecking=OFF)
# - note: wait for completion, if the result values should be read form DB
myJob.waitForCompletion()
2.12.2011
Page 80 Analysis of Structures - WS10/11
The goal of our next example is, to create a mesh from a 2 dimensional
sketch of an U geometry by extrusion. Figure 5.2 shows an U profile with
it’s parameters [6].
The model of the U profile is created step by step according to the mod-
eling chain diagram (figure 4.2). The created mesh is shown in figure 5.2.
Figure 5.3 shows the load onto the upper flange of the girder. The girder is
supported on both ends at the lower flange’s edges. Additional point sup-
port is needed to avoid longitudinal and transversal movements as well as
rigid body rotations.
Note that the figures are printed automatically after having created the finite
element model. Figure 5.1: U Profile
Within the header section of the script all parameters are defined and ini-
tialized. The script is able to perform optionally a linear or a buckling analysis. This is controlled
by the flag bBuckl. Optionally system plots as well as result plots are created and printed into
png graphic files. This is controlled by the flag bPNGFile.
Within this implementation steps were recorded with the macro recorder and the recorded code
was reduced by the settings of standard parameters which are generally also recorded. Furthermore
we introduce consequently abbreviations for larger instance item accesses. This gives us a more
compact and readable code.
’’’
the script can be starte like this command
execfile("c:\\Cm\\CM-AoS\\Ws1011\\Abaqus\\UProfile2 1.py")
import math
E. Baeck
5.2. U PROFILE SCRIPT Page 81
Logger = Base(logname)
Logger.reset()
Logger.AppendLog(">> Project: ’%s’" % prjname)
2.12.2011
Page 82 Analysis of Structures - WS10/11
E. Baeck
5.2. U PROFILE SCRIPT Page 83
# - and mesh it
myMesh = myPart.generateMesh()
# overwrite
nodes = myPart.nodes
n = len(nodes)
Logger.SetTime(False)
Logger.SetScreen(False)
Logger.AppendLog(’\nNumber of mesh nodes: %d’ % n)
Logger.AppendLog(’--no ----x----- ----y----- ----y-----’)
for node in nodes:
nodepos[node.label] = (node.coordinates[0],node.coordinates[1], \
node.coordinates[2])
Logger.AppendLog("%4d %10.3f %10.3f %10.3f" % (node.label, \
node.coordinates[0], node.coordinates[1], \
node.coordinates[2]));
Logger.AppendLog(’--no ----x----- ----y----- ----y-----\n’)
Logger.SetTime(True)
Logger.SetScreen(True)
2.12.2011
Page 84 Analysis of Structures - WS10/11
if bBuckl:
stepname = ’buckling step’
Logger.AppendLog("Create step ’%s’..." % stepname)
myModel.BuckleStep(name=stepname, previous=’Initial’,
description=’Bucklung Calculation’,
numEigen=numEigen, vectors=numEigen +8,
maxIterations = numIterX)
else:
# (9b) Linear static calculation step
Logger.AppendLog("Create step ’%s’..." % stepname)
myModel.StaticStep(name=stepname, previous=’Initial’,
description=’UProfile-Step1’)
E. Baeck
5.2. U PROFILE SCRIPT Page 85
2.12.2011
Page 86 Analysis of Structures - WS10/11
# create plot
myViewport.setValues(displayedObject=mySession)
myViewport.odbDisplay.setFrame(step=0,frame=mode)
myViewport.odbDisplay.display.setValues(plotState=(
CONTOURS ON DEF, ))
myViewport.view.fitView() # fit the plot to the view
# linear calculation
else:
# plot mises
Logger.AppendLog("View Results...")
myViewport.setValues(displayedObject=mySession)
myViewport.odbDisplay.display.setValues(plotState=(
CONTOURS ON DEF, ))
myViewport.view.fitView()
E. Baeck
5.2. U PROFILE SCRIPT Page 87
session.printToFile(fileName=pngfile, format=PNG,
canvasObjects=(myViewport, ))
# open database
myOdb = visualization.openOdb(odbname)
Logger.SetTime(False)
Logger.SetScreen(False)
# write a header
Logger.AppendLog("\nData of frame %d" % nFrame)
header = "\n--no ---ux----- ---uy----- ---uz-----|"
header += " ----x----- ----y----- ----z-----"
Logger.AppendLog(header)
# maximum search
max = [None, None, None]
pos = [None, None, None]
2.12.2011
Page 88 Analysis of Structures - WS10/11
# close Logger
Logger.AppendLog("----> end of script")
del Logger
End of Coding
Figure 5.4 shows the resulting Mieses stresses of a linear analysis. Figure 5.5 shows the resulting dis-
placements in 1(x), 2(y) and 3(z) direction of a linear analysis.
The figures 5.6 to 5.15 show the first 10 buckling modes of the girder.
E. Baeck
5.2. U PROFILE SCRIPT Page 89
2.12.2011
Page 90 Analysis of Structures - WS10/11
E. Baeck
5.2. U PROFILE SCRIPT Page 91
2.12.2011
Page 92 Analysis of Structures - WS10/11
E. Baeck
5.2. U PROFILE SCRIPT Page 93
2.12.2011
Page 94 Analysis of Structures - WS10/11
E. Baeck
Part III
Appendix
95
Appendix A
Some Theory
Within this chapter the formulas for the section properties of a thin walled model are given.
A thin walled model for a profile section consists of a set of lines which describes the profile section
geometry at the centerline.
The Area is approximately the sum of the areas of the lines of the thin walled model.
Z n
X
A= eµ · dA ≈ eµ,i · Li · ti (A.1)
A i=1
The first moments of an area are the area integrals given below. The (y,z) values are related to an given
coordinate system.
Z n
X
Sy = eµ · z · dA ≈ eµ,i · z i · Ai
A i=1
Z n
X
Sz = eµ · y · dA ≈ eµ,i · y i · Ai (A.2)
A i=1
97
Page 98 Analysis of Structures - WS10/11
The moments of inertia can be calculated with the formulas below. If we have a given arbitrary coordinate
system in general we have three values of inertia the Iy , the Iz and the mixed Iyz . If we use the main
coordinate system, the mixed moment of inertia is vanishing, so we use the symbols Iξ and Iη .
Z n
X
eµ · z 2 · dA ≈ (zb,i − za,i )2 /12) + z 2i · Ai
Iy = eµ,i ·
A i=1
Z Xn
eµ · y 2 · dA ≈ (yb,i − ya,i )2 /12) + y 2i · Ai
Iz = eµ,i ·
A i=1
Z Xn
Iyz = eµ · y · z · dA ≈ eµ,i · (((yb,i − ya,i )(zb,i − za,i )/12) + y i · z i ) · Ai ) (A.3)
A i=1
To solve an integral like Iy = A z 2 · dA for a polyline we can split up the integral into the sum of
R
To solve an integral for a polyline segment we simple calculate it for the center of mass, because a simple
shift only will give us an additional term, the Steiner term. If we now want to calculate the polyline
integral at the center of mass we rotate the coordinate system by an angle ϕ into the line’s longitudinal
direction, because the transversal dimension, the thickness, is constant and so the respective integral will
be trivial.
E. Baeck
A.1. SECTION PROPERTIES Page 99
The coordinates of the center of mass are calculated with the arithmetic mean. Because the numerator of
the arithmetic mean is identical with the first moment of the area (see section A.1.2) and the denominator
is identical with the area of the profile, which is calculated in section A.1.1 we can use this values.
R
y · dA Sz
yc = AR =
dA A
R A
z · dA Sy
zc = AR = (A.8)
A dA A
If we know the center of mass coordinates given in section A.1.4 we can calculate the moments of inertia
with respect to the center of mass using Steiner’s Theorem as follows.
Iy,c = Iy − zc2 · A
Iz ,c = Iz − yc2 · A
Iyz ,c = Iyz − yc · zc · A (A.9)
2.12.2011
Page 100 Analysis of Structures - WS10/11
To get the moments of inertia Iξ and Iη we have to transform the moments of inertia into the main
coordinate system. Using this coordinate system the mixed moment of inertia is vanishing.
The main axis transformation is given with equation A.10.1
Idel = Iz ,c − Iy,c
Isum = Iy,c + Iz ,c
q
2 + 4 · I2
Isqr = IDel yz ,c
1 2 · Iyz ,c
ϕ= · arctan( )
2 Idel
1
Iξ = · (Isum + Isqr )
2
1
Iη = · (Isum − Isqr ) (A.10)
2
1
The rotation angle ϕ should be shifted into the intervall [−π/2... + π/2]. To avoid a zero division calculating the rotation
angle ϕ a special version of the atan function should be used, which is able to handle the pole problem. In Python like in C this
function is called atan2(y, x ), which calculates the atan( xy ).
E. Baeck
Bibliography
101
Index
:, 28 area, 97
arithmetic mean, 99
Abaqus as, 14
abaqusMacros, 73
BaseWire, 74 Basic, 20
close, 76 bit’s values , 16
ConstrainedSketch, 74 break, 23
DisplacementBC, 75 buckling analysis, 80
element type buckling modes, 88
T2D2, 75
C, 15, 19, 20, 22
elemType, 75
center of mass, 99
fieldOutputs, 76
chdir, 73
frame, 76
class
generateMesh, 75
AList, 53, 58
getSequenceFromMask, 74
Base, 52
instance, 74
Line, 58
job, 75
Point, 56
material, 74 ProfDB, 64
mesh, 75 Profile, 53
model, 73 Profiles, 51
openOdb, 76 UProfile, 51
part, 74 code blocks, 21
regionToolset, 75 complement, 15
rootAssembly, 74 ComTypes, 6
SectionAssignment, 74 continue, 23
seedEdgeByNumber, 75 cos, 14
session, 75 count, 33
setElementType, 75
StaticStep, 75 datetime, 38
step, 76 day, 38
submit, 75 def, 28
TrussSection, 74 derivative, 29
values, 76 dictionary, 34, 62
vertices, 75
e/E format, 20
viewport, 75
eigenforms, 88
visualization, 76
eigenvalues, 88
waitForCompletion, 75
extend, 33
append, 33
Aptana, 8 f format, 20
102
INDEX Page 103
factorial, 22 Parameters, 28
factorial(170), 24 pi, 14
factorial(400), 24 PNG export, 80
file pop, 33
close, 41 printf, 19
open, 40 push, 34
write, 40 PyDev, 8
Finite Element Model, 69 PyLab, 62
first moment, 97 PythonWin, 10
float, 20
random, 35
for, 22
remove, 33
Fortran, 20
reserved words, 13
g/G format, 20 return, 28
reverse, 33
hour, 38
SciPy, 7
IDE, 8
second, 38
if, 25
second moment, 98
import, 14
Setup, 5
indent, 22
sin, 14
index, 33
site-packages, 5
insert, 33
sort, 33
int, 20
stack, 34
Interactive window, 10
strings, 32
iteration, 29
The Beatles, 35
Lib, 5
timedelta, 38
LIFO, 34
Tkinter, 53
linear analysis, 80
tuple, 32
list, 32, 33
type, 21
long, 20
type checking, 28
main axis, 100
UML, 41
map, 34
aggregation, 42
microsecond, 38
class diagram, 41
minute, 38
composition, 42
moment of inertia, 98
inheritance diagram, 42
month, 38
note diagram, 42
name, 21 note diagram assignment, 42
negative number, 15
version, 5
Newton’s Algorithm, 30
now, 38 while, 22
NumPy, 7
year, 38
OOP, 41
package, 14, 52
2.12.2011