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

UDF switching with a conditional statement

Register Blogs Community New Posts Updated Threads Search

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
Old   June 29, 2019, 09:13
Default UDF switching with a conditional statement
  #1
Senior Member
 
AH
Join Date: Apr 2014
Posts: 282
Rep Power: 13
visitor is on a distinguished road
Hello, any tips anyone please?
The UDF shown below read's an input A, then issues an output B. What I am looking for a simple way to give A=200000 a miss, say 2 times. Actioning the A=200000 once every 3 times.

The A=200000 and A=100000 are the peaks and troughs of a sine wave. Once code reads 200000, B is actioned. The next two readings of A=200000 are ignored by the loop, and actioned on third time and so on. Looping over and over.

Thanks in advance.
"
"
"
{
face_t f;
begin_f_loop(f,th)
{
if A=200000 THEN //*
B=300;
else if A=100000 THEN
B=310;
}
end_f_loop(f,th)
}
visitor is offline   Reply With Quote

Old   July 1, 2019, 00:31
Default
  #2
Senior Member
 
Alexander
Join Date: Apr 2013
Posts: 2,363
Rep Power: 34
AlexanderZ will become famous soon enoughAlexanderZ will become famous soon enough
you may use UDMI for that, in define_init macro you should define udmi_0 = 0

Code:
DEFINE_INIT(my_init_func,d)
{
face_t f;
Thread *th;
thread_loop_f(th,d)
{
begin_f_loop(f,th)
{
F_UDMI(f,th,0)=0;
}
end_f_loop(f,th)
}
}

{
i=0
face_t f;
begin_f_loop(f,th)
{
if (A=200000 && F_UDMI(f,th,0)%3 =0)
{
B=300;
F_UDMI(f,th,0)=F_UDMI(f,th,0)+1;
}
else if A=100000
B=310;
}
end_f_loop(f,th)
}
best regards
AlexanderZ is offline   Reply With Quote

Old   July 1, 2019, 12:17
Default
  #3
Senior Member
 
AH
Join Date: Apr 2014
Posts: 282
Rep Power: 13
visitor is on a distinguished road
Thanks for your help, will try it out.

Is there a C++ way, non fluent? Compiling code outside c++.

Thanks again for your help. Regards.
visitor is offline   Reply With Quote

Old   July 2, 2019, 09:19
Default Possible way is...
  #4
Senior Member
 
AH
Join Date: Apr 2014
Posts: 282
Rep Power: 13
visitor is on a distinguished road
I have rephrased wording for simplicity. If the input A read's 1, then output B is 1. Skip the next input A reading 1, B will be 0. Then act on the third A input reading 1, and output B will be 1.

{
If A=1 then B=1;
// above works on first input.
//then skip next A=1, output will be B=0.
// Third input reading A=1, output B=1
// Keep looping
}

Thanks in advance, but need to be fluent independent to start with please. Pure C++ thanks.
visitor is offline   Reply With Quote

Old   July 2, 2019, 23:56
Default
  #5
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'll not write code instead of you,
make your code, than I'll try to fix if you there are some problems

best regards
AlexanderZ is offline   Reply With Quote

Old   July 3, 2019, 04:40
Default
  #6
Senior Member
 
AH
Join Date: Apr 2014
Posts: 282
Rep Power: 13
visitor is on a distinguished road
Int I;
While
{
If A=1 then B=1;
For (I=1; I <1; I++);
If A=1 then B=0
End loop
else B=0
// above works on first input.
//then skip next A=1, output will be B=0.
// Third input reading A=1, output B=1
// Keep looping
}


I think above should do it. Is there a better quicker way. Please?
visitor is offline   Reply With Quote

Old   July 4, 2019, 00:26
Default
  #7
Senior Member
 
Alexander
Join Date: Apr 2013
Posts: 2,363
Rep Power: 34
AlexanderZ will become famous soon enoughAlexanderZ will become famous soon enough
what is it? fortran code?
I have no experience in fortran coding

if you are making UDF, why do you use fortran? use C

best regards
AlexanderZ is offline   Reply With Quote

Old   July 4, 2019, 06:36
Default It's in C++
  #8
Senior Member
 
AH
Join Date: Apr 2014
Posts: 282
Rep Power: 13
visitor is on a distinguished road
Just left out the beginning, declarations. Trying to get the loops corrected. It's in C++.
visitor is offline   Reply With Quote

Old   July 4, 2019, 08:54
Default
  #9
Senior Member
 
Join Date: Nov 2013
Posts: 1,965
Rep Power: 26
pakk will become famous soon enough
It's not C++, and also not C.


And your code and explanation of what it should do is extremely confusing.
I think that you don't really understand what you really want. Think about that first.
pakk is offline   Reply With Quote

Old   July 4, 2019, 10:03
Default It's C++, just left out declarations
  #10
Senior Member
 
AH
Join Date: Apr 2014
Posts: 282
Rep Power: 13
visitor is on a distinguished road
Here it is anyway. Basically action one A=1 to give B=1, and the next A=1, B=0. Then again if A=1, B=1. Looping on and on.

include <iostream>

