changed argument handling in line with other scripts

now also works with stdin/out
This commit is contained in:
Franz Roters 2017-04-28 12:36:10 +02:00
parent f1aedf4bc3
commit ed85ce5bdb
1 changed files with 73 additions and 71 deletions

View File

@ -1,10 +1,9 @@
#!/usr/bin/env python2.7
# -*- coding: UTF-8 no BOM -*-
from __future__ import print_function
import sys,os,re,time,tempfile
import numpy as np
import argparse
from optparse import OptionParser
import damask
sys.path.append(damask.solver.Marc().libraryPath())
@ -15,9 +14,8 @@ scriptID = ' '.join([scriptName,damask.version])
# Convert .mfd file into a usable format
# Broken into labeled sections (eg. nodes, links, etc)
# Each section has a list of labeled elements with formatted numerical data
def parseMFD(fname):
def parseMFD(dat):
formatted = []
with open(fname,'r') as dat:
section = 0
formatted.append({'label': 'header', 'uid': -1, 'els': []})
# in between =beg= and =end= part of file
@ -203,30 +201,32 @@ def add_servoLinks(mfd_data,active=[True,True,True]): # directions on which to
#--------------------------------------------------------------------------------------------------
# MAIN
#--------------------------------------------------------------------------------------------------
parser = argparse.ArgumentParser(description = """
parser = OptionParser(option_class=damask.extendableOption, usage='%prog options [file[s]]', description = """
Set up servo linking to achieve periodic boundary conditions for a regular hexahedral mesh.
Use *py_connection to operate on model presently opened in MSC.Mentat.
""", version = scriptID)
parser.add_argument('-p', '--port',
parser.add_option('-p', '--port',
type = int, metavar = 'int', default = None,
help = 'Mentat connection port')
parser.add_argument('-x',
action = 'store_false',
parser.add_option('-x',
action = 'store_false', default = True,
help = 'no PBC along x direction')
parser.add_argument('-y',
action = 'store_false',
parser.add_option('-y',
action = 'store_false', default = True,
help = 'no PBC along y direction')
parser.add_argument('-z',
action = 'store_false',
parser.add_option('-z',
action = 'store_false', default = True,
help = 'no PBC along z direction')
parser.add_argument('file', nargs='*',
help = 'Mentat formatted data (.mfd) file[s] to add periodic boundary conditions to')
args = parser.parse_args()
(options, filenames) = parser.parse_args()
remote = args.port is not None
remote = options.port is not None
if remote and filenames != []:
parser.error('file can not be specified when port is given.')
if filenames == []: filenames = [None]
if remote:
try: import py_mentat
@ -235,25 +235,27 @@ if remote:
sys.exit(-1)
damask.util.report(scriptName, 'waiting to connect...')
args.file = [os.path.join(tempfile._get_default_tempdir(), next(tempfile._get_candidate_names()) + '.mfd')]
filenames = [os.path.join(tempfile._get_default_tempdir(), next(tempfile._get_candidate_names()) + '.mfd')]
try:
py_mentat.py_connect('',args.port)
py_mentat.py_connect('',options.port)
py_mentat.py_send('*set_save_formatted on')
py_mentat.py_send('*save_as_model "{}" yes'.format(args.file[0]))
py_mentat.py_send('*save_as_model "{}" yes'.format(filenames[0]))
py_mentat.py_get_int("nnodes()") # hopefully blocks until file is written
except:
damask.util.croak('failed. try setting Tools/Python/"Run as Separate Process" & "Initiate".')
sys.exit()
damask.util.croak( 'connected...')
for mfdfile in args.file:
while remote and not os.path.exists(mfdfile): time.sleep(0.5) # wait for Mentat to write MFD file
damask.util.report(scriptName, mfdfile)
mfd = parseMFD(mfdfile)
add_servoLinks(mfd,[args.x,args.y,args.z])
with open(mfdfile, 'w') as file:
file.write(asMFD(mfd))
for name in filenames:
while remote and not os.path.exists(name): time.sleep(0.5) # wait for Mentat to write MFD file
with open( name,'r') if name is not None else sys.stdin as fileIn:
damask.util.report(scriptName, name)
mfd = parseMFD(fileIn)
add_servoLinks(mfd,[options.x,options.y,options.z])
with open( name,'w') if name is not None else sys.stdout as fileOut:
fileOut.write(asMFD(mfd))
if remote:
try: py_mentat.py_send('*open_model "{}"'.format(args.file[0]))
except: damask.util.croak('lost connection on sending open command for "{}".'.format(args.file[0]))
try: py_mentat.py_send('*open_model "{}"'.format(filenames[0]))
except: damask.util.croak('lost connection on sending open command for "{}".'.format(filenames[0]))