Fluent: How to take a 2d simulation result to initialize a 3d simulation


Imagine you have a 2d cas and dat file (for example 2d channel) and would like to use these 2d results to initialize the 3d simulation (3d channel). The 3rd direction is straight, ie. it is not applicable for axisymmetric to full 3d problems.
We created a little UDF that does the following:
This UDF reads in a 2d interpolation file and writes a new
interpolation file suitable for a 3d grid. The UDF can
handle any number of variables. The 2d solution will be interpolated
onto the x-y plane, the data in the z direction remains constant.

How to use it:
(1) Run a 2d case and export the interpolation file (File/Interpolate/Write Data). You have to select the fields that you want to export. The name has to be 2d.ip

(2) Open 3d FLUENT and read the mesh file, do the case set up. Using read-bc and write-bc accelerates that. If the 3d case has turbulence disabled the turbulent quantities of the 2d case will NOT be read in.

(3) Compile and load the UDF. Interpreting it will NOT work.

(4) Execute the UDF, "read_write_data" through Define/User Defined/Execute On Demand

(5) The UDF reads the interpolation file 2d.ip, stores the variables in arrays and writes a 3d interpolation file "3d.ip", While writing the 3d interpolation file the variable in the third dimension(e.g. z vel) is set to zero.

(6) File/Interpolate Interpolate Fields to read in the 3d.ip file.

-------------------------------------------------------------------------------------------------------------------------------------------------------------
/* Created by asw, mrr */
#include "udf.h"
#include "stdio.h"

int ver, dim, Npts, nflds;
char fvar[25],*fldarr;
float *xarr, *yarr, *zarr;

void Read_Data()
{
int i,j,k;
int DataIsRead;
float x,y, get_data;
FILE *fp,*fp2;

DataIsRead = 0;

fp=fopen("2d.ip", "r");
if ( fp!=NULL )
CX_Message("nReading the file 2d.ipn");
else
CX_Message("Error in opening file n");

fp2=fopen("3d.ip","w");

fscanf(fp,"%dn", &ver);
if(ver == 1)
CX_Message("Reading the interpolation file for FLUENT 5n");
else
CX_Message("Reading the interpolation file for FLUENT 6n");

fprintf(fp2,"%dn",ver);/*FLUENT version FLUENT 5 if 1 else FLUENT 6*/

fscanf(fp,"%dn", &dim);
CX_Message("Reading the interpolation file for %d dimensionsn",dim);
fprintf(fp2,"%dn",dim+1);/*changes the dimensions from 2d to 3d*/

fscanf(fp,"%dn", &Npts);
CX_Message("Number of points in the interpolation file = %d n",Npts);
fprintf(fp2,"%dn",Npts);/*No of data points*/

fscanf(fp,"%dn",&nflds);
CX_Message("Number of data fields read in are %d nn",nflds);
fprintf(fp2,"%dn",nflds);/* No of flow variables*/

/* Allocated memory for the x, y and z coordinates*/

xarr = (float *) malloc(Npts*sizeof(float));
yarr = (float *) malloc(Npts*sizeof(float));
zarr = (float *) malloc(Npts*sizeof(float));

CX_Message("Memory allocation done for x, y , z and variable_arr arraysn");

CX_Message("*******************************************************************n");
CX_Message(" Following fields will be read from the interpolation file 2d.ip n");
CX_Message("*******************************************************************n");
for (j = 0; j < nflds; j++)
{
fscanf(fp, "%sn",fvar);
CX_Message("%d %sn",j,fvar);
fprintf(fp2,"%sn",fvar);
}

for (i=0; i<Npts; i++)
{
fscanf(fp,"%e",&x);
fprintf(fp2,"%en",x);
}

CX_Message("x coordinate data read and written sucessfullyn");

fscanf(fp,"n");
for (i=0; i<Npts; i++)
{
fscanf(fp, "%e",&y);
fprintf(fp2,"%en",y);
}

CX_Message("y coordinate data read and written sucessfullyn");

for (i=0; i<Npts; i++)
{
zarr[i]=0.0;
fprintf(fp2,"%en",zarr[i]);
}

CX_Message("z coordinate data set to zero and written to 3d.ip sucessfullyn");

fscanf(fp,"n");

for (i=0; i<nflds; i++)
{
for(k = 0; k< Npts; k++)
{
fscanf(fp, "%e",&get_data);
fprintf(fp2,"%en",get_data);
}
fscanf(fp,"n");
CX_Message("Data read for %d th variablen",i);
}

CX_Message("All the Data from the file was read and written in 3d.ip file sucessfullyn");

fclose(fp);
fclose(fp2);
}


DEFINE_ON_DEMAND(read_write_data)
{
Read_Data();
CX_Message("Written a 3d interpolation file named 3d.ipn");
CX_Message("Now Read the interpolation filen");
}





Show Form
No comments yet. Be the first to add a comment!