2020-09-19 16:31:19 +05:30
|
|
|
import numpy as np
|
|
|
|
|
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
|
2020-09-19 16:31:19 +05:30
|
|
|
|
2020-09-30 11:23:25 +05:30
|
|
|
class ConfigMaterial(Config):
|
2020-09-19 16:31:19 +05:30
|
|
|
"""Material configuration."""
|
|
|
|
|
2020-12-18 04:38:55 +05:30
|
|
|
_defaults = {'material': [],
|
|
|
|
'homogenization': {},
|
|
|
|
'phase': {}}
|
|
|
|
|
2021-01-03 16:40:39 +05:30
|
|
|
def __init__(self,d=_defaults):
|
2020-12-18 04:38:55 +05:30
|
|
|
"""Initialize object with default dictionary keys."""
|
|
|
|
super().__init__(d)
|
2021-01-03 16:40:39 +05:30
|
|
|
|
2020-12-17 21:17:56 +05:30
|
|
|
|
2020-09-30 16:02:37 +05:30
|
|
|
def save(self,fname='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
|
|
|
|
def load(cls,fname='material.yaml'):
|
|
|
|
"""
|
|
|
|
Load from yaml file.
|
|
|
|
|
|
|
|
Parameters
|
|
|
|
----------
|
|
|
|
fname : file, str, or pathlib.Path, optional
|
|
|
|
Filename or file for writing. Defaults to 'material.yaml'.
|
|
|
|
|
|
|
|
"""
|
|
|
|
return super(ConfigMaterial,cls).load(fname)
|
|
|
|
|
|
|
|
|
2020-10-09 11:15:20 +05:30
|
|
|
@staticmethod
|
2021-02-23 09:14:12 +05:30
|
|
|
def from_table(table,**kwargs):
|
2020-10-09 17:49:19 +05:30
|
|
|
"""
|
|
|
|
Load from an ASCII table.
|
|
|
|
|
|
|
|
Parameters
|
|
|
|
----------
|
2020-10-09 22:49:05 +05:30
|
|
|
table : damask.Table
|
2020-10-09 17:49:19 +05:30
|
|
|
Table that contains material information.
|
|
|
|
**kwargs
|
2021-02-23 09:14:12 +05:30
|
|
|
Keyword arguments where the key is the name and the value specifies
|
|
|
|
the label of the data column in the table.
|
2020-10-09 17:49:19 +05:30
|
|
|
|
|
|
|
Examples
|
|
|
|
--------
|
|
|
|
>>> import damask
|
|
|
|
>>> import damask.ConfigMaterial as cm
|
2020-10-09 22:49:05 +05:30
|
|
|
>>> t = damask.Table.load('small.txt')
|
|
|
|
>>> t
|
2020-10-09 17:49:19 +05:30
|
|
|
pos pos pos 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
|
2021-02-23 22:20:13 +05:30
|
|
|
1 1 1 0 0.8 0.19 0.24 -0.51 Steel SX
|
2021-02-23 09:14:12 +05:30
|
|
|
>>> cm.from_table(t,O='qu',phase='phase',homogenization='homog')
|
2020-10-09 17:49:19 +05:30
|
|
|
material:
|
|
|
|
- constituents:
|
|
|
|
- O: [0.19, 0.8, 0.24, -0.51]
|
2021-02-04 18:16:01 +05:30
|
|
|
v: 1.0
|
2020-10-09 17:49:19 +05:30
|
|
|
phase: Aluminum
|
|
|
|
homogenization: SX
|
|
|
|
- constituents:
|
|
|
|
- O: [0.8, 0.19, 0.24, -0.51]
|
2021-02-04 18:16:01 +05:30
|
|
|
v: 1.0
|
2020-10-09 17:49:19 +05:30
|
|
|
phase: Steel
|
|
|
|
homogenization: SX
|
2020-12-18 11:39:05 +05:30
|
|
|
homogenization: {}
|
|
|
|
phase: {}
|
2020-10-09 17:49:19 +05:30
|
|
|
|
|
|
|
"""
|
2020-10-29 11:47:41 +05:30
|
|
|
kwargs_ = {k:table.get(v) for k,v in kwargs.items()}
|
2020-10-09 11:15:20 +05:30
|
|
|
|
2021-02-23 22:20:13 +05:30
|
|
|
_,idx = np.unique(np.hstack(list(kwargs_.values())),return_index=True,axis=0)
|
2020-10-29 11:47:41 +05:30
|
|
|
idx = np.sort(idx)
|
2021-02-23 22:20:13 +05:30
|
|
|
kwargs_ = {k:np.atleast_1d(v[idx].squeeze()) for k,v in kwargs_.items()}
|
2020-10-09 11:15:20 +05:30
|
|
|
|
2021-02-23 09:14:12 +05:30
|
|
|
return ConfigMaterial().material_add(**kwargs_)
|
2020-10-09 11:15:20 +05:30
|
|
|
|
|
|
|
|
2020-09-19 16:31:19 +05:30
|
|
|
@property
|
|
|
|
def is_complete(self):
|
|
|
|
"""Check for completeness."""
|
2020-09-21 22:40:20 +05:30
|
|
|
ok = True
|
2020-10-02 21:21:33 +05:30
|
|
|
for top_level in ['homogenization','phase','material']:
|
2020-09-21 22:40:20 +05:30
|
|
|
ok &= top_level in self
|
|
|
|
if top_level not in self: print(f'{top_level} entry missing')
|
|
|
|
|
|
|
|
if ok:
|
2020-10-02 21:21:33 +05:30
|
|
|
ok &= len(self['material']) > 0
|
|
|
|
if len(self['material']) < 1: print('Incomplete material definition')
|
2020-09-21 22:40:20 +05:30
|
|
|
|
|
|
|
if ok:
|
|
|
|
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:
|
2020-10-02 21:21:33 +05:30
|
|
|
print(f'No homogenization specified in 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:
|
|
|
|
print('No orientation specified in constituent {ii} of material {i}')
|
2020-09-21 22:40:20 +05:30
|
|
|
ok = False
|
|
|
|
if 'phase' in vv:
|
|
|
|
phase.add(vv['phase'])
|
|
|
|
else:
|
2020-10-02 21:21:33 +05:30
|
|
|
print(f'No phase specified in constituent {ii} of material {i}')
|
2020-09-21 22:40:20 +05:30
|
|
|
ok = False
|
|
|
|
|
|
|
|
for k,v in self['phase'].items():
|
|
|
|
if 'lattice' not in v:
|
|
|
|
print(f'No lattice specified in phase {k}')
|
|
|
|
ok = False
|
|
|
|
|
2020-10-09 00:59:58 +05:30
|
|
|
for k,v in self['homogenization'].items():
|
|
|
|
if 'N_constituents' not in v:
|
|
|
|
print(f'No. of constituents not specified in homogenization {k}')
|
|
|
|
ok = False
|
2020-09-21 22:40:20 +05:30
|
|
|
|
|
|
|
if phase - set(self['phase']):
|
|
|
|
print(f'Phase(s) {phase-set(self["phase"])} missing')
|
|
|
|
ok = False
|
|
|
|
if homogenization - set(self['homogenization']):
|
|
|
|
print(f'Homogenization(s) {homogenization-set(self["homogenization"])} missing')
|
|
|
|
ok = False
|
|
|
|
|
|
|
|
return ok
|
2020-09-19 16:31:19 +05:30
|
|
|
|
|
|
|
|
|
|
|
@property
|
|
|
|
def is_valid(self):
|
2021-02-24 05:24:55 +05:30
|
|
|
"""Check for valid content."""
|
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():
|
2020-09-19 16:31:19 +05:30
|
|
|
if 'lattice' in v:
|
|
|
|
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
|
|
|
|
|
|
|
|
|
2020-10-02 21:21:33 +05:30
|
|
|
def material_rename_phase(self,mapping,ID=None,constituent=None):
|
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
|
|
|
|
-------
|
|
|
|
cfg : damask.ConfigMaterial
|
|
|
|
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
|
|
|
|
|
|
|
|
|
2020-10-02 21:21:33 +05:30
|
|
|
def material_rename_homogenization(self,mapping,ID=None):
|
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
|
|
|
|
-------
|
|
|
|
cfg : damask.ConfigMaterial
|
|
|
|
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
|
|
|
|
|
|
|
|
2021-02-23 09:14:12 +05:30
|
|
|
def material_add(self,**kwargs):
|
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.
|
2020-10-09 00:59:58 +05:30
|
|
|
|
2021-02-24 05:10:32 +05:30
|
|
|
Returns
|
|
|
|
-------
|
|
|
|
cfg : damask.ConfigMaterial
|
|
|
|
Updated material configuration.
|
|
|
|
|
2020-10-09 00:59:58 +05:30
|
|
|
Examples
|
|
|
|
--------
|
2021-02-24 05:10:32 +05:30
|
|
|
>>> import numpy as np
|
2020-10-09 17:49:19 +05:30
|
|
|
>>> import damask
|
2021-02-24 05:10:32 +05:30
|
|
|
>>> m = damask.ConfigMaterial().material_add(phase = ['Aluminum','Steel'],
|
|
|
|
... O = damask.Rotation.from_random(2),
|
2021-02-23 09:14:12 +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
|
2020-10-09 17:49:19 +05:30
|
|
|
phase: Aluminum
|
|
|
|
homogenization: SX
|
|
|
|
- constituents:
|
|
|
|
- O: [0.184176, 0.340305, 0.737247, 0.553840]
|
2021-02-04 18:16:01 +05:30
|
|
|
v: 1.0
|
2020-10-09 17:49:19 +05:30
|
|
|
phase: Steel
|
|
|
|
homogenization: SX
|
2021-02-24 05:10:32 +05:30
|
|
|
homogenization: {}
|
|
|
|
phase: {}
|
|
|
|
|
|
|
|
>>> m = damask.ConfigMaterial().material_add(phase = np.array(['Austenite','Martensite']).reshape(1,2),
|
|
|
|
... O = damask.Rotation.from_random((2,2)),
|
|
|
|
... v = np.array([0.2,0.8]).reshape(1,2),
|
|
|
|
... homogenization = ['A','B'])
|
|
|
|
>>> m
|
|
|
|
material:
|
2020-10-09 17:49:19 +05:30
|
|
|
- constituents:
|
2021-02-24 05:10:32 +05:30
|
|
|
- phase: Austenite
|
|
|
|
O: [0.659802978293224, 0.6953785848195171, 0.22426295326327111, -0.17554139512785227]
|
|
|
|
v: 0.2
|
|
|
|
- phase: Martensite
|
|
|
|
O: [0.49356745891301596, 0.2841806579193434, -0.7487679215072818, -0.339085707289975]
|
|
|
|
v: 0.8
|
|
|
|
homogenization: A
|
|
|
|
- constituents:
|
|
|
|
- phase: Austenite
|
|
|
|
O: [0.26542221365204055, 0.7268854930702071, 0.4474726435701472, -0.44828201137283735]
|
|
|
|
v: 0.2
|
|
|
|
- phase: Martensite
|
|
|
|
O: [0.6545817158479885, -0.08004812803625233, -0.6226561293931374, 0.4212059104577611]
|
|
|
|
v: 0.8
|
|
|
|
homogenization: B
|
2020-12-18 11:39:05 +05:30
|
|
|
homogenization: {}
|
|
|
|
phase: {}
|
2020-10-09 22:49:05 +05:30
|
|
|
|
2020-10-09 00:59:58 +05:30
|
|
|
"""
|
2021-02-23 09:14:12 +05:30
|
|
|
N,n,shaped = 1,1,{}
|
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)
|
|
|
|
s = shaped[k].shape[:-1] if k=='O' else shaped[k].shape
|
|
|
|
N = max(N,s[0]) if len(s)>0 else N
|
|
|
|
n = max(n,s[1]) if len(s)>1 else n
|
|
|
|
|
|
|
|
mat = [{'constituents':[{} for _ in range(n)]} for _ in range(N)]
|
|
|
|
|
|
|
|
if 'v' not in kwargs:
|
|
|
|
shaped['v'] = np.broadcast_to(1/n,(N,n))
|
|
|
|
|
|
|
|
for k,v in shaped.items():
|
2021-02-24 05:31:10 +05:30
|
|
|
target = (N,n,4) if k=='O' else (N,n)
|
|
|
|
obj = np.broadcast_to(v.reshape(util.shapeshifter(v.shape,target,mode='right')),target)
|
2021-02-23 09:14:12 +05:30
|
|
|
for i in range(N):
|
|
|
|
if k in ['phase','O','v']:
|
|
|
|
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]
|
|
|
|
|
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
|
2020-10-09 00:59:58 +05:30
|
|
|
|
|
|
|
return dup
|