2020-09-19 16:31:19 +05:30
|
|
|
import numpy as np
|
2021-01-11 16:23:07 +05:30
|
|
|
import h5py
|
2022-11-23 02:56:15 +05:30
|
|
|
from typing import Optional, Union, Sequence, Dict, Any, Collection
|
2020-09-19 16:31:19 +05:30
|
|
|
|
2022-02-01 13:00:00 +05:30
|
|
|
from ._typehints import FileHandle
|
2020-09-30 11:23:25 +05:30
|
|
|
from . import Config
|
2020-09-19 16:31:19 +05:30
|
|
|
from . import Rotation
|
2020-11-10 01:50:56 +05:30
|
|
|
from . import Orientation
|
2021-02-23 09:14:12 +05:30
|
|
|
from . import util
|
2022-02-01 13:00:00 +05:30
|
|
|
from . import Table
|
|
|
|
|
2020-09-19 16:31:19 +05:30
|
|
|
|
2020-09-30 11:23:25 +05:30
|
|
|
class ConfigMaterial(Config):
|
2021-03-27 14:40:35 +05:30
|
|
|
"""
|
|
|
|
Material configuration.
|
|
|
|
|
|
|
|
Manipulate material configurations for storage in YAML format.
|
|
|
|
A complete material configuration file has the entries 'material',
|
|
|
|
'phase', and 'homogenization'. For use in DAMASK, it needs to be
|
|
|
|
stored as 'material.yaml'.
|
2021-04-23 22:50:07 +05:30
|
|
|
|
2021-03-27 14:40:35 +05:30
|
|
|
"""
|
2020-09-19 16:31:19 +05:30
|
|
|
|
2022-02-01 13:00:00 +05:30
|
|
|
def __init__(self,
|
2022-11-23 02:56:15 +05:30
|
|
|
d: Optional[Dict[str, Any]] = None,
|
2022-02-01 13:00:00 +05:30
|
|
|
**kwargs):
|
2021-03-27 14:40:35 +05:30
|
|
|
"""
|
|
|
|
New material configuration.
|
|
|
|
|
|
|
|
Parameters
|
|
|
|
----------
|
2021-12-03 04:50:27 +05:30
|
|
|
d : dictionary or YAML string, optional
|
2021-03-28 15:05:40 +05:30
|
|
|
Initial content. Defaults to None, in which case empty entries for
|
2021-12-03 04:50:27 +05:30
|
|
|
any missing material, homogenization, and phase entry are created.
|
|
|
|
kwargs : key=value pairs, optional
|
|
|
|
Initial content specified as pairs of key=value.
|
2021-03-27 14:40:35 +05:30
|
|
|
|
|
|
|
"""
|
2022-02-01 13:00:00 +05:30
|
|
|
default: Collection
|
2021-12-03 04:50:27 +05:30
|
|
|
if d is None:
|
2022-02-01 13:00:00 +05:30
|
|
|
for section, default in {'material':[],'homogenization':{},'phase':{}}.items():
|
2021-12-03 04:50:27 +05:30
|
|
|
if section not in kwargs: kwargs.update({section:default})
|
|
|
|
|
|
|
|
super().__init__(d,**kwargs)
|
2021-01-03 16:40:39 +05:30
|
|
|
|
2020-12-17 21:17:56 +05:30
|
|
|
|
2022-02-01 13:00:00 +05:30
|
|
|
def save(self,
|
|
|
|
fname: FileHandle = 'material.yaml',
|
|
|
|
**kwargs):
|
2020-09-30 12:19:55 +05:30
|
|
|
"""
|
|
|
|
Save to yaml file.
|
|
|
|
|
|
|
|
Parameters
|
|
|
|
----------
|
|
|
|
fname : file, str, or pathlib.Path, optional
|
|
|
|
Filename or file for writing. Defaults to 'material.yaml'.
|
2020-10-09 17:49:19 +05:30
|
|
|
**kwargs
|
2020-09-30 16:02:37 +05:30
|
|
|
Keyword arguments parsed to yaml.dump.
|
2020-09-30 12:19:55 +05:30
|
|
|
|
|
|
|
"""
|
2020-09-30 16:02:37 +05:30
|
|
|
super().save(fname,**kwargs)
|
2020-09-30 12:19:55 +05:30
|
|
|
|
2020-10-09 11:15:20 +05:30
|
|
|
|
2020-11-05 01:44:08 +05:30
|
|
|
@classmethod
|
2022-02-01 13:00:00 +05:30
|
|
|
def load(cls,
|
|
|
|
fname: FileHandle = 'material.yaml') -> 'ConfigMaterial':
|
2020-11-05 01:44:08 +05:30
|
|
|
"""
|
|
|
|
Load from yaml file.
|
|
|
|
|
|
|
|
Parameters
|
|
|
|
----------
|
|
|
|
fname : file, str, or pathlib.Path, optional
|
2021-04-25 11:17:00 +05:30
|
|
|
Filename or file to read from. Defaults to 'material.yaml'.
|
|
|
|
|
|
|
|
Returns
|
|
|
|
-------
|
|
|
|
loaded : damask.ConfigMaterial
|
|
|
|
Material configuration from file.
|
2020-11-05 01:44:08 +05:30
|
|
|
|
|
|
|
"""
|
|
|
|
return super(ConfigMaterial,cls).load(fname)
|
|
|
|
|
|
|
|
|
2021-01-11 16:23:07 +05:30
|
|
|
@staticmethod
|
2022-02-01 13:00:00 +05:30
|
|
|
def load_DREAM3D(fname: str,
|
2022-11-23 02:56:15 +05:30
|
|
|
grain_data: Optional[str] = None,
|
|
|
|
cell_data: Optional[str] = None,
|
2022-02-01 13:00:00 +05:30
|
|
|
cell_ensemble_data: str = 'CellEnsembleData',
|
|
|
|
phases: str = 'Phases',
|
|
|
|
Euler_angles: str = 'EulerAngles',
|
|
|
|
phase_names: str = 'PhaseName',
|
2022-11-23 02:56:15 +05:30
|
|
|
base_group: Optional[str] = None) -> 'ConfigMaterial':
|
2021-03-12 13:13:57 +05:30
|
|
|
"""
|
2021-03-20 18:07:06 +05:30
|
|
|
Load DREAM.3D (HDF5) file.
|
2021-01-11 19:35:48 +05:30
|
|
|
|
2021-03-20 18:07:06 +05:30
|
|
|
Data in DREAM.3D files can be stored per cell ('CellData')
|
|
|
|
and/or per grain ('Grain Data'). Per default, cell-wise data
|
|
|
|
is assumed.
|
|
|
|
|
|
|
|
damask.Grid.load_DREAM3D allows to get the corresponding geometry
|
|
|
|
for the grid solver.
|
2021-01-11 16:23:07 +05:30
|
|
|
|
|
|
|
Parameters
|
|
|
|
----------
|
2021-03-12 12:39:25 +05:30
|
|
|
fname : str
|
2021-03-20 17:21:41 +05:30
|
|
|
Filename of the DREAM.3D (HDF5) file.
|
2021-03-20 18:07:06 +05:30
|
|
|
grain_data : str
|
|
|
|
Name of the group (folder) containing grain-wise data. Defaults
|
|
|
|
to None, in which case cell-wise data is used.
|
|
|
|
cell_data : str
|
2021-03-23 18:58:56 +05:30
|
|
|
Name of the group (folder) containing cell-wise data. Defaults to
|
|
|
|
None in wich case it is automatically detected.
|
2021-03-20 18:07:06 +05:30
|
|
|
cell_ensemble_data : str
|
2021-03-23 18:12:04 +05:30
|
|
|
Name of the group (folder) containing data of cell ensembles. This
|
|
|
|
group is used to inquire the name of the phases. Phases will get
|
|
|
|
numeric IDs if this group is not found. Defaults to 'CellEnsembleData'.
|
2021-03-20 18:07:06 +05:30
|
|
|
phases : str
|
|
|
|
Name of the dataset containing the phase ID (cell-wise or grain-wise).
|
|
|
|
Defaults to 'Phases'.
|
|
|
|
Euler_angles : str
|
|
|
|
Name of the dataset containing the crystallographic orientation as
|
|
|
|
Euler angles in radians (cell-wise or grain-wise). Defaults to 'EulerAngles'.
|
|
|
|
phase_names : str
|
2021-03-23 18:12:04 +05:30
|
|
|
Name of the dataset containing the phase names. Phases will get
|
|
|
|
numeric IDs if this dataset is not found. Defaults to 'PhaseName'.
|
2021-01-11 16:23:07 +05:30
|
|
|
base_group : str
|
2021-03-20 18:07:06 +05:30
|
|
|
Path to the group (folder) that contains geometry (_SIMPL_GEOMETRY),
|
|
|
|
and grain- or cell-wise data. Defaults to None, in which case
|
2021-03-20 17:21:41 +05:30
|
|
|
it is set as the path that contains _SIMPL_GEOMETRY/SPACING.
|
2021-01-11 16:23:07 +05:30
|
|
|
|
2021-06-17 21:56:37 +05:30
|
|
|
Notes
|
|
|
|
-----
|
|
|
|
Homogenization and phase entries are emtpy and need to be defined separately.
|
|
|
|
|
2021-04-25 11:17:00 +05:30
|
|
|
Returns
|
|
|
|
-------
|
|
|
|
loaded : damask.ConfigMaterial
|
|
|
|
Material configuration from file.
|
|
|
|
|
2021-01-11 16:23:07 +05:30
|
|
|
"""
|
2021-03-20 04:19:41 +05:30
|
|
|
b = util.DREAM3D_base_group(fname) if base_group is None else base_group
|
2021-03-23 18:58:56 +05:30
|
|
|
c = util.DREAM3D_cell_data_group(fname) if cell_data is None else cell_data
|
2021-03-20 17:21:41 +05:30
|
|
|
f = h5py.File(fname,'r')
|
2021-01-11 19:32:15 +05:30
|
|
|
|
2021-03-20 17:21:41 +05:30
|
|
|
if grain_data is None:
|
2021-04-05 13:43:08 +05:30
|
|
|
phase = f['/'.join([b,c,phases])][()].flatten()
|
|
|
|
O = Rotation.from_Euler_angles(f['/'.join([b,c,Euler_angles])]).as_quaternion().reshape(-1,4) # noqa
|
2021-03-20 17:21:41 +05:30
|
|
|
_,idx = np.unique(np.hstack([O,phase.reshape(-1,1)]),return_index=True,axis=0)
|
|
|
|
idx = np.sort(idx)
|
2021-01-12 17:31:11 +05:30
|
|
|
else:
|
2021-04-05 13:43:08 +05:30
|
|
|
phase = f['/'.join([b,grain_data,phases])][()]
|
|
|
|
O = Rotation.from_Euler_angles(f['/'.join([b,grain_data,Euler_angles])]).as_quaternion() # noqa
|
2021-03-20 17:21:41 +05:30
|
|
|
idx = np.arange(phase.size)
|
2021-01-12 17:31:11 +05:30
|
|
|
|
2021-03-23 18:12:04 +05:30
|
|
|
if cell_ensemble_data is not None and phase_names is not None:
|
|
|
|
try:
|
2021-04-05 13:43:08 +05:30
|
|
|
names = np.array([s.decode() for s in f['/'.join([b,cell_ensemble_data,phase_names])]])
|
2021-03-23 18:12:04 +05:30
|
|
|
phase = names[phase]
|
|
|
|
except KeyError:
|
|
|
|
pass
|
|
|
|
|
2021-01-11 19:32:15 +05:30
|
|
|
|
2022-11-11 05:44:16 +05:30
|
|
|
base_config = ConfigMaterial({'phase':{k if isinstance(k,int) else str(k): None for k in np.unique(phase)},
|
2021-03-23 16:34:40 +05:30
|
|
|
'homogenization':{'direct':{'N_constituents':1}}})
|
|
|
|
constituent = {k:np.atleast_1d(v[idx].squeeze()) for k,v in zip(['O','phase'],[O,phase])}
|
|
|
|
|
|
|
|
return base_config.material_add(**constituent,homogenization='direct')
|
2021-03-12 13:13:57 +05:30
|
|
|
|
2021-03-12 12:39:25 +05:30
|
|
|
|
2021-04-23 22:50:07 +05:30
|
|
|
@staticmethod
|
2022-02-01 13:00:00 +05:30
|
|
|
def from_table(table: Table,
|
|
|
|
**kwargs) -> 'ConfigMaterial':
|
2021-04-23 22:50:07 +05:30
|
|
|
"""
|
|
|
|
Generate from an ASCII table.
|
|
|
|
|
|
|
|
Parameters
|
|
|
|
----------
|
|
|
|
table : damask.Table
|
|
|
|
Table that contains material information.
|
|
|
|
**kwargs
|
2022-02-14 10:24:16 +05:30
|
|
|
Keyword arguments where the key is the property name and
|
|
|
|
the value specifies either the label of the data column in the table
|
|
|
|
or a constant value.
|
2021-04-23 22:50:07 +05:30
|
|
|
|
2021-04-25 11:17:00 +05:30
|
|
|
Returns
|
|
|
|
-------
|
|
|
|
new : damask.ConfigMaterial
|
|
|
|
Material configuration from values in table.
|
|
|
|
|
2021-04-23 22:50:07 +05:30
|
|
|
Examples
|
|
|
|
--------
|
|
|
|
>>> import damask
|
|
|
|
>>> import damask.ConfigMaterial as cm
|
|
|
|
>>> t = damask.Table.load('small.txt')
|
|
|
|
>>> t
|
2022-11-11 22:08:02 +05:30
|
|
|
3:pos pos pos 4:qu qu qu qu phase homog
|
|
|
|
0 0 0 0 0.19 0.8 0.24 -0.51 Aluminum SX
|
|
|
|
1 1 0 0 0.8 0.19 0.24 -0.51 Steel SX
|
|
|
|
2 1 1 0 0.8 0.19 0.24 -0.51 Steel SX
|
2021-04-23 22:50:07 +05:30
|
|
|
>>> cm.from_table(t,O='qu',phase='phase',homogenization='homog')
|
|
|
|
material:
|
|
|
|
- constituents:
|
|
|
|
- O: [0.19, 0.8, 0.24, -0.51]
|
|
|
|
v: 1.0
|
|
|
|
phase: Aluminum
|
|
|
|
homogenization: SX
|
|
|
|
- constituents:
|
|
|
|
- O: [0.8, 0.19, 0.24, -0.51]
|
|
|
|
v: 1.0
|
|
|
|
phase: Steel
|
|
|
|
homogenization: SX
|
2022-11-11 05:44:16 +05:30
|
|
|
homogenization: {SX: null}
|
|
|
|
phase: {Aluminum: null, Steel: null}
|
2021-04-23 22:50:07 +05:30
|
|
|
|
2022-02-14 20:02:02 +05:30
|
|
|
>>> cm.from_table(t,O='qu',phase='phase',homogenization='single_crystal')
|
2022-02-14 10:24:16 +05:30
|
|
|
material:
|
|
|
|
- constituents:
|
|
|
|
- O: [0.19, 0.8, 0.24, -0.51]
|
|
|
|
v: 1.0
|
|
|
|
phase: Aluminum
|
2022-02-14 20:02:02 +05:30
|
|
|
homogenization: single_crystal
|
2022-02-14 10:24:16 +05:30
|
|
|
- constituents:
|
|
|
|
- O: [0.8, 0.19, 0.24, -0.51]
|
|
|
|
v: 1.0
|
|
|
|
phase: Steel
|
2022-02-14 20:02:02 +05:30
|
|
|
homogenization: single_crystal
|
2022-11-11 05:44:16 +05:30
|
|
|
homogenization: {single_crystal: null}
|
|
|
|
phase: {Aluminum: null, Steel: null}
|
2022-02-14 10:24:16 +05:30
|
|
|
|
2021-04-23 22:50:07 +05:30
|
|
|
"""
|
2022-02-14 10:24:16 +05:30
|
|
|
kwargs_ = {k:table.get(v) if v in table.labels else np.atleast_2d([v]*len(table)).T for k,v in kwargs.items()}
|
2021-04-23 22:50:07 +05:30
|
|
|
|
|
|
|
_,idx = np.unique(np.hstack(list(kwargs_.values())),return_index=True,axis=0)
|
|
|
|
idx = np.sort(idx)
|
2022-11-18 21:31:01 +05:30
|
|
|
kwargs_ = {k:np.atleast_1d(v[idx].squeeze()) for k,v in kwargs_.items()}
|
|
|
|
for what in ['phase','homogenization']:
|
|
|
|
if what not in kwargs_: kwargs_[what] = what+'_label'
|
2021-04-23 22:50:07 +05:30
|
|
|
|
|
|
|
return ConfigMaterial().material_add(**kwargs_)
|
|
|
|
|
|
|
|
|
2020-09-19 16:31:19 +05:30
|
|
|
@property
|
2022-02-01 13:00:00 +05:30
|
|
|
def is_complete(self) -> bool:
|
2021-04-23 22:50:07 +05:30
|
|
|
"""
|
|
|
|
Check for completeness.
|
|
|
|
|
|
|
|
Only the general file layout is considered.
|
2022-11-12 04:37:48 +05:30
|
|
|
This check does not consider whether specific parameters for
|
2021-04-23 22:50:07 +05:30
|
|
|
a particular phase/homogenization model are missing.
|
|
|
|
|
|
|
|
Returns
|
|
|
|
-------
|
|
|
|
complete : bool
|
|
|
|
Whether the material.yaml definition is complete.
|
|
|
|
|
|
|
|
"""
|
2022-11-15 01:22:07 +05:30
|
|
|
def LabeledList(label,items):
|
|
|
|
return f'{label.capitalize()}{"s" if len(items)>1 else ""} {util.srepr(items,",",quote=True)}'
|
|
|
|
|
2020-09-21 22:40:20 +05:30
|
|
|
ok = True
|
2022-11-12 04:37:48 +05:30
|
|
|
msg = []
|
|
|
|
all = set(['homogenization','phase','material'])
|
|
|
|
miss = set([item for item in all if item not in self])
|
|
|
|
empty = set([item for item in all-miss if self[item] is None])
|
2020-09-21 22:40:20 +05:30
|
|
|
|
2022-11-12 04:37:48 +05:30
|
|
|
if miss:
|
2022-11-15 01:22:07 +05:30
|
|
|
msg.append(f'{LabeledList("top-level",miss)} missing')
|
2022-11-12 04:37:48 +05:30
|
|
|
ok = False
|
|
|
|
if empty:
|
2022-11-15 01:22:07 +05:30
|
|
|
msg.append(f'{LabeledList("top-level",empty)} empty')
|
2020-09-21 22:40:20 +05:30
|
|
|
|
|
|
|
if ok:
|
2022-11-12 04:37:48 +05:30
|
|
|
ok &= len(self['material']) > 0
|
|
|
|
if len(self['material']) < 1: msg.append('No materials defined')
|
|
|
|
|
2020-09-21 22:40:20 +05:30
|
|
|
homogenization = set()
|
|
|
|
phase = set()
|
2020-10-02 21:21:33 +05:30
|
|
|
for i,v in enumerate(self['material']):
|
2020-09-21 22:40:20 +05:30
|
|
|
if 'homogenization' in v:
|
|
|
|
homogenization.add(v['homogenization'])
|
|
|
|
else:
|
2022-11-12 04:37:48 +05:30
|
|
|
msg.append(f'No homogenization specified for material {i}')
|
2020-09-21 22:40:20 +05:30
|
|
|
ok = False
|
|
|
|
|
|
|
|
if 'constituents' in v:
|
|
|
|
for ii,vv in enumerate(v['constituents']):
|
2020-10-02 21:21:33 +05:30
|
|
|
if 'O' not in vv:
|
2022-11-12 04:37:48 +05:30
|
|
|
msg.append(f'No orientation specified for constituent {ii} of material {i}')
|
2020-09-21 22:40:20 +05:30
|
|
|
ok = False
|
|
|
|
if 'phase' in vv:
|
|
|
|
phase.add(vv['phase'])
|
|
|
|
else:
|
2022-11-12 04:37:48 +05:30
|
|
|
msg.append(f'No phase specified for constituent {ii} of material {i}')
|
2020-09-21 22:40:20 +05:30
|
|
|
ok = False
|
|
|
|
|
2022-11-12 04:37:48 +05:30
|
|
|
for v,other in {'phase':phase,
|
|
|
|
'homogenization':homogenization}.items():
|
|
|
|
me = set([] if v in empty else self[v])
|
|
|
|
if _miss := other - me:
|
2022-11-15 01:22:07 +05:30
|
|
|
msg.append(f'{LabeledList(v,_miss)} missing')
|
2020-09-21 22:40:20 +05:30
|
|
|
ok = False
|
2022-11-15 01:22:07 +05:30
|
|
|
if len(_empty := [item for item in me if self[v][item] is None]) > 0:
|
|
|
|
msg.append(f'{LabeledList(v,_empty)} undefined')
|
2020-10-09 00:59:58 +05:30
|
|
|
ok = False
|
2020-09-21 22:40:20 +05:30
|
|
|
|
2022-11-12 04:37:48 +05:30
|
|
|
print(util.srepr(msg))
|
2020-09-21 22:40:20 +05:30
|
|
|
return ok
|
2020-09-19 16:31:19 +05:30
|
|
|
|
|
|
|
|
|
|
|
@property
|
2022-02-01 13:00:00 +05:30
|
|
|
def is_valid(self) -> bool:
|
2021-04-23 22:50:07 +05:30
|
|
|
"""
|
|
|
|
Check for valid content.
|
|
|
|
|
|
|
|
Only the generic file content is considered.
|
|
|
|
This check does not consider whether parameters for a
|
|
|
|
particular phase/homogenization mode are out of bounds.
|
|
|
|
|
|
|
|
Returns
|
|
|
|
-------
|
|
|
|
valid : bool
|
|
|
|
Whether the material.yaml definition is valid.
|
|
|
|
|
|
|
|
"""
|
2020-09-19 16:31:19 +05:30
|
|
|
ok = True
|
|
|
|
|
|
|
|
if 'phase' in self:
|
2020-09-21 22:40:20 +05:30
|
|
|
for k,v in self['phase'].items():
|
2022-11-11 05:44:16 +05:30
|
|
|
if v is not None and 'lattice' in v:
|
2020-09-19 16:31:19 +05:30
|
|
|
try:
|
2020-11-10 01:50:56 +05:30
|
|
|
Orientation(lattice=v['lattice'])
|
2020-09-19 16:31:19 +05:30
|
|
|
except KeyError:
|
2021-02-24 05:24:55 +05:30
|
|
|
print(f"Invalid lattice '{v['lattice']}' in phase '{k}'")
|
2020-09-19 16:31:19 +05:30
|
|
|
ok = False
|
|
|
|
|
2020-10-02 21:21:33 +05:30
|
|
|
if 'material' in self:
|
2021-02-07 19:35:38 +05:30
|
|
|
for i,m in enumerate(self['material']):
|
|
|
|
if 'constituents' in m:
|
|
|
|
v = 0.0
|
|
|
|
for c in m['constituents']:
|
2021-02-24 05:24:55 +05:30
|
|
|
v += float(c['v'])
|
2020-10-02 21:21:33 +05:30
|
|
|
if 'O' in c:
|
2020-09-19 16:31:19 +05:30
|
|
|
try:
|
2020-10-02 21:21:33 +05:30
|
|
|
Rotation.from_quaternion(c['O'])
|
2020-09-19 16:31:19 +05:30
|
|
|
except ValueError:
|
2021-02-24 05:24:55 +05:30
|
|
|
print(f"Invalid orientation '{c['O']}' in material '{i}'")
|
2020-09-19 16:31:19 +05:30
|
|
|
ok = False
|
2021-02-07 19:35:38 +05:30
|
|
|
if not np.isclose(v,1.0):
|
2021-02-24 05:24:55 +05:30
|
|
|
print(f"Total fraction v = {v} ≠ 1 in material '{i}'")
|
2020-09-19 16:31:19 +05:30
|
|
|
ok = False
|
|
|
|
|
|
|
|
return ok
|
|
|
|
|
|
|
|
|
2022-02-01 13:00:00 +05:30
|
|
|
def material_rename_phase(self,
|
|
|
|
mapping: Dict[str, str],
|
2022-11-23 02:56:15 +05:30
|
|
|
ID: Optional[Sequence[int]] = None,
|
|
|
|
constituent: Optional[Sequence[int]] = None) -> 'ConfigMaterial':
|
2020-09-19 16:31:19 +05:30
|
|
|
"""
|
2020-10-02 21:21:33 +05:30
|
|
|
Change phase name in material.
|
2020-09-19 16:31:19 +05:30
|
|
|
|
|
|
|
Parameters
|
|
|
|
----------
|
|
|
|
mapping: dictionary
|
|
|
|
Mapping from old name to new name
|
|
|
|
ID: list of ints, optional
|
2020-10-02 21:21:33 +05:30
|
|
|
Limit renaming to selected material IDs.
|
2020-09-19 16:31:19 +05:30
|
|
|
constituent: list of ints, optional
|
|
|
|
Limit renaming to selected constituents.
|
|
|
|
|
2021-02-24 05:24:55 +05:30
|
|
|
Returns
|
|
|
|
-------
|
2021-04-23 22:50:07 +05:30
|
|
|
updated : damask.ConfigMaterial
|
2021-02-24 05:24:55 +05:30
|
|
|
Updated material configuration.
|
|
|
|
|
2020-09-19 16:31:19 +05:30
|
|
|
"""
|
2021-01-03 16:33:40 +05:30
|
|
|
dup = self.copy()
|
2020-10-02 21:21:33 +05:30
|
|
|
for i,m in enumerate(dup['material']):
|
2021-02-19 21:04:28 +05:30
|
|
|
if ID is not None and i not in ID: continue
|
2020-09-19 16:31:19 +05:30
|
|
|
for c in m['constituents']:
|
|
|
|
if constituent is not None and c not in constituent: continue
|
|
|
|
try:
|
|
|
|
c['phase'] = mapping[c['phase']]
|
|
|
|
except KeyError:
|
|
|
|
continue
|
|
|
|
return dup
|
|
|
|
|
|
|
|
|
2022-02-01 13:00:00 +05:30
|
|
|
def material_rename_homogenization(self,
|
|
|
|
mapping: Dict[str, str],
|
2022-11-23 02:56:15 +05:30
|
|
|
ID: Optional[Sequence[int]] = None) -> 'ConfigMaterial':
|
2020-09-19 16:31:19 +05:30
|
|
|
"""
|
2020-10-02 21:21:33 +05:30
|
|
|
Change homogenization name in material.
|
2020-09-19 16:31:19 +05:30
|
|
|
|
|
|
|
Parameters
|
|
|
|
----------
|
|
|
|
mapping: dictionary
|
|
|
|
Mapping from old name to new name
|
|
|
|
ID: list of ints, optional
|
|
|
|
Limit renaming to selected homogenization IDs.
|
|
|
|
|
2021-02-24 05:24:55 +05:30
|
|
|
Returns
|
|
|
|
-------
|
2021-04-23 22:50:07 +05:30
|
|
|
updated : damask.ConfigMaterial
|
2021-02-24 05:24:55 +05:30
|
|
|
Updated material configuration.
|
|
|
|
|
2020-09-19 16:31:19 +05:30
|
|
|
"""
|
2021-01-03 16:33:40 +05:30
|
|
|
dup = self.copy()
|
2020-10-02 21:21:33 +05:30
|
|
|
for i,m in enumerate(dup['material']):
|
2021-02-19 21:04:28 +05:30
|
|
|
if ID is not None and i not in ID: continue
|
2020-09-19 16:31:19 +05:30
|
|
|
try:
|
|
|
|
m['homogenization'] = mapping[m['homogenization']]
|
|
|
|
except KeyError:
|
|
|
|
continue
|
|
|
|
return dup
|
2020-10-09 00:59:58 +05:30
|
|
|
|
|
|
|
|
2022-02-01 13:00:00 +05:30
|
|
|
def material_add(self,
|
|
|
|
**kwargs: Any) -> 'ConfigMaterial':
|
2020-10-09 00:59:58 +05:30
|
|
|
"""
|
|
|
|
Add material entries.
|
|
|
|
|
|
|
|
Parameters
|
|
|
|
----------
|
2020-10-09 17:49:19 +05:30
|
|
|
**kwargs
|
2020-10-29 02:22:51 +05:30
|
|
|
Key-value pairs.
|
2022-11-19 03:00:26 +05:30
|
|
|
First index of array-like values runs over materials,
|
|
|
|
whereas second index runs over constituents.
|
2020-10-09 00:59:58 +05:30
|
|
|
|
2021-02-24 05:10:32 +05:30
|
|
|
Returns
|
|
|
|
-------
|
2021-04-23 22:50:07 +05:30
|
|
|
updated : damask.ConfigMaterial
|
2021-02-24 05:10:32 +05:30
|
|
|
Updated material configuration.
|
|
|
|
|
2020-10-09 00:59:58 +05:30
|
|
|
Examples
|
|
|
|
--------
|
2022-11-19 03:00:26 +05:30
|
|
|
Create two grains of ferrite and one grain of martensite, each with random orientation:
|
2021-04-24 10:43:36 +05:30
|
|
|
|
2020-10-09 17:49:19 +05:30
|
|
|
>>> import damask
|
2021-04-24 10:43:36 +05:30
|
|
|
>>> m = damask.ConfigMaterial()
|
2022-11-19 03:00:26 +05:30
|
|
|
>>> m = m.material_add(phase = ['Ferrite','Martensite','Ferrite'],
|
|
|
|
... O = damask.Rotation.from_random(3),
|
2021-04-24 10:43:36 +05:30
|
|
|
... homogenization = 'SX')
|
2020-10-30 00:39:13 +05:30
|
|
|
>>> m
|
2020-10-09 17:49:19 +05:30
|
|
|
material:
|
|
|
|
- constituents:
|
|
|
|
- O: [0.577764, -0.146299, -0.617669, 0.513010]
|
2021-02-04 18:16:01 +05:30
|
|
|
v: 1.0
|
2021-04-24 10:43:36 +05:30
|
|
|
phase: Ferrite
|
2020-10-09 17:49:19 +05:30
|
|
|
homogenization: SX
|
|
|
|
- constituents:
|
|
|
|
- O: [0.184176, 0.340305, 0.737247, 0.553840]
|
2021-02-04 18:16:01 +05:30
|
|
|
v: 1.0
|
2021-04-24 10:43:36 +05:30
|
|
|
phase: Martensite
|
2020-10-09 17:49:19 +05:30
|
|
|
homogenization: SX
|
2022-11-19 03:00:26 +05:30
|
|
|
- constituents:
|
|
|
|
- O: [0.47925185, -0.04294454, 0.78760173, -0.3849116 ]
|
|
|
|
v: 1.0
|
|
|
|
phase: Ferrite
|
|
|
|
homogenization: SX
|
2022-11-11 05:44:16 +05:30
|
|
|
homogenization: {SX: null}
|
|
|
|
phase: {Ferrite: null, Martensite: null}
|
2021-02-24 05:10:32 +05:30
|
|
|
|
2022-11-19 03:00:26 +05:30
|
|
|
Create hundred materials that each approximate a duplex stainless steel microstructure
|
|
|
|
with three austenite and one relatively bigger ferrite grain of random orientation each:
|
2021-04-24 10:43:36 +05:30
|
|
|
|
|
|
|
>>> import damask
|
|
|
|
>>> m = damask.ConfigMaterial()
|
2022-11-19 03:00:26 +05:30
|
|
|
>>> m = m.material_add(phase = np.array(['Austenite']*3+['Ferrite']),
|
|
|
|
... O = damask.Rotation.from_random((100,4)),
|
|
|
|
... v = np.array([0.2]*3+[0.4]),
|
2021-04-24 10:43:36 +05:30
|
|
|
... homogenization = 'Taylor')
|
2021-02-24 05:10:32 +05:30
|
|
|
>>> m
|
|
|
|
material:
|
2020-10-09 17:49:19 +05:30
|
|
|
- constituents:
|
2022-11-19 03:00:26 +05:30
|
|
|
- v: 0.2
|
|
|
|
phase: Austenite
|
|
|
|
O: [0.46183665006602664, 0.2215160420973196, -0.5594313187331139, 0.6516702781083836]
|
|
|
|
- v: 0.2
|
|
|
|
phase: Austenite
|
|
|
|
O: [0.11321658382410027, 0.6354079414360444, 0.00562701344273936, 0.7638108992590535]
|
|
|
|
- v: 0.2
|
|
|
|
phase: Austenite
|
|
|
|
O: [0.050991978809077604, 0.8069522034362003, -0.11352928955610851, -0.5773552285027659]
|
|
|
|
- v: 0.4
|
|
|
|
phase: Ferrite
|
|
|
|
O: [0.9460076150721788, 0.15880754622367604, -0.0069841062241482385, -0.28249066842661014]
|
2021-04-24 10:43:36 +05:30
|
|
|
homogenization: Taylor
|
2022-11-19 03:00:26 +05:30
|
|
|
.
|
|
|
|
.
|
|
|
|
.
|
2021-02-24 05:10:32 +05:30
|
|
|
- constituents:
|
2022-11-19 03:00:26 +05:30
|
|
|
- v: 0.2
|
|
|
|
phase: Austenite
|
|
|
|
O: [0.12531400788494199, -0.18637769037997565, 0.31737548053338394, -0.9213210951197429]
|
|
|
|
- v: 0.2
|
|
|
|
phase: Austenite
|
|
|
|
O: [0.37453930577161404, -0.33529507696450805, -0.3266564259130028, -0.800370601162502]
|
|
|
|
- v: 0.2
|
|
|
|
phase: Austenite
|
|
|
|
O: [0.035776891752713764, -0.720706371010592, -0.4540438656728926, -0.5226342017569017]
|
|
|
|
- v: 0.4
|
|
|
|
phase: Ferrite
|
|
|
|
O: [0.6782596727966124, -0.20800082041703685, -0.138636083554039, 0.6909989227925536]
|
2021-04-24 10:43:36 +05:30
|
|
|
homogenization: Taylor
|
2022-11-19 03:00:26 +05:30
|
|
|
|
2022-11-11 05:44:16 +05:30
|
|
|
homogenization: {Taylor: null}
|
2022-11-19 03:00:26 +05:30
|
|
|
|
2022-11-11 05:44:16 +05:30
|
|
|
phase: {Austenite: null, Ferrite: null}
|
2020-10-09 22:49:05 +05:30
|
|
|
|
2020-10-09 00:59:58 +05:30
|
|
|
"""
|
2022-11-19 03:00:26 +05:30
|
|
|
_constituent_properties = ['phase','O','v','V_e']
|
|
|
|
_dim = {'O':(4,),'V_e':(3,3,)}
|
|
|
|
_ex = dict((k, -len(v)) for k, v in _dim.items())
|
|
|
|
|
2022-11-20 07:25:23 +05:30
|
|
|
N,n = 1,1
|
|
|
|
shaped : Dict[str, Union[None,np.ndarray]] = \
|
|
|
|
{'v': None,
|
|
|
|
'phase': None,
|
|
|
|
'homogenization': None,
|
|
|
|
}
|
2020-11-14 21:43:38 +05:30
|
|
|
|
|
|
|
for k,v in kwargs.items():
|
2021-02-23 09:14:12 +05:30
|
|
|
shaped[k] = np.array(v)
|
2022-11-20 07:25:23 +05:30
|
|
|
s = shaped[k].shape[:_ex.get(k,None)] # type: ignore
|
2021-02-23 09:14:12 +05:30
|
|
|
N = max(N,s[0]) if len(s)>0 else N
|
|
|
|
n = max(n,s[1]) if len(s)>1 else n
|
|
|
|
|
2022-11-19 03:00:26 +05:30
|
|
|
shaped['v'] = np.array(1./n) if shaped['v'] is None else shaped['v']
|
2021-02-23 09:14:12 +05:30
|
|
|
|
2022-11-19 03:00:26 +05:30
|
|
|
mat: Sequence[dict] = [{'constituents':[{} for _ in range(n)]} for _ in range(N)]
|
2021-02-23 09:14:12 +05:30
|
|
|
|
|
|
|
for k,v in shaped.items():
|
2022-11-19 03:00:26 +05:30
|
|
|
target = (N,n) + _dim.get(k,())
|
|
|
|
obj = np.broadcast_to(np.array(v).reshape(util.shapeshifter(() if v is None else v.shape,
|
|
|
|
target,
|
|
|
|
mode = 'right')),
|
|
|
|
target)
|
2021-02-23 09:14:12 +05:30
|
|
|
for i in range(N):
|
2022-11-19 03:00:26 +05:30
|
|
|
if k in _constituent_properties:
|
2021-02-23 09:14:12 +05:30
|
|
|
for j in range(n):
|
|
|
|
mat[i]['constituents'][j][k] = obj[i,j].item() if isinstance(obj[i,j],np.generic) else obj[i,j]
|
|
|
|
else:
|
|
|
|
mat[i][k] = obj[i,0].item() if isinstance(obj[i,0],np.generic) else obj[i,0]
|
2020-10-09 00:59:58 +05:30
|
|
|
|
2021-01-03 16:37:02 +05:30
|
|
|
dup = self.copy()
|
2021-02-23 09:14:12 +05:30
|
|
|
dup['material'] = dup['material'] + mat if 'material' in dup else mat
|
2022-11-11 05:44:16 +05:30
|
|
|
|
2022-11-20 07:25:23 +05:30
|
|
|
for what in [item for item in ['phase','homogenization'] if shaped[item] is not None]:
|
|
|
|
for k in np.unique(shaped[what]): # type: ignore
|
|
|
|
if k not in dup[what]: dup[what][str(k)] = None
|
2020-10-09 00:59:58 +05:30
|
|
|
|
|
|
|
return dup
|