using functions for CoefficientFunction

Hello NGSolve,

I need to use a coefficient function that depends on the spatial coordinates. I tried the following:

[code]#!/usr/bin/env python

coding: utf-8

import netgen.gui
from ngsolve import *
from netgen.csg import *
from netgen.geom2d import unit_square

initialise mpi

comm = mpi_world
rank = comm.rank
nproc= comm.size

mesh = Mesh(unit_square.GenerateMesh(maxh=0.2))

fes = H1(mesh, complex=True, order=2)
u = fes.TrialFunction()
v = fes.TestFunction()
class myCoeff:
def init(self):
self.R = R

def eval(self, value, x):
    outside  = ( x >= R )
    coeff = 1. / (x - R)
    value = outside * coeff       

C = CoefficientFunction(myCoeff)

want to use a coefficient in the bilinear form:

a = BilinearForm (fes, symmetric=True)

a += SymbolicBFI (C*grad(u)*grad(v))


But the class can not be an argument for the CoefficientFunction. How to incorporate slightly involved functions such as the one above to be used in bilinear form? Thank you!!

Hi sm22,

the IfPos CoefficientFunction should work


see also the documentation for CoefficientFunctions. If you need a fancy CoefficientFunction which is not available there is the possibility to define your own CoefficientFunction in C++ and then to export it to Python, see this link.