Subject: [FEniCS-users] Weird error with dolfin::Function.vector().get()

From: phil marinier <[email-masked]>
Date: Wed, 19 Aug 2009 23:45:20 -0400

Using print statments I isolated the error to these 2 statements:

double nodes[Phi.vector().size()];
Phi.vector().get(nodes);

I moved them around my program to see if where the problem starts. They work fine in the main body of the function, but as soon as I pass Function Phi, the error shows up. This is the relevant code snippet:

    VariationalProblem problem(a, L, bcs);
    problem.solve(Phi0);
    
    //recalculate current density and potential distributions at interface, put answer in phiBC1
    phiBC1 = new double[numNodes];
    gradients = new double[totNodes];
    LGrad.f = Phi0;
    VariationalProblem grads0(aGrad, LGrad);
    grads0.solve(Gradients0);
    Gradients0.vector().get(gradients);
    IntCurrent(gradients, domain, anode, cathode, rho, iInt);
    PhiDistribution(anode, cathode, domain, Phi0, iInt, phiBC1);

LGrad is the linear form used to get the gradients. declared above. As far as I can tell it works just fine in my main program, it worked beutifully in my test program before I copied it to my main. 

I copied the 2 statments that form the vector().get() right before PhiDistribution() and there was no error. I copied the 2 statements as the first line of PhiDistribution, and I had the same error. These are the first couple lines of PhiDistribution as they are when I get the error:

void PhiDistribution(Anode *anode, Cathode *cathode, Domain *domain, Function Phi, double *i, double *phi)
{
    double nodes[Phi.vector().size()];
    Phi.vector().get(nodes);
    ....

It blows up at that line. Somehow the act of passing the Function from the main, to the function introduces an error. The weirdest part is that I successfully pass Functions this way, and call the same 2 problematic lines earlier in my program and theres no problem. I am using namespace dolfin.

What I would like to know is what kind of things could cause this kind of error. I check for memory allocation, as in new//delete pairs, as per a suggestion, and that didn't seem to be it, so I completely eliminated new/delete by creating nodes right before I used it. I checked the Phi.vector().size() and it outputted exactly the size i expected, the total number of nodes in the domain, so it appears to still be defined over the right FunctionSpace. I ploted Phi0 in my main and it plotted successfully and looked good. I tried plot(Phi) in my PhiDistribution and I got the same segmentation error as I got before. So this confirmed to me that there was an error with my passing of the Function. However, I have been passing Functions this way ever since I knew enough about dolfin to pass a Function and this is the first time I have a problem. This is the code with the plotting:

void PhiDistribution(Anode *anode, Cathode *cathode, Domain *domain, Function Phi, double *i, double *phi)
{
    plot(Phi);

I left out the cout statments that let me know exactly where the problem is. 

I usually would not ask for help with debugging my own code and I still feel bad asking you guys for help, but I really don't know how to proceed from here. Again, any help would be appreciated, and I am extremely gratefull for what you've done to help me so far.

Phil

---

From: "Garth N. Wells" <[email-masked]>
Date: Thu, 20 Aug 2009 06:36:24 +0100

> double nodes[Phi.vector().size()];

The above is not legal C/C++ code. It should be

   double* nodes = new double[Phi.vector().size()];
   // Do something
   delete [] nodes;

Garth

> Phi.vector().get(nodes);
> 
> I moved them around my program to see if where the problem starts. They 
> work fine in the main body of the function, but as soon as I pass 
> Function Phi, the error shows up. This is the relevant code snippet:
> 
> LGrad is the linear form used to get the gradients. declared above. As 
> far as I can tell it works just fine in my main program, it worked 
> beutifully in my test program before I copied it to my main.
> 
> I copied the 2 statments that form the vector().get() right before 
> PhiDistribution() and there was no error. I copied the 2 statements as 
> the first line of PhiDistribution, and I had the same error. These are 
> the first couple lines of PhiDistribution as they are when I get the error:
> 
> It blows up at that line. Somehow the act of passing the Function from 
> the main, to the function introduces an error. The weirdest part is that 
> I successfully pass Functions this way, and call the same 2 problematic 
> lines earlier in my program and theres no problem. I am using namespace 
> dolfin.
> 
> What I would like to know is what kind of things could cause this kind 
> of error. I check for memory allocation, as in new//delete pairs, as per 
> a suggestion, and that didn't seem to be it, so I completely eliminated 
> new/delete by creating nodes right before I used it. I checked the 
> Phi.vector().size() and it outputted exactly the size i expected, the 
> total number of nodes in the domain, so it appears to still be defined 
> over the right FunctionSpace. I ploted Phi0 in my main and it plotted 
> successfully and looked good. I tried plot(Phi) in my PhiDistribution 
> and I got the same segmentation error as I got before. So this confirmed 
> to me that there was an error with my passing of the Function. However, 
> I have been passing Functions this way ever since I knew enough about 
> dolfin to pass a Function and this is the first time I have a problem. 
> This is the code with the plotting:
> 
> 
> I usually would not ask for help with debugging my own code and I still 
> feel bad asking you guys for help, but I really don't know how to 
> proceed from here. Again, any help would be appreciated, and I am 
> extremely gratefull for what you've done to help me so far.
> 
> Phil
> 
> 
> ------------------------------------------------------------------------
> 
> _______________________________________________
> FEniCS-users mailing list
> [email-masked]
> http://fenics.org/mailman/listinfo/fenics-users