Dear All,
I would like to define a Function in C++.
The question is: How can we divide by a function?
Here is some code to explain the issue.
First some declarations:
// Create mesh and function space
int nx = 10;
IntervalMesh mesh(nx, 0, 1);
Band::FunctionSpace V(mesh);
// Set parameter values
double g0 = 5;
double eta = .05;
// Create functions
Function u(V);
Function f(V);
Function g(V);
Now initialisation
//Create intial conditions and interpolate
SourceIni u_init; //u_init is zero plus a small gaussian noise
u.interpolate(u_init);
//initial condition for function f
//compute <u>
double umean=0;
for (MeshEntityIterator e(mesh, 0); !e.end(); ++e){
umean+=(double)(*u.vector())[e->index()];
}
umean=umean/nx;
Now I want to create g(x) = g0 + (<u> – u(x))/eta.
I do it in this way, which works, even though it doesn't look pretty (any comments on that?):
*g.vector() = *u.vector();
*g.vector() -= umean;
*g.vector() *= -1./eta;
*g.vector() += g0;
Here is where I need help:
I want to define f(x) = g(x)/(1 + g(x)). Note that g represents a physical quantity that needs to be positive.
//THIS DOES NOT WORK:
*temp.vector() = *g.vector();
*temp.vector() += 1.;
f = project(g/temp, V);
L.f = f;
Later on, there is a time loop, in which the function u is calculated at each time step, and g and f need to be recomputed at each time step too, as I need to update L.
Thanks for any hints!
PS the .ufl file is:
element = FiniteElement("Lagrange", interval, 1)
k = Constant("interval")
D = 0.001
# Define variational problem
u = TrialFunction(element)
v = TestFunction(element)
u1 = Coefficient(element)
f = Coefficient(element)
a = ((1 + k)*inner(u,v) + D*k*inner(grad(u),grad(v)))*dx
L = inner((u1 + k*f),v)*dx