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/)
-   -   Defining a Matrix in OpenFOAM-1.6-ext (https://www.cfd-online.com/Forums/openfoam-programming-development/125207-defining-matrix-openfoam-1-6-ext.html)

Daniel_Khazaei October 21, 2013 11:33

Defining a Matrix in OpenFOAM-1.6-ext
 
Hello foamers


I am trying to define a Matrix in my OpenFOAM solver but I have a few problems.
I try to describe my problem with an example:

Assuming I want to define a Matrix in the solver, this is what I do:

Matrix<scalar> A(n, m, 0.0);

Now when I want to compile the solver, it gives me the following error:

Code:

setInterfaceDisplacement.H:98: error: wrong number of template arguments (1, should be 2)
/opt/OpenFOAM/OpenFOAM-1.6-ext/src/OpenFOAM/lnInclude/Matrix.H:54: error: provided for ‘template<class Form, class Type> class Foam::Matrix’

I can understand that I need to add one more argument in declaration, but I don't know what does "class Form" mean?


regards

kmou November 26, 2013 10:45

Hi,

I am encountering the same problem and would be grateful if you could share how you resolved this problem. Thank you very much.

wyldckat November 26, 2013 17:11

Greetings to all!

Actually, while I was tracking kmou's posts, this previous thread gave the hint: http://www.cfd-online.com/Forums/ope...t-compile.html
The solution seems to be to use "scalarSquareMatrix" instead of "Matrix<scalar>".

A bit more explanation:
  1. The definition of "scalarSquareMatrix" is made in the file "src/OpenFOAM/matrices/scalarMatrices/scalarSquareMatrix.H", where it defines the class based on the "SquareMatrix" template:
    Code:

    class scalarSquareMatrix
    :
        public SquareMatrix<scalar>

  2. The template class "SquareMatrix" is defined in the file "src/OpenFOAM/matrices/SquareMatrix/SquareMatrix.H", as follows:
    Code:

    template<class Type>
    class SquareMatrix
    :
        public Matrix<SquareMatrix<Type>, Type>

    Namely, said "class Form" is "SquareMatrix<Type>" in this case... :eek: pretty confusing template voodoo, isn't it? :)
Either way, the quick solution is indicated in the post #3 at the aforementioned thread ;).

Best regards,
Bruno

Daniel_Khazaei November 27, 2013 11:46

thanks for the suggestion...

I had resolved that issue a few weeks ago. I have changed the declaration as follow:

Matrix <scalarField,scalar> A (n,m)

---------


squareScalarMatrix is working fine on square matrices but how can I define a matrix with one column or one row?

like this one:

Matrix <scalarField,scalar> A (n,1) or A (1,n)

-------

OK I have fixed the last one by using rectangular matrix

Daniel_Khazaei November 27, 2013 13:26

Quote:

Originally Posted by kmou (Post 463585)
Hi,

I am encountering the same problem and would be grateful if you could share how you resolved this problem. Thank you very much.

you need to add the following headers to your program:

#include "scalarSquareMatrix.H" --->> for square type matrix

if you want to define a square matrix: A (n,n) and initiate it with 0

scalarSquareMatrix A(n, 0.0);


#include "RectangularMatrix.H" ---->> for rectangular matrix

if you want to define a rectangular matrix: A (n,m)

RectangularMatrix <scalar> A(n,m);

kmou December 2, 2013 05:06

Thank you Daniel and Bruno for your replies.
So new classes of matrices have been created, such as SquareMatrix and RectangularMatrix, which effectively correspond do the "Form" that is required. Thus, for each Matrix<scalar> that I have, I will need to make a decision as to if it is a Square matrix, or a Rectangular matrix or Diagonal matrix etc...

13msmemusman March 23, 2016 10:19

Matrix with different dimensions for different elements (kg,m,N,etc)
 
Hi guys i have a problem related to matrices. I have to define and write a matrix in which every element have different dimensions. for example 1st element has dimensions in kilogram 2nd in meters 3rd in second like that. Please guide me how can i do that.

wyldckat March 26, 2016 06:41

Quote:

Originally Posted by 13msmemusman (Post 591313)
Hi guys i have a problem related to matrices. I have to define and write a matrix in which every element have different dimensions. for example 1st element has dimensions in kilogram 2nd in meters 3rd in second like that. Please guide me how can i do that.

Quick answer: :confused: AFAIK, that's not possible. You should not include the dimensions in the matrix and instead you should only use the values.

13msmemusman March 27, 2016 01:50

Thank you. but there should be a way...... coz..........

wyldckat March 27, 2016 06:27

Quote:

Originally Posted by 13msmemusman (Post 591811)
Thank you. but there should be a way...... coz..........

:mad: If you had provided an example I could have worked with, it would have be easier/faster for me to figure it out. :rolleyes:

Anyway, this is an example of how to do it:
Code:

    SquareMatrix<dimensionedScalar> hmm(3);

    hmm[0][0].dimensions().reset(dimLength);
    hmm[0][0] = dimensionedScalar("cookie1",dimLength,-3.0);
    hmm[0][1] = 10.0;
    hmm[0][2] = -4.0;
    hmm[1][0] = 2.0;
    hmm[1][1] = 3.0;
    hmm[1][2] = 10.0;
    hmm[2][0] = 2.0;
    hmm[2][1] = 6.0;
    hmm[2][2] = 1.0;

It's based on the test application "applications/test/Matrix" and it took me a while to figure out the details. :cool:

13msmemusman March 27, 2016 06:54

1 Attachment(s)
Attachment 46268

Please have a look on this file attached.

wyldckat March 27, 2016 09:34

Quote:

Originally Posted by 13msmemusman (Post 591828)
Please have a look on this file attached.

Quick questions:
  1. What is the calculation you're trying to perform?
    • I ask this because this matrix has to be multiplied by something!?
  2. How exactly is this matrix meant to be used?

13msmemusman March 27, 2016 09:42

its matrix for mass in mass damper spring system. actually i am working with some kind of fluid and vehicle dynamics interface. Yes this matrix is gonna be multiplied by a vector

wyldckat March 27, 2016 10:04

Hi Muhammad,

Quote:

Originally Posted by 13msmemusman (Post 591856)
its matrix for mass in mass damper spring system. actually i am working with some kind of fluid and vehicle dynamics interface. Yes this matrix is gonna be multiplied by a vector

I don't know if you've noticed, but if you keep giving vague descriptions, then I'm only able to give vague answers as well :(

There is a thread explaining how to provide information, so that this kind of vague exchange of information doesn't happen: http://www.cfd-online.com/Forums/ope...-get-help.html

Best regards,
Bruno

santiagomarquezd April 16, 2016 20:55

Quote:

Originally Posted by 13msmemusman (Post 591313)
Hi guys i have a problem related to matrices. I have to define and write a matrix in which every element have different dimensions. for example 1st element has dimensions in kilogram 2nd in meters 3rd in second like that. Please guide me how can i do that.

It is not possible in the present versions of FOAM.

Regards!

tjliang March 11, 2020 07:58

Hello Everyone,


my problem is a litle bit related to the Matrix problem. I want to have one field value obtained from a two-dimensional lookup table, which means the value is dependent on two variables. Is it possible that I insert values into the Matrix and look up and interpolate among them? I know it may be quite wrong, any hints from you will be appreciated by me. Thank you.


Best regards,


Peng

klausb December 11, 2023 14:44

I want to multiply two matrices E and F to obtain G so G = E*F.
 
I want to multiply two matrices E and F to obtain G so G = E*F. I tried the following, I don't get compile errors but from the line //Compute G = E * F onwards, things don't work. Here's my code:

Code:

    // Set the size of the matrices
    const label N = 4; // matrix size
   
    // Declare and initialize lduMatrices E, F, and G
    RectangularMatrix <scalar> E(N,N);
    RectangularMatrix <scalar> F(N,N);
    RectangularMatrix <scalar> G(N,N);

    // Set coefficients for matrix E diagonal
    scalar diagonalValuesE[] = {1.0, 2.0, 3.0, 4.0};
    for (label i = 0; i < N; ++i)
    {
        E[i][i] = diagonalValuesE[i];
    }

    // Set coefficients for matrix F diagonal
    scalar diagonalValuesF[] = {5.0, 6.0, 7.0, 8.0};
    for (label i = 0; i < N; ++i)
    {
        F[i][i] = diagonalValuesF[i];
    }

    //Compute G = E * F
    G=E*F; // THIS DOESN'T WORK I THINK, THERE IS A FUNCTION CALLED
                //"multiply", too but I don't know how to use it
   
    // Print the diagonal element G[0][0]
    Info << "Diagonal element G[0][0]: " << G[0][0] << endl;

Regarding "multiply" see: // https://www.openfoam.com/documentati...trices_8H.html

Can someone tell me how to multiply two matrices?


All times are GMT -4. The time now is 05:06.