CFD Online Discussion Forums

CFD Online Discussion Forums (https://www.cfd-online.com/Forums/)
-   OpenFOAM Programming & Development (https://www.cfd-online.com/Forums/openfoam-programming-development/)
-   -   Rotating a vector in OpenFOAM (https://www.cfd-online.com/Forums/openfoam-programming-development/210561-rotating-vector-openfoam.html)

anon_q October 29, 2018 13:42

Rotating a vector in OpenFOAM
 
In mathematics, to rotate a vector \vec{V}(x, y, z) about the Z-axis by an angle of \theta, we do the the matrix multiplication:


\vec{W} = R\vec{V}
\vec{W}: is the new vector obtained by rotation.
R is the rotation matrix. [R] is given by:

R = \begin{pmatrix}\cos\theta  & -\sin\theta & 0 \\
  \sin\theta  & \cos\theta  & 0 \\
  0 & 0 & 1  \end{pmatrix} .

My question is: Is there any builtin function in OpenFOAM to rotate a vector without doing that manually? something like:
Code:

vector V(5.0, 1.0, 3.0);
vector axis(0.0, 0.0, 1.0);
vector W = vector::rotate(V, axis);

???

msaravia October 29, 2018 15:22

Hi again... I don't know if it can help you, but it may help others searching for the same thing.

First of all, rotations in 3D are very different from rotations in 2D. 3D rotations belong to SO3 group, which is not a vector space, but a manifold. 3D rotations can be parametrized in different ways; a set of 3 or 4 parameters is needed to build the rotational operator. Then, in contrast to the 2D case, 3D rotations are not commutative, and the parameters are often not additive.

I know that OpenFOAM can use quaternions to rotate a vector field (I dont know if another set of parameters can be used, the quaternions play the role of your angles). Quaternions are a set a of 4 parameters that parametrize a 3D rotation globally. Since OF is thought to be 3D, you would probably have to use a 3D function and feed it with a 2D rotation parameters.

As far as I know, that may not be enough, there is a function called transform such that, passing the quaternion as a parameter you obtain the rotated vector field.

This is:

Code:

Foam::tmp<Foam::vectorField> Foam::transform
 (
    const quaternion& q,
    const vectorField& tf
 )


anon_q October 29, 2018 16:56

Thank you very much dear Martin for your answer

olesen November 15, 2018 09:42

Quote:

Originally Posted by Evren Linda (Post 713426)
In mathematics, to rotate a vector \vec{V}(x, y, z) about the Z-axis by an angle of \theta, we do the the matrix multiplication:


\vec{W} = R\vec{V}
\vec{W}: is the new vector obtained by rotation.
R is the rotation matrix. [R] is given by:

R = \begin{pmatrix}\cos\theta  & -\sin\theta & 0 \\
  \sin\theta  & \cos\theta  & 0 \\
  0 & 0 & 1  \end{pmatrix} .

My question is: Is there any builtin function in OpenFOAM to rotate a vector without doing that manually? something like:
Code:

vector V(5.0, 1.0, 3.0);
vector axis(0.0, 0.0, 1.0);
vector W = vector::rotate(V, axis);

???


If you have the rotation matrix, you can just do that directly. Eg
Code:

tensor rot
(
    cos(theta), -sin(theta), 0,
    sin(theta), cos(theta), 0,
    0, 0, 1
);.


vector vec(someX, someY, somZ);


vector  vec1 = transform(rot, vec);
vector  vec2 = invTransform(rot, vec);  // probably doesn't yet work, but does in1812


... or manually:


vector  vec1 = rot & vec;
vector  vec2 = vec & rot;

Instead of defining the rotation matrix yourself, the coordinateRotation classes can be more useful. Eg,


Code:

tensor rot = axesRotation(axis1, axis2).R();
vector vec1 = rot & vec;

Could also just use a coordinateSystem directly. The additional overhead of an origin that you don't use is quite minimal.


/mark


All times are GMT -4. The time now is 08:36.