Skip to content

Commit

Permalink
0908
Browse files Browse the repository at this point in the history
  • Loading branch information
tian50 committed Sep 9, 2021
1 parent 8179004 commit e646cee
Show file tree
Hide file tree
Showing 7 changed files with 338 additions and 14 deletions.
Empty file added msgpi/ms/__init__.py
Empty file.
Empty file added msgpi/sg/__init__.py
Empty file.
320 changes: 320 additions & 0 deletions msgpi/sg/exec.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,320 @@
import os
import traceback
import subprocess as sbp

import msgpi.utils.logger as utlog
# import msgpi.timer as mtime
import msgpi.sg.presg as sgpre
import msgpi.sg.io as sgio
# import msgpi.utils as utils

def solve(
sg_xml, analysis, ppcmd, solver, integrated=False,
aperiodic=False, output_gmsh_format=True, reduced_integration=False,
timeout=30, scrnout=True, logger=None, timer=None
):
"""Solve
Parameters
----------
sg_xml : str
File name of SG design parameters (XML format).
analysis : str
Analysis to be carried out.
* h - homogenization
* d - dehomogenization/localization/recover
* f - initial failure strength
* fe - initial failure envelope
* fi - initial failure indices and strength ratios
ppcmd : str
Preprocessor command.
solver : str
Command of the solver.
integrated : bool, optional
Use integrated solver or not (standalone), by default False.
aperiodic : bool, optional
(SwiftComp) If the structure gene is periodic, by default False.
output_gmsh_format : bool, optional
(SwiftComp) If output dehomogenization results in Gmsh format, by default True
reduced_integration : bool, optional
(SwiftComp) If reduced integration is used for certain elements, by default False.
timeout : int, optional
Time to wait before stop, by default 30.
scrnout : bool, optional
Switch of printing solver messages, by default True.
logger : logging.Logger, optional
Logger object, by default None.
Returns
-------
various
Different analyses return different types of results.
"""

if logger is None:
logger = utlog.initLogger(__name__)

# t = mtime.Timer(logger=logger.info)

# Preprocess
logger.info('preprocessing...')

if timer:
timer.start()
sg_in, smdim = sgpre.preSG(
sg_xml, analysis, ppcmd, solver, integrated,
timeout=timeout, scrnout=scrnout, logger=logger
)
if timer:
timer.stop()


# Solve
if not integrated:
logger.info('running analysis...')
logger.debug('solver: ' + solver)
if timer:
timer.start()
run(
solver, sg_in, analysis, smdim,
aperiodic, output_gmsh_format, reduced_integration,
scrnout, logger=logger
)
if timer:
timer.stop()


# Parse results
logger.info('reading results...')

results = None

if timer:
timer.start()

if 'vabs' in solver.lower():
results = sgio.readVABSOut(sg_in, analysis, scrnout)

elif 'swiftcomp' in solver.lower():
results = sgio.readSCOut(sg_in, smdim, analysis, scrnout)


if timer:
timer.stop()

return results









def runVABS(command, input_name, analysis, scrnout=True, logger=None):
"""Run VABS.
Parameters
----------
command : str
Command name of VABS
input_name : str
Name of the input file.
analysis : {0, 1, 2, 3, '', 'h', 'dn', 'dl', 'd', 'l', 'fi'}
Analysis to be carried out.
* 0 or 'h' or '' - homogenization
* 1 or 'dn' - dehomogenization (nonlinear)
* 2 or 'dl' or 'd' or 'l' - dehomogenization (linear)
* 3 or 'fi' - initial failure indices and strength ratios
scrnout : bool, default True
Switch of printing solver messages.
logger : logging.Logger
Logger object
"""

if logger is None:
logger = utlog.initLogger(__name__)

try:
cmd = [command, input_name]

if analysis == 0 or analysis == 'h' or analysis == '':
pass
elif analysis == 1 or analysis == 'dn':
cmd.append('1')
elif analysis == 2 or analysis == 'dl' or analysis == 'd' or analysis == 'l':
cmd.append('2')
elif analysis == 3 or analysis == 'fi':
cmd.append('3')

logger.info(' '.join(cmd))

if scrnout:
sbp.call(cmd)
else:
FNULL = open(os.devnull, 'w')
sbp.call(cmd, stdout=FNULL, stderr=sbp.STDOUT)

except:
e = traceback.format_exc()
logger.critical(e, exc_info=1)

return









def runSwiftComp(
command, input_name, analysis, smdim,
aperiodic=False, output_gmsh_format=True, reduced_integration=False,
scrnout=True, logger=None):
"""Run SwiftComp.
Parameters
----------
command : str
Command name of SwiftComp
input_name : str
Name of the input file.
analysis : {0, 2, 3, 4, 5, '', 'h', 'dl', 'd', 'l', 'fi', 'f', 'fe'}
Analysis to be carried out.
* 0 or 'h' or '' - homogenization
* 2 or 'dl' or 'd' or 'l' - dehomogenization (linear)
* 3 or 'fi' - initial failure indices and strength ratios
* 4 or 'f' - initial failure strength
* 5 or 'fe' - initial failure envelope
smdim : int
Dimension of the macroscopic structural model.
aperiodic : bool
If the structure gene is periodic.
output_gmsh_format : bool
If output dehomogenization results in Gmsh format
reduced_integration : bool
If reduced integration is used for certain elements.
scrnout : bool, default True
Switch of printing solver messages..
logger : logging.Logger
Logger object
"""

if logger is None:
logger = utlog.initLogger(__name__)

try:
cmd = [command, input_name]

cmd.append(str(smdim) + 'D')

arg = ''

