Skip to content

Commit

Permalink
restructured the directory, also slipped minor updates to READMEs and…
Browse files Browse the repository at this point in the history
… INSTALL
  • Loading branch information
williamhunter committed Jun 7, 2016
1 parent c60a489 commit 9c65aa4
Show file tree
Hide file tree
Showing 126 changed files with 2,255 additions and 7 deletions.
9 changes: 8 additions & 1 deletion CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,14 @@ v0.1.1
---
1. SymPy related: All *_K.py files in data directory, corrected TypeError: 'Symbol' object is not iterable

v0.1.2
v0.2.1
---
1. Moved ToPy from Google Code to GitHub.
1. Updated files, moved a few things around, renamed/deleted a few, etc. No changes to the core.

v0.2.2
---
1. Cleaned up by moving ToPy source code files to their own directory
1. Added a docs folder and added master's dissertation to it
1. Deleted a few files that weren't needed (such as MS sln file)
1. Fixed a few typos in READMEs and INSTALL
36 changes: 31 additions & 5 deletions INSTALL.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# Prerequisites

1. Start by installing Python 2.7 (32-bit works fine), download from the official site. If you're uing Linux, you most probably have it already.
2. **Windows only:** Make sure Python is in your Path Environment Variable.
1. **Windows only:** Make sure Python is in your Path Environment Variable.
1. If you don't know how to add it, please search the web.
1. Check if Python works by typing it into a *cmd* shell.
1. Check if 'pip' works, also by typing it into a *cmd* shell. If it doesn't work, add Python27\Scripts to the Environment Variables too.
Expand All @@ -10,18 +10,44 @@
1. Install PySparse, also from http://www.lfd.uci.edu/~gohlke/pythonlibs/#numpy
1. Install PyVTK, also from http://www.lfd.uci.edu/~gohlke/pythonlibs/#numpy
1. **Linux**: Install above packages via `pip` or by other means (e.g., apt-get, yum, rpm, etc).
7. Install matplotlib via `pip`
8. Install SymPy via `pip`
1. Install matplotlib via `pip`
1. Install SymPy via `pip`

Installing matplotlib and SymPy via other 'official' channels should also work fine (in that ToPy should still work).

If everything installed correctly, you're set.

# Installing ToPy
In a shell ('cmd' window on Windows), type:
In a terminal ('cmd' window on Windows), type:

python setup.py install

or if you want to install locally, type:

python setup.py install --user
python setup.py install --user

You may require Administrator rights on Windows, depending on your setup.

## Creating the element stiffness matrices
The first time you run ToPy after a fresh install you'll see the following in your terminal:

It seems as though all or some of the element stiffness matrices
do not exist. Creating them...
This is usually only required once and may take a few minutes.
SymPy is integrating: K for Q4bar...
Created C:\Users\William\Programming\ToPy\topy\data\Q4bar.K (stiffness matrix).
SymPy is integrating: K for Q4...
Created C:\Users\William\Programming\ToPy\topy\data\Q4.K (stiffness matrix).
SymPy is integrating: K for Q5B...
Created C:\Users\William\Programming\ToPy\topy\data\Q5B.K (stiffness matrix).
SymPy is integrating: K for Q4T...
Created C:\Users\William\Programming\ToPy\topy\data\Q4T.K (stiffness matrix).
SymPy is integrating: K for H8...
Created C:\Users\William\Programming\ToPy\topy\data\H8.K (stiffness matrix).
SymPy is integrating: K for H18B...
Created C:\Users\William\Programming\ToPy\topy\data\H18B.K (stiffness matrix).
SymPy is integrating: K for H8T...
Created C:\Users\William\Programming\ToPy\topy\data\H8T.K (stiffness matrix).

You won't (shouldn't) see it again, even if ToPy is updated, since these
files shouldn't need to change.
2 changes: 1 addition & 1 deletion LICENSE.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
The MIT License (MIT)

