-
Notifications
You must be signed in to change notification settings - Fork 25
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[BUG] Can't use natural log in expressions #813
Comments
A workaround for now is to do: import festim as F
import sympy as sp
import numpy as np
my_model = F.Simulation()
my_model.mesh = F.MeshFromVertices(np.linspace(0, 1))
my_model.materials = F.Material(1, 1, 0)
log_bis = sp.Function("std::log")
my_model.T = F.Temperature(log_bis(F.t))
my_model.settings = F.Settings(1e10, 1e-10)
my_model.dt = F.Stepsize(1)
my_model.initialise() |
Actually the code does not work: import festim as F
import sympy as sp
import numpy as np
my_model = F.Simulation()
my_model.mesh = F.MeshFromVertices(np.linspace(0, 1))
my_model.materials = F.Material(1, 1, 0)
log_bis = sp.Function("std::log")
my_model.T = F.Temperature(log_bis(F.t))
my_model.settings = F.Settings(1e10, 1e-10, final_time=10)
my_model.dt = F.Stepsize(1)
my_model.initialise() produces
|
The only way I found to circumvent this issue is by monkey patching the sympy C printer: import festim as F
import sympy as sp
import numpy as np
my_model = F.Simulation()
my_model.mesh = F.MeshFromVertices(np.linspace(0, 1))
my_model.materials = F.Material(1, 1, 0)
log_bis = sp.Function("std::log")
# Monkey patch the C99CodePrinter class
from sympy.printing.c import C99CodePrinter
original_print_function = C99CodePrinter._print_Function
def _custom_print_Function(self, expr):
if expr.func == log_bis:
return f"std::log({self._print(expr.args[0])})"
return original_print_function(self, expr)
C99CodePrinter._print_Function = _custom_print_Function
my_model.T = F.Temperature(log_bis(F.t))
my_model.settings = F.Settings(1e10, 1e-10, final_time=10)
my_model.dt = F.Stepsize(1)
my_model.initialise() |
A fix would be to replace this line: FESTIM/festim/temperature/temperature.py Line 41 in 47d7dc4
by ccode = sp.printing.ccode(self.value).replace("log", "std::log")
self.expression = f.Expression(ccode , t=0, degree=2) we'd have to propagate this fix in all the codebase where Maybe we could make a def ccode(string):
return sp.printing.ccode(self.value).replace("log", "std::log") But maybe not really needed. OR Monkey patch |
Describe the bug
@KulaginVladimir and I recently noticed that we can't use natural log in expressions (probably temperature, sources, boundary conditions) due to this issue
To Reproduce
Run
It produces
A solution is provided in the FEniCS discourse ticket, simply replace the string
"log"
by"std::log"
.The text was updated successfully, but these errors were encountered: