Commit 07604102 authored by Daniel Boeckenhoff's avatar Daniel Boeckenhoff
Browse files

mesh3D.cut - before the idea to manipulate fields in _cut_sympy to append the...

mesh3D.cut - before the idea to manipulate fields in _cut_sympy to append the 'scalarMap' and remove it in the end.
parent 06041647
...@@ -473,9 +473,13 @@ class Tensors(AbstractNdarray): ...@@ -473,9 +473,13 @@ class Tensors(AbstractNdarray):
bases.append(t.coordSys) bases.append(t.coordSys)
except AttributeError: except AttributeError:
pass pass
# get most frequent coordSys if bases:
coordSys = sorted(bases, key=Counter(bases).get, reverse=True)[0] # get most frequent coordSys
kwargs['coordSys'] = coordSys coordSys = sorted(bases, key=Counter(bases).get, reverse=True)[0]
kwargs['coordSys'] = coordSys
else:
default = cls.__slot_defaults__[cls.__slots__.index('coordSys')]
kwargs['coordSys'] = default
''' transform all raw inputs to cls type with correct coordSys. Also ''' transform all raw inputs to cls type with correct coordSys. Also
automatically make a copy of those instances that are of the correct automatically make a copy of those instances that are of the correct
...@@ -1235,6 +1239,25 @@ class TensorFields(Tensors): ...@@ -1235,6 +1239,25 @@ class TensorFields(Tensors):
for i, field in enumerate(item.fields): for i, field in enumerate(item.fields):
self.fields[i].__setitem__(index, field) self.fields[i].__setitem__(index, field)
@classmethod
def merged(cls, *objects, **kwargs):
if not all([isinstance(o, cls) for o in objects]):
# TODO: could allow if all faceScalars are none
raise TypeError("Merge constructor only accepts {cls} instances."
.format(**locals()))
inst = super(TensorFields, cls).merged(*objects, **kwargs)
fields = []
if all([len(obj.fields) == len(objects[0].fields)
for obj in objects]):
for fld_idx in range(len(objects[0].fields)):
field = tfields.Tensors.merged(*[obj.fields[fld_idx]
for obj in objects])
fields.append(field)
inst = cls.__new__(cls, inst, *fields)
return inst
def transform(self, coordSys): def transform(self, coordSys):
super(TensorFields, self).transform(coordSys) super(TensorFields, self).transform(coordSys)
for field in self.fields: for field in self.fields:
...@@ -1377,10 +1400,10 @@ class TensorMaps(TensorFields): ...@@ -1377,10 +1400,10 @@ class TensorMaps(TensorFields):
item = super(TensorMaps, self).__getitem__(index) item = super(TensorMaps, self).__getitem__(index)
try: try:
if issubclass(type(item), TensorMaps): if issubclass(type(item), TensorMaps):
item.maps = [mp.copy() for mp in item.maps]
if isinstance(index, tuple): if isinstance(index, tuple):
index = index[0] index = index[0]
if item.maps: if item.maps:
item.maps = [mp.copy() for mp in item.maps]
indices = np.array(range(len(self))) indices = np.array(range(len(self)))
keep_indices = indices[index] keep_indices = indices[index]
if isinstance(keep_indices, int): if isinstance(keep_indices, int):
......
import numpy as np import numpy as np
def ensure_complex(*base_vectors):
# ensure, that the third entry in base_vector of type tuple becomes a complex type
base_vectors = list(base_vectors)
for i, vector in enumerate(base_vectors):
if isinstance(vector, tuple):
if len(vector) == 3:
vector = list(vector)
vector[2] = complex(vector[2])
base_vectors[i] = tuple(vector)
return base_vectors
def to_base_vectors(*base_vectors):
"""
Transform tuples to arrays with np.mgrid
Args:
tuple of lenght 3 with complex third entry -> start, stop, n_steps
Returns:
list if np.array for each base
"""
base_vectors = list(base_vectors)
# transform tuples to arrays with mgrid
for i in range(len(base_vectors)):
if isinstance(base_vectors[i], tuple):
base_vectors[i] = np.mgrid[slice(*base_vectors[i])]
if isinstance(base_vectors[i], list):
base_vectors[i] = np.array(base_vectors[i])
return base_vectors
def igrid(*base_vectors, **kwargs): def igrid(*base_vectors, **kwargs):
""" """
Args: Args:
...@@ -55,15 +85,7 @@ def igrid(*base_vectors, **kwargs): ...@@ -55,15 +85,7 @@ def igrid(*base_vectors, **kwargs):
""" """
iter_order = kwargs.pop('iter_order', np.arange(len(base_vectors))) iter_order = kwargs.pop('iter_order', np.arange(len(base_vectors)))
base_vectors = ensure_complex(*base_vectors)
# ensure, that the third entry in base_vector of type tuple becomes a complex type
base_vectors = list(base_vectors)
for i, vector in enumerate(base_vectors):
if isinstance(vector, tuple):
if len(vector) == 3:
vector = list(vector)
vector[2] = complex(vector[2])
base_vectors[i] = tuple(vector)
# create the grid # create the grid
if all([isinstance(val, tuple) for val in base_vectors]): if all([isinstance(val, tuple) for val in base_vectors]):
...@@ -71,13 +93,7 @@ def igrid(*base_vectors, **kwargs): ...@@ -71,13 +93,7 @@ def igrid(*base_vectors, **kwargs):
obj = np.mgrid[base_vectors] obj = np.mgrid[base_vectors]
obj = obj.reshape(obj.shape[0], -1).T obj = obj.reshape(obj.shape[0], -1).T
else: else:
base_vectors = list(base_vectors) base_vectors = to_base_vectors(*base_vectors)
# transform tuples to arrays with mgrid
for i in range(len(base_vectors)):
if isinstance(base_vectors[i], tuple):
base_vectors[i] = np.mgrid[slice(*base_vectors[i])]
if isinstance(base_vectors[i], list):
base_vectors[i] = np.array(base_vectors[i])
obj = np.empty(shape=(reduce(lambda x, y: x * y, obj = np.empty(shape=(reduce(lambda x, y: x * y,
map(len, base_vectors)), map(len, base_vectors)),
...@@ -95,11 +111,12 @@ def igrid(*base_vectors, **kwargs): ...@@ -95,11 +111,12 @@ def igrid(*base_vectors, **kwargs):
return i return i
loop_rec([base_vectors[i] for i in iter_order], len(base_vectors)) loop_rec([base_vectors[i] for i in iter_order], len(base_vectors))
swap_indices = compare_permutations(iter_order, np.arange(len(base_vectors))) swap_indices = compare_permutations(iter_order, np.arange(len(base_vectors)))
swap_columns(obj, *swap_indices) swap_columns(obj, *swap_indices)
return obj return obj
def swap_columns(array, *index_tuples): def swap_columns(array, *index_tuples):
""" """
Args: Args:
......
This diff is collapsed.
...@@ -87,10 +87,13 @@ class Triangles3D(tfields.TensorFields): ...@@ -87,10 +87,13 @@ class Triangles3D(tfields.TensorFields):
if start % 3 == 0 and (stop - start) % 3 == 0 and step is None: if start % 3 == 0 and (stop - start) % 3 == 0 and step is None:
tri_index = slice(start // 3, stop // 3) tri_index = slice(start // 3, stop // 3)
else: else:
tri_index = self._to_triangles_mask(index) try:
tri_index = self._to_triangles_mask(index)
except ValueError as err:
pass
# apply triangle index to fields # apply triangle index to fields
if not tri_index is None: if tri_index is not None:
item.fields = [field.__getitem__(tri_index) item.fields = [field.__getitem__(tri_index)
for field in item.fields] for field in item.fields]
else: else:
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment