Skip to content

Commit

Permalink
Enhance FieldContainer.link() (#918)
Browse files Browse the repository at this point in the history
  • Loading branch information
adtzlr authored Dec 10, 2024
1 parent be2f25c commit e28bf5b
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 3 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ All notable changes to this project will be documented in this file. The format
- Enhance the `hello_world(axisymmetric=False, planestrain=False, curve=False, xdmf=False, container=False)` function with new arguments to customize the generated template script.
- Enhance `Boundary` with added support for multiaxial prescribed values.
- Enhance `math.linsteps(..., values=0)` with default values except for the column `axis` if `axis` is not None.
- Link all field-values to the values of the first field if no other field is given in `FieldContainer.link()`.

### Fixed
- Fix `Boundary(..., mode="and")` by ignoring any undefined axis.
Expand Down
10 changes: 7 additions & 3 deletions src/felupe/field/_container.py
Original file line number Diff line number Diff line change
Expand Up @@ -210,10 +210,14 @@ def copy(self):
"Return a copy of the field."
return deepcopy(self)

def link(self, other_field):
def link(self, other_field=None):
"Link value array of other field."
for field, newfield in zip(self.fields, other_field.fields):
field.values = newfield.values
if other_field is None:
for u in self.fields[1:]: # link n-to-1 (all-to-first)
u.values = self.fields[0].values
else:
for field, newfield in zip(self.fields, other_field.fields): # link 1-to-1
field.values = newfield.values

def view(self, point_data=None, cell_data=None, cell_type=None, project=None):
"""View the field with optional given dicts of point- and cell-data items.
Expand Down
17 changes: 17 additions & 0 deletions tests/test_field.py
Original file line number Diff line number Diff line change
Expand Up @@ -247,9 +247,26 @@ def test_view():
# ax = mesh.imshow()


def test_link():
mesh = fem.Cube(n=2)
region = fem.RegionHexahedron(mesh)
field = fem.Field(region, dim=3) & fem.Field(region, dim=1)
field.link()

assert field[0].values is field[1].values

field1 = fem.Field(region, dim=3) & fem.Field(region, dim=1)
field2 = fem.Field(region, dim=3) & fem.Field(region, dim=1)
field1.link(field2)

assert field1[0].values is field2[0].values
assert field1[1].values is field2[1].values


if __name__ == "__main__":
test_axi()
test_3d()
test_3d_mixed()
test_mixed_lagrange()
test_view()
test_link()

0 comments on commit e28bf5b

Please sign in to comment.