Skip to content

Commit

Permalink
Merge pull request #301 from pyiron/refactor_loading
Browse files Browse the repository at this point in the history
[minor] Refactor `join_path`
  • Loading branch information
samwaseda committed May 15, 2024
2 parents 8cadc35 + f70c226 commit 15d71ef
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 51 deletions.
35 changes: 26 additions & 9 deletions pyiron_continuum/damask/damaskjob.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@
) as damask_alarm:
from damask import Result
from pyiron_continuum.damask.factory import Create as DAMASKCreator, GridFactory
import os
import numpy as np
import matplotlib.pyplot as plt
from pathlib import Path

__author__ = "Muhammad Hassani"
__copyright__ = (
Expand Down Expand Up @@ -50,6 +50,12 @@ def __init__(self, project, job_name):
self.input.material = None
self.input.loading = None

def _join_path(self, path, return_str=True):
file_path = Path(self.working_directory) / path
if return_str:
return str(file_path)
return file_path

def set_elasticity(self, **kwargs):
"""
Example:
Expand Down Expand Up @@ -225,6 +231,21 @@ def loading(self, value):
)
self.input.loading = value

@staticmethod
def get_dot_F(reduction_speed):
return [["x", 0, 0], [0, 0, 0], [0, 0, -1.0 * reduction_speed]]

@staticmethod
def get_loadstep(dot_F, reduction_time, reduction_outputs, P=None):
if P is None:
P = [[0, "x", "x"], ["x", "x", "x"], ["x", "x", "x"]]
return {
"boundary_conditions": {"mechanical": {"P": P, "dot_F": dot_F}},
"discretization": {"t": reduction_time, "N": reduction_outputs},
"f_out": 5,
"f_restart": 5,
}

def set_loading(self, solver, load_steps):
"""
Creates the required damask loading.
Expand All @@ -243,18 +264,15 @@ def set_loading(self, solver, load_steps):

def _write_material(self):
if self.input.material is not None:
file_path = os.path.join(self.working_directory, "material.yaml")
self.input.material.save(fname=file_path)
self.input.material.save(self._join_path("material.yaml"))

def _write_loading(self):
if self.input.loading is not None:
file_path = os.path.join(self.working_directory, "loading.yaml")
self.input.loading.save(file_path)
self.input.loading.save(self._join_path("loading.yaml"))

def _write_geometry(self):
if self.input.grid is not None:
file_path = os.path.join(self.working_directory, "damask")
self.input.grid.save(file_path)
self.input.grid.save(self._join_path("damask"))

def write_input(self):
self._write_loading()
Expand All @@ -278,12 +296,11 @@ def _load_results(self, file_name="damask_loading_material.hdf5", run_all=True):
Args:
file_name(str): path to the hdf file
"""
damask_hdf = os.path.join(self.working_directory, file_name)

def _average(d):
return np.average(list(d.values()), axis=1)

results = Result(damask_hdf)
results = Result(self._join_path(file_name))
if not run_all:
return results
results.add_stress_Cauchy()
Expand Down
43 changes: 1 addition & 42 deletions pyiron_continuum/damask/rolling.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
import matplotlib.pyplot as plt

import os
from pathlib import Path
from pyiron_base import ImportAlarm

with ImportAlarm(
Expand Down Expand Up @@ -31,30 +30,6 @@ def __init__(self, project, job_name):
self.output.results_file = []
self.output.job_names = []

def _join_path(self, path, return_str=True):
file_path = Path(self.working_directory) / path
if return_str:
return str(file_path)
return file_path

def loading_discretization(self, rolltimes, filename):
time = (
rolltimes
* self._height_reduction
/ (self._rolling_speed * self._number_passes)
)

self.input.loading = YAML(
solver={"mechanical": "spectral_basic"}, loadstep=[]
)
self.input.loading["loadstep"].append(
self.get_loadstep(
self.get_dot_F(self._rollling_speed), time, self._increments * rolltimes
)
)
self.input.loading.save(self._join_path(filename + ".yaml"))
print(self.input.loading)

@property
def reduction_time(self):
return self.input.reduction_height / self.input.reduction_speed
Expand All @@ -76,15 +51,14 @@ def set_rolling(
self.input.regrid = regrid

def write_input(self):
super().write_input()
self.input.loading["loadstep"].append(
self.get_loadstep(
self.get_dot_F(self.input.reduction_speed),
self.reduction_time,
self.input.reduction_outputs,
)
)
self.input.loading.save(self._join_path("loading.yaml"))
super().write_input()
if self.input.regrid and len(self.input.job_names) > 0:
self.regridding(self.input.regrid_scale)

Expand All @@ -94,21 +68,6 @@ def geom_name(self):
return self.regrid_geom_name
return "damask"

@staticmethod
def get_dot_F(reduction_speed):
return [["x", 0, 0], [0, 0, 0], [0, 0, -1.0 * reduction_speed]]

@staticmethod
def get_loadstep(dot_F, reduction_time, reduction_outputs, P=None):
if P is None:
P = [[0, "x", "x"], ["x", "x", "x"], ["x", "x", "x"]]
return {
"boundary_conditions": {"mechanical": {"P": P, "dot_F": dot_F}},
"discretization": {"t": reduction_time, "N": reduction_outputs},
"f_out": 5,
"f_restart": 5,
}

def collect_output(self):
self.output.job_names.append(self.job_name)
super().collect_output()
Expand Down

0 comments on commit 15d71ef

Please sign in to comment.