Using if loops or min/max functions

40 views
Skip to first unread message

nathan magnan

unread,
Oct 24, 2025, 8:39:43 AMOct 24
to Dedalus Users
Hello,

One of my substitutions links viscosity to temperature:
nu_func = lambda T: nu_max * np.tanh(np.exp(Nnu * (NT / T - 1)) / nu_max)
namespace[f'nu'] = nu_func(T)
The issue is that if Nnu * (NT / T - 1) is larger than 1000, the exponential overflows.
Physically, large value of  Nnu * (NT / T - 1 are irrelevant, since the tanh saturates as soon as Nnu * (NT / T - 1) is larger than 10.
Therefore, I would be completely with something like this
def nu_func(T):
    if (Nnu * (NT / T - 1) <= 10):
        return nu_max * np.tanh(np.exp(Nnu * (NT / T - 1)) / nu_max)
    else:
        return nu_max
But the if loop is rejected by dedalus, and I get the following error:
TypeError: operand type(s) all returned NotImplemented from __array_ufunc__(<ufunc 'less'>, '__call__', array(13.12236338), Mul(0.1, Add(Mul(2500001.0000000023, Pow(Add(Convert(Convert(<Field 127701709158352>)), Mul(2500000.0000000023, <Field 127701846261968>)), -1)), Convert(Convert(<Field 127701847398800>))))): 'ndarray', 'MultiplyNumberField'

I've tried the equivalent trick using the min function, but got a similar error.

What's the dedalus way to work around this?

Thanks for your help !
Nathan

Daniel Lecoanet

unread,
Oct 24, 2025, 8:44:06 AMOct 24
to dedalu...@googlegroups.com
Hi Nathan,

You want to use a GeneralFunction. You can search the mailing list for more information about this.

Daniel

--
You received this message because you are subscribed to the Google Groups "Dedalus Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to dedalus-user...@googlegroups.com.
To view this discussion visit https://groups.google.com/d/msgid/dedalus-users/2778534d-cafb-4902-88a4-8657ca714e10n%40googlegroups.com.

nathan magnan

unread,
Oct 24, 2025, 9:20:36 AMOct 24
to Dedalus Users
Hi Daniel,

Thanks for the swift answer !

I haven't tried yet, I'm gonna do that.
Though I'm worried I might run into the same issue as described in the thread "General function implentation in equation has no effect", because I'm also solving a NLBVP.

Best regards,
Nathan

Daniel Lecoanet

unread,
Oct 26, 2025, 12:12:00 PMOct 26
to dedalu...@googlegroups.com
Hi Nathan,

The way that we solve NLBVPs is to linearize the equations and use Newton’s method to find a solution to the linearized problem. It is tricky to linearize an if statement. I would recommend you use a smoothed version of the if statement if you want to use an NLBVP. This way you would not need to use a GeneralFunction.

Daniel

Reply all
Reply to author
Forward
0 new messages