herwig is hosted by Hepforge, IPPP Durham
Herwig 7.3.0
CellGrid.h
1// -*- C++ -*-
2//
3// CellGrid.hpp is a part of ExSample
4// Copyright (C) 2012-2019 Simon Platzer, The Herwig Collaboration
5//
6// ExSample is licenced under version 3 of the GPL, see COPYING for details.
7//
8
9#ifndef EXSAMPLE_CellGrid_hpp_included
10#define EXSAMPLE_CellGrid_hpp_included
11
12#include <vector>
13#include <utility>
14#include <set>
15#include <map>
16#include <cstdlib>
17#include <cassert>
18#include <cmath>
19
20#define units
21
22#ifdef units
23#include <iostream>
24#include <string>
25#endif
26
27#include "Herwig/Utilities/XML/Element.h"
28
29namespace ExSample {
30
34 inline double sqr(double x) {
35 return x*x;
36 }
37
42 class CellGrid {
43
44 public:
45
50 : theVolumeOrIntegral(0.0), theWeight(0.0) {}
51
55 CellGrid(const std::vector<double>& newLowerLeft,
56 const std::vector<double>& newUpperRight,
57 double newWeight = 0.0);
58
62 virtual CellGrid* makeInstance() const;
63
67 virtual CellGrid* makeInstance(const std::vector<double>& newLowerLeft,
68 const std::vector<double>& newUpperRight,
69 double newWeight = 0.0) const;
70
74 virtual ~CellGrid();
75
76 public:
77
81 void boundaries(const std::vector<double>& newLowerLeft,
82 const std::vector<double>& newUpperRight);
83
87 const std::vector<double>& lowerLeft() const { return theLowerLeft; }
88
92 const std::vector<double>& upperRight() const { return theUpperRight; }
93
97 const std::vector<bool>& upperBoundInclusive() const { return theUpperBoundInclusive; }
98
102 double volume(const std::vector<double>& lowerLeft,
103 const std::vector<double>& upperRight) const;
104
108 bool isLeaf() const { return theChildren.empty(); }
109
113 std::size_t depth() const;
114
118 std::size_t size() const;
119
124 virtual void split(std::size_t newSplitDimension, double newSplitCoordinate);
125
130 std::pair<std::size_t,double> splitPoint() const;
131
135 const CellGrid& firstChild() const;
136
141
145 const CellGrid& secondChild() const;
146
151
155 void splitCoordinates(std::size_t, std::set<double>&) const;
156
157 public:
158
163 bool active() const;
164
168 double volume() const;
169
173 double integral() const;
174
178 double weight() const { return theWeight; }
179
183 void weight(double w);
184
189
193 void minimumSelection(double p = 0.1);
194
198 bool contains(const std::vector<double>& point,
199 const std::vector<bool>& parameterFlags) const;
200
205 double nonParametricVolume(const std::vector<double>& point,
206 const std::vector<bool>& parameterFlags) const;
207
212 void updateIntegral(const std::vector<double>& point,
213 const std::vector<bool>& parameterFlags,
214 std::vector<bool>::iterator hashPosition);
215
222 double projectInterval(const std::pair<double,double>& interval,
223 std::size_t dimension) const;
224
228 std::map<std::pair<double,double>,double>
229 project(std::pair<double,double> interval,
230 std::size_t dimension) const;
231
232 public:
233
237 virtual void fromXML(const XML::Element&);
238
242 virtual XML::Element toXML() const;
243
244 private:
245
249 void doMinimumSelection(double r,
250 double ref);
251
255 std::vector<double> theLowerLeft;
256
260 std::vector<double> theUpperRight;
261
265 std::vector<bool> theUpperBoundInclusive;
266
272
276 double theWeight;
277
281 std::vector<CellGrid*> theChildren;
282
283#ifdef units
284
285 public:
286
290 template<class RndGenerator>
291 void randomGrid(RndGenerator& rnd,
292 std::size_t maxDepth,
293 bool forceSplit = true) {
294 if ( maxDepth > 0 ) {
295 if ( !forceSplit )
296 if ( rnd.rnd() < 0.5 )
297 return;
298 std::size_t dimension = (std::size_t)(std::floor(rnd.rnd()*lowerLeft().size()));
299 double point = 0.5*(lowerLeft()[dimension]+upperRight()[dimension]);
300 split(dimension,point);
301 firstChild().weight(rnd.rnd());
302 secondChild().weight(rnd.rnd());
303 firstChild().randomGrid(rnd,maxDepth-1,false);
304 secondChild().randomGrid(rnd,maxDepth-1,false);
305 }
306 }
307
311 void dumpToC(std::ostream& os,
312 const std::string& name) const;
313
317 void dumpPartToC(std::ostream& os,
318 std::string prefix = "") const;
319
320#endif
321
322 };
323
324}
325
326#endif // EXSAMPLE_CellGrid_hpp_included
327
A binary cell grid.
Definition: CellGrid.h:42
std::map< std::pair< double, double >, double > project(std::pair< double, double > interval, std::size_t dimension) const
Return the projection to the given interval and dimension.
std::vector< double > theUpperRight
The upper right corner of the cell grid.
Definition: CellGrid.h:260
std::size_t depth() const
Return the depth of this grid.
void updateIntegral()
Update the integrals.
double projectInterval(const std::pair< double, double > &interval, std::size_t dimension) const
Return the projection to the given interval and dimension, provided the interval is not overlapping w...
virtual XML::Element toXML() const
Return an XML element for the data of this CellGrid.
void doMinimumSelection(double r, double ref)
Ensure a minimum cell selection probability.
std::vector< bool > theUpperBoundInclusive
Flag in which dimension this cell is upper bound inclusive.
Definition: CellGrid.h:265
void boundaries(const std::vector< double > &newLowerLeft, const std::vector< double > &newUpperRight)
Set the boundaries.
std::vector< CellGrid * > theChildren
The children cell grids.
Definition: CellGrid.h:281
const std::vector< double > & lowerLeft() const
Return the lower left corner of the cell grid.
Definition: CellGrid.h:87
const CellGrid & firstChild() const
Return the first child.
bool contains(const std::vector< double > &point, const std::vector< bool > &parameterFlags) const
Return true, if this grid contains the given parameter point.
void dumpPartToC(std::ostream &os, std::string prefix="") const
Write out C code corresponding to the cell grid function.
double integral() const
Return the integral relevant for the last parameter point set.
std::pair< std::size_t, double > splitPoint() const
Return the dimension and coordinate along which the first split of this grid occurs.
virtual void fromXML(const XML::Element &)
Fill CellGrid data from an XML element.
bool isLeaf() const
Return true, if this is a leaf in the tree.
Definition: CellGrid.h:108
void splitCoordinates(std::size_t, std::set< double > &) const
Fill split coordinates along a given dimension.
bool active() const
Return true, if this grid is active with respect to the last parameter point passed.
virtual void split(std::size_t newSplitDimension, double newSplitCoordinate)
Split this cell grid in the given dimension and coordinate, if it is a leaf.
double theWeight
The weight.
Definition: CellGrid.h:276
double volume(const std::vector< double > &lowerLeft, const std::vector< double > &upperRight) const
Calculate a volume given upper and lower bound.
std::vector< double > theLowerLeft
The lower left corner of the cell grid.
Definition: CellGrid.h:255
virtual ~CellGrid()
The destructor.
void randomGrid(RndGenerator &rnd, std::size_t maxDepth, bool forceSplit=true)
Generate a random grid of given maximum depth.
Definition: CellGrid.h:291
const std::vector< bool > & upperBoundInclusive() const
Flag in which dimension this cell is upper bound inclusive.
Definition: CellGrid.h:97
double volume() const
Return the volume relevant for the last parameter point set.
double theVolumeOrIntegral
The volume (leafs) or integral (nodes) relevant for the last parameter point set.
Definition: CellGrid.h:271
virtual CellGrid * makeInstance() const
Produce a new instance of a cell grid.
virtual CellGrid * makeInstance(const std::vector< double > &newLowerLeft, const std::vector< double > &newUpperRight, double newWeight=0.0) const
Produce a new instance of a cell grid.
std::size_t size() const
Return the number of nodes contained in this grid.
double weight() const
Return the weight.
Definition: CellGrid.h:178
void dumpToC(std::ostream &os, const std::string &name) const
Write out C code corresponding to the cell grid function.
void minimumSelection(double p=0.1)
Ensure a minimum cell selection probability.
double nonParametricVolume(const std::vector< double > &point, const std::vector< bool > &parameterFlags) const
Get the volume relevant for the dimensions not considered parameters.
CellGrid()
Default constructor.
Definition: CellGrid.h:49
const std::vector< double > & upperRight() const
Return the upper right corner of the cell grid.
Definition: CellGrid.h:92
void weight(double w)
Set the weight.
const CellGrid & secondChild() const
Return the second child.
CellGrid(const std::vector< double > &newLowerLeft, const std::vector< double > &newUpperRight, double newWeight=0.0)
Construct given boundaries and a weight.
void updateIntegral(const std::vector< double > &point, const std::vector< bool > &parameterFlags, std::vector< bool >::iterator hashPosition)
Set a parameter point and flag which dimensions are considered parameters.
CellGrid & secondChild()
Access the second child.
CellGrid & firstChild()
Access the first child.
Element represents a (tree of) XML elements.
Definition: Element.h:56
constexpr auto sqr(const T &x) -> decltype(x *x)