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;
148 reader.parse(JsonSchema::grid, schema);
149 validator.
Parse(schema, path);
158 std::vector<double> lower;
159 for (
auto &lv : json[
"lower"]) {
160 lower.push_back(lv.asDouble());
163 size_t dimension = lower.size();
166 std::vector<double> upper;
167 for (
auto &uv : json[
"upper"]) {
168 upper.push_back(uv.asDouble());
171 if (upper.size() != dimension) {
173 "number of lower values!"});
177 std::vector<int> number_points;
178 for (
auto &np : json[
"number_points"]) {
179 number_points.push_back(np.asInt());
182 if (number_points.size() != dimension) {
183 throw BuildException({
"Arrays \"lower\" and \"number_points\" do "
184 "not have the same size!"});
188 std::vector<bool> isPeriodic;
189 for (
auto &periodic : json[
"periodic"]) {
190 isPeriodic.push_back(periodic.asBool());
193 if (isPeriodic.size() == 0) {
194 isPeriodic = std::vector<bool>(dimension,
false);
195 }
else if (isPeriodic.size() != dimension) {
197 "have the same size!"});
201 Grid<T>* grid =
new Grid(number_points, lower, upper, isPeriodic);
315 for (
size_t i = 0; i <
grid_->GetDimension() - 1; ++i) {
355 if (shift.size() !=
grid_->GetDimension()) {
356 throw std::invalid_argument(
"Vector to shift iterator does not "
357 "match grid dimension.");
360 for (
size_t i = 0; i <
grid_->GetDimension(); ++i) {
389 for (
size_t i = 0; i <
grid_->GetDimension() - 1; ++i) {
417 if (shift.size() !=
grid_->GetDimension()) {
418 throw std::invalid_argument(
"Vector to shift iterator does not "
419 "match histogram dimension.");
422 for (
size_t i = 0; i <
grid_->GetDimension(); ++i) {
458 return !( (*this) == rhs );
540 for (
size_t i = 0; i < indices.size(); ++i) {
571 for (
size_t i = 0; i < indices.size(); ++i) {
585 std::ofstream output(file.c_str(), std::ofstream::out);
588 output <<
"#! type grid\n";
590 output <<
"#! count ";
594 output <<
"#! lower ";
598 output <<
"#! upper ";
602 output <<
"#! periodic ";
607 for(
auto it = this->
begin(); it != this->
end(); ++it)
609 auto coords = it.coordinates();
610 for(
auto& c : coords)
611 output << std::setprecision(8) << std::fixed << c <<
" ";
612 output.unsetf(std::ios_base::fixed);
613 output << std::setprecision(16) << *it <<
"\n";
628 std::ifstream file(filename);
629 std::string line, buff;
632 std::getline(file, line);
637 std::getline(file, line);
638 std::istringstream iss(line);
639 iss >> buff >> buff >> dim;
641 throw std::invalid_argument(filename +
643 " but got " + std::to_string(dim) +
" instead.");
648 std::getline(file, line);
649 std::istringstream iss(line);
653 for(
int i = 0; iss >> count; ++i)
655 if(count != counts[i])
656 throw std::invalid_argument(filename +
657 ": Expected count " + std::to_string(counts[i]) +
658 " on dimension " + std::to_string(i) +
" but got " +
659 std::to_string(count) +
" instead.");
665 std::getline(file, line);
666 std::istringstream iss(line);
670 for(
int i = 0; iss >> lower; ++i)
672 if(std::abs(lower - lowers[i]) > 1e-8)
673 throw std::invalid_argument(filename +
674 ": Expected lower " + std::to_string(lowers[i]) +
675 " on dimension " + std::to_string(i) +
" but got " +
676 std::to_string(lower) +
" instead.");
682 std::getline(file, line);
683 std::istringstream iss(line);
687 for(
int i = 0; iss >> upper; ++i)
689 if(std::abs(upper - uppers[i]) > 1e-8)
690 throw std::invalid_argument(filename +
691 ": Expected upper " + std::to_string(uppers[i]) +
692 " on dimension " + std::to_string(i) +
" but got " +
693 std::to_string(upper) +
" instead.");
699 std::getline(file, line);
700 std::istringstream iss(line);
704 for(
int i = 0; iss >> periodic; ++i)
706 if(periodic != periodics[i])
707 throw std::invalid_argument(filename +
708 ": Expected periodic " + std::to_string(periodics[i]) +
709 " on dimension " + std::to_string(i) +
" but got " +
710 std::to_string(periodic) +
" instead.");
717 std::getline(file, line);
718 std::istringstream iss(line);
721 for(
int i = 0; i < GridBase<T>::dimension_; ++i)
bool HasErrors()
Check if errors have occured.
GridIterator self_type
Type name of the iterator.
bool operator==(const self_type &rhs) const
Equality operator.
R & reference
Either T& or T const& for iterator and const_iterator, respectively.
bool operator!=(const self_type &rhs) const
Non-equality operator.
R * pointer
Either T* or T const* for iterator and const_iterator, respectively.
GridIterator()=default
Use default constructor.
void WriteToFile(const std::string &file)
Write grid out to file.
GridIterator< T > iterator
Custom iterator over a grid.
self_type operator++(int)
Post-increment operator.
virtual void Parse(Value json, const std::string &path) override
Parse JSON value to generate Requirement(s).
self_type & operator++()
Pre-increment operator.
self_type & operator+=(std::vector< int > shift)
Addition assignment operator.
iterator begin()
Return iterator at first grid point.
Exception to be thrown when building the Driver fails.
self_type operator--(int)
Post-decrement operator.
std::vector< std::string > GetErrors()
Get list of error messages.
const_iterator begin() const
Return const iterator at first grid point.
GridIterator(const GridIterator &other)
Copy constructor.
Requirements on an object.
Grid< T > * grid_
Pointer to grid to iterate over.
GridIterator(const std::vector< int > &indices, Grid< T > *grid)
Constructor.
GridIterator(const std::vector< int > &indices, const Grid< T > *grid)
Const constructor.
const std::vector< int > & GetNumPoints() const
Get the number of points for all dimensions.
std::vector< int > indices_
Indices of current grid point.
const self_type operator-(std::vector< int > shift)
Subtraction iterator.
self_type & operator--()
Pre-decrement operator.
static Grid< T > * BuildGrid(const Json::Value &json, const std::string &path)
Set up the grid.
static Grid< T > * BuildGrid(const Json::Value &json)
Set up the grid.
std::vector< int > & indices()
Access indices.
const self_type operator+(std::vector< int > shift)
Addition operator.
std::vector< double > coordinates() const
Access coordinates.
double coordinate(size_t d) const
Access specific coordinate dimension.
std::bidirectional_iterator_tag iterator_category
HistIterator is a bidirectional iterator.
const_iterator end() const
Return const iterator after last valid grid point.
int & index(size_t d)
Access a specific index.
Grid(std::vector< int > numPoints, std::vector< double > lower, std::vector< double > upper, std::vector< bool > isPeriodic)
Constructor.
int difference_type
Difference type is an int.
void LoadFromFile(const std::string &filename)
Builds a grid from file.
reference operator*()
Dereference operator.
size_t mapTo1d(const std::vector< int > &indices) const override
Map d-dimensional indices to 1-d data vector.
GridIterator< const T > const_iterator
Custom constant iterator over a grid.
R value_type
Either T or const T for iterator and const_iterator, respectively.
virtual void Validate(const Value &json, const std::string &path) override
Validate JSON value.
iterator end()
Return iterator after last valid grid point.
self_type & operator-=(std::vector< int > shift)
Subtraction assignment operator.