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

Save & read a vector (array) in same or other macros for next time step or iteration

Register Blogs Community New Posts Updated Threads Search

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
Old   February 28, 2019, 13:43
Default Save & read a vector (array) in same or other macros for next time step or iteration
  #1
Member
 
Peter Maroul
Join Date: May 2018
Posts: 52
Rep Power: 8
Pmaroul is on a distinguished road
Hello all;

How can we save a written vector(array) (for example 2*30 ) and read (use) it for other or same UDF macros in the next time step or iterations?

Thanks a lot for any advice.

P.Maroul
Pmaroul is offline   Reply With Quote

Old   March 3, 2019, 18:55
Default
  #2
Member
 
Peter Maroul
Join Date: May 2018
Posts: 52
Rep Power: 8
Pmaroul is on a distinguished road
Maybe I couldn't ask my question clearly, therefore I try to re-raise my question with a Pseudocode .


Code:
#include udf.h
for(int i=0;i<n0;i++)
for(int j=0;j<m0;j++)
A(i,j)= A(i,j)+ /*some modifications or updates*/

DEFINE_ADJUST(my_adjust,d)
{
.
.
.

/*some written text codes*/
.
.

}
Is the updating of this array(A(i,j) ) possible or this quantity re-initialized to zero in every iteration?
Pmaroul is offline   Reply With Quote

Old   March 3, 2019, 21:38
Default
  #3
Senior Member
 
Alexander
Join Date: Apr 2013
Posts: 2,363
Rep Power: 34
AlexanderZ will become famous soon enoughAlexanderZ will become famous soon enough
Code:
#include udf.h
int n,m,i;
real **A;
A= (real**)malloc((m)*sizeof(real*));
for (i=0; i<m; i++)
		{
			A[i] = (real*)malloc(n*sizeof(real));
		}
you may use this array everywhere and update whenever you want

best regards
AlexanderZ is offline   Reply With Quote

Old   March 4, 2019, 06:15
Default
  #4
Member
 
Peter Maroul
Join Date: May 2018
Posts: 52
Rep Power: 8
Pmaroul is on a distinguished road
Quote:
Originally Posted by AlexanderZ View Post
Code:
#include udf.h
int n,m,i;
real **A;
A= (real**)malloc((m)*sizeof(real*));
for (i=0; i<m; i++)
		{
			A[i] = (real*)malloc(n*sizeof(real));
		}
you may use this array everywhere and update whenever you want

best regards
Thanks sir so much.

How about two-dimensional arrays?
Is the following Pseudocode right?
Code:
#include udf.h
int n,m,i;
real **A;
A= (real**)malloc((m)*sizeof(real*));
for (j=0; j<m; j++)
{
for (i=0; i<n; i++)
		{
			A[i][j] = (real*)malloc(n*sizeof(real));
		}}
Moreover, is the file printing a good idea(For example , rewriting a new matrix in the file and its reading for the next iteration ) specially if matrix (array) post-processing is to be important for us in any time step?
Pmaroul is offline   Reply With Quote

Old   March 4, 2019, 06:27
Default
  #5
Senior Member
 
Join Date: Nov 2013
Posts: 1,965
Rep Power: 27
pakk will become famous soon enough
No, your code is not correct.


See for example here how to correctly allocate a 2D array in c.
pakk is offline   Reply With Quote

Old   March 4, 2019, 21:48
Default
  #6
Senior Member
 
Alexander
Join Date: Apr 2013
Posts: 2,363
Rep Power: 34
AlexanderZ will become famous soon enoughAlexanderZ will become famous soon enough
I gave you code for memory allocation for 2D array (matrix)
than you may define value of each element in matrix using 2 loops for i and j

you may do whatever you want. but reading/writing to file may take some time, depends on the size of your matrix. play with it.

reading/writing to file functions are different for single core computation and parallel approach, take it into account

best regards
AlexanderZ is offline   Reply With Quote

Old   March 5, 2019, 16:27
Default
  #7
Member
 
