0% found this document useful (0 votes)
14 views45 pages

Python Manual Pages 1

The document is the Marc® Python 2016 Tutorial and Reference Manual, providing comprehensive guidance on using the Marc software with Python. It includes sections on the basics of Python programming, creating scripts for Mentat, and processing data, along with numerous examples and scripts. The manual is intended for educational purposes and is subject to copyright and licensing restrictions.

Uploaded by

qq2109235237
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
14 views45 pages

Python Manual Pages 1

The document is the Marc® Python 2016 Tutorial and Reference Manual, providing comprehensive guidance on using the Marc software with Python. It includes sections on the basics of Python programming, creating scripts for Mentat, and processing data, along with numerous examples and scripts. The manual is intended for educational purposes and is subject to copyright and licensing restrictions.

Uploaded by

qq2109235237
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 45

Marc® Python 2016

Tutorial and Reference Manual


Corporate Europe, Middle East, Africa
MSC Software Corporation MSC Software GmbH
4675 MacArthur Court, Suite 900 Am Moosfeld 13
Newport Beach, CA 92660 81829 Munich, Germany
Telephone: (714) 540-8900 Telephone: (49) 89 431 98 70
Toll Free Number: 1 855 672 7638 Email: [email protected]
Email: [email protected]

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

Marc Python Tutorial

Preface
Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10

Organization of this Manual . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10

Contents of the Chapters . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10

Documentation Conventions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11

1 Introduction
Welcome . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13

The Basics of Using PyMentat . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13

The Basics of Using PyPost . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15

A Brief Look at Python Programming . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 16


Python Data Types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17

2 PyMentat: A Simple Example


Chapter Overview . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 20

Creating a Simple Python Script for Mentat . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 20

Python Programming Hints . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 22

Running the Script. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 22


4 Marc Python Tutorial and Reference Manual

3 PyMentat: Obtaining Data from Mentat


Chapter Overview . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 26

Creating a Simple Python Script for Mentat . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 26

The py_get_int and py_get_float Methods . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 27

Running the Script . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 28

4 PyMentat: Building Complex Geometry


Chapter Overview . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 30

Background Information . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 30

Running the Script . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 33

5 PyMentat: Adding Model Properties


Chapter Overview . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 36

Plate Properties . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 36

Node and Element IDs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 40

Running the Script . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 41

Submitting the Job . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 43

6 PyMentat: Processing a Post File


Chapter Overview . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 45

Postprocessing Basics . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 45

Scalar Values . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 49

Running the Script . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 49

7 PyMentat: Obtaining Model Data


Chapter Overview . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 51

Database Properties Basics . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 51


Contents 5

Set Basics . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 52

A Sample Script to Extract Data . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 52

Running the Script . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 57

Set ID’s and Set Names . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 61

8 PyPost: Reading a Post File


Chapter Overview . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 63

PyPost Basics. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 63

Running the Script . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 65

9 PyPost: Obtaining Element Data


Chapter Overview . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 68

Processing Element Data . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 68

Running the Script . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 72

10 PyPost: Element Tensor Data


Chapter Overview . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 75

Processing Element Tensor Data . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 75

Running the Script . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 78

11 PyMentat: Using the py_connect Method


Chapter Overview . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 80

Creating a Simple Python Script for Marc Mentat . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 80

The py_connect Method . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 80

Handling Socket Errors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 81

Running the Script . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 82


6 Marc Python Tutorial and Reference Manual

12 PyPost: Plotting
Chapter Overview . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 85

Charting Script for Mentat. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 85

The Gnuplot Module . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 88

The PyOpenGL Module . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 92

13 PyMentat: Menu System Lookup Functions


Chapter Overview . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 103

Menu System Lookup Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 103

Integer Function . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 104

Double Function. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 104

String Function . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 104

Boolean Function. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 105

State Function . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 106

14 MentatDCOM: Connecting to Mentat using a .NET Module


Chapter Overview . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 111

MentatDCOM Basics . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 111

Running the Program . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 114

MentatDCOM and PyMentat . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 115

15 MarcPost: Reading a Post File using a .NET Module


Chapter Overview . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 118

MarcPost Basics . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 118

Running the Program . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 121

MarcPost and PyPost . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 122


