Skip to content

Commit

Permalink
move more processing out of main function
Browse files Browse the repository at this point in the history
  • Loading branch information
tbhartman committed Oct 24, 2018
1 parent b715149 commit 7486b0e
Show file tree
Hide file tree
Showing 6 changed files with 99 additions and 16 deletions.
2 changes: 2 additions & 0 deletions .behaverc
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
[behave]
paths = src/abaqus2dyna/test/features
4 changes: 2 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
test:
pytest
behave src/abaqus2dyna/test/features/
behave

coverage:
pytest --cov=abaqus2dyna --cov-report html --cov-config=.coveragerc
mv coverage/.coverage coverage/.coverage.pytest
coverage run -m behave src/abaqus2dyna/test/features -k --tags=unit
coverage run -m behave -k --tags=unit
mv coverage/.coverage coverage/.coverage.behave
coverage combine coverage
coverage report
Expand Down
File renamed without changes.
79 changes: 65 additions & 14 deletions src/abaqus2dyna/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,46 @@

import numpy as np

# class definitions
class Model:
def __init__(self):
self.assembly = None

@staticmethod
def parse(cls, file_or_name):
NotImplemented()
def write(cls, file_or_name):
NotImplemented()

class Assembly:
def __init__(self):
self.name = None
self.instances = []
self.sets = {}

class Instance:
def __init__(self):
self.name = None
self.part = None
self.translation = None
self.rotation = None

class Part:
def __init__(self):
self.name = None
self.nodes = None
self.elements = None
self.sets = None

class AbaqusInput:
def __init__(self):
self.assembly = None
self.header = None

@staticmethod
def parse(cls, file_or_name):
ret = cls()
return ret


class Orientation():
system = None
Expand Down Expand Up @@ -325,12 +364,12 @@ def update_term(complete=False):
#import pdb; pdb.set_trace()
return ret

def WriteDynaFromAbaqus(total_nodel, inp_name, abaqus_keyword, output_filename):
def WriteDynaFromAbaqus(total_nodel, inp_name, abaqus_keyword, ostream):
#global total_nodel
written_nodel = 0
def update_term(complete=False):
#global total_nodel
#global output_filename
#global ostream
total = total_nodel
counted = count['node'] + count['element']
perc = int(1000 * counted * 1.0 / total)
Expand Down Expand Up @@ -547,7 +586,7 @@ def comment_line(string, fill='', newline=True):
output['stats']['nsid'].append([set_id,set_name])

update_term(True)
k = open(output_filename, 'w')
k = ostream
k.write(output['header'])
k.write(output['timestamp'])
k.write(output['sep'])
Expand All @@ -564,15 +603,16 @@ def comment_line(string, fill='', newline=True):
k.write(output['sep'])
k.write('*END\n')
k.write(comment_line('End of translated output.', fill='-', newline=False))
k.close()
print('File ' + output_filename + ' written.')
return


def cmdline(argv):
def cmdline(argv = None):
""" command line processor
returns namespace via argparse, or throws SystemExit
if argv is none, get from sys.argv (via argparse)
"""
from . import _version
version = _version.get_versions()['version']
Expand All @@ -590,18 +630,15 @@ def cmdline(argv):
parser.add_argument('-o', '--output',
dest='output',
metavar='OUTPUT',
help='LS-DYNA keyword file output location',
type=argparse.FileType('w'))
help='LS-DYNA keyword file output location')
args = parser.parse_args(argv)

return args


def main():
args = cmdline(sys.argv[1:])

def convert(fin, fout):

inp = ParseAbaqus(args.input)
inp = ParseAbaqus(fin)
#print(inp.count)

# get nodes + elements (these will take the longest)
Expand All @@ -612,7 +649,21 @@ def main():
total_nodel += len(part.element)

# finally, output dyna keyword
WriteDynaFromAbaqus(total_nodel, args.input.name, inp, args.input.name + '.k')
WriteDynaFromAbaqus(total_nodel, fin.name, inp, fout)

def main():
args = cmdline()
with open(args.input) as fin:
if args.output:
fout = open(args.output, 'w')
else:
fout = sys.stdout
try:
return convert(fin, fout)
finally:
if args.output:
fout.close()

if __name__ == '__main__':
sys.exit(main())

7 changes: 7 additions & 0 deletions src/abaqus2dyna/test/features/run.feature
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
Feature: full test

Scenario: example.inp
Given example.inp
When we convert it
Then we expect an output file with 69722 lines

23 changes: 23 additions & 0 deletions src/abaqus2dyna/test/features/steps/run.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import io

from behave import given, then, when

import abaqus2dyna.__main__

@given('{filename}')
def step_impl(context, filename):
context.name = filename

@when("we convert it")
def step_impl(context):
context.out = io.StringIO()
with open(context.name) as fin:
abaqus2dyna.__main__.convert(fin, context.out)

@then('we expect an output file with {number:d} lines')
def step_impl(context, number):
context.out.seek(0)
print(len(context.out.read().splitlines()))
context.out.seek(0)
assert len(context.out.read().splitlines()) == number

0 comments on commit 7486b0e

Please sign in to comment.