herwig
is hosted by
Hepforge
,
IPPP Durham
Herwig
7.3.0
Sampling
CellGrids
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
29
namespace
ExSample {
30
34
inline
double
sqr
(
double
x) {
35
return
x*x;
36
}
37
42
class
CellGrid
{
43
44
public
:
45
49
CellGrid
()
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
140
CellGrid
&
firstChild
();
141
145
const
CellGrid
&
secondChild
()
const
;
146
150
CellGrid
&
secondChild
();
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
188
void
updateIntegral
();
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
271
double
theVolumeOrIntegral
;
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
ExSample::CellGrid
A binary cell grid.
Definition:
CellGrid.h:42
ExSample::CellGrid::project
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.
ExSample::CellGrid::theUpperRight
std::vector< double > theUpperRight
The upper right corner of the cell grid.
Definition:
CellGrid.h:260
ExSample::CellGrid::depth
std::size_t depth() const
Return the depth of this grid.
ExSample::CellGrid::updateIntegral
void updateIntegral()
Update the integrals.
ExSample::CellGrid::projectInterval
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...
ExSample::CellGrid::toXML
virtual XML::Element toXML() const
Return an XML element for the data of this CellGrid.
ExSample::CellGrid::doMinimumSelection
void doMinimumSelection(double r, double ref)
Ensure a minimum cell selection probability.
ExSample::CellGrid::theUpperBoundInclusive
std::vector< bool > theUpperBoundInclusive
Flag in which dimension this cell is upper bound inclusive.
Definition:
CellGrid.h:265
ExSample::CellGrid::boundaries
void boundaries(const std::vector< double > &newLowerLeft, const std::vector< double > &newUpperRight)
Set the boundaries.
ExSample::CellGrid::theChildren
std::vector< CellGrid * > theChildren
The children cell grids.
Definition:
CellGrid.h:281
ExSample::CellGrid::lowerLeft
const std::vector< double > & lowerLeft() const
Return the lower left corner of the cell grid.
Definition:
CellGrid.h:87
ExSample::CellGrid::firstChild
const CellGrid & firstChild() const
Return the first child.
ExSample::CellGrid::contains
bool contains(const std::vector< double > &point, const std::vector< bool > ¶meterFlags) const
Return true, if this grid contains the given parameter point.
ExSample::CellGrid::dumpPartToC
void dumpPartToC(std::ostream &os, std::string prefix="") const
Write out C code corresponding to the cell grid function.
ExSample::CellGrid::integral
double integral() const
Return the integral relevant for the last parameter point set.
ExSample::CellGrid::splitPoint
std::pair< std::size_t, double > splitPoint() const
Return the dimension and coordinate along which the first split of this grid occurs.
ExSample::CellGrid::fromXML
virtual void fromXML(const XML::Element &)
Fill CellGrid data from an XML element.
ExSample::CellGrid::isLeaf
bool isLeaf() const
Return true, if this is a leaf in the tree.
Definition:
CellGrid.h:108
ExSample::CellGrid::splitCoordinates
void splitCoordinates(std::size_t, std::set< double > &) const
Fill split coordinates along a given dimension.
ExSample::CellGrid::active
bool active() const
Return true, if this grid is active with respect to the last parameter point passed.
ExSample::CellGrid::split
virtual void split(std::size_t newSplitDimension, double newSplitCoordinate)
Split this cell grid in the given dimension and coordinate, if it is a leaf.
ExSample::CellGrid::theWeight
double theWeight
The weight.
Definition:
CellGrid.h:276
ExSample::CellGrid::volume
double volume(const std::vector< double > &lowerLeft, const std::vector< double > &upperRight) const
Calculate a volume given upper and lower bound.
ExSample::CellGrid::theLowerLeft
std::vector< double > theLowerLeft
The lower left corner of the cell grid.
Definition:
CellGrid.h:255
ExSample::CellGrid::~CellGrid
virtual ~CellGrid()
The destructor.
ExSample::CellGrid::randomGrid
void randomGrid(RndGenerator &rnd, std::size_t maxDepth, bool forceSplit=true)
Generate a random grid of given maximum depth.
Definition:
CellGrid.h:291
ExSample::CellGrid::upperBoundInclusive
const std::vector< bool > & upperBoundInclusive() const
Flag in which dimension this cell is upper bound inclusive.
Definition:
CellGrid.h:97
ExSample::CellGrid::volume
double volume() const
Return the volume relevant for the last parameter point set.
ExSample::CellGrid::theVolumeOrIntegral
double theVolumeOrIntegral
The volume (leafs) or integral (nodes) relevant for the last parameter point set.
Definition:
CellGrid.h:271
ExSample::CellGrid::makeInstance
virtual CellGrid * makeInstance() const
Produce a new instance of a cell grid.
ExSample::CellGrid::makeInstance
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.
ExSample::CellGrid::size
std::size_t size() const
Return the number of nodes contained in this grid.
ExSample::CellGrid::weight
double weight() const
Return the weight.
Definition:
CellGrid.h:178
ExSample::CellGrid::dumpToC
void dumpToC(std::ostream &os, const std::string &name) const
Write out C code corresponding to the cell grid function.
ExSample::CellGrid::minimumSelection
void minimumSelection(double p=0.1)
Ensure a minimum cell selection probability.
ExSample::CellGrid::nonParametricVolume
double nonParametricVolume(const std::vector< double > &point, const std::vector< bool > ¶meterFlags) const
Get the volume relevant for the dimensions not considered parameters.
ExSample::CellGrid::CellGrid
CellGrid()
Default constructor.
Definition:
CellGrid.h:49
ExSample::CellGrid::upperRight
const std::vector< double > & upperRight() const
Return the upper right corner of the cell grid.
Definition:
CellGrid.h:92
ExSample::CellGrid::weight
void weight(double w)
Set the weight.
ExSample::CellGrid::secondChild
const CellGrid & secondChild() const
Return the second child.
ExSample::CellGrid::CellGrid
CellGrid(const std::vector< double > &newLowerLeft, const std::vector< double > &newUpperRight, double newWeight=0.0)
Construct given boundaries and a weight.
ExSample::CellGrid::updateIntegral
void updateIntegral(const std::vector< double > &point, const std::vector< bool > ¶meterFlags, std::vector< bool >::iterator hashPosition)
Set a parameter point and flag which dimensions are considered parameters.
ExSample::CellGrid::secondChild
CellGrid & secondChild()
Access the second child.
ExSample::CellGrid::firstChild
CellGrid & firstChild()
Access the first child.
XML::Element
Element represents a (tree of) XML elements.
Definition:
Element.h:56
sqr
constexpr auto sqr(const T &x) -> decltype(x *x)
Generated on Thu Jun 20 2024 17:50:53 for Herwig by
1.9.6