Skip to content
Permalink
Browse files

Merge pull request #13371 from rwcarlsen/ad-wrapper-types

AD wrapper types
  • Loading branch information...
moosebuild committed May 8, 2019
2 parents 45f1b96 + 7c73b4b commit 4d9b6d64bc0590efedf6114aba41431d5345c09a
@@ -380,12 +380,12 @@ dataStore(std::ostream & stream, DenseVector<T> & v, void * context)
}
}

template <>
void dataStore(std::ostream & stream, DenseMatrix<Real> & v, void * context);

template <typename T>
void dataStore(std::ostream & stream, TensorValue<T> & v, void * context);

template <typename T>
void dataStore(std::ostream & stream, DenseMatrix<T> & v, void * context);

template <typename T>
void dataStore(std::ostream & stream, VectorValue<T> & v, void * context);

@@ -627,12 +627,12 @@ dataLoad(std::istream & stream, DenseVector<T> & v, void * context)
}
}

template <>
void dataLoad(std::istream & stream, DenseMatrix<Real> & v, void * context);

template <typename T>
void dataLoad(std::istream & stream, TensorValue<T> & v, void * context);

template <typename T>
void dataLoad(std::istream & stream, DenseMatrix<T> & v, void * context);

template <typename T>
void dataLoad(std::istream & stream, VectorValue<T> & v, void * context);

@@ -15,8 +15,10 @@
#include "RankThreeTensor.h"
#include "RankFourTensor.h"

#include "libmesh/dense_vector.h"
#include "libmesh/vector_value.h"
#include "libmesh/tensor_value.h"
#include "libmesh/dense_matrix.h"

#include "metaphysicl/numberarray.h"
#include "metaphysicl/dualnumber.h"
@@ -321,3 +323,142 @@ class MooseADWrapper<RankFourTensorTempl<Real>>
void *);
};

template <>
class MooseADWrapper<DenseVector<Real>>
{
public:
MooseADWrapper(bool use_ad = false);
MooseADWrapper(MooseADWrapper<DenseVector<Real>> &&) = default;

typedef DenseVector<DualReal> DNType;

const DenseVector<Real> & value() const { return _val; }

DenseVector<Real> & value() { return _val; }

const DenseVector<DualReal> & dn(bool = true) const;

DenseVector<DualReal> & dn(bool = true);

void copyDualNumberToValue();

void markAD(bool use_ad);

MooseADWrapper<DenseVector<Real>> & operator=(const MooseADWrapper<DenseVector<Real>> &);
MooseADWrapper<DenseVector<Real>> & operator=(MooseADWrapper<DenseVector<Real>> &&) = default;

private:
bool _use_ad;
DenseVector<Real> _val;
mutable std::unique_ptr<DenseVector<DualReal>> _dual_number;
friend void
dataStore<DenseVector<Real>>(std::ostream &, MooseADWrapper<DenseVector<Real>> &, void *);
friend void
dataLoad<DenseVector<Real>>(std::istream &, MooseADWrapper<DenseVector<Real>> &, void *);
};

template <>
class MooseADWrapper<DenseMatrix<Real>>
{
public:
MooseADWrapper(bool use_ad = false);
MooseADWrapper(MooseADWrapper<DenseMatrix<Real>> &&) = default;

typedef DenseMatrix<DualReal> DNType;

const DenseMatrix<Real> & value() const { return _val; }

DenseMatrix<Real> & value() { return _val; }

const DenseMatrix<DualReal> & dn(bool = true) const;

DenseMatrix<DualReal> & dn(bool = true);

void copyDualNumberToValue();

void markAD(bool use_ad);

MooseADWrapper<DenseMatrix<Real>> & operator=(const MooseADWrapper<DenseMatrix<Real>> &);
MooseADWrapper<DenseMatrix<Real>> & operator=(MooseADWrapper<DenseMatrix<Real>> &&) = default;

private:
bool _use_ad;
DenseMatrix<Real> _val;
mutable std::unique_ptr<DenseMatrix<DualReal>> _dual_number;
friend void
dataStore<DenseMatrix<Real>>(std::ostream &, MooseADWrapper<DenseMatrix<Real>> &, void *);
friend void
dataLoad<DenseMatrix<Real>>(std::istream &, MooseADWrapper<DenseMatrix<Real>> &, void *);
};

template <>
class MooseADWrapper<std::vector<DenseMatrix<Real>>>
{
public:
MooseADWrapper(bool use_ad = false);
MooseADWrapper(MooseADWrapper<std::vector<DenseMatrix<Real>>> &&) = default;

typedef std::vector<DenseMatrix<DualReal>> DNType;

const std::vector<DenseMatrix<Real>> & value() const { return _val; }

std::vector<DenseMatrix<Real>> & value() { return _val; }

const std::vector<DenseMatrix<DualReal>> & dn(bool = true) const;

std::vector<DenseMatrix<DualReal>> & dn(bool = true);

void copyDualNumberToValue();

void markAD(bool use_ad);

MooseADWrapper<std::vector<DenseMatrix<Real>>> &
operator=(const MooseADWrapper<std::vector<DenseMatrix<Real>>> &);
MooseADWrapper<std::vector<DenseMatrix<Real>>> &
operator=(MooseADWrapper<std::vector<DenseMatrix<Real>>> &&) = default;

private:
bool _use_ad;
std::vector<DenseMatrix<Real>> _val;
mutable std::unique_ptr<std::vector<DenseMatrix<DualReal>>> _dual_number;
friend void dataStore<std::vector<DenseMatrix<Real>>>(
std::ostream &, MooseADWrapper<std::vector<DenseMatrix<Real>>> &, void *);
friend void dataLoad<std::vector<DenseMatrix<Real>>>(
std::istream &, MooseADWrapper<std::vector<DenseMatrix<Real>>> &, void *);
};

