This is a read only copy of the old FEniCS QA forum. Please visit the new QA forum to ask questions

How to create 1D vector Constant?

0 votes

The constructors for Constant don't seem to provide a way to create a 1D vector valued constant:

/// Create scalar constant
explicit Constant(double value);


/// Create vector constant (dim = 2)
Constant(double value0, double value1);

/// Create vector constant (dim = 3)
Constant(double value0, double value1, double value2);

The following two look promising, but haven't helped me in applying a 1D vector dirichlet BC.

/// Create vector-valued constant
explicit Constant(std::vector<double> values);

/// Create tensor-valued constant for flattened array of values
Constant(std::vector<std::size_t> value_shape,
         std::vector<double> values);

When I set the boundary condition equal to a Constant, I keep getting a runtime error:

*** -------------------------------------------------------------------------
*** Error: Unable to create Dirichlet boundary condition.
*** Reason: Expecting a vector-valued boundary value but given function is scalar.
*** Where: This error was encountered inside DirichletBC.cpp.
*** Process: 0
*** -------------------------------------------------------------------------

How should I do this? Thanks!

asked Oct 23, 2013 by Charles FEniCS User (4,220 points)
edited Oct 23, 2013 by Charles

I had this issue before I updated to building from bitbucket, but handled it with a custom 'fix' that was deemed incorrect (it worked, but im trying to avoid the same incorrect hack here).

https://bugs.launchpad.net/dolfin/+bug/1129366

http://bazaar.launchpad.net/~rodbourn/dolfin/1dupdates/revision/7455

This constructor should do the trick

/// Create vector-valued constant
explicit Constant(std::vector<double> values);

with values of length 1. Does it work?

I did try that overload, but I couldn't get it to work. It behaved the same as the other constructor.

Ok, please, post a minimal running example which fails.

I ended up having to do a clean install through dorsal and ended up taking a second look to see if I could get a clean build. I found my mistake, it was in understanding how to set _value_shape :)

2 Answers

0 votes
 
Best answer

Jan's solution is the correct one and works well at least as of 1.6+

/// Create vector-valued constant
explicit Constant(std::vector<double> values);

Instead of using a single double value to construct the Constant do (if using shared pointers):

/// example
std::shared_ptr<Constant> constantValue;
std::vector<double> vecValue = {dblValue};
constantValue.reset(new Constant(vecValue));
answered Sep 6, 2015 by Charles FEniCS User (4,220 points)
0 votes

The error was not so surprisingly my own. Some digging into how the rank and dimensions go into the value shape, and I can not set the _value_shape as needed like so:

    _value_shape.resize(rank);
    for (std::size_t i = 0; i < rank; i++)
        _value_shape[i] = dimensions;
answered Dec 12, 2013 by Charles FEniCS User (4,220 points)
...