changed argument handling in line with other scripts
now also works with stdin/out
This commit is contained in:
parent
f1aedf4bc3
commit
ed85ce5bdb
|
@ -1,10 +1,9 @@
|
||||||
#!/usr/bin/env python2.7
|
#!/usr/bin/env python2.7
|
||||||
# -*- coding: UTF-8 no BOM -*-
|
# -*- coding: UTF-8 no BOM -*-
|
||||||
|
|
||||||
from __future__ import print_function
|
|
||||||
import sys,os,re,time,tempfile
|
import sys,os,re,time,tempfile
|
||||||
import numpy as np
|
import numpy as np
|
||||||
import argparse
|
from optparse import OptionParser
|
||||||
import damask
|
import damask
|
||||||
|
|
||||||
sys.path.append(damask.solver.Marc().libraryPath())
|
sys.path.append(damask.solver.Marc().libraryPath())
|
||||||
|
@ -15,9 +14,8 @@ scriptID = ' '.join([scriptName,damask.version])
|
||||||
# Convert .mfd file into a usable format
|
# Convert .mfd file into a usable format
|
||||||
# Broken into labeled sections (eg. nodes, links, etc)
|
# Broken into labeled sections (eg. nodes, links, etc)
|
||||||
# Each section has a list of labeled elements with formatted numerical data
|
# Each section has a list of labeled elements with formatted numerical data
|
||||||
def parseMFD(fname):
|
def parseMFD(dat):
|
||||||
formatted = []
|
formatted = []
|
||||||
with open(fname,'r') as dat:
|
|
||||||
section = 0
|
section = 0
|
||||||
formatted.append({'label': 'header', 'uid': -1, 'els': []})
|
formatted.append({'label': 'header', 'uid': -1, 'els': []})
|
||||||
# in between =beg= and =end= part of file
|
# 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
|
# 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.
|
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.
|
Use *py_connection to operate on model presently opened in MSC.Mentat.
|
||||||
|
|
||||||
""", version = scriptID)
|
""", version = scriptID)
|
||||||
|
|
||||||
parser.add_argument('-p', '--port',
|
parser.add_option('-p', '--port',
|
||||||
type = int, metavar = 'int', default = None,
|
type = int, metavar = 'int', default = None,
|
||||||
help = 'Mentat connection port')
|
help = 'Mentat connection port')
|
||||||
parser.add_argument('-x',
|
parser.add_option('-x',
|
||||||
action = 'store_false',
|
action = 'store_false', default = True,
|
||||||
help = 'no PBC along x direction')
|
help = 'no PBC along x direction')
|
||||||
parser.add_argument('-y',
|
parser.add_option('-y',
|
||||||
action = 'store_false',
|
action = 'store_false', default = True,
|
||||||
help = 'no PBC along y direction')
|
help = 'no PBC along y direction')
|
||||||
parser.add_argument('-z',
|
parser.add_option('-z',
|
||||||
action = 'store_false',
|
action = 'store_false', default = True,
|
||||||
help = 'no PBC along z direction')
|
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:
|
if remote:
|
||||||
try: import py_mentat
|
try: import py_mentat
|
||||||
|
@ -235,25 +235,27 @@ if remote:
|
||||||
sys.exit(-1)
|
sys.exit(-1)
|
||||||
|
|
||||||
damask.util.report(scriptName, 'waiting to connect...')
|
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:
|
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('*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
|
py_mentat.py_get_int("nnodes()") # hopefully blocks until file is written
|
||||||
except:
|
except:
|
||||||
damask.util.croak('failed. try setting Tools/Python/"Run as Separate Process" & "Initiate".')
|
damask.util.croak('failed. try setting Tools/Python/"Run as Separate Process" & "Initiate".')
|
||||||
sys.exit()
|
sys.exit()
|
||||||
damask.util.croak( 'connected...')
|
damask.util.croak( 'connected...')
|
||||||
|
|
||||||
for mfdfile in args.file:
|
for name in filenames:
|
||||||
while remote and not os.path.exists(mfdfile): time.sleep(0.5) # wait for Mentat to write MFD file
|
while remote and not os.path.exists(name): time.sleep(0.5) # wait for Mentat to write MFD file
|
||||||
damask.util.report(scriptName, mfdfile)
|
with open( name,'r') if name is not None else sys.stdin as fileIn:
|
||||||
mfd = parseMFD(mfdfile)
|
damask.util.report(scriptName, name)
|
||||||
add_servoLinks(mfd,[args.x,args.y,args.z])
|
mfd = parseMFD(fileIn)
|
||||||
with open(mfdfile, 'w') as file:
|
|
||||||
file.write(asMFD(mfd))
|
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:
|
if remote:
|
||||||
try: py_mentat.py_send('*open_model "{}"'.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(args.file[0]))
|
except: damask.util.croak('lost connection on sending open command for "{}".'.format(filenames[0]))
|
||||||
|
|
Loading…
Reference in New Issue