template <>
class MooseADWrapper<std::vector<DenseVector<Real>>>
{
public:
MooseADWrapper(bool use_ad = false);
MooseADWrapper(MooseADWrapper<std::vector<DenseVector<Real>>> &&) = default;

typedef std::vector<DenseVector<DualReal>> DNType;

const std::vector<DenseVector<Real>> & value() const { return _val; }

std::vector<DenseVector<Real>> & value() { return _val; }

const std::vector<DenseVector<DualReal>> & dn(bool = true) const;

std::vector<DenseVector<DualReal>> & dn(bool = true);

void copyDualNumberToValue();

void markAD(bool use_ad);

MooseADWrapper<std::vector<DenseVector<Real>>> &
operator=(const MooseADWrapper<std::vector<DenseVector<Real>>> &);
MooseADWrapper<std::vector<DenseVector<Real>>> &
operator=(MooseADWrapper<std::vector<DenseVector<Real>>> &&) = default;

private:
bool _use_ad;
std::vector<DenseVector<Real>> _val;
mutable std::unique_ptr<std::vector<DenseVector<DualReal>>> _dual_number;
friend void dataStore<std::vector<DenseVector<Real>>>(
std::ostream &, MooseADWrapper<std::vector<DenseVector<Real>>> &, void *);
friend void dataLoad<std::vector<DenseVector<Real>>>(
std::istream &, MooseADWrapper<std::vector<DenseVector<Real>>> &, void *);
};
@@ -168,36 +168,39 @@ dataStore(std::ostream & stream, std::stringstream *& s, void * context)
dataStore(stream, *s, context);
}

template <>
template <typename T>
void
dataStore(std::ostream & stream, DenseMatrix<Real> & v, void * context)
dataStore(std::ostream & stream, TensorValue<T> & v, void * context)
{
unsigned int m = v.m();
unsigned int n = v.n();
stream.write((char *)&m, sizeof(m));
stream.write((char *)&n, sizeof(n));
for (unsigned int i = 0; i < v.m(); i++)
for (unsigned int j = 0; j < v.n(); j++)
for (unsigned int i = 0; i < LIBMESH_DIM; i++)
for (unsigned int j = 0; i < LIBMESH_DIM; i++)
{
Real r = v(i, j);
T r = v(i, j);
dataStore(stream, r, context);
}
}

template void dataStore(std::ostream & stream, TensorValue<Real> & v, void * context);
template void dataStore(std::ostream & stream, TensorValue<DualReal> & v, void * context);

template <typename T>
void
dataStore(std::ostream & stream, TensorValue<T> & v, void * context)
dataStore(std::ostream & stream, DenseMatrix<T> & v, void * context)
{
for (unsigned int i = 0; i < LIBMESH_DIM; i++)
for (unsigned int j = 0; i < LIBMESH_DIM; i++)
unsigned int m = v.m();
unsigned int n = v.n();
stream.write((char *)&m, sizeof(m));
stream.write((char *)&n, sizeof(n));
for (unsigned int i = 0; i < m; i++)
for (unsigned int j = 0; j < n; j++)
{
T r = v(i, j);
dataStore(stream, r, context);
}
}

template void dataStore(std::ostream & stream, TensorValue<Real> & v, void * context);
template void dataStore(std::ostream & stream, TensorValue<DualReal> & v, void * context);
template void dataStore(std::ostream & stream, DenseMatrix<Real> & v, void * context);
template void dataStore(std::ostream & stream, DenseMatrix<DualReal> & v, void * context);

template <typename T>
void
@@ -375,23 +378,6 @@ dataLoad(std::istream & stream, std::stringstream *& s, void * context)
dataLoad(stream, *s, context);
}

template <>
void
dataLoad(std::istream & stream, DenseMatrix<Real> & v, void * /*context*/)
{
unsigned int nr = 0, nc = 0;
stream.read((char *)&nr, sizeof(nr));
stream.read((char *)&nc, sizeof(nc));
v.resize(nr, nc);
for (unsigned int i = 0; i < v.m(); i++)
for (unsigned int j = 0; j < v.n(); j++)
{
Real r = 0;
stream.read((char *)&r, sizeof(r));
v(i, j) = r;
}
}

template <typename T>
void
dataLoad(std::istream & stream, TensorValue<T> & v, void * context)
@@ -410,6 +396,26 @@ dataLoad(std::istream & stream, TensorValue<T> & v, void * context)
template void dataLoad(std::istream & stream, TensorValue<Real> & v, void * context);
template void dataLoad(std::istream & stream, TensorValue<DualReal> & v, void * context);

template <typename T>
void
dataLoad(std::istream & stream, DenseMatrix<T> & v, void * context)
{
unsigned int m = 0, n = 0;
stream.read((char *)&m, sizeof(m));
stream.read((char *)&n, sizeof(n));
v.resize(m, n);
for (unsigned int i = 0; i < m; i++)
for (unsigned int j = 0; j < n; j++)
{
T r = 0;
dataLoad(stream, r, context);
v(i, j) = r;
}
}

template void dataLoad(std::istream & stream, DenseMatrix<Real> & v, void * context);
template void dataLoad(std::istream & stream, DenseMatrix<DualReal> & v, void * context);

template <typename T>
void
dataLoad(std::istream & stream, VectorValue<T> & v, void * context)
Oops, something went wrong.

0 comments on commit 4d9b6d6

Please sign in to comment.
You can’t perform that action at this time.