Skip to content

File spatialcoordinate.h

File List > algebra > coordinates > spatialcoordinate.h

Go to the documentation of this file

#ifndef TEMPLAT_LATTICE_ALGEBRA_COORDINATES_SPATIALCOORDINATE_H
#define TEMPLAT_LATTICE_ALGEBRA_COORDINATES_SPATIALCOORDINATE_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, Franz R. Sattler, Year: 2025

#include "TempLat/lattice/memory/memorytoolbox.h"
#include "TempLat/lattice/algebra/helpers/getvectorcomponent.h"

namespace TempLat
{
  MakeException(SpatialCoordinateConfigWrongSpaceConfirmation);

  template <size_t NDim> class SpatialCoordinate
  {
  public:
    // Put public methods here. These should change very little over time.

    SpatialCoordinate(device::memory::host_ptr<MemoryToolBox<NDim>> toolBox)
        : mToolBox(toolBox), mLayout(toolBox->mLayouts.getConfigSpaceLayout())
    {
    }

    static constexpr device::Idx getVectorSize() { return NDim; }

    template <typename... IDX>
      requires IsVariadicNDIndex<NDim, IDX...>
    DEVICE_INLINE_FUNCTION auto eval(const IDX &...idx) const
    {
      device::IdxArray<NDim> result;
      mLayout.putSpatialLocationFromMemoryIndexInto(result, idx...);
      return result;
    }

    void doWeNeedGhosts(device::Idx i) const {}
    template <int N> device::Idx confirmGhostsUpToDate(Tag<N> i) const { return 1; }

    virtual void confirmSpace(device::Idx i, const LayoutStruct<NDim> &newLayout, const SpaceStateType &spaceType) const
    {
      switch (spaceType) {
      case SpaceStateType::Fourier:
        throw SpatialCoordinateConfigWrongSpaceConfirmation(
            "SpatialCoordinate explicitly only can be used in configuration space. Abort.");
        break;
      case SpaceStateType::Configuration:
      default:
        break;
      }
    }

    template <int N> auto vectorGet(Tag<N> t) const
    {
      static_assert(N > 0 && N <= NDim, "VectorGet: N must be between 1 and NDim for SpatialCoordinate");
      return getVectorComponent(*this, Tag<N - 1>());
    }

    template <int N> auto operator()(Tag<N> t) const
    {
      static_assert(N > 0 && N <= NDim, "Operator(): N must be between 1 and NDim for SpatialCoordinate");
      return vectorGet(t);
    }

    inline auto getToolBox() const { return mToolBox; }

    static std::string toString(device::Idx j) { return "x_" + std::to_string(j); }
    static std::string toString() { return "x"; }

  private:
    /* Put all member variables and private methods here. These may change arbitrarily. */
    device::memory::host_ptr<MemoryToolBox<NDim>> mToolBox;
    LayoutStruct<NDim> mLayout;
  };
} // namespace TempLat

#endif