2021-06-15 23:08:01 +05:30
|
|
|
|
import os
|
2020-08-22 23:25:18 +05:30
|
|
|
|
import copy
|
2021-06-15 23:08:01 +05:30
|
|
|
|
import warnings
|
2020-09-18 20:02:08 +05:30
|
|
|
|
import multiprocessing as mp
|
2020-03-29 22:42:23 +05:30
|
|
|
|
from functools import partial
|
2022-01-13 01:04:29 +05:30
|
|
|
|
import typing
|
2022-12-14 00:02:19 +05:30
|
|
|
|
from typing import Optional, Union, TextIO, Sequence, Dict
|
2021-12-06 18:52:52 +05:30
|
|
|
|
from pathlib import Path
|
2019-05-25 02:00:25 +05:30
|
|
|
|
|
2019-05-26 15:33:21 +05:30
|
|
|
|
import numpy as np
|
2020-10-28 16:11:34 +05:30
|
|
|
|
import pandas as pd
|
2020-10-08 22:03:40 +05:30
|
|
|
|
import h5py
|
2022-11-09 00:22:08 +05:30
|
|
|
|
from scipy import ndimage, spatial, interpolate
|
2019-05-26 15:33:21 +05:30
|
|
|
|
|
2020-03-11 12:02:03 +05:30
|
|
|
|
from . import VTK
|
2019-05-28 01:30:26 +05:30
|
|
|
|
from . import util
|
2020-03-29 22:42:23 +05:30
|
|
|
|
from . import grid_filters
|
2020-10-08 22:03:40 +05:30
|
|
|
|
from . import Rotation
|
2021-12-06 18:52:52 +05:30
|
|
|
|
from . import Table
|
2022-02-21 15:49:53 +05:30
|
|
|
|
from . import Colormap
|
2022-12-14 00:02:19 +05:30
|
|
|
|
from ._typehints import FloatSequence, IntSequence, NumpyRngSeed
|
2022-12-10 12:52:22 +05:30
|
|
|
|
try:
|
2022-12-14 00:02:19 +05:30
|
|
|
|
import numba as nb # type: ignore
|
2022-12-10 12:52:22 +05:30
|
|
|
|
except ImportError:
|
|
|
|
|
nb = False
|
|
|
|
|
|
|
|
|
|
def numba_njit_wrapper(**kwargs):
|
|
|
|
|
return (lambda function: nb.njit(function) if nb else function)
|
|
|
|
|
|
2019-05-26 15:33:21 +05:30
|
|
|
|
|
2023-11-28 03:11:28 +05:30
|
|
|
|
class GeomGrid:
|
2021-03-27 14:40:35 +05:30
|
|
|
|
"""
|
|
|
|
|
Geometry definition for grid solvers.
|
|
|
|
|
|
2023-02-21 20:57:06 +05:30
|
|
|
|
Create and manipulate geometry definitions for storage as VTK ImageData
|
|
|
|
|
files ('.vti' extension). A grid has a physical size, a coordinate origin,
|
|
|
|
|
and contains the material ID (indexing an entry in 'material.yaml')
|
|
|
|
|
as well as initial condition fields.
|
2021-03-27 14:40:35 +05:30
|
|
|
|
"""
|
2019-11-23 01:22:36 +05:30
|
|
|
|
|
2021-12-06 18:52:52 +05:30
|
|
|
|
def __init__(self,
|
|
|
|
|
material: np.ndarray,
|
2022-01-13 01:04:29 +05:30
|
|
|
|
size: FloatSequence,
|
|
|
|
|
origin: FloatSequence = np.zeros(3),
|
2022-11-23 02:56:15 +05:30
|
|
|
|
initial_conditions: Optional[Dict[str,np.ndarray]] = None,
|
|
|
|
|
comments: Union[None, str, Sequence[str]] = None):
|
2019-11-23 01:22:36 +05:30
|
|
|
|
"""
|
2021-03-27 14:40:35 +05:30
|
|
|
|
New geometry definition for grid solvers.
|
2019-11-23 01:22:36 +05:30
|
|
|
|
|
|
|
|
|
Parameters
|
|
|
|
|
----------
|
2022-11-09 00:22:08 +05:30
|
|
|
|
material : numpy.ndarray of int, shape (:,:,:)
|
2021-06-19 16:58:56 +05:30
|
|
|
|
Material indices. The shape of the material array defines
|
|
|
|
|
the number of cells.
|
2022-01-13 01:04:29 +05:30
|
|
|
|
size : sequence of float, len (3)
|
2021-06-17 21:56:37 +05:30
|
|
|
|
Physical size of grid in meter.
|
2022-01-13 01:04:29 +05:30
|
|
|
|
origin : sequence of float, len (3), optional
|
|
|
|
|
Coordinates of grid origin in meter. Defaults to [0.0,0.0,0.0].
|
2022-03-20 01:19:33 +05:30
|
|
|
|
initial_conditions : dictionary, optional
|
2023-02-21 20:57:06 +05:30
|
|
|
|
Initial condition label and field values at each grid point.
|
2022-11-14 19:39:45 +05:30
|
|
|
|
comments : (sequence of) str, optional
|
2022-05-19 21:49:55 +05:30
|
|
|
|
Additional, human-readable information, e.g. history of operations.
|
2019-11-23 01:22:36 +05:30
|
|
|
|
|
|
|
|
|
"""
|
2020-11-08 22:41:30 +05:30
|
|
|
|
self.material = material
|
2022-12-14 00:02:19 +05:30
|
|
|
|
self.size = size # type: ignore
|
|
|
|
|
self.origin = origin # type: ignore
|
2022-03-25 02:52:18 +05:30
|
|
|
|
self.initial_conditions = {} if initial_conditions is None else initial_conditions
|
2022-12-14 00:02:19 +05:30
|
|
|
|
self.comments = [] if comments is None else \
|
|
|
|
|
[comments] if isinstance(comments,str) else \
|
|
|
|
|
[str(c) for c in comments]
|
2019-05-30 19:05:45 +05:30
|
|
|
|
|
2021-12-06 18:52:52 +05:30
|
|
|
|
def __repr__(self) -> str:
|
2022-07-08 21:36:41 +05:30
|
|
|
|
"""
|
|
|
|
|
Return repr(self).
|
|
|
|
|
|
2023-02-21 20:57:06 +05:30
|
|
|
|
Give short, human-readable summary.
|
2022-07-08 21:36:41 +05:30
|
|
|
|
|
|
|
|
|
"""
|
2020-12-05 15:17:42 +05:30
|
|
|
|
mat_min = np.nanmin(self.material)
|
|
|
|
|
mat_max = np.nanmax(self.material)
|
|
|
|
|
mat_N = self.N_materials
|
2019-11-23 01:22:36 +05:30
|
|
|
|
return util.srepr([
|
2022-01-29 20:29:14 +05:30
|
|
|
|
f'cells: {util.srepr(self.cells, " × ")}',
|
2022-02-17 11:43:39 +05:30
|
|
|
|
f'size: {util.srepr(self.size, " × ")} m³',
|
|
|
|
|
f'origin: {util.srepr(self.origin," ")} m',
|
2020-12-11 01:23:11 +05:30
|
|
|
|
f'# materials: {mat_N}' + ('' if mat_min == 0 and mat_max+1 == mat_N else
|
|
|
|
|
f' (min: {mat_min}, max: {mat_max})')
|
2022-03-25 02:52:18 +05:30
|
|
|
|
]+(['initial_conditions:']+[f' - {f}' for f in self.initial_conditions] if self.initial_conditions else []))
|
2019-11-23 01:22:36 +05:30
|
|
|
|
|
2020-03-15 02:23:48 +05:30
|
|
|
|
|
2023-11-28 03:11:28 +05:30
|
|
|
|
def __copy__(self) -> 'GeomGrid':
|
2022-07-08 21:36:41 +05:30
|
|
|
|
"""
|
|
|
|
|
Return deepcopy(self).
|
|
|
|
|
|
|
|
|
|
Create deep copy.
|
|
|
|
|
|
|
|
|
|
"""
|
2020-08-22 23:25:18 +05:30
|
|
|
|
return copy.deepcopy(self)
|
|
|
|
|
|
2021-01-03 16:33:40 +05:30
|
|
|
|
copy = __copy__
|
2020-08-22 23:25:18 +05:30
|
|
|
|
|
|
|
|
|
|
2022-01-27 04:07:07 +05:30
|
|
|
|
def __eq__(self,
|
|
|
|
|
other: object) -> bool:
|
2020-08-24 04:16:01 +05:30
|
|
|
|
"""
|
2022-07-08 21:36:41 +05:30
|
|
|
|
Return self==other.
|
|
|
|
|
|
2021-04-06 21:40:35 +05:30
|
|
|
|
Test equality of other.
|
2020-08-24 04:16:01 +05:30
|
|
|
|
|
|
|
|
|
Parameters
|
|
|
|
|
----------
|
2023-11-28 03:11:28 +05:30
|
|
|
|
other : damask.GeomGrid
|
|
|
|
|
GeomGrid to compare self against.
|
2020-08-24 04:16:01 +05:30
|
|
|
|
|
|
|
|
|
"""
|
2023-11-28 03:11:28 +05:30
|
|
|
|
if not isinstance(other, GeomGrid):
|
2022-01-13 01:04:29 +05:30
|
|
|
|
return NotImplemented
|
2022-03-10 04:54:05 +05:30
|
|
|
|
return bool( np.allclose(other.size,self.size)
|
|
|
|
|
and np.allclose(other.origin,self.origin)
|
|
|
|
|
and np.all(other.cells == self.cells)
|
|
|
|
|
and np.all(other.material == self.material))
|
2019-11-23 01:22:36 +05:30
|
|
|
|
|
2020-03-15 02:23:48 +05:30
|
|
|
|
|
2020-11-08 22:41:30 +05:30
|
|
|
|
@property
|
2021-12-06 18:52:52 +05:30
|
|
|
|
def material(self) -> np.ndarray:
|
2020-11-08 22:41:30 +05:30
|
|
|
|
"""Material indices."""
|
|
|
|
|
return self._material
|
|
|
|
|
|
|
|
|
|
@material.setter
|
2022-01-26 20:55:27 +05:30
|
|
|
|
def material(self,
|
|
|
|
|
material: np.ndarray):
|
2020-11-08 22:41:30 +05:30
|
|
|
|
if len(material.shape) != 3:
|
2022-02-22 21:12:05 +05:30
|
|
|
|
raise ValueError(f'invalid material shape {material.shape}')
|
2022-01-30 03:08:17 +05:30
|
|
|
|
if material.dtype not in np.sctypes['float'] and material.dtype not in np.sctypes['int']:
|
2022-02-22 21:12:05 +05:30
|
|
|
|
raise TypeError(f'invalid material data type "{material.dtype}"')
|
2020-11-08 22:41:30 +05:30
|
|
|
|
|
2022-01-30 03:08:17 +05:30
|
|
|
|
self._material = np.copy(material)
|
|
|
|
|
|
|
|
|
|
if self.material.dtype in np.sctypes['float'] and \
|
2022-06-02 23:10:18 +05:30
|
|
|
|
np.all(self.material == self.material.astype(np.int64).astype(float)):
|
|
|
|
|
self._material = self.material.astype(np.int64)
|
2020-11-08 22:41:30 +05:30
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@property
|
2021-12-06 18:52:52 +05:30
|
|
|
|
def size(self) -> np.ndarray:
|
2023-02-21 20:57:06 +05:30
|
|
|
|
"""Edge lengths of grid in meter."""
|
2020-11-08 22:41:30 +05:30
|
|
|
|
return self._size
|
|
|
|
|
|
|
|
|
|
@size.setter
|
2022-01-26 20:55:27 +05:30
|
|
|
|
def size(self,
|
|
|
|
|
size: FloatSequence):
|
2021-02-23 22:20:13 +05:30
|
|
|
|
if len(size) != 3 or any(np.array(size) < 0):
|
2022-02-22 21:12:05 +05:30
|
|
|
|
raise ValueError(f'invalid size {size}')
|
2022-01-30 03:08:17 +05:30
|
|
|
|
|
|
|
|
|
self._size = np.array(size)
|
2020-11-08 22:41:30 +05:30
|
|
|
|
|
|
|
|
|
@property
|
2022-01-13 01:04:29 +05:30
|
|
|
|
def origin(self) -> np.ndarray:
|
2023-02-21 20:57:06 +05:30
|
|
|
|
"""Vector to grid origin in meter."""
|
2020-11-08 22:41:30 +05:30
|
|
|
|
return self._origin
|
|
|
|
|
|
|
|
|
|
@origin.setter
|
2022-01-26 20:55:27 +05:30
|
|
|
|
def origin(self,
|
|
|
|
|
origin: FloatSequence):
|
2020-11-08 22:41:30 +05:30
|
|
|
|
if len(origin) != 3:
|
2022-02-22 21:12:05 +05:30
|
|
|
|
raise ValueError(f'invalid origin {origin}')
|
2022-01-30 03:08:17 +05:30
|
|
|
|
|
|
|
|
|
self._origin = np.array(origin)
|
2020-11-08 22:41:30 +05:30
|
|
|
|
|
2022-03-25 02:52:18 +05:30
|
|
|
|
@property
|
|
|
|
|
def initial_conditions(self) -> Dict[str,np.ndarray]:
|
|
|
|
|
"""Fields of initial conditions."""
|
2022-04-22 23:09:23 +05:30
|
|
|
|
self._ic = dict(zip(self._ic.keys(), # type: ignore
|
|
|
|
|
[v if isinstance(v,np.ndarray) else
|
|
|
|
|
np.broadcast_to(v,self.cells) for v in self._ic.values()])) # type: ignore
|
2022-03-25 02:52:18 +05:30
|
|
|
|
return self._ic
|
|
|
|
|
|
|
|
|
|
@initial_conditions.setter
|
|
|
|
|
def initial_conditions(self,
|
|
|
|
|
ic: Dict[str,np.ndarray]):
|
|
|
|
|
if not isinstance(ic,dict):
|
|
|
|
|
raise TypeError('initial conditions is not a dictionary')
|
|
|
|
|
|
|
|
|
|
self._ic = ic
|
|
|
|
|
|
2019-12-08 13:47:57 +05:30
|
|
|
|
@property
|
2021-12-06 18:52:52 +05:30
|
|
|
|
def cells(self) -> np.ndarray:
|
2023-02-21 20:57:06 +05:30
|
|
|
|
"""Cell counts along x,y,z direction."""
|
2020-09-24 02:57:15 +05:30
|
|
|
|
return np.asarray(self.material.shape)
|
2019-11-23 01:22:36 +05:30
|
|
|
|
|
2020-03-15 02:23:48 +05:30
|
|
|
|
|
2020-03-18 18:19:53 +05:30
|
|
|
|
@property
|
2021-12-06 18:52:52 +05:30
|
|
|
|
def N_materials(self) -> int:
|
2020-12-04 11:42:18 +05:30
|
|
|
|
"""Number of (unique) material indices within grid."""
|
2020-09-24 02:57:15 +05:30
|
|
|
|
return np.unique(self.material).size
|
2019-11-23 01:22:36 +05:30
|
|
|
|
|
2020-03-15 02:23:48 +05:30
|
|
|
|
|
2020-10-01 02:57:49 +05:30
|
|
|
|
@staticmethod
|
2023-11-28 19:05:30 +05:30
|
|
|
|
def _load(fname: Union[str, Path], label: str) -> 'GeomGrid':
|
2020-10-01 02:57:49 +05:30
|
|
|
|
"""
|
2023-02-21 20:57:06 +05:30
|
|
|
|
Load from VTK ImageData file.
|
2020-10-01 02:57:49 +05:30
|
|
|
|
|
|
|
|
|
Parameters
|
|
|
|
|
----------
|
2022-01-13 01:04:29 +05:30
|
|
|
|
fname : str or pathlib.Path
|
2023-11-28 12:46:47 +05:30
|
|
|
|
VTK ImageData file to read.
|
2022-02-14 19:49:09 +05:30
|
|
|
|
Valid extension is .vti, which will be appended if not given.
|
2023-11-28 12:46:47 +05:30
|
|
|
|
label : str
|
|
|
|
|
Label of the dataset containing the material IDs.
|
2020-10-01 02:57:49 +05:30
|
|
|
|
|
2021-04-23 22:45:11 +05:30
|
|
|
|
Returns
|
|
|
|
|
-------
|
2023-11-28 03:11:28 +05:30
|
|
|
|
loaded : damask.GeomGrid
|
|
|
|
|
GeomGrid-based geometry from file.
|
2021-04-23 22:45:11 +05:30
|
|
|
|
|
2020-10-01 02:57:49 +05:30
|
|
|
|
"""
|
2022-02-11 03:43:37 +05:30
|
|
|
|
v = VTK.load(fname if str(fname).endswith('.vti') else str(fname)+'.vti')
|
2020-12-04 02:28:24 +05:30
|
|
|
|
cells = np.array(v.vtk_data.GetDimensions())-1
|
|
|
|
|
bbox = np.array(v.vtk_data.GetBounds()).reshape(3,2).T
|
2023-11-28 19:05:30 +05:30
|
|
|
|
ic = {l:v.get(l).reshape(cells,order='F') for l in set(v.labels['Cell Data']) - {label}}
|
2020-10-01 02:57:49 +05:30
|
|
|
|
|
2023-11-28 12:46:47 +05:30
|
|
|
|
return GeomGrid(material = v.get(label).reshape(cells,order='F'),
|
2023-11-28 03:11:28 +05:30
|
|
|
|
size = bbox[1] - bbox[0],
|
|
|
|
|
origin = bbox[0],
|
|
|
|
|
initial_conditions = ic,
|
|
|
|
|
comments = v.comments,
|
|
|
|
|
)
|
2020-10-01 02:57:49 +05:30
|
|
|
|
|
2023-11-28 12:46:47 +05:30
|
|
|
|
@staticmethod
|
|
|
|
|
def load(fname: Union[str, Path]) -> 'GeomGrid':
|
|
|
|
|
"""
|
|
|
|
|
Load from VTK ImageData file with material IDs stored as 'material'.
|
|
|
|
|
|
|
|
|
|
Parameters
|
|
|
|
|
----------
|
|
|
|
|
fname : str or pathlib.Path
|
|
|
|
|
GeomGrid file to read.
|
|
|
|
|
Valid extension is .vti, which will be appended if not given.
|
|
|
|
|
|
|
|
|
|
Returns
|
|
|
|
|
-------
|
|
|
|
|
loaded : damask.GeomGrid
|
|
|
|
|
GeomGrid-based geometry from file.
|
|
|
|
|
|
|
|
|
|
"""
|
|
|
|
|
return GeomGrid._load(fname,'material')
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
|
def load_SPPARKS(fname: Union[str, Path]) -> 'GeomGrid':
|
|
|
|
|
"""
|
2023-11-28 22:51:06 +05:30
|
|
|
|
Load from SPPARKS VTK dump.
|
2023-11-28 12:46:47 +05:30
|
|
|
|
|
|
|
|
|
Parameters
|
|
|
|
|
----------
|
|
|
|
|
fname : str or pathlib.Path
|
|
|
|
|
SPPARKS VTK dump file to read.
|
|
|
|
|
Valid extension is .vti, which will be appended if not given.
|
|
|
|
|
|
|
|
|
|
Returns
|
|
|
|
|
-------
|
|
|
|
|
loaded : damask.GeomGrid
|
|
|
|
|
GeomGrid-based geometry from file.
|
|
|
|
|
|
|
|
|
|
Notes
|
|
|
|
|
-----
|
2023-11-28 22:51:06 +05:30
|
|
|
|
A SPPARKS VTI dump is equivalent to a DAMASK VTI file,
|
2023-11-29 01:38:53 +05:30
|
|
|
|
but stores the materialID information as 'Spin' rather than 'material'.
|
2023-11-28 12:46:47 +05:30
|
|
|
|
|
|
|
|
|
"""
|
2023-11-29 01:38:53 +05:30
|
|
|
|
return GeomGrid._load(fname,'Spin')
|
2023-11-28 12:46:47 +05:30
|
|
|
|
|
2020-10-01 02:57:49 +05:30
|
|
|
|
|
2022-03-25 02:52:18 +05:30
|
|
|
|
@typing.no_type_check
|
2019-11-27 01:02:54 +05:30
|
|
|
|
@staticmethod
|
2023-11-28 03:11:28 +05:30
|
|
|
|
def load_ASCII(fname)-> 'GeomGrid':
|
2019-11-23 01:22:36 +05:30
|
|
|
|
"""
|
2020-12-04 02:28:24 +05:30
|
|
|
|
Load from geom file.
|
2019-11-23 01:22:36 +05:30
|
|
|
|
|
2020-11-14 22:24:47 +05:30
|
|
|
|
Storing geometry files in ASCII format is deprecated.
|
|
|
|
|
This function will be removed in a future version of DAMASK.
|
|
|
|
|
|
2019-11-23 01:22:36 +05:30
|
|
|
|
Parameters
|
|
|
|
|
----------
|
2020-11-08 22:41:30 +05:30
|
|
|
|
fname : str, pathlib.Path, or file handle
|
2020-08-08 23:12:34 +05:30
|
|
|
|
Geometry file to read.
|
2019-11-23 01:22:36 +05:30
|
|
|
|
|
2021-04-23 22:45:11 +05:30
|
|
|
|
Returns
|
|
|
|
|
-------
|
2023-11-28 03:11:28 +05:30
|
|
|
|
loaded : damask.GeomGrid
|
|
|
|
|
GeomGrid-based geometry from file.
|
2021-04-23 22:45:11 +05:30
|
|
|
|
|
2019-11-23 01:22:36 +05:30
|
|
|
|
"""
|
2022-01-05 18:36:06 +05:30
|
|
|
|
warnings.warn('Support for ASCII-based geom format will be removed in DAMASK 3.0.0', DeprecationWarning,2)
|
2021-12-06 18:52:52 +05:30
|
|
|
|
if isinstance(fname, (str, Path)):
|
2019-11-25 18:17:14 +05:30
|
|
|
|
f = open(fname)
|
2021-12-06 18:52:52 +05:30
|
|
|
|
elif isinstance(fname, TextIO):
|
2019-11-25 18:17:14 +05:30
|
|
|
|
f = fname
|
2021-12-06 18:52:52 +05:30
|
|
|
|
else:
|
|
|
|
|
raise TypeError
|
2019-11-25 18:17:14 +05:30
|
|
|
|
|
|
|
|
|
f.seek(0)
|
2020-08-27 03:24:56 +05:30
|
|
|
|
try:
|
2021-12-06 18:52:52 +05:30
|
|
|
|
header_length_,keyword = f.readline().split()[:2]
|
|
|
|
|
header_length = int(header_length_)
|
2020-08-27 03:24:56 +05:30
|
|
|
|
except ValueError:
|
|
|
|
|
header_length,keyword = (-1, 'invalid')
|
2019-11-23 01:22:36 +05:30
|
|
|
|
if not keyword.startswith('head') or header_length < 3:
|
2022-02-22 21:12:05 +05:30
|
|
|
|
raise TypeError('invalid or missing header length information')
|
2019-11-23 01:22:36 +05:30
|
|
|
|
|
2020-03-21 15:37:21 +05:30
|
|
|
|
comments = []
|
2020-11-20 00:56:15 +05:30
|
|
|
|
content = f.readlines()
|
2019-11-23 01:22:36 +05:30
|
|
|
|
for i,line in enumerate(content[:header_length]):
|
2022-01-13 01:04:29 +05:30
|
|
|
|
items = line.split('#')[0].lower().strip().split()
|
2022-01-29 22:46:19 +05:30
|
|
|
|
if (key := items[0] if items else '') == 'grid':
|
2022-01-13 01:04:29 +05:30
|
|
|
|
cells = np.array([ int(dict(zip(items[1::2],items[2::2]))[i]) for i in ['a','b','c']])
|
2019-11-23 01:22:36 +05:30
|
|
|
|
elif key == 'size':
|
|
|
|
|
size = np.array([float(dict(zip(items[1::2],items[2::2]))[i]) for i in ['x','y','z']])
|
|
|
|
|
elif key == 'origin':
|
|
|
|
|
origin = np.array([float(dict(zip(items[1::2],items[2::2]))[i]) for i in ['x','y','z']])
|
|
|
|
|
else:
|
|
|
|
|
comments.append(line.strip())
|
|
|
|
|
|
2022-03-10 04:54:05 +05:30
|
|
|
|
material = np.empty(cells.prod()) # initialize as flat array
|
2019-11-23 01:22:36 +05:30
|
|
|
|
i = 0
|
|
|
|
|
for line in content[header_length:]:
|
2022-01-29 22:46:19 +05:30
|
|
|
|
if len(items := line.split('#')[0].split()) == 3:
|
2021-12-06 18:52:52 +05:30
|
|
|
|
if items[1].lower() == 'of':
|
|
|
|
|
material_entry = np.ones(int(items[0]))*float(items[2])
|
2019-11-23 01:22:36 +05:30
|
|
|
|
elif items[1].lower() == 'to':
|
2021-12-06 18:52:52 +05:30
|
|
|
|
material_entry = np.linspace(int(items[0]),int(items[2]),
|
2019-11-23 01:22:36 +05:30
|
|
|
|
abs(int(items[2])-int(items[0]))+1,dtype=float)
|
2021-12-06 18:52:52 +05:30
|
|
|
|
else: material_entry = list(map(float, items))
|
|
|
|
|
else: material_entry = list(map(float, items))
|
|
|
|
|
material[i:i+len(material_entry)] = material_entry
|
2019-11-23 01:22:36 +05:30
|
|
|
|
i += len(items)
|
2020-03-21 15:37:21 +05:30
|
|
|
|
|
2020-12-04 02:28:24 +05:30
|
|
|
|
if i != cells.prod():
|
2022-02-22 21:12:05 +05:30
|
|
|
|
raise TypeError(f'mismatch between {cells.prod()} expected entries and {i} found')
|
2020-03-21 15:37:21 +05:30
|
|
|
|
|
2020-11-19 14:10:19 +05:30
|
|
|
|
if not np.any(np.mod(material,1) != 0.0): # no float present
|
2022-06-02 23:10:18 +05:30
|
|
|
|
material = material.astype(np.int64) - (1 if material.min() > 0 else 0)
|
2020-03-21 15:37:21 +05:30
|
|
|
|
|
2023-11-28 03:11:28 +05:30
|
|
|
|
return GeomGrid(material = material.reshape(cells,order='F'),
|
|
|
|
|
size = size,
|
|
|
|
|
origin = origin,
|
|
|
|
|
comments = comments,
|
|
|
|
|
)
|
2019-11-23 01:22:36 +05:30
|
|
|
|
|
|
|
|
|
|
2021-06-16 00:53:10 +05:30
|
|
|
|
@staticmethod
|
2023-11-28 03:11:28 +05:30
|
|
|
|
def load_Neper(fname: Union[str, Path]) -> 'GeomGrid':
|
2021-06-16 00:53:10 +05:30
|
|
|
|
"""
|
|
|
|
|
Load from Neper VTK file.
|
|
|
|
|
|
|
|
|
|
Parameters
|
|
|
|
|
----------
|
2022-01-13 01:04:29 +05:30
|
|
|
|
fname : str or pathlib.Path
|
2021-06-16 00:53:10 +05:30
|
|
|
|
Geometry file to read.
|
|
|
|
|
|
|
|
|
|
Returns
|
|
|
|
|
-------
|
2023-11-28 03:11:28 +05:30
|
|
|
|
loaded : damask.GeomGrid
|
|
|
|
|
GeomGrid-based geometry from file.
|
2021-06-16 00:53:10 +05:30
|
|
|
|
|
2023-05-22 21:02:39 +05:30
|
|
|
|
Notes
|
|
|
|
|
-----
|
|
|
|
|
Material indices in Neper usually start at 1 unless
|
|
|
|
|
a buffer material with index 0 is added.
|
|
|
|
|
|
2022-03-04 21:47:03 +05:30
|
|
|
|
Examples
|
|
|
|
|
--------
|
|
|
|
|
Read a periodic polycrystal generated with Neper.
|
|
|
|
|
|
|
|
|
|
>>> import damask
|
|
|
|
|
>>> N_grains = 20
|
|
|
|
|
>>> cells = (32,32,32)
|
2022-04-22 23:57:03 +05:30
|
|
|
|
>>> damask.util.run(f'neper -T -n {N_grains} -tesrsize {cells[0]}:{cells[1]}:{cells[2]} -periodicity all -format vtk')
|
2023-11-28 03:11:28 +05:30
|
|
|
|
>>> damask.GeomGrid.load_Neper(f'n{N_grains}-id1.vtk').renumber()
|
2022-03-04 21:47:03 +05:30
|
|
|
|
cells: 32 × 32 × 32
|
|
|
|
|
size: 1.0 × 1.0 × 1.0 m³
|
|
|
|
|
origin: 0.0 0.0 0.0 m
|
|
|
|
|
# materials: 20
|
|
|
|
|
|
2021-06-16 00:53:10 +05:30
|
|
|
|
"""
|
2022-01-22 12:35:49 +05:30
|
|
|
|
v = VTK.load(fname,'ImageData')
|
2021-06-16 00:53:10 +05:30
|
|
|
|
cells = np.array(v.vtk_data.GetDimensions())-1
|
|
|
|
|
bbox = np.array(v.vtk_data.GetBounds()).reshape(3,2).T
|
|
|
|
|
|
2023-11-28 03:11:28 +05:30
|
|
|
|
return GeomGrid(material = v.get('MaterialId').reshape(cells,order='F').astype('int32',casting='unsafe'),
|
|
|
|
|
size = bbox[1] - bbox[0],
|
|
|
|
|
origin = bbox[0],
|
|
|
|
|
comments = util.execution_stamp('GeomGrid','load_Neper'),
|
|
|
|
|
)
|
2021-06-16 00:53:10 +05:30
|
|
|
|
|
|
|
|
|
|
2020-10-09 17:49:19 +05:30
|
|
|
|
@staticmethod
|
2022-01-13 01:04:29 +05:30
|
|
|
|
def load_DREAM3D(fname: Union[str, Path],
|
2022-11-23 02:56:15 +05:30
|
|
|
|
feature_IDs: Optional[str] = None,
|
|
|
|
|
cell_data: Optional[str] = None,
|
|
|
|
|
phases: str = 'Phases',
|
|
|
|
|
Euler_angles: str = 'EulerAngles',
|
2023-11-28 03:11:28 +05:30
|
|
|
|
base_group: Optional[str] = None) -> 'GeomGrid':
|
2020-10-09 17:49:19 +05:30
|
|
|
|
"""
|
2021-03-20 18:07:06 +05:30
|
|
|
|
Load DREAM.3D (HDF5) file.
|
|
|
|
|
|
2023-09-22 17:41:25 +05:30
|
|
|
|
Data in DREAM.3D files can be stored per cell ('CellData')
|
|
|
|
|
and/or per grain ('Grain Data'). Per default, i.e. if
|
|
|
|
|
'feature_IDs' is None, cell-wise data is assumed.
|
2021-03-20 18:07:06 +05:30
|
|
|
|
|
2020-10-09 17:49:19 +05:30
|
|
|
|
|
|
|
|
|
Parameters
|
|
|
|
|
----------
|
2023-09-22 18:45:52 +05:30
|
|
|
|
fname : str or pathlib.Path
|
2021-03-20 17:21:41 +05:30
|
|
|
|
Filename of the DREAM.3D (HDF5) file.
|
2022-01-13 01:04:29 +05:30
|
|
|
|
feature_IDs : str, optional
|
2021-03-20 18:07:06 +05:30
|
|
|
|
Name of the dataset containing the mapping between cells and
|
|
|
|
|
grain-wise data. Defaults to 'None', in which case cell-wise
|
|
|
|
|
data is used.
|
2022-01-13 01:04:29 +05:30
|
|
|
|
cell_data : str, optional
|
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.
|
2022-01-13 01:04:29 +05:30
|
|
|
|
phases : str, optional
|
2021-03-20 18:07:06 +05:30
|
|
|
|
Name of the dataset containing the phase ID. It is not used for
|
|
|
|
|
grain-wise data, i.e. when feature_IDs is not None.
|
|
|
|
|
Defaults to 'Phases'.
|
2022-01-13 01:04:29 +05:30
|
|
|
|
Euler_angles : str, optional
|
2021-03-20 18:07:06 +05:30
|
|
|
|
Name of the dataset containing the crystallographic orientation as
|
|
|
|
|
Euler angles in radians It is not used for grain-wise data, i.e.
|
|
|
|
|
when feature_IDs is not None. Defaults to 'EulerAngles'.
|
2022-01-13 01:04:29 +05:30
|
|
|
|
base_group : str, optional
|
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 04:19:41 +05:30
|
|
|
|
it is set as the path that contains _SIMPL_GEOMETRY/SPACING.
|
2021-03-20 17:21:41 +05:30
|
|
|
|
|
2021-04-23 22:45:11 +05:30
|
|
|
|
Returns
|
|
|
|
|
-------
|
2023-11-28 03:11:28 +05:30
|
|
|
|
loaded : damask.GeomGrid
|
|
|
|
|
GeomGrid-based geometry from file.
|
2021-03-20 18:07:06 +05:30
|
|
|
|
|
2023-09-22 17:41:25 +05:30
|
|
|
|
Notes
|
|
|
|
|
-----
|
|
|
|
|
damask.ConfigMaterial.load_DREAM3D gives the corresponding
|
|
|
|
|
material definition.
|
|
|
|
|
|
|
|
|
|
For cell-wise data, only unique combinations of
|
|
|
|
|
orientation and phase are considered.
|
|
|
|
|
|
2020-10-09 17:49:19 +05:30
|
|
|
|
"""
|
2023-09-22 18:45:52 +05:30
|
|
|
|
with h5py.File(fname, 'r') as f:
|
|
|
|
|
b = util.DREAM3D_base_group(f) if base_group is None else base_group
|
|
|
|
|
c = util.DREAM3D_cell_data_group(f) if cell_data is None else cell_data
|
|
|
|
|
|
|
|
|
|
cells = f['/'.join([b,'_SIMPL_GEOMETRY','DIMENSIONS'])][()]
|
|
|
|
|
size = f['/'.join([b,'_SIMPL_GEOMETRY','SPACING'])] * cells
|
|
|
|
|
origin = f['/'.join([b,'_SIMPL_GEOMETRY','ORIGIN'])][()]
|
|
|
|
|
|
|
|
|
|
if feature_IDs is None:
|
|
|
|
|
phase = f['/'.join([b,c,phases])][()].reshape(-1,1)
|
|
|
|
|
O = Rotation.from_Euler_angles(f['/'.join([b,c,Euler_angles])]).as_quaternion().reshape(-1,4) # noqa
|
|
|
|
|
unique,unique_inverse = np.unique(np.hstack([O,phase]),return_inverse=True,axis=0)
|
|
|
|
|
ma = np.arange(cells.prod()) if len(unique) == cells.prod() else \
|
|
|
|
|
np.arange(unique.size)[np.argsort(pd.unique(unique_inverse))][unique_inverse]
|
|
|
|
|
else:
|
|
|
|
|
ma = f['/'.join([b,c,feature_IDs])][()].flatten()
|
2020-10-09 17:49:19 +05:30
|
|
|
|
|
2023-11-28 03:11:28 +05:30
|
|
|
|
return GeomGrid(material = ma.reshape(cells,order='F'),
|
|
|
|
|
size = size,
|
|
|
|
|
origin = origin,
|
|
|
|
|
comments = util.execution_stamp('GeomGrid','load_DREAM3D'),
|
|
|
|
|
)
|
2020-10-09 17:49:19 +05:30
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@staticmethod
|
2022-01-13 01:04:29 +05:30
|
|
|
|
def from_table(table: Table,
|
|
|
|
|
coordinates: str,
|
2023-11-28 03:11:28 +05:30
|
|
|
|
labels: Union[str, Sequence[str]]) -> 'GeomGrid':
|
2020-10-09 17:49:19 +05:30
|
|
|
|
"""
|
2021-06-15 20:32:02 +05:30
|
|
|
|
Create grid from ASCII table.
|
2020-10-09 17:49:19 +05:30
|
|
|
|
|
|
|
|
|
Parameters
|
|
|
|
|
----------
|
2020-10-09 22:49:05 +05:30
|
|
|
|
table : damask.Table
|
|
|
|
|
Table that contains material information.
|
2020-10-09 17:49:19 +05:30
|
|
|
|
coordinates : str
|
2020-11-08 22:41:30 +05:30
|
|
|
|
Label of the vector column containing the spatial coordinates.
|
2020-10-29 11:47:41 +05:30
|
|
|
|
Need to be ordered (1./x fast, 3./z slow).
|
2022-11-14 19:39:45 +05:30
|
|
|
|
labels : (sequence of) str
|
2020-10-09 17:49:19 +05:30
|
|
|
|
Label(s) of the columns containing the material definition.
|
2021-02-23 22:20:13 +05:30
|
|
|
|
Each unique combination of values results in one material ID.
|
2020-10-09 17:49:19 +05:30
|
|
|
|
|
2021-04-23 22:45:11 +05:30
|
|
|
|
Returns
|
|
|
|
|
-------
|
2023-11-28 03:11:28 +05:30
|
|
|
|
new : damask.GeomGrid
|
|
|
|
|
GeomGrid-based geometry from values in table.
|
2021-04-23 22:45:11 +05:30
|
|
|
|
|
2020-10-09 17:49:19 +05:30
|
|
|
|
"""
|
2020-12-07 22:19:37 +05:30
|
|
|
|
cells,size,origin = grid_filters.cellsSizeOrigin_coordinates0_point(table.get(coordinates))
|
2020-10-09 17:49:19 +05:30
|
|
|
|
|
|
|
|
|
labels_ = [labels] if isinstance(labels,str) else labels
|
2020-10-28 16:11:34 +05:30
|
|
|
|
unique,unique_inverse = np.unique(np.hstack([table.get(l) for l in labels_]),return_inverse=True,axis=0)
|
2020-10-31 21:53:58 +05:30
|
|
|
|
|
2020-12-04 02:28:24 +05:30
|
|
|
|
ma = np.arange(cells.prod()) if len(unique) == cells.prod() else \
|
2020-10-31 21:53:58 +05:30
|
|
|
|
np.arange(unique.size)[np.argsort(pd.unique(unique_inverse))][unique_inverse]
|
2020-10-09 17:49:19 +05:30
|
|
|
|
|
2023-11-28 03:11:28 +05:30
|
|
|
|
return GeomGrid(material = ma.reshape(cells,order='F'),
|
|
|
|
|
size = size,
|
|
|
|
|
origin = origin,
|
|
|
|
|
comments = util.execution_stamp('GeomGrid','from_table'),
|
|
|
|
|
)
|
2020-10-09 17:49:19 +05:30
|
|
|
|
|
|
|
|
|
|
2020-03-29 22:42:23 +05:30
|
|
|
|
@staticmethod
|
2022-01-26 20:55:27 +05:30
|
|
|
|
def _find_closest_seed(seeds: np.ndarray,
|
|
|
|
|
weights: np.ndarray,
|
|
|
|
|
point: np.ndarray) -> np.integer:
|
2020-03-29 22:42:23 +05:30
|
|
|
|
return np.argmin(np.sum((np.broadcast_to(point,(len(seeds),3))-seeds)**2,axis=1) - weights)
|
2020-06-24 21:35:12 +05:30
|
|
|
|
|
2020-03-29 22:42:23 +05:30
|
|
|
|
@staticmethod
|
2022-01-13 01:04:29 +05:30
|
|
|
|
def from_Laguerre_tessellation(cells: IntSequence,
|
|
|
|
|
size: FloatSequence,
|
|
|
|
|
seeds: np.ndarray,
|
|
|
|
|
weights: FloatSequence,
|
2022-11-23 02:56:15 +05:30
|
|
|
|
material: Optional[IntSequence] = None,
|
2022-01-13 01:04:29 +05:30
|
|
|
|
periodic: bool = True):
|
2020-03-29 22:42:23 +05:30
|
|
|
|
"""
|
2021-06-15 20:32:02 +05:30
|
|
|
|
Create grid from Laguerre tessellation.
|
2020-03-29 22:42:23 +05:30
|
|
|
|
|
|
|
|
|
Parameters
|
|
|
|
|
----------
|
2022-01-13 01:04:29 +05:30
|
|
|
|
cells : sequence of int, len (3)
|
2023-02-21 20:57:06 +05:30
|
|
|
|
Cell counts along x,y,z direction.
|
2022-01-13 01:04:29 +05:30
|
|
|
|
size : sequence of float, len (3)
|
2023-02-21 20:57:06 +05:30
|
|
|
|
Edge lengths of the grid in meter.
|
2022-11-09 00:22:08 +05:30
|
|
|
|
seeds : numpy.ndarray of float, shape (:,3)
|
2023-10-13 02:51:40 +05:30
|
|
|
|
Position of the seed points in meter. All points need
|
|
|
|
|
to lay within the box [(0,0,0),size].
|
2022-01-13 01:04:29 +05:30
|
|
|
|
weights : sequence of float, len (seeds.shape[0])
|
2023-10-13 02:51:40 +05:30
|
|
|
|
Weights of the seeds. Setting all weights to 1.0 gives a
|
|
|
|
|
standard Voronoi tessellation.
|
2022-01-13 01:04:29 +05:30
|
|
|
|
material : sequence of int, len (seeds.shape[0]), optional
|
2020-11-08 22:41:30 +05:30
|
|
|
|
Material ID of the seeds.
|
|
|
|
|
Defaults to None, in which case materials are consecutively numbered.
|
2022-01-13 03:43:38 +05:30
|
|
|
|
periodic : bool, optional
|
2020-12-04 11:42:18 +05:30
|
|
|
|
Assume grid to be periodic. Defaults to True.
|
2020-03-29 22:42:23 +05:30
|
|
|
|
|
2021-04-23 22:45:11 +05:30
|
|
|
|
Returns
|
|
|
|
|
-------
|
2023-11-28 03:11:28 +05:30
|
|
|
|
new : damask.GeomGrid
|
|
|
|
|
GeomGrid-based geometry from tessellation.
|
2021-04-23 22:45:11 +05:30
|
|
|
|
|
2020-03-29 22:42:23 +05:30
|
|
|
|
"""
|
2022-01-14 22:26:58 +05:30
|
|
|
|
weights_p: FloatSequence
|
2020-03-29 22:42:23 +05:30
|
|
|
|
if periodic:
|
2020-04-20 23:46:25 +05:30
|
|
|
|
weights_p = np.tile(weights,27) # Laguerre weights (1,2,3,1,2,3,...,1,2,3)
|
2020-03-29 22:42:23 +05:30
|
|
|
|
seeds_p = np.vstack((seeds -np.array([size[0],0.,0.]),seeds, seeds +np.array([size[0],0.,0.])))
|
|
|
|
|
seeds_p = np.vstack((seeds_p-np.array([0.,size[1],0.]),seeds_p,seeds_p+np.array([0.,size[1],0.])))
|
|
|
|
|
seeds_p = np.vstack((seeds_p-np.array([0.,0.,size[2]]),seeds_p,seeds_p+np.array([0.,0.,size[2]])))
|
|
|
|
|
else:
|
2022-01-20 17:22:56 +05:30
|
|
|
|
weights_p = np.array(weights,float)
|
2020-03-29 22:42:23 +05:30
|
|
|
|
seeds_p = seeds
|
2021-07-09 15:43:18 +05:30
|
|
|
|
|
|
|
|
|
coords = grid_filters.coordinates0_point(cells,size).reshape(-1,3)
|
2022-01-13 01:04:29 +05:30
|
|
|
|
|
2021-04-28 23:56:25 +05:30
|
|
|
|
pool = mp.Pool(int(os.environ.get('OMP_NUM_THREADS',4)))
|
2023-11-28 03:11:28 +05:30
|
|
|
|
result = pool.map_async(partial(GeomGrid._find_closest_seed,seeds_p,weights_p), coords)
|
2020-03-29 22:42:23 +05:30
|
|
|
|
pool.close()
|
|
|
|
|
pool.join()
|
2021-07-09 15:43:18 +05:30
|
|
|
|
material_ = np.array(result.get()).reshape(cells)
|
2020-03-29 22:42:23 +05:30
|
|
|
|
|
2021-07-09 15:43:18 +05:30
|
|
|
|
if periodic: material_ %= len(weights)
|
2020-03-29 22:42:23 +05:30
|
|
|
|
|
2023-11-28 03:11:28 +05:30
|
|
|
|
return GeomGrid(material = material_ if material is None else np.array(material)[material_],
|
|
|
|
|
size = size,
|
|
|
|
|
comments = util.execution_stamp('GeomGrid','from_Laguerre_tessellation'),
|
|
|
|
|
)
|
2020-03-29 22:42:23 +05:30
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@staticmethod
|
2022-01-13 01:04:29 +05:30
|
|
|
|
def from_Voronoi_tessellation(cells: IntSequence,
|
|
|
|
|
size: FloatSequence,
|
2021-12-06 18:52:52 +05:30
|
|
|
|
seeds: np.ndarray,
|
2022-11-23 02:56:15 +05:30
|
|
|
|
material: Optional[IntSequence] = None,
|
2023-11-28 03:11:28 +05:30
|
|
|
|
periodic: bool = True) -> 'GeomGrid':
|
2020-03-29 22:42:23 +05:30
|
|
|
|
"""
|
2021-06-15 20:32:02 +05:30
|
|
|
|
Create grid from Voronoi tessellation.
|
2020-03-29 22:42:23 +05:30
|
|
|
|
|
|
|
|
|
Parameters
|
|
|
|
|
----------
|
2022-01-13 01:04:29 +05:30
|
|
|
|
cells : sequence of int, len (3)
|
2023-02-21 20:57:06 +05:30
|
|
|
|
Cell counts along x,y,z direction.
|
2022-01-13 01:04:29 +05:30
|
|
|
|
size : sequence of float, len (3)
|
2023-02-21 20:57:06 +05:30
|
|
|
|
Edge lengths of the grid in meter.
|
2022-11-09 00:22:08 +05:30
|
|
|
|
seeds : numpy.ndarray of float, shape (:,3)
|
2023-10-13 02:51:40 +05:30
|
|
|
|
Position of the seed points in meter. All points need
|
|
|
|
|
to lay within the box [(0,0,0),size].
|
2022-01-13 01:04:29 +05:30
|
|
|
|
material : sequence of int, len (seeds.shape[0]), optional
|
2020-11-08 22:41:30 +05:30
|
|
|
|
Material ID of the seeds.
|
|
|
|
|
Defaults to None, in which case materials are consecutively numbered.
|
2022-01-13 03:43:38 +05:30
|
|
|
|
periodic : bool, optional
|
2020-12-04 11:42:18 +05:30
|
|
|
|
Assume grid to be periodic. Defaults to True.
|
2020-03-29 22:42:23 +05:30
|
|
|
|
|
2021-04-23 22:45:11 +05:30
|
|
|
|
Returns
|
|
|
|
|
-------
|
2023-11-28 03:11:28 +05:30
|
|
|
|
new : damask.GeomGrid
|
|
|
|
|
GeomGrid-based geometry from tessellation.
|
2021-04-23 22:45:11 +05:30
|
|
|
|
|
2020-03-29 22:42:23 +05:30
|
|
|
|
"""
|
2020-12-04 03:30:49 +05:30
|
|
|
|
coords = grid_filters.coordinates0_point(cells,size).reshape(-1,3)
|
2021-04-29 12:26:40 +05:30
|
|
|
|
tree = spatial.cKDTree(seeds,boxsize=size) if periodic else \
|
|
|
|
|
spatial.cKDTree(seeds)
|
|
|
|
|
try:
|
|
|
|
|
material_ = tree.query(coords, workers = int(os.environ.get('OMP_NUM_THREADS',4)))[1]
|
|
|
|
|
except TypeError:
|
|
|
|
|
material_ = tree.query(coords, n_jobs = int(os.environ.get('OMP_NUM_THREADS',4)))[1] # scipy <1.6
|
2020-03-29 22:42:23 +05:30
|
|
|
|
|
2023-11-28 03:11:28 +05:30
|
|
|
|
return GeomGrid(material = (material_ if material is None else np.array(material)[material_]).reshape(cells),
|
|
|
|
|
size = size,
|
|
|
|
|
comments = util.execution_stamp('GeomGrid','from_Voronoi_tessellation'),
|
|
|
|
|
)
|
2020-03-29 22:42:23 +05:30
|
|
|
|
|
|
|
|
|
|
2020-10-01 02:57:49 +05:30
|
|
|
|
_minimal_surface = \
|
|
|
|
|
{'Schwarz P': lambda x,y,z: np.cos(x) + np.cos(y) + np.cos(z),
|
|
|
|
|
'Double Primitive': lambda x,y,z: ( 0.5 * (np.cos(x)*np.cos(y) + np.cos(y)*np.cos(z) + np.cos(z)*np.cos(x))
|
|
|
|
|
+ 0.2 * (np.cos(2*x) + np.cos(2*y) + np.cos(2*z)) ),
|
|
|
|
|
'Schwarz D': lambda x,y,z: ( np.sin(x)*np.sin(y)*np.sin(z)
|
|
|
|
|
+ np.sin(x)*np.cos(y)*np.cos(z)
|
|
|
|
|
+ np.cos(x)*np.cos(y)*np.sin(z)
|
|
|
|
|
+ np.cos(x)*np.sin(y)*np.cos(z) ),
|
|
|
|
|
'Complementary D': lambda x,y,z: ( np.cos(3*x+y)*np.cos(z) - np.sin(3*x-y)*np.sin(z) + np.cos(x+3*y)*np.cos(z)
|
|
|
|
|
+ np.sin(x-3*y)*np.sin(z) + np.cos(x-y)*np.cos(3*z) - np.sin(x+y)*np.sin(3*z) ),
|
|
|
|
|
'Double Diamond': lambda x,y,z: 0.5 * (np.sin(x)*np.sin(y)
|
|
|
|
|
+ np.sin(y)*np.sin(z)
|
|
|
|
|
+ np.sin(z)*np.sin(x)
|
|
|
|
|
+ np.cos(x) * np.cos(y) * np.cos(z) ),
|
|
|
|
|
'Dprime': lambda x,y,z: 0.5 * ( np.cos(x)*np.cos(y)*np.cos(z)
|
|
|
|
|
+ np.cos(x)*np.sin(y)*np.sin(z)
|
|
|
|
|
+ np.sin(x)*np.cos(y)*np.sin(z)
|
|
|
|
|
+ np.sin(x)*np.sin(y)*np.cos(z)
|
|
|
|
|
- np.sin(2*x)*np.sin(2*y)
|
|
|
|
|
- np.sin(2*y)*np.sin(2*z)
|
|
|
|
|
- np.sin(2*z)*np.sin(2*x) ) - 0.2,
|
|
|
|
|
'Gyroid': lambda x,y,z: np.cos(x)*np.sin(y) + np.cos(y)*np.sin(z) + np.cos(z)*np.sin(x),
|
|
|
|
|
'Gprime': lambda x,y,z : ( np.sin(2*x)*np.cos(y)*np.sin(z)
|
|
|
|
|
+ np.sin(2*y)*np.cos(z)*np.sin(x)
|
|
|
|
|
+ np.sin(2*z)*np.cos(x)*np.sin(y) ) + 0.32,
|
|
|
|
|
'Karcher K': lambda x,y,z: ( 0.3 * ( np.cos(x) + np.cos(y) + np.cos(z)
|
|
|
|
|
+ np.cos(x)*np.cos(y) + np.cos(y)*np.cos(z) + np.cos(z)*np.cos(x) )
|
|
|
|
|
- 0.4 * ( np.cos(2*x) + np.cos(2*y) + np.cos(2*z) ) ) + 0.2,
|
|
|
|
|
'Lidinoid': lambda x,y,z: 0.5 * ( np.sin(2*x)*np.cos(y)*np.sin(z)
|
|
|
|
|
+ np.sin(2*y)*np.cos(z)*np.sin(x)
|
|
|
|
|
+ np.sin(2*z)*np.cos(x)*np.sin(y)
|
|
|
|
|
- np.cos(2*x)*np.cos(2*y)
|
|
|
|
|
- np.cos(2*y)*np.cos(2*z)
|
|
|
|
|
- np.cos(2*z)*np.cos(2*x) ) + 0.15,
|
|
|
|
|
'Neovius': lambda x,y,z: ( 3 * (np.cos(x)+np.cos(y)+np.cos(z))
|
|
|
|
|
+ 4 * np.cos(x)*np.cos(y)*np.cos(z) ),
|
|
|
|
|
'Fisher-Koch S': lambda x,y,z: ( np.cos(2*x)*np.sin( y)*np.cos( z)
|
|
|
|
|
+ np.cos( x)*np.cos(2*y)*np.sin( z)
|
|
|
|
|
+ np.sin( x)*np.cos( y)*np.cos(2*z) ),
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2020-09-30 10:40:15 +05:30
|
|
|
|
@staticmethod
|
2022-01-13 01:04:29 +05:30
|
|
|
|
def from_minimal_surface(cells: IntSequence,
|
|
|
|
|
size: FloatSequence,
|
2021-12-06 18:52:52 +05:30
|
|
|
|
surface: str,
|
|
|
|
|
threshold: float = 0.0,
|
|
|
|
|
periods: int = 1,
|
2023-11-28 03:11:28 +05:30
|
|
|
|
materials: IntSequence = (0,1)) -> 'GeomGrid':
|
2020-09-30 10:40:15 +05:30
|
|
|
|
"""
|
2023-02-21 20:57:06 +05:30
|
|
|
|
Create grid from definition of triply-periodic minimal surface.
|
2020-09-30 10:40:15 +05:30
|
|
|
|
|
|
|
|
|
Parameters
|
|
|
|
|
----------
|
2022-01-13 01:04:29 +05:30
|
|
|
|
cells : sequence of int, len (3)
|
2023-02-21 20:57:06 +05:30
|
|
|
|
Cell counts along x,y,z direction.
|
2022-01-13 01:04:29 +05:30
|
|
|
|
size : sequence of float, len (3)
|
2023-02-21 20:57:06 +05:30
|
|
|
|
Edge lengths of the grid in meter.
|
2020-10-01 12:55:32 +05:30
|
|
|
|
surface : str
|
|
|
|
|
Type of the minimal surface. See notes for details.
|
2020-09-30 10:40:15 +05:30
|
|
|
|
threshold : float, optional.
|
|
|
|
|
Threshold of the minimal surface. Defaults to 0.0.
|
|
|
|
|
periods : integer, optional.
|
|
|
|
|
Number of periods per unit cell. Defaults to 1.
|
2022-01-13 01:04:29 +05:30
|
|
|
|
materials : sequence of int, len (2)
|
2021-07-16 13:51:06 +05:30
|
|
|
|
Material IDs. Defaults to (0,1).
|
2020-10-28 14:01:55 +05:30
|
|
|
|
|
2021-04-23 22:45:11 +05:30
|
|
|
|
Returns
|
|
|
|
|
-------
|
2023-11-28 03:11:28 +05:30
|
|
|
|
new : damask.GeomGrid
|
|
|
|
|
GeomGrid-based geometry from definition of minimal surface.
|
2021-04-23 22:45:11 +05:30
|
|
|
|
|
2020-10-01 12:55:32 +05:30
|
|
|
|
Notes
|
|
|
|
|
-----
|
|
|
|
|
The following triply-periodic minimal surfaces are implemented:
|
|
|
|
|
- Schwarz P
|
|
|
|
|
- Double Primitive
|
|
|
|
|
- Schwarz D
|
|
|
|
|
- Complementary D
|
|
|
|
|
- Double Diamond
|
|
|
|
|
- Dprime
|
|
|
|
|
- Gyroid
|
|
|
|
|
- Gprime
|
|
|
|
|
- Karcher K
|
|
|
|
|
- Lidinoid
|
|
|
|
|
- Neovius
|
|
|
|
|
- Fisher-Koch S
|
2020-09-30 10:40:15 +05:30
|
|
|
|
|
2020-10-01 02:57:49 +05:30
|
|
|
|
References
|
|
|
|
|
----------
|
2021-03-18 20:06:40 +05:30
|
|
|
|
S.B.G. Blanquer et al., Biofabrication 9(2):025001, 2017
|
2020-11-08 22:41:30 +05:30
|
|
|
|
https://doi.org/10.1088/1758-5090/aa6553
|
2020-10-01 02:57:49 +05:30
|
|
|
|
|
2021-03-18 20:06:40 +05:30
|
|
|
|
M. Wohlgemuth et al., Macromolecules 34(17):6083-6089, 2001
|
2020-11-08 22:41:30 +05:30
|
|
|
|
https://doi.org/10.1021/ma0019499
|
2020-10-01 02:57:49 +05:30
|
|
|
|
|
2021-03-18 20:06:40 +05:30
|
|
|
|
M.-T. Hsieh and L. Valdevit, Software Impacts 6:100026, 2020
|
2020-11-08 22:41:30 +05:30
|
|
|
|
https://doi.org/10.1016/j.simpa.2020.100026
|
2020-10-01 02:57:49 +05:30
|
|
|
|
|
2021-07-16 13:51:06 +05:30
|
|
|
|
Examples
|
|
|
|
|
--------
|
|
|
|
|
Minimal surface of 'Gyroid' type.
|
|
|
|
|
|
|
|
|
|
>>> import numpy as np
|
|
|
|
|
>>> import damask
|
2023-11-28 03:11:28 +05:30
|
|
|
|
>>> damask.GeomGrid.from_minimal_surface([64]*3,np.ones(3)*1.e-4,'Gyroid')
|
2023-02-21 20:57:06 +05:30
|
|
|
|
cells : 64 × 64 × 64
|
|
|
|
|
size : 0.0001 × 0.0001 × 0.0001 m³
|
2022-02-17 11:43:39 +05:30
|
|
|
|
origin: 0.0 0.0 0.0 m
|
2021-07-16 13:51:06 +05:30
|
|
|
|
# materials: 2
|
|
|
|
|
|
2023-02-21 20:57:06 +05:30
|
|
|
|
Minimal surface of 'Neovius' type with specific material IDs.
|
2021-07-16 13:51:06 +05:30
|
|
|
|
|
|
|
|
|
>>> import numpy as np
|
|
|
|
|
>>> import damask
|
2023-11-28 03:11:28 +05:30
|
|
|
|
>>> damask.GeomGrid.from_minimal_surface([80]*3,np.ones(3)*5.e-4,
|
2021-07-16 13:51:06 +05:30
|
|
|
|
... 'Neovius',materials=(1,5))
|
2023-02-21 20:57:06 +05:30
|
|
|
|
cells : 80 × 80 × 80
|
|
|
|
|
size : 0.0005 × 0.0005 × 0.0005 m³
|
2022-02-17 11:43:39 +05:30
|
|
|
|
origin: 0.0 0.0 0.0 m
|
2021-07-16 13:51:06 +05:30
|
|
|
|
# materials: 2 (min: 1, max: 5)
|
|
|
|
|
|
2020-09-30 10:40:15 +05:30
|
|
|
|
"""
|
2020-12-04 02:28:24 +05:30
|
|
|
|
x,y,z = np.meshgrid(periods*2.0*np.pi*(np.arange(cells[0])+0.5)/cells[0],
|
|
|
|
|
periods*2.0*np.pi*(np.arange(cells[1])+0.5)/cells[1],
|
|
|
|
|
periods*2.0*np.pi*(np.arange(cells[2])+0.5)/cells[2],
|
2020-09-30 10:40:15 +05:30
|
|
|
|
indexing='ij',sparse=True)
|
2023-11-28 03:11:28 +05:30
|
|
|
|
return GeomGrid(material = np.where(threshold < GeomGrid._minimal_surface[surface](x,y,z),materials[1],materials[0]),
|
|
|
|
|
size = size,
|
|
|
|
|
comments = util.execution_stamp('GeomGrid','from_minimal_surface'),
|
|
|
|
|
)
|
2020-09-30 10:40:15 +05:30
|
|
|
|
|
|
|
|
|
|
2022-01-26 20:55:27 +05:30
|
|
|
|
def save(self,
|
|
|
|
|
fname: Union[str, Path],
|
|
|
|
|
compress: bool = True):
|
2020-10-01 02:57:49 +05:30
|
|
|
|
"""
|
2023-02-21 20:57:06 +05:30
|
|
|
|
Save as VTK ImageData file.
|
2020-10-01 02:57:49 +05:30
|
|
|
|
|
|
|
|
|
Parameters
|
|
|
|
|
----------
|
2020-11-20 00:56:15 +05:30
|
|
|
|
fname : str or pathlib.Path
|
2023-02-21 20:57:06 +05:30
|
|
|
|
Filename to write.
|
|
|
|
|
Valid extension is .vti, which will be appended if not given.
|
2020-10-01 02:57:49 +05:30
|
|
|
|
compress : bool, optional
|
|
|
|
|
Compress with zlib algorithm. Defaults to True.
|
|
|
|
|
|
|
|
|
|
"""
|
2022-02-21 16:47:00 +05:30
|
|
|
|
v = VTK.from_image_data(self.cells,self.size,self.origin)\
|
2022-05-12 04:24:03 +05:30
|
|
|
|
.set('material',self.material.flatten(order='F'))
|
2022-03-25 02:52:18 +05:30
|
|
|
|
for label,data in self.initial_conditions.items():
|
2022-05-12 04:24:03 +05:30
|
|
|
|
v = v.set(label,data.flatten(order='F'))
|
2022-03-09 20:05:36 +05:30
|
|
|
|
v.comments = self.comments
|
2020-10-01 02:57:49 +05:30
|
|
|
|
|
2022-01-13 01:04:29 +05:30
|
|
|
|
v.save(fname,parallel=False,compress=compress)
|
2020-10-01 02:57:49 +05:30
|
|
|
|
|
|
|
|
|
|
2022-01-26 20:55:27 +05:30
|
|
|
|
def save_ASCII(self,
|
|
|
|
|
fname: Union[str, TextIO]):
|
2019-11-23 01:22:36 +05:30
|
|
|
|
"""
|
2020-12-04 02:28:24 +05:30
|
|
|
|
Save as geom file.
|
2019-11-23 01:22:36 +05:30
|
|
|
|
|
2020-11-14 22:24:47 +05:30
|
|
|
|
Storing geometry files in ASCII format is deprecated.
|
|
|
|
|
This function will be removed in a future version of DAMASK.
|
|
|
|
|
|
2019-11-23 01:22:36 +05:30
|
|
|
|
Parameters
|
|
|
|
|
----------
|
|
|
|
|
fname : str or file handle
|
2020-09-15 10:28:06 +05:30
|
|
|
|
Geometry file to write with extension '.geom'.
|
2020-09-18 20:02:08 +05:30
|
|
|
|
compress : bool, optional
|
2023-02-21 20:57:06 +05:30
|
|
|
|
Compress geometry using 'x of y' and 'a to b'.
|
2019-11-23 01:22:36 +05:30
|
|
|
|
|
|
|
|
|
"""
|
2022-01-05 18:36:06 +05:30
|
|
|
|
warnings.warn('Support for ASCII-based geom format will be removed in DAMASK 3.0.0', DeprecationWarning,2)
|
2020-09-23 00:52:58 +05:30
|
|
|
|
header = [f'{len(self.comments)+4} header'] + self.comments \
|
2020-12-04 02:28:24 +05:30
|
|
|
|
+ ['grid a {} b {} c {}'.format(*self.cells),
|
2020-09-23 00:52:58 +05:30
|
|
|
|
'size x {} y {} z {}'.format(*self.size),
|
|
|
|
|
'origin x {} y {} z {}'.format(*self.origin),
|
|
|
|
|
'homogenization 1',
|
|
|
|
|
]
|
2020-09-15 10:28:06 +05:30
|
|
|
|
|
2020-10-10 13:11:11 +05:30
|
|
|
|
format_string = '%g' if self.material.dtype in np.sctypes['float'] else \
|
|
|
|
|
'%{}i'.format(1+int(np.floor(np.log10(np.nanmax(self.material)))))
|
|
|
|
|
np.savetxt(fname,
|
2020-12-04 02:28:24 +05:30
|
|
|
|
self.material.reshape([self.cells[0],np.prod(self.cells[1:])],order='F').T,
|
2020-10-10 13:11:11 +05:30
|
|
|
|
header='\n'.join(header), fmt=format_string, comments='')
|
2020-09-15 10:28:06 +05:30
|
|
|
|
|
|
|
|
|
|
2022-02-21 15:49:53 +05:30
|
|
|
|
def show(self,
|
2022-03-09 03:13:54 +05:30
|
|
|
|
colormap: Union[Colormap, str] = 'cividis') -> None:
|
2022-02-21 15:49:53 +05:30
|
|
|
|
"""
|
|
|
|
|
Show on screen.
|
|
|
|
|
|
|
|
|
|
Parameters
|
|
|
|
|
----------
|
2022-03-08 20:01:08 +05:30
|
|
|
|
colormap : damask.Colormap or str, optional
|
|
|
|
|
Colormap for visualization of material IDs. Defaults to 'cividis'.
|
2022-02-21 15:49:53 +05:30
|
|
|
|
|
|
|
|
|
"""
|
2022-02-22 22:53:29 +05:30
|
|
|
|
VTK.from_image_data(self.cells,self.size,self.origin) \
|
2022-05-12 04:24:03 +05:30
|
|
|
|
.set('material',self.material.flatten('F'),) \
|
2022-02-22 22:53:29 +05:30
|
|
|
|
.show('material',colormap)
|
2019-11-23 02:18:41 +05:30
|
|
|
|
|
|
|
|
|
|
2022-03-12 02:52:12 +05:30
|
|
|
|
def canvas(self,
|
2022-11-23 02:56:15 +05:30
|
|
|
|
cells: Optional[IntSequence] = None,
|
|
|
|
|
offset: Optional[IntSequence] = None,
|
2023-11-28 03:11:28 +05:30
|
|
|
|
fill: Optional[int] = None) -> 'GeomGrid':
|
2020-08-08 23:44:30 +05:30
|
|
|
|
"""
|
2022-03-12 02:52:12 +05:30
|
|
|
|
Crop or enlarge/pad grid.
|
2020-08-08 23:44:30 +05:30
|
|
|
|
|
|
|
|
|
Parameters
|
|
|
|
|
----------
|
2022-03-12 02:52:12 +05:30
|
|
|
|
cells : sequence of int, len (3), optional
|
2023-02-21 20:57:06 +05:30
|
|
|
|
Cell counts along x,y,z direction.
|
2022-03-12 02:52:12 +05:30
|
|
|
|
offset : sequence of int, len (3), optional
|
|
|
|
|
Offset (measured in cells) from old to new grid.
|
|
|
|
|
Defaults to [0,0,0].
|
2020-08-09 00:26:17 +05:30
|
|
|
|
fill : int, optional
|
2022-03-12 02:52:12 +05:30
|
|
|
|
Material ID to fill the background.
|
2023-02-21 20:57:06 +05:30
|
|
|
|
Defaults to material.max()+1.
|
2020-08-08 23:44:30 +05:30
|
|
|
|
|
2021-04-23 22:45:11 +05:30
|
|
|
|
Returns
|
|
|
|
|
-------
|
2023-11-28 03:11:28 +05:30
|
|
|
|
updated : damask.GeomGrid
|
2021-04-24 18:17:52 +05:30
|
|
|
|
Updated grid-based geometry.
|
2021-04-23 22:45:11 +05:30
|
|
|
|
|
2021-07-02 09:46:12 +05:30
|
|
|
|
Examples
|
|
|
|
|
--------
|
2022-03-12 02:52:12 +05:30
|
|
|
|
Remove lower 1/2 of the microstructure in z-direction.
|
2021-07-02 09:46:12 +05:30
|
|
|
|
|
|
|
|
|
>>> import numpy as np
|
|
|
|
|
>>> import damask
|
2023-11-28 03:11:28 +05:30
|
|
|
|
>>> g = damask.GeomGrid(np.zeros([32]*3,int),np.ones(3)*1e-3)
|
2022-03-12 02:52:12 +05:30
|
|
|
|
>>> g.canvas([32,32,16],[0,0,16])
|
2023-02-21 20:57:06 +05:30
|
|
|
|
cells: 32 × 32 × 16
|
|
|
|
|
size: 0.001 × 0.001 × 0.0005 m³
|
|
|
|
|
origin: 0.0 0.0 0.0005 m
|
2022-03-12 02:52:12 +05:30
|
|
|
|
# materials: 1
|
2021-07-02 09:46:12 +05:30
|
|
|
|
|
2020-08-08 23:44:30 +05:30
|
|
|
|
"""
|
2022-04-22 23:57:03 +05:30
|
|
|
|
offset_ = np.array(offset,np.int64) if offset is not None else np.zeros(3,np.int64)
|
|
|
|
|
cells_ = np.array(cells,np.int64) if cells is not None else self.cells
|
2020-08-08 23:44:30 +05:30
|
|
|
|
|
2022-03-12 02:52:12 +05:30
|
|
|
|
canvas = np.full(cells_,np.nanmax(self.material) + 1 if fill is None else fill,self.material.dtype)
|
2020-08-08 23:44:30 +05:30
|
|
|
|
|
2022-03-12 02:52:12 +05:30
|
|
|
|
LL = np.clip( offset_, 0,np.minimum(self.cells, cells_+offset_))
|
|
|
|
|
UR = np.clip( offset_+cells_, 0,np.minimum(self.cells, cells_+offset_))
|
|
|
|
|
ll = np.clip(-offset_, 0,np.minimum( cells_,self.cells-offset_))
|
|
|
|
|
ur = np.clip(-offset_+self.cells,0,np.minimum( cells_,self.cells-offset_))
|
2020-08-08 23:44:30 +05:30
|
|
|
|
|
2022-03-12 02:52:12 +05:30
|
|
|
|
canvas[ll[0]:ur[0],ll[1]:ur[1],ll[2]:ur[2]] = self.material[LL[0]:UR[0],LL[1]:UR[1],LL[2]:UR[2]]
|
2020-08-08 23:44:30 +05:30
|
|
|
|
|
2023-11-28 03:11:28 +05:30
|
|
|
|
return GeomGrid(material = canvas,
|
|
|
|
|
size = self.size/self.cells*np.asarray(canvas.shape),
|
|
|
|
|
origin = self.origin+offset_*self.size/self.cells,
|
|
|
|
|
comments = self.comments+[util.execution_stamp('GeomGrid','canvas')],
|
|
|
|
|
)
|
2020-08-08 23:44:30 +05:30
|
|
|
|
|
|
|
|
|
|
2022-01-26 20:55:27 +05:30
|
|
|
|
def mirror(self,
|
|
|
|
|
directions: Sequence[str],
|
2023-11-28 03:11:28 +05:30
|
|
|
|
reflect: bool = False) -> 'GeomGrid':
|
2019-11-23 02:18:41 +05:30
|
|
|
|
"""
|
2020-12-04 11:42:18 +05:30
|
|
|
|
Mirror grid along given directions.
|
2019-11-24 13:22:46 +05:30
|
|
|
|
|
2019-11-23 02:18:41 +05:30
|
|
|
|
Parameters
|
|
|
|
|
----------
|
2022-01-30 03:46:57 +05:30
|
|
|
|
directions : (sequence of) {'x', 'y', 'z'}
|
2020-12-04 11:42:18 +05:30
|
|
|
|
Direction(s) along which the grid is mirrored.
|
2019-11-23 02:18:41 +05:30
|
|
|
|
reflect : bool, optional
|
2020-08-25 12:04:04 +05:30
|
|
|
|
Reflect (include) outermost layers. Defaults to False.
|
2019-11-24 13:22:46 +05:30
|
|
|
|
|
2021-04-23 22:45:11 +05:30
|
|
|
|
Returns
|
|
|
|
|
-------
|
2023-11-28 03:11:28 +05:30
|
|
|
|
updated : damask.GeomGrid
|
2021-04-24 18:17:52 +05:30
|
|
|
|
Updated grid-based geometry.
|
2021-04-23 22:45:11 +05:30
|
|
|
|
|
2021-07-02 09:46:12 +05:30
|
|
|
|
Examples
|
|
|
|
|
--------
|
2023-02-21 20:57:06 +05:30
|
|
|
|
Mirror along y-direction.
|
2021-07-02 09:46:12 +05:30
|
|
|
|
|
|
|
|
|
>>> import numpy as np
|
|
|
|
|
>>> import damask
|
2023-11-28 03:11:28 +05:30
|
|
|
|
>>> (g := damask.GeomGrid(np.arange(4*5*6).reshape([4,5,6]),np.ones(3)))
|
2023-02-21 20:57:06 +05:30
|
|
|
|
cells: 4 × 5 × 6
|
|
|
|
|
size: 1.0 × 1.0 × 1.0 m³
|
2022-02-17 11:43:39 +05:30
|
|
|
|
origin: 0.0 0.0 0.0 m
|
2023-02-21 20:57:06 +05:30
|
|
|
|
# materials: 120
|
|
|
|
|
>>> g.mirror('y')
|
|
|
|
|
cells: 4 × 8 × 6
|
|
|
|
|
size: 1.0 × 1.6 × 1.0 m³
|
|
|
|
|
origin: 0.0 0.0 0.0 m
|
|
|
|
|
# materials: 120
|
|
|
|
|
|
|
|
|
|
Reflect along x- and y-direction.
|
|
|
|
|
|
|
|
|
|
>>> g.mirror('xy',reflect=True)
|
|
|
|
|
cells: 8 × 10 × 6
|
|
|
|
|
size: 2.0 × 2.0 × 1.0 m³
|
|
|
|
|
origin: 0.0 0.0 0.0 m
|
|
|
|
|
# materials: 120
|
|
|
|
|
|
|
|
|
|
Independence of mirroring order.
|
|
|
|
|
|
|
|
|
|
>>> g.mirror('xy') == g.mirror(['y','x'])
|
|
|
|
|
True
|
2021-07-02 09:46:12 +05:30
|
|
|
|
|
2019-11-23 02:18:41 +05:30
|
|
|
|
"""
|
2022-01-29 22:46:19 +05:30
|
|
|
|
if not set(directions).issubset(valid := ['x', 'y', 'z']):
|
2022-02-22 21:12:05 +05:30
|
|
|
|
raise ValueError(f'invalid direction "{set(directions).difference(valid)}" specified')
|
2019-11-23 02:18:41 +05:30
|
|
|
|
|
2021-12-06 18:52:52 +05:30
|
|
|
|
limits: Sequence[Optional[int]] = [None,None] if reflect else [-2,0]
|
2020-09-24 02:57:15 +05:30
|
|
|
|
mat = self.material.copy()
|
2019-11-23 02:18:41 +05:30
|
|
|
|
|
|
|
|
|
if 'x' in directions:
|
2020-09-24 02:57:15 +05:30
|
|
|
|
mat = np.concatenate([mat,mat[limits[0]:limits[1]:-1,:,:]],0)
|
2020-09-23 00:17:08 +05:30
|
|
|
|
if 'y' in directions:
|
2020-09-24 02:57:15 +05:30
|
|
|
|
mat = np.concatenate([mat,mat[:,limits[0]:limits[1]:-1,:]],1)
|
2020-09-23 00:17:08 +05:30
|
|
|
|
if 'z' in directions:
|
2020-09-24 02:57:15 +05:30
|
|
|
|
mat = np.concatenate([mat,mat[:,:,limits[0]:limits[1]:-1]],2)
|
2020-03-21 15:37:21 +05:30
|
|
|
|
|
2023-11-28 03:11:28 +05:30
|
|
|
|
return GeomGrid(material = mat,
|
|
|
|
|
size = self.size/self.cells*np.asarray(mat.shape),
|
|
|
|
|
origin = self.origin,
|
|
|
|
|
comments = self.comments+[util.execution_stamp('GeomGrid','mirror')],
|
|
|
|
|
)
|
2020-08-24 04:16:01 +05:30
|
|
|
|
|
|
|
|
|
|
2022-01-26 20:55:27 +05:30
|
|
|
|
def flip(self,
|
2023-11-28 03:11:28 +05:30
|
|
|
|
directions: Sequence[str]) -> 'GeomGrid':
|
2020-08-24 04:16:01 +05:30
|
|
|
|
"""
|
2020-12-04 11:42:18 +05:30
|
|
|
|
Flip grid along given directions.
|
2020-08-24 04:16:01 +05:30
|
|
|
|
|
|
|
|
|
Parameters
|
|
|
|
|
----------
|
2022-01-30 03:46:57 +05:30
|
|
|
|
directions : (sequence of) {'x', 'y', 'z'}
|
2020-12-04 11:42:18 +05:30
|
|
|
|
Direction(s) along which the grid is flipped.
|
2020-08-24 04:16:01 +05:30
|
|
|
|
|
2021-04-23 22:45:11 +05:30
|
|
|
|
Returns
|
|
|
|
|
-------
|
2023-11-28 03:11:28 +05:30
|
|
|
|
updated : damask.GeomGrid
|
2021-04-24 18:17:52 +05:30
|
|
|
|
Updated grid-based geometry.
|
2021-04-23 22:45:11 +05:30
|
|
|
|
|
2023-02-21 20:57:06 +05:30
|
|
|
|
Examples
|
|
|
|
|
--------
|
|
|
|
|
Invariance of flipping order.
|
|
|
|
|
|
|
|
|
|
>>> import numpy as np
|
|
|
|
|
>>> import damask
|
2023-11-28 03:11:28 +05:30
|
|
|
|
>>> (g := damask.GeomGrid(np.arange(4*5*6).reshape([4,5,6]),np.ones(3)))
|
2023-02-21 20:57:06 +05:30
|
|
|
|
cells: 4 × 5 × 6
|
|
|
|
|
size: 1.0 × 1.0 × 1.0 m³
|
|
|
|
|
origin: 0.0 0.0 0.0 m
|
|
|
|
|
# materials: 120
|
|
|
|
|
>>> g.flip('xyz') == g.flip(['x','z','y'])
|
|
|
|
|
True
|
|
|
|
|
|
|
|
|
|
Invariance of flipping a (fully) mirrored grid.
|
|
|
|
|
|
|
|
|
|
>>> g.mirror('x',True) == g.mirror('x',True).flip('x')
|
|
|
|
|
True
|
|
|
|
|
|
2020-08-24 04:16:01 +05:30
|
|
|
|
"""
|
2022-01-29 22:46:19 +05:30
|
|
|
|
if not set(directions).issubset(valid := ['x', 'y', 'z']):
|
2022-02-22 21:12:05 +05:30
|
|
|
|
raise ValueError(f'invalid direction "{set(directions).difference(valid)}" specified')
|
2020-08-24 04:16:01 +05:30
|
|
|
|
|
2022-01-14 22:26:58 +05:30
|
|
|
|
mat = np.flip(self.material, [valid.index(d) for d in directions if d in valid])
|
2020-08-24 04:16:01 +05:30
|
|
|
|
|
2023-11-28 03:11:28 +05:30
|
|
|
|
return GeomGrid(material = mat,
|
|
|
|
|
size = self.size,
|
|
|
|
|
origin = self.origin,
|
|
|
|
|
comments = self.comments+[util.execution_stamp('GeomGrid','flip')],
|
|
|
|
|
)
|
2019-11-23 02:18:41 +05:30
|
|
|
|
|
|
|
|
|
|
2022-03-12 02:52:12 +05:30
|
|
|
|
def rotate(self,
|
|
|
|
|
R: Rotation,
|
2023-11-28 03:11:28 +05:30
|
|
|
|
fill: Optional[int] = None) -> 'GeomGrid':
|
2022-03-12 02:52:12 +05:30
|
|
|
|
"""
|
2023-02-21 20:57:06 +05:30
|
|
|
|
Rotate grid (possibly extending its bounding box).
|
2022-03-12 02:52:12 +05:30
|
|
|
|
|
|
|
|
|
Parameters
|
|
|
|
|
----------
|
|
|
|
|
R : damask.Rotation
|
|
|
|
|
Rotation to apply to the grid.
|
|
|
|
|
fill : int, optional
|
|
|
|
|
Material ID to fill enlarged bounding box.
|
2023-02-21 20:57:06 +05:30
|
|
|
|
Defaults to material.max()+1.
|
2022-03-12 02:52:12 +05:30
|
|
|
|
|
|
|
|
|
Returns
|
|
|
|
|
-------
|
2023-11-28 03:11:28 +05:30
|
|
|
|
updated : damask.GeomGrid
|
2022-03-12 02:52:12 +05:30
|
|
|
|
Updated grid-based geometry.
|
|
|
|
|
|
2023-02-21 20:57:06 +05:30
|
|
|
|
Examples
|
|
|
|
|
--------
|
|
|
|
|
Rotation by 180° (π) is equivalent to twice flipping.
|
|
|
|
|
|
|
|
|
|
>>> import numpy as np
|
|
|
|
|
>>> import damask
|
2023-11-28 03:11:28 +05:30
|
|
|
|
>>> (g := damask.GeomGrid(np.arange(4*5*6).reshape([4,5,6]),np.ones(3)))
|
2023-02-21 20:57:06 +05:30
|
|
|
|
cells: 4 × 5 × 6
|
|
|
|
|
size: 1.0 × 1.0 × 1.0 m³
|
|
|
|
|
origin: 0.0 0.0 0.0 m
|
|
|
|
|
# materials: 120
|
|
|
|
|
>>> g.rotate(damask.Rotation.from_axis_angle([0,0,1,180],degrees=True)) == g.flip('xy')
|
|
|
|
|
True
|
|
|
|
|
|
2022-03-12 02:52:12 +05:30
|
|
|
|
"""
|
|
|
|
|
material = self.material
|
|
|
|
|
# These rotations are always applied in the reference coordinate system, i.e. (z,x,z) not (z,x',z'')
|
|
|
|
|
# see https://www.cs.utexas.edu/~theshark/courses/cs354/lectures/cs354-14.pdf
|
|
|
|
|
for angle,axes in zip(R.as_Euler_angles(degrees=True)[::-1], [(0,1),(1,2),(0,1)]):
|
|
|
|
|
material_temp = ndimage.rotate(material,angle,axes,order=0,prefilter=False,
|
|
|
|
|
output=self.material.dtype,
|
|
|
|
|
cval=np.nanmax(self.material) + 1 if fill is None else fill)
|
|
|
|
|
# avoid scipy interpolation errors for rotations close to multiples of 90°
|
|
|
|
|
material = material_temp if np.prod(material_temp.shape) != np.prod(material.shape) else \
|
2022-06-02 23:10:18 +05:30
|
|
|
|
np.rot90(material,k=np.rint(angle/90.).astype(np.int64),axes=axes)
|
2022-03-12 02:52:12 +05:30
|
|
|
|
|
|
|
|
|
origin = self.origin-(np.asarray(material.shape)-self.cells)*.5 * self.size/self.cells
|
|
|
|
|
|
2023-11-28 03:11:28 +05:30
|
|
|
|
return GeomGrid(material = material,
|
|
|
|
|
size = self.size/self.cells*np.asarray(material.shape),
|
|
|
|
|
origin = origin,
|
|
|
|
|
comments = self.comments+[util.execution_stamp('GeomGrid','rotate')],
|
|
|
|
|
)
|
2022-03-12 02:52:12 +05:30
|
|
|
|
|
|
|
|
|
|
2022-01-26 20:55:27 +05:30
|
|
|
|
def scale(self,
|
2023-11-28 03:11:28 +05:30
|
|
|
|
cells: IntSequence) -> 'GeomGrid':
|
2019-11-24 19:43:26 +05:30
|
|
|
|
"""
|
2023-02-21 20:57:06 +05:30
|
|
|
|
Scale grid to new cell counts.
|
2019-11-24 19:43:26 +05:30
|
|
|
|
|
|
|
|
|
Parameters
|
|
|
|
|
----------
|
2022-01-13 01:04:29 +05:30
|
|
|
|
cells : sequence of int, len (3)
|
2023-02-21 20:57:06 +05:30
|
|
|
|
Cell counts along x,y,z direction.
|
2019-11-24 19:43:26 +05:30
|
|
|
|
|
2021-04-23 22:45:11 +05:30
|
|
|
|
Returns
|
|
|
|
|
-------
|
2023-11-28 03:11:28 +05:30
|
|
|
|
updated : damask.GeomGrid
|
2021-04-24 18:17:52 +05:30
|
|
|
|
Updated grid-based geometry.
|
2021-04-23 22:45:11 +05:30
|
|
|
|
|
2021-07-02 09:46:12 +05:30
|
|
|
|
Examples
|
|
|
|
|
--------
|
2023-02-21 20:57:06 +05:30
|
|
|
|
Double grid resolution.
|
2021-07-02 09:46:12 +05:30
|
|
|
|
|
|
|
|
|
>>> import numpy as np
|
|
|
|
|
>>> import damask
|
2023-11-28 03:11:28 +05:30
|
|
|
|
>>> (g := damask.GeomGrid(np.zeros([32]*3,int),np.ones(3)*1e-4))
|
2022-11-09 00:22:08 +05:30
|
|
|
|
cells: 32 × 32 × 32
|
|
|
|
|
size: 0.0001 × 0.0001 × 0.0001 m³
|
|
|
|
|
origin: 0.0 0.0 0.0 m
|
|
|
|
|
# materials: 1
|
2021-07-02 11:18:01 +05:30
|
|
|
|
>>> g.scale(g.cells*2)
|
2023-02-21 20:57:06 +05:30
|
|
|
|
cells : 64 × 64 × 64
|
|
|
|
|
size : 0.0001 × 0.0001 × 0.0001 m³
|
2022-02-17 11:43:39 +05:30
|
|
|
|
origin: 0.0 0.0 0.0 m
|
2021-07-02 09:46:12 +05:30
|
|
|
|
# materials: 1
|
|
|
|
|
|
2019-11-24 19:43:26 +05:30
|
|
|
|
"""
|
2022-11-09 00:22:08 +05:30
|
|
|
|
orig = tuple(map(np.linspace,self.origin + self.size/self.cells*.5,
|
|
|
|
|
self.origin + self.size - self.size/self.cells*.5,self.cells))
|
2022-11-14 17:12:13 +05:30
|
|
|
|
interpolator = partial(interpolate.RegularGridInterpolator,
|
|
|
|
|
points=orig,method='nearest',bounds_error=False,fill_value=None)
|
2022-11-09 00:22:08 +05:30
|
|
|
|
new = grid_filters.coordinates0_point(cells,self.size,self.origin)
|
|
|
|
|
|
2023-11-28 03:11:28 +05:30
|
|
|
|
return GeomGrid(material = interpolator(values=self.material)(new).astype(int),
|
|
|
|
|
size = self.size,
|
|
|
|
|
origin = self.origin,
|
|
|
|
|
initial_conditions = {k: interpolator(values=v)(new)
|
|
|
|
|
for k,v in self.initial_conditions.items()},
|
|
|
|
|
comments = self.comments+[util.execution_stamp('GeomGrid','scale')],
|
|
|
|
|
)
|
2019-11-24 19:43:26 +05:30
|
|
|
|
|
|
|
|
|
|
2022-11-09 00:22:08 +05:30
|
|
|
|
def assemble(self,
|
2023-11-28 03:11:28 +05:30
|
|
|
|
idx: np.ndarray) -> 'GeomGrid':
|
2022-11-09 00:22:08 +05:30
|
|
|
|
"""
|
|
|
|
|
Assemble new grid from index map.
|
|
|
|
|
|
|
|
|
|
Parameters
|
|
|
|
|
----------
|
|
|
|
|
idx : numpy.ndarray of int, shape (:,:,:) or (:,:,:,3)
|
2023-11-28 03:11:28 +05:30
|
|
|
|
GeomGrid of flat indices or coordinate indices.
|
2022-11-09 00:22:08 +05:30
|
|
|
|
|
|
|
|
|
Returns
|
|
|
|
|
-------
|
2023-11-28 03:11:28 +05:30
|
|
|
|
updated : damask.GeomGrid
|
2022-11-09 00:22:08 +05:30
|
|
|
|
Updated grid-based geometry.
|
|
|
|
|
Cell count of resulting grid matches shape of index map.
|
|
|
|
|
|
|
|
|
|
"""
|
|
|
|
|
cells = idx.shape[:3]
|
|
|
|
|
flat = (idx if len(idx.shape)==3 else grid_filters.ravel_index(idx)).flatten(order='F')
|
|
|
|
|
ic = {k: v.flatten(order='F')[flat].reshape(cells,order='F') for k,v in self.initial_conditions.items()}
|
|
|
|
|
|
2023-11-28 03:11:28 +05:30
|
|
|
|
return GeomGrid(material = self.material.flatten(order='F')[flat].reshape(cells,order='F'),
|
|
|
|
|
size = self.size,
|
|
|
|
|
origin = self.origin,
|
|
|
|
|
initial_conditions = ic,
|
|
|
|
|
comments = self.comments+[util.execution_stamp('GeomGrid','assemble')],
|
|
|
|
|
)
|
2022-11-09 00:22:08 +05:30
|
|
|
|
|
|
|
|
|
|
2023-11-28 03:11:28 +05:30
|
|
|
|
def renumber(self) -> 'GeomGrid':
|
2022-03-12 02:52:12 +05:30
|
|
|
|
"""
|
|
|
|
|
Renumber sorted material indices as 0,...,N-1.
|
|
|
|
|
|
|
|
|
|
Returns
|
|
|
|
|
-------
|
2023-11-28 03:11:28 +05:30
|
|
|
|
updated : damask.GeomGrid
|
2022-03-12 02:52:12 +05:30
|
|
|
|
Updated grid-based geometry.
|
|
|
|
|
|
|
|
|
|
"""
|
|
|
|
|
_,renumbered = np.unique(self.material,return_inverse=True)
|
|
|
|
|
|
2023-11-28 03:11:28 +05:30
|
|
|
|
return GeomGrid(material = renumbered.reshape(self.cells),
|
|
|
|
|
size = self.size,
|
|
|
|
|
origin = self.origin,
|
|
|
|
|
initial_conditions = self.initial_conditions,
|
|
|
|
|
comments = self.comments+[util.execution_stamp('GeomGrid','renumber')],
|
|
|
|
|
)
|
2022-03-12 02:52:12 +05:30
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def substitute(self,
|
|
|
|
|
from_material: Union[int,IntSequence],
|
2023-11-28 03:11:28 +05:30
|
|
|
|
to_material: Union[int,IntSequence]) -> 'GeomGrid':
|
2022-03-12 02:52:12 +05:30
|
|
|
|
"""
|
|
|
|
|
Substitute material indices.
|
|
|
|
|
|
|
|
|
|
Parameters
|
|
|
|
|
----------
|
2022-11-14 19:39:45 +05:30
|
|
|
|
from_material : (sequence of) int
|
2022-03-12 02:52:12 +05:30
|
|
|
|
Material indices to be substituted.
|
2022-11-14 19:39:45 +05:30
|
|
|
|
to_material : (sequence of) int
|
2022-03-12 02:52:12 +05:30
|
|
|
|
New material indices.
|
|
|
|
|
|
|
|
|
|
Returns
|
|
|
|
|
-------
|
2023-11-28 03:11:28 +05:30
|
|
|
|
updated : damask.GeomGrid
|
2022-03-12 02:52:12 +05:30
|
|
|
|
Updated grid-based geometry.
|
|
|
|
|
|
|
|
|
|
"""
|
|
|
|
|
material = self.material.copy()
|
|
|
|
|
for f,t in zip(from_material if isinstance(from_material,(Sequence,np.ndarray)) else [from_material],
|
|
|
|
|
to_material if isinstance(to_material,(Sequence,np.ndarray)) else [to_material]): # ToDo Python 3.10 has strict mode for zip
|
|
|
|
|
material[self.material==f] = t
|
|
|
|
|
|
2023-11-28 03:11:28 +05:30
|
|
|
|
return GeomGrid(material = material,
|
|
|
|
|
size = self.size,
|
|
|
|
|
origin = self.origin,
|
|
|
|
|
initial_conditions = self.initial_conditions,
|
|
|
|
|
comments = self.comments+[util.execution_stamp('GeomGrid','substitute')],
|
|
|
|
|
)
|
2022-03-12 02:52:12 +05:30
|
|
|
|
|
|
|
|
|
|
2023-11-28 03:11:28 +05:30
|
|
|
|
def sort(self) -> 'GeomGrid':
|
2022-03-12 02:52:12 +05:30
|
|
|
|
"""
|
2023-02-21 20:57:06 +05:30
|
|
|
|
Sort material indices such that min(material ID) is located at (0,0,0).
|
2022-03-12 02:52:12 +05:30
|
|
|
|
|
|
|
|
|
Returns
|
|
|
|
|
-------
|
2023-11-28 03:11:28 +05:30
|
|
|
|
updated : damask.GeomGrid
|
2022-03-12 02:52:12 +05:30
|
|
|
|
Updated grid-based geometry.
|
|
|
|
|
|
|
|
|
|
"""
|
|
|
|
|
a = self.material.flatten(order='F')
|
|
|
|
|
from_ma = pd.unique(a)
|
|
|
|
|
sort_idx = np.argsort(from_ma)
|
|
|
|
|
ma = np.unique(a)[sort_idx][np.searchsorted(from_ma,a,sorter = sort_idx)]
|
|
|
|
|
|
2023-11-28 03:11:28 +05:30
|
|
|
|
return GeomGrid(material = ma.reshape(self.cells,order='F'),
|
|
|
|
|
size = self.size,
|
|
|
|
|
origin = self.origin,
|
|
|
|
|
initial_conditions = self.initial_conditions,
|
|
|
|
|
comments = self.comments+[util.execution_stamp('GeomGrid','sort')],
|
|
|
|
|
)
|
2022-03-12 02:52:12 +05:30
|
|
|
|
|
|
|
|
|
|
2022-01-13 01:04:29 +05:30
|
|
|
|
def clean(self,
|
2022-03-10 04:54:05 +05:30
|
|
|
|
distance: float = np.sqrt(3),
|
2022-12-14 00:02:19 +05:30
|
|
|
|
selection: Optional[IntSequence] = None,
|
2022-02-27 20:46:09 +05:30
|
|
|
|
invert_selection: bool = False,
|
2022-03-10 04:54:05 +05:30
|
|
|
|
periodic: bool = True,
|
2023-11-28 03:11:28 +05:30
|
|
|
|
rng_seed: Optional[NumpyRngSeed] = None) -> 'GeomGrid':
|
2019-11-23 02:18:41 +05:30
|
|
|
|
"""
|
2022-02-26 18:52:00 +05:30
|
|
|
|
Smooth grid by selecting most frequent material ID within given stencil at each location.
|
2019-11-24 13:22:46 +05:30
|
|
|
|
|
2019-11-23 02:18:41 +05:30
|
|
|
|
Parameters
|
|
|
|
|
----------
|
2022-03-10 04:54:05 +05:30
|
|
|
|
distance : float, optional
|
|
|
|
|
Voxel distance checked for presence of other materials.
|
|
|
|
|
Defaults to sqrt(3).
|
2022-12-14 00:02:19 +05:30
|
|
|
|
selection : (sequence of) int, optional
|
2022-03-12 02:52:12 +05:30
|
|
|
|
Material IDs to consider. Defaults to all.
|
2022-02-27 20:46:09 +05:30
|
|
|
|
invert_selection : bool, optional
|
|
|
|
|
Consider all material IDs except those in selection. Defaults to False.
|
2022-01-13 03:43:38 +05:30
|
|
|
|
periodic : bool, optional
|
2020-12-04 11:42:18 +05:30
|
|
|
|
Assume grid to be periodic. Defaults to True.
|
2022-03-10 04:54:05 +05:30
|
|
|
|
rng_seed : {None, int, array_like[ints], SeedSequence, BitGenerator, Generator}, optional
|
|
|
|
|
A seed to initialize the BitGenerator. Defaults to None.
|
|
|
|
|
If None, then fresh, unpredictable entropy will be pulled from the OS.
|
2020-08-23 07:03:38 +05:30
|
|
|
|
|
2021-04-23 22:45:11 +05:30
|
|
|
|
Returns
|
|
|
|
|
-------
|
2023-11-28 03:11:28 +05:30
|
|
|
|
updated : damask.GeomGrid
|
2021-04-24 18:17:52 +05:30
|
|
|
|
Updated grid-based geometry.
|
2021-04-23 22:45:11 +05:30
|
|
|
|
|
2022-03-10 04:54:05 +05:30
|
|
|
|
Notes
|
|
|
|
|
-----
|
|
|
|
|
If multiple material IDs are most frequent within a stencil, a random choice is taken.
|
|
|
|
|
|
2020-08-23 07:03:38 +05:30
|
|
|
|
"""
|
2022-03-10 04:54:05 +05:30
|
|
|
|
def most_frequent(stencil: np.ndarray,
|
2022-12-14 00:02:19 +05:30
|
|
|
|
selection: Union[None,np.ndarray],
|
|
|
|
|
rng: np.random.Generator):
|
2022-03-10 04:54:05 +05:30
|
|
|
|
me = stencil[stencil.size//2]
|
2022-03-12 02:52:12 +05:30
|
|
|
|
if selection is None or me in selection:
|
2022-03-10 04:54:05 +05:30
|
|
|
|
unique, counts = np.unique(stencil,return_counts=True)
|
|
|
|
|
return rng.choice(unique[counts==np.max(counts)])
|
2020-08-23 07:03:38 +05:30
|
|
|
|
else:
|
|
|
|
|
return me
|
2019-11-23 02:18:41 +05:30
|
|
|
|
|
2022-03-10 04:54:05 +05:30
|
|
|
|
rng = np.random.default_rng(rng_seed)
|
2022-03-12 02:52:12 +05:30
|
|
|
|
|
2022-06-02 23:10:18 +05:30
|
|
|
|
d = np.floor(distance).astype(np.int64)
|
2022-03-10 04:54:05 +05:30
|
|
|
|
ext = np.linspace(-d,d,1+2*d,dtype=float),
|
|
|
|
|
xx,yy,zz = np.meshgrid(ext,ext,ext)
|
|
|
|
|
footprint = xx**2+yy**2+zz**2 <= distance**2+distance*1e-8
|
2022-03-12 02:52:12 +05:30
|
|
|
|
selection_ = None if selection is None else \
|
2022-12-14 00:02:19 +05:30
|
|
|
|
np.setdiff1d(self.material,selection) if invert_selection else \
|
|
|
|
|
np.intersect1d(self.material,selection)
|
2022-11-09 00:22:08 +05:30
|
|
|
|
material = ndimage.generic_filter(
|
|
|
|
|
self.material,
|
|
|
|
|
most_frequent,
|
|
|
|
|
footprint=footprint,
|
|
|
|
|
mode='wrap' if periodic else 'nearest',
|
|
|
|
|
extra_keywords=dict(selection=selection_,rng=rng),
|
|
|
|
|
).astype(self.material.dtype)
|
2023-11-28 03:11:28 +05:30
|
|
|
|
return GeomGrid(material = material,
|
|
|
|
|
size = self.size,
|
|
|
|
|
origin = self.origin,
|
|
|
|
|
initial_conditions = self.initial_conditions,
|
|
|
|
|
comments = self.comments+[util.execution_stamp('GeomGrid','clean')],
|
|
|
|
|
)
|
2019-11-24 22:51:05 +05:30
|
|
|
|
|
|
|
|
|
|
2022-03-12 02:52:12 +05:30
|
|
|
|
def add_primitive(self,
|
|
|
|
|
dimension: Union[FloatSequence, IntSequence],
|
|
|
|
|
center: Union[FloatSequence, IntSequence],
|
|
|
|
|
exponent: Union[FloatSequence, float],
|
2022-11-23 02:56:15 +05:30
|
|
|
|
fill: Optional[int] = None,
|
2022-03-12 02:52:12 +05:30
|
|
|
|
R: Rotation = Rotation(),
|
|
|
|
|
inverse: bool = False,
|
2023-11-28 03:11:28 +05:30
|
|
|
|
periodic: bool = True) -> 'GeomGrid':
|
2020-05-30 21:01:50 +05:30
|
|
|
|
"""
|
2022-03-12 02:52:12 +05:30
|
|
|
|
Insert a primitive geometric object at a given position.
|
2020-05-30 21:01:50 +05:30
|
|
|
|
|
|
|
|
|
Parameters
|
|
|
|
|
----------
|
2022-03-12 02:52:12 +05:30
|
|
|
|
dimension : sequence of int or float, len (3)
|
|
|
|
|
Dimension (diameter/side length) of the primitive.
|
|
|
|
|
If given as integers, cell centers are addressed.
|
|
|
|
|
If given as floats, physical coordinates are addressed.
|
|
|
|
|
center : sequence of int or float, len (3)
|
|
|
|
|
Center of the primitive.
|
|
|
|
|
If given as integers, cell centers are addressed.
|
|
|
|
|
If given as floats, physical coordinates are addressed.
|
2022-11-14 19:39:45 +05:30
|
|
|
|
exponent : (sequence of) float, len (3)
|
2022-03-12 02:52:12 +05:30
|
|
|
|
Exponents for the three axes.
|
|
|
|
|
0 gives octahedron (ǀxǀ^(2^0) + ǀyǀ^(2^0) + ǀzǀ^(2^0) < 1)
|
|
|
|
|
1 gives sphere (ǀxǀ^(2^1) + ǀyǀ^(2^1) + ǀzǀ^(2^1) < 1)
|
2022-01-13 01:04:29 +05:30
|
|
|
|
fill : int, optional
|
2022-03-12 02:52:12 +05:30
|
|
|
|
Fill value for primitive. Defaults to material.max()+1.
|
|
|
|
|
R : damask.Rotation, optional
|
2023-02-21 20:57:06 +05:30
|
|
|
|
Rotation of the primitive. Defaults to no rotation.
|
2022-03-12 02:52:12 +05:30
|
|
|
|
inverse : bool, optional
|
|
|
|
|
Retain original materials within primitive and fill outside.
|
|
|
|
|
Defaults to False.
|
|
|
|
|
periodic : bool, optional
|
|
|
|
|
Assume grid to be periodic. Defaults to True.
|
2020-05-30 21:01:50 +05:30
|
|
|
|
|
2021-04-23 22:45:11 +05:30
|
|
|
|
Returns
|
|
|
|
|
-------
|
2023-11-28 03:11:28 +05:30
|
|
|
|
updated : damask.GeomGrid
|
2021-04-24 18:17:52 +05:30
|
|
|
|
Updated grid-based geometry.
|
2021-04-23 22:45:11 +05:30
|
|
|
|
|
2021-07-02 09:46:12 +05:30
|
|
|
|
Examples
|
|
|
|
|
--------
|
2022-03-12 02:52:12 +05:30
|
|
|
|
Add a sphere at the center.
|
2021-07-02 09:46:12 +05:30
|
|
|
|
|
|
|
|
|
>>> import numpy as np
|
|
|
|
|
>>> import damask
|
2023-11-28 03:11:28 +05:30
|
|
|
|
>>> g = damask.GeomGrid(np.zeros([64]*3,int), np.ones(3)*1e-4)
|
2022-03-12 02:52:12 +05:30
|
|
|
|
>>> g.add_primitive(np.ones(3)*5e-5,np.ones(3)*5e-5,1)
|
2023-02-21 20:57:06 +05:30
|
|
|
|
cells : 64 × 64 × 64
|
|
|
|
|
size : 0.0001 × 0.0001 × 0.0001 m³
|
2022-03-12 02:52:12 +05:30
|
|
|
|
origin: 0.0 0.0 0.0 m
|
|
|
|
|
# materials: 2
|
2020-05-30 21:01:50 +05:30
|
|
|
|
|
2022-03-12 02:52:12 +05:30
|
|
|
|
Add a cube at the origin.
|
2020-05-30 21:01:50 +05:30
|
|
|
|
|
2022-03-12 02:52:12 +05:30
|
|
|
|
>>> import numpy as np
|
|
|
|
|
>>> import damask
|
2023-11-28 03:11:28 +05:30
|
|
|
|
>>> g = damask.GeomGrid(np.zeros([64]*3,int), np.ones(3)*1e-4)
|
2022-03-12 02:52:12 +05:30
|
|
|
|
>>> g.add_primitive(np.ones(3,int)*32,np.zeros(3),np.inf)
|
2023-02-21 20:57:06 +05:30
|
|
|
|
cells : 64 × 64 × 64
|
|
|
|
|
size : 0.0001 × 0.0001 × 0.0001 m³
|
2022-03-12 02:52:12 +05:30
|
|
|
|
origin: 0.0 0.0 0.0 m
|
|
|
|
|
# materials: 2
|
2021-04-23 22:45:11 +05:30
|
|
|
|
|
2020-05-30 21:01:50 +05:30
|
|
|
|
"""
|
2022-03-12 02:52:12 +05:30
|
|
|
|
# radius and center
|
|
|
|
|
r = np.array(dimension)/2.0*self.size/self.cells if np.array(dimension).dtype in np.sctypes['int'] else \
|
|
|
|
|
np.array(dimension)/2.0
|
|
|
|
|
c = (np.array(center) + .5)*self.size/self.cells if np.array(center).dtype in np.sctypes['int'] else \
|
|
|
|
|
(np.array(center) - self.origin)
|
2020-08-08 23:12:34 +05:30
|
|
|
|
|
2022-03-12 02:52:12 +05:30
|
|
|
|
coords = grid_filters.coordinates0_point(self.cells,self.size,
|
|
|
|
|
-(0.5*(self.size + (self.size/self.cells
|
|
|
|
|
if np.array(center).dtype in np.sctypes['int'] else
|
|
|
|
|
0)) if periodic else c))
|
|
|
|
|
coords_rot = R.broadcast_to(tuple(self.cells))@coords
|
2021-04-23 22:45:11 +05:30
|
|
|
|
|
2022-03-12 02:52:12 +05:30
|
|
|
|
with np.errstate(all='ignore'):
|
2022-08-29 17:14:50 +05:30
|
|
|
|
mask = np.sum(np.power(np.abs(coords_rot)/r,2.0**np.array(exponent)),axis=-1) > 1.0
|
2021-04-23 22:45:11 +05:30
|
|
|
|
|
2022-03-12 02:52:12 +05:30
|
|
|
|
if periodic: # translate back to center
|
2022-06-02 23:10:18 +05:30
|
|
|
|
mask = np.roll(mask,((c/self.size-0.5)*self.cells).round().astype(np.int64),(0,1,2))
|
2020-11-01 01:16:21 +05:30
|
|
|
|
|
2023-11-28 03:11:28 +05:30
|
|
|
|
return GeomGrid(material = np.where(np.logical_not(mask) if inverse else mask,
|
2022-03-12 02:52:12 +05:30
|
|
|
|
self.material,
|
|
|
|
|
np.nanmax(self.material)+1 if fill is None else fill),
|
2023-11-28 03:11:28 +05:30
|
|
|
|
size = self.size,
|
|
|
|
|
origin = self.origin,
|
|
|
|
|
initial_conditions = self.initial_conditions,
|
|
|
|
|
comments = self.comments+[util.execution_stamp('GeomGrid','add_primitive')],
|
|
|
|
|
)
|
2020-11-01 01:16:21 +05:30
|
|
|
|
|
|
|
|
|
|
2021-12-06 18:52:52 +05:30
|
|
|
|
def vicinity_offset(self,
|
2022-03-10 04:54:05 +05:30
|
|
|
|
distance: float = np.sqrt(3),
|
2022-11-23 02:56:15 +05:30
|
|
|
|
offset: Optional[int] = None,
|
2022-12-14 00:02:19 +05:30
|
|
|
|
selection: Optional[IntSequence] = None,
|
2022-02-27 20:46:09 +05:30
|
|
|
|
invert_selection: bool = False,
|
2023-11-28 03:11:28 +05:30
|
|
|
|
periodic: bool = True) -> 'GeomGrid':
|
2020-08-08 23:12:34 +05:30
|
|
|
|
"""
|
2022-03-10 04:54:05 +05:30
|
|
|
|
Offset material ID of points in the vicinity of selected (or just other) material IDs.
|
2020-08-08 23:12:34 +05:30
|
|
|
|
|
2022-03-09 20:05:36 +05:30
|
|
|
|
Trigger points are variations in material ID, i.e. grain/phase
|
|
|
|
|
boundaries or explicitly given material IDs.
|
2020-08-08 23:12:34 +05:30
|
|
|
|
|
|
|
|
|
Parameters
|
|
|
|
|
----------
|
2022-03-10 04:54:05 +05:30
|
|
|
|
distance : float, optional
|
2020-09-23 00:17:08 +05:30
|
|
|
|
Voxel distance checked for presence of other materials.
|
2022-03-10 04:54:05 +05:30
|
|
|
|
Defaults to sqrt(3).
|
2020-08-08 23:12:34 +05:30
|
|
|
|
offset : int, optional
|
2022-03-10 04:54:05 +05:30
|
|
|
|
Offset (positive or negative) to tag material IDs.
|
|
|
|
|
Defaults to material.max()+1.
|
2022-12-14 00:02:19 +05:30
|
|
|
|
selection : (sequence of) int, optional
|
2022-03-10 04:54:05 +05:30
|
|
|
|
Material IDs that trigger an offset.
|
|
|
|
|
Defaults to any other than own material ID.
|
2022-02-27 20:46:09 +05:30
|
|
|
|
invert_selection : bool, optional
|
2022-03-10 04:54:05 +05:30
|
|
|
|
Consider all material IDs except those in selection.
|
|
|
|
|
Defaults to False.
|
2022-01-13 03:43:38 +05:30
|
|
|
|
periodic : bool, optional
|
2020-12-04 11:42:18 +05:30
|
|
|
|
Assume grid to be periodic. Defaults to True.
|
2020-08-08 23:12:34 +05:30
|
|
|
|
|
2021-04-23 22:45:11 +05:30
|
|
|
|
Returns
|
|
|
|
|
-------
|
2023-11-28 03:11:28 +05:30
|
|
|
|
updated : damask.GeomGrid
|
2021-04-24 18:17:52 +05:30
|
|
|
|
Updated grid-based geometry.
|
2021-04-23 22:45:11 +05:30
|
|
|
|
|
2020-08-08 23:12:34 +05:30
|
|
|
|
"""
|
2022-12-10 12:52:22 +05:30
|
|
|
|
@numba_njit_wrapper()
|
|
|
|
|
def tainted_neighborhood(stencil: np.ndarray,
|
|
|
|
|
selection: Optional[np.ndarray] = None):
|
2022-03-10 04:54:05 +05:30
|
|
|
|
me = stencil[stencil.size//2]
|
2022-12-10 12:52:22 +05:30
|
|
|
|
if selection is None:
|
|
|
|
|
return np.any(stencil != me)
|
|
|
|
|
elif not len(selection)==0:
|
|
|
|
|
for stencil_item in stencil:
|
|
|
|
|
for selection_item in selection:
|
|
|
|
|
if stencil_item==selection_item and selection_item!=me:
|
|
|
|
|
return True
|
|
|
|
|
return False
|
2022-06-02 23:10:18 +05:30
|
|
|
|
d = np.floor(distance).astype(np.int64)
|
2022-03-10 04:54:05 +05:30
|
|
|
|
ext = np.linspace(-d,d,1+2*d,dtype=float),
|
|
|
|
|
xx,yy,zz = np.meshgrid(ext,ext,ext)
|
|
|
|
|
footprint = xx**2+yy**2+zz**2 <= distance**2+distance*1e-8
|
2020-11-04 04:13:57 +05:30
|
|
|
|
offset_ = np.nanmax(self.material)+1 if offset is None else offset
|
2022-03-12 02:52:12 +05:30
|
|
|
|
selection_ = None if selection is None else \
|
2022-12-14 00:02:19 +05:30
|
|
|
|
np.setdiff1d(self.material,selection) if invert_selection else \
|
|
|
|
|
np.intersect1d(self.material,selection)
|
2022-12-10 12:52:22 +05:30
|
|
|
|
|
2022-11-09 00:22:08 +05:30
|
|
|
|
mask = ndimage.generic_filter(self.material,
|
|
|
|
|
tainted_neighborhood,
|
|
|
|
|
footprint=footprint,
|
|
|
|
|
mode='wrap' if periodic else 'nearest',
|
|
|
|
|
extra_keywords=dict(selection=selection_),
|
|
|
|
|
)
|
2020-08-08 23:12:34 +05:30
|
|
|
|
|
2023-11-28 03:11:28 +05:30
|
|
|
|
return GeomGrid(material = np.where(mask, self.material + offset_,self.material),
|
|
|
|
|
size = self.size,
|
|
|
|
|
origin = self.origin,
|
|
|
|
|
initial_conditions = self.initial_conditions,
|
|
|
|
|
comments = self.comments+[util.execution_stamp('GeomGrid','vicinity_offset')],
|
|
|
|
|
)
|
2020-11-28 00:46:06 +05:30
|
|
|
|
|
|
|
|
|
|
2022-01-26 20:55:27 +05:30
|
|
|
|
def get_grain_boundaries(self,
|
|
|
|
|
periodic: bool = True,
|
2022-01-31 17:10:29 +05:30
|
|
|
|
directions: Sequence[str] = 'xyz') -> VTK:
|
2020-11-18 16:55:08 +05:30
|
|
|
|
"""
|
2020-11-28 00:46:06 +05:30
|
|
|
|
Create VTK unstructured grid containing grain boundaries.
|
2020-11-18 16:44:12 +05:30
|
|
|
|
|
|
|
|
|
Parameters
|
|
|
|
|
----------
|
2022-01-13 03:43:38 +05:30
|
|
|
|
periodic : bool, optional
|
2020-12-04 11:42:18 +05:30
|
|
|
|
Assume grid to be periodic. Defaults to True.
|
2022-01-30 03:46:57 +05:30
|
|
|
|
directions : (sequence of) {'x', 'y', 'z'}, optional
|
2020-12-04 11:42:18 +05:30
|
|
|
|
Direction(s) along which the boundaries are determined.
|
2022-01-30 03:46:57 +05:30
|
|
|
|
Defaults to 'xyz'.
|
2020-11-18 16:44:12 +05:30
|
|
|
|
|
2021-04-25 11:17:00 +05:30
|
|
|
|
Returns
|
|
|
|
|
-------
|
|
|
|
|
grain_boundaries : damask.VTK
|
|
|
|
|
VTK-based geometry of grain boundary network.
|
|
|
|
|
|
2020-11-18 16:44:12 +05:30
|
|
|
|
"""
|
2022-01-29 22:46:19 +05:30
|
|
|
|
if not set(directions).issubset(valid := ['x', 'y', 'z']):
|
2022-02-22 21:12:05 +05:30
|
|
|
|
raise ValueError(f'invalid direction "{set(directions).difference(valid)}" specified')
|
2020-11-19 00:40:04 +05:30
|
|
|
|
|
2020-12-04 02:28:24 +05:30
|
|
|
|
o = [[0, self.cells[0]+1, np.prod(self.cells[:2]+1)+self.cells[0]+1, np.prod(self.cells[:2]+1)],
|
|
|
|
|
[0, np.prod(self.cells[:2]+1), np.prod(self.cells[:2]+1)+1, 1],
|
|
|
|
|
[0, 1, self.cells[0]+1+1, self.cells[0]+1]] # offset for connectivity
|
2020-11-30 01:20:41 +05:30
|
|
|
|
|
2020-11-28 00:46:06 +05:30
|
|
|
|
connectivity = []
|
|
|
|
|
for i,d in enumerate(['x','y','z']):
|
2020-11-30 01:20:41 +05:30
|
|
|
|
if d not in directions: continue
|
2020-11-28 00:46:06 +05:30
|
|
|
|
mask = self.material != np.roll(self.material,1,i)
|
|
|
|
|
for j in [0,1,2]:
|
|
|
|
|
mask = np.concatenate((mask,np.take(mask,[0],j)*(i==j)),j)
|
|
|
|
|
if i == 0 and not periodic: mask[0,:,:] = mask[-1,:,:] = False
|
|
|
|
|
if i == 1 and not periodic: mask[:,0,:] = mask[:,-1,:] = False
|
|
|
|
|
if i == 2 and not periodic: mask[:,:,0] = mask[:,:,-1] = False
|
2020-11-30 01:20:41 +05:30
|
|
|
|
|
2020-11-28 00:46:06 +05:30
|
|
|
|
base_nodes = np.argwhere(mask.flatten(order='F')).reshape(-1,1)
|
|
|
|
|
connectivity.append(np.block([base_nodes + o[i][k] for k in range(4)]))
|
2020-11-30 01:20:41 +05:30
|
|
|
|
|
2020-12-04 03:30:49 +05:30
|
|
|
|
coords = grid_filters.coordinates0_node(self.cells,self.size,self.origin).reshape(-1,3,order='F')
|
2020-11-30 01:20:41 +05:30
|
|
|
|
return VTK.from_unstructured_grid(coords,np.vstack(connectivity),'QUAD')
|