[Python] Generate a batch of Abaqus .inp files using Python Multiprocess

Discussão em 'Python' iniciado por Stack, Setembro 27, 2024.

  1. Stack

    Stack Membro Participativo

    I am using a commercial finite element application called Abaqus/CAE with a built-in Python 2.6 interpreter and API. The inp file is for Abauqus to calculate with having geometry, material properties, boundary condition information. The inp file can be generated by a Python script using the command abaqus cae noGUI=inp.py.

    The geomerty that I am trying to generate is a batch of a rectangle with a random circle inside, these do not depend on each other. so I want to use Python multiprocess to generate them in parallel.

    The code file name is geninp.py, the details are as follows:

    import re
    from pathlib import Path
    import subprocess
    from concurrent.futures import ProcessPoolExecutor

    num_elp = 2
    samplesize = 100

    thisdir = Path(__file__).resolve().parent

    template_path = thisdir / 'template_qfnelpinp.py'


    def print_runtime_info(proc: subprocess.Popen, proc_comment: str) -> None:
    while True:
    output = proc.stdout.readline()
    if output == b'' and proc.poll() is not None:
    break
    if output:
    print(output.strip().decode())
    if proc.stderr:
    print(proc.stderr.read().strip().decode())

    print('\n' + '-'*25 + proc_comment + ' finished.' + '-'*25 + '\n')

    def run_cmd(cmd: str, cmd_comment: str) -> None:
    try:
    with subprocess.Popen(cmd, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE) as proc:
    print_runtime_info(proc, cmd_comment)
    proc.wait()
    except Exception as e:
    print(f'Error while running {cmd}: {e}')

    def create_and_run(num_seg, inp_idx):
    inp_path = thisdir / f'qfnelpinp{num_seg}-{inp_idx}.py'

    with open(template_path, 'r') as f:
    file_content = f.read()
    file_content = re.sub(r'{num_seg}', f'{num_seg}', file_content)
    file_content = re.sub(r'{inum}', f'{inp_idx}', file_content)

    with open(inp_path, 'w') as f:
    f.write(file_content)

    cmd = f'abaqus cae noGUI={inp_path}'
    run_cmd(cmd, 'Generate abaqus inp file')


    if __name__ == '__main__':
    num_segs = [num_elp for _ in range(samplesize)]
    odb_idxs = range(samplesize)
    with ProcessPoolExecutor(max_workers=30) as executor:
    executor.map(create_and_run, num_segs, odb_idxs)


    There need a template_qnfelpinp.py file, I generate inp.py file from it by modify the num_seg and inum variables. The num_seg is the number of circle, the inum is to label each .inp file.

    when I run the code by python3 geninp.py', there is about 40% .inp' file can be generate correctly, others are failed. but when I generate the failed file individually by abaqus cae noGUI=elp2-i.py(i is the label of faild file), that can be done rigtly.

    can anybody help me analysis the reason, very thanks.

    Continue reading...

Compartilhe esta Página