Copyright (c) [2011, 2015] [William Hunter]
Copyright (c) [2011, 2015, 2016] [William Hunter]

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
Expand Down
Binary file not shown.
10 changes: 10 additions & 0 deletions docs/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
# Documents

The docs folder currently only contains my master's dissertation since
ToPy was part of it. The maths behind topology optimization is explained in it.
There is also quite a bit about ToPy in it, but now a little outdated.
The persistent URL for the dissertation is: http://hdl.handle.net/10019.1/2648

Other useful documentation pertaining to ToPy and/or topology optimisation
can be added to this folder.

Binary file added examples/mbb_beam/beam_2d_reci_gsf_001.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added examples/mbb_beam/beam_2d_reci_gsf_002.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added examples/mbb_beam/beam_2d_reci_gsf_003.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added examples/mbb_beam/beam_2d_reci_gsf_004.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added examples/mbb_beam/beam_2d_reci_gsf_005.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added examples/mbb_beam/beam_2d_reci_gsf_006.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added examples/mbb_beam/beam_2d_reci_gsf_007.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added examples/mbb_beam/beam_2d_reci_gsf_008.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added examples/mbb_beam/beam_2d_reci_gsf_009.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added examples/mbb_beam/beam_2d_reci_gsf_010.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added examples/mbb_beam/beam_2d_reci_gsf_011.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added examples/mbb_beam/beam_2d_reci_gsf_012.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added examples/mbb_beam/beam_2d_reci_gsf_013.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added examples/mbb_beam/beam_2d_reci_gsf_014.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added examples/mbb_beam/beam_2d_reci_gsf_015.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added examples/mbb_beam/beam_2d_reci_gsf_016.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added examples/mbb_beam/beam_2d_reci_gsf_017.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added examples/mbb_beam/beam_2d_reci_gsf_018.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added examples/mbb_beam/beam_2d_reci_gsf_019.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added examples/mbb_beam/beam_2d_reci_gsf_020.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added examples/mbb_beam/beam_2d_reci_gsf_021.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added examples/mbb_beam/beam_2d_reci_gsf_022.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added examples/mbb_beam/beam_2d_reci_gsf_023.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added examples/mbb_beam/beam_2d_reci_gsf_024.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added examples/mbb_beam/beam_2d_reci_gsf_025.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added examples/mbb_beam/beam_2d_reci_gsf_026.png
Binary file added examples/mbb_beam/beam_2d_reci_gsf_027.png
Binary file added examples/mbb_beam/beam_2d_reci_gsf_028.png
Binary file added examples/mbb_beam/beam_2d_reci_gsf_029.png
Binary file added examples/mbb_beam/beam_2d_reci_gsf_030.png
Binary file added examples/mbb_beam/beam_2d_reci_gsf_031.png
Binary file added examples/mbb_beam/beam_2d_reci_gsf_032.png
Binary file added examples/mbb_beam/beam_2d_reci_gsf_033.png
Binary file added examples/mbb_beam/beam_2d_reci_gsf_034.png
Binary file added examples/mbb_beam/beam_2d_reci_gsf_035.png
Binary file added examples/mbb_beam/beam_2d_reci_gsf_036.png
Binary file added examples/mbb_beam/beam_2d_reci_gsf_037.png
Binary file added examples/mbb_beam/beam_2d_reci_gsf_038.png
Binary file added examples/mbb_beam/beam_2d_reci_gsf_039.png
Binary file added examples/mbb_beam/beam_2d_reci_gsf_040.png
Binary file added examples/mbb_beam/beam_2d_reci_gsf_041.png
Binary file added examples/mbb_beam/beam_2d_reci_gsf_042.png
Binary file added examples/mbb_beam/beam_2d_reci_gsf_043.png
Binary file added examples/mbb_beam/beam_2d_reci_gsf_044.png
Binary file added examples/mbb_beam/beam_2d_reci_gsf_045.png
Binary file added examples/mbb_beam/beam_2d_reci_gsf_046.png
Binary file added examples/mbb_beam/beam_2d_reci_gsf_047.png
Binary file added examples/mbb_beam/beam_2d_reci_gsf_048.png
Binary file added examples/mbb_beam/beam_2d_reci_gsf_049.png
Binary file added examples/mbb_beam/beam_2d_reci_gsf_050.png
Binary file added examples/mbb_beam/beam_2d_reci_gsf_051.png
Binary file added examples/mbb_beam/beam_2d_reci_gsf_052.png
Binary file added examples/mbb_beam/beam_2d_reci_gsf_053.png
Binary file added examples/mbb_beam/beam_2d_reci_gsf_054.png
Binary file added examples/mbb_beam/beam_2d_reci_gsf_055.png
Binary file added examples/mbb_beam/beam_2d_reci_gsf_056.png
Binary file added examples/mbb_beam/beam_2d_reci_gsf_057.png
Binary file added examples/mbb_beam/beam_2d_reci_gsf_058.png
Binary file added examples/mbb_beam/beam_2d_reci_gsf_059.png
Binary file added examples/mbb_beam/beam_2d_reci_gsf_060.png
Binary file added examples/mbb_beam/beam_2d_reci_gsf_061.png
Binary file added examples/mbb_beam/beam_2d_reci_gsf_062.png
Binary file added examples/mbb_beam/beam_2d_reci_gsf_063.png
Binary file added examples/mbb_beam/beam_2d_reci_gsf_064.png
Binary file added examples/mbb_beam/beam_2d_reci_gsf_065.png
Binary file added examples/mbb_beam/beam_2d_reci_gsf_066.png
Binary file added examples/mbb_beam/beam_2d_reci_gsf_067.png
Binary file added examples/mbb_beam/beam_2d_reci_gsf_068.png
Binary file added examples/mbb_beam/beam_2d_reci_gsf_069.png
Binary file added examples/mbb_beam/beam_2d_reci_gsf_070.png
Binary file added examples/mbb_beam/beam_2d_reci_gsf_071.png
Binary file added examples/mbb_beam/beam_2d_reci_gsf_072.png
Binary file added examples/mbb_beam/beam_2d_reci_gsf_073.png
Binary file added examples/mbb_beam/beam_2d_reci_gsf_074.png
Binary file added examples/mbb_beam/beam_2d_reci_gsf_075.png
Binary file added examples/mbb_beam/beam_2d_reci_gsf_076.png
Binary file added examples/mbb_beam/beam_2d_reci_gsf_077.png
Binary file added examples/mbb_beam/beam_2d_reci_gsf_078.png
Binary file added examples/mbb_beam/beam_2d_reci_gsf_079.png
Binary file added examples/mbb_beam/beam_2d_reci_gsf_080.png
Binary file added examples/mbb_beam/beam_2d_reci_gsf_081.png
Binary file added examples/mbb_beam/beam_2d_reci_gsf_082.png
Binary file added examples/mbb_beam/beam_2d_reci_gsf_083.png
Binary file added examples/mbb_beam/beam_2d_reci_gsf_084.png
Binary file added examples/mbb_beam/beam_2d_reci_gsf_085.png
Binary file added examples/mbb_beam/beam_2d_reci_gsf_086.png
Binary file added examples/mbb_beam/beam_2d_reci_gsf_087.png
Binary file added examples/mbb_beam/beam_2d_reci_gsf_088.png
Binary file added examples/mbb_beam/beam_2d_reci_gsf_089.png
Binary file added examples/mbb_beam/beam_2d_reci_gsf_090.png
Binary file added examples/mbb_beam/beam_2d_reci_gsf_091.png
Binary file added examples/mbb_beam/beam_2d_reci_gsf_092.png
Binary file added examples/mbb_beam/beam_2d_reci_gsf_093.png
Binary file added examples/mbb_beam/beam_2d_reci_gsf_094.png
Binary file added examples/mbb_beam/beam_2d_reci_gsf_095.png
Binary file added examples/mbb_beam/beam_2d_reci_gsf_096.png
Binary file added examples/mbb_beam/beam_2d_reci_gsf_097.png
Binary file added examples/mbb_beam/beam_2d_reci_gsf_098.png
Binary file added examples/mbb_beam/beam_2d_reci_gsf_099.png
Binary file added examples/mbb_beam/beam_2d_reci_gsf_100.png
Binary file added images/beam_2d_reci_094.png
Binary file added images/beam_2d_reci_094_mirrored.png
Binary file added images/beam_2d_reci_gsf_094.png
Binary file added images/topy_2d_tut1_prob.png
13 changes: 13 additions & 0 deletions topy/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
"""
# ==============================================================================
# ToPy -- Topology optimization with Python.
# Copyright (C) 2012, 2015, 2016 William Hunter.
# ==============================================================================
"""

