26 #include "Drivers/DriverException.h"
29 #include "Validator/ObjectRequirement.h"
70 size_t mapTo1d(
const std::vector<int> &indices)
const override
73 for (
size_t i = 0; i < GridBase<T>::GetDimension(); ++i) {
74 int index = indices.at(i);
76 if ( index < 0 || index >= numpoints )
78 throw std::out_of_range(
"Grid index out of range.");
84 for (
size_t i = 0; i < GridBase<T>::GetDimension(); ++i) {
85 idx += indices.at(i) * fac;
103 Grid(std::vector<int> numPoints,
104 std::vector<double> lower,
105 std::vector<double> upper,
106 std::vector<bool> isPeriodic)
107 :
GridBase<T>(numPoints, lower, upper, isPeriodic)
109 size_t data_size = 1;
110 for (
size_t d = 0; d < GridBase<T>::GetDimension(); ++d) {
112 data_size *= storage_size;
146 Json::CharReaderBuilder rbuilder;
147 Json::CharReader* reader = rbuilder.newCharReader();
149 reader->parse(JsonSchema::grid.c_str(),
150 JsonSchema::grid.c_str() + JsonSchema::grid.
size(),
152 validator.
Parse(schema, path);
161 std::vector<double> lower;
162 for (
auto &lv : json[
"lower"]) {
163 lower.push_back(lv.asDouble());
166 size_t dimension = lower.size();
169 std::vector<double> upper;
170 for (
auto &uv : json[
"upper"]) {
171 upper.push_back(uv.asDouble());
174 if (upper.size() != dimension) {
176 "number of lower values!"});
180 std::vector<int> number_points;
181 for (
auto &np : json[
"number_points"]) {
182 number_points.push_back(np.asInt());
185 if (number_points.size() != dimension) {
186 throw BuildException({
"Arrays \"lower\" and \"number_points\" do "
187 "not have the same size!"});
191 std::vector<bool> isPeriodic;
192 for (
auto &periodic : json[
"periodic"]) {
193 isPeriodic.push_back(periodic.asBool());
196 if (isPeriodic.size() == 0) {
197 isPeriodic = std::vector<bool>(dimension,
false);
198 }
else if (isPeriodic.size() != dimension) {
200 "have the same size!"});
204 Grid<T>* grid =
new Grid(number_points, lower, upper, isPeriodic);
318 for (
size_t i = 0; i <
grid_->GetDimension() - 1; ++i) {
358 if (shift.size() !=
grid_->GetDimension()) {
359 throw std::invalid_argument(
"Vector to shift iterator does not "
360 "match grid dimension.");
363 for (
size_t i = 0; i <
grid_->GetDimension(); ++i) {
392 for (
size_t i = 0; i <
grid_->GetDimension() - 1; ++i) {
420 if (shift.size() !=
grid_->GetDimension()) {
421 throw std::invalid_argument(
"Vector to shift iterator does not "
422 "match histogram dimension.");
425 for (
size_t i = 0; i <
grid_->GetDimension(); ++i) {
461 return !( (*this) == rhs );
543 for (
size_t i = 0; i < indices.size(); ++i) {
574 for (
size_t i = 0; i < indices.size(); ++i) {
588 std::ofstream output(filename.c_str(), std::ofstream::out);
591 output <<
"#! type grid\n";
593 output <<
"#! count ";
597 output <<
"#! lower ";
601 output <<
"#! upper ";
605 output <<
"#! periodic ";
610 for(
auto it = this->
begin(); it != this->
end(); ++it)
612 auto coords = it.coordinates();
613 for(
auto& c : coords)
614 output << std::setprecision(8) << std::fixed << c <<
" ";
615 output.unsetf(std::ios_base::fixed);
616 output << std::setprecision(16) << *it <<
"\n";
631 std::ifstream file(filename);
633 throw BuildException ({
"Attempt to load grid " + filename +
" unsuccessful."});
635 std::string line, buff;
638 std::getline(file, line);
643 std::getline(file, line);
644 std::istringstream iss(line);
645 iss >> buff >> buff >> dim;
647 throw std::invalid_argument(filename +
649 " but got " + std::to_string(dim) +
" instead.");
654 std::getline(file, line);
655 std::istringstream iss(line);
659 for(
int i = 0; iss >> count; ++i)
661 if((count) != counts[i])
662 throw std::invalid_argument(filename +
663 ": Expected count " + std::to_string(counts[i]) +
664 " on dimension " + std::to_string(i) +
" but got " +
665 std::to_string(count) +
" instead.");
671 std::getline(file, line);
672 std::istringstream iss(line);
676 for(
int i = 0; iss >> lower; ++i)
683 if(std::abs(lower-spacing/2 - lowers[i]) > 1e-8)
684 throw std::invalid_argument(filename +
685 ": Expected lower " + std::to_string(lowers[i]+spacing/2) +
686 " on dimension " + std::to_string(i) +
" but got " +
687 std::to_string(lower) +
" instead.");
693 std::getline(file, line);
694 std::istringstream iss(line);
698 for(
int i = 0; iss >> upper; ++i)
705 if(std::abs((upper+spacing/2) - uppers[i]) > 1e-8)
706 throw std::invalid_argument(filename +
707 ": Expected upper " + std::to_string(uppers[i]-spacing/2) +
708 " on dimension " + std::to_string(i) +
" but got " +
709 std::to_string(upper) +
" instead.");
715 std::getline(file, line);
716 std::istringstream iss(line);
720 for(
int i = 0; iss >> periodic; ++i)
722 if(periodic != periodics[i])
723 throw std::invalid_argument(filename +
724 ": Expected periodic " + std::to_string(periodics[i]) +
725 " on dimension " + std::to_string(i) +
" but got " +
726 std::to_string(periodic) +
" instead.");
733 std::getline(file, line);
734 std::istringstream iss(line);
737 for(
size_t i = 0; i < GridBase<T>::dimension_; ++i)
Requirements on an object.
virtual void Parse(Value json, const std::string &path) override
Parse JSON value to generate Requirement(s).
virtual void Validate(const Value &json, const std::string &path) override
Validate JSON value.
std::vector< std::string > GetErrors()
Get list of error messages.
bool HasErrors()
Check if errors have occured.
Exception to be thrown when building the Driver fails.
const std::vector< double > GetLower() const
Return the lower edges of the Grid.
size_t size() const
Get the size of the internal storage vector.
const std::vector< double > GetUpper() const
Return the upper edges of the Grid.
const std::vector< int > GetNumPoints() const
Get the number of points for all dimensions.
bool operator==(const self_type &rhs) const
Equality operator.
GridIterator(const std::vector< int > &indices, const Grid< T > *grid)
Const constructor.
GridIterator()=default
Use default constructor.
self_type & operator--()
Pre-decrement operator.
std::bidirectional_iterator_tag iterator_category
HistIterator is a bidirectional iterator.
const self_type operator-(std::vector< int > shift)
Subtraction iterator.
GridIterator(const GridIterator &other)
Copy constructor.
R * pointer
Either T* or T const* for iterator and const_iterator, respectively.
Grid< T > * grid_
Pointer to grid to iterate over.
bool operator!=(const self_type &rhs) const
Non-equality operator.
std::vector< int > indices_
Indices of current grid point.
const self_type operator+(std::vector< int > shift)
Addition operator.
self_type & operator-=(std::vector< int > shift)
Subtraction assignment operator.
std::vector< int > & indices()
Access indices.
double coordinate(size_t d) const
Access specific coordinate dimension.
R value_type
Either T or const T for iterator and const_iterator, respectively.
reference operator*()
Dereference operator.
std::vector< double > coordinates() const
Access coordinates.
self_type & operator+=(std::vector< int > shift)
Addition assignment operator.
GridIterator(const std::vector< int > &indices, Grid< T > *grid)
Constructor.
R & reference
Either T& or T const& for iterator and const_iterator, respectively.
GridIterator self_type
Type name of the iterator.
self_type operator--(int)
Post-decrement operator.
self_type & operator++()
Pre-increment operator.
self_type operator++(int)
Post-increment operator.
int difference_type
Difference type is an int.
int & index(size_t d)
Access a specific index.
const_iterator begin() const
Return const iterator at first grid point.
static Grid< T > * BuildGrid(const Json::Value &json)
Set up the grid.
void LoadFromFile(const std::string &filename)
Builds a grid from file.
iterator begin()
Return iterator at first grid point.
GridIterator< const T > const_iterator
Custom constant iterator over a grid.
GridIterator< T > iterator
Custom iterator over a grid.
static Grid< T > * BuildGrid(const Json::Value &json, const std::string &path)
Set up the grid.
Grid(std::vector< int > numPoints, std::vector< double > lower, std::vector< double > upper, std::vector< bool > isPeriodic)
Constructor.
void WriteToFile(const std::string &filename)
Write grid out to file.
iterator end()
Return iterator after last valid grid point.
size_t mapTo1d(const std::vector< int > &indices) const override
Map d-dimensional indices to 1-d data vector.
const_iterator end() const
Return const iterator after last valid grid point.