File vector3d.hxx

Class for 3D vectors. Built on the Field3D class.

Copyright 2010 B.D.Dudson, S.Farley, M.V.Umansky, X.Q.Xu

B. Dudson, October 2007

Contact: Ben Dudson,

This file is part of BOUT++.

BOUT++ is free software: you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.

BOUT++ is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.

You should have received a copy of the GNU Lesser General Public License along with BOUT++. If not, see




const Vector3D operator*(BoutReal lhs, const Vector3D &rhs)
const Vector3D operator*(const Field2D &lhs, const Vector3D &rhs)
const Vector3D operator*(const Field3D &lhs, const Vector3D &rhs)
const Vector3D cross(const Vector3D &lhs, const Vector3D &rhs)

Cross-product of two vectors.

const Vector3D cross(const Vector3D &lhs, const Vector2D &rhs)

Cross-product of two vectors.

const Field3D abs(const Vector3D &v, const std::string &region = "RGN_ALL")

Absolute magnitude (modulus) of a vector |v|

sqrt( v.x^2 + v.y^2 + v.z^2 )

const Field3D abs(const Vector3D &v, REGION region)
Vector3D &ddt(Vector3D &f)

Time derivative of 3D vector field.

class Vector3D : public FieldData
#include <vector3d.hxx>

Represents a 3D vector, with x,y,z components stored as separate Field3D objects


Vector3D f;

a.x; // Error! a.x not allocated

Public Functions

Vector3D(Mesh *fieldmesh = nullptr)

Constructor. Just sets covariant = true and deriv = NULL

Does not initialise any of the fields

Vector3D(const Vector3D &f)

Copy constructor. After this the components (x,y,z) will refer to the same data as f.(x,y,z)


Destructor. If the time derivative has been used, then some book-keeping is needed to ensure that fields are only destroyed once.

void toCovariant()

In-place conversion to covariant form.

If already covariant (covariant = true) then does nothing If contravariant, multiplies by metric tensor g_{ij}

void toContravariant()

In-place conversion to contravariant form

If already contravariant (covariant = false) then does nothing If covariant, multiplies by metric tensor g^{ij}

Vector3D *timeDeriv()

Return a pointer to the time-derivative field

The first time this is called, a new Vector3D object is created. Subsequent calls return a pointer to this same object

For convenience, a standalone function “ddt” exists, so that

ddt(v) is equivalent to *(v.timeDeriv())

This does some book-keeping to ensure that the time derivative of the components is the same as the components of the time derivative

ddt(v).x == ddt(v.x)

Vector3D &operator=(const Vector3D &rhs)
Vector3D &operator=(const Vector2D &rhs)
Vector3D &operator=(BoutReal val)
Vector3D &operator+=(const Vector3D &rhs)
Vector3D &operator+=(const Vector2D &rhs)
const Vector3D operator-() const
Vector3D &operator-=(const Vector3D &rhs)
Vector3D &operator-=(const Vector2D &rhs)
Vector3D &operator*=(BoutReal rhs)
Vector3D &operator*=(const Field2D &rhs)
Vector3D &operator*=(const Field3D &rhs)
Vector3D &operator/=(BoutReal rhs)
Vector3D &operator/=(const Field2D &rhs)
Vector3D &operator/=(const Field3D &rhs)
const Vector3D operator+(const Vector3D &rhs) const
const Vector3D operator+(const Vector2D &rhs) const
const Vector3D operator-(const Vector3D &rhs) const
const Vector3D operator-(const Vector2D &rhs) const
const Vector3D operator*(BoutReal rhs) const
const Vector3D operator*(const Field2D &rhs) const
const Vector3D operator*(const Field3D &rhs) const
const Vector3D operator/(BoutReal rhs) const
const Vector3D operator/(const Field2D &rhs) const
const Vector3D operator/(const Field3D &rhs) const
const Field3D operator*(const Vector3D &rhs) const
const Field3D operator*(const Vector2D &rhs) const
void setLocation(CELL_LOC loc)

Set variable cell location

CELL_LOC getLocation() const
void accept(FieldVisitor &v)

Visitor pattern support.

bool isReal() const

Returns true if field consists of BoutReal values.

bool is3D() const

True if variable is 3D.

int byteSize() const

Number of bytes for a single point.

int BoutRealSize() const

Number of BoutReals (not implemented if not BoutReal)

void applyBoundary(bool init = false)
void applyBoundary(const std::string &condition)
void applyBoundary(const char *condition)
void applyTDerivBoundary()

Public Members

Field3D x

The components of the vector. These can be either co- or contra-variant, depending on the boolean flag “covariant”

Field3D y
Field3D z
bool covariant = {true}

Flag to specify whether the components (x,y,z) are co- or contra-variant.

true if the components are covariant (default) false if the components are contravariant

Conversion between forms should be done by calling the toContravariant and toCovariant methods.

Only modify this variable directly if you know what you are doing!

Private Members

Vector3D *deriv = {nullptr}

Time-derivative, can be NULL.


Location of the variable in the cell.