from topology import *
from visualisation import *
from elements import *

__version__ = "0.2.2"
__author__ = "William Hunter <whunter.za at gmail dot com>"
115 changes: 115 additions & 0 deletions topy/data/H18B_K.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
"""
# =============================================================================
# Write the stiffness matrix of finite element to file. The created file name
# is equal to the string between the underscores of *this* file's name, plus a
# 'K' extension, e.g.,
#
# python ELEM_K.py
#
# gives a file named ELEM.K in the same directory.
#
# Author: William Hunter
# Copyright (C) 2008, 2015, William Hunter.
# =============================================================================
"""

from __future__ import division

from sympy import symbols, Matrix, diff, integrate, zeros, eye

from numpy import abs, array, transpose, dot
from numpy.linalg import inv

from matlcons import *

# Get file name:
fname = __file__.split('_')[0] + '.K'

try:
f = open(fname)
print fname ,'(stiffness matrix) exists!'
f.close()
except IOError:
# SymPy symbols:
a, b, c, x, y, z = symbols('a b c x y z')
N1, N2, N3, N4 = symbols('N1 N2 N3 N4')
N5, N6, N7, N8 = symbols('N5 N6 N7 N8')
E, nu, g, G = symbols('E nu g G')
o = symbols('o') # dummy symbol
xlist = [x, x, x, x, x, x, x, x, x, x, x, x, x, x, x, x, x, x, x, x, x, x, x, x]
ylist = [y, y, y, y, y, y, y, y, y, y, y, y, y, y, y, y, y, y, y, y, y, y, y, y]
zlist = [z, z, z, z, z, z, z, z, z, z, z, z, z, z, z, z, z, z, z, z, z, z, z, z]
yxlist = [y, x, o, y, x, o, y, x, o, y, x, o, y, x, o, y, x, o, y, x, o, y, x, o]
zylist = [o, z, y, o, z, y, o, z, y, o, z, y, o, z, y, o, z, y, o, z, y, o, z, y]
zxlist = [z, o, x, z, o, x, z, o, x, z, o, x, z, o, x, z, o, x, z, o, x, z, o, x]