Contents 7

Marc Python Reference

Introduction
Using the Python Modules . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3

Variable Types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3

1 PyMentat References
PyMentat . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5

2 PyPost References
PyPost . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 21

A Arithmetic and Database Functions


Introduction. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 80

Arithmetic Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 80

Database Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 81

Utility Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 101

Post File Database Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 102

Set Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 104


Marc Python Tutorial
Marc Python

Marc Python Tutorial


Preface

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.

Organization of this Manual


This manual is divided into three parts:

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.

Contents of the Chapters

Introduction provides information on the basics using PyMentat and of Python


programming.
PART I PyMentat Module
PyMentat: A Simple Example describes a basic PyMentat script.
PyMentat: Obtaining Data from Mentat expands on the common features of PyMentat and describes how a
Python script can get data from Mentat.
PyMentat: Building Complex Geometry displays how you can build complex geometry that cannot be done with
procedure files alone.
PyMentat: Adding Model Properties shows how to add boundary conditions, material properties, geometric
properties and job parameters to allow you to perform a complete linear
elastic analysis of a rectangular strip with a series of holes subjected to
compression loading.
PyMentat: Processing a Post File shows how to process a Marc post file.
PyMentat: Sets shows how to write PyMentat scripts to work with SETS.
11
Preface

PART II PyPost Module


PyPost: Reading a Post File introduces the basics of using the PyPost module to obtain post file data
and find the maximum scalar values.
PyPost: Obtaining Element Data shows how to obtain element scalar data from a post file.
PyPost: Element Tensor Data expands on obtaining element data by showing how to obtain element
tensor data from a post file. It also introduces the PyTensor class.
PART III Advanced Discussions
PyMentat: Using the py_connect Method introduces the basics of using the PyMentat module in a client/server
environment.
PyPost: Plotting Post File Data shows some example of using third party modules to plot the results of a
post file.

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>.

• The mouse cursor is indicated by < > .


• A filename implies a concatenation of pathname and filename. The pathname may be omitted if the file name
is in your current directory. You will note the path names are specified with the forward slash character "/", as
in examples/python/userguide. For those using Microsoft Windows, you will need to substitute the forward
slashes with the backslash character "\".
Chapter 1: Introduction

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.

The Basics of Using PyMentat


When you select a menu item in Mentat, it does one of two things:
1. You are presented with a new menu screen.
2. A command is issued.
The concept of using the PyMentat module uses Item 2. To build or modify models using the PyMentat module, a
Python script sends a sequence of commands to Mentat. These commands are the same commands that are issued when
you select the appropriate menu item.
For example, if you select the menus:

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

GENERATE TEST MODEL

RUN py_create.py USING mentat.proc is created


MENTAT.PROC TO CREATE when Mentat is run
INITIAL PYTHON SCRIPT

DEVELOP AND MODIFY Add new commands as


PYTHON SCRIPT needed in the script

TEST PYTHON SCRIPT

Figure 1-1 The Python Script Development Process

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

The Basics of Using PyPost


The PyPost module is used by a Python script to process a Marc post (results) file. It operates independently from
Mentat. The PyPost API provides a set of methods to be used to obtain the scalar, tensor or vector values stored in a
post file.

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 Brief Look at Python Programming


In order to enhance your understanding of using PyMentat or PyPost, we will introduce you to the basics of
programming with Python.
Python is an interpreted, interactive object-oriented programming language sometimes compared to Perl, Java, and Tcl.
It has interfaces to IP networking, windowing systems, audio, and other technologies. Integrated with Mentat, it
provides a more powerful scripting language than procedure files since it contains conditional logic and looping
statements such as if, while, and for.
CHAPTER 1 17
Introduction

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.

Python Data Types


When programming in Python, you don’t explicitly declare a variable’s data type. Python determines the data type
by how the variable is used. Python supports the following implied data types:

Basic Data Types


string A character string similar to the char data in C and character in FORTRAN. a string may be specified
using either single or double quotes.
float A floating point number similar to the double data type in C and the real*8 data type in FORTRAN.
integer An integer or fixed point number similar to the long int data type in C and the integer*8 data type in
FORTRAN.

Extended Data Types


