Commit b6c9f64c authored by Martin Bauer's avatar Martin Bauer
Browse files

Merge branch 'Datahandling.add_arrays' into 'master'

Add DataHandling.add_arrays

See merge request !120
parents 5d0a131c 023e9659
Pipeline #21016 passed with stages
in 9 minutes and 55 seconds
......@@ -62,6 +62,33 @@ class DataHandling(ABC):
pystencils field, that can be used to formulate symbolic kernels
"""
def add_arrays(self, description: str) -> Tuple[Field]:
"""Adds multiple arrays using a string description similar to :func:`pystencils.fields`
>>> from pystencils.datahandling import create_data_handling
>>> dh = create_data_handling((20, 30))
>>> x, y =dh.add_arrays('x, y(9)')
>>> print(dh.fields)
{'x': x: double[22,32], 'y': y(9): double[22,32]}
>>> assert x == dh.fields['x']
>>> assert dh.fields['x'].shape == (22, 32)
>>> assert dh.fields['y'].index_shape == (9,)
Args:
description (str): String description of the fields to add
Returns:
Fields representing the just created arrays
"""
from pystencils.field import _parse_part1
names = []
for name, indices in _parse_part1(description):
names.append(name)
self.add_array(name, values_per_cell=indices)
return (self.fields[n] for n in names)
@abstractmethod
def has_data(self, name):
"""Returns true if a field or custom data element with this name was added."""
......
......@@ -1058,15 +1058,17 @@ type_description_regex = re.compile(r"""
""", re.VERBOSE | re.IGNORECASE)
def _parse_description(description):
def parse_part1(d):
def _parse_part1(d):
result = field_description_regex.match(d)
while result:
name, index_str = result.group(1), result.group(2)
index = tuple(int(e) for e in index_str.split(",")) if index_str else ()
yield name, index
d = d[result.end():]
result = field_description_regex.match(d)
while result:
name, index_str = result.group(1), result.group(2)
index = tuple(int(e) for e in index_str.split(",")) if index_str else ()
yield name, index
d = d[result.end():]
result = field_description_regex.match(d)
def _parse_description(description):
def parse_part2(d):
result = type_description_regex.match(d)
......@@ -1091,7 +1093,7 @@ def _parse_description(description):
else:
field_description, field_info = description, 'float64[2D]'
fields_info = [e for e in parse_part1(field_description)]
fields_info = [e for e in _parse_part1(field_description)]
if not field_info:
raise ValueError("Could not parse field description")
......
......@@ -13,7 +13,6 @@ except ImportError:
pytest = unittest.mock.MagicMock()
def basic_iteration(dh):
dh.add_array('basic_iter_test_gl_default')
dh.add_array('basic_iter_test_gl_3', ghost_layers=3)
......@@ -227,3 +226,18 @@ def test_vtk_output():
for domain_shape in [(4, 5), (3, 4, 5)]:
dh = create_data_handling(domain_size=domain_shape, periodicity=True)
vtk_output(dh)
def test_add_arrays():
domain_shape = (3, 4, 5)
field_description = 'x, y(9)'
dh = create_data_handling(domain_size=domain_shape, default_ghost_layers=0, default_layout='numpy')
x_, y_ = dh.add_arrays(field_description)
x, y = ps.fields(field_description + ': [3,4,5]')
assert x_ == x
assert y_ == y
assert x == dh.fields['x']
assert y == dh.fields['y']
Markdown is supported
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