# Shape functions:
N1 = (a - x) * (b - y) * (c - z) / (8 * a * b * c)
N2 = (a + x) * (b - y) * (c - z) / (8 * a * b * c)
N3 = (a + x) * (b + y) * (c - z) / (8 * a * b * c)
N4 = (a - x) * (b + y) * (c - z) / (8 * a * b * c)
N5 = (a - x) * (b - y) * (c + z) / (8 * a * b * c)
N6 = (a + x) * (b - y) * (c + z) / (8 * a * b * c)
N7 = (a + x) * (b + y) * (c + z) / (8 * a * b * c)
N8 = (a - x) * (b + y) * (c + z) / (8 * a * b * c)

# Create strain-displacement matrix B:
B0 = map(diff, [N1, 0, 0, N2, 0, 0, N3, 0, 0, N4, 0, 0,\
N5, 0, 0, N6, 0, 0, N7, 0, 0, N8, 0, 0], xlist)
B1 = map(diff, [0, N1, 0, 0, N2, 0, 0, N3, 0, 0, N4, 0,\
0, N5, 0, 0, N6, 0, 0, N7, 0, 0, N8, 0], ylist)
B2 = map(diff, [0, 0, N1, 0, 0, N2, 0, 0, N3, 0, 0, N4,\
0, 0, N5, 0, 0, N6, 0, 0, N7, 0, 0, N8], zlist)
B3 = map(diff, [N1, N1, N1, N2, N2, N2, N3, N3, N3, N4, N4, N4,\
N5, N5, N5, N6, N6, N6, N7, N7, N7, N8, N8, N8], yxlist)
B4 = map(diff, [N1, N1, N1, N2, N2, N2, N3, N3, N3, N4, N4, N4,\
N5, N5, N5, N6, N6, N6, N7, N7, N7, N8, N8, N8], zylist)
B5 = map(diff, [N1, N1, N1, N2, N2, N2, N3, N3, N3, N4, N4, N4,\
N5, N5, N5, N6, N6, N6, N7, N7, N7, N8, N8, N8], zxlist)
B = Matrix([B0, B1, B2, B3, B4, B5])

