Page Menu
Home
HEPForge
Search
Configure Global Search
Log In
Files
F11221216
No One
Temporary
Actions
View File
Edit File
Delete File
View Transforms
Subscribe
Mute Notifications
Award Token
Flag For Later
Size
11 KB
Subscribers
None
View Options
diff --git a/Config/PhysicalQty.h b/Config/PhysicalQty.h
--- a/Config/PhysicalQty.h
+++ b/Config/PhysicalQty.h
@@ -1,332 +1,337 @@
// -*- C++ -*-
//
// PhysicalQty.h is a part of ThePEG - Toolkit for HEP Event Generation
// Copyright (C) 2006-2011 David Grellscheid, Leif Lonnblad
//
// ThePEG is licenced under version 2 of the GPL, see COPYING for details.
// Please respect the MCnet academic guidelines, see GUIDELINES for details.
//
#ifndef Physical_Qty_H
#define Physical_Qty_H
#include "TemplateTools.h"
#include <sstream>
/** @file
*
* The PhysicalQty class allows compile-time checking of dimensional
* correctness. Mathematical operations that are inconsistent are
* flagged as type errors.
*
* Do not use the classes directly in ThePEG, use the wrappers defined
* in Units.h or Phys_Qty.h instead.
*/
namespace ThePEG {
/// Helper class to construct zero unitful quantities.
struct ZeroUnit {
/** Automatic conversion to double. */
constexpr operator double() const { return 0.0; }
};
/// ZERO can be used as zero for any unitful quantity.
constexpr ZeroUnit ZERO = ZeroUnit();
/// Helper classes to extend or shorten fractions
//@{
/**
* Template to help with fractional powers of dimensions
*/
template <int M, int II>
struct QtyHelper
{
/// The numerator, indicating failure.
static constexpr int I = -999999;
};
/**
* Template to help with fractional powers of dimensions
*/
template <int II>
struct QtyHelper<0,II>
{
/// The new numerator.
static constexpr int I = II;
};
/**
* Template to help with fractional powers of dimensions
*/
template <int II, int DI, int DI2>
struct QtyInt
{
/// The new numerator.
static constexpr int I = QtyHelper<(DI2*II)%DI,(DI2*II)/DI>::I;
};
//@}
/**
* This template class allows the compiler to check calculations with
* physical quantities for dimensional correctness. A quantity can be
* composed of arbitrary fractional powers of length L, energy E and
* charge Q. Commonly used quantities should be typedef'ed (see Units.h).
*
* Some member functions can break dimensional consistency if the user
* is not careful; these are marked explicitly.
*
* Do not use this class directly in ThePEG, use the pre-defined quantities
* from Units.h or the wrapper in Phys_Qty.h instead.
*/
template<int L, int E, int Q, int DL = 1, int DE = 1, int DQ = 1>
class Qty
{
private:
/// Constructor from raw values. Breaks consistency.
constexpr Qty(double val) : rawValue_(val) {}
public:
/// The name of the class for persistent IO
static std::string className() {
std::ostringstream os;
os << "Qty<"
<< L << ','
<< E << ','
<< Q << ','
<< DL << ','
<< DE << ','
<< DQ << '>';
return os.str();
}
/// The squared type.
typedef Qty<2*L,2*E,2*Q,DL,DE,DQ> Squared;
/// Basic unit of this quantity.
static constexpr Qty<L,E,Q,DL,DE,DQ> baseunit()
{
return Qty<L,E,Q,DL,DE,DQ>(1.0);
}
/// Default constructor to 0.
constexpr Qty() : rawValue_(0.0) {}
/// Default constructor to 0.
constexpr Qty(ZeroUnit) : rawValue_(0.0) {}
/// Constructor from a compatible quantity
template <int DL2, int DE2, int DQ2>
constexpr
Qty(const Qty<QtyInt<L,DL,DL2>::I,
QtyInt<E,DE,DE2>::I,
QtyInt<Q,DQ,DQ2>::I,
DL2,DE2,DQ2> & q,
double factor = 1.0)
: rawValue_(q.rawValue() * factor) {}
/// Access to the raw value. Breaks consistency.
constexpr double rawValue() const { return rawValue_; }
/// Assignment multiplication by dimensionless number.
Qty<L,E,Q,DL,DE,DQ> & operator*=(double x) { rawValue_ *= x; return *this; }
/// Assignment division by dimensionless number.
Qty<L,E,Q,DL,DE,DQ> & operator/=(double x) { rawValue_ /= x; return *this; }
/// Assignment addition with compatible quantity.
template <int DL2, int DE2, int DQ2>
Qty<L,E,Q,DL,DE,DQ> &
operator+=(const Qty<QtyInt<L,DL,DL2>::I,
QtyInt<E,DE,DE2>::I,
QtyInt<Q,DQ,DQ2>::I,
DL2,DE2,DQ2> x)
{
rawValue_ += x.rawValue();
return *this;
}
/// Assignment subtraction with compatible quantity.
template <int DL2, int DE2, int DQ2>
Qty<L,E,Q,DL,DE,DQ> &
operator-=(const Qty<QtyInt<L,DL,DL2>::I,
QtyInt<E,DE,DE2>::I,
QtyInt<Q,DQ,DQ2>::I,
DL2,DE2,DQ2> x)
{
rawValue_ -= x.rawValue();
return *this;
}
private:
/// The raw value in units of Qty::baseunit().
double rawValue_;
};
/// Specialization of Qty for <0,0,0> with conversions to double.
template<int DL, int DE, int DQ>
class Qty<0,0,0,DL,DE,DQ>
{
public:
/// The squared type.
typedef double Squared;
/// Basic unit of this quantity.
static constexpr double baseunit() {
return 1.0;
}
/// Default constructor to 0.
constexpr Qty(ZeroUnit) : rawValue_(0.0) {}
/// Default constructor from a double.
constexpr Qty(double x = 0.0, double factor=1.0)
: rawValue_(x * factor) {}
/// Constructor from a compatible quantity
template <int DL2, int DE2, int DQ2>
constexpr
Qty(const Qty<0,0,0,DL2,DE2,DQ2> & q, double factor=1.0)
: rawValue_(q.rawValue() * factor) {}
/// Access to the raw value.
constexpr double rawValue() const { return rawValue_; }
/// Cast to double.
constexpr operator double() const { return rawValue_; }
/// Assignment multiplication by dimensionless number.
Qty<0,0,0,DL,DE,DQ> & operator*=(double x) { rawValue_ *= x; return *this; }
/// Assignment division by dimensionless number.
Qty<0,0,0,DL,DE,DQ> & operator/=(double x) { rawValue_ /= x; return *this; }
/// Assignment addition with compatible quantity.
template <int DL2, int DE2, int DQ2>
Qty<0,0,0,DL,DE,DQ> & operator+=(const Qty<0,0,0,DL2,DE2,DQ2> x) {
rawValue_ += x.rawValue();
return *this;
}
/// Assignment subtraction with compatible quantity.
template <int DL2, int DE2, int DQ2>
Qty<0,0,0,DL,DE,DQ> & operator-=(const Qty<0,0,0,DL2,DE2,DQ2> x) {
rawValue_ -= x.rawValue();
return *this;
}
/// Assignment addition with double.
Qty<0,0,0,DL,DE,DQ> & operator+=(double x) {
rawValue_ += x;
return *this;
}
/// Assignment subtraction with double.
Qty<0,0,0,DL,DE,DQ> & operator-=(double x) {
rawValue_ -= x;
return *this;
}
private:
/// The raw value.
double rawValue_;
};
/// @name Result types for binary operations.
//@{
/**
* BinaryOpTraits should be specialized with typdefs called MulT and
* DivT which gives the type resulting when multiplying and dividing
* the template argument types respectively.
*/
template <typename T, typename U>
struct BinaryOpTraits;
/** @cond TRAITSPECIALIZATIONS */
template<int L1, int L2, int E1, int E2, int Q1, int Q2,
int DL1, int DL2, int DE1, int DE2, int DQ1, int DQ2>
struct BinaryOpTraits<Qty<L1,E1,Q1,DL1,DE1,DQ1>,
Qty<L2,E2,Q2,DL2,DE2,DQ2> > {
/** The type resulting from multiplication of the template type with
itself. */
typedef Qty<L1*DL2+L2*DL1,E1*DE2+E2*DE1,Q1*DQ2+Q2*DQ1,
DL1*DL2,DE1*DE2,DQ1*DQ2> MulT;
/** The type resulting from division of one template type with
another. */
typedef Qty<L1*DL2-L2*DL1,E1*DE2-E2*DE1,Q1*DQ2-Q2*DQ1,
DL1*DL2,DE1*DE2,DQ1*DQ2> DivT;
};
template<int L1, int E1, int Q1, int DL1, int DE1, int DQ1>
struct BinaryOpTraits<Qty<L1,E1,Q1,DL1,DE1,DQ1>,
Qty<L1,E1,Q1,DL1,DE1,DQ1> > {
/** The type resulting from multiplication of the template type with
itself. */
typedef Qty<2*L1,2*E1,2*Q1,
DL1,DE1,DQ1> MulT;
/** The type resulting from division of one template type with
another. */
typedef double DivT;
};
/**
* Multiplication template
*/
template<int L1, int E1, int Q1, int DL1, int DE1, int DQ1>
struct BinaryOpTraits<double,
Qty<L1,E1,Q1,DL1,DE1,DQ1> > {
/** The type resulting from multiplication of the template type */
typedef Qty<L1,E1,Q1,
DL1,DE1,DQ1> MulT;
/** The type resulting from division of the template type */
typedef Qty<-L1,-E1,-Q1,
DL1,DE1,DQ1> DivT;
};
/**
* Multiplication template
*/
template<int L1, int E1, int Q1, int DL1, int DE1, int DQ1>
struct BinaryOpTraits<Qty<L1,E1,Q1,DL1,DE1,DQ1>,
double> {
/** The type resulting from multiplication of the template type */
typedef Qty<L1,E1,Q1,
DL1,DE1,DQ1> MulT;
/** The type resulting from division of the template type */
typedef Qty<L1,E1,Q1,
DL1,DE1,DQ1> DivT;
};
//@}
/// @name Type traits for alternative code generation.
//@{
/** Type traits for alternative code generation*/
template <int L, int E, int Q, int DL, int DE, int DQ>
struct TypeTraits<Qty<L,E,Q,DL,DE,DQ> >
{
/** Enum for dimensions*/
enum { hasDimension = true };
/// Type switch set to dimensioned type.
typedef DimensionT DimType;
+ /// Base unit
+ static constexpr Qty<L,E,Q,DL,DE,DQ> baseunit
+ = Qty<L,E,Q,DL,DE,DQ>::baseunit();
};
/** Type traits for alternative code generation*/
template <int DL, int DE, int DQ>
struct TypeTraits<Qty<0,0,0,DL,DE,DQ> >
{
/** Enum for dimensions*/
enum { hasDimension = false };
/// Type switch set to standard type.
typedef StandardT DimType;
+ /// Base unit
+ static constexpr double baseunit = 1.0;
};
//@}
/** @endcond */
}
#endif
diff --git a/Config/TemplateTools.h b/Config/TemplateTools.h
--- a/Config/TemplateTools.h
+++ b/Config/TemplateTools.h
@@ -1,86 +1,90 @@
// -*- C++ -*-
//
// TemplateTools.h is a part of ThePEG - Toolkit for HEP Event Generation
// Copyright (C) 2006-2011 David Grellscheid, Leif Lonnblad
//
// ThePEG is licenced under version 2 of the GPL, see COPYING for details.
// Please respect the MCnet academic guidelines, see GUIDELINES for details.
//
#ifndef Template_Tools_H
#define Template_Tools_H
+#include <type_traits>
+
/**
* @file TemplateTools.h
* Useful template machinery. Based on Alexandrescu, "Modern C++ Design".
*/
namespace ThePEG {
/// Conversion between integers and types.
template <int v>
struct Int2Type
{
enum { value = v };
};
/// Dummy type for ambiguous function signatures.
struct DummyType {};
/// Result type calculations for binary operators.
template <typename T, typename U>
struct BinaryOpTraits;
/** @cond TRAITSPECIALIZATIONS */
template <>
struct BinaryOpTraits<double,double> {
/** The type resulting from multiplication of the template types. */
typedef double MulT;
/** The type resulting from division of the first template type by
the second. */
typedef double DivT;
};
template <>
struct BinaryOpTraits<long double, long double> {
/** The type resulting from multiplication of the template types. */
typedef long double MulT;
/** The type resulting from division of the first template type by
the second. */
typedef long double DivT;
};
template <>
struct BinaryOpTraits<int,int> {
/** The type resulting from multiplication of the template types. */
typedef int MulT;
/** The type resulting from division of the first template type by
the second. */
typedef int DivT;
};
/** @endcond */
/// Selection mechanism for type-dependent implementations.
enum ImplSelector { Dimensioned, Standard };
/// Typedef for dimensioned types.
typedef Int2Type<Dimensioned> DimensionT;
/// Typedef for non-dimensioned types.
typedef Int2Type<Standard> StandardT;
/// Type traits for built-in types
template <typename T>
struct TypeTraits
{
/// Boolean flag. Is true for physical quantities.
enum { hasDimension = false };
/// Implementation selector
typedef StandardT DimType;
+ /// Base unit for arithmetic types
+ static constexpr double baseunit = 1.0;
};
}
#endif
File Metadata
Details
Attached
Mime Type
text/x-diff
Expires
Wed, May 14, 10:04 AM (1 d, 13 h)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
5089711
Default Alt Text
(11 KB)
Attached To
rTHEPEGHG thepeghg
Event Timeline
Log In to Comment