Geant4 11.4.0
Toolkit for the simulation of the passage of particles through matter
Loading...
Searching...
No Matches
GIDI_settings_group.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 <iostream>
11#include <stdio.h>
12#include <stdlib.h>
13
14#include "GIDI.hpp"
15
16namespace GIDI {
17
18namespace Transporting {
19
20/*! \class MultiGroup
21 * Specifies the flux data for a specified Legendre order (see class Flux).
22 */
23
24/* *********************************************************************************************************//**
25 ***********************************************************************************************************/
26
30
31/* *********************************************************************************************************//**
32 * @param a_label [in] The label for the MultiGroup.
33 * @param a_length [in] The number of boundaries values.
34 * @param a_boundaries [in] The list of boundaries.
35 ***********************************************************************************************************/
36
37MultiGroup::MultiGroup( std::string const &a_label, int a_length, double const *a_boundaries ) :
38 m_label( a_label ) {
39
40 for( int i1 = 0; i1 < a_length; ++i1 ) m_boundaries.push_back( a_boundaries[i1] );
41}
42
43/* *********************************************************************************************************//**
44 * @param a_label [in] The label for the MultiGroup.
45 * @param a_boundaries [in] The list of boundaries.
46 ***********************************************************************************************************/
47
48MultiGroup::MultiGroup( std::string const &a_label, std::vector<double> const &a_boundaries ) :
49 m_label( a_label ),
50 m_boundaries( a_boundaries ) {
51
52}
53
54/* *********************************************************************************************************//**
55 * @param a_group [in] The Group used to set *this*.
56 ***********************************************************************************************************/
57
58MultiGroup::MultiGroup( Group const &a_group ) :
59 m_label( a_group.label( ) ),
60 m_boundaries( a_group.data( ) ) {
61
62}
63
64/* *********************************************************************************************************//**
65 * @param a_multiGroup [in] The MultiGroup instance to copy.
66 ***********************************************************************************************************/
67
68MultiGroup::MultiGroup( MultiGroup const &a_multiGroup ) :
69 m_label( a_multiGroup.label( ) ),
70 m_boundaries( a_multiGroup.boundaries( ) ) {
71
72}
73
74/* *********************************************************************************************************//**
75 ***********************************************************************************************************/
76
80
81/* *********************************************************************************************************//**
82 * The assignment operator. This method sets the members of *this* to those of *a_rhs*.
83 *
84 * @param a_rhs [in] Instance whose member are used to set the members of *this*.
85 *
86 * @return A reference to the updated MultiGroup instance.
87 ***********************************************************************************************************/
88
90
91 if( this != &a_rhs ) {
92 m_label = a_rhs.label( );
93 m_boundaries = a_rhs.boundaries( );
94 }
95
96 return( *this );
97}
98
99/* *********************************************************************************************************//**
100 * Returns the multi-group index whose boundaries enclose *a_energy*. If *a_encloseOutOfRange* is true and
101 * *a_energy* is below the lowest boundary, 0 is returned, otherwise -2 is returned. If *a_encloseOutOfRange* is true and
102 * *a_energy* is above the highest boundary, the last multi-group index is returned, otherwise -1 is returned.
103 *
104 * @param a_energy [in] The energy of the whose index is to be returned.
105 * @param a_encloseOutOfRange Determines the action if energy is below or above the domain of the boundaries.
106 * @return The index whose boundaries enclose *a_energy*.
107 ***********************************************************************************************************/
108
109int MultiGroup::multiGroupIndexFromEnergy( double a_energy, bool a_encloseOutOfRange ) const {
110
111 std::size_t iMin = 0, iMid, iMax = m_boundaries.size( ), iMaxM1 = iMax - 1;
112
113 if( iMax == 0 ) return( -3 );
114 if( a_energy < m_boundaries[0] ) {
115 if( a_encloseOutOfRange ) return( 0 );
116 return( -2 );
117 }
118 if( a_energy > m_boundaries[iMaxM1] ) {
119 if( a_encloseOutOfRange ) return( static_cast<int>( iMax ) - 2 );
120 return( -1 );
121 }
122 while( 1 ) {
123 iMid = ( iMin + iMax ) >> 1;
124 if( iMid == iMin ) break;
125 if( a_energy < m_boundaries[iMid] ) {
126 iMax = iMid; }
127 else {
128 iMin = iMid;
129 }
130 }
131 if( iMin == iMaxM1 ) iMin--;
132 return( static_cast<int>( iMin ) );
133}
134
135/* *********************************************************************************************************//**
136 * @param a_label [in] The label for *this*.
137 * @param a_boundaries [in] The boundaries to set *this* to.
138 ***********************************************************************************************************/
139
140void MultiGroup::set( std::string const &a_label, std::vector<double> const &a_boundaries ) {
141
142 m_label = a_label;
143 m_boundaries = a_boundaries;
144}
145
146/* *********************************************************************************************************//**
147 * Print the MultiGroup to std::cout. Mainly for debugging.
148 *
149 * @param a_indent [in] The std::string to print at the beginning.
150 * @param a_outline [in] If true, does not print the flux values.
151 * @param a_valuesPerLine [in] The number of points (i.e., energy, flux pairs) to print per line.
152 ***********************************************************************************************************/
153
154void MultiGroup::print( std::string const &a_indent, bool a_outline, unsigned int a_valuesPerLine ) const {
155
156 std::size_t nbs = size( );
157 bool printIndent( true );
158
159 std::cout << a_indent << "GROUP: label = '" << m_label << "': length = " << nbs << std::endl;
160 if( a_outline ) return;
161 for( std::size_t ib = 0; ib < nbs; ib++ ) {
162 if( printIndent ) std::cout << a_indent;
163 printIndent = false;
164 std::cout << LUPI::Misc::argumentsToString( "%16.8e", m_boundaries[ib] );
165 if( ( ( ib + 1 ) % a_valuesPerLine ) == 0 ) {
166 std::cout << std::endl;
167 printIndent = true;
168 }
169 }
170 if( nbs % a_valuesPerLine ) std::cout << std::endl;
171}
172
173/*! \class Groups_from_bdfls
174 * Specifies the data for a specified Legendre order (see class Flux).
175 */
176
177/* *********************************************************************************************************//**
178 * Reads in multi-group data from a *bdfls* file as a list of MultiGroup instances.
179 *
180 * @param a_fileName [in] The *bdfls* file name.
181 ***********************************************************************************************************/
182
183Groups_from_bdfls::Groups_from_bdfls( std::string const &a_fileName ) {
184
185 initialize( a_fileName.c_str( ) );
186}
187
188/* *********************************************************************************************************//**
189 * Reads in multi-group data from a *bdfls* file as a list of MultiGroup instances.
190 *
191 * @param a_fileName [in] The *bdfls* file name.
192 ***********************************************************************************************************/
193
194Groups_from_bdfls::Groups_from_bdfls( char const *a_fileName ) {
195
196 initialize( a_fileName );
197}
198
199/* *********************************************************************************************************//**
200 * Used by constructors to do most of the work.
201 *
202 * @param a_fileName [in] The *bdfls* file name.
203 ***********************************************************************************************************/
204
205void Groups_from_bdfls::initialize( char const *a_fileName ) {
206
207 char buffer[132], *pEnd, cValue[16];
208 FILE *fIn = fopen( a_fileName, "r" );
209 if( fIn == nullptr ) throw Exception( "Groups_from_bdfls::initialize: Could not open bdfls file." );
210
211 while( true ) {
212 int gid( -1 );
213 if( fgets( buffer, 132, fIn ) == nullptr ) throw Exception( "Groups_from_bdfls::initialize: fgets failed for gid." );
214 if( strlen( buffer ) > 73 ) {
215 if( buffer[72] == '1' ) break;
216 }
217 gid = (int) strtol( buffer, &pEnd, 10 );
218 if( gid == -1 ) throw Exception( "Groups_from_bdfls::initialize: converting gid to long failed." );
219 std::string label( LLNL_gidToLabel( gid ) );
220
221 long numberOfBoundaries( -1 );
222 if( fgets( buffer, 132, fIn ) == nullptr ) throw Exception( "Groups_from_bdfls::initialize: fgets failed for numberOfBoundaries." );
223 numberOfBoundaries = strtol( buffer, &pEnd, 10 );
224 if( numberOfBoundaries == -1 ) throw Exception( "Groups_from_bdfls::initialize: converting gid to long failed." );
225
226 std::size_t index( 0 );
227 std::vector<double> boundaries( static_cast<std::size_t>( numberOfBoundaries ) );
228 while( numberOfBoundaries > 0 ) {
229 long i1, n1( 6 );
230 if( numberOfBoundaries < 6 ) n1 = numberOfBoundaries;
231 if( fgets( buffer, 132, fIn ) == nullptr ) throw Exception( "Groups_from_bdfls::initialize: fgets failed for boundaries." );
232 for( i1 = 0; i1 < n1; ++i1, ++index ) {
233 strncpy( cValue, &buffer[12*i1], 12 );
234 cValue[12] = 0;
235 boundaries[index] = strtod( cValue, &pEnd );
236 }
237 numberOfBoundaries -= n1;
238 }
239 m_multiGroups.push_back( MultiGroup( label, boundaries ) );
240 }
241
242 fclose( fIn );
243}
244
245/* *********************************************************************************************************//**
246 ***********************************************************************************************************/
247
251
252/* *********************************************************************************************************//**
253 * Returns the MultiGroup whose *label* is *a_label*.
254 *
255 * @param a_label [in] The *label* of the MultiGroup to return.
256 * @return Returns the MultiGroup whose *label* is *a_label*.
257 ***********************************************************************************************************/
258
259MultiGroup Groups_from_bdfls::viaLabel( std::string const &a_label ) const {
260
261 for( std::size_t ig = 0; ig < m_multiGroups.size( ); ++ig ) {
262 if( m_multiGroups[ig].label( ) == a_label ) return( m_multiGroups[ig] );
263 }
264 throw Exception( "Groups_from_bdfls::viaLabel: label not found." );
265}
266
267/* *********************************************************************************************************//**
268 * Returns the MultiGroup whose *gid* is *a_gid*.
269 *
270 * @param a_gid [in] The bdfls *gid*.
271 * @return Returns the MultiGroup whose *label* is *a_label*.
272 ***********************************************************************************************************/
273
275
276 std::string label( LLNL_gidToLabel( a_gid ) );
277
278 return( viaLabel( label ) );
279}
280
281/* *********************************************************************************************************//**
282 * Returns a list of *label*'s for all the MultiGroup's present in *this*.
283 *
284 * @return Returns the MultiGroup whose *label* is *a_label*.
285 ***********************************************************************************************************/
286
287std::vector<std::string> Groups_from_bdfls::labels( ) const {
288
289 std::size_t size = m_multiGroups.size( );
290 std::vector<std::string> _labels( size );
291
292 for( std::size_t if1 = 0; if1 < size; ++if1 ) _labels[if1] = m_multiGroups[if1].label( );
293 return( _labels );
294}
295
296/* *********************************************************************************************************//**
297 * Returns a list of *gid*'s for all the MultiGroup's present in *this*.
298 *
299 * @return The list of *gid*'s.
300 ***********************************************************************************************************/
301
302std::vector<int> Groups_from_bdfls::GIDs( ) const {
303
304 std::size_t size = m_multiGroups.size( );
305 std::vector<int> fids( size );
306 char *e;
307
308 for( std::size_t if1 = 0; if1 < size; ++if1 ) {
309 fids[if1] = (int) strtol( &(m_multiGroups[if1].label( ).c_str( )[9]), &e, 10 );
310 }
311 return( fids );
312}
313
314/* *********************************************************************************************************//**
315 * Print each MultiGroup to std::cout in *this*. Mainly for debugging.
316 *
317 * @param a_outline [in] Passed to each MultiGroup print method.
318 * @param a_valuesPerLine [in] Passed to each MultiGroup print method.
319 ***********************************************************************************************************/
320
321void Groups_from_bdfls::print( bool a_outline, unsigned int a_valuesPerLine ) const {
322
323 std::size_t ngs = m_multiGroups.size( );
324
325 std::cout << "BDFLS GROUPs: number of groups = " << ngs << std::endl;
326 for( std::size_t if1 = 0; if1 < ngs ; ++if1 ) m_multiGroups[if1].print( " ", a_outline, a_valuesPerLine );
327}
328
329}
330
331}
MultiGroup viaLabel(std::string const &a_label) const
std::vector< std::string > labels() const
Groups_from_bdfls(std::string const &a_fileName)
void print(bool a_outline=true, unsigned int a_valuesPerLine=10) const
std::vector< double > const & boundaries() const
Definition GIDI.hpp:3501
MultiGroup & operator=(MultiGroup const &a_rhs)
void print(std::string const &a_indent, bool a_outline=false, unsigned int a_valuesPerLine=10) const
void set(std::string const &a_label, std::vector< double > const &a_boundaries)
std::string const & label() const
Definition GIDI.hpp:3505
int multiGroupIndexFromEnergy(double a_energy, bool a_encloseOutOfRange) const
std::size_t size() const
Definition GIDI.hpp:3499
Definition GIDI.hpp:32
std::string LLNL_gidToLabel(int a_gid)
Definition GIDI_misc.cc:311
std::string argumentsToString(char const *a_format,...)
Definition LUPI_misc.cc:305