Skip to content

File toolwithownmemory.h

File List > code_source > templat > include > TempLat > lattice > measuringtools > toolwithownmemory.h

Go to the documentation of this file

#ifndef TEMPLAT_LATTICE_MEASUREMENTS_TOOLWITHOWNMEMORY_H
#define TEMPLAT_LATTICE_MEASUREMENTS_TOOLWITHOWNMEMORY_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): Wessel Valkenburg, Year: 2019

#include "TempLat/lattice/field/field.h"

#include "TempLat/lattice/algebra/helpers/getgetreturntype.h"
#include "TempLat/lattice/algebra/helpers/getfloattype.h"
#include "TempLat/lattice/algebra/spacestateinterface.h"

namespace TempLat
{

  template <typename T, size_t NDim> class ToolWithOwnMemory
  {
  public:
    typedef typename GetGetReturnType<T>::type SV;
    typedef typename GetFloatType<SV>::type S;
    typedef Field<S, NDim> fieldType;

    // Put public methods here. These should change very little over time.
    ToolWithOwnMemory(T instance) : mInstance(instance), usePersistentField(false), allocatedPersistentField(false) {}

    void setPersistentMemory() { usePersistentField = true; }

  private:
    T mInstance;
    std::shared_ptr<fieldType> persistentField;
    bool usePersistentField;
    bool allocatedPersistentField;

  protected:
    T &getInstance() { return mInstance; }

    fieldType getFieldForMeasurement(std::string postfix)
    {
      auto toolBox = mInstance.getToolBox();
      std::string fieldName = /*"[" + mInstance.toString() +*/ "  [for " + postfix + "]";
      if (usePersistentField && !allocatedPersistentField) {
        allocatedPersistentField = true;
        persistentField = std::make_shared<fieldType>(fieldName, toolBox);
      }
      fieldType result = usePersistentField ? *persistentField : fieldType(fieldName, toolBox);
      result.setDisableFFTBlocking();
      return result;
    }

    friend struct ToolWithOwnMemoryTester;
  };
} // namespace TempLat

#endif