Geant4 11.4.0
Toolkit for the simulation of the passage of particles through matter
Loading...
Searching...
No Matches
GIDI_regions1d.cc
Go to the documentation of this file.
1/*
2# <<BEGIN-copyright>>
3# Copyright 2019, Lawrence Livermore National Security, LLC.
4# This file is part of the gidiplus package (https://github.com/LLNL/gidiplus).
5# gidiplus is licensed under the MIT license (see https://opensource.org/licenses/MIT).
6# SPDX-License-Identifier: MIT
7# <<END-copyright>>
8*/
9
10#include "GIDI.hpp"
11#include <HAPI.hpp>
12
13namespace GIDI {
14
15namespace Functions {
16
17/* *********************************************************************************************************//**
18 *
19 *
20 * @param a_x [in] The
21 * @param a_epsilon [in] The fractional amount to move the x-value.
22 *
23 * @return double.
24 ***********************************************************************************************************/
25
26static double getAdjustedX( double a_x, double a_epsilon ) {
27
28 double xp = a_epsilon;
29
30 if( a_x != 0.0 ) {
31 if( a_x < 0 ) {
32 xp = a_x * ( 1.0 - a_epsilon ); }
33 else {
34 xp = a_x * ( 1.0 + a_epsilon );
35 }
36 }
37
38 return( xp );
39}
40
41/*! \class Regions1d
42 * Class for the GNDS <**regions1d**> node.
43 */
44
45/* *********************************************************************************************************//**
46 * @param a_construction [in] Used to pass user options to the constructor.
47 * @param a_node [in] The **HAPI::Node** to be parsed and used to construct the XYs2d.
48 * @param a_setupInfo [in] Information create my the Protare constructor to help in parsing.
49 * @param a_parent [in] The parent GIDI::Suite.
50 ***********************************************************************************************************/
51
52Regions1d::Regions1d( Construction::Settings const &a_construction, HAPI::Node const &a_node, SetupInfo &a_setupInfo, Suite *a_parent ) :
53 Function1dForm( a_construction, a_node, a_setupInfo, FormType::regions1d, a_parent ) {
54
56 data1dListParse( a_construction, a_node.child( GIDI_function1dsChars ), a_setupInfo, m_function1ds );
57 checkSequentialDomainLimits1d( m_function1ds, m_Xs );
58 return; // Need to add uncertainty parsing.
59 }
60
61 for( HAPI::Node child = a_node.first_child( ); !child.empty( ); child.to_next_sibling( ) ) {
62 std::string name( child.name( ) );
63
64 if( name == GIDI_axesChars ) continue;
65 if( name == GIDI_uncertaintyChars ) continue;
66
67 Function1dForm *_form = data1dParse( a_construction, child, a_setupInfo, nullptr );
68 if( _form == nullptr ) throw Exception( "Regions1d::Regions1d: data1dParse returned nullptr." );
69 append( _form );
70 }
71}
72
73/* *********************************************************************************************************//**
74 ***********************************************************************************************************/
75
77
78 for( std::vector<Function1dForm *>::iterator iter = m_function1ds.begin( ); iter < m_function1ds.end( ); ++iter ) delete *iter;
79}
80
81/* *********************************************************************************************************//**
82 * Returns the domain minimum for the instance.
83 *
84 * @return The domain minimum for the instance.
85 ***********************************************************************************************************/
86
87double Regions1d::domainMin( ) const {
88
89 if( m_Xs.size( ) == 0 ) throw Exception( "Regions1d::domainMin: Regions1d has no regions" );
90 return( m_Xs[0] );
91}
92
93/* *********************************************************************************************************//**
94 * Returns the domain maximum for the instance.
95 *
96 * @return The domain maximum for the instance.
97 ***********************************************************************************************************/
98
99double Regions1d::domainMax( ) const {
100
101 if( m_Xs.size( ) == 0 ) throw Exception( "Regions1d::domainMax: Regions1d has not regions" );
102 return( m_Xs[m_Xs.size( )-1] );
103}
104
105/* *********************************************************************************************************//**
106 * Appends the 1d function *a_function* to the region.
107 *
108 * @param a_function [in] The 1d function (i.e., 1d region) to append to the Regions1d.
109 ***********************************************************************************************************/
110
112
113 if( dimension( ) != a_function->dimension( ) ) throw Exception( "Regions1d::append: dimensions differ." );
114
115 double _domainMin = a_function->domainMin( ), _domainMax = a_function->domainMax( );
116
117 if( m_Xs.size( ) == 0 ) {
118 m_Xs.push_back( _domainMin ); }
119 else {
120 if( m_Xs.back( ) != _domainMin ) throw Exception( "Regions1d::append: regions do not abut." );
121 }
122
123 m_Xs.push_back( _domainMax );
124 m_function1ds.push_back( a_function );
125}
126
127/* *********************************************************************************************************//**
128 * The value of *y(x1)* at the point *a_x1*.
129 *
130 * @param a_x1 [in] Domain value to evaluate this at.
131 * @return The value of this at the point *a_x1*.
132 ***********************************************************************************************************/
133
134
135double Regions1d::evaluate( double a_x1 ) const {
136
137 if( m_Xs.size( ) == 0 ) throw Exception( "Regions1d::evaluate: Regions1d has not regions" );
138
139 long iX1 = binarySearchVector( a_x1, m_Xs );
140
141 if( iX1 < 0 ) {
142 if( iX1 == -1 ) { /* x1 > last value of Xs. */
143 return( m_function1ds.back( )->evaluate( a_x1 ) );
144 }
145 iX1 = 0; /* x1 < last value of Xs. */
146 }
147
148 return( m_function1ds[static_cast<std::size_t>(iX1)]->evaluate( a_x1 ) );
149}
150
151/* *********************************************************************************************************//**
152 * Evaluates *this* at the X-values in *a_Xs*[*a_offset*:] and adds the results to *a_results*[*a_offset*:].
153 * *a_Xs* and *a_results* must be the same size otherwise a throw is executed.
154 *
155 * @param a_offset [in] The offset in *a_Xs* to start.
156 * @param a_Xs [in] The list of domain values to evaluate *this* at.
157 * @param a_results [in] The list whose values are added to by the Y-values of *this*.
158 * @param a_scaleFactor [in] A factor applied to each evaluation before it is added to *a_results*.
159 ***********************************************************************************************************/
160
161void Regions1d::mapToXsAndAdd( std::size_t a_offset, std::vector<double> const &a_Xs, std::vector<double> &a_results, double a_scaleFactor ) const {
162
163 for( auto iter = m_function1ds.begin( ); iter < m_function1ds.end( ); ++iter ) {
164 (*iter)->mapToXsAndAdd( a_offset, a_Xs, a_results, a_scaleFactor );
165 }
166}
167
168/* *********************************************************************************************************//**
169 * This methods returns an XYs1d representation of *this*. The calling function owns the created instance and is responible
170 * for freeing it.
171 *
172 * @param a_asLinlin [in] If **true**, the inpolatation of the returned XYs1d instance will always be lin-lin. Otherwise,
173 * the interpolation depends on the child 1d functions.
174 * @param a_accuracy [in] The accuracy use to convert the data to lin=lin interpolation if needed.
175 * @param a_lowerEps [in] The relative domain ammount to put a point below a boundary between two regions.
176 * @param a_upperEps [in] The relative domain ammount to put a point above a boundary between two regions.
177 *
178 * @return A pointer to an XYs1d instance that must be freed by the calling function.
179 ***********************************************************************************************************/
180
181XYs1d *Regions1d::asXYs1d( bool a_asLinlin, double a_accuracy, double a_lowerEps, double a_upperEps ) const {
182
183 XYs1d *xys1d1 = nullptr, *xys1d2 = nullptr;
184
185 if( a_lowerEps < 1e-14 ) a_lowerEps = 0.0; // 1e-14 should be a global parameter.
186 if( a_upperEps < 1e-14 ) a_upperEps = 0.0;
187
188 if( !a_asLinlin ) {
189 bool firstRegion = true;
191 for( auto regionIter = m_function1ds.begin( ); regionIter != m_function1ds.end( ); ++regionIter ) {
192 if( (*regionIter)->moniker( ) == GIDI_XYs1dChars ) {
193 xys1d2 = static_cast<XYs1d *>( *regionIter );
194 if( firstRegion ) interpolation2 = ptwXY_getInterpolation( xys1d2->ptwXY( ) );
195 if( interpolation2 != ptwXY_getInterpolation( xys1d2->ptwXY( ) ) ) {
196 a_asLinlin = true;
197 break;
198 } }
199 else {
200 a_asLinlin = true;
201 break;
202 }
203 }
204 }
205
206 std::vector<double> xs;
207 std::vector<double> ys;
208
209 for( auto regionIter = m_function1ds.begin( ); regionIter != m_function1ds.end( ); ++regionIter ) {
210 xys1d2 = (*regionIter)->asXYs1d( a_asLinlin, a_accuracy, a_lowerEps, a_upperEps );
211 if( xys1d2 == nullptr ) {
212 delete xys1d1;
213 return( nullptr );
214 }
215
216 std::vector<double> xs2( xys1d2->xs( ) );
217 if( xs2.size( ) < 2 ) continue;
218
219 std::size_t xySize = xs.size( );
220 std::vector<double> ys2( xys1d2->ys( ) );
221 if( xySize > 0 ) {
222 double y1u = ys.back( );
223 double y2l = ys2[0];
224
225 if( y1u == y2l ) { // Simple, just remove last point in xs and ys.
226 xs.resize( xySize - 1 );
227 ys.resize( xySize - 1 ); }
228 else {
229 bool addLower = false, addUpper = false;
230 double x1l = xs[xySize-2];
231 double x1u = xs.back( );
232 double x2l = xs2[0];
233 double x2u = xs2[1];
234 double x1up = 0.0, x2lp= 0.0;
235
236 if( a_lowerEps != 0.0 ) {
237 double x1lp = getAdjustedX( x1l, 0.8 * a_lowerEps );
238 x1up = getAdjustedX( x1u, -a_lowerEps ); // Point to add in xs below xs.back() if room.
239
240 addLower = x1lp < x1up; // Only add if relative spacing between existing point is greater than 1.8 * a_lowerEps.
241 }
242
243 if( a_upperEps > 0 ) {
244 x2lp = getAdjustedX( x2l, a_upperEps ); // Point to add in xs2 above xs2[0] if room.
245 double x2up = getAdjustedX( x2u, 0.8 * -a_upperEps );
246
247 addUpper = x2lp < x2up; // Only add if relative spacing between existing point is greater than 1.8 * a_upperEps.
248 }
249
250 if( addLower ) {
251 if( addUpper ) {
252 xs.back( ) = x1up;
253 ys.back( ) = xys1d1->evaluate( x1up );
254 xs.push_back( x1u );
255 ys.push_back( 0.5 * ( y1u + y2l ) );
256 xs2[0] = x2lp;
257 ys2[0] = xys1d2->evaluate( x2lp ); }
258 else {
259 xs.back( ) = x1up;
260 ys.back( ) = xys1d1->evaluate( x1up );
261 } }
262 else if( addUpper ) {
263 xs2[0] = x2lp;
264 ys2[0] = xys1d2->evaluate( x2lp ); }
265 else {
266 ys2[0] = 0.5 * ( y1u + y2l );
267 xs.resize( xySize - 1 );
268 ys.resize( xySize - 1 );
269 }
270 }
271 }
272 xs.insert( xs.end( ), xs2.begin( ), xs2.end( ) );
273 ys.insert( ys.end( ), ys2.begin( ), ys2.end( ) );
274 xys1d1 = xys1d2;
275 }
276 delete xys1d1;
277
278 return( new XYs1d( axes( ), ptwXY_interpolationLinLin, xs, ys ) );
279}
280
281/* *********************************************************************************************************//**
282 * Fills the argument *a_writeInfo* with the XML lines that represent *this*. Recursively enters each sub-node.
283 *
284 * @param a_writeInfo [in/out] Instance containing incremental indentation and other information and stores the appended lines.
285 * @param a_indent [in] The amount to indent *this* node.
286 * @param a_embedded [in] If *true*, *this* function is embedded in a higher dimensional function.
287 * @param a_inRegions [in] If *true*, *this* is in a Regions1d container.
288 ***********************************************************************************************************/
289
290void Regions1d::toXMLList_func( GUPI::WriteInfo &a_writeInfo, std::string const &a_indent, bool a_embedded, bool a_inRegions ) const {
291
292 std::string indent2 = a_writeInfo.incrementalIndent( a_indent );
293 std::string indent3 = a_writeInfo.incrementalIndent( indent2 );
294 std::string attributes;
295
296 if( a_embedded ) {
298 else {
299 if( a_inRegions ) {
300 attributes = a_writeInfo.addAttribute( GIDI_indexChars, intToString( index( ) ) ); }
301 else {
302 if( label( ) != "" ) attributes = a_writeInfo.addAttribute( GIDI_labelChars, label( ) );
303 }
304 }
305
306 a_writeInfo.addNodeStarter( a_indent, moniker( ), attributes );
307 axes( ).toXMLList( a_writeInfo, indent2 );
308 a_writeInfo.push_back( indent2 + "<function1ds>" );
309 for( std::vector<Function1dForm *>::const_iterator iter = m_function1ds.begin( ); iter != m_function1ds.end( ); ++iter ) (*iter)->toXMLList_func( a_writeInfo, indent3, false, true );
310 a_writeInfo.push_back( "</function1ds>" );
311 a_writeInfo.addNodeEnder( moniker( ) );
312}
313
314/* *********************************************************************************************************//**
315 * This method calls the write method for each region of *this*.
316 *
317 * @param a_file [in] The C FILE instance to write the data to.
318 * @param a_format [in] The format string passed to each region's write method.
319 ***********************************************************************************************************/
320
321void Regions1d::write( FILE *a_file, std::string const &a_format ) const {
322
323 for( auto regionIter = m_function1ds.begin( ); regionIter != m_function1ds.end( ); ++regionIter ) {
324 (*regionIter)->write( a_file, a_format );
325 }
326}
327
328} // End namespace Functions.
329
330} // End namespace GIDI.
#define GIDI_XYs1dChars
Definition GIDI.hpp:288
#define GIDI_outerDomainValueChars
Definition GIDI.hpp:436
#define GIDI_labelChars
Definition GIDI.hpp:438
#define GIDI_indexChars
Definition GIDI.hpp:437
#define GNDS_formatVersion_1_10Chars
Definition LUPI.hpp:48
void toXMLList(GUPI::WriteInfo &a_writeInfo, std::string const &a_indent="") const
Definition GIDI_axes.cc:113
std::string const & label() const
Definition GIDI.hpp:658
Function1dForm(std::string const &a_moniker, FormType a_type, ptwXY_interpolation a_interpolation, int a_index, double a_outerDomainValue)
Definition GIDI_form.cc:348
virtual double domainMax() const =0
Axes const & axes() const
Definition GIDI.hpp:1012
double outerDomainValue() const
Definition GIDI.hpp:1010
virtual double domainMin() const =0
void write(FILE *a_file, std::string const &a_format) const
void append(Function1dForm *a_function)
Regions1d(Construction::Settings const &a_construction, HAPI::Node const &a_node, SetupInfo &a_setupInfo, Suite *a_parent)
void mapToXsAndAdd(std::size_t a_offset, std::vector< double > const &a_Xs, std::vector< double > &a_results, double a_scaleFactor) const
double evaluate(double a_x1) const
XYs1d * asXYs1d(bool a_asLinlin, double a_accuray, double a_lowerEps, double a_upperEps) const
void toXMLList_func(GUPI::WriteInfo &a_writeInfo, std::string const &a_indent, bool a_embedded, bool a_inRegions) const
double evaluate(double a_x1) const
LUPI::FormatVersion m_formatVersion
Definition GIDI.hpp:599
std::string const & moniker() const
Definition GUPI.hpp:102
void push_back(std::string const &a_line)
Definition GUPI.hpp:53
void addNodeEnder(std::string const &a_moniker)
Definition GUPI.hpp:59
std::string incrementalIndent(std::string const &indent)
Definition GUPI.hpp:52
void addNodeStarter(std::string const &indent, std::string const &a_moniker, std::string const &a_attributes="")
Definition GUPI.hpp:55
std::string addAttribute(std::string const &a_name, std::string const &a_value) const
Definition GUPI.hpp:60
bool empty() const
Definition HAPI_Node.cc:150
Node first_child() const
Definition HAPI_Node.cc:82
std::string const & format() const
Definition LUPI.hpp:74
Definition GIDI.hpp:32
FormType
Definition GIDI.hpp:118
long binarySearchVector(double a_x, std::vector< double > const &a_Xs)
Definition GIDI_misc.cc:33
std::string intToString(int a_value)
Definition GIDI_misc.cc:415
std::string doubleToShortestString(double a_value, int a_significantDigits=15, int a_favorEFormBy=0)
Definition LUPI_misc.cc:349
ptwXY_interpolation ptwXY_getInterpolation(ptwXYPoints *ptwXY)
Definition ptwXY_core.c:518
enum ptwXY_interpolation_e ptwXY_interpolation
@ ptwXY_interpolationLinLin
Definition ptwXY.h:37