# Create constitutive (material property) matrix:
C = Matrix([[(1 - nu) * g, nu * g, nu * g, 0, 0, 0],
[nu * g, (1 - nu) * g, nu * g, 0, 0, 0],
[nu * g, nu * g, (1 - nu) * g, 0, 0, 0],
[0, 0, 0, G, 0, 0],
[0, 0, 0, 0, G, 0],
[0, 0, 0, 0, 0, G]])

PI = eye(6)
PH3 = Matrix([\
[y/b, z/c, (y*z)/(b*c), 0, 0, 0, 0, 0, 0, 0, 0, 0],\
[0, 0, 0, x/a, z/c, (x*z)/(a*c), 0, 0, 0, 0, 0, 0],\
[0, 0, 0, 0, 0, 0, x/a, y/b, (x*y)/(a*b), 0, 0, 0],\
[0, 0, 0, 0, 0, 0, 0, 0, 0, z/c, 0, 0],\
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, x/a, 0],\
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, y/b]])
P = PI.row_join(PH3)
tP = P.transpose()
dJ = tP * B
dH = tP * C.inv() * P

# Integration:
print 'SymPy is integrating: K for H18B...'
J = dJ.integrate((x, -a, a),(y, -b, b),(z, -c, c))
J = J.subs({a:_a, b:_b, c:_c, E:_E, nu:_nu, g:_g, G:_G})
H = dH.integrate((x, -a, a),(y, -b, b),(z, -c, c))
H = H.subs({a:_a, b:_b, c:_c, E:_E, nu:_nu, g:_g, G:_G})

# Convert SymPy Matrix to NumPy array:
J = array(J).astype('double')
iH = inv(array(H).astype('double'))

# Convert SymPy Matrix to NumPy array:
# NOTE:
# sympy.Matrix() * sympy.Matrix == numpy.dot(numpy.array(), numpy.array())
K = dot(dot(J.transpose(), iH), J) # use NumPy's dot for arrays, NOT '*'

# Set small (<< 0) values equal to zero:
K[abs(K) < 1e-6] = 0

# Create file:
K.dump(fname)
print 'Created', fname, '(stiffness matrix).'

# EOF H18B_K.py
78 changes: 78 additions & 0 deletions topy/data/H8T_K.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
"""
# =============================================================================
# Write the stiffness matrix of finite element to file. The created file name
# is equal to the string between the underscores of *this* file's name, plus a
# 'K' extension, e.g.,
#
# python ELEM_K.py
#
# gives a file named ELEM.K in the same directory.
#
# Author: William Hunter
# Copyright (C) 2008, 2015, William Hunter.
# =============================================================================
"""

