CFD Online Logo CFD Online URL
www.cfd-online.com
[Sponsors]
Home > Forums > Software User Forums > OpenFOAM > OpenFOAM Programming & Development

Rotate a field

Register Blogs Community New Posts Updated Threads Search

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
Old   January 27, 2023, 09:42
Default Rotate a field
  #1
New Member
 
Join Date: Jan 2023
Posts: 2
Rep Power: 0
vtAM is on a distinguished road
Hi all,


Ive been reading this forum a lot and have drawn many useful information for developing my new solver. Close to finishing, I am now stuck and looking for some help.

What I want to do: I want to rotate the components of a vector respectively a tensor field. By rotation I mean just rotate the vectors or tensor by changing theirs components. I dont want to rotate the mesh here nor do I want to change the cell center locations or anything.

What I do have:

  • The reference to the mesh "mesh_"
  • A quaternion "rotationquaternion" that describes the rotation i want to perform
This is my code:
Code:
 

            // Print a vector before rotation (debugging)
             Info<<mesh_.lookupObjectRef<volVectorField>
            (
                "U"
            ).ref()[1] << nl << endl;

            // Rotation to be performed
            vector rotaxis(0, 0, 1);        // about z axis
            scalar rotangle(3.14159/2.0) ; // around 90°
            quaternion rotationquaternion(rotangle,rotaxis);
            
            // Perform rotation
            Foam::transform
            (
                rotationquaternion,
                mesh_.lookupObjectRef<volVectorField>
                (
                    "U"
                ).ref() 
            );

            // Print a vector after rotation (debugging)
            Info<<mesh_.lookupObjectRef<volVectorField>
            (
                "U"
            ).ref()[1] << nl << endl;
However, the first vector of the field I print out to check if the transformation was done remains unchanged. The output I get:
Code:
(-3.89156e-05 1.63329e-05 -2.96595e-05)

(-3.89156e-05 1.63329e-05 -2.96595e-05)
The output I want:
Code:
(-3.89156e-05 1.63329e-05 -2.96595e-05)

(-1.63329e-05 -3.89156e-05 -2.96595e-05)
I need to use the lookup because later on I want to rotate any field (not just "U") dynamically without hardcoding their names.


Any help is highly appreciated! Thank you!


vtAM

Last edited by vtAM; January 27, 2023 at 09:43. Reason: Code formatting
vtAM is offline   Reply With Quote

Old   January 27, 2023, 11:31
Default
  #2
New Member
 
Join Date: Jan 2023
Posts: 2
Rep Power: 0
vtAM is on a distinguished road
Just managed to solve. This is working as inteded:


Code:
            

            // Print a vector before rotation (debugging)
            Info<<mesh_.lookupObjectRef<volVectorField>
            (
                "U"
            ).internalField()[678] << nl << endl;

            // Rotation to be performed
            vector rotaxis(0, 0, 1);        // about z axis
            scalar rotangle(M_PI_2) ; // 90°
            quaternion rotationquaternion(rotaxis, rotangle);
            Info<< "quaternion " << rotationquaternion << nl << endl;

            // Perform rotation
            Foam::transform
            (
                                mesh_.lookupObjectRef<volVectorField>
                (
                    "U"
                ).ref(),
                rotationquaternion,
                mesh_.lookupObjectRef<volVectorField>
                (
                    "U"
                ).internalField() 
            );

            // Print a vector after rotation (debugging)
            Info<<mesh_.lookupObjectRef<volVectorField>
            (
                "U"
            ).internalField()[678] << nl << endl;

There are three changes made.
  1. Correct initialization of the quaternion. The axis goes first, the angle second
  2. Use internalField() instead of ref(). Both work, but internalField() is a static reference and recommended for speed, see src/OpenFOAM/fields/GeometricFields/GeometricField/GeometricField.H
  3. Use Foam::transform(ref to field, quaternion, ref to field) to actual change the field itself. I am not a C++ pro but from my understanding the previous Foam::transform(quaternion, ref to field) would only return a copy of the rotated field.
Hope this helps someone in the future. Thanks to everybody who was about to help.


Cheers,


vtAM
vtAM is offline   Reply With Quote

Reply


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Initial pressure field from velocity field (incompressible) dhein STAR-CCM+ 3 January 18, 2022 08:12
turbulence field omega stops solver woodie OpenFOAM Pre-Processing 1 January 9, 2022 10:42
Implement field function with data from previous time steps as boundary condition Faraam STAR-CCM+ 0 August 20, 2021 15:33
potential flows, helmholtz decomposition and other stuffs pigna Main CFD Forum 1 October 26, 2017 08:34
[General] How to create an additional vector with {Field 4, Field 5, Field 6} Bombacar ParaView 1 August 15, 2015 18:05


All times are GMT -4. The time now is 23:07.