if analysis == 0 or analysis == 'h' or analysis == '':
arg = 'H'
if aperiodic:
arg += 'A'
elif analysis == 2 or analysis == 'dl' or analysis == 'd' or analysis == 'l':
arg = 'L'
if aperiodic:
arg += 'A'
if output_gmsh_format:
arg += 'G'
elif analysis == 3 or analysis == 'fi':
arg = 'FI'
elif analysis == 4 or analysis == 'f':
arg = 'F'
elif analysis == 5 or analysis == 'fe':
arg = 'FE'



cmd.append(arg)

if reduced_integration:
cmd.append('R')

logger.info(' '.join(cmd))

if scrnout:
sbp.call(cmd)
else:
FNULL = open(os.devnull, 'w')
sbp.call(cmd, stdout=FNULL, stderr=sbp.STDOUT)

except:
e = traceback.format_exc()
logger.critical(e, exc_info=1)

return









def run(
solver, input_name, analysis, smdim=2,
aperiodic=False, output_gmsh_format=True, reduced_integration=False,
scrnout=True, logger=None):
"""Run codes.
Parameters
----------
solver : str
solver name of VABS or SwiftComp
input_name : str
Name of the input file.
analysis : {0, 1, 2, 3, 4, 5, '', 'h', 'dn', 'dl', 'd', 'l', 'fi', 'f', 'fe'}
Analysis to be carried out.
* 0 or 'h' or '' - homogenization
* 1 or 'dn' - (VABS) dehomogenization (nonlinear)
* 2 or 'dl' or 'd' or 'l' - dehomogenization (linear)
* 3 or 'fi' - initial failure indices and strength ratios
* 4 or 'f' - (SwiftComp) initial failure strength
* 5 or 'fe' - (SwiftComp) initial failure envelope
smdim : int
(SwiftComp) Dimension of the macroscopic structural model.
aperiodic : bool
(SwiftComp) If the structure gene is periodic.
output_gmsh_format : bool
(SwiftComp) If output dehomogenization results in Gmsh format
reduced_integration : bool
(SwiftComp) If reduced integration is used for certain elements.
scrnout : bool, default True
Switch of printing solver messages.
logger : logging.Logger
Logger object
"""
if logger is None:
logger = utlog.initLogger(__name__)

try:
if solver.lower().startswith('v'):
runVABS(solver, input_name, analysis, scrnout, logger)

elif solver.lower().startswith('s'):
runSwiftComp(
solver, input_name, analysis, smdim,
aperiodic, output_gmsh_format, reduced_integration,
scrnout, logger
)

except:
e = traceback.format_exc()
logger.critical(e, exc_info=1)


def runBatch():

return
19 changes: 10 additions & 9 deletions msgpi/sg/presg.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@
import numpy as np
import traceback as tb
import xml.etree.ElementTree as et
import msgpi.sg as sgm
import msgpi.io.iosc as miosc
import msgpi.io.iovabs as miovabs
import msgpi.utils as utl
import msgpi.logger as mlog
import msgpi.sg.sg as sgm
import msgpi.sg.io as sgio
import msgpi.utils.io as utio
import msgpi.utils.exec as utrun
import msgpi.utils.logger as utlog


def readMaterialFromXMLElement(xem):
Expand Down Expand Up @@ -181,7 +181,7 @@ def preSG1D(xr_sg, smdim):
# d_material[lm]['id'] = len(l_material_used)
lt = d_lamina[ln]['thickness']
layup_code = layup.find('code').text.strip()
code = utl.parseLayupCode(layup_code)
code = utio.parseLayupCode(layup_code)
for lo in code:
ld_layer.append({'material': lm, 'thickness': lt, 'angle': lo})
elif method == 'pp':
Expand Down Expand Up @@ -333,7 +333,7 @@ def preSG(
# fn_head, fn_tail = os.path.split(xml_sg)

if logger is None:
logger = mlog.initLogger(__name__)
logger = utlog.initLogger(__name__)

try:
fn_base, fn_extn = os.path.splitext(sg_xml)
Expand All @@ -358,7 +358,7 @@ def preSG(

# cmd = ' '.join(cmd)
logger.info(' '.join(cmd))
utl.run(cmd, timeout, scrnout)
utrun.run(cmd, timeout, scrnout)
# if scrnout:
# print('cmd:', cmd)
# sbp.call(cmd)
Expand Down Expand Up @@ -386,7 +386,8 @@ def preSG(
sg = preSG1D(xr_sg, smdim)

if write_input:
miosc.writeSCIn(sg, fn_sg_in)
# sgio.writeSCIn(sg, fn_sg_in)
sg.writeInput(fn_sg_in, 'sc')
return fn_sg_in, smdim
else:
return sg
Expand Down
Empty file added msgpi/utils/__init__.py
Empty file.
1 change: 1 addition & 0 deletions tests/test_run/test_run_vabs_h/log.txt
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,4 @@ Traceback (most recent call last):
File "C:\Users\unita\anaconda3\lib\subprocess.py", line 1307, in _execute_child
hp, ht, pid, tid = _winapi.CreateProcess(executable, args,
FileNotFoundError: [WinError 2] The system cannot find the file specified
INFO | 2021-09-09 00:34:50 | exec.runVABS | vabs box.sg
12 changes: 7 additions & 5 deletions tests/test_run/test_run_vabs_h/test_run_vabs_h.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
import sys
import msgpi.analysis
import msgpi.sg.exec as sgrun
import msgpi.utils.logger as utlog

command = 'vabs'
input_name = sys.argv[1]
logger = utlog.initLogger(__name__, cout_level='DEBUG')

solver = 'vabs'
input_name = 'box.sg'
analysis = 'h'

msgpi.analysis.runVABS(command, input_name, analysis)
sgrun.run(solver, input_name, analysis, logger=logger)

0 comments on commit e646cee

Please sign in to comment.