List A Python list is essentially a linked list that can be accessed like an array using the square bracket
operators [ ]. The list can be composed of strings, floats, or integers to name a few.
18 Marc Python Tutorial

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

2 PyMentat: A Simple Example



Chapter Overview 20

Creating a Simple Python Script for Mentat 20
 Python Programming Hints 22

Running the Script 22
20 Marc Python Tutorial

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

Creating a Simple Python Script for Mentat


To begin, we will examine a script that creates a simple element grid. This example is named chap2.py and can be
found in the Mentat directory examples/python/tutorial/c02. Note that the first Python statement contains.
1. from py_mentat import *
This statement will import the PyMentat module into the Python script. It must be used in all Python scripts that will
need to access the PyMentat module.
The remaining code that creates the grid is as follows:
2. # Create the node grid
3. def make_nodes(s,t, xs, ys):
4. y= ys
5. z = 0.0
6. delx = 1.0/s
7. dely = 1.0/t
8. for i in range(0,t):
9. x = xs
10. for j in range(0,s):
11. str = "*add_nodes %f %f %f" % (x, y, z)
12. py_send(str)
13. x = x + delx
14. y = y + dely
15. return
16.
CHAPTER 2 21
PyMentat: A Simple Example

17. # Create the element grid


18. def make_elements(n,m):
19. for i in range(1,m): # the "y" dir
20. n1 = (i-1) * (n) + 1
21. n2 = n1 + 1
22. n4 = n1 + (n)
23. n3 = n2 + (n)
24. for j in range(1,n): # the "x" dir
25. str = "*add_elements %d %d %d %d" %
(n1,n2,n3,n4)
26. py_send(str)
27. n1 = n1 + 1
28. n2 = n2 + 1
29. n3 = n3 + 1
30. n4 = n4 + 1
31. return
32.
33. def main():
34. n = 6
35. m = 8
36. xs = -1.0
37. ys = -1.0
38. make_nodes(n,m,xs,ys)
39. make_elements(n,m)
40. return
22 Marc Python Tutorial

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.

Python Programming Hints


In this example, some Python functions (or routines) were created to build the grid. A Python function is defined with
the compound statement def. The syntax is:
def NAME ’(’ [args] ’)’ ’:’
What follows after the colon is the code that comprises the function. The lines of Python code that define the function
must be indented at least one space. To end the function definition, the code is “unindented”.
The range function is used quite often in these examples. When the range function is used, Python builds a “list” of
integers starting at the first value and ending at one less than the last value. For example, the statement:
for i in range(0,5):
is actually executed as
for i in [0,1,2,3,4]:
In the Python script shown, a string is created and sent to Mentat using the py_send routine. The string could also be
built in the calling argument for py_send, as shown below:
py_send("*add_nodes %f %f %f" % (x, y, z))
The command and the floating point values to be passed are specified in the calling argument.

Running the Script


Start Mentat, and from the main screen select the following menu buttons.

MAIN
UTILS
PYTHON

This brings up the Python Menu screen as shown in Figure 2-1.


CHAPTER 2 23
PyMentat: A Simple Example

Figure 2-1 Python Menu Screen

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.

Figure 2-2 The Python Browser Window

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

Figure 2-3 Python Menu Screen with a Created Mesh

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

PyMentat: Obtaining Data from


3 Mentat

Chapter Overview 26
 Creating a Simple Python Script for Mentat 26

The py_get_int and py_get_float Methods 27

Running the Script 28
26 Marc Python Tutorial

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

Creating a Simple Python Script for Mentat


To continue with the example discussed in Chapter 2, we will examine a script that creates a simple element grid and
uses Mentat parameters to control the size of the grid. This example is named chap3.py and can be found in the Mentat
directory examples/python/tutorial/c03. The script is as follows:
1. from py_mentat import *
2. # Create the node grid
3. def make_nodes(s, t, xs, ys):
4. y= ys
5. z = 0.0
6. delx = 1.0/s
7. dely = 1.0/t
8. for i in range(0,t):
9. x = xs
10. for j in range(0,s):
11. str = "*add_nodes %f %f %f" % (x, y, z)
12. py_send(str)
13. x = x + delx
14. y = y + dely
15. return
16.
17. # Create the element grid
18. def make_elements(n,m):
19. for i in range(1,m): # the "y" dir
20. n1 = (i-1) * (n) + 1
21. n2 = n1 + 1
22. n4 = n1 + (n)
23. n3 = n2 + (n)
24. for j in range(1,n): # the "x" dir
CHAPTER 3 27
PyMentat: Obtaining Data from Mentat

