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

adding a C code to OpenFOAM

Register Blogs Members List Search Today's Posts Mark Forums Read

Reply
 
LinkBack Thread Tools Display Modes
Old   November 20, 2012, 12:39
Default adding a C code to OpenFOAM
  #1
Member
 
Martin
Join Date: Dec 2011
Location: Latvia
Posts: 52
Rep Power: 4
latvietis is on a distinguished road
Greetings!

I have a code which I want to use in OpenFOAM. The idea is that at first there is given an initial value of "caurl" and then this loop goes trough all cells of my mesh and recalculates value of "caurl" which is afterwards written out and used again.

How it is possible to make this work with OpenFOAM? I have problems understanding all this class/object thing.

Lastly, b0, r0 and r2 are values taken from graph points and B2 is volScalarField.

The additional questions are:

1) Can I ignore the fact that B2 is defined as volScalarField and I'm trying to compare it with a float data type?

2) How it is possible to make this code work in every cell? Is it something similar to forAll(mesh.V(), celli)?

Code:
    float b0[n]={0,0.04,0.16,0.36,0.64,1.,1.44,1.69,1.96,2.25,2.56};
    float r0[n]={0.109E-03,0.109E-03,0.109E-03,0.11E-03,0.111E-03,0.112E-03,0.12E-03,0.143E-03,0.196E-03,0.417E-03,0.125E-02};
    float r2[n]={0,0,0,0,0.439E-05,0,0.248E-03,0.421E-03,0.466E-03,0.951E-02,0};
Code:
{
    float rval=1;
    for (int i=0; i<n; i++)
    {
        float* b0 = x;
        float* r0 = y;
        float* r2 = z;
        int kl,kr;

        if ( B2 <= b0[n-1])
            {
                kl = 0;
                kr = n-1;

                    if (kr - kl >= 1)
                    {
                        int k = (kr + kl)/2.;
                            if (b0[n-1] >= B2)
                                kr = k;
                            else
                                kl = k;
                    }
            float dx = b0[kr] - b0[kl];
            float du = (b0[kr] - B2)/dx;
            float dl = (B2 - b0[kl])/dx;
            float du2 = pow(du,2);
            float dl2 = pow(dl,2);
            rval = du * r0[kl] + dl * r0[kr] + ((du2 - 1.) * du * r2[kl] + (dl2 - 1.) * dl * r2[kr]) * pow(dx,2)/6.;
            }
            else
            {
            float dx = b0[n-1] - b0[n-2];
            float rder = -(r0[n-2] - r0[n-1])/dx + (+r2[n-2] + 2. * r2[n-1]) * dx/6.;
            rval = r0[n-1] + (B2 - b0[n-1]) * rder;
            }
    b0=NULL;
    r0=NULL;
    r2=NULL;
    }
    caurl=rval;
}
Sincerely,
Martin
latvietis is offline   Reply With Quote

Old   November 21, 2012, 03:46
Default
  #2
Senior Member
 
akidess's Avatar
 
Anton Kidess
Join Date: May 2009
Location: Delft, Netherlands
Posts: 912
Rep Power: 16
akidess will become famous soon enough
1) scalar and float are related, you only might get troubles with dimensions
2) Yes, it's exactly like that. forAll(B2, celli) will iterate through the volScalarField. If you use B2[celli], I think you will get a scalar value without dimensions.

- Anton
__________________
*On twitter @akidTwit
*Spend as much time formulating your questions as you expect people to spend on their answer.
*Check out the scientific computing exchange http://scicomp.stackexchange.com
akidess is offline   Reply With Quote

Reply

Thread Tools
Display Modes

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 On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Specific OpenFOAM Code pbhuter OpenFOAM 13 June 30, 2012 19:06
get OpenFOAM source code Ahmed Khattab OpenFOAM 2 February 9, 2012 04:48
Modeling the Earth's Mantle: is OpenFOAM a good candidate? decapitor OpenFOAM Running, Solving & CFD 4 January 18, 2012 19:13
OpenFOAM Debian packaging current status problems and TODOs oseen OpenFOAM Installation 9 August 26, 2007 13:50
Adding Fotran code in Fluent S.Venkat FLUENT 2 December 6, 2002 17:13


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