Skip to content

File su2wrapper.h

File List > algebra > su2algebra > su2wrapper.h

Go to the documentation of this file

#ifndef COSMOINTERFACE_SU2ALGEBRA_SU2WRAPPER_H
#define COSMOINTERFACE_SU2ALGEBRA_SU2WRAPPER_H

/* This file is part of TempLat, available at https://cosmolattice.github.io/templat .
   Copyright 2021-2026 The TempLat authors, see AUTHORS.md.
   Released under the MIT license, see LICENSE.md. */

// File info: Main contributor(s): Adrien Florio, Year: 2019

#include "TempLat/lattice/algebra/helpers/isvariadicindex.h"
#include "TempLat/util/rangeiteration/tagliteral.h"
#include "TempLat/lattice/algebra/su2algebra/su2operator.h"
#include "TempLat/lattice/algebra/su2algebra/helpers/su2getgetreturntype.h"
#include "TempLat/lattice/algebra/helpers/getstring.h"

#include "TempLat/lattice/algebra/helpers/doeval.h"
#include "TempLat/lattice/algebra/helpers/preget.h"
#include "TempLat/lattice/algebra/helpers/postget.h"
#include "TempLat/lattice/algebra/helpers/confirmspace.h"
#include "TempLat/lattice/algebra/helpers/ghostshunter.h"
#include "TempLat/lattice/algebra/helpers/confirmghosts.h"

#include "TempLat/parallel/device.h"

namespace TempLat
{
  template <class A, class B, class C, class D> class SU2Wrapper : public SU2Operator
  {
  public:
    using SV = typename GetGetReturnType<A>::type;

    SU2Wrapper(const A &pA, const B &pB, const C &pC, const D &pD) : data(pA, pB, pC, pD) {}
    SU2Wrapper() = default;

    template <int N> auto SU2Get(Tag<N> t) const
    {
      static_assert(N >= 0 && N <= 3, "SU2Get: N must be between 0 and 3 for SU2Wrapper");
      return device::get<N>(data);
    }
    template <int N> auto operator()(Tag<N> t) const { return SU2Get(t); }

    std::string toString() const
    {
      return "SU2(" + GetString::get(device::get<0>(data)) + "," + GetString::get(device::get<1>(data)) + "," +
             GetString::get(device::get<2>(data)) + "," + GetString::get(device::get<3>(data)) + ")";
    }

    template <typename... IDX>
      requires requires(std::decay_t<A> a, std::decay_t<B> b, std::decay_t<C> c, std::decay_t<D> d, IDX... idx) {
        requires IsVariadicIndex<IDX...>;
        DoEval::eval(a, idx...);
        DoEval::eval(b, idx...);
        DoEval::eval(c, idx...);
        DoEval::eval(d, idx...);
      }
    DEVICE_INLINE_FUNCTION auto eval(const IDX &...idx) const
    {
      device::array<SV, 4> result;
      result[0] = DoEval::eval(device::get<0>(data), idx...);
      result[1] = DoEval::eval(device::get<1>(data), idx...);
      result[2] = DoEval::eval(device::get<2>(data), idx...);
      result[3] = DoEval::eval(device::get<3>(data), idx...);
      return result;
    }

    void preGet()
    {
      PreGet::apply(device::get<0>(data));
      PreGet::apply(device::get<1>(data));
      PreGet::apply(device::get<2>(data));
      PreGet::apply(device::get<3>(data));
    }

    void postGet()
    {
      PostGet::apply(device::get<0>(data));
      PostGet::apply(device::get<1>(data));
      PostGet::apply(device::get<2>(data));
      PostGet::apply(device::get<3>(data));
    }

    // Space/ghost confirmation forwarded to the 4 stored component expressions (see SU2Field operator=).
    void doWeNeedGhosts() const
    {
      GhostsHunter::apply(device::get<0>(data));
      GhostsHunter::apply(device::get<1>(data));
      GhostsHunter::apply(device::get<2>(data));
      GhostsHunter::apply(device::get<3>(data));
    }
    device::Idx confirmGhostsUpToDate() const
    {
      return ConfirmGhosts::apply(device::get<0>(data)) + ConfirmGhosts::apply(device::get<1>(data)) +
             ConfirmGhosts::apply(device::get<2>(data)) + ConfirmGhosts::apply(device::get<3>(data));
    }
    template <size_t NDim> void confirmSpace(const LayoutStruct<NDim> &newLayout, const SpaceStateType &spaceType) const
    {
      ConfirmSpace::apply(device::get<0>(data), newLayout, spaceType);
      ConfirmSpace::apply(device::get<1>(data), newLayout, spaceType);
      ConfirmSpace::apply(device::get<2>(data), newLayout, spaceType);
      ConfirmSpace::apply(device::get<3>(data), newLayout, spaceType);
    }

  private:
    /* Put all member variables and private methods here. These may change arbitrarily. */

    device::tuple<std::decay_t<A>, std::decay_t<B>, std::decay_t<C>, std::decay_t<D>> data;
  };

  template <class A, class B, class C, class D> auto SU2Wrap(const A &pA, const B &pB, const C &pC, const D &pD)
  {
    return SU2Wrapper<A, B, C, D>(pA, pB, pC, pD);
  }

  template <typename F> auto SU2Wrap(const F &f) { return SU2Wrap(f(0_c), f(1_c), f(2_c), f(3_c)); }

#define MakeSU2(a, expr) SU2Wrap([&](auto a) { return expr; })
} // namespace TempLat

#endif