Calfem For Python
Calfem For Python
Release 3.4.1
Jonas Lindemann
3 Function reference 17
3.1 Core functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17
3.2 Geometry functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 29
3.3 Mesh functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 37
3.4 User interface functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 39
3.5 Utility functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 39
3.6 Visualisation functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 41
Index 55
i
ii
CALFEM for Python Documentation, Release 3.4.1
Contents:
Contents 1
CALFEM for Python Documentation, Release 3.4.1
2 Contents
CHAPTER 1
3
CALFEM for Python Documentation, Release 3.4.1
The computation is initialized by defining the topology matrix Edof, containing element numbers and global element
degrees of freedom,:
In [3]: Edof = np.array([
...: [1,2],
...: [2,3],
...: [2,3]
...: ])
and the load vector f (3x1) with the load F = 100 in position 2.:
In [7]: f = np.matrix(np.zeros((3,1))); f[1] = 100
In [8]: print(f)
[[ 0.]
[ 100.]
[ 0.]]
Element stiffness matrices are generated by the function spring1e. The element property ep for the springs contains
the spring stiffnesses k and 2k respectively, where k = 1500.:
In [9]: k = 1500.; ep1 = k; ep2 = 2.*k
In [11]: Ke1 = cfc.spring1e(ep1)
In [12]: print(Ke1)
[[ 1500. -1500.]
[-1500. 1500.]]
The element stiffness matrices are assembled into the global stiffness matrix K according to the topology.:
In []: cfc.assem(Edof[0,:], K, Ke2)
Out[16]:
matrix([[ 3000., -3000., 0.],
[-3000., 3000., 0.],
[ 0., 0., 0.]])
The global system of equations is solved considering the boundary conditions given in bc.:
In [19]: bc = np.array([1,3])
In [20]: a, r = cfc.solveq(K, f, bc)
In [21]: print(a)
[[ 0. ]
[ 0.01333333]
[ 0. ]]
In [22]: print(r)
[[-40.]
[ 0.]
[-60.]]
Element forces are evaluated from the element displacements. These are obtained from the global displacements a
using the function extract.:
In [23]: ed1 = cfc.extractEldisp(Edof[0,:], a)
In [24]: print(ed1)
[ 0. 0.01333333]
A global system matrix K and a heat flow vector f are defined. The heat source inside the wall is considered by setting
f4 = 10. The element matrices Ke are computed using spring1e, and the function assem assembles the global stiffness
matrix.
The system of equations is solved using solveq with considerations to the boundary conditions in bc and bcVal. The
prescribed temperatures are T1 = 17 ◦C and T2 = 20◦C.
Necessary modules are first imported.:
import numpy as np
import calfem.core as cfc
Edof = np.array([
[1,2],
[2,3],
[3,4],
[4,5],
[5,6]
])
K = np.mat(np.zeros((6,6)))
f = np.mat(np.zeros((6,1)))
f[3] = 10.0
Define element properties and create element matrices for the different material layers.:
ep1 = 25.0
ep2 = 24.3
ep3 = 0.4
ep4 = 17.0
ep5 = 7.7
Ke1 = cfc.spring1e(ep1)
Ke2 = cfc.spring1e(ep2)
Ke3 = cfc.spring1e(ep3)
Ke4 = cfc.spring1e(ep4)
Ke5 = cfc.spring1e(ep5)
cfc.assem(Edof[0,:], K, Ke1)
cfc.assem(Edof[1,:], K, Ke2)
cfc.assem(Edof[2,:], K, Ke3)
cfc.assem(Edof[3,:], K, Ke4)
cfc.assem(Edof[4,:], K, Ke5)
bc = np.array([1,6])
bcVal = np.array([-17.0, 20.0])
a,r = cfc.solveq(K, f, bc, bcVal)
print("Displacements a:")
print(a)
ed1 = cfc.extractEldisp(Edof[0,:], a)
ed2 = cfc.extractEldisp(Edof[1,:], a)
ed3 = cfc.extractEldisp(Edof[2,:], a)
ed4 = cfc.extractEldisp(Edof[3,:], a)
ed5 = cfc.extractEldisp(Edof[4,:], a)
q1 = cfc.spring1s(ep1, ed1)
q2 = cfc.spring1s(ep2, ed2)
q3 = cfc.spring1s(ep3, ed3)
q4 = cfc.spring1s(ep4, ed4)
q5 = cfc.spring1s(ep5, ed5)
print("q1 = "+str(q1))
print("q2 = "+str(q2))
print("q3 = "+str(q3))
print("q4 = "+str(q4))
print("q5 = "+str(q5))
Stiffness matrix K:
[[ 25. -25. 0. 0. 0. 0. ]
[-25. 49.3 -24.3 0. 0. 0. ]
[ 0. -24.3 24.7 -0.4 0. 0. ]
[ 0. 0. -0.4 17.4 -17. 0. ]
[ 0. 0. 0. -17. 24.7 -7.7]
[ 0. 0. 0. 0. -7.7 7.7]]
Displacements a:
[[-17. ]
[-16.43842455]
[-15.86067203]
[ 19.23779344]
[ 19.47540439]
[ 20. ]]
Reaction forces r:
[[ -1.40393862e+01]
[ -5.68434189e-14]
[ 0.00000000e+00]
[ 0.00000000e+00]
[ 0.00000000e+00]
[ 4.03938619e+00]]
q1 = 14.0393861892
q2 = 14.0393861892
q3 = 14.0393861892
q4 = 4.03938618922
q5 = 4.03938618922
Included in the Python version of CALFEM is a mesh generation library based on GMSH. The library encapsulates
the usage of GMSH transparently for the user. It will also parse the output from GMSH and create the neccesary data
structures required by CALFEM for solving finite element problems.
Mesh generation in CALFEM is divided in three steps:
1. Definining the geometry of the model.
2. Creating a finite element mesh with the desired elements and properties
3. Exctracting data structures that can be used by CALFEM.
The following sections describe these steps.
To use the CALFEM geometry and mesh generation routines, we use the following import directives:
Geometry in CALFEM is described using the Geometry class. A Geometry-object will hold all points, lines and
surfaces describing the geometry. This object is also what is passed to the mesh generation routines in the following
sections.
A Geometry-object, g, is created with the following code:
g = cfg.Geometry()
9
CALFEM for Python Documentation, Release 3.4.1
This creates a Geometry object which will be used to described our geometry. Next we define the points that will be
used to define lines, splines or ellipses. In this example we define a simple triangle:
The points are connected together with spline-elements. These can have 2 or three nodes. In this case we only use 2
node splines (lines):
Finally we create the surface by defining what lines make up the surface:
g.surface([0, 1, 2])
cfv.drawGeometry(g)
cfv.showAndWait()
Running this example will show the following window with a simple rectangle:
To create a mesh we need to create a GmshMesh object and initialise this with our geometry:
mesh = cfm.GmshMesh(g)
The eltype property determines the element used for mesh generation. Elements that can be generated are:
• 2 - 3 node triangle element
• 3 - 4 node quadrangle element
• 5 - 8 node hexahedron
• 16 - 8 node second order quadrangle
The dofsPerNode defines the number of degrees of freedom for each node. elSizeFactor determines the coarsness of
the mesh.
To generate the mesh and at the same time get the needed data structures for use with CALFEM we call the .create()
method of the mesh object:
cfv.figure()
cfv.drawMesh(
coords=coords,
edof=edof,
dofsPerNode=meshGen.dofsPerNode,
elType=meshGen.elType,
filled=True,
title="Example 01"
)
Running the exmaple will produce the following mesh with quad elements:
Changing the elType property to 2 will produce a mesh with triangle elements instead:
To assist in assigning boundary conditions, markers can be defined on the geometry, which can be used to identify
which dofs are assigned to nodes, lines and surfaces.
In this example we add a marker, 10, to line number 2. Markers are added as a parameter to the .spline() method of
the Geometry object as shown in the following code:
It is also possible to assign markers to points. The marker parameter is added to the .point() method of the Geometry
object.
To extract the dofs defined by the marker we use the bdofs dictionary returned when the mesh was created by the
.create() method. If we print the bdofs dictionary we get the following output:
{20: [2], 0: [1, 2, ... , 67], 10: [1, 3, 68, ... , 98]}
If we examine the output we see that there is a key, 10, containing the dofs of the number 2 line. We also have the key
20 with a single dof 2 in this case. If the dofsPerNode property in the mesh generator was set to 2 the marker 20 would
have contained 2 integers.
To illustrate the workflow of the mesh generation modules we implement a complete 2D plane stress solver.
We need to add some additional import directives, such as the core calfem module as well as the calfem.utils module.
We will also need NumPy as well as the standard math routines:
import numpy as np
from math import *
To make it easier to update our example we define a number of variables describing our problem. First some geometric
parameters describing our module, in this case a simple rectangular beam:
l = 5.0
h = 1.0
t = 0.2
Next, we define our material properties we will need later in the code:
v = 0.35
E = 2.1e9
ptype = 1
ep = [ptype,t]
D=cfc.hooke(ptype, E, v)
To make it easier to read our code we define 3 constants, which we will use instead of numerical markers.
left_support = 10
right_support = 20
top_line = 30
g = cfg.Geometry()
g.surface([0, 1, 2, 3])
A quadrilateral mesh is now created with the following code. Please not that we use the dofsPerNode property to
specify 2 dofs per node as this is a mechanical example.
mesh = cfm.GmshMesh(g)
We now have the neccesary input to implement a simple CALFEM solver. First, we create some convenience variables,
nDofs (total number of dofs), ex and ey (element x and y coordinates).
nDofs = np.size(dofs)
ex, ey = cfc.coordxtr(edof, coords, dofs)
K = np.zeros([nDofs,nDofs])
Please not the difference from standard MATLAB CALFEM that the assem routine does not require returning the K
matrix on the left side as the assemblation is done in place.
Next, we need to setup our boundary conditions. Two empty arrays are created, bc for storing the dof to prescribe and
and a second bcVal for storing the prescribed values.
bc = np.array([],'i')
bcVal = np.array([],'f')
To prescribe a boundary condition the utility function applybc() is used. This function takes the boundary dictionary
as input and applies the boundary condition to the correct dofs. Here we prescribe the left support as fixed and the
right support fixed in y-direction.
Function reference
17
CALFEM for Python Documentation, Release 3.4.1
Parameters
• ex (list) – element x coordinates [x1, x2]
• ey (list) – element y coordinates [y1, y2]
• ep (list) – [E, A]: E - Young’s modulus, A - Cross section area
Return mat Ke stiffness matrix, [4 x 4]
calfem.core.bar2g(ex, ey, ep, N)
Compute element stiffness matrix for two dimensional geometric nonlinear bar element.
Parameters
• ex (list) – element x coordinates [x1, x2]
• ey (list) – element y coordinates [y1, y2]
• ep (list) – element properties [E, A], E - Young’s modulus, A - Cross section area
• N (float) – normal force
Return mat Ke stiffness matrix [4 x 4]
calfem.core.bar2s(ex, ey, ep, ed)
Compute normal force in two dimensional bar element.
Parameters
• ex (list) – element x coordinates [x1, x2]
• ey (list) – element y coordinates [y1, y2]
• ep (list) – element properties [E, A], E - Young’s modulus, A - Cross section area
• ed (list) – element displacements [u1, u2, u3, u4]
Return float N element foce [N]
calfem.core.bar3e(ex, ey, ez, ep)
Compute element stiffness matrix for three dimensional bar element.
Parameters
• ex (list) – element x coordinates [x1, x2]
• ey (list) – element y coordinates [y1, y2]
• ez (list) – element z coordinates [z1, z2]
• ep (list) – element properties [E, A], E - Young’s modulus, A - Cross section area
Return mat Ke stiffness matrix, [6 x 6]
calfem.core.bar3s(ex, ey, ez, ep, ed)
Compute normal force in three dimensional bar element.
Parameters
• ex (list) – element x coordinates [x1, x2]
• ey (list) – element y coordinates [y1, y2]
• ez (list) – element z coordinates [z1, z2]
• ep (list) – element properties [E, A], E - Young’s modulus, A - Cross section area
• ed (list) – element displacements [u1, . . . , u6]
Returns:
Ke element stiffness matrix (6 x 6) Me element mass martix Ce element damping matrix, optional
calfem.core.beam2e(ex, ey, ep, eq=None)
Compute the stiffness matrix for a two dimensional beam element.
Parameters
• ex (list) – element x coordinates [x1, x2]
• ey (list) – element y coordinates [y1, y2]
• ep (list) – element properties [E, A, I], E - Young’s modulus, A - Cross section area, I -
Moment of inertia
• eq (list) – distributed loads, local directions [qx, qy]
Return mat Ke element stiffness matrix [6 x 6]
Return mat fe element stiffness matrix [6 x 1] (if eq!=None)
calfem.core.beam2g(ex, ey, ep, N, eq=None)
Compute the element stiffness matrix for a two dimensional beam element with respect to geometric nonlinear-
ity.
Parameters:
ex = [x1, x2] ey = [y1, y2] element node coordinates
ep = [E,A,I] element properties; E: Young’s modulus A: cross section area I: moment of inertia
N axial force in the beam
eq distributed transverse load
Returns:
Ke element stiffness matrix (6 x 6)
fe element load vector (6 x 1)
calfem.core.beam2gs(ex, ey, ep, ed, N, eq=None)
Calculate section forces in a two dimensional nonlinear beam element.
Parameters:
ex = [x1, x2] ey = [y1, y2] element node coordinates
ep = [E,A,I] element properties; E: Young’s modulus A: cross section area I: moment of inertia
Returns:
eseff = [eseff_0 .. eseff_nel-1]
calfem.core.error(msg)
Write msg to error log.
calfem.core.extractEldisp(edof, a)
Extract element displacements from the global displacement vector according to the topology matrix edof.
Parameters:
a the global displacement vector edof dof topology array
Returns:
ed: element displacement array
calfem.core.flw2i4e(ex, ey, ep, D, eq=None)
Compute element stiffness (conductivity) matrix for 4 node isoparametric field element
Parameters:
ex = [x1 x2 x3 x4] element coordinates ey = [y1 y2 y3 y4]
ep = [t ir] thickness and integration rule
D = [[kxx kxy], [kyx kyy]] constitutive matrix
eq heat supply per unit volume
Returns:
es = [[qx, qy], [.., ..]] element flows
et = [[qx, qy], [. . . ..]] element gradients
eci=[[ix1, iy1], Gauss point location vector [. . . . . . ], nint: number of integration points [ix(nint),
iy(nint)]
Returns:
es = [[qx,qy], [..,..]] element flows
et = [[qx,qy], [..,..]] element gradients
eci=[[ix1,iy1], Gauss point location vector [. . . ,. . . ], nint: number of integration points
[ix(nint),iy(nint)]]
et = [ epsx epsy [epsz] gamxy element strain matrix . . . . . . ] one row for each element
elDistribVal - Float. Determines how severe the element distribution is. Only works for structured
meshes. elOnCurv and elDistribType must be be defined if this param is used.
bump:
Smaller value means elements are bunched up at the edges of the curve, larger means bunched in the
middle.
progression:
The edge of each element along this curve (from starting point to end) will be larger than the preceding
one by this factor. elDistribVal = 2 meaning for example that each line element in the series will be
twice as long as the preceding one. elDistribVal < 1 makes each element smaller than the preceeding
one.
addEllipse(points, ID=None, marker=0, elOnCurve=None, elDistribType=None, elDistrib-
Val=None)
Adds a Ellipse arc curve.
points - List of 4 indices of point that make a ellipse arc smaller than Pi. [startpoint, centerpoint,
mAxisPoint, endpoint] Startpoint is the starting point of the arc. Centerpoint is the point at the center
of the ellipse. MAxisPoint is any point on the major axis of the ellipse. Endpoint is the end point of
the arc.
ID - Positive integer ID of this curve. If left unspecified the curve will be assigned the smallest unused
curve-ID. It is recommended to specify all curve-IDs or none.
marker - Integer. Marker applied to this curve. Default 0.
elOnCurv - Positive integer. Elements on curve. The number of element edges that will be distributed
along this curve. Only works for structured meshes.
elDistribType - String. Either “bump” or “progression”. Determines how the density of elements vary
along the curve for structured meshes. Only works for structured meshes. elOnCurv and elDistribVal
must be be defined if this param is used.
elDistribVal - Float. Determines how severe the element distribution is. Only works for structured
meshes. elOnCurv and elDistribType must be be defined if this param is used.
bump:
Smaller value means elements are bunched up at the edges of the curve, larger means bunched in the
middle.
progression:
The edge of each element along this curve (from starting point to end) will be larger than the preceding
one by this factor. elDistribVal = 2 meaning for example that each line element in the series will be
twice as long as the preceding one. elDistribVal < 1 makes each element smaller than the preceeding
one.
addPoint(coord, ID=None, marker=0, elSize=1)
Adds a point.
Parameters: coord - [x, y] or [x, y, z].
List, not array.
ID - Positive integer ID of this point. If left unspecified the point will be assigned the smallest unused
point-ID. It is recommended to specify all point-IDs or none.
marker - Marker applied to this point. Default 0. It is not a good idea to apply non-zero markers to
points that are control points on B-splines or center points on circles/ellipses, since this can lead to
“loose” nodes that are not part of any elements.
elSize - The size of elements at this point. Default 1. Use to make a mesh denser or sparser here. Only
affects unstructured meshes
ID - Positive integer ID of this surface. If left unspecified the surface will be assigned the smallest un-
used surface-ID. It is recommended to specify all surface-IDs or none.
ID - Positive integer ID of this surface. If left unspecified the surface will be assigned the smallest un-
used surface-ID. It is recommended to specify all surface-IDs or none.
ID - Positive integer ID of this volume. If left unspecified the volume will be assigned the smallest un-
used volume-ID. It is recommended to specify all volume-IDs or none.
holes - List of lists of curve IDs that make up the inner boundaries of the surface. The curves must lie
in the same plane.
ID - Positive integer ID of this surface. If left unspecified the surface will be assigned the smallest un-
used surface-ID. It is recommended to specify all surface-IDs or none.
holes - List of lists of surface IDs that make up the inner boundaries of the volume.
ID - Positive integer ID of this volume. If left unspecified the volume will be assigned the smallest un-
used volume-ID. It is recommended to specify all volume-IDs or none.
The edge of each element along this curve (from starting point to end) will be larger than the preceding
one by this factor. elDistribVal = 2 meaning for example that each line element in the series will be
twice as long as the preceding one. elDistribVal < 1 makes each element smaller than the preceeding
one.
circle(points, ID=None, marker=0, elOnCurve=None, elDistribType=None, elDistribVal=None)
Adds a Circle arc curve.
points - list of 3 indices of point that make a circle arc smaller than Pi. [startpoint, centerpoint, end-
point]
ID - Positive integer ID of this curve. If left unspecified the curve will be assigned the smallest unused
curve-ID. It is recommended to specify all curve-IDs or none.
marker - Marker applied to this curve. Default 0.
elOnCurv - Elements on curve. The number of element edges that will be distributed along this curve.
Only works for structured meshes.
elDistribType - String. Either “bump” or “progression”. Determines how the density of elements vary
along the curve for structured meshes. Only works for structured meshes. elOnCurv and elDistribVal
must be be defined if this param is used.
elDistribVal - Float. Determines how severe the element distribution is. Only works for structured
meshes. elOnCurv and elDistribType must be be defined if this param is used.
bump:
Smaller value means elements are bunched up at the edges of the curve, larger means bunched in the
middle.
progression:
The edge of each element along this curve (from starting point to end) will be larger than the preceding
one by this factor. elDistribVal = 2 meaning for example that each line element in the series will be
twice as long as the preceding one. elDistribVal < 1 makes each element smaller than the preceeding
one.
ellipse(points, ID=None, marker=0, elOnCurve=None, elDistribType=None, elDistribVal=None)
Adds a Ellipse arc curve.
points - List of 4 indices of point that make a ellipse arc smaller than Pi. [startpoint, centerpoint,
mAxisPoint, endpoint] Startpoint is the starting point of the arc. Centerpoint is the point at the center
of the ellipse. MAxisPoint is any point on the major axis of the ellipse. Endpoint is the end point of
the arc.
ID - Positive integer ID of this curve. If left unspecified the curve will be assigned the smallest unused
curve-ID. It is recommended to specify all curve-IDs or none.
marker - Integer. Marker applied to this curve. Default 0.
elOnCurv - Positive integer. Elements on curve. The number of element edges that will be distributed
along this curve. Only works for structured meshes.
elDistribType - String. Either “bump” or “progression”. Determines how the density of elements vary
along the curve for structured meshes. Only works for structured meshes. elOnCurv and elDistribVal
must be be defined if this param is used.
elDistribVal - Float. Determines how severe the element distribution is. Only works for structured
meshes. elOnCurv and elDistribType must be be defined if this param is used.
bump:
Smaller value means elements are bunched up at the edges of the curve, larger means bunched in the
middle.
progression:
The edge of each element along this curve (from starting point to end) will be larger than the preceding
one by this factor. elDistribVal = 2 meaning for example that each line element in the series will be
twice as long as the preceding one. elDistribVal < 1 makes each element smaller than the preceeding
one.
getPointCoords(IDs=None)
Returns an N-by-3 list of point coordinates if the parameter is a list of IDs. If the parameter is just a single
integer then a single coordinate (simple 3-element list) is returned. If the parameter is undefined (or None)
all point coords will be returned
line(points, ID=None, marker=0, elOnCurve=None, elDistribType=None, elDistribVal=None)
Adds a Spline curve
points - List of indices of control points that make a Spline [p1, p2, . . . , pn]
ID - Positive integer ID of this curve. If left unspecified the curve will be assigned the smallest unused
curve-ID. It is recommended to specify all curve-IDs or none.
marker - Integer. Marker applied to this curve. Default 0.
elOnCurv - Positive integer. Elements on curve. The number of element edges that will be distributed
along this curve. Only works for structured meshes.
elDistribType - String. Either “bump” or “progression”. Determines how the density of elements vary
along the curve for structured meshes. Only works for structured meshes. elOnCurv and elDistribVal
must be be defined if this param is used.
elDistribVal - Float. Determines how severe the element distribution is. Only works for structured
meshes. elOnCurv and elDistribType must be be defined if this param is used.
bump:
Smaller value means elements are bunched up at the edges of the curve, larger means bunched in the
middle.
progression:
The edge of each element along this curve (from starting point to end) will be larger than the preceding
one by this factor. elDistribVal = 2 meaning for example that each line element in the series will be
twice as long as the preceding one. elDistribVal < 1 makes each element smaller than the preceeding
one.
point(coord, ID=None, marker=0, elSize=1)
Adds a point.
Parameters: coord - [x, y] or [x, y, z].
List, not array.
ID - Positive integer ID of this point. If left unspecified the point will be assigned the smallest unused
point-ID. It is recommended to specify all point-IDs or none.
marker - Marker applied to this point. Default 0. It is not a good idea to apply non-zero markers to
points that are control points on B-splines or center points on circles/ellipses, since this can lead to
“loose” nodes that are not part of any elements.
elSize - The size of elements at this point. Default 1. Use to make a mesh denser or sparser here. Only
affects unstructured meshes
pointsOnCurves(IDs)
Returns a list of all geometric points (not nodes) on the curves specified in IDs. IDs may be an integer or
a list of integers.
removeCurve(ID)
Removes the curve with this ID
removePoint(ID)
Removes the point with this ID
removeSurface(ID)
Removes the surface with this ID
removeVolume(ID)
Removes the volume with this ID
ruledSurface(outerLoop, ID=None, marker=0)
Adds a Ruled Surface (bent surface). Parameters: outerLoop - List of 3 or 4 curve IDs that make up the
boundary of
the surface.
ID - Positive integer ID of this surface. If left unspecified the surface will be assigned the smallest un-
used surface-ID. It is recommended to specify all surface-IDs or none.
ID - Positive integer ID of this surface. If left unspecified the surface will be assigned the smallest un-
used surface-ID. It is recommended to specify all surface-IDs or none.
elDistribType - String. Either “bump” or “progression”. Determines how the density of elements vary
along the curve for structured meshes. Only works for structured meshes. elOnCurv and elDistribVal
must be be defined if this param is used.
elDistribVal - Float. Determines how severe the element distribution is. Only works for structured
meshes. elOnCurv and elDistribType must be be defined if this param is used.
bump:
Smaller value means elements are bunched up at the edges of the curve, larger means bunched in the
middle.
progression:
The edge of each element along this curve (from starting point to end) will be larger than the preceding
one by this factor. elDistribVal = 2 meaning for example that each line element in the series will be
twice as long as the preceding one. elDistribVal < 1 makes each element smaller than the preceeding
one.
structuredSurface(outerLoop, ID=None, marker=0)
Adds a Structured Surface. Parameters: outerLoop - List of 4 curve IDs that make up the boundary of
the surface. The curves must be structured, i.e. their parameter ‘elOnCurv’ must be defined.
ID - Positive integer ID of this surface. If left unspecified the surface will be assigned the smallest un-
used surface-ID. It is recommended to specify all surface-IDs or none.
ID - Positive integer ID of this volume. If left unspecified the volume will be assigned the smallest un-
used volume-ID. It is recommended to specify all volume-IDs or none.
ID - Positive integer ID of this surface. If left unspecified the surface will be assigned the smallest un-
used surface-ID. It is recommended to specify all surface-IDs or none.
ID - Positive integer ID of this volume. If left unspecified the volume will be assigned the smallest un-
used volume-ID. It is recommended to specify all volume-IDs or none.
stuffOnSurfaces(IDs)
Returns lists of all geometric points and curves on the surfaces specified in IDs. IDs may be an integer or
a list of integers
stuffOnVolumes(IDs)
Returns lists of all geometric points, curves, and surfaces on the volumes specified in IDs. IDs may be an
integer or a list of integers
surface(outerLoop, holes=[], ID=None, marker=0)
Adds a plane surface (flat). Parameters: outerLoop - List of curve IDs that make up the outer boundary of
the surface. The curves must lie in the same plane.
holes - List of lists of curve IDs that make up the inner boundaries of the surface. The curves must lie
in the same plane.
ID - Positive integer ID of this surface. If left unspecified the surface will be assigned the smallest un-
used surface-ID. It is recommended to specify all surface-IDs or none.
holes - List of lists of surface IDs that make up the inner boundaries of the volume.
ID - Positive integer ID of this volume. If left unspecified the volume will be assigned the smallest un-
used volume-ID. It is recommended to specify all volume-IDs or none.
bdofs Boundary dofs. Dictionary containing lists of dofs for each boundary marker.
Dictionary key = marker id.
elementmarkers List of integer markers. Row i contains the marker of element i.
Markers are similar to boundary markers and can be used to identify in which region
an element lies.
boundaryElements (optional) returned if self.return_boundary_elements is true.
Contains dictionary with boundary elements. The keys are markers and the values are
lists of elements for that marker.
bdofs Boundary dofs. Dictionary containing lists of dofs for each boundary marker. Dictionary
key = marker id.
Returns:
bcPresc Updated 1-dim integer array containing prescribed dofs. bcVal Updated 1-dim float array
containing prescribed values.
calfem.utils.applyforce(boundaryDofs, f, marker, value=0.0, dimension=0)
Apply boundary force to f matrix. The value is added to all boundaryDofs defined by marker. Applicable to 2D
problems with 2 dofs per node.
Parameters:
boundaryDofs Dictionary with boundary dofs. f force matrix. marker Boundary marker to assign
boundary condition. value Value to assign boundary condition.
If not given 0.0 is assigned.
dimension dimension to apply force. 0 - all, 1 - x, 2 - y
calfem.utils.applyforce3D(boundaryDofs, f, marker, value=0.0, dimension=0)
Apply boundary force to f matrix. The value is added to all boundaryDofs defined by marker. Applicable to 3D
problems with 3 dofs per node.
Parameters:
boundaryDofs Dictionary with boundary dofs. f force matrix. marker Boundary marker to assign
boundary condition. value Value to assign boundary condition.
If not given 0.0 is assigned.
calfem.utils.readFloat(f )
Read a row from file, f, and return a list of floats.
calfem.utils.readInt(f )
Read a row from file, f, and return a list of integers.
calfem.utils.readSingleFloat(f )
Read a single float from a row in file f. All other values on row are discarded.
calfem.utils.readSingleInt(f )
Read a single integer from a row in file f. All other values on row are discarded.
calfem.utils.scalfact2(ex, ey, ed, rat=0.2)
Determine scale factor for drawing computational results, such as displacements, section forces or flux.
Parameters:
ex, ey element node coordinates
ed element displacement matrix or section force matrix
rat relation between illustrated quantity and element size. If not specified, 0.2 is used.
calfem.utils.which(filename)
Return complete path to executable given by filename.
coords - An N-by-2 or N-by-3 array. Row i contains the x,y,z coordinates of node i.
edof - An E-by-L array. Element topology. (E is the number of elements and L is the number of dofs per
element)
dofsPerNode - Integer. Dofs per node. elType - Integer. Element Type. See Gmsh manual for details. Usually 2
for triangles or 3 for quadrangles.
nodeVals - An N-by-1 array or a list of scalars. The Scalar values at the nodes. nodeVals[i] should be the
value of node i.
clim - 2-tuple. Colorbar limits (min, max). Defines the value range of the colorbar. Defaults to None, in
which case min/max are set to min/max of nodeVals.
axes - Visvis Axes. The Axes where the model will be drawn. If unspecified the current Axes will be used,
or a new Axes will be created if none exist.
axesAdjust - Boolean. True if the view should be changed to show the whole model. Default True.
doDrawMesh - Boolean. True if mesh wire should be drawn. Default True. magnfac - Float. Magnification
factor. Displacements are multiplied by
this value. Use this to make small displacements more visible.
title - String. Changes title of the figure. Default None (in which case title depends on other parameters).
coords - An N-by-2 or N-by-3 array. Row i contains the x,y,z coordinates of node i.
edof - An E-by-L array. Element topology. (E is the number of elements and L is the number of dofs per
element)
dofsPerNode - Integer. Dofs per node. elType - Integer. Element Type. See Gmsh manual for details. Usually 2
for triangles or 3 for quadrangles.
displacements - An N-by-2 or N-by-3 array. Row i contains the x,y,z displacements of node i.
clim - 2-tuple. Colorbar limits (min, max). Defines the value range of the colorbar. Defaults to None, in
which case min/max are set to min/max of nodeVals.
axes - Visvis Axes. The Axes where the model will be drawn. If unspecified the current Axes will be used,
or a new Axes will be created if none exist.
axesAdjust - Boolean. True if the view should be changed to show the whole model. Default True.
doDrawMesh - Boolean. True if mesh wire should be drawn. Default True. doDrawUndisplacedMesh - Boolean.
True if the wire of the undisplaced mesh
should be drawn on top of the displaced mesh. Default False. Use only if displacements != None.
magnfac - Float. Magnification factor. Displacements are multiplied by this value. Use this to make small
displacements more visible.
axes - Visvis Axes. The Axes where the model will be drawn. If unspecified the current Axes will be used,
or a new Axes will be created if none exist.
axesAdjust - Boolean. If True the view will be changed to show the whole model. Default True.
drawPoints - Boolean. If True points will be drawn. labelPoints- Boolean. If True Points will be labeled. The
format is:
ID[marker]. If a point has marker==0 only the ID is written.
labelCurves- Boolean. If True Curves will be labeled. The format is: ID(elementsOnCurve)[marker].
fontSize - Integer. Size of the text in the text labels. Default 11. N - Integer. The number of discrete points per
curve segment.
Default 20. Increase for smoother curves. Decrease for better performance.
calfem.vis.drawMesh(coords, edof, dofsPerNode, elType, axes=None, axesAdjust=True, title=None,
color=(0, 0, 0), faceColor=(1, 1, 1), filled=False)
Draws wire mesh of model in 2D or 3D. Returns the Mesh object that represents the mesh. Parameters: coords
- An N-by-2 or N-by-3 array. Row i contains the x,y,z coordinates
of node i.
edof - An E-by-L array. Element topology. (E is the number of elements and L is the number of dofs per
element)
dofsPerNode - Integer. Dofs per node. elType - Integer. Element Type. See Gmsh manual for details. Usually 2
for triangles or 3 for quadrangles.
axes - Visvis Axes. The Axes where the model will be drawn. If unspecified the current Axes will be used,
or a new Axes will be created if none exist.
axesAdjust - Boolean. True if the view should be changed to show the whole model. Default True.
title - String. Changes title of the figure. Default “Mesh”. color - 3-tuple or char. Color of the wire. Defaults to
black (0,0,0).
Can also be given as a character in ‘rgbycmkw’.
faceColor - 3-tuple or char. Color of the faces. Defaults to white (1,1,1). Parameter filled must be True or
faces will not be drawn at all.
filled - Boolean. Faces will be drawn if True. Otherwise only the wire is drawn. Default False.
coords - An N-by-2 or N-by-3 array. Row i contains the x,y,z coordinates of node i.
edof - An E-by-L array. Element topology. (E is the number of elements and L is the number of dofs per
element)
dofsPerNode - Integer. Dofs per node. elType - Integer. Element Type. See Gmsh manual for details. Usually 2
for triangles or 3 for quadrangles.
clim - 2-tuple. Colorbar limits (min, max). Defines the value range of the colorbar. Defaults to None, in
which case min/max are set to min/max of nodeVals.
axes - Visvis Axes. The Axes where the model will be drawn. If unspecified the current Axes will be used,
or a new Axes will be created if none exist.
axesAdjust - Boolean. True if the view should be changed to show the whole model. Default True.
doDrawMesh - Boolean. True if mesh wire should be drawn. Default True. title - String. Changes title of the
figure. Default “Node Values”.
calfem.vis.draw_displacements(displacements, coords, edof, dofsPerNode, elType, node-
Vals=None, clim=None, axes=None, axesAdjust=True, do-
DrawUndisplacedMesh=True, magnfac=1.0, title=None)
Draws mesh with displacements in 2D or 3D. Scalar nodal values can also be drawn on the mesh. Returns the
displaced Mesh object. Parameters: displacements-An N-by-1 array (or matrix). Row i contains the displace-
ment of
dof i. N-by-2 or N-by-3 arrays are also accepted, in which case row i contains the x,y,z displacements
of node i.
coords - An N-by-2 or N-by-3 array. Row i contains the x,y,z coordinates of node i.
edof - An E-by-L array. Element topology. (E is the number of elements and L is the number of dofs per
element)
dofsPerNode - Integer. Dofs per node. elType - Integer. Element Type. See Gmsh manual for details. Usually 2
for triangles or 3 for quadrangles.
nodeVals - An N-by-1 array or a list of scalars. The Scalar values at the nodes. nodeVals[i] should be the
value of node i.
clim - 2-tuple. Colorbar limits (min, max). Defines the value range of the colorbar. Defaults to None, in
which case min/max are set to min/max of nodeVals.
axes - Visvis Axes. The Axes where the model will be drawn. If unspecified the current Axes will be used,
or a new Axes will be created if none exist.
axesAdjust - Boolean. True if the view should be changed to show the whole model. Default True.
doDrawMesh - Boolean. True if mesh wire should be drawn. Default True. magnfac - Float. Magnification
factor. Displacements are multiplied by
this value. Use this to make small displacements more visible.
title - String. Changes title of the figure. Default None (in which case title depends on other parameters).
coords - An N-by-2 or N-by-3 array. Row i contains the x,y,z coordinates of node i.
edof - An E-by-L array. Element topology. (E is the number of elements and L is the number of dofs per
element)
dofsPerNode - Integer. Dofs per node. elType - Integer. Element Type. See Gmsh manual for details. Usually 2
for triangles or 3 for quadrangles.
displacements - An N-by-2 or N-by-3 array. Row i contains the x,y,z displacements of node i.
clim - 2-tuple. Colorbar limits (min, max). Defines the value range of the colorbar. Defaults to None, in
which case min/max are set to min/max of nodeVals.
axes - Visvis Axes. The Axes where the model will be drawn. If unspecified the current Axes will be used,
or a new Axes will be created if none exist.
axesAdjust - Boolean. True if the view should be changed to show the whole model. Default True.
doDrawMesh - Boolean. True if mesh wire should be drawn. Default True. doDrawUndisplacedMesh - Boolean.
True if the wire of the undisplaced mesh
should be drawn on top of the displaced mesh. Default False. Use only if displacements != None.
magnfac - Float. Magnification factor. Displacements are multiplied by this value. Use this to make small
displacements more visible.
axes - Visvis Axes. The Axes where the model will be drawn. If unspecified the current Axes will be used,
or a new Axes will be created if none exist.
axesAdjust - Boolean. If True the view will be changed to show the whole model. Default True.
drawPoints - Boolean. If True points will be drawn. labelPoints- Boolean. If True Points will be labeled. The
format is:
ID[marker]. If a point has marker==0 only the ID is written.
labelCurves- Boolean. If True Curves will be labeled. The format is: ID(elementsOnCurve)[marker].
fontSize - Integer. Size of the text in the text labels. Default 11. N - Integer. The number of discrete points per
curve segment.
Default 20. Increase for smoother curves. Decrease for better performance.
edof - An E-by-L array. Element topology. (E is the number of elements and L is the number of dofs per
element)
dofsPerNode - Integer. Dofs per node. elType - Integer. Element Type. See Gmsh manual for details. Usually 2
for triangles or 3 for quadrangles.
axes - Visvis Axes. The Axes where the model will be drawn. If unspecified the current Axes will be used,
or a new Axes will be created if none exist.
axesAdjust - Boolean. True if the view should be changed to show the whole model. Default True.
title - String. Changes title of the figure. Default “Mesh”. color - 3-tuple or char. Color of the wire. Defaults to
black (0,0,0).
Can also be given as a character in ‘rgbycmkw’.
faceColor - 3-tuple or char. Color of the faces. Defaults to white (1,1,1). Parameter filled must be True or
faces will not be drawn at all.
filled - Boolean. Faces will be drawn if True. Otherwise only the wire is drawn. Default False.
coords - An N-by-2 or N-by-3 array. Row i contains the x,y,z coordinates of node i.
edof - An E-by-L array. Element topology. (E is the number of elements and L is the number of dofs per
element)
dofsPerNode - Integer. Dofs per node. elType - Integer. Element Type. See Gmsh manual for details. Usually 2
for triangles or 3 for quadrangles.
clim - 2-tuple. Colorbar limits (min, max). Defines the value range of the colorbar. Defaults to None, in
which case min/max are set to min/max of nodeVals.
axes - Visvis Axes. The Axes where the model will be drawn. If unspecified the current Axes will be used,
or a new Axes will be created if none exist.
axesAdjust - Boolean. True if the view should be changed to show the whole model. Default True.
doDrawMesh - Boolean. True if mesh wire should be drawn. Default True. title - String. Changes title of the
figure. Default “Node Values”.
calfem.vis.eldraw2(ex, ey, plotpar, elnum)
eldraw2(ex,ey,plotpar) eldraw2(ex,ey)
PURPOSE Draw the undeformed 2D mesh for a number of elements of the same type. Supported
elements are:
1) -> bar element 2) -> beam el. 3) -> triangular 3 node el. 4) -> quadrilateral 4 node el. 5) ->
8-node isopar. elemen
INPUT
ex,ey:. . . . . . . . . . nen: number of element nodes nel: number of elements
plotpar=[ linetype, linecolor, nodemark]
linetype=1 -> solid linecolor=1 -> black 2 -> dashed 2 -> blue 3 -> dotted 3 -> ma-
genta
4 -> red
nodemark=1 -> circle 2 -> star 0 -> no mark
elnum=edof(:,1) ; i.e. the first column in the topology matrix
Rem. Default is solid white lines with circles at nodes.
calfem.vis.eldraw2_mpl(ex, ey, plotpar=[1, 2, 1], elnum=[])
eldraw2(ex,ey,plotpar,elnum) eldraw2(ex,ey,plotpar) eldraw2(ex,ey)
PURPOSE Draw the undeformed 2D mesh for a number of elements of the same type. Supported
elements are:
1) -> bar element 2) -> beam el. 3) -> triangular 3 node el. 4) -> quadrilateral 4 node el. 5) ->
8-node isopar. elemen
INPUT
ex,ey:. . . . . . . . . . nen: number of element nodes nel: number of elements
plotpar=[ linetype, linecolor, nodemark]
linetype=1 -> solid linecolor=1 -> black 2 -> dashed 2 -> blue 3 -> dotted 3 -> ma-
genta
4 -> red
nodemark=1 -> circle 2 -> star 0 -> no mark
elnum=edof(:,1) ; i.e. the first column in the topology matrix
Rem. Default is solid white lines with circles at nodes.
calfem.vis.eldraw2_old(ex, ey)
Draw elements in 2d.
Parameters:
ex, ey Element coordinates plotpar (not implemented yet)
calfem.vis.eliso2_old(ex, ey, ed, showMesh=False)
Draw nodal values in 2d.
Parameters:
ex, ey Element coordinates ed Element nodal values plotpar (not implemented yet)
calfem.vis.elval2(ex, ey, ev, showMesh=False)
Draw elements values in 2d.
Parameters:
ex, ey Element coordinates ev Element values (scalar) plotpar (not implemented yet)
calfem.vis.error(msg)
Log error message
calfem.vis.figure(figure=None, show=True)
Create a visvis figure with extras.
calfem.vis.figureClass()
Return visvis Figure class.
calfem.vis.figure_class()
Return visvis Figure class.
calfem.vis.gca()
Get current axis of the current visvis figure.
calfem.vis.getColorbar(axes=None)
Returns the Colorbar. If axes is None the colorbar in the current axes will be found. If several colorbars exists
in the axes the first found will be returned If no colorbar is found None is returned.
calfem.vis.get_color_bar(axes=None)
Returns the Colorbar. If axes is None the colorbar in the current axes will be found. If several colorbars exists
in the axes the first found will be returned If no colorbar is found None is returned.
calfem.vis.info(msg)
Log information message
calfem.vis.showAndWait()
Show visvis windows and enter application loop.
calfem.vis.showGrid(flag=True)
Show grid.
calfem.vis.show_and_wait()
Show visvis windows and enter application loop.
calfem.vis.show_grid(flag=True)
Show grid.
calfem.vis.subplot(*args)
Create a visvis subplot.
• genindex
• modindex
• search
51
CALFEM for Python Documentation, Release 3.4.1
c
calfem.core, 17
calfem.geometry, 29
calfem.mesh, 37
calfem.ui, 39
calfem.utils, 39
calfem.vis, 41
53
CALFEM for Python Documentation, Release 3.4.1
B D
bar1e() (in module calfem.core), 17 draw_displacements() (in module calfem.vis), 45
bar1s() (in module calfem.core), 17 draw_element_values() (in module calfem.vis), 45
bar2e() (in module calfem.core), 17 draw_geometry() (in module calfem.vis), 46
bar2g() (in module calfem.core), 18 draw_mesh() (in module calfem.vis), 46
bar2s() (in module calfem.core), 18 draw_nodal_values() (in module calfem.vis), 47
bar3e() (in module calfem.core), 18 drawDisplacements() (in module calfem.vis), 42
bar3s() (in module calfem.core), 18 drawElementValues() (in module calfem.vis), 43
55
CALFEM for Python Documentation, Release 3.4.1
56 Index
CALFEM for Python Documentation, Release 3.4.1
T
trimesh2d() (in module calfem.mesh), 38
V
volume() (calfem.geometry.Geometry method), 37
W
which() (in module calfem.utils), 41
Index 57