I'm trying to run a code written in C++ which should be the same as one I have in Python. They both use Fenics.
My problem is that I cannot translate a Python class into a C++ class.
For instance, my Python code is:
V = VectorFunctionSpace(mesh, "Lagrange", 1)
u = Function(V)
En = 5*dx - dot(3, u)*ds(2)
def f(self, x):
u.vector()[:] = x
return assemble(En)
The problem here is that the compiler cannot find En as it is not defined inside the class.
My C++ code:
double f(const GenericVector& x)
{
u.vector() = x;
return assemble(F);
}
int main()
{
//...things that apparently work...
Function u(Vh);
MyClass::BilinearForm F;
}
How can I solve this?
C++ has globals in the same way Python has globals.
If you want access to something defined outside your class, either prefix it with its class name and make it static ( MyClass::Thingy
), make it a global, give it an accessor function ( MyInstance.GetThingy()
), or pass it in directly as an argument to functions that use it.
As one of many methods, you can simply try defining F as global variable as follows:
...
MyClass::BilinearForm F; // The global scope
double f(const GenericVector& x)
{
u.vector() = x;
return assemble(F);
}
int main()
{
//...things that apparently work...
Function u(Vh);
// you can use F here or call the function f
...
}
Here F is a global variable and you can use it in any method in this scope. I do not know how your class is defined or whether you are using a custom namespace but you may get rid of the scope resolution (MyClass::)
part based on your implementation if BilinearForm
is an accessible class here. You can also define F
as a static
variable so if you could provide more details, I am sure you might get more precise feedback.
Hope that helps!
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.