We have moved the forum to https://forum.ngsolve.org . This is an archived version of the topics until 05/05/23. All the topics were moved to the new forum and conversations can be continued there. This forum is just kept as legacy to not invalidate old links. If you want to continue a conversation just look for the topic in the new forum.

Notice

The forum is in read only mode.

using functions for CoefficientFunction

3 years 7 months ago #2972 by sm22
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() R=0.8 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!!
3 years 7 months ago #2974
Hi sm22,

the IfPos CoefficientFunction should work
Code:
IfPos(x-R,1/(x-R),0)

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 .

Best
Michael
Time to create page: 0.102 seconds