IAP GITLAB

Commit 80f3ed36 authored by Maximilian Reininghaus's avatar Maximilian Reininghaus 🖖

Removed bitset2 and canInteractFlag for Sibyll/Interaction

parent ed38f9de
......@@ -177,10 +177,8 @@ namespace corsika::process::sibyll {
i++;
cout << "Interaction: get interaction length for target: " << targetId << endl;
auto const [productionCrossSection, elaCrossSection] =
[[maybe_unused]] auto const [productionCrossSection, elaCrossSection] =
GetCrossSection(corsikaBeamId, targetId, ECoM);
[[maybe_unused]] auto elaCrossSectionCopy =
elaCrossSection; // ONLY TO AVOID COMPILER WARNING
cout << "Interaction: "
<< " IntLength: sibyll return (mb): " << productionCrossSection / 1_mb
......@@ -188,8 +186,8 @@ namespace corsika::process::sibyll {
weightedProdCrossSection += w[i] * productionCrossSection;
}
cout << "Interaction: "
<< "IntLength: weighted CrossSection (mb): "
<< weightedProdCrossSection / 1_mb << endl;
<< "IntLength: weighted CrossSection (mb): " << weightedProdCrossSection / 1_mb
<< endl;
// calculate interaction length in medium
//#warning check interaction length units
......
......@@ -14,8 +14,6 @@
#include <corsika/particles/ParticleProperties.h>
#include <bitset2/bitset2.hpp>
#include <map>
namespace corsika::process::sibyll {
......@@ -25,14 +23,6 @@ namespace corsika::process::sibyll {
#include <corsika/process/sibyll/Generated.inc>
bool constexpr KnownBySibyll(corsika::particles::Code pCode) {
return isKnown[static_cast<corsika::particles::CodeIntType>(pCode)];
}
bool constexpr CanInteract(corsika::particles::Code pCode) {
return canInteract[static_cast<corsika::particles::CodeIntType>(pCode)];
}
SibyllCode constexpr ConvertToSibyll(corsika::particles::Code pCode) {
return static_cast<SibyllCode>(
corsika2sibyll[static_cast<corsika::particles::CodeIntType>(pCode)]);
......@@ -50,6 +40,10 @@ namespace corsika::process::sibyll {
return corsika2sibyllXStype[static_cast<corsika::particles::CodeIntType>(pCode)];
}
bool constexpr CanInteract(corsika::particles::Code pCode) {
return GetSibyllXSCode(pCode) != 0;
}
} // namespace corsika::process::sibyll
#endif
#!/usr/bin/env python3
# (c) Copyright 2018 CORSIKA Project, corsika-project@lists.kit.edu
# (c) Copyright 2018-2019 CORSIKA Project, corsika-project@lists.kit.edu
#
# See file AUTHORS for a list of contributors.
#
......@@ -31,7 +31,6 @@ def read_sibyll_codes(filename, particle_db):
identifier, sib_code, canInteractFlag, xsType = line.split()
try:
particle_db[identifier]["sibyll_code"] = int(sib_code)
particle_db[identifier]["sibyll_canInteract"] = int(canInteractFlag)
particle_db[identifier]["sibyll_xsType"] = int(xsType)
except KeyError as e:
raise Exception("Identifier '{:s}' not found in particle_db".format(identifier))
......@@ -101,54 +100,6 @@ def generate_sibyll2corsika(particle_db) :
string += "};\n"
return string
# generates the bitset for the flag whether Sibyll knows the particle
def generate_known_particle(particle_db):
num_particles = len(particle_db)
num_bytes = num_particles // 32 + 1
string = "Bitset2::bitset2<{:d}> constexpr isKnown{{ std::array<uint32_t, {:d}>{{{{\n".format(num_particles, num_bytes)
numeric = 0
for identifier, pData in reversed(particle_db.items()):
handledBySibyll = int("sibyll_code" in pData) & 0x1
numeric = (numeric << 1) | handledBySibyll
while numeric != 0:
low = numeric & 0xFFFFFFFF
numeric = numeric >> 32
string += " 0x{:0x},\n".format(low)
string += "}}};\n"
return string
# generates the bitset for the flag whether Sibyll can use particle as projectile
def generate_interacting_particle(particle_db):
num_particles = len(particle_db)
num_bytes = num_particles // 32 + 1
string = "Bitset2::bitset2<{:d}> constexpr canInteract{{ std::array<uint32_t, {:d}>{{{{\n".format(num_particles, num_bytes)
#string = "std::array<bool, {:d}> constexpr corsika2sibyll = {{\n".format(num_particles)
numeric = 0
for identifier, pData in reversed(particle_db.items()):
can = 0
if 'sibyll_canInteract' in pData:
if pData['sibyll_canInteract'] > 0:
can = 0x1
numeric = (numeric << 1) | can
while numeric != 0:
low = numeric & 0xFFFFFFFF
numeric = numeric >> 32
string += " 0x{:0x},\n".format(low)
string += "}}};\n"
return string
if __name__ == "__main__":
if len(sys.argv) != 3:
print("usage: {:s} <particle_db.pkl> <sibyll_codes.dat>".format(sys.argv[0]), file=sys.stderr)
......@@ -163,7 +114,5 @@ if __name__ == "__main__":
print("// this file is automatically generated\n// edit at your own risk!\n", file=f)
print(generate_sibyll_enum(particle_db), file=f)
print(generate_corsika2sibyll(particle_db), file=f)
print(generate_known_particle(particle_db), file=f)
print(generate_sibyll2corsika(particle_db), file=f)
print(generate_interacting_particle(particle_db), file=f)
print(generate_corsika2sibyll_xsType(particle_db), file=f)
......@@ -30,16 +30,16 @@ KPlus 9 1 3
KMinus 10 1 3
KStarPlus 28 0 0
KStarMinus 29 0 0
DPlus 59 1 0
DMinus 60 1 0
DPlus 59 1 3
DMinus 60 1 3
DStarPlus 78 0 0
DStarMinus 79 0 0
D0 71 1 0
D0Bar 72 1 0
D0 71 1 3
D0Bar 72 1 3
DStar0 80 0 0
DStar0Bar 81 0 0
DsPlus 74 1 0
DsMinus 75 1 0
DsPlus 74 1 3
DsMinus 75 1 3
DStarSPlus 76 0 0
DStarSMinus 77 0 0
EtaC 73 0 0
......
......@@ -41,12 +41,6 @@ TEST_CASE("Sibyll", "[processes]") {
REQUIRE(process::sibyll::ConvertToSibyllRaw(particles::Proton::GetCode()) == 13);
}
SECTION("KnownBySibyll") {
REQUIRE(process::sibyll::KnownBySibyll(particles::Electron::GetCode()));
REQUIRE_FALSE(process::sibyll::KnownBySibyll(particles::XiPrimeC0::GetCode()));
}
SECTION("canInteractInSibyll") {
REQUIRE(process::sibyll::CanInteract(particles::Proton::GetCode()));
......
......@@ -9,4 +9,3 @@ target_include_directories (CORSIKAthirdparty SYSTEM
install (DIRECTORY phys DESTINATION include/ThirdParty/)
install (DIRECTORY catch2 DESTINATION include/ThirdParty/)
install (DIRECTORY bitset2 DESTINATION include/ThirdParty/)
Boost Software License - Version 1.0 - August 17th, 2003
Permission is hereby granted, free of charge, to any person or organization
obtaining a copy of the software and accompanying documentation covered by
this license (the "Software") to use, reproduce, display, distribute,
execute, and transmit the Software, and to prepare derivative works of the
Software, and to permit third-parties to whom the Software is furnished to
do so, all subject to the following:
The copyright notices in the Software and this entire statement, including
the above license grant, this restriction and the following disclaimer,
must be included in all copies of the Software, in whole or in part, and
all derivative works of the Software, unless such copies or derivative
works are solely in the form of machine-executable object code generated by
a source language processor.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT
SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE
FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE,
ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
DEALINGS IN THE SOFTWARE.
# bitset2: bitset improved
|Note|
|----|
|This version of bitset2 is for C++17. For C++14 checkout the corresponding [branch](https://github.com/ClaasBontus/bitset2/tree/cpp14).|
Bitset2::bitset2 is a header only library. It provides the same functionality as [std::bitset](http://en.cppreference.com/w/cpp/utility/bitset) with the
following extentions/changes.
Focus was set on having as many functions
implemented as [constexpr](http://en.cppreference.com/w/cpp/language/constexpr)
as possible. Moreover a second template parameter (with appropriate default)
allows control of the underlying data structure (see below).
* Copy and move constructors are specified constexpr.
* Additional constexpr constructor `bitset2( std::array<T,N> const & )`, where `T` needs not necessarily be equal to `base_t`. `T` has to be an unsigned integral type.
* Conversion from and to `std::bitset`.
* Operators implemented as constexpr are `~`, `==`, `!=`, `|`, `&`, `^`, `<<` (shift left), `>>` (shift right), `[]` (bit access).
* Non-const operators implemented as constexpr are `<<=`, `>>=`, `|=`, `&=`, `^=`
* Functions implemented as constexpr are `test`, `none`, `any`, `all`, `count`, `to_ulong`, `to_ullong`.
* Non-const functions implemented as constexpr are `set`, `reset`, `flip`.
* Additional constexpr operator `+` for adding two bitset2 objects.
* Additional constexpr operators `++`, `--`, `+=`.
* Additional constexpr operators `<`, `>`, `<=`, `>=`.
* Additional constexpr functions `rotate_left` and `rotate_right` for binary rotations.
* Additional constexpr member functions `rotate_left` and `rotate_right`.
* Additional member function `to_hex_string()` (see below).
* Additional constexpr member function `test_set( size_t bit, bool value= true )`, which sets or clears the specified bit and returns its previous state. Throws `out_of_range` if bit >= N.
* Additional constexpr function `difference`, which computes the set difference (`bs1 & ~bs2`) of two bitset2 objects.
* Additional constexpr member function `difference`.
* Additional constexpr member functions `find_first()` and `find_next(size_t)` returning the index of the first (next) bit set. Returning npos if all (remaining) bits are false.
* Additional constexpr function `complement2(bs)` computing the [two's complement](https://en.wikipedia.org/wiki/Two%27s_complement) (~bs +1).
* Additional constexpr member function `complement2`.
* Additional constexpr function `reverse`, which returns argument with bits reversed.
* Additional constexpr member function `reverse`.
* Additional constexpr function `convert_to<n>` for converting an *m*-bit bitset2 into an *n*-bit bitset2.
* Additional constexpr function `convert_to<n,T>` for converting an *m*-bit bitset2 into an *n*-bit bitset2 with `base_t=T`.
* Constexpr member function `data()` gives read access to the underlying `array<base_t,N>`. Here element with index zero is the least significant word.
* Additional constexpr functions `zip_fold_and` and `zip_fold_or`. See below for details.
## Examples
```.cpp
#include <iostream>
#include <array>
#include <cassert>
#include "bitset2.hpp"
template<size_t n_bits>
using BS2= Bitset2::bitset2<n_bits>;
int main()
{
using bs_128= BS2<128>;
using base_t_128= bs_128::base_t;
constexpr std::array<base_t_128,2>
ar1{{ ~(base_t_128(0)), base_t_128(0xFEDCBA) }};
constexpr bs_128 b1{ ar1 };
constexpr auto b1_add= b1 + b1;
constexpr auto b1_shft= b1 << 1; // binary shift
static_assert( b1_add == b1_shft, "" );
std::cout << b1.to_hex_string() // 0000000000fedcbaffffffffffffffff
<< "\n"
<< b1_add.to_hex_string() // 0000000001fdb975fffffffffffffffe
<< "\n";
BS2<12> b2;
for( size_t c= 0; c < 12; c += 2 ) b2[c]= true;
auto b3= ~b2;
std::cout << b2 << "\n"; // 010101010101
std::cout << b2.flip() << "\n"; // 101010101010
assert( b2 == b3 );
BS2<7> const b4{ "1110000" };
auto const b5= Bitset2::rotate_left( b4, 3 );
std::cout << b4 << "\n" // 1110000
<< b5 << "\n"; // 0000111
BS2<7> b6{ "1010010" };
b6.reverse();
std::cout << b6 << "\n"; // 0100101
}
```
The following example illustrates how
[non-const constexpr](https://stackoverflow.com/q/43592862/3876684)
operators and functions are useful for writing constexpr functions.
It converts between [gray](https://en.wikipedia.org/wiki/Gray_code)
and binary coding.
```.cpp
template<size_t N,class T>
constexpr
Bitset2::bitset2<N,T>
binary_to_gray( Bitset2::bitset2<N,T> const &bs )
{ return bs ^ (bs >> 1); }
template<size_t N,class T>
constexpr
Bitset2::bitset2<N,T>
gray_to_binary( Bitset2::bitset2<N,T> bs )
{
Bitset2::bitset2<N,T> mask= bs >> 1;
for( ; !mask.none(); mask >>= 1 ) bs ^= mask;
return bs;
} // gray_to_binary
int main()
{
using ULLONG= unsigned long long;
constexpr std::array<ULLONG,2> arr_01a{{ 0xFEFDFCFBFAF9F8F7ull, 1ull }};
constexpr Bitset2::bitset2<129> bs_01a{ arr_01a };
constexpr auto gray_01a= binary_to_gray( bs_01a );
constexpr auto bin_01a= gray_to_binary( gray_01a );
static_assert( bs_01a == bin_01a, "" );
}
```
## Template parameters and underlying data type
`bitset2` is declared as
```.cpp
template< size_t N, class T >
class bitset2;
```
`N` is the number of bits and `T` has to be an unsigned
[integral type](http://en.cppreference.com/w/cpp/types/is_integral). Data
represented by `bitset2` objects are stored in elements of type
`std::array<T,n_array>`.
`T` defaults
to `uint8_t`, `uint16_t`, or `uint32_t` if `N` bits fit into these integers
(and the type is supported by the system).
`T` defaults to `unsigned long long` otherwise. The following aliases and
constants are public within `bitset2`:
* `using base_t= T;`
* `size_t n_array;` Number of words in underlying array
* `using array_t= std::array<T,n_array>;` Underlying data type
## to_hex_string
Function `to_hex_string` takes - as an optional argument - an element of type
`hex_params`, which is defined as
```.cpp
template< class CharT = char,
class Traits = std::char_traits<CharT>,
class Allocator = std::allocator<CharT> >
struct hex_params
{
using str_t= std::basic_string<CharT,Traits,Allocator>;
CharT zeroCh= CharT( '0' );
CharT aCh= CharT( 'a' );
bool leadingZeroes= true;
bool nonEmpty= true;
str_t prefix;
};
```
It allows fine tuning the outcome of the function. In the following
examples the output is shown in the comments.
```.cpp
bitset2<16> b16_a( "0000101000011111" );
bitset2<16> b16_b;
std::cout
<< b16_a.to_hex_string() << '\n' // 0a1f
<< b16_a.to_hex_string( hex_params<>{'0', 'A', false, true, "0x"}) // 0xA1F
<< '\n'
<< b16_b.to_hex_string() << '\n' // 0000
<< b16_b.to_hex_string( hex_params<>{'0', 'a', false, false, "0X"}) // 0X
<< '\n';
```
## zip\_fold\_&ast;
Functions `zip_fold_and(bs1,bs2,f)` and `zip_fold_or(bs1,bs2,f)` expect two
variables of type `bitset2` and a functional object `f`.
The latter must accept two variables of type `base_t` and return a `bool`.
`zip_fold_*` are mapped over the underlying
`std::array<base_t,n>`s. They will
[short-circuit](http://en.cppreference.com/w/cpp/language/operator_logical)
if possible, which can result in performance advantages.
`zip_fold_and` returns `true` if `f`
returns `true` for each pair of `base_t`s, while `zip_fold_or` returns `true`
if `f` returns `true` for at least one pair of `base_t`s.
In other words `zip_fold_and` and `zip_fold_or` are similar to
[`std::inner_product(...,BinOp1 op1,BinOp2 op2)`](http://en.cppreference.com/w/cpp/algorithm/inner_product)
with `op1` set to `&&` and `||`, resp.
For instance `is_subset_of` as proposed in [p0125r0](http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2015/p0125r0.html)
can be implemented as follows.
```.cpp
template<size_t N,class T>
constexpr
bool
is_subset_of( Bitset2::bitset2<N,T> const &bs1,
Bitset2::bitset2<N,T> const &bs2 ) noexcept
{
using base_t= T;
return Bitset2::zip_fold_and( bs1, bs2,
[]( base_t v1, base_t v2 ) noexcept
// Any bit unset in v2 must not be set in v1
{ return (v1 & ~v2) == 0; } );
}
constexpr Bitset2::bitset2<7> b7_a( 0b1000101ull );
constexpr Bitset2::bitset2<7> b7_b( 0b1010101ull );
static_assert( is_subset_of( b7_a, b7_b), "" );
```
Similarly an `unequal` function can be defined as
```.cpp
template<size_t N,class T>
bool
unequal( Bitset2::bitset2<N,T> const &bs1, Bitset2::bitset2<N,T> const &bs2 )
{
using base_t= T;
return Bitset2::zip_fold_or( bs1, bs2,
[]( base_t v1, base_t v2 ) noexcept
{ return v1 != v2; } );
}
```
## Trivia
The following code shows a counter based on a 128-bit integer. If the
counter gets incremented once at each nanosecond, you have to wait for
overflow for *only* [1.078 * 10<sup>22</sup> years](http://www.wolframalpha.com/input/?i=2%5E128+nanoseconds).
```.cpp
Bitset2::bitset2<128> c;
for( ;; ++c ) {}
```
## Caveats
* bitset2 requires a C++17 compliant compiler.
* Tested with gcc 7 and clang 5.
This diff is collapsed.
// BITSET2
//
// Copyright Claas Bontus
//
// Use, modification and distribution is subject to the
// Boost Software License, Version 1.0. (See accompanying
// file LICENSE.txt or copy at
// http://www.boost.org/LICENSE_1_0.txt)
//
// Project home: https://github.com/ClaasBontus/bitset2
//
#ifndef BITSET2_ARRAY2ARRAY_CB_HPP
#define BITSET2_ARRAY2ARRAY_CB_HPP
#include "h_types.hpp"
namespace Bitset2
{
namespace detail
{
/// \brief Convert std::array<Tsrc,src_n> to std::array<Ttrgt,trgt_n>
template<size_t trgt_n, size_t src_n, class Ttrgt, class Tsrc>
struct array2array
{
using h_t_trgt= h_types<Ttrgt>;
using h_t_src= h_types<Tsrc>;
using trgt_array_t= typename h_t_trgt::template array_t<trgt_n>;
using src_array_t= typename h_t_src:: template array_t<src_n>;
enum : size_t
{ trgt_base_n_bits= h_t_trgt::base_t_n_bits
, src_base_n_bits= h_t_src:: base_t_n_bits
};
enum : bool
{ small_to_large= ( src_base_n_bits < trgt_base_n_bits ) };
enum : size_t
{ ts_div= small_to_large ? trgt_base_n_bits / src_base_n_bits
: src_base_n_bits / trgt_base_n_bits };
enum : Tsrc
{ h_all_set= Tsrc( Ttrgt(~Ttrgt(0)) ) };
/// Applies pttrn to most significant entry in result
constexpr
trgt_array_t
operator()( Ttrgt pttrn, src_array_t const &src ) const noexcept
{
static_assert( ( small_to_large && trgt_base_n_bits % src_base_n_bits == 0)
|| (!small_to_large && src_base_n_bits % trgt_base_n_bits == 0),
"Conversion between arrays of these types not supported" );
return small_to_large
? conv_small_to_large( pttrn, src, std::make_index_sequence<trgt_n-1>() )
: conv_large_to_small( pttrn, src, std::make_index_sequence<trgt_n-1>() );
}
template<size_t ... S1>
constexpr
trgt_array_t
conv_small_to_large( Ttrgt pttrn,
src_array_t const &src,
std::index_sequence<S1...> ) const noexcept
{
return {{ get_from_smaller( S1, src, S1 * ts_div )...,
Ttrgt( get_from_smaller( trgt_n-1, src, (trgt_n-1) * ts_div )
& pttrn ) }};
}
template<size_t ... S1>
constexpr
trgt_array_t
conv_large_to_small( Ttrgt pttrn,
src_array_t const &src,
std::index_sequence<S1...> ) const noexcept
{
return
{{ get_from_larger( S1 / ts_div, S1 % ts_div, src )...,
Ttrgt( get_from_larger((trgt_n-1) / ts_div, (trgt_n-1) % ts_div, src)
& pttrn ) }};
}
constexpr
Ttrgt
get_from_smaller( size_t trgt_idx,
src_array_t const &src,
size_t src_idx,
size_t src_ct= 0,
Ttrgt so_far= Ttrgt(0) ) const noexcept
{
return ( src_ct >= ts_div || src_idx >= src_n )
? so_far
: get_from_smaller( trgt_idx, src, src_idx + 1, src_ct + 1,
so_far | Ttrgt( Ttrgt(src[src_idx])
<< (src_base_n_bits * src_ct) ) );
}
constexpr
Ttrgt
get_from_larger( size_t div_val,
size_t mod_val,
src_array_t const &src ) const noexcept
{
return ( div_val >= src_n ) ? Ttrgt(0)
: Ttrgt(Tsrc( src[div_val] >> (mod_val*trgt_base_n_bits) ) & h_all_set );
}
}; // struct array2array
} // namespace detail
} // namespace Bitset2
#endif // BITSET2_ARRAY2ARRAY_CB_HPP
// BITSET2
//
// Copyright Claas Bontus
//
// Use, modification and distribution is subject to the
// Boost Software License, Version 1.0. (See accompanying
// file LICENSE.txt or copy at
// http://www.boost.org/LICENSE_1_0.txt)
//
// Project home: https://github.com/ClaasBontus/bitset2
//
#ifndef BITSET2_ARRAY2U_LONG_T_CB_HPP
#define BITSET2_ARRAY2U_LONG_T_CB_HPP
#include "bit_chars.hpp"
namespace Bitset2
{
namespace detail
{
/// \brief Takes a std::array 'arr' and returns a variable 'x' of type Tlong.
/// Bits in 'x' are set if corresponding bits in 'arr' are set.
template<size_t N, class T, class Tlong>
struct array2u_long_t
{
using base_t= T;
using b_c= bit_chars<N,T>;
enum : size_t
{ n_bits= N
, base_t_n_bits= b_c::base_t_n_bits
, long_t_n_bits= sizeof( Tlong ) * CHAR_BIT
, n_array= b_c::n_array
, div_val= long_t_n_bits / base_t_n_bits
, mod_val= long_t_n_bits % base_t_n_bits
, use_vals= ce_min( div_val + (mod_val!=0), n_array )
, bit_diff= mod_val == 0 ? 0 : ( base_t_n_bits - mod_val )
};
enum : base_t
{ allset= base_t( ~base_t(0) )
, h_pttrn= use_vals < n_array ? allset : ce_right_shift( allset, bit_diff )
, i_pttrn= base_t( ~h_pttrn )
};
using array_t= typename h_types<T>::template array_t<n_array>;
constexpr
Tlong
operator()( array_t const & arr ) const noexcept
{
return
base_t_n_bits >= long_t_n_bits ? Tlong( arr[0] )
: combine( Tlong(0), arr, 0 );
}
/// \brief Returns true if arr cannot be converted to Tlong.
constexpr
bool
check_overflow( array_t const & arr ) const noexcept
{ return N <= long_t_n_bits ? false : check_impl( arr, use_vals - 1 ); }
constexpr
Tlong
combine( Tlong v, array_t const & arr, size_t idx ) const noexcept
{
return ( idx >= use_vals ) ? v