Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor FieldData to be base class of Field #2313

Merged
merged 31 commits into from
May 13, 2021
Merged

Conversation

ZedThree
Copy link
Member

The reason for doing all of this was originally to be able to remove uses of bout::global::mesh from FieldData, so that I could add the fix for the case-sensitive Options for ignoring the boundary region inputs without adding more uses of the global mesh. There were a few other plus points to doing this, namely removing the double-inheritance from Field2/3D, and tidying up the code a bit. It has turned out to be a bit of a bigger effort than I had anticipated/hoped for!


Make FieldData the base class of Field, and move some members for Field into FieldData. This does a few things:

  • remove the double-inheritance from Field2D and Field3D
  • FieldPerp now also ultimately inherits from FieldData, where it previously was only Field
  • move the Mesh, Coordinates, and CELL_LOC members from Field to FieldData
    • this means Vector2/3D now have getMesh() and getCoordinates() methods, which means we don't need to do the hacky v.x.getMesh() etc.
  • replace Field::copyFieldMembers with more idiomatic Field::operator=(rhs) calls in the derived classes, and makes sure the copy-constructor methods delegate to the Field constructor
    • this makes sure all the copy operations are done consistently down the whole class hierarchy
  • make most of the FieldData members private rather than protected
    • I understand why protected members are bad now! They make refactoring that much more awkward
    • use getters instead
    • fieldmesh is still protected because it's used in a lot of places. I should probably also bite the bullet and make this private too
  • remove several unused methods from FieldData, and the unused experimental FieldVisitor
  • remove use of bout::globals::mesh from FieldData

Also includes #2306 so that I didn't have to deal with the conflicts separately.


If we were to go further here, I think we could do some or all of the following:

  • make FieldData::applyBoundary(bool) have a default implementation -- it appears to be basically the same between all the implementations
  • rename Field to ScalarField and add a VectorField base class to remove duplicated code from the vector classes
  • move the time-derivative member (deriv) into one of the base-classes and make it a unique_ptr -- or even just a value?

ZedThree added 20 commits May 6, 2021 16:58
The Coordinates' metric elements can hold shared_ptrs that result in a
cyclic dependency between the Fields and the Coordinates, with the
result that some unit tests have memory leaks.

One place the issue can arise is when multiplying fields by metric
elements with `CHECK > 0`. Then, `ASSERT1_FIELDS_COMPATIBLE` checks
that both fields share a `Coordinates` -- but this has the side effect
of initialising the `shared_ptr` in the metric elements.

This means that when the `Mesh` gets destroyed, the `coords_map`
starts to destroy its contents, but because the `Coordinates` metric
elements members have references to the `Coordinates`, the
`Coordinates` in `coords_map` doesn't get destroyed. The end result is
that the `Coordinates` gets leaked.

The fix here is to use a `weak_ptr` in `Field`. This only gets a
reference to the `Coordinates` when needed.
This removes the double-inheritance from `Field2D` and `Field3D`, as
well as making `FieldPerp` a subclass of `FieldData`
* fix-field-coords-leak:
  Fix memory leak of Coordinates in unit tests
@ZedThree ZedThree added this to the BOUT-5.0 milestone May 12, 2021
Copy link
Contributor

@github-actions github-actions bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

clang-tidy made some suggestions

include/field2d.hxx Show resolved Hide resolved
include/field_data.hxx Show resolved Hide resolved
src/field/field3d.cxx Show resolved Hide resolved
src/field/field_data.cxx Outdated Show resolved Hide resolved
src/field/field_data.cxx Show resolved Hide resolved
src/field/field_data.cxx Outdated Show resolved Hide resolved
src/field/vector2d.cxx Outdated Show resolved Hide resolved
src/field/vector2d.cxx Show resolved Hide resolved
src/field/vector3d.cxx Outdated Show resolved Hide resolved
src/field/vector3d.cxx Show resolved Hide resolved
Copy link
Contributor

@github-actions github-actions bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

clang-tidy made some suggestions

include/field2d.hxx Outdated Show resolved Hide resolved
src/field/field2d.cxx Outdated Show resolved Hide resolved
src/field/field2d.cxx Outdated Show resolved Hide resolved
bendudson
bendudson previously approved these changes May 12, 2021
Copy link
Contributor

@bendudson bendudson left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice! Thanks @ZedThree

@ZedThree
Copy link
Member Author

@bendudson I just fixed the few clang-tidy warnings, please could you re-approve?

@ZedThree ZedThree merged commit 0e5e308 into next May 13, 2021
@ZedThree ZedThree deleted the fielddata-refactor branch May 13, 2021 10:37
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants