RuntimeError: Unable to compile C++ code with dijitso Cahn-Hilliard

Hey, I tried out the Cahn-Hilliard-Fenics-Python Demo:
https://fenicsproject.org/olddocs/dolfin/latest/python/demos/cahn-hilliard/demo_cahn-hilliard.py.html
but wanted to use another f like:
f(c) = c(1-c) + k_B(c*ln(c) + (1-c)*ln(1-c)) - fmin

But my way doesn’t work:

import random
from dolfin import *
from scipy import constants
import numpy as np
import math
[...]
c = variable(c)
kB = constants.k
fmin = 0.25 + 298.15*kB*ln(0.5)
f    = Expression('c[0]*(1-c[0]) + 298.15*kB*(c[0]*ln(c[0]) + (1-c[0])*ln(1-c[0])) - fmin', degree=2, fmin = fmin, kB = kB)
[...]

I also tried using only c instead of c[0] but that doesn’t change anything.

Can you provide a minimal working example?

The Expression you’ve written encapsulates C/C++ code which has variables which have not been declared or provided to the constructor (specifically c). Furthermore ln is not in the standard library. I suspect you mean to use std::log.

Changed f to:

c = variable(c)
kB = constants.k
log1 = Expression("std::log(0.5)", degree=1)
log2 = Expression("std::log(c)", degree=1, c=c)
log3 = Expression("std::log(1-c)", degree=1, c=c)
fmin = 0.25 + 298.15*kB*log1
f = c*(1-c) + 298.15*kB*(c*log2 + (1-c)*log3) - fmin

Which leads to the same RuntimeError as before sadly.

It seems like you’re still mixing up the pythonic ufl symbolic formulation with C/C++ code, specifically in your variable c = variable(c).

I’m not sure why you want to use pure C/C++ code wrapped in an Expression since you could evaluate your terms with, e.g., ufl.ln(c). Furthermore this will allow ufl to automatically compute the directional derivative should you wish to use Newton’s iterative method.

1 Like