Can ngsolve automatically recognize the linear form & bilinear form from a given weak form Instead of compute them manually?
(I mean the following fenicsx codes can automatically recognize the linear form & bilinear form.
u, v = ufl.TrialFunction(V), ufl.TestFunction(V)
F = u * v * ufl.dx + dt * ufl.dot(ufl.grad(u), ufl.grad(v)) * ufl.dx - (u_n + dt * f) * v * ufl.dx
a = fem.form(ufl.lhs(F))
L = fem.form(ufl.rhs(F))
Here are suggestions for ufl-like rhs/lhs functions:
all trial-functions are found with equ.GetProxies().
For the lhs, we take the directional derivatives with respect to proxies, into the direction of proxies. Since grad(u).Diff(grad(u), grad(u))
returns grad(u)
, as well as grad(u).Diff(u,u)
returns the same, we take only the derivatives with proxies having a named derivative (a hack, for the moment).
For the rhs, we replace all trial-functions by zero.
from ngsolve import *
mesh = Mesh(unit_square.GenerateMesh(maxh=0.2))
fes = H1(mesh, order=1)
u,v = fes.TnT()
def lhs(equ):
return sum(equ.Diff(proxy,proxy) for proxy in equ.GetProxies(trial=True) if proxy.derivname!='')
def rhs(equ):
repl = dict((proxy, CF((0,)*proxy.dim).Reshape(proxy.shape)) for proxy in equ.GetProxies(trial=True))
return equ.Replace(repl)
dt = 0.1
u_n = GridFunction(fes)
f = x*y
F = u * v * dx + dt * InnerProduct(grad(u), grad(v)) * dx - (u_n + dt * f) * v * dx
a = lhs(F)
print ("lhs = ", a)
L = rhs(F)
print ("rhs =", L)```