OpenSees Tcl to Python Converter

Although Python is a more popular programming language than Tcl, I suspect Tcl remains the most used language for OpenSees. Python is gaining ground though as it appeals to newer users of OpenSees. I don’t have data to back any of this up.

To help the transition from Tcl to Python, OpenSeesPy was designed to be unpythonic. A pythonic implementation would have alienated a lot of OpenSees users–myself included.

Instead, the commands are basically one-to-one between the two interpreters, making conversions from Tcl to Python straightforward. A few years ago, I wrote a simple converter script that is more procedural than a television crime drama.

The basic assumption is that the OpenSees Tcl scripts you would like to convert to OpenSeesPy contain line by line commands with no variable substitutions, expressions, procedures, or control structures (if/else, while, etc.). Perfect for the Tcl files generated by OpenSees Navigator or some other front end.

If you don’t have simple commands, you’ll have to home brew some middleware as described in the converter script as well as in this post. There’s a few other caveats with the converter as well, but I don’t want to get down in the weeds.

Suppose we want to convert this simple OpenSees Tcl script, beam.tcl, to OpenSeesPy.

wipe
model basic -ndm 2 -ndf 3

node 1 0 0
node 2 10 0
node 3 25 0
node 4 35 0

fix 1 1 1 0
fix 4 0 1 0

section Elastic 4 29000.0 20.0 1400.0

uniaxialMaterial Elastic 1 29000.0
section Fiber 7 {
    patch rect 1 10 10 -1 -1 1 1
}

geomTransf Linear 8

# Original element format
element nonlinearBeamColumn 1 1 2 3 4 8
# Newer element format
element forceBeamColumn 2 2 3 8 Legendre 7 2
# Newest element format
beamIntegration Lobatto 5 7 3
element forceBeamColumn 3 3 4 8 5

timeSeries Constant 6
pattern Plain 1 6 {
    load 2 0 -10 0
}

recorder Element -file output.out -ele 2 section 1 force

analysis Static
analyze 1

reactions

The main complications are converting the nonlinearBeamColumn and forceBeamColumn command formats to the newest format with beamIntegration objects, as well as removing braces from the fiber section and load pattern commands. Check the converter script for those hacks.

To convert the beam.tcl file to OpenSeesPy, use the following commands in a Python script.

exec(open('toOpenSeesPy.py').read())

outfile = open('beam.py','w')

outfile.write('import openseespy.opensees as ops\n\n')
toOpenSeesPy('beam.tcl',outfile,'ops')
# outfile.write('from openseespy.opensees import *\n\n')
# toOpenSeesPy('beam.tcl',outfile)
# toOpenSeesPy('anotherScript.tcl',outfile)

outfile.close()

Note that you are responsible for the import statement, but the converter script will accommodate any alias you choose, including no alias at all. You can also convert multiple OpenSees Tcl scripts to OpenSeesPy.

The resulting beam.py file is shown below.

import openseespy.opensees as ops

ops.wipe()
ops.model('basic','-ndm',2,'-ndf',3)

ops.node(1,0,0)
ops.node(2,10,0)
ops.node(3,25,0)
ops.node(4,35,0)

ops.fix(1,1,1,0)
ops.fix(4,0,1,0)

ops.section('Elastic',4,29000.0,20.0,1400.0)

ops.uniaxialMaterial('Elastic',1,29000.0)
ops.section('Fiber',7)
ops.patch('rect',1,10,10,-1,-1,1,1)

ops.geomTransf('Linear',8)

# Original element format
ops.beamIntegration('Lobatto',1,4,3)
ops.element('forceBeamColumn',1,1,2,8,1)
# Newer element format
ops.beamIntegration('Legendre',2,7,2)
ops.element('forceBeamColumn',2,2,3,8,2)
# Newest element format
ops.beamIntegration('Lobatto',5,7,3)
ops.element('forceBeamColumn',3,3,4,8,5)

ops.timeSeries('Constant',6)
ops.pattern('Plain',1,6)
ops.load(2,0,-10,0)

ops.recorder('Element','-file','output.out','-ele',2,'section',1,'force')

ops.analysis('Static')
ops.analyze(1)

ops.reactions()

This converter script is not perfect, so check the output. If you see errors or conversions that need to be improved, please let me know or make a PR.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.