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

simple question about OpenFoam matrix class

Register Blogs Community New Posts Updated Threads Search

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
Old   August 8, 2018, 10:37
Default simple question about OpenFoam matrix class
  #1
Senior Member
 
Join Date: Oct 2017
Location: United States
Posts: 233
Blog Entries: 1
Rep Power: 10
TurbJet is on a distinguished road
Hello,

I have one simple question about OpenFoam matrix class.

In it's definition, the function 'allocate' says:
Code:
void Foam::Matrix<Form, Type>::allocate()
{
    if (n_ && m_)
    {
        v_ = new Type*[n_];
        v_[0] = new Type[n_*m_];

        for (register label i=1; i<n_; i++)
        {
            v_[i] = v_[i-1] + m_;
        }
    }
}
What baffles me is that since v_ is a pointer to a pointer, it supposes to be a 2D array. However, what about this part:
Code:
v_[0] = new Type[n_*m_];
? It seems only v_[0] saves the entire matrix, namely the matrix is stored in a 1D-form. But why it still do this :
Code:
v_ = new Type*[n_];
?

Why not doing something like:
Code:
v_ = new Type * [n_];
for(i=0; i<n_;++i)
    v_[i] = new Type [m_];
Thanks ahead !
TurbJet is offline   Reply With Quote

Old   August 9, 2018, 03:47
Default
  #2
Senior Member
 
Gerhard Holzinger
Join Date: Feb 2012
Location: Austria
Posts: 342
Rep Power: 28
GerhardHolzinger will become famous soon enoughGerhardHolzinger will become famous soon enough
My guess is that two memory allocations

Code:
v_ = new Type*[n_];
v_[0] = new Type[n_*m_];
were deemed more efficient than doing n+1 allocations

Code:
v_ = new Type * [n_];
for(i=0; i<n_;++i)
    v_[i] = new Type [m_];

The way it is implemented

Code:
void Foam::Matrix<Form, Type>::allocate()
{
    if (n_ && m_)
    {
        v_ = new Type*[n_];
        v_[0] = new Type[n_*m_];

        for (register label i=1; i<n_; i++)
        {
            v_[i] = v_[i-1] + m_;
        }
    }
}
you get pretty much the look&feel of a 2D array.
GerhardHolzinger is offline   Reply With Quote

Old   August 10, 2018, 21:53
Default
  #3
Senior Member
 
Join Date: Oct 2017
Location: United States
Posts: 233
Blog Entries: 1
Rep Power: 10
TurbJet is on a distinguished road
Quote:
Originally Posted by GerhardHolzinger View Post
My guess is that two memory allocations

Code:
v_ = new Type*[n_];
v_[0] = new Type[n_*m_];
were deemed more efficient than doing n+1 allocations

Code:
v_ = new Type * [n_];
for(i=0; i<n_;++i)
    v_[i] = new Type [m_];

The way it is implemented

Code:
void Foam::Matrix<Form, Type>::allocate()
{
    if (n_ && m_)
    {
        v_ = new Type*[n_];
        v_[0] = new Type[n_*m_];

        for (register label i=1; i<n_; i++)
        {
            v_[i] = v_[i-1] + m_;
        }
    }
}
you get pretty much the look&feel of a 2D array.

I still don't get it. With this one
Code:
v_[0] = new Type[n_*m_];
doesn't it mean all the elements will be saved in v[0][i] ? If so, why doesn't it just declare a 1D array to save the entire matrix?
TurbJet is offline   Reply With Quote

Reply

Tags
matrix, openfoam


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
Map of the OpenFOAM Forum - Understanding where to post your questions! wyldckat OpenFOAM 10 September 2, 2021 06:29
OpenFOAM v3.0+ ?? SBusch OpenFOAM 22 December 26, 2016 15:24
How to rewrite a standard OpenFOAM solver as a C++ class cfbaptista OpenFOAM Programming & Development 7 March 23, 2016 05:50
Cross-compiling OpenFOAM 1.7.0 on Linux for Windows 32 and 64bits with Mingw-w64 wyldckat OpenFOAM Announcements from Other Sources 3 September 8, 2010 07:25
Errors running allwmake in OpenFOAM141dev with WM_COMPILE_OPTION%3ddebug unoder OpenFOAM Installation 11 January 30, 2008 21:30


All times are GMT -4. The time now is 15:54.