Skip to content

Output bingham average as quaternions - #7289

Open
Theo-Geo wants to merge 7 commits into
geodynamics:mainfrom
Theo-Geo:output_bingham_average_as_quaternions
Open

Output bingham average as quaternions#7289
Theo-Geo wants to merge 7 commits into
geodynamics:mainfrom
Theo-Geo:output_bingham_average_as_quaternions

Conversation

@Theo-Geo

Copy link
Copy Markdown
Contributor

In this pull request i added the possibility to output rotations created by the bingham average particle property plugin as quaternions. Unit quaternions are a representation of rotations that is more numerically stable and compact than rotation matrices and avoid representation problems such as ambiguity and gimbal lock that euler angles struggle with.

The space of unit quaternions is a double cover of the space of rotations i.e. q and -q correspond to the same rotation matrix. Therefore the convention is used, where the scalar component of any quaternion is set to be >0.

The pull request contains:

  1. two functions in aspect::Utilities::Quaternions
  2. a unit test to test back and forth conversion between quaternions and rotation matrices
  3. the new parameter "Output rotation as" in the particle property plugin which can be set to full matrix, euler angles or quaternion
  4. repacing all existing occurrences of Use rotation matrix by Output rotation as
  5. A test for the new output property

In case I should add more documentation for the general concept of quaternions somewhere let me know!
Also in case removing a parameter and directly replacing it with a new one is bad style please please let me know and i will keep the old parameter as deprecated somehow.

Some comments:
I am planning to add a separate particle interpolator for rotations based on quaternions.
(preparations for this are in PR #7288).
The interpolator is already written, but i thought it would be nice to split the pull requests.
The new rotation average will contain a small benchmark and will be used in a more thorough benchmark (see PR #7231)
Stay tuned and thanks for anyone who is willing to review these additions.
Cheers!

Before your first pull request:

For all pull requests:

I used AI to understand and reconcile problems compiling my incorrect c++ code.

If yes, please describe your usage of AI models in the creation of this pull request

For new features/models or changes of existing features:

  • I have tested my new feature locally to ensure it is correct.
  • I have created a testcase for the new feature/benchmark in the tests/ directory.
  • I have added a changelog entry in the doc/modules/changes directory that will inform other users of my change.

@tjhei tjhei left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

We typically try to deprecate old parameter names or at least include a "conversion script". Not sure what the right choice here is...

* called 'Output rotation as'.
*/
bool use_rotmat;
std::string output_rotation;

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Should this be called "output format"?

Comment thread include/aspect/utilities.h Outdated
* follows from the quaternion algebra.
* (https://en.wikipedia.org/wiki/Quaternions_and_spatial_rotation)
*/
Tensor<2,3> quaternion_to_rotation_matrix(const double w,const double x, const double y, const double z);

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Why are you using separate arguments instead of an array here?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

I oriented myself on zxz_euler_angles_to_rotation_matrix, but yes it would be more consistent to use one argument.

* Quaternions are a double cover of 3x3 orthogonal matrices. R(q) = R(-q)
* and we choose the convention to only work with quaternions that fulfill q[0] > 0.
*/
std::array<double,4> rotation_matrix_to_quaternion(const Tensor<2,3> &rotation_matrix, const long double tolerance);

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Do you need to document the order of the 4 return values?

@Theo-Geo

Theo-Geo commented Aug 12, 2026

Copy link
Copy Markdown
Contributor Author

Okay i now reintroduced the old parameter "Use rotation matrix" and changed its description.
When running the model and specifying "Use rotation matrix = false", the not default case, aspect returns a warning that the parameter is deprecaded.

@Theo-Geo

Copy link
Copy Markdown
Contributor Author

@tjhei this is ready for another review
(I will squash the commits again when everything is final if that's okay)

@Theo-Geo

Copy link
Copy Markdown
Contributor Author

also @Wang-yijun @KiralyAgi i think you are the only other two using the Use rotation matrix feature so maybe you want to look at this too.

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.

2 participants