CFD Online Logo CFD Online URL
www.cfd-online.com
[Sponsors]
Home > Forums > Software User Forums > ANSYS > FLUENT > Fluent UDF and Scheme Programming

Creating an external file with data

Register Blogs Community New Posts Updated Threads Search

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
Old   January 30, 2019, 18:00
Default Creating an external file with data
  #1
New Member
 
GenD
Join Date: Nov 2017
Posts: 23
Rep Power: 9
GenD is on a distinguished road
Hi,
I'm trying to create an external file with data after each time step. To do this I use DEFINE_EXECUTE_AT_END macro, but I've some problems.

First of all, in the generated file, the only thing that is saving is the data for the last time step, but I want to have it for every one, so I suppose it just overwrites every time step. I thought I will omit this issue by using arrays, but I just can't get through with them. Could you please help me solve it?

Secondly, there happen to be another problem with macros for temperature (C_T(c,t)) and other, cause ANSYS says that there are no initialized variables of c and t, but in my code I have them.

I will put code in here.

Maybe whole idea is wrong? Could you please assist me with this?

Code:
#include "udf.h"

int i = 0;

real rho[10000];
real T[10000];
real velocity[10000];
real r_num[10000];
real time[10000];

DEFINE_EXECUTE_AT_END(write_file)
{
		
		Thread *t;
		cell_t c;
		T[i] = C_T(c, t);
		rho[i] = C_R(c, t);
		real mu = 0.001003;
		velocity[i] = C_V(c, t);
		real diameter = 0.038;
		r_num[i] = ((rho[i] * pow(diameter, 2) * velocity[i]) / mu);
		time[i] = CURRENT_TIME;

		FILE *fptr;

		fptr = fopen("example.txt", "w");

		fprintf(fptr, "Temperature       Density       Viscosity       Reynolds number       Time\n");
		fprintf(fptr, "%4.2f %4.2f %4.6f %4.2f %4.3f\n", T[i], rho[i], mu, r_num[i], time[i]);

		fclose(fptr);

		i++;
}
GenD is offline   Reply With Quote

Old   January 30, 2019, 21:30
Default
  #2
Senior Member
 
Join Date: Feb 2010
Posts: 164
Rep Power: 17
gearboy is on a distinguished road
Quote:
Originally Posted by GenD View Post
Hi,
I'm trying to create an external file with data after each time step. To do this I use DEFINE_EXECUTE_AT_END macro, but I've some problems.

First of all, in the generated file, the only thing that is saving is the data for the last time step, but I want to have it for every one, so I suppose it just overwrites every time step. I thought I will omit this issue by using arrays, but I just can't get through with them. Could you please help me solve it?

Secondly, there happen to be another problem with macros for temperature (C_T(c,t)) and other, cause ANSYS says that there are no initialized variables of c and t, but in my code I have them.

I will put code in here.

Maybe whole idea is wrong? Could you please assist me with this?

Code:
#include "udf.h"

int i = 0;

real rho[10000];
real T[10000];
real velocity[10000];
real r_num[10000];
real time[10000];

DEFINE_EXECUTE_AT_END(write_file)
{
        
        Thread *t;
        cell_t c;
        T[i] = C_T(c, t);
        rho[i] = C_R(c, t);
        real mu = 0.001003;
        velocity[i] = C_V(c, t);
        real diameter = 0.038;
        r_num[i] = ((rho[i] * pow(diameter, 2) * velocity[i]) / mu);
        time[i] = CURRENT_TIME;

        FILE *fptr;

        fptr = fopen("example.txt", "w");

        fprintf(fptr, "Temperature       Density       Viscosity       Reynolds number       Time\n");
        fprintf(fptr, "%4.2f %4.2f %4.6f %4.2f %4.3f\n", T[i], rho[i], mu, r_num[i], time[i]);

        fclose(fptr);

        i++;
}
You can use "append" flag instead of "overwrite" flag when opening a file.
fptr = fopen("example.txt", "a+");
gearboy is offline   Reply With Quote

Old   January 31, 2019, 20:05
Default
  #3
New Member
 
GenD
Join Date: Nov 2017
Posts: 23
Rep Power: 9
GenD is on a distinguished road
Thank you gearboy. It really helped me.

