|
[Sponsors] |
June 29, 2019, 10:13 |
UDF switching with a conditional statement
|
#1 |
Senior Member
AH
Join Date: Apr 2014
Posts: 287
Rep Power: 0 |
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) } |
|
July 1, 2019, 01:31 |
|
#2 |
Senior Member
Alexander
Join Date: Apr 2013
Posts: 2,363
Rep Power: 34 |
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) } |
|
July 1, 2019, 13:17 |
|
#3 |
Senior Member
AH
Join Date: Apr 2014
Posts: 287
Rep Power: 0 |
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. |
|
July 2, 2019, 10:19 |
Possible way is...
|
#4 |
Senior Member
AH
Join Date: Apr 2014
Posts: 287
Rep Power: 0 |
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. |
|
July 3, 2019, 00:56 |
|
#5 |
Senior Member
Alexander
Join Date: Apr 2013
Posts: 2,363
Rep Power: 34 |
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 |
|
July 3, 2019, 05:40 |
|
#6 |
Senior Member
AH
Join Date: Apr 2014
Posts: 287
Rep Power: 0 |
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? |
|
July 4, 2019, 01:26 |
|
#7 |
Senior Member
Alexander
Join Date: Apr 2013
Posts: 2,363
Rep Power: 34 |
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 |
|
July 4, 2019, 07:36 |
It's in C++
|
#8 |
Senior Member
AH
Join Date: Apr 2014
Posts: 287
Rep Power: 0 |
Just left out the beginning, declarations. Trying to get the loops corrected. It's in C++.
|
|
July 4, 2019, 09:54 |
|
#9 |
Senior Member
Join Date: Nov 2013
Posts: 1,965
Rep Power: 27 |
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. |
|
July 4, 2019, 11:03 |
It's C++, just left out declarations
|
#10 |
Senior Member
AH
Join Date: Apr 2014
Posts: 287
Rep Power: 0 |
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 } } |
|
July 4, 2019, 12:33 |
|
#11 |
Senior Member
Join Date: Nov 2013
Posts: 1,965
Rep Power: 27 |
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); } } |
|
July 4, 2019, 15:22 |
More clarification with a sketch
|
#12 |
Senior Member
AH
Join Date: Apr 2014
Posts: 287
Rep Power: 0 |
Thanks everyone, please find attached PDF sketch for problem clarification.
|
|
July 4, 2019, 16:35 |
|
#13 |
Senior Member
Join Date: Nov 2013
Posts: 1,965
Rep Power: 27 |
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"); } 1. What will your code get as input? 2. What should your code give as output? |
|
July 5, 2019, 06:33 |
|
#14 |
Senior Member
AH
Join Date: Apr 2014
Posts: 287
Rep Power: 0 |
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"); } |
|
July 5, 2019, 07:17 |
|
#15 |
Senior Member
Join Date: Nov 2013
Posts: 1,965
Rep Power: 27 |
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. |
|
July 5, 2019, 13:28 |
|
#16 |
Senior Member
AH
Join Date: Apr 2014
Posts: 287
Rep Power: 0 |
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"); } } |
|
Tags |
c++, conditional if statement, loop., udf |
|
|
Similar Threads | ||||
Thread | Thread Starter | Forum | Replies | Last Post |
Help with unsteady calculation with source/sink UDF | RobV | FLUENT | 1 | November 13, 2016 06:44 |
Unable to use if statement in UDF | AGP | Fluent UDF and Scheme Programming | 22 | June 6, 2016 11:03 |
UDF programming | fullmonty | FLUENT | 5 | June 30, 2011 03:40 |
UDF...UDF...UDF...UDF | Luc SEMINEL | FLUENT | 0 | November 25, 2002 05:03 |
UDF, UDF, UDF, UDF | Luc SEMINEL | Main CFD Forum | 0 | November 25, 2002 05:01 |