from __future__ import division

from sympy import symbols, Matrix, diff, integrate, zeros

from numpy import abs, array

from matlcons import *

# Get file name:
fname = __file__.split('_')[0] + '.K'

try:
f = open(fname)
print fname ,'(stiffness matrix) exists!'
f.close()
except IOError:
# SymPy symbols:
a, b, c, x, y, z = symbols('a b c x y z')
N1, N2, N3, N4 = symbols('N1 N2 N3 N4')
N5, N6, N7, N8 = symbols('N5 N6 N7 N8')
k = symbols('k')
xlist = [x, x, x, x, x, x, x, x]
ylist = [y, y, y, y, y, y, y, y]
zlist = [z, z, z, z, z, z, z, z]

# Shape functions:
N1 = (a - x) * (b - y) * (c - z) / (8 * a * b * c)
N2 = (a + x) * (b - y) * (c - z) / (8 * a * b * c)
N3 = (a + x) * (b + y) * (c - z) / (8 * a * b * c)
N4 = (a - x) * (b + y) * (c - z) / (8 * a * b * c)
N5 = (a - x) * (b - y) * (c + z) / (8 * a * b * c)
N6 = (a + x) * (b - y) * (c + z) / (8 * a * b * c)
N7 = (a + x) * (b + y) * (c + z) / (8 * a * b * c)
N8 = (a - x) * (b + y) * (c + z) / (8 * a * b * c)

# Create strain-displacement matrix B:
B0 = map(diff, [N1, N2, N3, N4, N5, N6, N7, N8], xlist)
B1 = map(diff, [N1, N2, N3, N4, N5, N6, N7, N8], ylist)
B2 = map(diff, [N1, N2, N3, N4, N5, N6, N7, N8], zlist)
B = Matrix([B0, B1, B2])

# Create conductivity matrix:
C = Matrix([[k, 0, 0],
[0, k, 0],
[0, 0, k]])

dK = B.T * C * B

# Integration:
print 'SymPy is integrating: K for H8T...'
K = dK.integrate((x, -a, a),(y, -b, b),(z, -c, c))

# Convert SymPy Matrix to NumPy array:
K = array(K.subs({a:_a, b:_b, c:_c, k:_k})).astype('double')

# Set small (<< 0) values equal to zero:
K[abs(K) < 1e-6] = 0

# Create file:
K.dump(fname)
print 'Created', fname, '(stiffness matrix).'

# EOF H8T_K.py
94 changes: 94 additions & 0 deletions topy/data/H8_K.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
"""
# =============================================================================
# Write the stiffness matrix of finite element to file. The created file name
# is equal to the string between the underscores of *this* file's name, plus a
# 'K' extension, e.g.,
#
# python ELEM_K.py
#
# gives a file named ELEM.K in the same directory.
#
# Author: William Hunter
# Copyright (C) 2008, 2015, William Hunter.
# =============================================================================
"""

from __future__ import division

from sympy import symbols, Matrix, diff, integrate, zeros

from numpy import abs, array

from matlcons import *

# Get file name:
fname = __file__.split('_')[0] + '.K'

