Discussion Closed This discussion was created more than 6 months ago and has been closed. To start a new discussion with a link back to this one, click here.

Issue with the built-in functions (such as if(), floor() ) in COMSOL

Please login with a confirmed email address before reporting spam

I am facing an issue while running the Charge-Discharge Cycling simulation in Lithium-Ion Battery Interface. For the context, there are some variables (cycle_no, t_factor, corr_factor1_) defined in the "Definitions" node under the "comp1" node with the expression mentioned as follows. Also, a required interpolation function ("t_factor_int") is defined earlier in the global definitions.


t_factor = t_factor_int(comp1.liion.cdc1.cycle_counter)

corr_factor1_ = if(t_factor==1,1,0)

cycle_no = if(comp1.liion.cdc1.cycle_counter<3,comp1.liion.cdc1.cycle_countert_factor,(comp1.liion.cdc1.cycle_counter-floor(comp1.liion.cdc1.cycle_counter/3))250+corr_factor1_)


For the current battery cycling study, I need to supply charge and discharge current as a function of cycle number in the Charge-Discharge Cycling boundary condition, which are defined as:

Discharge Current = if((cycle_no==0)+(mod(cycle_no,10)==1),-i_1C/3,-i_1C); Charge Current = i_1C

(i_1C is the 1C current value defined earlier in the global definitions)

After running the simulations for some cycles, the following issues arise.

Issue with the if() function: The if() function in the "corr_factor1_" variables is not updated as expected i.e., does not give value as 1 if "t_factor" is 1.

Issue with the floor() function: The floor() function too does not work as expected. For example, while calculating "floor(liion.cdc1.cycle_counter/3)", the output obtained for floor(39/3) is 12 instead of 13.

The aforementioned issues continue for further cycles although they weren't observed during the earlier cycles of the same run.


2 Replies Last Post Nov 23, 2023, 2:29 a.m. EST
Magnus Ringh COMSOL Employee

Please login with a confirmed email address before reporting spam

Posted: 11 months ago Nov 22, 2023, 8:54 a.m. EST

Hi Shantanu,

I don't know your entire model setup, but I think it can be better to not do exact comparisons to 1 and 0 when you are dealing with numerical simulations using floating-point numbers. Some numerical noise might make such comparisons return false, since the value that you compare to 1 isn't exactly 1 but 1 plus or minus a few "eps" (the machine precision number). Instead of t_factor==1 you might use abs(tfactor-1)<100*eps or something similar. For the floor case, floor(round(liion.cdc1.cycle_counter)/3) might be more robust.

Best regards,

Magnus

Hi Shantanu, I don't know your entire model setup, but I think it can be better to not do exact comparisons to 1 and 0 when you are dealing with numerical simulations using floating-point numbers. Some numerical noise might make such comparisons return false, since the value that you compare to 1 isn't exactly 1 but 1 plus or minus a few "eps" (the machine precision number). Instead of `t_factor==1` you might use `abs(tfactor-1)

Please login with a confirmed email address before reporting spam

Posted: 11 months ago Nov 23, 2023, 2:29 a.m. EST

Hi Shantanu,

I don't know your entire model setup, but I think it can be better to not do exact comparisons to 1 and 0 when you are dealing with numerical simulations using floating-point numbers. Some numerical noise might make such comparisons return false, since the value that you compare to 1 isn't exactly 1 but 1 plus or minus a few "eps" (the machine precision number). Instead of t_factor==1 you might use `abs(tfactor-1)

Hi Magnus,

Thanks for a quick reply. The floor() function issue was resolved by the change you suggested using round() in its expression. As for the if() function, it worked by using the round() function if(round(t_factor)==1,1,0).

As you suggested, the issue was the floating point error as the value of t_factor may have been 0.99999... so the if() expression returned false.

Thanks and Regards, Shantanu

>Hi Shantanu, > >I don't know your entire model setup, but I think it can be better to not do exact comparisons to 1 and 0 when you are dealing with numerical simulations using floating-point numbers. Some numerical noise might make such comparisons return false, since the value that you compare to 1 isn't exactly 1 but 1 plus or minus a few "eps" (the machine precision number). Instead of `t_factor==1` you might use `abs(tfactor-1) Hi Magnus, Thanks for a quick reply. The floor() function issue was resolved by the change you suggested using round() in its expression. As for the if() function, it worked by using the round() function if(round(t_factor)==1,1,0). As you suggested, the issue was the floating point error as the value of t_factor may have been 0.99999... so the if() expression returned false. Thanks and Regards, Shantanu

Note that while COMSOL employees may participate in the discussion forum, COMSOL® software users who are on-subscription should submit their questions via the Support Center for a more comprehensive response from the Technical Support team.