From e646cee040c507d2d02f769a506c03d0d0a1b196 Mon Sep 17 00:00:00 2001 From: Su Tian Date: Thu, 9 Sep 2021 00:42:20 -0400 Subject: [PATCH] 0908 --- msgpi/ms/__init__.py | 0 msgpi/sg/__init__.py | 0 msgpi/sg/exec.py | 320 ++++++++++++++++++ msgpi/sg/presg.py | 19 +- msgpi/utils/__init__.py | 0 tests/test_run/test_run_vabs_h/log.txt | 1 + .../test_run_vabs_h/test_run_vabs_h.py | 12 +- 7 files changed, 338 insertions(+), 14 deletions(-) create mode 100644 msgpi/ms/__init__.py create mode 100644 msgpi/sg/__init__.py create mode 100644 msgpi/sg/exec.py create mode 100644 msgpi/utils/__init__.py diff --git a/msgpi/ms/__init__.py b/msgpi/ms/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/msgpi/sg/__init__.py b/msgpi/sg/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/msgpi/sg/exec.py b/msgpi/sg/exec.py new file mode 100644 index 0000000..63d18a4 --- /dev/null +++ b/msgpi/sg/exec.py @@ -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 diff --git a/msgpi/sg/presg.py b/msgpi/sg/presg.py index f238337..ec137da 100644 --- a/msgpi/sg/presg.py +++ b/msgpi/sg/presg.py @@ -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): @@ -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': @@ -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) @@ -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) @@ -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 diff --git a/msgpi/utils/__init__.py b/msgpi/utils/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/tests/test_run/test_run_vabs_h/log.txt b/tests/test_run/test_run_vabs_h/log.txt index b820fd5..22884c6 100644 --- a/tests/test_run/test_run_vabs_h/log.txt +++ b/tests/test_run/test_run_vabs_h/log.txt @@ -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 diff --git a/tests/test_run/test_run_vabs_h/test_run_vabs_h.py b/tests/test_run/test_run_vabs_h/test_run_vabs_h.py index 13f46bb..46b2a8e 100644 --- a/tests/test_run/test_run_vabs_h/test_run_vabs_h.py +++ b/tests/test_run/test_run_vabs_h/test_run_vabs_h.py @@ -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)