Skip to content

Commit

Permalink
Add complex types (idaholab#401)
Browse files Browse the repository at this point in the history
  • Loading branch information
dschwen committed Apr 29, 2020
1 parent 63d0c56 commit 8aabb42
Show file tree
Hide file tree
Showing 3 changed files with 137 additions and 0 deletions.
57 changes: 57 additions & 0 deletions include/utils/ComplexTypes.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
/**********************************************************************/
/* DO NOT MODIFY THIS HEADER */
/* MAGPIE - Mesoscale Atomistic Glue Program for Integrated Execution */
/* */
/* Copyright 2017 Battelle Energy Alliance, LLC */
/* ALL RIGHTS RESERVED */
/**********************************************************************/

#pragma once

#include <complex>

#include "RankTwoTensor.h"
#include "RankThreeTensor.h"
#include "RankFourTensor.h"

#include "libmesh/vector_value.h"

typedef std::complex<Real> Complex;
typedef VectorValue<std::complex<Real>> ComplexVectorValue;
typedef RankTwoTensorTempl<std::complex<Real>> ComplexRankTwoTensor;
typedef RankThreeTensorTempl<std::complex<Real>> ComplexRankThreeTensor;
typedef RankFourTensorTempl<std::complex<Real>> ComplexRankFourTensor;

// helper template to select the corresponding complex tensor type
template <typename T>
struct ComplexType;

template <>
struct ComplexType<Real>
{
using type = Complex;
};

template <>
struct ComplexType<RealVectorValue>
{
using type = ComplexVectorValue;
};

template <>
struct ComplexType<RankTwoTensor>
{
using type = ComplexRankTwoTensor;
};

template <>
struct ComplexType<RankThreeTensor>
{
using type = ComplexRankThreeTensor;
};

template <>
struct ComplexType<RankFourTensor>
{
using type = ComplexRankFourTensor;
};
44 changes: 44 additions & 0 deletions src/utils/ComplexTypes.C
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
/**********************************************************************/
/* DO NOT MODIFY THIS HEADER */
/* MAGPIE - Mesoscale Atomistic Glue Program for Integrated Execution */
/* */
/* Copyright 2017 Battelle Energy Alliance, LLC */
/* ALL RIGHTS RESERVED */
/**********************************************************************/

#include "ComplexTypes.h"
#include "MooseError.h"
#include "MooseUtils.h"

namespace MooseUtils
{

template <>
bool
absoluteFuzzyEqual(const std::complex<Real> & var1,
const std::complex<Real> & var2,
const std::complex<Real> & tol)
{
return (std::abs(var1 - var2) <= std::abs(tol));
}

template <>
bool
absoluteFuzzyEqual(const std::complex<Real> & var1,
const Real & var2,
const std::complex<Real> & tol)
{
return (std::abs(var1 - var2) <= std::abs(tol));
}

} // namespace MooseUtils

#include "RankTwoTensorImplementation.h"
#include "RankThreeTensorImplementation.h"
#include "RankFourTensorImplementation.h"

template class VectorValue<std::complex<Real>>;

template class RankTwoTensorTempl<std::complex<Real>>;
template class RankThreeTensorTempl<std::complex<Real>>;
template class RankFourTensorTempl<std::complex<Real>>;
36 changes: 36 additions & 0 deletions unit/src/ComplexTypesTest.C
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/****************************************************************/
/* DO NOT MODIFY THIS HEADER */
/* MOOSE - Multiphysics Object Oriented Simulation Environment */
/* */
/* (c) 2010 Battelle Energy Alliance, LLC */
/* ALL RIGHTS RESERVED */
/* */
/* Prepared by Battelle Energy Alliance, LLC */
/* Under Contract No. DE-AC07-05ID14517 */
/* With the U. S. Department of Energy */
/* */
/* See COPYRIGHT for full restrictions */
/****************************************************************/
#include <cmath>
#include <gtest/gtest.h>

#include "ComplexTypes.h"

TEST(ComplexTypesTest, rankTwo)
{
Complex c_1;
ComplexType<Real>::type c_2;
auto c = c_1 + c_2;

ComplexVectorValue v_1;
ComplexType<RealVectorValue>::type v_2;
auto v = v_1 + v_2;

ComplexRankTwoTensor r2_1;
ComplexType<RankTwoTensor>::type r2_2;
auto r2 = r2_1 + r2_2;

ComplexRankThreeTensor r3_1;
ComplexType<RankThreeTensor>::type r3_2;
auto r3 = r3_1 + r3_2;
}

0 comments on commit 8aabb42

Please sign in to comment.