Python Manual Pages 1
Python Manual Pages 1
Japan Asia-Pacific
MSC Software Japan Ltd. MSC Software (S) Pte. Ltd.
Shinjuku First West 8F 100 Beach Road
23-7 Nishi Shinjuku #16-05 Shaw Tower
1-Chome, Shinjuku-Ku Singapore 189702
Tokyo 160-0023, JAPAN Telephone: 65-6272-0082
Telephone: (81) (3)-6911-1200 Email: [email protected]
Email: [email protected]
Worldwide Web
www.mscsoftware.com
User Documentation: Copyright 2016 MSC Software Corporation. All Rights Reserved.
U.S. Patent 9,361,413
This document, and the software described in it, are furnished under license and may be used or copied only in accordance with the
terms of such license. Any reproduction or distribution of this document, in whole or in part, without the prior written authorization of
MSC Software Corporation is strictly prohibited.
MSC Software Corporation reserves the right to make changes in specifications and other information contained in this document
without prior notice. The concepts, methods, and examples presented in this document are for illustrative and educational purposes
only and are not intended to be exhaustive or to apply to any particular engineering problem or design. THIS DOCUMENT IS
PROVIDED ON AN “AS-IS” BASIS AND ALL EXPRESS AND IMPLIED CONDITIONS, REPRESENTATIONS AND WARRANTIES,
INCLUDING ANY IMPLIED WARRANTY OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE, ARE
DISCLAIMED, EXCEPT TO THE EXTENT THAT SUCH DISCLAIMERS ARE HELD TO BE LEGALLY INVALID.
MSC Software logo, MSC, MSC Nastran, Adams, Dytran, Marc, Mentat, and Patran are trademarks or registered trademarks of
MSC Software Corporation or its subsidiaries in the United States and/or other countries.
NASTRAN is a registered trademark of NASA. Python is a trademark of the Python Software Foundation. Parasolid is a registered
trademark of Siemens Product Lifecycle Management Software, Inc. All other trademarks are the property of their respective owners.
This software may contain certain third-party software that is protected by copyright and licensed from MSC Software suppliers.
Additional terms and conditions and/or notices may apply for certain third party software. Such additional third party software terms
and conditions and/or notices may be set forth in documentation and/or at http://web.mscsoftware.com/thirdpartysoftware (or
successor website designated by MSC from time to time).
METIS is copyrighted by the regents of the University of Minnesota. MS MPI is developed by Microsoft Corporation. PCGLSS 8.0,
Copyright 1992-2016 Computational Applications and System Integration Inc. All rights reserved. Portions of this software are
owned by Siemens Product Lifecycle Management Software, Inc. Copyright 2016.
Use, duplication, or disclosure by the U.S. Government is subject to restrictions as set forth in FAR 12.212 (Commercial Computer
Software) and DFARS 227.7202 (Commercial Computer Software and Commercial Computer Software Documentation),
as applicable.
MA*V2016*Z*Z*Z*DC-PYTH
Contents
Marc Python Tutorial and Reference Manual
Contents
Preface
Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10
Documentation Conventions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11
1 Introduction
Welcome . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13
Background Information . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 30
Plate Properties . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 36
Postprocessing Basics . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 45
Scalar Values . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 49
Set Basics . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 52
PyPost Basics. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 63
12 PyPost: Plotting
Chapter Overview . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 85
Introduction
Using the Python Modules . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3
Variable Types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3
1 PyMentat References
PyMentat . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5
2 PyPost References
PyPost . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 21
Arithmetic Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 80
Database Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 81
Preface
10 Marc Python Tutorial
Introduction
The purpose of this manual is to introduce the user to the Python modules. The tutorial covers the basics of the modules
and helps the novice Python user in becoming comfortable in programming with these modules through a number of
examples that display some typical uses of creating and processing a model in various stages. This tutorial is not
intended to be a Python programming manual. The book Programming Python by Mark Lutz should be used for this
purpose. This manual does assume that the Python user is experienced in using Mentat.
Part I introduces the user to the PyMentat module. This module interacts with Mentat to create, modify, and post
process models.
Part II introduces the user to the PyPost module. This module functions independently of Mentat to postprocess
a Marc post file (results file).
Part III introduces the user to more advanced uses of the PyMentat and PyPost modules.
Documentation Conventions
Listed below are some font and syntax conventions that are used to make the information in this manual easier to
understand:
• Cross-references (Links) are highlighted in Blue.
• Names of buttons that appear on the Mentat screen are in Initial Caps. The command issued will appear in
lower case, such as *add_nodes. Note that the "*" always precedes the command name to instruct Mentat that
a command is being issued.
• Literal user input and program prompts are in courier font.
• Names of processors are indicated in UPPER CASE.
• A carriage return keystroke is indicated by <CR>.
• The left mouse button is indicated by <ML>.
• The middle mouse button is indicated by <MM>.
• The right mouse button is indicated by <MR>.
1 Introduction
Welcome 13
The Basics of Using PyMentat 13
The Basics of Using PyPost 15
A Brief Look at Python Programming 16
CHAPTER 1 13
Introduction
Welcome
Welcome to Python – a set of API’s designed to be used by Python scripts to enhance the functionality of Mentat.
MAIN
MESH GENERATION
grid ON
nodes ADD
you will see that the command name of *add_nodes is issued in the dialogue area of Mentat. Therefore, if you want
your Python script to add a node, your script should send the command *add_nodes to Mentat.
Unless you are a very experienced Mentat user, you will most likely develop your PyMentat Python script using the
technique outlined in Figure 1-1.
14 Marc Python Tutorial
To aid in the development of converting the procedure files to Python scripts, the Python script shown in Figure 1-2
can be used to generate the Python code for any given Mentat procedure file. To use the script, run it as follows:
python py_create.py mentat.proc my_script.py
This script simply reads the procedure file and wraps a py_send command around it. You need to edit the resulting
Python script to add PARAMETERS and other variables for conditional processing.
CHAPTER 1 15
Introduction
#!/usr/bin/python
import sys
import string
def main(fnamei, fnameo):
i = open(fnamei,"r")
o = open(fnameo,"w")
o.write("from py_mentat import *\n")
o.write("def main():\n")
for line in i.readlines():
s = string.strip(line)
if s == "":
continue
st = " py_send(\"%s \")\n" % s
o.write(st)
o.write("\n")
o.write("if __name__ == '__main__':\n")
o.write(" main()\n")
if __name__ == "__main__":
main(sys.argv[1],sys.argv[2])
Figure 1-2 A Python Script to Convert a Marc Mentat Procedure File to a Python Script
Function Description
close Closes a currently open post file and frees any allocated memory.
dies Returns the number of dies in the post file.
domains Returns the number of domains stored in the post file.
element Returns the element data for a particular element.
elements Returns the number of elements in the current increment.
element_id Returns the id of a particular element.
element_scalar Returns a list of a particular element scalar data for a particular element.
element_scalars Returns the number of element scalars available.
element_scalar_label Returns the name of a particular element scalar.
element_sequence Returns the index number of a particular element ID.
element_tensor Returns a list of a particular element tensor for a particular element.
element_tensors Returns the number of element tensors available.
16 Marc Python Tutorial
Function Description
element_tensor_label Returns the name of a particular element tensor.
element_vector Returns a list of a particular element vector data for a particular element.
element_vectors Returns the number of element vectors available
element_vector_label Returns the name of a particular element vector.
extrapolation Sets the integration point extrapolation method.
increments Returns the number of increments available in the post file
moveto Move to a specified increment in the post file
node Returns the nodal data for a particular node.
nodes Returns the number of nodes for the current increment.
node_displacement Returns the nodal displacement values for a particular node.
node_displacements Returns a 1 if nodal displacements are present in the post file, 0 otherwise.
node_id Returns the ID of a particular node
node_scalar Returns the value of a particular node scalar for a particular node.
node_scalars Returns the number of node scalars available.
node_scalar_label Returns the name of a particular node scalar.
node_sequence Returns the index number of a particular node ID
node_vector Returns the value of a particular node vector at a particular node.
node_vectors Returns the number of node vectors available.
node_vector_label Returns the name of a particular node vector.
post_open Opens a post file and returns the PyPost object of it.
set Returns the data for a particular set.
sets Returns the number of sets in the post file.
title Returns the title of the post file.
A Python script that will use the PyMentat module needs to import the py_mentat module using the following
syntax:
from py_mentat import *
Similarly, a Python script that will use the PyPost module needs to import the py_post module using the following
syntax
from py_post import *
One of the biggest differences between the Python language and other programming languages is that Python does not
denote blocks of code with reserved words or symbols such as if..then..endif (FORTRAN) or if { ... }
(curly braces in C). Instead, indenting is used for this purpose. For example, the take following block of FORTRAN
code:
if(jtype.eq.49) then
ladt=idt+lofr
endif
The block of FORTRAN code would need to be coded as follows in Python:
if jtype == 49:
ladt=idt+lofr
Python matches the amount of indenting to the block of code. The colon at the end of the if statement denotes that it
is a compound statement. All the lines that are to be in that block of code need to be at the same indent level. The block
of code is ended when the indenting level returns to the level of to the compound statement. The examples in the
following chapters will show you more about the Python syntax.
The material covered in this tutorial is very basic and should be easy to access and understand for the first time Python
user. Once you have worked through the sample sessions in Part II you should feel comfortable enough to do a
complete analysis simply by extrapolating from what you have learned in the example problems.
A multi-dimension list is created by first creating a single dimensional list, and then creating the other dimensions, as
follows (a 3x2 array):
A = [None] * 3
for i in range(3)
A[i] = [None] * 2
Chapter 2: PyMentat: A Simple Example
Chapter Overview
In this section, it will be demonstrated how to create a basic Python script that sends commands to Mentat. For this
purpose, a rectangular grid of elements will be created. Upon completion of this chapter, you should have a clearer
understanding of the following areas:
• The basics for a PyMentat script
• How a Python script communicates with Mentat
• Python Menu layout
Lines 2-15 The make_nodes routine creates a grid of s x t nodes, starting in the lower left-hand corner at the
point (xs, ys, 0.0). The function range is a built-in Python function that builds a Python list of integers.
Note line 11: This is where the Mentat command string for adding a node is composed. The
*add_nodes command is placed in a Python string, along with the x,y,z node coordinates. The
statement uses Python’s % operator. It is used on string objects, and works much like C’s sprintf
function. A %f, %g, or %e designates a floating point number, and a %d designates an integer. The
objects on the right of the % symbol are paired with the conversion codes starting with the first % code
inside the quoted string. The py_send routine is called with the string to send the command string
to Mentat, at which time it runs the command string just as if it was typed in the Mentat dialogue area.
Lines 18-31 The make_elements routine performs the element connectivity of the node grid using the Mentat
command *add_elements. The loop beginning in line 24 builds the elements moving across the
node grid in the X-direction and up the grid in the Y-direction.
Lines 33-40 The main routine sets the size of the grid and the starting position.
MAIN
UTILS
PYTHON
To run the script, select the RUN button. The other menu items in Figure 2-1 will be discussed in a later chapter on
sockets. When the RUN button is selected, the browser window as shown in Figure 2-2 appears.
Note the button named RUN AS SEPARATE PROCESS. This will run the script in a separate window and
communicate to Mentat using a BSD socket connection. This item is discussed in a later chapter. In order to use this
option, a Python script would need to use the py_connect routine to complete the socket connection with Mentat.
This will be discussed in the next chapter. Do not select this option for this example. Browse to the
examples/python/c02 directory and select the file chap2.py. Then select OK to run the script.
When the script completes, a mesh is created as shown in Figure 2-3.
24 Marc Python Tutorial
In the next chapter, we will modify the script to use Mentat PARAMETERS to create a variably sized grid.
Chapter 3: PyMentat: Obtaining Data from Mentat
Chapter Overview
In this chapter, it will be demonstrated how to use Mentat PARAMETERS in a Python script using the PyMentat
module. It will also be shown how to run a Python script as a separate process. Upon completion of this chapter, you
should have a clearer understanding of the following areas:
• The py_get_int and py_get_float methods
Lines 2-31 The make_nodes and make_elements routines are the same as in the previous chapter.
Lines 33-39 The main routine obtains the values for the size of the grid and the starting point from Mentat using
parameters. The py_get_int method evaluates a formula and returns an integer (fixed point) result
similar to the *calculator command. The py_get_float method evaluates a formula and returns a
floating point number. In this case, these methods obtain the values for these parameters.
Lines 41-44 If the script is invoked as a separate process, (such as “python chap3.py”), then the Python interpreter
sets the Python variable __name__ to the string ‘__main__’. If this is the case, then the Python
script should attempt to make a socket connection to Mentat. The py_connect method is discussed
in more detail in a later chapter.
the py_get_int or py_get_float method. These parameters may be combined with any of the Arithmetic or
Database functions described in the Marc Python Reference Manual.
When using the py_get_int or py_get_float methods, they must be used on a system of the same byte
architecture. The values are not converted to a machine independent format before being transmitted.
MAIN
UTILS
PARAMETERS
NEW PARAMETER
x_size
10
y_size
8
x_start
-1.0
y_start
-1.0
Remember to press the carriage <CR> after typing in each of the numbers to create the parameters. The procedure file
chap3.proc may be executed to perform the above commands for you if you wish.
You may also type the *define command in Mentat’s command prompt window to create or edit the parameter. For
example, to create the parameter x_size you would type
*define x_size 10
As in the previous chapter, bring up the Python browser window with the menus:
MAIN
UTILS
PYTHON
RUN
When the script completes, a mesh will be created the same as in the previous chapter.
Chapter 4: PyMentat: Building Complex Geometry
Chapter Overview
In this chapter, it will be demonstrated how to build more complex geometry using Python. To continue with the
example discussed in Chapter 3, we will examine a script that creates a more complex model. This example uses
Mentat’s user-defined menu to present a simple menu for controlling the model’s properties.
Upon completion of this chapter, you should have a clearer understanding of the following areas:
• Adding points, curves, and lines
• Become more familiar with Python programming
Background Information
A rectangular plate of user defined width and height contains a varying number of holes of user-defined diameter d,
equally spaced by a user-defined distance s. It is desired that the Python script determine how many holes can be placed
in the plate, and center the array of holes in the plate with the distance from any edge to the nearest hole to be at least
the distance s.
d d d
t=.75
height
d d d
d d d
width
This example is named chap4.py and can be found in the Mentat directory examples/python/tutorial/c04. The
following is the Python script:
1. from py_mentat import *
2. def add_plate(xs,ys,width, height):
3. py_send("*set_curve_type line")
4. py_send("*add_points")
5. py_send("%f %f 0" % (xs, ys))
6. py_send("%f %f 0" % (xs+width, ys))
7. py_send("%f %f 0" % (xs+width, ys+height))
8. py_send("%f %f 0" % (xs, ys+height))
9. py_send("*set_curve_type line")
10. py_send("*add_curves")
11. py_send("1 2")
12. py_send("2 3")
13. py_send("3 4")
14. py_send("4 1")
15. py_send("*fill_view")
16. return
17.
18. def build_plate(xstart,ystart,width, height, diameter, spacing):
19. f = diameter+spacing
20. nx = int((width-spacing)/f)
21. ny = int((height-spacing)/f)
22. if (nx < 1 or ny < 1 ):
23. print "Holes are too big!"
24. return 1
25. print "creating hole grid of ",nx, " by ",ny
26.
27. add_plate(xs,ys,width,height)
28. xs = .5 * (width - f * (nx - 1)) + xstart
29. ys = .5 * (height - f * (ny - 1)) + ystart
30. py_send("*set_curve_type circle_cr")
31. py_send("*add_curves")
32. r = diameter / 2
32 Marc Python Tutorial
Lines 2-16 The add_plate routine creates the plate using four connected curves. It first generates the end points
of the curve using the *add_points command, and then creates the curves by connecting the points
with the *add_curves commands. In line 15, the *fill_view command is issued to center and
fill the plate properly on the screen.
Lines 18-38 The build_plate routine calculates the number of holes that can be placed in the plate and creates them
in their proper position. The variables nx and ny in lines 20 and 21 are the number of holes to be
placed in the x and y direction, respectively. The Python int function is used to force the values to be
an integer. Once it has been determined that the plate is big enough to contain at least one hole, the
add_plate routine is called in line 27 to create the plate itself. The initial x and y position for the first
hole is calculated in lines 28-29. The loop in lines 38-44 controls the vertical (height) placement, and
the inner loop in lines 35-37 controls the horizontal (width) placement.
Lines 40-48 The main routine obtains the values for the size of the plate, the diameter of the holes, and the spacing
between holes from Mentat. The lower-left corner of the plate is calculated in lines 45-46 so that the
plate has its center at (0,0). The build_plate routine is then called to create the geometry.
Lines 50-52 If run in a separate process, make a socket connection to Mentat.
CHAPTER 4 33
PyMentat: Building Complex Geometry
WIDTH
12.0
HEIGHT
8.0
DIAMETER
.75
SPACING
0.4
MESH SIZE
0.2
Remember to press the carriage return <CR> after typing in the numbers. Note that the MESH SIZE parameter is not
used in this example. When you specify these values, the menu creates these parameters in Mentat, and the values for
these parameters are obtained by the Python script to control the number of holes in the plate. Select the BUILD button
to create the model.
When the script completes, a plate will be created as shown in Figure 4-2.
34 Marc Python Tutorial
In the next chapter, we will mesh the plate and add the geometric and material properties, boundary conditions, and
job parameters.
Chapter 5: PyMentat: Adding Model Properties
Chapter Overview
In this chapter, it will be demonstrated how to add properties to the geometry create in the previous chapter.
Upon completion of this chapter, you should have a clearer understanding of the following areas:
• Using the Database functions
• Node and Element IDs
Plate Properties
As shown in Chapter 4, a rectangular plate of user defined width and height contains a varying number of holes of
user-defined diameter d, equally spaced by a distance s specified by the user. The array of holes will be centered in the
plate, with the distance from any edge to the nearest hole to be at least the distance s.
In this chapter, we will update the Python script to apply the properties described in Figure 5-1 to the plate. The material
behavior is linear elastic with Young’s modulus E = 30e6 and Poisson’s ratio = 0.3. A compression load with
magnitude p = 1000 Lb/in2 will be applied to the top of the plate.
Y
1000 Lb/in2
d d d
t=.75
height
fixed d d d
d d d
width
This example is named chap5.py and can be found in the Mentat directory examples/python/tutorial/c05. The
following Python code has been added to the code shown in the previous chapter:
79. # check if f1 == f2 within tolerance of tol
80. def check_tol(f1, f2, tol):
81. if f1 == f2:
82. return 1
83. if f1 + tol < f2:
84. if f1 - tol > f2:
85. return 1
86. return 0
87.
88. def mesh_plate(width, height, diameter, spacing):
89. py_send("*set_curve_div_type_fix_avgl")
90. l = spacing/2.0
91. py_send("*set_curve_div_avgl %f" % l)
92. py_send("*apply_curve_divisions all_existing")
93. py_send("*dt_planar_trimesh all_existing")
94. return
95.
96. def add_bc(xs,ys,width,height):
97. py_send("*renumber_all")
98. n = py_get_int("nnodes()")
99. # add fixed bc at left edge
100. py_send("*apply_type fixed_displacement")
101. py_send("*apply_dof x")
102. py_send("*apply_dof y")
103. py_send("*apply_dof z")
104.
105. node_list = []
106. for i in range(1,n+1):
107. str = "node_x(%d)" % i
108. f = py_get_float(str)
109. if check_tol(f, xs, .001):
110. node_list.append(i)
38 Marc Python Tutorial
111.
112. py_send("*add_apply_nodes ")
113. for i in range(0,len(node_list)):
114. str = "%d " % node_list[i]
115. py_send(str)
116. py_send(" # ")
117. # add edge load at top
118. py_send("*new_apply")
119. py_send("*apply_type edge_load")
120. py_send("*apply_value p 1000.0 ")
121. py_send("*add_apply_curves 3 #")
122. return
123.
124. def add_matl():
125. py_send("*material_type mechanical:isotropic")
126. py_send("*material_value isotropic:youngs_modulus 3e7 .3")
127. py_send("*add_material_elements all_existing")
128. return
129.
130. def add_job():
131. py_send("*loadcase_type static")
132. py_send("*job_class mechanical")
133. py_send("*job_option dimen:pstress")
134. py_send("*add_post_tensor stress")
135. py_send("*add_post_var von_mises")
136. py_send("*element_type 6 all_existing")
137. py_send("*element_type 6 all_existing")
138. return
139.
140. def add_geom_prop():
141. py_send("*geometry_type mech_planar_pstress ")
142. py_send("*geometry_value thick .75")
143. py_send("*add_geometry_elements all_existing")
144. return
CHAPTER 5 39
PyMentat: Adding Model Properties
145.
146. def main():
147. xs = 0.0
148. ys = 0.0
149. width = py_get_float("width")
150. height = py_get_float("height")
151. diameter = py_get_float("diameter")
152. spacing = py_get_float("spacing")
153. build_plate(xs,ys,width, height, diameter, spacing)
154. mesh_plate(width, height, diameter, spacing)
155. add_bc(xs,ys,width, height)
156. add_matl()
157. add_geom_prop()
158. add_job()
159. py_send("save_as_model chap5.mfd yes")
160. return
161.
162. if __name__ == ’__main__’:
163. py_connect("",40007)
164. main()
165. py_disconnect()
166.
Lines 2-8 The check_tol routine is a convenience function to compare two floating point number for equality
within a certain tolerance.
Lines 10-17 The mesh_plate routine generates a 2-D planar Delaunay triangle mesh. To generate the curve
divisions, the spacing between the holes is used to control the average length of the segments.
Lines 19-46 The add_bc routine creates the boundary conditions for the plate. The left edge of the plate is fixed,
and the top edge of the plate has an edge load. We first obtain the number of nodes using the Database
function nnodes in line 3. In line 11, we renumber all the nodes in the database so that the node index
is the same as the node ID. This will be discussed more in detail later in this chapter. We set the
displacement type to be fixed in lines 6-12. The code in lines 14-20 searches through the entire node
list looking for a node that lies on the left edge of the plate by examining it’s position in the X direction.
If a match is found, we obtain the node ID (which may be different from it’s index position in the
database) and store that in our node_list. In lines 21-25, the command *add_apply_nodes is used
to apply the fixed boundary conditions. We loop through the node_list and send the IDs, and then #
symbol to signal the End List command. Lines 28-31 is where the top edge load is created. Here,
we simply apply the load to the curve at the top of the plate (curve number 3).
40 Marc Python Tutorial
Lines 48-52 The add_matl routine applies the material properties to all the elements of the plate.
Lines 54-61 The add_job routine creates the necessary job parameters.
Lines 63-67 The add_geom_prop routine applies the necessary geometric properties.
Lines 69-83 The main routine is similar to that of Chapter 4, however, the additional routines are called to add the
boundary conditions, the material properties, the geometric properties, and the job parameters.
x = -1.0
y = -1.0
z = 0.0
ID = 1
x = 1.0
y = -1.0
z = 0.0
ID = 2
x = 1.0
y = 1.0
z = 0.0
ID = 3
x = -1.0
y = 1.0
z = 0.0
ID = 4
The linked list of nodes in Figure 5-2 shows a square grid of nodes 1,2,3,4. If node 2 is deleted, then the list would
become 1,3,4 as shown in Figure 5-3.
CHAPTER 5 41
PyMentat: Adding Model Properties
x = -1.0
y = -1.0
z = 0.0
ID = 1
x = 1.0
y = 1.0
z = 0.0
ID = 3
x = -1.0
y = 1.0
z = 0.0
ID = 4
If the database function node_id is now called with the value of 2, it returns with 3, since the second item in the list
is node 3. If the command renumber_all is issued, then the nodes will be renumbered to the sequence 1,2,3 and
node_id(2) will return 2.
If we did not issue the renumber_all command, then the code sequence in lines 20-24 would need to be:
for i in range(1,n+1):
str = "node_id(%d)" % i
j = py_get_int(str)
str = "node_x(%d)" % j
f = py_get_float(str)
if check_tol(f, xs, .001):
node_list.append(j)
We want to avoid using the node_id function as much as possible because it needs to search the entire database to
find the node.
UTIL
PARAMETERS
NEW PARAMETER
WIDTH
8.0
HEIGHT
42 Marc Python Tutorial
4.0
DIAMETER
1.0
SPACING
0.5
Remember to press the carriage return <CR> after typing in each of the numbers. Select the BUILD button to create the
model.
When the script completes, a mesh will be created as shown in Figure 5-4.
MAIN
JOBS
RUN
SUBMIT 1
In the next chapter, we will examine postprocessing using the PyMentat module.
Chapter 6: PyMentat: Processing a Post File
Chapter Overview
In this chapter, it will be demonstrated how to use the Postprocessing commands of Mentat in a Python script. This
example will use the post file of the example created in the previous chapter.
Upon completion of this chapter, you should have a clearer understanding of the following areas:
• The basics of using PyMentat for Postprocessing
• How to write an efficient PyMentat postprocessing PyMentat script
• Selecting nodes for display
Postprocessing Basics
PyMentat may also be used to postprocess a post file.
In this example, we will obtain all of the nodal scalar values on the post file generated by running the example in the
previous chapter.
This example is named chap6.py and can be found in the Mentat directory examples/python/tutorial/c06. The
following is the Python code:
1. from py_mentat import *
2.
3. def main():
4. py_send("*post_close")
5. py_send("*post_open chap5_job1.t16")
6. py_send("*post_next")
7. py_send("*fill_view")
8. py_send("*post_contour_lines")
9.
10. n = py_get_int("nnodes()") # get number of nodes
11. label = []
12. label.append("Displacement x")
13. label.append("Displacement y")
14. label.append("*External Force x")
15. label.append("External Force y")
16. label.append("Reaction Force x")
17. label.append("Reaction Force y")
18.
19. max_scalars = []