try:
f = open(fname)
print fname ,'(stiffness matrix) exists!'
f.close()
except IOError:
# SymPy symbols:
a, b, c, x, y, z = symbols('a b c x y z')
N1, N2, N3, N4 = symbols('N1 N2 N3 N4')
N5, N6, N7, N8 = symbols('N5 N6 N7 N8')
E, nu, g, G = symbols('E nu g G')
o = symbols('o') # dummy symbol
xlist = [x, x, x, x, x, x, x, x, x, x, x, x, x, x, x, x, x, x, x, x, x, x, x, x]
ylist = [y, y, y, y, y, y, y, y, y, y, y, y, y, y, y, y, y, y, y, y, y, y, y, y]
zlist = [z, z, z, z, z, z, z, z, z, z, z, z, z, z, z, z, z, z, z, z, z, z, z, z]
yxlist = [y, x, o, y, x, o, y, x, o, y, x, o, y, x, o, y, x, o, y, x, o, y, x, o]
zylist = [o, z, y, o, z, y, o, z, y, o, z, y, o, z, y, o, z, y, o, z, y, o, z, y]
zxlist = [z, o, x, z, o, x, z, o, x, z, o, x, z, o, x, z, o, x, z, o, x, z, o, x]

# Shape functions:
N1 = (a - x) * (b - y) * (c - z) / (8 * a * b * c)
N2 = (a + x) * (b - y) * (c - z) / (8 * a * b * c)
N3 = (a + x) * (b + y) * (c - z) / (8 * a * b * c)
N4 = (a - x) * (b + y) * (c - z) / (8 * a * b * c)
N5 = (a - x) * (b - y) * (c + z) / (8 * a * b * c)
N6 = (a + x) * (b - y) * (c + z) / (8 * a * b * c)
N7 = (a + x) * (b + y) * (c + z) / (8 * a * b * c)
N8 = (a - x) * (b + y) * (c + z) / (8 * a * b * c)

# Create strain-displacement matrix B:
B0 = map(diff, [N1, 0, 0, N2, 0, 0, N3, 0, 0, N4, 0, 0,\
N5, 0, 0, N6, 0, 0, N7, 0, 0, N8, 0, 0], xlist)
B1 = map(diff, [0, N1, 0, 0, N2, 0, 0, N3, 0, 0, N4, 0,\
0, N5, 0, 0, N6, 0, 0, N7, 0, 0, N8, 0], ylist)
B2 = map(diff, [0, 0, N1, 0, 0, N2, 0, 0, N3, 0, 0, N4,\
0, 0, N5, 0, 0, N6, 0, 0, N7, 0, 0, N8], zlist)
B3 = map(diff, [N1, N1, N1, N2, N2, N2, N3, N3, N3, N4, N4, N4,\
N5, N5, N5, N6, N6, N6, N7, N7, N7, N8, N8, N8], yxlist)
B4 = map(diff, [N1, N1, N1, N2, N2, N2, N3, N3, N3, N4, N4, N4,\
N5, N5, N5, N6, N6, N6, N7, N7, N7, N8, N8, N8], zylist)
B5 = map(diff, [N1, N1, N1, N2, N2, N2, N3, N3, N3, N4, N4, N4,\
N5, N5, N5, N6, N6, N6, N7, N7, N7, N8, N8, N8], zxlist)
B = Matrix([B0, B1, B2, B3, B4, B5])

# Create constitutive (material property) matrix:
C = Matrix([[(1 - nu) * g, nu * g, nu * g, 0, 0, 0],
[nu * g, (1 - nu) * g, nu * g, 0, 0, 0],
[nu * g, nu * g, (1 - nu) * g, 0, 0, 0],
[0, 0, 0, G, 0, 0],
[0, 0, 0, 0, G, 0],
[0, 0, 0, 0, 0, G]])

dK = B.T * C * B

# Integration:
print 'SymPy is integrating: K for H8...'
K = dK.integrate((x, -a, a),(y, -b, b),(z, -c, c))

# Convert SymPy Matrix to NumPy array:
K = array(K.subs({a:_a, b:_b, c:_c, E:_E, nu:_nu, g:_g, G:_G})).astype('double')

# Set small (<< 0) values equal to zero:
K[abs(K) < 1e-6] = 0

# Create file:
K.dump(fname)
print 'Created', fname, '(stiffness matrix).'

# EOF H8_K.py
Loading

0 comments on commit 9c65aa4

Please sign in to comment.