Peter Maroul
Join Date: May 2018
Posts: 52
Rep Power: 8
Pmaroul is on a distinguished road
Thanks Sirs very much !

If you're not in a hurry, can you send a Pseudocode executing the matrix's printing(A[i,j]) in a file, for example, in intervals of every ten time steps, and then from which part of the FLUENT options, the output of this matrix is visible?

for example for a 2D array , how can I plot A[i][1] versus A[i][2] in each 10 deltaT in Fluent or other post processing tools?

Thanks in advance for any help.
Pmaroul is offline   Reply With Quote

Old   March 21, 2019, 11:10
Default
  #8
Member
 
Peter Maroul
Join Date: May 2018
Posts: 52
Rep Power: 8
Pmaroul is on a distinguished road
Quote:
Originally Posted by AlexanderZ View Post
I gave you code for memory allocation for 2D array (matrix)
than you may define value of each element in matrix using 2 loops for i and j

you may do whatever you want. but reading/writing to file may take some time, depends on the size of your matrix. play with it.

reading/writing to file functions are different for single core computation and parallel approach, take it into account

best regards
Dear Alexander.

Are the n, m constant parameters dynamic allocated in udf's , too?
Pmaroul is offline   Reply With Quote

Old   March 22, 2019, 07:18
Default
  #9
Member
 
Peter Maroul
Join Date: May 2018
Posts: 52
Rep Power: 8
Pmaroul is on a distinguished road
Quote:
Originally Posted by AlexanderZ View Post
Code:
#include udf.h
int n,m,i;
real **A;
A= (real**)malloc((m)*sizeof(real*));  /*command line4*/
for (i=0; i<m; i++)
		{
			A[i] = (real*)malloc(n*sizeof(real));
		}
you may use this array everywhere and update whenever you want

best regards
Dear Alexander

I compiled your suggested sub-code and encountered below error:

error C2040: 'A' : 'int ' differs in levels of indirection from 'double ** ' for the command line 4:"A= (real**)malloc((m)*sizeof(real*));"

How can I resolve this error?
Pmaroul is offline   Reply With Quote

Old   March 25, 2019, 01:27
Default
  #10
Senior Member
 
Alexander
Join Date: Apr 2013
Posts: 2,363
Rep Power: 34
AlexanderZ will become famous soon enoughAlexanderZ will become famous soon enough
Code:
#include "udf.h"
int n = 10,m =10;
real **A;

DEFINE_ON_DEMAND(Allocate_memory)
{
int i,j;
A= (real**)malloc((m)*sizeof(real*));
for (i=0; i<m; i++)
		{
			A[i] = (real*)malloc(n*sizeof(real));
		}
}

DEFINE_ON_DEMAND(Define_matrix)
{
	int i,j;
	for (j=0; j<m; j++)
	{	for (i=0; i<n; i++)
		{
			A[i][j] = i+j;
		}
	}
}

DEFINE_ON_DEMAND(Free_memory)
{
	free(A);
}
best regards
AlexanderZ is offline   Reply With Quote

Old   March 30, 2019, 07:05
Default
  #11
Member
 
Peter Maroul
Join Date: May 2018
Posts: 52
Rep Power: 8
Pmaroul is on a distinguished road
Dear Alexander.

Your answer was so helpful!!. Really I had to define that array in a define-on-demand and then to update in other macros.

Good luck and best wish for you.
Pmaroul is offline   Reply With Quote

Reply

Tags
next time step, read array, save array


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
AMI speed performance danny123 OpenFOAM 21 October 24, 2020 05:13
Transient simulation not converging skabilan OpenFOAM Running, Solving & CFD 14 December 17, 2019 00:12
Kernel for new CPUs Simbelmynė Hardware 22 January 5, 2018 17:41
High Courant Number @ icoFoam Artex85 OpenFOAM Running, Solving & CFD 11 February 16, 2017 14:40
Floating point exception error lpz_michele OpenFOAM Running, Solving & CFD 53 October 19, 2015 03:50


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