DirichletBC virtual methods

Asked by Phil Marinier on 2010-06-16

I need to make many instances of DirichletBC, but I exactly how many is a user function. As such, I am using a loop to create my bcs.

 for(int j = 0; j < numberCathodes; j++){

  cathode = &cathodes[j];

  //assemble
  CathodeBC(iInit, cathode, phiCBC0);

  // create anodic and cathodic boundary condition Functions from BCs
  numRows = PhiCBC0.vector().size();
  rows = new dolfin::uint[numRows];
  for(dolfin::uint j = 0; j < numRows; j ++)
   rows[j] = j;
  PhiCBC0.vector().set(phiCBC0, numRows, rows);
  delete [] rows;

  //Define boundary condition
  DirichletBC cbc0(V, PhiCBC0, *cathode);
  bcsTEMP.push_back(cbc0);
 }

where bcsTEMP is a std::vector<dolfin::DirichletBC>

This does not work, since DirichletBC is abstract and so I can't make it a type for a vector. Trying to use pointers will obviously not work, since once the object goes out of scope, it is cleaned up, and I end up following a dangling pointer to nowhere.

I am going to make my own class called dirichletBC, the only difference being that I over load the apply() method, since it is the one that is virtual. My question is which apply() should I use?

Since my VariationalProblem is Laplaces equation with a single bilinear form and a single linear form ( a(V, V) and L(V) ), I assume that I need to apply the boundary condition to a matrix and a vector, hence using:

00171 void DirichletBC::apply(GenericMatrix& A, GenericVector& b) const
00172 {
00173 apply(&A, &b, 0);
00174 }

Is that right? Is there something else I need to do to make this work? is there an easier way to make this work? Do you need more information about my problem?

Thank you for your help.

Question information

Language:
English Edit question
Status:
Solved
For:
DOLFIN Edit question
Assignee:
No assignee Edit question
Solved by:
Phil Marinier
Solved:
2010-06-16
Last query:
2010-06-16
Last reply:
2010-06-16
Andre Massing (massing) said : #1

Onsdag 16. juni 2010 17.55.35 skrev Phil Marinier :
> New question #114825 on DOLFIN:
> https://answers.launchpad.net/dolfin/+question/114825
>
> I need to make many instances of DirichletBC, but I exactly how many is a
> user function. As such, I am using a loop to create my bcs.
>
>
> for(int j = 0; j < numberCathodes; j++){
>
> cathode = &cathodes[j];
>
> //assemble
> CathodeBC(iInit, cathode, phiCBC0);
>
> // create anodic and cathodic boundary condition Functions from BCs
> numRows = PhiCBC0.vector().size();
> rows = new dolfin::uint[numRows];
> for(dolfin::uint j = 0; j < numRows; j ++)
> rows[j] = j;
> PhiCBC0.vector().set(phiCBC0, numRows, rows);
> delete [] rows;
>
> //Define boundary condition
> DirichletBC cbc0(V, PhiCBC0, *cathode);
> bcsTEMP.push_back(cbc0);
> }
>
> where bcsTEMP is a std::vector<dolfin::DirichletBC>
>
> This does not work, since DirichletBC is abstract and so I can't make it a
> type for a vector.

As far as I can see is DirichletBC is not abstract. Did you mean the
BoundaryCondition class?

> Trying to use pointers will obviously not work, since
> once the object goes out of scope, it is cleaned up, and I end up
> following a dangling pointer to nowhere.

Hmm, maybe I miss a point here, but why does it not work for pointers?
If you add your boundary conditions by

 std::vector<const BoundaryCondition*> bcs;
  bcs.push_back( new YourFancyBoundaryConditionClass(.....) );

you can just go through the vector bcs and delete your objects before bcs is
going out of scope.

>
> I am going to make my own class called dirichletBC, the only difference
> being that I over load the apply() method, since it is the one that is
> virtual. My question is which apply() should I use?
>
> Since my VariationalProblem is Laplaces equation with a single bilinear
> form and a single linear form ( a(V, V) and L(V) ), I assume that I need
> to apply the boundary condition to a matrix and a vector, hence using:
>
> 00171 void DirichletBC::apply(GenericMatrix& A, GenericVector& b) const
> 00172 {
> 00173 apply(&A, &b, 0);
> 00174 }
>
> Is that right? Is there something else I need to do to make this work? is
> there an easier way to make this work? Do you need more information about
> my problem?
>
> Thank you for your help.

Phil Marinier (lonewolf-13p) said : #2

sorry you're right, I am an idiot. I was too busy looking for ways to get around DirichletBC being an abstract function that I didn't double check whether or not I had made my bcs vector with DirichletBC or BoundaryCondition.

It compiles when I do std::vector<DirichletBC>

Thanks...

Andre Massing (massing) said : #3

Onsdag 16. juni 2010 18.52.13 skrev Phil Marinier :
> Question #114825 on DOLFIN changed:
> https://answers.launchpad.net/dolfin/+question/114825
>
> Status: Answered => Solved
>
> Phil Marinier confirmed that the question is solved:
> sorry you're right, I am an idiot. I was too busy looking for ways to
> get around DirichletBC being an abstract function that I didn't double
> check whether or not I had made my bcs vector with DirichletBC or
> BoundaryCondition.

I know that by heart :) and can not count how many times I have struggled with
code because I was too much into it, busy with other stuff or whatever. And I
am very glad that there exists also a community to ask for help.
So happy hacking :)

--
Andre

>
> It compiles when I do std::vector<DirichletBC>
>
> Thanks...