Skip to content
GitLab
Projects
Groups
Snippets
/
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
Menu
Open sidebar
Daniel Boeckenhoff
tfields
Commits
687fae33
Commit
687fae33
authored
Jul 20, 2018
by
Daniel Boeckenhoff
Browse files
renamed coordSys to coord_sys
parent
df704e78
Changes
8
Hide whitespace changes
Inline
Side-by-side
test/test_core.py
View file @
687fae33
...
...
@@ -51,7 +51,7 @@ class Base_Test(object):
merge_list
[
1
].
transform
(
tfields
.
bases
.
CYLINDER
)
merge_list
[
2
].
transform
(
tfields
.
bases
.
SPHERICAL
)
obj
=
type
(
self
.
_inst
).
merged
(
*
merge_list
)
self
.
assertTrue
(
obj
.
coord
S
ys
==
tfields
.
bases
.
CARTESIAN
)
self
.
assertTrue
(
obj
.
coord
_s
ys
==
tfields
.
bases
.
CARTESIAN
)
for
i
in
range
(
len
(
merge_list
)):
value
=
np
.
allclose
(
merge_list
[
0
],
obj
[
i
*
len
(
self
.
_inst
):
(
i
+
1
)
*
...
...
@@ -59,7 +59,7 @@ class Base_Test(object):
atol
=
ATOL
)
self
.
assertTrue
(
value
)
obj_cs
=
type
(
self
.
_inst
).
merged
(
*
merge_list
,
coord
S
ys
=
tfields
.
bases
.
CYLINDER
)
obj_cs
=
type
(
self
.
_inst
).
merged
(
*
merge_list
,
coord
_s
ys
=
tfields
.
bases
.
CYLINDER
)
for
i
in
range
(
len
(
merge_list
)):
value
=
np
.
allclose
(
merge_list
[
1
],
obj_cs
[
i
*
len
(
self
.
_inst
):
(
i
+
1
)
*
...
...
@@ -99,7 +99,7 @@ class TensorFields_Empty_Test(Tensors_Empty_Test):
class
TensorFields_Copy_Test
(
TensorFields_Empty_Test
):
def
setUp
(
self
):
base
=
[(
-
5
,
5
,
11
)]
*
3
self
.
_fields
=
[
tfields
.
Tensors
.
grid
(
*
base
,
coord
S
ys
=
'cylinder'
),
self
.
_fields
=
[
tfields
.
Tensors
.
grid
(
*
base
,
coord
_s
ys
=
'cylinder'
),
tfields
.
Tensors
(
range
(
11
**
3
))]
tensors
=
tfields
.
Tensors
.
grid
(
*
base
)
self
.
_inst
=
tfields
.
TensorFields
(
tensors
,
*
self
.
_fields
)
...
...
@@ -116,7 +116,7 @@ class TensorMaps_Copy_Test(TensorMaps_Empty_Test):
def
setUp
(
self
):
base
=
[(
-
1
,
1
,
3
)]
*
3
tensors
=
tfields
.
Tensors
.
grid
(
*
base
)
self
.
_fields
=
[
tfields
.
Tensors
.
grid
(
*
base
,
coord
S
ys
=
'cylinder'
),
self
.
_fields
=
[
tfields
.
Tensors
.
grid
(
*
base
,
coord
_s
ys
=
'cylinder'
),
tfields
.
Tensors
(
range
(
len
(
tensors
)))]
self
.
_maps_tensors
=
[[[
0
,
0
,
0
],
[
1
,
2
,
3
],
...
...
tfields/__init__.py
View file @
687fae33
__version__
=
'0.1.0.dev
3
'
__version__
=
'0.1.0.dev
4
'
__author__
=
"Daniel Boeckenhoff"
__email__
=
"daniel.boeckenhoff@ipp.mpg.de"
...
...
tfields/bases/bases.py
View file @
687fae33
...
...
@@ -27,7 +27,7 @@ def get_coord_system(base):
if
not
isinstance
(
base
,
sympy
.
diffgeom
.
CoordSystem
):
bse_tpe
=
type
(
base
)
expctd_tpe
=
type
(
sympy
.
diffgeom
.
CoordSystem
)
raise
TypeError
(
"Wrong type of coord
S
ystem base {bse_tpe}. "
raise
TypeError
(
"Wrong type of coord
_s
ystem base {bse_tpe}. "
"Expected {expctd_tpe}"
.
format
(
**
locals
()))
return
base
...
...
tfields/core.py
View file @
687fae33
...
...
@@ -124,7 +124,7 @@ class AbstractNdarray(np.ndarray):
>>> from tfields import Tensors, TensorFields
>>> scalars = Tensors([0, 1, 2])
>>> vectors = Tensors([[0, 0, 0], [0, 0, 1], [0, -1, 0]])
>>> scalarField = TensorFields(vectors, scalars, coord
S
ys='cylinder')
>>> scalarField = TensorFields(vectors, scalars, coord
_s
ys='cylinder')
Save it and restore it
>>> outFile = NamedTemporaryFile(suffix='.pickle')
...
...
@@ -134,7 +134,7 @@ class AbstractNdarray(np.ndarray):
>>> _ = outFile.seek(0)
>>> sf = pickle.load(outFile)
>>> sf.coord
S
ys == 'cylinder'
>>> sf.coord
_s
ys == 'cylinder'
True
>>> sf.fields[0][2] == 2.
True
...
...
@@ -269,7 +269,7 @@ class AbstractNdarray(np.ndarray):
else
:
kwargs
[
attr
]
=
getattr
(
self
,
attr
)
np
.
savez
(
path
,
self
,
**
kwargs
)
np
.
savez
(
path
,
self
,
**
kwargs
)
@
classmethod
def
_load_npz
(
cls
,
path
,
**
load_kwargs
):
...
...
@@ -305,7 +305,7 @@ class Tensors(AbstractNdarray):
True
>>> vectors.dim == 3
True
>>> assert vectors.coord
S
ys == 'cartesian'
>>> assert vectors.coord
_s
ys == 'cartesian'
Initialize the Levi-Zivita Tensor
>>> matrices = tfields.Tensors([[[0, 0, 0], [0, 0, 1], [0, -1, 0]],
...
...
@@ -319,10 +319,10 @@ class Tensors(AbstractNdarray):
True
Initializing in different start coordinate system
>>> cyl = tfields.Tensors([[5, np.arctan(4. / 3.), 42]], coord
S
ys='cylinder')
>>> assert cyl.coord
S
ys == 'cylinder'
>>> cyl = tfields.Tensors([[5, np.arctan(4. / 3.), 42]], coord
_s
ys='cylinder')
>>> assert cyl.coord
_s
ys == 'cylinder'
>>> cyl.transform('cartesian')
>>> assert cyl.coord
S
ys == 'cartesian'
>>> assert cyl.coord
_s
ys == 'cartesian'
>>> cart = cyl
>>> assert round(cart[0, 0], 10) == 3.
>>> assert round(cart[0, 1], 10) == 4.
...
...
@@ -331,8 +331,8 @@ class Tensors(AbstractNdarray):
Initialize with copy constructor keeps the coordinate system
>>> with vectors.tmp_transform('cylinder'):
... vect_cyl = tfields.Tensors(vectors)
... assert vect_cyl.coord
S
ys == vectors.coord
S
ys
>>> assert vect_cyl.coord
S
ys == 'cylinder'
... assert vect_cyl.coord
_s
ys == vectors.coord
_s
ys
>>> assert vect_cyl.coord
_s
ys == 'cylinder'
You can demand a special dimension.
>>> _ = tfields.Tensors([[1, 2, 3]], dim=3)
...
...
@@ -351,7 +351,7 @@ class Tensors(AbstractNdarray):
Tensors([], shape=(0, 7), dtype=float64)
"""
__slots__
=
[
'coord
S
ys'
]
__slots__
=
[
'coord
_s
ys'
]
__slot_defaults__
=
[
'cartesian'
]
__slot_setters__
=
[
tfields
.
bases
.
get_coord_system_name
]
...
...
@@ -364,10 +364,10 @@ class Tensors(AbstractNdarray):
if
issubclass
(
type
(
tensors
),
Tensors
):
if
dim
is
not
None
:
dim
=
tensors
.
dim
coord
S
ys
=
kwargs
.
pop
(
'coord
S
ys'
,
tensors
.
coord
S
ys
)
coord
_s
ys
=
kwargs
.
pop
(
'coord
_s
ys'
,
tensors
.
coord
_s
ys
)
tensors
=
tensors
.
copy
()
tensors
.
transform
(
coord
S
ys
)
kwargs
[
'coord
S
ys'
]
=
coord
S
ys
tensors
.
transform
(
coord
_s
ys
)
kwargs
[
'coord
_s
ys'
]
=
coord
_s
ys
if
dtype
is
None
:
dtype
=
tensors
.
dtype
else
:
...
...
@@ -436,10 +436,10 @@ class Tensors(AbstractNdarray):
Use of most frequent coordinate system
>>> vec_a = tfields.Tensors([[0, 0, 0], [0, 0, 1], [0, -1, 0]])
>>> vec_b = tfields.Tensors([[5, 4, 1]], coord
S
ys=tfields.bases.cylinder)
>>> vec_c = tfields.Tensors([[4, 2, 3]], coord
S
ys=tfields.bases.cylinder)
>>> vec_b = tfields.Tensors([[5, 4, 1]], coord
_s
ys=tfields.bases.cylinder)
>>> vec_c = tfields.Tensors([[4, 2, 3]], coord
_s
ys=tfields.bases.cylinder)
>>> merge = tfields.Tensors.merged(vec_a, vec_b, vec_c, [[2, 0, 1]])
>>> assert merge.coord
S
ys == 'cylinder'
>>> assert merge.coord
_s
ys == 'cylinder'
>>> assert merge.equal([[0, 0, 0],
... [0, 0, 1],
... [1, -np.pi / 2, 0],
...
...
@@ -450,42 +450,42 @@ class Tensors(AbstractNdarray):
Merge also shifts the maps to still refer to the same tensors
>>> tm_a = tfields.TensorMaps(merge, maps=[[[0, 1, 2]]])
>>> tm_b = tm_a.copy()
>>> assert tm_a.coord
S
ys == 'cylinder'
>>> assert tm_a.coord
_s
ys == 'cylinder'
>>> tm_merge = tfields.TensorMaps.merged(tm_a, tm_b)
>>> assert tm_merge.coord
S
ys == 'cylinder'
>>> assert tm_merge.coord
_s
ys == 'cylinder'
>>> assert tm_merge.maps[0].equal([[0, 1, 2],
... list(range(len(merge),
... len(merge) + 3,
... 1))])
>>> obj_list = [tfields.Tensors([[1, 2, 3]], coord
S
ys=tfields.bases.CYLINDER),
>>> obj_list = [tfields.Tensors([[1, 2, 3]], coord
_s
ys=tfields.bases.CYLINDER),
... tfields.Tensors([[3] * 3]),
... tfields.Tensors([[5, 1, 3]])]
>>> merge2 = tfields.Tensors.merged(*obj_list, coord
S
ys=tfields.bases.CARTESIAN)
>>> merge2 = tfields.Tensors.merged(*obj_list, coord
_s
ys=tfields.bases.CARTESIAN)
>>> assert merge2.equal([[-0.41614684, 0.90929743, 3.],
... [3, 3, 3], [5, 1, 3]], atol=1e-8)
"""
''' get most frequent coord
S
ys or predefined coord
S
ys '''
coord
S
ys
=
kwargs
.
get
(
'coord
S
ys'
,
None
)
''' get most frequent coord
_s
ys or predefined coord
_s
ys '''
coord
_s
ys
=
kwargs
.
get
(
'coord
_s
ys'
,
None
)
dimension
=
kwargs
.
get
(
'dim'
,
None
)
if
coord
S
ys
is
None
:
if
coord
_s
ys
is
None
:
bases
=
[]
for
t
in
objects
:
try
:
bases
.
append
(
t
.
coord
S
ys
)
bases
.
append
(
t
.
coord
_s
ys
)
except
AttributeError
:
pass
if
bases
:
# get most frequent coord
S
ys
coord
S
ys
=
sorted
(
bases
,
key
=
Counter
(
bases
).
get
,
reverse
=
True
)[
0
]
kwargs
[
'coord
S
ys'
]
=
coord
S
ys
# get most frequent coord
_s
ys
coord
_s
ys
=
sorted
(
bases
,
key
=
Counter
(
bases
).
get
,
reverse
=
True
)[
0
]
kwargs
[
'coord
_s
ys'
]
=
coord
_s
ys
else
:
default
=
cls
.
__slot_defaults__
[
cls
.
__slots__
.
index
(
'coord
S
ys'
)]
kwargs
[
'coord
S
ys'
]
=
default
default
=
cls
.
__slot_defaults__
[
cls
.
__slots__
.
index
(
'coord
_s
ys'
)]
kwargs
[
'coord
_s
ys'
]
=
default
''' transform all raw inputs to cls type with correct coord
S
ys. Also
''' transform all raw inputs to cls type with correct coord
_s
ys. Also
automatically make a copy of those instances that are of the correct
type already.'''
objects
=
[
cls
.
__new__
(
cls
,
t
,
**
kwargs
)
for
t
in
objects
]
...
...
@@ -584,10 +584,10 @@ class Tensors(AbstractNdarray):
"""
return
dim
(
self
)
def
transform
(
self
,
coord
S
ys
):
def
transform
(
self
,
coord
_s
ys
):
"""
Args:
coord
S
ys (str)
coord
_s
ys (str)
Examples:
>>> import numpy as np
...
...
@@ -618,7 +618,7 @@ class Tensors(AbstractNdarray):
>>> tCart = tfields.Tensors([[3, 4, 42], [1, 0, 0], [0, 1, -1], [-1, 0, 1], [0, 0, 0]])
>>> t_cyl = tCart.copy()
>>> t_cyl.transform('cylinder')
>>> assert t_cyl.coord
S
ys == 'cylinder'
>>> assert t_cyl.coord
_s
ys == 'cylinder'
R
>>> assert t_cyl[0, 0] == 5
...
...
@@ -637,40 +637,40 @@ class Tensors(AbstractNdarray):
>>> assert t_cyl[2, 2] == -1
>>> t_cyl.transform('cartesian')
>>> assert t_cyl.coord
S
ys == 'cartesian'
>>> assert t_cyl.coord
_s
ys == 'cartesian'
>>> assert t_cyl[0, 0] == 3
"""
# scalars empty already there
if
self
.
rank
==
0
or
self
.
shape
[
0
]
==
0
or
self
.
coord
S
ys
==
coord
S
ys
:
self
.
coord
S
ys
=
coord
S
ys
if
self
.
rank
==
0
or
self
.
shape
[
0
]
==
0
or
self
.
coord
_s
ys
==
coord
_s
ys
:
self
.
coord
_s
ys
=
coord
_s
ys
return
tfields
.
bases
.
transform
(
self
,
self
.
coord
S
ys
,
coord
S
ys
)
# self[:] = tfields.bases.transform(self, self.coord
S
ys, coord
S
ys)
self
.
coord
S
ys
=
coord
S
ys
tfields
.
bases
.
transform
(
self
,
self
.
coord
_s
ys
,
coord
_s
ys
)
# self[:] = tfields.bases.transform(self, self.coord
_s
ys, coord
_s
ys)
self
.
coord
_s
ys
=
coord
_s
ys
@
contextmanager
def
tmp_transform
(
self
,
coord
S
ys
):
def
tmp_transform
(
self
,
coord
_s
ys
):
"""
Temporarily change the coord
S
ys to another coord
S
ys and change it back at exit
Temporarily change the coord
_s
ys to another coord
_s
ys and change it back at exit
This method is for cleaner code only.
No speed improvements go with this.
Args:
see transform
Examples:
>>> import tfields
>>> p = tfields.Tensors([[1,2,3]], coord
S
ys=tfields.bases.SPHERICAL)
>>> p = tfields.Tensors([[1,2,3]], coord
_s
ys=tfields.bases.SPHERICAL)
>>> with p.tmp_transform(tfields.bases.CYLINDER):
... assert p.coord
S
ys == tfields.bases.CYLINDER
>>> assert p.coord
S
ys == tfields.bases.SPHERICAL
... assert p.coord
_s
ys == tfields.bases.CYLINDER
>>> assert p.coord
_s
ys == tfields.bases.SPHERICAL
"""
baseBefore
=
self
.
coord
S
ys
if
baseBefore
==
coord
S
ys
:
baseBefore
=
self
.
coord
_s
ys
if
baseBefore
==
coord
_s
ys
:
yield
else
:
self
.
transform
(
coord
S
ys
)
self
.
transform
(
coord
_s
ys
)
yield
...
...
@@ -716,7 +716,7 @@ class Tensors(AbstractNdarray):
def
to_segment
(
self
,
segment
,
num_segments
,
coordinate
,
periodicity
=
2
*
np
.
pi
,
offset
=
0.
,
coord
S
ys
=
None
):
coord
_s
ys
=
None
):
"""
For circular (close into themself after
<periodicity>) coordinates at index <coordinate> assume
...
...
@@ -728,14 +728,14 @@ class Tensors(AbstractNdarray):
coordinate (int): coordinate index
periodicity (float): after what lenght, the coordiante repeats
offset (float): offset in the mapping
coord
S
ys (str or sympy.CoordinateSystem): in which coord sys the
coord
_s
ys (str or sympy.CoordinateSystem): in which coord sys the
transformation should be done
Examples:
>>> import tfields
>>> import numpy as np
>>> pStart = tfields.Points3D([[6, 2 * np.pi, 1],
... [6, 2 * np.pi / 5 * 3, 1]],
... coord
S
ys='cylinder')
... coord
_s
ys='cylinder')
>>> p = tfields.Points3D(pStart)
>>> p.to_segment(0, 5, 1, offset=-2 * np.pi / 10)
>>> assert np.array_equal(p[:, 1], [0, 0])
...
...
@@ -748,9 +748,9 @@ class Tensors(AbstractNdarray):
if
segment
>
num_segments
-
1
:
raise
ValueError
(
"Segment {0} not existent."
.
format
(
segment
))
if
coord
S
ys
is
None
:
coord
S
ys
=
self
.
coord
S
ys
with
self
.
tmp_transform
(
coord
S
ys
):
if
coord
_s
ys
is
None
:
coord
_s
ys
=
self
.
coord
_s
ys
with
self
.
tmp_transform
(
coord
_s
ys
):
# map all values to first segment
self
[:,
coordinate
]
=
\
(
self
[:,
coordinate
]
-
offset
)
%
(
periodicity
/
num_segments
)
+
\
...
...
@@ -768,9 +768,9 @@ class Tensors(AbstractNdarray):
equal_nan (bool)
see numpy.isclose
"""
if
issubclass
(
type
(
other
),
Tensors
)
and
self
.
coord
S
ys
!=
other
.
coord
S
ys
:
if
issubclass
(
type
(
other
),
Tensors
)
and
self
.
coord
_s
ys
!=
other
.
coord
_s
ys
:
other
=
other
.
copy
()
other
.
transform
(
self
.
coord
S
ys
)
other
.
transform
(
self
.
coord
_s
ys
)
x
,
y
=
np
.
asarray
(
self
),
np
.
asarray
(
other
)
if
rtol
is
None
and
atol
is
None
:
mask
=
(
x
==
y
)
...
...
@@ -854,7 +854,7 @@ class Tensors(AbstractNdarray):
array([2, 3, 4])
"""
with
other
.
tmp_transform
(
self
.
coord
S
ys
):
with
other
.
tmp_transform
(
self
.
coord
_s
ys
):
# balanced_tree option gives huge speedup!
kd_tree
=
sp
.
spatial
.
cKDTree
(
other
,
1000
,
balanced_tree
=
False
)
...
...
@@ -863,11 +863,11 @@ class Tensors(AbstractNdarray):
return
array
def
evalf
(
self
,
expression
=
None
,
coord
S
ys
=
None
):
def
evalf
(
self
,
expression
=
None
,
coord
_s
ys
=
None
):
"""
Args:
expression (sympy logical expression)
coord
S
ys (str): coord
S
ys to evalfuate the expression in.
coord
_s
ys (str): coord
_s
ys to evalfuate the expression in.
Returns:
np.ndarray: mask of dtype bool with lenght of number of points in self.
This array is True, where expression evalfuates True.
...
...
@@ -897,17 +897,17 @@ class Tensors(AbstractNdarray):
"""
coords
=
sympy
.
symbols
(
'x y z'
)
with
self
.
tmp_transform
(
coord
S
ys
or
self
.
coord
S
ys
):
with
self
.
tmp_transform
(
coord
_s
ys
or
self
.
coord
_s
ys
):
mask
=
tfields
.
evalf
(
self
,
expression
,
coords
=
coords
)
return
mask
def
cut
(
self
,
expression
,
coord
S
ys
=
None
):
def
cut
(
self
,
expression
,
coord
_s
ys
=
None
):
"""
Default cut method for Points3D. Works on a copy.
Args:
expression (sympy logical expression): logical expression which will be evalfuated.
use symbols x, y and z
coord
S
ys (str): coord
S
ys to evalfuate the expression in.
coord
_s
ys (str): coord
_s
ys to evalfuate the expression in.
Examples:
>>> import tfields
>>> import sympy
...
...
@@ -930,7 +930,7 @@ class Tensors(AbstractNdarray):
"""
if
len
(
self
)
==
0
:
return
self
.
copy
()
mask
=
self
.
evalf
(
expression
,
coord
S
ys
=
coord
S
ys
or
self
.
coord
S
ys
)
mask
=
self
.
evalf
(
expression
,
coord
_s
ys
=
coord
_s
ys
or
self
.
coord
_s
ys
)
mask
.
astype
(
bool
)
inst
=
self
[
mask
].
copy
()
return
inst
...
...
@@ -955,9 +955,9 @@ class Tensors(AbstractNdarray):
True
"""
if
issubclass
(
type
(
other
),
Tensors
)
and
self
.
coord
S
ys
!=
other
.
coord
S
ys
:
if
issubclass
(
type
(
other
),
Tensors
)
and
self
.
coord
_s
ys
!=
other
.
coord
_s
ys
:
other
=
other
.
copy
()
other
.
transform
(
self
.
coord
S
ys
)
other
.
transform
(
self
.
coord
_s
ys
)
return
sp
.
spatial
.
distance
.
cdist
(
self
,
other
,
**
kwargs
)
def
min_dists
(
self
,
other
=
None
,
**
kwargs
):
...
...
@@ -1139,10 +1139,10 @@ class TensorFields(Tensors):
>>> cp = TensorFields(vectorField)
>>> assert vectorField.equal(cp)
Copying takes care of coord
S
ys
Copying takes care of coord
_s
ys
>>> cp.transform(tfields.bases.CYLINDER)
>>> cp_cyl = TensorFields(cp)
>>> assert cp_cyl.coord
S
ys == tfields.bases.CYLINDER
>>> assert cp_cyl.coord
_s
ys == tfields.bases.CYLINDER
Copying with changing type
>>> tcp = TensorFields(vectorField, dtype=int)
...
...
@@ -1162,7 +1162,7 @@ class TensorFields(Tensors):
>>> assert len(loose) != 1
"""
__slots__
=
[
'coord
S
ys'
,
'fields'
]
__slots__
=
[
'coord
_s
ys'
,
'fields'
]
def
__new__
(
cls
,
tensors
,
*
fields
,
**
kwargs
):
rigid
=
kwargs
.
pop
(
'rigid'
,
True
)
...
...
@@ -1280,10 +1280,10 @@ class TensorFields(Tensors):
inst
=
cls
.
__new__
(
cls
,
inst
,
*
fields
)
return
inst
def
transform
(
self
,
coord
S
ys
):
super
(
TensorFields
,
self
).
transform
(
coord
S
ys
)
def
transform
(
self
,
coord
_s
ys
):
super
(
TensorFields
,
self
).
transform
(
coord
_s
ys
)
for
field
in
self
.
fields
:
field
.
transform
(
coord
S
ys
)
field
.
transform
(
coord
_s
ys
)
def
equal
(
self
,
other
,
**
kwargs
):
"""
...
...
@@ -1296,7 +1296,7 @@ class TensorFields(Tensors):
if
not
issubclass
(
type
(
other
),
Tensors
):
return
super
(
TensorFields
,
self
).
equal
(
other
,
**
kwargs
)
else
:
with
other
.
tmp_transform
(
self
.
coord
S
ys
):
with
other
.
tmp_transform
(
self
.
coord
_s
ys
):
mask
=
super
(
TensorFields
,
self
).
equal
(
other
,
**
kwargs
)
if
issubclass
(
type
(
other
),
TensorFields
):
if
len
(
self
.
fields
)
!=
len
(
other
.
fields
):
...
...
@@ -1325,7 +1325,7 @@ class TensorMaps(TensorFields):
tensors: see Tensors class
*fields (Tensors): see TensorFields class
**kwargs:
coord
S
ys ('str'): see Tensors class
coord
_s
ys ('str'): see Tensors class
maps (array-like): indices indicating a connection between the
tensors at the respective index positions
Examples:
...
...
@@ -1346,10 +1346,10 @@ class TensorMaps(TensorFields):
Copy constructor
>>> mesh_copy = tfields.TensorMaps(mesh)
Copying takes care of coord
S
ys
Copying takes care of coord
_s
ys
>>> mesh_copy.transform(tfields.bases.CYLINDER)
>>> mesh_cp_cyl = tfields.TensorMaps(mesh_copy)
>>> assert mesh_cp_cyl.coord
S
ys == tfields.bases.CYLINDER
>>> assert mesh_cp_cyl.coord
_s
ys == tfields.bases.CYLINDER
Raises:
>>> import tfields
...
...
@@ -1359,7 +1359,7 @@ class TensorMaps(TensorFields):
ValueError: Incorrect map rank 0
"""
__slots__
=
[
'coord
S
ys'
,
'fields'
,
'maps'
]
__slots__
=
[
'coord
_s
ys'
,
'fields'
,
'maps'
]
def
__new__
(
cls
,
tensors
,
*
fields
,
**
kwargs
):
maps
=
kwargs
.
pop
(
'maps'
,
[])
...
...
@@ -1506,7 +1506,7 @@ class TensorMaps(TensorFields):
if
not
issubclass
(
type
(
other
),
Tensors
):
return
super
(
TensorMaps
,
self
).
equal
(
other
,
**
kwargs
)
else
:
with
other
.
tmp_transform
(
self
.
coord
S
ys
):
with
other
.
tmp_transform
(
self
.
coord
_s
ys
):
mask
=
super
(
TensorMaps
,
self
).
equal
(
other
,
**
kwargs
)
if
issubclass
(
type
(
other
),
TensorMaps
):
if
len
(
self
.
maps
)
!=
len
(
other
.
maps
):
...
...
tfields/mask.py
View file @
687fae33
...
...
@@ -18,7 +18,7 @@ def evalf(array, cutExpression=None, coords=None):
Args:
array (numpy ndarray)
cutExpression (sympy logical expression)
coord
S
ys (str): coord
S
ys to evalfuate the expression in.
coord
_s
ys (str): coord
_s
ys to evalfuate the expression in.
Returns:
np.array: mask which is True, where cutExpression evalfuates True.
Examples:
...
...
tfields/mesh3D.py
View file @
687fae33
...
...
@@ -462,7 +462,7 @@ class Mesh3D(tfields.TensorMaps):
new_points
=
np
.
empty
((
0
,
3
))
new_faces
=
np
.
empty
((
0
,
3
))
new_fields
=
[
tfields
.
Tensors
(
np
.
empty
((
0
,)
+
field
.
shape
[
1
:]),
coord
S
ys
=
field
.
coord
S
ys
)
coord
_s
ys
=
field
.
coord
_s
ys
)
for
field
in
inst
.
fields
]
new_map_fields
=
[[]
for
field
in
inst
.
maps
[
0
].
fields
]
new_norm_vectors
=
[]
...
...
@@ -524,11 +524,11 @@ class Mesh3D(tfields.TensorMaps):
face_map
=
tfields
.
TensorFields
(
faces
,
*
faces_fields
,
dtype
=
int
,
coord
S
ys
=
inst
.
maps
[
0
].
coord
S
ys
)
coord
_s
ys
=
inst
.
maps
[
0
].
coord
_s
ys
)
inst
=
tfields
.
Mesh3D
(
vertices
,
*
fields
,
maps
=
[
face_map
]
+
inst
.
maps
[
1
:],
coord
S
ys
=
inst
.
coord
S
ys
)
coord
_s
ys
=
inst
.
coord
_s
ys
)
mask
=
np
.
full
(
len
(
inst
.
maps
[
0
]),
True
,
dtype
=
bool
)
for
face_idx
in
range
(
len
(
inst
.
maps
[
0
])):
if
face_idx
in
face_delete_indices
:
...
...
@@ -627,7 +627,7 @@ class Mesh3D(tfields.TensorMaps):
maps
=
maps
)
return
inst
def
cut
(
self
,
expression
,
coord
S
ys
=
None
,
at_intersection
=
None
,
def
cut
(
self
,
expression
,
coord
_s
ys
=
None
,
at_intersection
=
None
,
return_template
=
False
):
"""
cut method for Mesh3D.
...
...
@@ -642,7 +642,7 @@ class Mesh3D(tfields.TensorMaps):
We use the vertices and maps of the Mesh as the sceleton of
the returned mesh. The fields are mapped according to
indices in the template.maps[i].fields.
coord
S
ys (coordinate system to cut in):
coord
_s
ys (coordinate system to cut in):
at_intersection (str): instruction on what to do, when a cut will intersect a triangle.
Options: "remove" (Default)
"split" - Create new triangles that make up the old one.
...
...
@@ -713,7 +713,7 @@ class Mesh3D(tfields.TensorMaps):
* optional: template
"""
with
self
.
tmp_transform
(
coord
S
ys
or
self
.
coord
S
ys
):
with
self
.
tmp_transform
(
coord
_s
ys
or
self
.
coord
_s
ys
):
if
isinstance
(
expression
,
Mesh3D
):
obj
=
self
.
_cut_template
(
expression
)
else
:
...
...
tfields/points3D.py
View file @
687fae33
...
...
@@ -18,11 +18,11 @@ class Points3D(tfields.Tensors):
Args:
points3DInstance -> copy constructor
[points3DInstance1, points3DInstance2, ...] -> coord
S
ys are correctly treated
[points3DInstance1, points3DInstance2, ...] -> coord
_s
ys are correctly treated
list of coordinates (see examples)
Kwargs:
coord
S
ys (str):
coord
_s
ys (str):
Use tfields.bases.CARTESIAN -> x, y, z
Use tfields.bases.CYLINDER -> r, phi, z
Use tfields.bases.SPHERICAL -> r, phi, theta
...
...
@@ -55,11 +55,11 @@ class Points3D(tfields.Tensors):
[ 8.06225775, 1.05165021, -1. ],
[ 9.43398113, 1.01219701, 0. ]])
Copy constructor with one instance preserves coord
S
ys of instance
>>> assert tfields.Points3D(p2).coord
S
ys == p2.coord
S
ys
Copy constructor with one instance preserves coord
_s
ys of instance
>>> assert tfields.Points3D(p2).coord
_s
ys == p2.coord
_s
ys
Unless you specify other:
>>> tfields.Points3D(p2, coord
S
ys=tfields.bases.CARTESIAN)
>>> tfields.Points3D(p2, coord
_s
ys=tfields.bases.CARTESIAN)
Points3D([[ 1., 4., 1.],
[ 2., 5., 2.],
[ 3., 6., -6.],
...
...
@@ -83,7 +83,7 @@ class Points3D(tfields.Tensors):
>>> p1.transform(tfields.bases.CYLINDER)
... unless specified other. Here it is specified
>>> tfields.Points3D.merged(p1, p2, coord
S
ys=tfields.bases.CYLINDER)
>>> tfields.Points3D.merged(p1, p2, coord
_s
ys=tfields.bases.CYLINDER)
Points3D([[ 2.23606798, 1.10714872, 3. ],
[ 6.40312424, 0.89605538, 6. ],
[ 2.23606798, 1.10714872, -6. ],
...
...
@@ -127,7 +127,7 @@ class Points3D(tfields.Tensors):
... [7.071, -2.356, -5.],
... [1. , 0. , -1.],
... [1. , 1.571, -1.]],
... coord
S
ys=tfields.bases.CYLINDER),
... coord
_s
ys=tfields.bases.CYLINDER),
... atol=1e-3)
>>> p.transform(tfields.bases.CARTESIAN)
>>> assert p.equal(p_cart, atol=1e-15)
...
...
tfields/triangles3D.py
View file @
687fae33
...
...
@@ -124,7 +124,7 @@ class Triangles3D(tfields.TensorFields):
def
triangleScalars
(
self
,
scalars
):
self
.
fields
=
tfields
.
scalars_to_fields
(
scalars
)
def
evalf
(
self
,
expression
=
None
,
coord
S
ys
=
None
):
def
evalf
(
self
,
expression
=
None
,
coord
_s
ys
=
None
):
"""
Triangle3D implementation
Examples:
...
...
@@ -141,12 +141,12 @@ class Triangles3D(tfields.TensorFields):
Returns:
np.array: mask which is True, where expression evaluates True
"""
mask
=
super
(
Triangles3D
,
self
).
evalf
(
expression
,
coord
S
ys
=
coord
S
ys
)
mask
=
super
(
Triangles3D
,
self
).
evalf
(
expression
,
coord
_s
ys
=
coord
_s
ys
)
mask
=
self
.
_to_triangles_mask
(
mask
)
mask
=
np
.
array
([
mask
]
*
3
).
T
.
reshape
((
len
(
self
)))
return
mask
def
cut
(
self
,
expression
,
coord
S
ys
=
None
):
def
cut
(
self
,
expression
,
coord
_s
ys
=
None
):
"""
Default cut method for Triangles3D
Examples:
...
...
@@ -166,7 +166,7 @@ class Triangles3D(tfields.TensorFields):
>>> assert np.array_equal(tc2.triangleScalars, np.array([[2.]]))
"""
mask
=
self
.
evalf
(
expression
,
coord
S
ys
=
coord
S
ys
)
mask
=
self
.
evalf
(
expression
,
coord
_s
ys
=
coord
_s
ys
)
inst
=
self
[
mask
].
copy
()
# inst.triangleScalars =
...
...
@@ -290,12 +290,12 @@ class Triangles3D(tfields.TensorFields):
nT
=
self
.
ntriangles
()
mat
=
np
.
ones
((
1
,
3
))
/
3.0
# matrix product calculatesq center of all triangles
return
tfields
.
Points3D
(
np
.
dot
(
mat
,
self
.
reshape
(
nT
,
3
,
3
))[
0
],
coord
S
ys
=
self
.
coord
S
ys
)
return
tfields
.
Points3D
(
np
.
dot
(
mat
,
self
.
reshape
(
nT
,
3
,
3
))[
0
],
coord
_s
ys
=
self
.
coord
_s
ys
)
"""
Old version:
pointA, pointB, pointC = self.corners()
return Points3D(1. / 3 * (pointA + pointB + pointC)), coord
S
ys=self.coord
S
ys
return Points3D(1. / 3 * (pointA + pointB + pointC)), coord
_s
ys=self.coord
_s
ys
This versioin was slightly slower (110 % of new version)
Memory usage of new version is better for a factor of 4 or so.
Not really reliable method of measurement
...
...
@@ -549,7 +549,7 @@ class Triangles3D(tfields.TensorFields):
return
np
.
empty
((
tensors
.
shape
[
0
],
0
),
dtype
=
bool
)
masks
=
np
.
zeros
((
len
(
tensors
),
self
.
ntriangles
()),
dtype
=
bool
)