25. str = "*add_elements %d %d %d %d"


(n1,n2,n3,n4)
26. py_send(str)
27. n1 = n1 + 1
28. n2 = n2 + 1
29. n3 = n3 + 1
30. n4 = n4 + 1
31. return
32.
33. def main():
34. n = py_get_int("x_size")
35. m = py_get_int("y_size")
36. xs = py_get_float("x_start")
37. ys = py_get_float("y_start")
38. make_nodes(n,m,xs,ys)
39. make_elements(n,m)
40.
41. if __name__ == ’__main__’:
42. py_connect("",40007)
43. main()
44. py_disconnect()

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 and py_get_float Methods


The py_get_int and py_get_float methods provide access to Mentat PARAMETERS and Arithmetic or
Database functions. Using Mentat parameters is the method to pass variables from Mentat to a PyMentat script. These
variables are obtained through PyMentat by specifying the name of the parameter or function in the string passed to
28 Marc Python Tutorial

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.

Running the Script


First, start Mentat. Before running this example, some PARAMETERS need to be defined. The values for these
parameters will be obtained by the Python script to control the size and the position of the grid.
Select the following menus:

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

PyMentat: Building Complex


4 Geometry

Chapter Overview 30
 Background Information 30

Running the Script 33
30 Marc Python Tutorial

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

Figure 4-1 Plate with varying Number of Holes


CHAPTER 4 31
PyMentat: Building Complex Geometry

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

33. for i in range(0, ny) :


34. y = ys + i * f
35. for j in range(0, nx) :
36. x = xs + j * f
37. py_send("%f %f 0 %f" % (x, y, r))
38. return 0
39.
40. def main():
41. width = py_get_float("width")
42. height = py_get_float("height")
43. diameter = py_get_float("diameter")
44. spacing = py_get_float("spacing")
45. xstart = -width/2
46. ystart = -height/2
47. build_plate(xstart,ystart,width, height, diameter, spacing)
48. return
49.
50. if __name__ == ’__main__’:
51. py_connect("127.0.0.1",40007)
52. main( )

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

Running the Script


Start Mentat and use the FILES menu SET DIRECTORY button to specify examples/python/tutorial/c04 as the current
directory. The parameters used in this model may be specified with the USER DEFINED menu HOLES IN PLATE
(Python). Enter the following values for this example:

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

Figure 4-2 Resulting Plate with Holes

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

PyMentat: Adding Model


5 Properties

Chapter Overview 36
 Plate Properties 36

Node and Element IDs 40

Running the Script 41
 Submitting the Job 43
36 Marc Python Tutorial

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

Figure 5-1 Plate with Varying Number of Holes


CHAPTER 5 37
PyMentat: Adding Model Properties

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.

Node and Element IDs


The Database functions use node and element IDs (similar to labels) as their arguments. In the add_bc routine of our
Python script, we issue the renumber_all command. This command renumbers all of the entities in the model. This
ensures that the index value passed to the database function node_x is the same as that of the loop value. We need to
do this because the entities (nodes in this case) are stored in the database as a linked list as shown in Figure 5-2.

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

Figure 5-2 A Linked List of Nodes

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

Figure 5-3 A Linked List of Nodes with Node 2 Deleted

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.

Running the Script


You may run the procedure file chap5.proc to set the parameters or you may specify them using the UTIL submenu
PARAMETERS as follows:

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.

Figure 5-4 Resulting Mesh with Boundary Conditions Applied


CHAPTER 5 43
PyMentat: Adding Model Properties

Submitting the Job


You may run the job by using the following menus:

MAIN
JOBS
RUN
SUBMIT 1

In the next chapter, we will examine postprocessing using the PyMentat module.
Chapter 6: PyMentat: Processing a Post File

PyMentat: Processing a Post


6 File

Chapter Overview 45
 Postprocessing Basics 45

Scalar Values 49

Running the Script 49
CHAPTER 6 45
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 = []

You might also like