2016-12-15 18:26:39 +00:00
|
|
|
/*************************************************************************************
|
|
|
|
|
2016-02-25 12:07:21 +00:00
|
|
|
Grid physics library, www.github.com/paboyle/Grid
|
|
|
|
|
2018-09-01 21:30:30 +01:00
|
|
|
Source file: Hadrons/Environment.cc
|
2016-02-25 12:07:21 +00:00
|
|
|
|
2019-02-05 18:55:24 +00:00
|
|
|
Copyright (C) 2015-2019
|
2016-02-25 12:07:21 +00:00
|
|
|
|
|
|
|
Author: Antonin Portelli <antonin.portelli@me.com>
|
|
|
|
|
|
|
|
This program is free software; you can redistribute it and/or modify
|
|
|
|
it under the terms of the GNU General Public License as published by
|
|
|
|
the Free Software Foundation; either version 2 of the License, or
|
|
|
|
(at your option) any later version.
|
|
|
|
|
|
|
|
This program is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
GNU General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License along
|
|
|
|
with this program; if not, write to the Free Software Foundation, Inc.,
|
|
|
|
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
|
|
|
|
|
2016-12-15 18:26:39 +00:00
|
|
|
See the full license in the file "LICENSE" in the top level distribution directory
|
|
|
|
*************************************************************************************/
|
2016-12-15 18:21:52 +00:00
|
|
|
/* END LEGAL */
|
2015-11-05 14:28:14 +00:00
|
|
|
|
2018-08-28 15:00:40 +01:00
|
|
|
#include <Hadrons/Environment.hpp>
|
|
|
|
#include <Hadrons/Module.hpp>
|
|
|
|
#include <Hadrons/ModuleFactory.hpp>
|
2015-11-05 14:28:14 +00:00
|
|
|
|
|
|
|
using namespace Grid;
|
2016-01-14 04:23:51 +00:00
|
|
|
using namespace QCD;
|
2015-11-05 14:28:14 +00:00
|
|
|
using namespace Hadrons;
|
|
|
|
|
2017-12-12 14:04:28 +00:00
|
|
|
#define ERROR_NO_ADDRESS(address)\
|
2018-07-26 16:47:45 +01:00
|
|
|
HADRONS_ERROR_REF(ObjectDefinition, "no object with address " + std::to_string(address), address);
|
2017-12-12 14:04:28 +00:00
|
|
|
|
2015-11-05 14:28:14 +00:00
|
|
|
/******************************************************************************
|
|
|
|
* Environment implementation *
|
|
|
|
******************************************************************************/
|
|
|
|
// constructor /////////////////////////////////////////////////////////////////
|
|
|
|
Environment::Environment(void)
|
2016-01-14 04:23:51 +00:00
|
|
|
{
|
2018-09-10 15:31:39 +01:00
|
|
|
dim_ = GridDefaultLatt();
|
|
|
|
nd_ = dim_.size();
|
2018-09-21 18:15:18 +01:00
|
|
|
createGrid<vComplex>(1);
|
2018-05-28 10:39:17 +01:00
|
|
|
vol_ = 1.;
|
|
|
|
for (auto d: dim_)
|
2016-05-10 19:07:41 +01:00
|
|
|
{
|
2018-05-28 10:39:17 +01:00
|
|
|
vol_ *= d;
|
2016-05-10 19:07:41 +01:00
|
|
|
}
|
2018-09-21 18:15:18 +01:00
|
|
|
rng4d_.reset(new GridParallelRNG(getGrid()));
|
2016-01-14 04:23:51 +00:00
|
|
|
}
|
2015-12-23 14:21:35 +00:00
|
|
|
|
2016-02-25 11:56:16 +00:00
|
|
|
// grids ///////////////////////////////////////////////////////////////////////
|
2017-01-11 18:37:49 +00:00
|
|
|
unsigned int Environment::getNd(void) const
|
|
|
|
{
|
|
|
|
return nd_;
|
|
|
|
}
|
|
|
|
|
2017-06-06 17:45:30 +01:00
|
|
|
std::vector<int> Environment::getDim(void) const
|
|
|
|
{
|
|
|
|
return dim_;
|
|
|
|
}
|
|
|
|
|
|
|
|
int Environment::getDim(const unsigned int mu) const
|
|
|
|
{
|
|
|
|
return dim_[mu];
|
|
|
|
}
|
|
|
|
|
2018-05-28 10:39:17 +01:00
|
|
|
double Environment::getVolume(void) const
|
2017-12-05 14:31:59 +00:00
|
|
|
{
|
2018-05-28 10:39:17 +01:00
|
|
|
return vol_;
|
2017-12-05 14:31:59 +00:00
|
|
|
}
|
|
|
|
|
2016-05-10 19:07:41 +01:00
|
|
|
// random number generator /////////////////////////////////////////////////////
|
|
|
|
GridParallelRNG * Environment::get4dRng(void) const
|
|
|
|
{
|
|
|
|
return rng4d_.get();
|
|
|
|
}
|
|
|
|
|
2016-06-06 17:45:37 +01:00
|
|
|
// general memory management ///////////////////////////////////////////////////
|
|
|
|
void Environment::addObject(const std::string name, const int moduleAddress)
|
2016-04-30 08:17:04 +01:00
|
|
|
{
|
2016-12-21 23:25:36 +00:00
|
|
|
if (!hasObject(name))
|
|
|
|
{
|
|
|
|
ObjInfo info;
|
|
|
|
|
|
|
|
info.name = name;
|
|
|
|
info.module = moduleAddress;
|
2017-11-22 23:27:19 +00:00
|
|
|
info.data = nullptr;
|
2016-12-21 23:25:36 +00:00
|
|
|
object_.push_back(std::move(info));
|
|
|
|
objectAddress_[name] = static_cast<unsigned int>(object_.size() - 1);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2018-07-26 16:47:45 +01:00
|
|
|
HADRONS_ERROR_REF(ObjectDefinition, "object '" + name + "' already exists",
|
|
|
|
getObjectAddress(name));
|
2016-12-21 23:25:36 +00:00
|
|
|
}
|
2016-05-07 21:19:38 +01:00
|
|
|
}
|
|
|
|
|
2017-12-05 14:31:59 +00:00
|
|
|
void Environment::setObjectModule(const unsigned int objAddress,
|
|
|
|
const int modAddress)
|
|
|
|
{
|
|
|
|
object_[objAddress].module = modAddress;
|
|
|
|
}
|
|
|
|
|
|
|
|
unsigned int Environment::getMaxAddress(void) const
|
|
|
|
{
|
|
|
|
return object_.size();
|
|
|
|
}
|
|
|
|
|
2016-05-10 19:07:41 +01:00
|
|
|
unsigned int Environment::getObjectAddress(const std::string name) const
|
2016-01-14 04:23:51 +00:00
|
|
|
{
|
2016-05-10 19:07:41 +01:00
|
|
|
if (hasObject(name))
|
2016-01-14 04:23:51 +00:00
|
|
|
{
|
2016-05-10 19:07:41 +01:00
|
|
|
return objectAddress_.at(name);
|
2016-01-14 04:23:51 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2018-04-25 16:49:14 +01:00
|
|
|
HADRONS_ERROR(Definition, "no object with name '" + name + "'");
|
2016-01-14 04:23:51 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-05-10 19:07:41 +01:00
|
|
|
std::string Environment::getObjectName(const unsigned int address) const
|
2016-01-14 04:23:51 +00:00
|
|
|
{
|
2016-05-10 19:07:41 +01:00
|
|
|
if (hasObject(address))
|
|
|
|
{
|
2016-06-06 17:45:37 +01:00
|
|
|
return object_[address].name;
|
2016-05-10 19:07:41 +01:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2017-12-12 14:04:28 +00:00
|
|
|
ERROR_NO_ADDRESS(address);
|
2016-05-10 19:07:41 +01:00
|
|
|
}
|
2016-05-07 21:19:38 +01:00
|
|
|
}
|
2016-05-04 20:17:27 +01:00
|
|
|
|
2016-06-06 17:45:37 +01:00
|
|
|
std::string Environment::getObjectType(const unsigned int address) const
|
|
|
|
{
|
2017-11-22 23:27:19 +00:00
|
|
|
if (hasObject(address))
|
2016-06-06 17:45:37 +01:00
|
|
|
{
|
2017-06-06 17:45:30 +01:00
|
|
|
if (object_[address].type)
|
|
|
|
{
|
|
|
|
return typeName(object_[address].type);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
return "<no type>";
|
|
|
|
}
|
2016-06-06 17:45:37 +01:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2017-12-12 14:04:28 +00:00
|
|
|
ERROR_NO_ADDRESS(address);
|
2016-06-06 17:45:37 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string Environment::getObjectType(const std::string name) const
|
|
|
|
{
|
|
|
|
return getObjectType(getObjectAddress(name));
|
|
|
|
}
|
|
|
|
|
2016-12-04 23:53:48 +00:00
|
|
|
Environment::Size Environment::getObjectSize(const unsigned int address) const
|
2016-01-14 04:23:51 +00:00
|
|
|
{
|
2017-11-22 23:27:19 +00:00
|
|
|
if (hasObject(address))
|
2016-01-14 04:23:51 +00:00
|
|
|
{
|
2016-05-10 19:07:41 +01:00
|
|
|
return object_[address].size;
|
|
|
|
}
|
2016-01-14 04:23:51 +00:00
|
|
|
else
|
|
|
|
{
|
2017-12-12 14:04:28 +00:00
|
|
|
ERROR_NO_ADDRESS(address);
|
2016-01-14 04:23:51 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-12-04 23:53:48 +00:00
|
|
|
Environment::Size Environment::getObjectSize(const std::string name) const
|
2016-04-30 08:17:04 +01:00
|
|
|
{
|
2016-05-10 19:07:41 +01:00
|
|
|
return getObjectSize(getObjectAddress(name));
|
|
|
|
}
|
|
|
|
|
2017-12-05 14:31:59 +00:00
|
|
|
Environment::Storage Environment::getObjectStorage(const unsigned int address) const
|
|
|
|
{
|
|
|
|
if (hasObject(address))
|
|
|
|
{
|
|
|
|
return object_[address].storage;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2017-12-12 14:04:28 +00:00
|
|
|
ERROR_NO_ADDRESS(address);
|
2017-12-05 14:31:59 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Environment::Storage Environment::getObjectStorage(const std::string name) const
|
|
|
|
{
|
|
|
|
return getObjectStorage(getObjectAddress(name));
|
|
|
|
}
|
|
|
|
|
|
|
|
int Environment::getObjectModule(const unsigned int address) const
|
2017-06-06 17:45:30 +01:00
|
|
|
{
|
|
|
|
if (hasObject(address))
|
|
|
|
{
|
|
|
|
return object_[address].module;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2017-12-12 14:04:28 +00:00
|
|
|
ERROR_NO_ADDRESS(address);
|
2017-06-06 17:45:30 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-12-05 14:31:59 +00:00
|
|
|
int Environment::getObjectModule(const std::string name) const
|
2017-06-06 17:45:30 +01:00
|
|
|
{
|
|
|
|
return getObjectModule(getObjectAddress(name));
|
|
|
|
}
|
|
|
|
|
2016-05-10 19:07:41 +01:00
|
|
|
unsigned int Environment::getObjectLs(const unsigned int address) const
|
|
|
|
{
|
2018-02-26 19:22:43 +00:00
|
|
|
if (hasCreatedObject(address))
|
2016-04-30 08:17:04 +01:00
|
|
|
{
|
2016-05-10 19:07:41 +01:00
|
|
|
return object_[address].Ls;
|
|
|
|
}
|
2016-04-30 08:17:04 +01:00
|
|
|
else
|
|
|
|
{
|
2017-12-12 14:04:28 +00:00
|
|
|
ERROR_NO_ADDRESS(address);
|
2016-04-30 08:17:04 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-05-10 19:07:41 +01:00
|
|
|
unsigned int Environment::getObjectLs(const std::string name) const
|
2016-05-07 21:19:38 +01:00
|
|
|
{
|
2016-05-10 19:07:41 +01:00
|
|
|
return getObjectLs(getObjectAddress(name));
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Environment::hasObject(const unsigned int address) const
|
|
|
|
{
|
|
|
|
return (address < object_.size());
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Environment::hasObject(const std::string name) const
|
|
|
|
{
|
|
|
|
auto it = objectAddress_.find(name);
|
2016-05-07 21:19:38 +01:00
|
|
|
|
2016-05-10 19:07:41 +01:00
|
|
|
return ((it != objectAddress_.end()) and hasObject(it->second));
|
|
|
|
}
|
|
|
|
|
2016-12-21 23:25:36 +00:00
|
|
|
bool Environment::hasCreatedObject(const unsigned int address) const
|
|
|
|
{
|
|
|
|
if (hasObject(address))
|
|
|
|
{
|
|
|
|
return (object_[address].data != nullptr);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Environment::hasCreatedObject(const std::string name) const
|
|
|
|
{
|
|
|
|
if (hasObject(name))
|
|
|
|
{
|
|
|
|
return hasCreatedObject(getObjectAddress(name));
|
2016-05-07 21:19:38 +01:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2016-05-10 19:07:41 +01:00
|
|
|
return false;
|
2016-05-07 21:19:38 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-05-10 19:07:41 +01:00
|
|
|
bool Environment::isObject5d(const unsigned int address) const
|
|
|
|
{
|
|
|
|
return (getObjectLs(address) > 1);
|
|
|
|
}
|
|
|
|
|
2016-05-07 21:19:38 +01:00
|
|
|
bool Environment::isObject5d(const std::string name) const
|
|
|
|
{
|
|
|
|
return (getObjectLs(name) > 1);
|
|
|
|
}
|
|
|
|
|
2016-12-04 23:53:48 +00:00
|
|
|
Environment::Size Environment::getTotalSize(void) const
|
2016-05-10 19:07:41 +01:00
|
|
|
{
|
2016-12-04 23:53:48 +00:00
|
|
|
Environment::Size size = 0;
|
2016-05-10 19:07:41 +01:00
|
|
|
|
|
|
|
for (auto &o: object_)
|
|
|
|
{
|
2017-11-22 23:27:19 +00:00
|
|
|
size += o.size;
|
2016-05-10 19:07:41 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return size;
|
|
|
|
}
|
|
|
|
|
2017-12-12 13:08:01 +00:00
|
|
|
void Environment::freeObject(const unsigned int address)
|
2016-01-14 04:23:51 +00:00
|
|
|
{
|
2017-12-12 13:08:01 +00:00
|
|
|
if (hasCreatedObject(address))
|
2016-06-06 17:45:37 +01:00
|
|
|
{
|
2017-12-12 13:08:01 +00:00
|
|
|
LOG(Message) << "Destroying object '" << object_[address].name
|
|
|
|
<< "'" << std::endl;
|
2016-05-05 03:11:03 +01:00
|
|
|
}
|
2017-12-12 13:08:01 +00:00
|
|
|
object_[address].size = 0;
|
|
|
|
object_[address].type = nullptr;
|
|
|
|
object_[address].data.reset(nullptr);
|
2016-02-25 11:56:16 +00:00
|
|
|
}
|
|
|
|
|
2017-12-12 13:08:01 +00:00
|
|
|
void Environment::freeObject(const std::string name)
|
2016-05-10 19:07:41 +01:00
|
|
|
{
|
2017-12-12 13:08:01 +00:00
|
|
|
freeObject(getObjectAddress(name));
|
2016-05-10 19:07:41 +01:00
|
|
|
}
|
|
|
|
|
2016-05-05 03:11:03 +01:00
|
|
|
void Environment::freeAll(void)
|
2016-02-25 11:56:16 +00:00
|
|
|
{
|
2016-06-06 17:45:37 +01:00
|
|
|
for (unsigned int i = 0; i < object_.size(); ++i)
|
|
|
|
{
|
|
|
|
freeObject(i);
|
|
|
|
}
|
2016-02-25 11:56:16 +00:00
|
|
|
}
|
2016-05-10 19:07:41 +01:00
|
|
|
|
2017-12-12 19:32:58 +00:00
|
|
|
void Environment::protectObjects(const bool protect)
|
|
|
|
{
|
|
|
|
protect_ = protect;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Environment::objectsProtected(void) const
|
|
|
|
{
|
|
|
|
return protect_;
|
|
|
|
}
|
|
|
|
|
2017-12-05 14:31:59 +00:00
|
|
|
// print environment content ///////////////////////////////////////////////////
|
|
|
|
void Environment::printContent(void) const
|
2016-05-10 19:07:41 +01:00
|
|
|
{
|
2017-12-01 19:38:23 +00:00
|
|
|
LOG(Debug) << "Objects: " << std::endl;
|
2016-05-10 19:07:41 +01:00
|
|
|
for (unsigned int i = 0; i < object_.size(); ++i)
|
|
|
|
{
|
2017-12-01 19:38:23 +00:00
|
|
|
LOG(Debug) << std::setw(4) << i << ": "
|
2017-12-12 19:32:58 +00:00
|
|
|
<< getObjectName(i) << " ("
|
|
|
|
<< sizeString(getObjectSize(i)) << ")" << std::endl;
|
2016-05-10 19:07:41 +01:00
|
|
|
}
|
|
|
|
}
|