Now, I struggle with the second problem. The code looks now like this.
Code:
#include "udf.h"

int i = 0;

real rho[10000];
real T[10000];
real velocity[10000];
real r_num[10000];
real time[10000];

cell_t c;
Thread *t;

DEFINE_ON_DEMAND(on_demand_write)
{
	FILE *fptr;

	fptr = fopen("example.txt", "w");

	fprintf(fptr, "Temperature       Density       Viscosity       Reynolds number       Time\n");

	fclose(fptr);
	
}

DEFINE_EXECUTE_AT_END(write_file_at_end)
{

		T[i] = C_T(c, t);
		rho[i] = C_R(c, t);
		real mu = 0.001003;
		velocity[i] = C_V(c, t);
		real diameter = 0.038;
		r_num[i] = ((rho[i] * pow(diameter, 2) * velocity[i]) / mu);
		time[i] = CURRENT_TIME;

		FILE *fptr;

		fptr = fopen("example.txt", "a+");

		fprintf(fptr, "%4.2f                 %4.2f        %4.6f              %4.2f            %4.3f\n", T[i], rho[i], mu, r_num[i], time[i]);

		fclose(fptr);

		i++;
It compiles fine, without any error, but then after I start simulation, I got an error:

Node 0: Process 14620: Received signal SIGSEGV.

================================================== ============================
MPI Application rank 0 exited before MPI_Finalize() with status 2
The fl process could not be started.


I think it is connected with macros C_T(c, t), C_R(c, t) and C_V(c, t), because, when I put instead of those macros just some numbers, it works fine. Do you have any idea, how to solve this?
GenD is offline   Reply With Quote

Old   February 1, 2019, 05:11
Default
  #4
Senior Member
 
Join Date: Feb 2010
Posts: 164
Rep Power: 17
gearboy is on a distinguished road
Quote:
Originally Posted by GenD View Post
Thank you gearboy. It really helped me.

Now, I struggle with the second problem. The code looks now like this.
Code:
#include "udf.h"

int i = 0;

real rho[10000];
real T[10000];
real velocity[10000];
real r_num[10000];
real time[10000];

cell_t c;
Thread *t;

DEFINE_ON_DEMAND(on_demand_write)
{
    FILE *fptr;

    fptr = fopen("example.txt", "w");

    fprintf(fptr, "Temperature       Density       Viscosity       Reynolds number       Time\n");

    fclose(fptr);
    
}

DEFINE_EXECUTE_AT_END(write_file_at_end)
{

        T[i] = C_T(c, t);
        rho[i] = C_R(c, t);
        real mu = 0.001003;
        velocity[i] = C_V(c, t);
        real diameter = 0.038;
        r_num[i] = ((rho[i] * pow(diameter, 2) * velocity[i]) / mu);
        time[i] = CURRENT_TIME;

        FILE *fptr;

        fptr = fopen("example.txt", "a+");

        fprintf(fptr, "%4.2f                 %4.2f        %4.6f              %4.2f            %4.3f\n", T[i], rho[i], mu, r_num[i], time[i]);

        fclose(fptr);

        i++;
It compiles fine, without any error, but then after I start simulation, I got an error:

Node 0: Process 14620: Received signal SIGSEGV.

================================================== ============================
MPI Application rank 0 exited before MPI_Finalize() with status 2
The fl process could not be started.


I think it is connected with macros C_T(c, t), C_R(c, t) and C_V(c, t), because, when I put instead of those macros just some numbers, it works fine. Do you have any idea, how to solve this?
For the confusing errors, you have to debug and trace the code step by step.
gearboy is offline   Reply With Quote

Old   February 1, 2019, 07:36
Default
  #5
New Member
 
GenD
Join Date: Nov 2017
Posts: 23
Rep Power: 9
GenD is on a distinguished road
Yes, that's exactly what I've done and it proved that a problem is with macros C_T(c, t), C_R(c, t) and C_V(c, t), and I don't know why. Does anyone know the reason?
GenD is offline   Reply With Quote

Old   February 2, 2019, 08:25
Default
  #6
New Member
 
GenD
Join Date: Nov 2017
Posts: 23
Rep Power: 9
GenD is on a distinguished road
I've been working to make UDF work by the use of Customization Manual and cases written in all kinds of forums and I've developed a code. BUT it does not work for me, what is funny because the errors are with macros. I've tried to replace macros (C_T(c,t), C_R(c,t), C_V(c,t), C_VOLUME(c,t) ) with number 1 and it worked properly, so i don't get how to omit this. Actual version of the code is below, so if somebody knows the issue, I will be glad if you help me.

In addition I can say that I've tried to use final, proper codes from forums and Customization Manual connected with macros given above and those didn't work too.

Code:
#include "udf.h"

cell_t c;
Thread *t;
Domain *d;

DEFINE_EXECUTE_AT_END(write_file_at_end)
{
	real vol_tot = 0;
	real volume = 0;
	real mu = 0;
	real rho = 0;
	real T = 0;
	real velocity = 0;
	real r_num = 0;
	real time = 0;
	real Tavg = 0;
	real rhoavg = 0;
	real velavg = 0;

	d = Get_Domain(1);
	int thread_num = 6;
	t = Lookup_Thread(d, thread_num);

	thread_loop_c(t, d)
	{

		begin_c_loop(c, t)
		{
			T = C_T(c, t);
			rho = C_R(c, t);
			velocity = C_V(c, t);
			time = CURRENT_TIME;
			volume = C_VOLUME(c, t);
			vol_tot += volume;
			Tavg += T * volume;
			rhoavg += rho * volume;
			velavg += velocity * volume;

		}
		end_c_loop(c, t)

		Tavg /= vol_tot;
		rhoavg /= vol_tot;
		velavg /= vol_tot;
		mu = 0.001003;
		real diameter = 0.038;
		r_num = ((rhoavg * pow(diameter, 2) * velavg) / mu);
	}


		FILE *fptr;

		fptr = fopen("example.txt", "a+");

		fprintf(fptr, "%4.2f                 %4.2f        %4.6f              %4.2f            %4.3f\n", Tavg, rhoavg, mu, r_num, time);

		fclose(fptr);
}

DEFINE_ON_DEMAND(on_demand_write)
{
	FILE *fptr;

	fptr = fopen("example.txt", "w");

	fprintf(fptr, "Temperature       Density       Viscosity       Reynolds number       Time\n");

	fclose(fptr);

}
GenD is offline   Reply With Quote

Old   February 4, 2019, 11:22
Default
  #7
Senior Member
 
Join Date: Nov 2013
Posts: 1,965
Rep Power: 27
pakk will become famous soon enough
You replaced macros C_T(c,t), C_R(c,t), C_V(c,t) and C_VOLUME(c,t) with number 1, and it worked.


Did you try to replace macros C_T(c,t) and C_R(c,t) with one (so leaving C_V(c,t) and C_VOLUME(c,t))?
If that gives no error, you know the problem is in C_T or C_R.
If so, try only C_T.


And make sure (double sure) that you initialize your data before you run this macro.
pakk is offline   Reply With Quote

Old   February 7, 2019, 16:14
Default
  #8
New Member
 
GenD
Join Date: Nov 2017
Posts: 23
Rep Power: 9
GenD is on a distinguished road
Hi,
I've applied your plan of troubleshooting and as a result I got that C_T(c, t) (so temperature) is a problem. Any other like macros for density, volume, etc. work well. Do you have any idea, why is that?

Yes, of course I initialize data before calculations. It is impossible do to so without it.
GenD is offline   Reply With Quote

Reply

Tags
execute at end, external file, udf


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
CFD by anderson, chp 10.... supersonic flow over flat plate varunjain89 Main CFD Forum 18 May 11, 2018 08:31
[OpenFOAM.org] Error creating ParaView-4.1.0 OpenFOAM 2.3.0 tlcoons OpenFOAM Installation 13 April 20, 2016 18:34
[foam-extend.org] problem when installing foam-extend-1.6 Thomas pan OpenFOAM Installation 7 September 9, 2015 22:53
[Other] Adding solvers from DensityBasedTurbo to foam-extend 3.0 Seroga OpenFOAM Community Contributions 9 June 12, 2015 18:18
"parabolicVelocity" in OpenFoam 2.1.0 ? sawyer86 OpenFOAM Running, Solving & CFD 21 February 7, 2012 12:44


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