added IC setter/getter; explicit init of returned Grids

This commit is contained in:
Philip Eisenlohr 2022-03-24 17:22:18 -04:00
parent 46259d983d
commit 7f23f7b5b6
1 changed files with 50 additions and 23 deletions

View File

@ -4,7 +4,7 @@ import warnings
import multiprocessing as mp import multiprocessing as mp
from functools import partial from functools import partial
import typing import typing
from typing import Union, Optional, TextIO, List, Sequence from typing import Union, Optional, TextIO, List, Sequence, Dict
from pathlib import Path from pathlib import Path
import numpy as np import numpy as np
@ -34,7 +34,7 @@ class Grid:
material: np.ndarray, material: np.ndarray,
size: FloatSequence, size: FloatSequence,
origin: FloatSequence = np.zeros(3), origin: FloatSequence = np.zeros(3),
initial_conditions = None, initial_conditions: Dict[str,np.ndarray] = None,
comments: Union[str, Sequence[str]] = None): comments: Union[str, Sequence[str]] = None):
""" """
New geometry definition for grid solvers. New geometry definition for grid solvers.
@ -55,10 +55,10 @@ class Grid:
""" """
self.material = material self.material = material
self.size = size # type: ignore self.size = size # type: ignore
self.origin = origin # type: ignore self.origin = origin # type: ignore
self.ic = initial_conditions if initial_conditions is not None else {} self.initial_conditions = {} if initial_conditions is None else initial_conditions
self.comments = [] if comments is None else comments # type: ignore self.comments = [] if comments is None else comments # type: ignore
def __repr__(self) -> str: def __repr__(self) -> str:
"""Give short human-readable summary.""" """Give short human-readable summary."""
@ -71,7 +71,7 @@ class Grid:
f'origin: {util.srepr(self.origin," ")} m', f'origin: {util.srepr(self.origin," ")} m',
f'# materials: {mat_N}' + ('' if mat_min == 0 and mat_max+1 == mat_N else f'# materials: {mat_N}' + ('' if mat_min == 0 and mat_max+1 == mat_N else
f' (min: {mat_min}, max: {mat_max})') f' (min: {mat_min}, max: {mat_max})')
]+(['initial_conditions:']+[f' - {f}' for f in self.ic.keys()] if self.ic else [])) ]+(['initial_conditions:']+[f' - {f}' for f in self.initial_conditions] if self.initial_conditions else []))
def __copy__(self) -> 'Grid': def __copy__(self) -> 'Grid':
@ -146,6 +146,19 @@ class Grid:
self._origin = np.array(origin) self._origin = np.array(origin)
@property
def initial_conditions(self) -> Dict[str,np.ndarray]:
"""Fields of initial conditions."""
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
@property @property
def comments(self) -> List[str]: def comments(self) -> List[str]:
"""Comments, e.g. history of operations.""" """Comments, e.g. history of operations."""
@ -195,11 +208,12 @@ class Grid:
return Grid(material = v.get('material').reshape(cells,order='F'), return Grid(material = v.get('material').reshape(cells,order='F'),
size = bbox[1] - bbox[0], size = bbox[1] - bbox[0],
origin = bbox[0], origin = bbox[0],
initial_conditions = ic,
comments = comments, comments = comments,
initial_conditions = ic) )
@typing. no_type_check @typing.no_type_check
@staticmethod @staticmethod
def load_ASCII(fname)-> 'Grid': def load_ASCII(fname)-> 'Grid':
""" """
@ -269,7 +283,10 @@ class Grid:
if not np.any(np.mod(material,1) != 0.0): # no float present if not np.any(np.mod(material,1) != 0.0): # no float present
material = material.astype(int) - (1 if material.min() > 0 else 0) material = material.astype(int) - (1 if material.min() > 0 else 0)
return Grid(material.reshape(cells,order='F'),size,origin,comments) return Grid(material = material.reshape(cells,order='F'),
size = size,
origin = origin,
comments = comments)
@staticmethod @staticmethod
@ -306,9 +323,11 @@ class Grid:
cells = np.array(v.vtk_data.GetDimensions())-1 cells = np.array(v.vtk_data.GetDimensions())-1
bbox = np.array(v.vtk_data.GetBounds()).reshape(3,2).T bbox = np.array(v.vtk_data.GetBounds()).reshape(3,2).T
return Grid(v.get('MaterialId').reshape(cells,order='F').astype('int32',casting='unsafe') - 1, return Grid(material = v.get('MaterialId').reshape(cells,order='F').astype('int32',casting='unsafe') - 1,
bbox[1] - bbox[0], bbox[0], size = bbox[1] - bbox[0],
util.execution_stamp('Grid','load_Neper')) origin = bbox[0],
comments = util.execution_stamp('Grid','load_Neper'),
)
@staticmethod @staticmethod
@ -371,7 +390,11 @@ class Grid:
else: else:
ma = f['/'.join([b,c,feature_IDs])][()].flatten() ma = f['/'.join([b,c,feature_IDs])][()].flatten()
return Grid(ma.reshape(cells,order='F'),size,origin,util.execution_stamp('Grid','load_DREAM3D')) return Grid(material = ma.reshape(cells,order='F'),
size = size,
origin = origin,
comments = util.execution_stamp('Grid','load_DREAM3D'),
)
@staticmethod @staticmethod
@ -406,7 +429,11 @@ class Grid:
ma = np.arange(cells.prod()) if len(unique) == cells.prod() else \ ma = np.arange(cells.prod()) if len(unique) == cells.prod() else \
np.arange(unique.size)[np.argsort(pd.unique(unique_inverse))][unique_inverse] np.arange(unique.size)[np.argsort(pd.unique(unique_inverse))][unique_inverse]
return Grid(ma.reshape(cells,order='F'),size,origin,util.execution_stamp('Grid','from_table')) return Grid(material = ma.reshape(cells,order='F'),
size = size,
origin = origin,
comments = util.execution_stamp('Grid','from_table'),
)
@staticmethod @staticmethod
@ -665,7 +692,7 @@ class Grid:
""" """
v = VTK.from_image_data(self.cells,self.size,self.origin)\ v = VTK.from_image_data(self.cells,self.size,self.origin)\
.add('material',self.material.flatten(order='F')) .add('material',self.material.flatten(order='F'))
for label,data in self.ic.items(): for label,data in self.initial_conditions.items():
v = v.add(label,data.flatten(order='F')) v = v.add(label,data.flatten(order='F'))
v.comments = self.comments v.comments = self.comments
@ -929,13 +956,13 @@ class Grid:
""" """
return Grid(material = ndimage.interpolation.zoom( return Grid(material = ndimage.interpolation.zoom(
self.material, self.material,
cells/self.cells, cells/self.cells,
output=self.material.dtype, output=self.material.dtype,
order=0, order=0,
mode='wrap' if periodic else 'nearest', mode='wrap' if periodic else 'nearest',
prefilter=False prefilter=False
), ),
size = self.size, size = self.size,
origin = self.origin, origin = self.origin,
comments = self.comments+[util.execution_stamp('Grid','scale')], comments = self.comments+[util.execution_stamp('Grid','scale')],