UDF switching with a conditional statement

 Register Blogs Members List Search Today's Posts Mark Forums Read

 June 29, 2019, 09:13 UDF switching with a conditional statement #1 Senior Member   AH Join Date: Apr 2014 Posts: 282 Rep Power: 13 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, 00: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) }``` best regards

 July 1, 2019, 12:17 #3 Senior Member   AH Join Date: Apr 2014 Posts: 282 Rep Power: 13 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, 09:19 Possible way is... #4 Senior Member   AH Join Date: Apr 2014 Posts: 282 Rep Power: 13 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 2, 2019, 23: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, 04:40 #6 Senior Member   AH Join Date: Apr 2014 Posts: 282 Rep Power: 13 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, 00: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, 06:36 It's in C++ #8 Senior Member   AH Join Date: Apr 2014 Posts: 282 Rep Power: 13 Just left out the beginning, declarations. Trying to get the loops corrected. It's in C++.

 July 4, 2019, 08:54 #9 Senior Member   Join Date: Nov 2013 Posts: 1,965 Rep Power: 26 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, 10:03 It's C++, just left out declarations #10 Senior Member   AH Join Date: Apr 2014 Posts: 282 Rep Power: 13 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 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, 11:33 #11 Senior Member   Join Date: Nov 2013 Posts: 1,965 Rep Power: 26 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 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.

July 4, 2019, 14:22
More clarification with a sketch
#12
Senior Member

AH
Join Date: Apr 2014
Posts: 282
Rep Power: 13
Thanks everyone, please find attached PDF sketch for problem clarification.
Attached Files
 example of conditional while loop .pdf (111.8 KB, 14 views)

 July 4, 2019, 15:35 #13 Senior Member   Join Date: Nov 2013 Posts: 1,965 Rep Power: 26 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?

 July 5, 2019, 05:33 #14 Senior Member   AH Join Date: Apr 2014 Posts: 282 Rep Power: 13 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, 06:17 #15 Senior Member   Join Date: Nov 2013 Posts: 1,965 Rep Power: 26 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, 12:28 #16 Senior Member   AH Join Date: Apr 2014 Posts: 282 Rep Power: 13 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