int main()
{
// while loop
int i;
int A;
int B;

while (1)
// The following detects if A=1 and issues //B=1. If A=0, B=0.
if A=1
{
B=1
}
else if
A=0
{
B=0
}
//Now for the next A=1, give B=0.
// And then with while loop, back to above //when A=1, B=1. So on ... Looping
For (i=0;i<1;i++)
if A=1
{
B=0
}
}
visitor is offline   Reply With Quote

Old   July 4, 2019, 11:33
Default
  #11
Senior Member
 
Join Date: Nov 2013
Posts: 1,965
Rep Power: 26
pakk will become famous soon enough
The code still makes no sense. I can not tell you what to improve because I have no idea what you are planning to do with the for-loop (it does nothing!), why you put things in a while loop, how and when A is read, what do you want to happen if A=0 the second time, and so on, and so on.


A am taking a big guess now and guess what you want, based on the strange code that you provide and the vague description.


Step 1: make a variable "COUNTAISONE" that counts how many times A was 1.
Step 2: check if this variable is divisible by 3, if it is output B=1, if it is not output B=0.

Code:
#include <udf.h>

int COUNTAISONE = 0; /* global variable! */

int yourfunction (int A) {
 if (A==0) return(0);
 ++COUNTAISONE;
 if (COUNTAISONE<3) return(0);
 COUNTAISONE=0;
 return(1);
}

int main () {
 int A;
 while (1) {
  A = readAinwhateverywayyoudoit();
  B = yourfunction(A);
  outputhoweveryouwantit(B);
 }
}
If this is not what you want, you should learn how to tell what you want. If this is what you want, you should still learn how to tell what you want, because then you were just lucky.
pakk is offline   Reply With Quote

Old   July 4, 2019, 14:22
Default More clarification with a sketch
  #12
Senior Member
 
AH
Join Date: Apr 2014
Posts: 282
Rep Power: 13
visitor is on a distinguished road
Thanks everyone, please find attached PDF sketch for problem clarification.
Attached Files
File Type: pdf example of conditional while loop .pdf (111.8 KB, 12 views)
visitor is offline   Reply With Quote

Old   July 4, 2019, 15:35
Default
  #13
Senior Member
 
Join Date: Nov 2013
Posts: 1,965
Rep Power: 26
pakk will become famous soon enough
That is not a problem clarification, at least not a succesful one.

- Is A input to your code, or should your code make the pulse A?
- How is this in any way related to Fluent?
- What should the code DO with A en B? Print it? Give it to something else? Discard it?

Should your code just output the following four states?
AB
11
00
10
00

Then it is even easier:
Code:
while (true) {
printf("A=1, B=1\n");
printf("A=0, B=0\n");
printf("A=1, B=0\n");
printf("A=0, B=0\n");
}
And now I am done guessing what you want with your code. If you want help, give a clear description of what you want your code to do. I didn't mean to write the same text in a PDF, I wanted you to think about the text before you write it. You should answer at least the following two basic questions of your code:

1. What will your code get as input?
2. What should your code give as output?
pakk is offline   Reply With Quote

Old   July 5, 2019, 05:33
Thumbs up
  #14
Senior Member
 
AH
Join Date: Apr 2014
Posts: 282
Rep Power: 13
visitor is on a distinguished road
Thanks for reply, I did think about this. However I thought there may be something much more quick. Thanks again.

Code:
---------
while (true)
{
printf("A=1, B=1\n");
printf("A=0, B=0\n");
printf("A=1, B=0\n");
printf("A=0, B=0\n");
}
visitor is offline   Reply With Quote

Old   July 5, 2019, 06:17
Default
  #15
Senior Member
 
Join Date: Nov 2013
Posts: 1,965
Rep Power: 26
pakk will become famous soon enough
If this code does what you want, it is about as fast as you can get.

But somehow I think that this code is not what you need, and that you should clarify (to yourself most of all) what you really need.
pakk is offline   Reply With Quote

Old   July 5, 2019, 12:28
Default
  #16
Senior Member
 
AH
Join Date: Apr 2014
Posts: 282
Rep Power: 13
visitor is on a distinguished road
Worked alright after adapting into UDF, THANKS.

However had to enter a delay in between the first A=1 and B=1. Therefore

while (true)
{
If A=1
{
_delay_s(3)
//this insert is a 3 seconds delay.
//After including the delay library at top
}
B=1
}
Else
{
printf("A=0, B=0\n");
printf("A=1, B=0\n");
printf("A=0, B=0\n");
}
}
visitor is offline   Reply With Quote

Reply

Tags
c++, conditional if statement, loop., 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
Help with unsteady calculation with source/sink UDF RobV FLUENT 1 November 13, 2016 05:44
Unable to use if statement in UDF AGP Fluent UDF and Scheme Programming 22 June 6, 2016 10:03
UDF programming fullmonty FLUENT 5 June 30, 2011 02:40
UDF...UDF...UDF...UDF Luc SEMINEL FLUENT 0 November 25, 2002 04:03
UDF, UDF, UDF, UDF Luc SEMINEL Main CFD Forum 0 November 25, 2002 04:01


All times are GMT -4. The time now is 23:38.