#! /usr/bin/env python
"""Rectangle compression workflow

Requires the following ``SConscript(..., exports={})``

* ``env`` - The SCons construction environment with the following required keys

  * ``solve_cpus`` - Integer number of cpus to use in the Abaqus solve task
  * ``unconditional_build`` - Boolean flag to force building of conditionally ignored targets
  * ``abaqus`` - String path for the Abaqus executable
"""

import pathlib

# Inherit the parent construction environment
Import("env")

# Simulation variables
build_directory = pathlib.Path(Dir(".").abspath)
workflow_name = build_directory.name

# Comment used in tutorial code snippets: marker-1

# Collect the target nodes to build a concise alias for all targets
workflow = []
datacheck = []

# Geometry
workflow.extend(
    env.AbaqusJournal(
        target=["rectangle_geometry.cae", "rectangle_geometry.jnl"],
        source=["#/modsim_package/abaqus/rectangle_geometry.py"],
        subcommand_options="",
    )
)

# Comment used in tutorial code snippets: marker-2

# Partition
workflow.extend(
    env.AbaqusJournal(
        target=["rectangle_partition.cae", "rectangle_partition.jnl"],
        source=["#/modsim_package/abaqus/rectangle_partition.py", "rectangle_geometry.cae"],
        subcommand_options="",
    )
)

# Mesh
workflow.extend(
    env.AbaqusJournal(
        target=["rectangle_mesh.inp", "rectangle_mesh.cae", "rectangle_mesh.jnl"],
        source=["#/modsim_package/abaqus/rectangle_mesh.py", "rectangle_partition.cae"],
        subcommand_options="",
    )
)

# Comment used in tutorial code snippets: marker-3

# SolverPrep
copy_source_list = [
    "#/modsim_package/abaqus/rectangle_compression.inp",
    "#/modsim_package/abaqus/assembly.inp",
    "#/modsim_package/abaqus/boundary.inp",
    "#/modsim_package/abaqus/field_output.inp",
    "#/modsim_package/abaqus/materials.inp",
    "#/modsim_package/abaqus/parts.inp",
    "#/modsim_package/abaqus/history_output.inp",
]
workflow.extend(
    env.CopySubstfile(
        copy_source_list,
    )
)

# Comment used in tutorial code snippets: marker-4

# Abaqus Solve
solve_source_list = [
    "rectangle_compression.inp",
    "assembly.inp",
    "boundary.inp",
    "field_output.inp",
    "materials.inp",
    "parts.inp",
    "history_output.inp",
    "rectangle_mesh.inp",
]

datacheck.extend(
    env.AbaqusSolver(
        target=[
            "rectangle_compression_DATACHECK.odb",
            "rectangle_compression_DATACHECK.dat",
            "rectangle_compression_DATACHECK.msg",
            "rectangle_compression_DATACHECK.com",
            "rectangle_compression_DATACHECK.prt",
            "rectangle_compression_DATACHECK.023",
            "rectangle_compression_DATACHECK.mdl",
            "rectangle_compression_DATACHECK.sim",
            "rectangle_compression_DATACHECK.stt",
        ],
        source=solve_source_list,
        job="rectangle_compression_DATACHECK",
        program_options="-double both -datacheck",
    )
)

# Comment used in tutorial code snippets: marker-5

workflow.extend(
    env.AbaqusSolver(
        target=[
            "rectangle_compression.odb",
            "rectangle_compression.dat",
            "rectangle_compression.msg",
            "rectangle_compression.com",
            "rectangle_compression.prt",
            "rectangle_compression.sta",
        ],
        source=solve_source_list,
        job="rectangle_compression",
        program_options="-double both -cpus $(${solve_cpus}$)",
        solve_cpus=env["solve_cpus"],
    )
)

# Comment used in tutorial code snippets: marker-6

# Collector alias based on parent directory name
env.Alias(workflow_name, workflow)
env.Alias(f"{workflow_name}_datacheck", datacheck)

if not env["unconditional_build"] and not env["ABAQUS_PROGRAM"]:
    print(f"Program 'abaqus' was not found in construction environment. Ignoring '{workflow_name}' target(s)")
    Ignore([".", workflow_name], workflow)
    Ignore([".", f"{workflow_name}_datacheck"], datacheck)
