Import Geant4 10.5.0.beta source tree

This commit is contained in:
Gabriele Cosmo
2018-06-29 10:58:11 +02:00
parent fe81a77428
commit 6aa23be517
1581 changed files with 124288 additions and 83758 deletions
@@ -0,0 +1,344 @@
// ********************************************************************
// * License and Disclaimer *
// * *
// * The Geant4 software is copyright of the Copyright Holders of *
// * the Geant4 Collaboration. It is provided under the terms and *
// * conditions of the Geant4 Software License, included in the file *
// * LICENSE and available at http://cern.ch/geant4/license . These *
// * include a list of copyright holders. *
// * *
// * Neither the authors of this software system, nor their employing *
// * institutes,nor the agencies providing financial support for this *
// * work make any representation or warranty, express or implied, *
// * regarding this software system or assume any liability for its *
// * use. Please see the license in the file LICENSE and URL above *
// * for the full disclaimer and the limitation of liability. *
// * *
// * This code implementation is the result of the scientific and *
// * technical work of the GEANT4 collaboration. *
// * By using, copying, modifying or distributing the software (or *
// * any work based on the software) you agree to acknowledge its *
// * use in resulting scientific publications, and indicate your *
// * acceptance of all terms of the Geant4 Software license. *
// ********************************************************************
//
//
// $Id: $
//
// G4BulirschStoer class implementation
// Based on bulirsch_stoer.hpp from boost
//
// Author: Dmitry Sorokin - GSoC 2016
//
///////////////////////////////////////////////////////////////////////////////
#include "G4BulirschStoer.hh"
#include "G4FieldUtils.hh"
namespace
{
const G4double STEPFAC1 = 0.65;
const G4double STEPFAC2 = 0.94;
const G4double STEPFAC3 = 0.02;
const G4double STEPFAC4 = 4.0;
const G4double KFAC1 = 0.8;
const G4double KFAC2 = 0.9;
} // namespace
G4BulirschStoer::G4BulirschStoer(G4EquationOfMotion* equation,
G4int nvar, G4double eps_rel, G4double max_dt)
: fnvar(nvar), m_eps_rel(eps_rel), m_midpoint(equation,nvar),
m_last_step_rejected(false), m_first(true), m_dt_last(0.0), m_max_dt(max_dt)
{
/* initialize sequence of stage numbers and work */
for(G4int i = 0; i < m_k_max + 1; ++i)
{
m_interval_sequence[i] = 2 * (i + 1);
if (i == 0)
{
m_cost[i] = m_interval_sequence[i];
}
else
{
m_cost[i] = m_cost[i-1] + m_interval_sequence[i];
}
for(G4int k = 0; k < i; ++k)
{
const G4double r = static_cast<G4double>(m_interval_sequence[i])
/ static_cast<G4double>(m_interval_sequence[k]);
m_coeff[i][k] = 1.0 / (r * r - 1.0); // coefficients for extrapolation
}
// crude estimate of optimal order
m_current_k_opt = 4;
// no calculation because log10 might not exist for value_type!
//const G4double logfact = -log10(std::max(eps_rel, 1.0e-12)) * 0.6 + 0.5;
//m_current_k_opt = std::max(1.,
// std::min(static_cast<G4double>(m_k_max-1), logfact));
}
}
G4BulirschStoer::step_result
G4BulirschStoer::try_step( const G4double in[], const G4double dxdt[],
G4double& t, G4double out[], G4double& dt)
{
if(m_max_dt < dt)
{
// given step size is bigger then max_dt set limit and return fail
//
dt = m_max_dt;
return step_result::fail;
}
if (dt != m_dt_last)
{
reset(); // step size changed from outside -> reset
}
G4bool reject = true;
G4double new_h = dt;
/* m_current_k_opt is the estimated current optimal stage number */
for(G4int k = 0; k <= m_current_k_opt+1; ++k)
{
// the stage counts are stored in m_interval_sequence
//
m_midpoint.SetSteps(m_interval_sequence[k]);
if(k == 0)
{
m_midpoint.DoStep(in, dxdt, out, dt);
/* the first step, nothing more to do */
}
else
{
m_midpoint.DoStep(in, dxdt, m_table[k-1], dt);
extrapolate(k, out);
// get error estimate
for (G4int i = 0; i < fnvar; ++i)
{
m_err[i] = out[i] - m_table[0][i];
}
const G4double error =
field_utils::relativeError(out, m_err, dt, m_eps_rel);
h_opt[k] = calc_h_opt(dt, error, k);
work[k] = static_cast<G4double>(m_cost[k]) / h_opt[k];
if( (k == m_current_k_opt-1) || m_first) // convergence before k_opt ?
{
if(error < 1.0)
{
// convergence
reject = false;
if( (work[k] < KFAC2 * work[k-1]) || (m_current_k_opt <= 2) )
{
// leave order as is (except we were in first round)
m_current_k_opt = std::min(m_k_max - 1 , std::max(2 , k + 1));
new_h = h_opt[k];
new_h *= static_cast<G4double>(m_cost[k + 1])
/ static_cast<G4double>(m_cost[k]);
}
else
{
m_current_k_opt = std::min(m_k_max - 1, std::max(2, k));
new_h = h_opt[k];
}
break;
}
else if(should_reject(error , k) && !m_first)
{
reject = true;
new_h = h_opt[k];
break;
}
}
if(k == m_current_k_opt) // convergence at k_opt ?
{
if(error < 1.0)
{
// convergence
reject = false;
if(work[k-1] < KFAC2 * work[k])
{
m_current_k_opt = std::max( 2 , m_current_k_opt-1 );
new_h = h_opt[m_current_k_opt];
}
else if( (work[k] < KFAC2 * work[k-1]) && !m_last_step_rejected )
{
m_current_k_opt = std::min(m_k_max - 1, m_current_k_opt + 1);
new_h = h_opt[k];
new_h *= static_cast<G4double>(m_cost[m_current_k_opt])
/ static_cast<G4double>(m_cost[k]);
}
else
{
new_h = h_opt[m_current_k_opt];
}
break;
}
else if(should_reject(error, k))
{
reject = true;
new_h = h_opt[m_current_k_opt];
break;
}
}
if(k == m_current_k_opt + 1) // convergence at k_opt+1 ?
{
if(error < 1.0) // convergence
{
reject = false;
if(work[k-2] < KFAC2 * work[k-1])
{
m_current_k_opt = std::max(2, m_current_k_opt - 1);
}
if((work[k] < KFAC2 * work[m_current_k_opt]) && !m_last_step_rejected)
{
m_current_k_opt = std::min(m_k_max - 1 , k);
}
new_h = h_opt[m_current_k_opt];
}
else
{
reject = true;
new_h = h_opt[m_current_k_opt];
}
break;
}
}
}
if(!reject)
{
t += dt;
}
if(!m_last_step_rejected || new_h < dt)
{
// limit step size
new_h = std::min(m_max_dt, new_h);
m_dt_last = new_h;
dt = new_h;
}
m_last_step_rejected = reject;
m_first = false;
return reject ? step_result::fail : step_result::success;
}
void G4BulirschStoer::reset()
{
m_first = true;
m_last_step_rejected = false;
}
void G4BulirschStoer::extrapolate(size_t k , G4double xest[])
{
/* polynomial extrapolation, see http://www.nr.com/webnotes/nr3web21.pdf
* uses the obtained intermediate results to extrapolate to dt->0 */
for(G4int j = k - 1 ; j > 0; --j)
{
for (G4int i = 0; i < fnvar; ++i)
{
m_table[j-1][i] = m_table[j][i] * (1. + m_coeff[k][j])
- m_table[j-1][i] * m_coeff[k][j];
}
}
for (G4int i = 0; i < fnvar; ++i)
{
xest[i] = m_table[0][i] * (1. + m_coeff[k][0]) - xest[i] * m_coeff[k][0];
}
}
G4double
G4BulirschStoer::calc_h_opt(G4double h , G4double error , size_t k) const
{
/* calculates the optimal step size for a given error and stage number */
G4double expo = 1.0 / (2 * k + 1);
G4double facmin = std::pow(STEPFAC3, expo);
G4double fac;
if (error == 0.0)
{
fac = 1.0 / facmin;
}
else
{
fac = STEPFAC2 / std::pow(error / STEPFAC1 , expo);
fac = std::max(facmin / STEPFAC4, std::min(1.0 / facmin, fac));
}
return h * fac;
}
//why is not used!!??
G4bool G4BulirschStoer::set_k_opt(size_t k, G4double& dt)
{
/* calculates the optimal stage number */
if(k == 1)
{
m_current_k_opt = 2;
return true;
}
if( (work[k-1] < KFAC1 * work[k]) || (k == m_k_max) ) // order decrease
{
m_current_k_opt = k - 1;
dt = h_opt[ m_current_k_opt ];
return true;
}
else if( (work[k] < KFAC2 * work[k-1])
|| m_last_step_rejected || (k == m_k_max-1) )
{ // same order - also do this if last step got rejected
m_current_k_opt = k;
dt = h_opt[m_current_k_opt];
return true;
}
else { // order increase - only if last step was not rejected
m_current_k_opt = k + 1;
dt = h_opt[m_current_k_opt - 1] * m_cost[m_current_k_opt]
/ m_cost[m_current_k_opt - 1];
return true;
}
}
G4bool G4BulirschStoer::in_convergence_window(G4int k) const
{
if( (k == m_current_k_opt - 1) && !m_last_step_rejected )
{
return true; // decrease stepsize only if last step was not rejected
}
return (k == m_current_k_opt) || (k == m_current_k_opt + 1);
}
G4bool G4BulirschStoer::should_reject(G4double error, G4int k) const
{
if(k == m_current_k_opt - 1)
{
const G4double d = G4double(m_interval_sequence[m_current_k_opt]
* m_interval_sequence[m_current_k_opt+1])
/ G4double(m_interval_sequence[0]
* m_interval_sequence[0]);
// step will fail, criterion 17.3.17 in NR
return error > d * d;
}
else if(k == m_current_k_opt)
{
const G4double d = G4double(m_interval_sequence[m_current_k_opt])
/ G4double(m_interval_sequence[0]);
return error > d * d;
}
else
{
return error > 1.0;
}
}
@@ -24,7 +24,7 @@
// ********************************************************************
//
//
// $Id: G4ChordFinder.cc 107508 2017-11-20 08:23:14Z gcosmo $
// $Id: G4ChordFinder.cc 110753 2018-06-12 15:44:03Z gcosmo $
//
//
// 25.02.97 - John Apostolakis - Design and implementation
@@ -76,8 +76,6 @@ G4ChordFinder::G4ChordFinder(G4VIntegrationDriver* pIntegrationDriver)
SetFractions_Last_Next( fFractionLast, fFractionNextEstimate);
// check the values and set the other parameters
// G4cout << "G4ChordFinder 1st Constructor called - (driver given). " << G4endl;
}
@@ -86,7 +84,6 @@ G4ChordFinder::G4ChordFinder(G4VIntegrationDriver* pIntegrationDriver)
G4ChordFinder::G4ChordFinder( G4MagneticField* theMagField,
G4double stepMinimum,
G4MagIntegratorStepper* pItsStepper, // nullptr is default
// G4bool useHigherEfficiencyStepper, // false by default
G4bool useFSALstepper ) // false by default
: fDefaultDeltaChord( 0.25 * mm ), // Constants
fDeltaChord( fDefaultDeltaChord ), // Parameters
@@ -102,9 +99,6 @@ G4ChordFinder::G4ChordFinder( G4MagneticField* theMagField,
using NewFsalStepperType = G4RK547FEq1; // or 2 or 3
const char* NewFSALStepperName = "G4RK574FEq1> FSAL 4th/5th order 7-stage 'Equilibrium-type' #1.";
// using OldFsalStepperType = G4FSALBogackiShampine45;
// const char* OldFSALStepperName = "FSAL BogackiShampine 45 (Embedded 5th/4th Order, 7-stage)";
// = G4FSALDormandPrince745; // = "FSAL Dormand Prince 745 stepper";
using RegularStepperType =
G4DormandPrince745; // Famous DOPRI5 (MatLab) 5th order embedded method. High efficiency.
// G4ClassicalRK4; // The old default
@@ -117,20 +111,16 @@ G4ChordFinder::G4ChordFinder( G4MagneticField* theMagField,
// Configurable
G4bool forceFSALstepper= false; // Choice - true to enable !!
// G4bool useNewFSALtype= true;
// G4bool forceHigherEffiencyStepper = false;
G4bool report = false; // Report type of stepper used
bool recallFSALflag = useFSALstepper;
G4bool recallFSALflag = useFSALstepper;
useFSALstepper = forceFSALstepper || useFSALstepper;
if( report ) {
#ifdef G4DEBUG_FIELD
G4cout << "G4ChordFinder 2nd Constructor called. " << G4endl;
G4cout << " Parameters: " << G4endl;
G4cout << " useFSAL stepper= " << useFSALstepper
<< " (request = " << recallFSALflag
<< " force FSAL = " << forceFSALstepper << " )" << G4endl;
}
#endif
// useHigherStepper = forceHigherEffiencyStepper || useHigherStepper;
@@ -149,7 +139,6 @@ G4ChordFinder::G4ChordFinder( G4MagneticField* theMagField,
G4bool errorInStepperCreation = false;
std::ostringstream message; // In case of failure, load with description !
message << "G4ChordFinder 2nd Constructor called. " << G4endl;
if( pItsStepper != nullptr )
{
@@ -169,9 +158,11 @@ G4ChordFinder::G4ChordFinder( G4MagneticField* theMagField,
if( regularStepper == nullptr )
{
message << " ERROR> 'Regular' RK Stepper instantiation FAILED." << G4endl;
message << "Stepper instantiation FAILED." << G4endl;
message << "G4ChordFinder: Attempted to instantiate "
<< RegularStepperName << " type stepper " << G4endl;
G4Exception("G4ChordFinder::G4ChordFinder()",
"GeomField1001", JustWarning, message);
errorInStepperCreation = true;
}
else
@@ -186,12 +177,13 @@ G4ChordFinder::G4ChordFinder( G4MagneticField* theMagField,
// new G4IntegrationDriver<RegularStepperType>(stepMinimum,
// ==== Create the driver which knows the class type
if( (fIntgrDriver==nullptr) || report ) {
message << "G4ChordFinder: Using G4IntegrationDriver with "
if( fIntgrDriver==nullptr)
{
message << "Using G4IntegrationDriver with "
<< RegularStepperName << " type stepper " << G4endl;
}
if(fIntgrDriver==nullptr) {
message << " ERROR> 'Regular' RK Driver instantiation FAILED." << G4endl;
message << "Driver instantiation FAILED." << G4endl;
G4Exception("G4ChordFinder::G4ChordFinder()",
"GeomField1001", JustWarning, message);
}
}
}
@@ -200,14 +192,14 @@ G4ChordFinder::G4ChordFinder( G4MagneticField* theMagField,
auto fsalStepper= new NewFsalStepperType(pEquation);
// ******************
fNewFSALStepperOwned = fsalStepper;
// delete fsalStepper;
// /*NewFsalStepperType* */ fsalStepper =nullptr; // To check the exception
if( fsalStepper == nullptr )
{
message << " ERROR> 'FSAL' RK Stepper instantiation FAILED." << G4endl;
message << "G4ChordFinder: Attempted to instantiate "
message << "Stepper instantiation FAILED." << G4endl;
message << "Attempted to instantiate "
<< NewFSALStepperName << " type stepper " << G4endl;
G4Exception("G4ChordFinder::G4ChordFinder()",
"GeomField1001", JustWarning, message);
errorInStepperCreation = true;
}
else
@@ -218,12 +210,13 @@ G4ChordFinder::G4ChordFinder( G4MagneticField* theMagField,
fsalStepper->GetNumberOfVariables() );
// ==== Create the driver which knows the class type
if( (fIntgrDriver==nullptr) || report ) {
message << "G4ChordFinder: Using G4FSALIntegrationDriver with stepper type: " << G4endl
<< NewFSALStepperName << " (new-FSAL type stepper.) " << G4endl;
}
if(fIntgrDriver==nullptr) {
message << " ERROR> FSAL Integration Driver instantiation FAILED." << G4endl;
if( fIntgrDriver==nullptr )
{
message << "Using G4FSALIntegrationDriver with stepper type: "
<< NewFSALStepperName << G4endl;
message << "Integration Driver instantiation FAILED." << G4endl;
G4Exception("G4ChordFinder::G4ChordFinder()",
"GeomField1001", JustWarning, message);
}
}
}
@@ -255,27 +248,19 @@ G4ChordFinder::G4ChordFinder( G4MagneticField* theMagField,
errmsg << " Configuration: (constructor arguments) " << G4endl
<< " provided Stepper = " << pItsStepper << G4endl
<< " use FSAL stepper = " << BoolName[useFSALstepper]
// ( useFSALstepper ? "True" : "False" )
<< " (request = " << BoolName[recallFSALflag]
<< " force FSAL = " << BoolName[forceFSALstepper] << " )" << G4endl;
// << " use new FSAL stp = " << ( useNewFSALstepper ? "True" : "False" ) << G4endl;
<< " force FSAL = " << BoolName[forceFSALstepper] << " )" << G4endl;
errmsg << message.str();
errmsg << "Aborting.";
G4Exception("G4ChordFinder::G4ChordFinder() - constructor 2", "GeomField0003",
FatalException, errmsg);
}
else if ( report )
{
G4cout << message.str();
G4Exception("G4ChordFinder::G4ChordFinder() - constructor 2",
"GeomField0003", FatalException, errmsg);
}
assert( ( pItsStepper != nullptr )
|| ( fRegularStepperOwned != nullptr )
|| ( fNewFSALStepperOwned != nullptr )
// || ( fOldFSALStepperOwned != nullptr )
);
assert( fIntgrDriver != nullptr );
}
@@ -286,7 +271,6 @@ G4ChordFinder::~G4ChordFinder()
delete fEquation; // fIntgrDriver->pIntStepper->theEquation_Rhs;
delete fRegularStepperOwned;
delete fNewFSALStepperOwned;
// delete fOldFSALStepperOwned;
delete fIntgrDriver;
if( fStatsVerbose ) { PrintStatistics(); }
@@ -321,9 +305,11 @@ G4ChordFinder::SetFractions_Last_Next( G4double fractLast, G4double fractNext )
}
else
{
G4cerr << "G4ChordFinder::SetFractions_Last_Next: Invalid "
<< " fraction Last = " << fractLast
<< " must be 0 < fractionLast <= 1 " << G4endl;
std::ostringstream message;
message << "Invalid fraction Last = " << fractLast
<< "; must be 0 < fractionLast <= 1 ";
G4Exception("G4ChordFinder::SetFractions_Last_Next()",
"GeomField1001", JustWarning, message);
}
if( (fractNext > 0.0) && (fractNext <1.0) )
{
@@ -331,9 +317,11 @@ G4ChordFinder::SetFractions_Last_Next( G4double fractLast, G4double fractNext )
}
else
{
G4cerr << "G4ChordFinder:: SetFractions_Last_Next: Invalid "
<< " fraction Next = " << fractNext
<< " must be 0 < fractionNext < 1 " << G4endl;
std::ostringstream message;
message << "Invalid fraction Next = " << fractNext
<< "; must be 0 < fractionNext < 1 ";
G4Exception("G4ChordFinder::SetFractions_Last_Next()",
"GeomField1001", JustWarning, message);
}
}
@@ -352,11 +340,9 @@ G4ChordFinder::AdvanceChordLimited( G4FieldTrack& yCurrent,
G4FieldTrack yEnd( yCurrent);
G4double startCurveLen= yCurrent.GetCurveLength();
G4double nextStep;
// *************
stepPossible= FindNextChord(yCurrent, stepMax, yEnd, dyErr, epsStep,
&nextStep, latestSafetyOrigin, latestSafetyRadius
);
// *************
&nextStep, latestSafetyOrigin, latestSafetyRadius);
G4bool good_advance;
@@ -398,8 +384,6 @@ G4ChordFinder::FindNextChord( const G4FieldTrack& yStart,
{
// Returns Length of Step taken
// G4cout << ">G4ChordFinder::FindNextChord called." << G4endl;
G4FieldTrack yCurrent= yStart;
G4double stepTrial, stepForAccuracy;
G4double dydx[G4FieldTrack::ncompSVEC];
@@ -497,14 +481,6 @@ G4ChordFinder::FindNextChord( const G4FieldTrack& yStart,
*pStepForAccuracy = stepForAccuracy;
}
#ifdef TEST_CHORD_PRINT
static int dbg=0;
if( dbg )
{
G4cout << "ChordF/FindNextChord: NoTrials= " << noTrials
<< " StepForGoodChord=" << std::setw(10) << stepTrial << G4endl;
}
#endif
yEnd= yCurrent;
return stepTrial;
}
@@ -24,7 +24,7 @@
// ********************************************************************
//
//
// $Id: G4FieldManager.cc 107059 2017-11-01 14:58:16Z gcosmo $
// $Id: G4FieldManager.cc 108823 2018-03-09 11:03:44Z gcosmo $
//
// -------------------------------------------------------------------
@@ -140,10 +140,16 @@ G4FieldManager::~G4FieldManager()
void
G4FieldManager::CreateChordFinder(G4MagneticField *detectorMagField)
{
if ( fAllocatedChordFinder )
if ( fAllocatedChordFinder )
delete fChordFinder;
fChordFinder= new G4ChordFinder( detectorMagField );
fAllocatedChordFinder= true;
fAllocatedChordFinder= false;
if( detectorMagField ) {
fChordFinder= new G4ChordFinder( detectorMagField );
fAllocatedChordFinder= true;
} else {
fChordFinder = nullptr;
}
}
void G4FieldManager::InitialiseFieldChangesEnergy()
@@ -24,7 +24,7 @@
// ********************************************************************
//
//
// $Id: G4MagIntegratorDriver.cc 107059 2017-11-01 14:58:16Z gcosmo $
// $Id: G4MagIntegratorDriver.cc 110753 2018-06-12 15:44:03Z gcosmo $
//
//
//
@@ -47,21 +47,6 @@
#include "G4MagIntegratorDriver.hh"
#include "G4FieldTrack.hh"
// Stepsize can increase by no more than 5.0
// and decrease by no more than 1/10. = 0.1
//
const G4double G4MagInt_Driver::max_stepping_increase = 5.0;
const G4double G4MagInt_Driver::max_stepping_decrease = 0.1;
// The (default) maximum number of steps is Base
// divided by the order of Stepper
//
const G4int G4MagInt_Driver::fMaxStepBase = 250; // Was 5000
#ifndef G4NO_FIELD_STATISTICS
#define G4FLD_STATS 1
#endif
// ---------------------------------------------------------
// Constructor
@@ -87,6 +72,12 @@ G4MagInt_Driver::G4MagInt_Driver( G4double hminimum,
RenewStepperAndAdjust( pStepper );
fMinimumStep= hminimum;
// The (default) maximum number of steps is Base
// divided by the order of Stepper
//
fMaxStepBase = 250; // Was 5000
fMaxNoSteps = fMaxStepBase / pIntStepper->IntegratorOrder();
#ifdef G4DEBUG_FIELD
fVerboseLevel=2;
@@ -117,10 +108,6 @@ G4MagInt_Driver::~G4MagInt_Driver()
}
}
// To add much printing for debugging purposes, uncomment the following
// and set verbose level to 1 or higher value !
// #define G4DEBUG_FIELD 1
// ---------------------------------------------------------
G4bool
@@ -230,7 +217,8 @@ G4MagInt_Driver::AccurateAdvance(G4FieldTrack& y_current,
//--------------------------------------
lastStepSucceeded= (hdid == h);
#ifdef G4DEBUG_FIELD
if (dbg>2) {
if (dbg>2)
{
PrintStatus( ySubStepStart, xSubStepStart, y, x, h, nstp); // Only
}
#endif
@@ -418,7 +406,7 @@ G4MagInt_Driver::AccurateAdvance(G4FieldTrack& y_current,
#ifdef G4DEBUG_FIELD
if( dbg && no_warnings )
{
G4cerr << "G4MagIntegratorDriver exit status: no-steps " << nstp <<G4endl;
G4cerr << "G4MagIntegratorDriver exit status: no-steps " << nstp << G4endl;
PrintStatus( yEnd, x1, y, x, hstep, nstp);
}
#endif
@@ -574,11 +562,17 @@ G4MagInt_Driver::OneGoodStep( G4double y[], // InOut
// Accuracy for momentum
G4double magvel_sq= sqr(y[3]) + sqr(y[4]) + sqr(y[5]) ;
G4double sumerr_sq = sqr(yerr[3]) + sqr(yerr[4]) + sqr(yerr[5]) ;
if( magvel_sq > 0.0 ) {
if( magvel_sq > 0.0 )
{
errvel_sq = sumerr_sq / magvel_sq;
}else{
G4cerr << "** G4MagIntegrationDriver: found case of zero momentum."
<< " iteration= " << iter << " h= " << h << G4endl;
}
else
{
std::ostringstream message;
message << "Found case of zero momentum." << G4endl
<< "- iteration= " << iter << "; h= " << h;
G4Exception("G4MagInt_Driver::OneGoodStep()",
"GeomField1001", JustWarning, message);
errvel_sq = sumerr_sq;
}
errvel_sq *= inv_eps_vel_sq;
@@ -604,23 +598,18 @@ G4MagInt_Driver::OneGoodStep( G4double y[], // InOut
xnew = x + h;
if(xnew == x)
{
G4cerr << "G4MagIntegratorDriver::OneGoodStep:" << G4endl
<< " Stepsize underflow in Stepper " << G4endl ;
G4cerr << " Step's start x=" << x << " and end x= " << xnew
<< " are equal !! " << G4endl
<<" Due to step-size= " << h
<< " . Note that input step was " << htry << G4endl;
std::ostringstream message;
message << "Stepsize underflow in Stepper !" << G4endl
<< "- Step's start x=" << x << " and end x= " << xnew
<< " are equal !! " << G4endl
<< " Due to step-size= " << h
<< ". Note that input step was " << htry;
G4Exception("G4MagInt_Driver::OneGoodStep()",
"GeomField1001", JustWarning, message);
break;
}
}
#ifdef G4FLD_STATS
// Sum of squares of position error // and momentum dir (underestimated)
fSumH_lg += h;
fDyerrPos_lgTot += errpos_sq;
fDyerrVel_lgTot += errvel_sq * h * h;
#endif
// Compute size of next Step
if (errmax_sq > errcon*errcon)
{
@@ -635,7 +624,7 @@ G4MagInt_Driver::OneGoodStep( G4double y[], // InOut
for(G4int k=0;k<fNoIntegrationVariables;k++) { y[k] = ytemp[k]; }
return;
} // end of OneGoodStep .............................
}
//----------------------------------------------------------------------
@@ -682,11 +671,9 @@ G4bool G4MagInt_Driver::QuickAdvance(
// Do an Integration Step
pIntStepper-> Stepper(yarrin, dydx, hstep, yarrout, yerr_vec) ;
// *******
// Estimate curve-chord distance
dchord_step= pIntStepper-> DistChord();
// *********
// Put back the values. yarrout ==> y_posvel
y_posvel.LoadFromArray( yarrout, fNoIntegrationVariables );
@@ -760,10 +747,9 @@ G4bool G4MagInt_Driver::QuickAdvance(
// This method computes new step sizes - but does not limit changes to
// within certain factors
//
G4double
G4MagInt_Driver::ComputeNewStepSize(
G4double errMaxNorm, // max error (normalised)
G4double hstepCurrent) // current step size
G4double G4MagInt_Driver::
ComputeNewStepSize(G4double errMaxNorm, // max error (normalised)
G4double hstepCurrent) // current step size
{
G4double hnew;
@@ -772,10 +758,14 @@ G4MagInt_Driver::ComputeNewStepSize(
{
// Step failed; compute the size of retrial Step.
hnew = GetSafety()*hstepCurrent*std::pow(errMaxNorm,GetPshrnk()) ;
} else if(errMaxNorm > 0.0 ) {
}
else if(errMaxNorm > 0.0 )
{
// Compute size of next Step for a successful step
hnew = GetSafety()*hstepCurrent*std::pow(errMaxNorm,GetPgrow()) ;
} else {
}
else
{
// if error estimate is zero (possible) or negative (dubious)
hnew = max_stepping_increase * hstepCurrent;
}
@@ -855,7 +845,7 @@ void G4MagInt_Driver::PrintStatus(
G4int subStepNo)
{
G4int verboseLevel= fVerboseLevel;
static G4ThreadLocal G4int noPrecision= 5;
const G4int noPrecision = 5;
G4int oldPrec= G4cout.precision(noPrecision);
// G4cout.setf(ios_base::fixed,ios_base::floatfield);
@@ -898,7 +888,6 @@ void G4MagInt_Driver::PrintStatus(
{
PrintStat_Aux( StartFT, requestStep, 0.,
0, 0.0, 1.0);
//*************
}
if( verboseLevel <= 3 )
@@ -906,21 +895,8 @@ void G4MagInt_Driver::PrintStatus(
G4cout.precision(noPrecision);
PrintStat_Aux( CurrentFT, requestStep, step_len,
subStepNo, subStepSize, DotStartCurrentVeloc );
//*************
}
else // if( verboseLevel > 3 )
{
// Multi-line output
// G4cout << "Current Position is " << CurrentPosition << G4endl
// << " and UnitVelocity is " << CurrentUnitVelocity << G4endl;
// G4cout << "Step taken was " << step_len
// << " out of PhysicalStep= " << requestStep << G4endl;
// G4cout << "Final safety is: " << safety << G4endl;
// G4cout << "Chord length = " << (CurrentPosition-StartPosition).mag()
// << G4endl << G4endl;
}
G4cout.precision(oldPrec);
}
@@ -1004,37 +980,6 @@ void G4MagInt_Driver::PrintStatisticsReport()
<< " Small= " << fNoSmallSteps
<< " Non-initial small= " << (fNoSmallSteps-fNoInitialSmallSteps)
<< G4endl;
#ifdef G4FLD_STATS
G4cout << "MID dyerr: "
<< " maximum= " << fDyerr_max
<< " Sum small= " << fDyerrPos_smTot
<< " std::sqrt(Sum large^2): pos= " << std::sqrt(fDyerrPos_lgTot)
<< " vel= " << std::sqrt( fDyerrVel_lgTot )
<< " Total h-distance: small= " << fSumH_sm
<< " large= " << fSumH_lg
<< G4endl;
#if 0
G4int noPrecSmall=4;
// Single line precis of statistics ... optional
G4cout.precision(noPrecSmall);
G4cout << "MIDnums: " << fMinimumStep
<< " " << fNoTotalSteps
<< " " << fNoSmallSteps
<< " " << fNoSmallSteps-fNoInitialSmallSteps
<< " " << fNoBadSteps
<< " " << fDyerr_max
<< " " << fDyerr_mx2
<< " " << fDyerrPos_smTot
<< " " << fSumH_sm
<< " " << fDyerrPos_lgTot
<< " " << fDyerrVel_lgTot
<< " " << fSumH_lg
<< G4endl;
#endif
#endif
G4cout.precision(oldPrec);
}
@@ -1048,13 +993,17 @@ void G4MagInt_Driver::SetSmallestFraction(G4double newFraction)
}
else
{
G4cerr << "Warning: SmallestFraction not changed. " << G4endl
<< " Proposed value was " << newFraction << G4endl
<< " Value must be between 1.e-8 and 1.e-16" << G4endl;
std::ostringstream message;
message << "Smallest Fraction not changed. " << G4endl
<< " Proposed value was " << newFraction << G4endl
<< " Value must be between 1.e-8 and 1.e-16";
G4Exception("G4MagInt_Driver::SetSmallestFraction()",
"GeomField1001", JustWarning, message);
}
}
void G4MagInt_Driver::GetDerivatives(const G4FieldTrack& y_curr, G4double* dydx) const
void G4MagInt_Driver::
GetDerivatives(const G4FieldTrack& y_curr, G4double* dydx) const
{
G4double ytemp[G4FieldTrack::ncompSVEC];
y_curr.DumpToArray(ytemp);
@@ -1081,3 +1030,9 @@ G4MagIntegratorStepper* G4MagInt_Driver::GetStepper()
return pIntStepper;
}
void G4MagInt_Driver::
RenewStepperAndAdjust(G4MagIntegratorStepper *pItsStepper)
{
pIntStepper = pItsStepper;
ReSetParameters();
}
@@ -24,14 +24,14 @@
// ********************************************************************
//
//
// $Id: G4MagneticField.cc 96678 2016-04-29 16:21:01Z gcosmo $
// $Id: G4MagneticField.cc 108823 2018-03-09 11:03:44Z gcosmo $
//
// --------------------------------------------------------------------
#include "G4MagneticField.hh"
G4MagneticField::G4MagneticField()
: G4ElectroMagneticField()
: G4Field( false ) // No gravitational field (default)
{
}
@@ -39,14 +39,14 @@ G4MagneticField::~G4MagneticField()
{
}
G4MagneticField::G4MagneticField(const G4MagneticField &MagField)
: G4ElectroMagneticField( MagField )
G4MagneticField::G4MagneticField(const G4MagneticField & )
: G4Field( false )
{
}
G4MagneticField& G4MagneticField::operator = (const G4MagneticField &p)
{
if (&p == this) return *this;
G4ElectroMagneticField::operator=(p);
G4Field::operator=(p);
return *this;
}
@@ -0,0 +1,152 @@
// ********************************************************************
// * License and Disclaimer *
// * *
// * The Geant4 software is copyright of the Copyright Holders of *
// * the Geant4 Collaboration. It is provided under the terms and *
// * conditions of the Geant4 Software License, included in the file *
// * LICENSE and available at http://cern.ch/geant4/license . These *
// * include a list of copyright holders. *
// * *
// * Neither the authors of this software system, nor their employing *
// * institutes,nor the agencies providing financial support for this *
// * work make any representation or warranty, express or implied, *
// * regarding this software system or assume any liability for its *
// * use. Please see the license in the file LICENSE and URL above *
// * for the full disclaimer and the limitation of liability. *
// * *
// * This code implementation is the result of the scientific and *
// * technical work of the GEANT4 collaboration. *
// * By using, copying, modifying or distributing the software (or *
// * any work based on the software) you agree to acknowledge its *
// * use in resulting scientific publications, and indicate your *
// * acceptance of all terms of the Geant4 Software license. *
// ********************************************************************
//
//
// $Id: $
//
// G4ModifiedMidpoint implementation
// Based on modified_midpoint.hpp from boost
//
// Author: Dmitry Sorokin - GSoC 2016
//
///////////////////////////////////////////////////////////////////////////////
#include "G4ModifiedMidpoint.hh"
G4ModifiedMidpoint::G4ModifiedMidpoint( G4EquationOfMotion* equation,
G4int nvar, G4int steps )
: fEquation(equation), fnvar(nvar), fsteps(steps)
{
if (nvar <= 0)
{
G4Exception("G4ModifiedMidpoint::G4ModifiedMidpoint()",
"GeomField0002", FatalException,
"Invalid number of variables; must be greater than zero!");
}
}
void G4ModifiedMidpoint::DoStep( const G4double yIn[], const G4double dydyIn[],
G4double yOut[], G4double hstep) const
{
G4double y0[G4FieldTrack::ncompSVEC];
G4double y1[G4FieldTrack::ncompSVEC];
G4double dydx[G4FieldTrack::ncompSVEC];
G4double yTemp[G4FieldTrack::ncompSVEC];
const G4double h = hstep / fsteps;
const G4double h2 = 2 * h;
// y1 = yIn + h * dydx
for (G4int i = 0; i < fnvar; ++i)
{
y1[i] = yIn[i] + h * dydyIn[i];
}
fEquation->RightHandSide(y1, dydx);
copy(y0, yIn);
// general step
// yTemp = y1; y1 = y0 + h2 * dydx; y0 = yTemp
for (G4int i = 1; i < fsteps; ++i)
{
copy(yTemp, y1);
for (G4int j = 0; j < fnvar; ++j)
{
y1[j] = y0[j] + h2 * dydx[j];
}
copy(y0, yTemp);
fEquation->RightHandSide(y1, dydx);
}
// last step
// yOut = 0.5 * (y0 + y1 + h * dydx)
for (G4int i = 0; i < fnvar; ++i)
{
yOut[i] = 0.5 * (y0[i] + y1[i] + h * dydx[i]);
}
}
void G4ModifiedMidpoint::DoStep( const G4double yIn[], const G4double dydxIn[],
G4double yOut[], G4double hstep, G4double yMid[],
G4double derivs[][G4FieldTrack::ncompSVEC]) const
{
G4double y0[G4FieldTrack::ncompSVEC];
G4double y1[G4FieldTrack::ncompSVEC];
G4double yTemp[G4FieldTrack::ncompSVEC];
const G4double h = hstep / fsteps;
const G4double h2 = 2 * h;
// y0 = yIn
copy(y0, yIn);
// y1 = y0 + h * dydx
for (G4int i = 0; i < fnvar; ++i)
{
y1[i] = y0[i] + h * dydxIn[i];
}
// result of first step already gives approximation
// at the center of the interval
if(fsteps == 2)
{
copy(yMid, y1);
}
fEquation->RightHandSide(y1, derivs[0]);
// general step
// yTemp = y1; y1 = y0 + h2 * dydx; y0 = yTemp
for (G4int i = 1; i < fsteps; ++i)
{
copy(yTemp, y1);
for (G4int j = 0; j < fnvar; ++j)
{
y1[j] = y0[j] + h2 * derivs[i-1][j];
}
copy(y0, yTemp);
// save approximation at the center of the interval
if(i == fsteps / 2 - 1 )
{
copy(yMid, y1);
}
fEquation->RightHandSide(y1, derivs[i]);
}
// last step
// yOut = 0.5 * (y0 + y1 + h * dydx)
for (G4int i = 0; i < fnvar; ++i)
{
yOut[i] = 0.5 * (y0[i] + y1[i] + h * derivs[fsteps-1][i]);
}
}
void G4ModifiedMidpoint::copy(G4double dst[], const G4double src[]) const
{
std::memcpy(dst, src, sizeof(G4double) * fnvar);
}
@@ -24,11 +24,11 @@
// ********************************************************************
//
//
// $Id: G4NystromRK4.cc 107821 2017-12-05 14:14:47Z gunter $
// $Id: G4NystromRK4.cc 110753 2018-06-12 15:44:03Z gcosmo $
//
// History:
// - Created: I.Gavrilenko 15.05.2009 (as G4AtlasRK4)
// - Adaptations: J. Apostolakis May-Nov 2009
// - Adaptations: J.Apostolakis May-Nov 2009
// -------------------------------------------------------------------
#include <iostream>
@@ -64,13 +64,14 @@ G4NystromRK4::~G4NystromRK4()
{
}
/////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////
// Integration in one step
/////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////
void
G4NystromRK4::Stepper
(const G4double P[],const G4double dPdS[],G4double Step,G4double Po[],G4double Err[])
G4NystromRK4::Stepper (const G4double P[],
const G4double dPdS[],
G4double Step, G4double Po[], G4double Err[])
{
const G4double perMillion = 1.0e-6;
G4double R[4] = { P[0], P[1] , P[2], P[7] }; // x, y, z, t
@@ -92,7 +93,8 @@ G4NystromRK4::Stepper
// - Quick check momentum magnitude (squared) against previous value
G4double newmom2 = (P[3]*P[3]+P[4]*P[4]+P[5]*P[5]);
G4double oldmom2 = m_mom * m_mom;
if( std::fabs(newmom2 - oldmom2) > perMillion * oldmom2 ) {
if( std::fabs(newmom2 - oldmom2) > perMillion * oldmom2 )
{
m_mom = std::sqrt(newmom2) ;
m_imom = 1./m_mom;
m_cof = m_fEq->FCof()*m_imom;
@@ -178,9 +180,9 @@ G4NystromRK4::Stepper
}
/////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////
// Estimate the maximum distance from the curve to the chord
/////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////
G4double
G4NystromRK4::DistChord() const
@@ -193,7 +195,8 @@ G4NystromRK4::DistChord() const
G4double dz = m_mPoint[2]-m_iPoint[2];
G4double d2 = (ax*ax+ay*ay+az*az) ;
if(d2!=0.) {
if(d2!=0.)
{
G4double ds = (ax*dx+ay*dy+az*dz)/d2;
dx -= (ds*ax) ;
dy -= (ds*ay) ;
@@ -202,9 +205,9 @@ G4NystromRK4::DistChord() const
return std::sqrt(dx*dx+dy*dy+dz*dz);
}
/////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////
// Derivatives calculation - caching the momentum value
/////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////
void
G4NystromRK4::ComputeRightHandSide(const G4double P[],G4double dPdS[])
@@ -236,12 +239,15 @@ G4NystromRK4::CheckFieldPosition( const G4double Position[3],
G4double dy = Position[1] - lastPosition[1];
G4double dz = Position[2] - lastPosition[2];
G4double distMag2 = dx*dx+dy*dy+dz*dz;
if( distMag2 > m_magdistance2) {
if( distMag2 > m_magdistance2)
{
const G4double allowedDist = std::sqrt( m_magdistance2 );
G4double dist= std::sqrt( distMag2 );
G4cerr << " NystromRK4::Stepper> ERROR> Moved from correct field position by "
<< dist << "( larger than allowed = " << allowedDist << " ) "
<< G4endl;
std::ostringstream message;
message << "Moved from correct field position by " << dist
<< "( larger than allowed = " << allowedDist << " ) ";
G4Exception("G4NystromRK4::CheckFieldPosition()",
"GeomField1001", JustWarning, message);
ok= false;
}
return ok;
@@ -256,16 +262,22 @@ G4bool G4NystromRK4::CheckCachedMomemtum( const G4double PosMom[6],
{
constexpr G4double perThousand = 1.0e-3;
G4bool ok= true;
G4double new_mom2= (PosMom[3]*PosMom[3]+PosMom[4]*PosMom[4]+PosMom[5]*PosMom[5]);
G4double new_mom2= (PosMom[3]*PosMom[3]
+PosMom[4]*PosMom[4]
+PosMom[5]*PosMom[5]);
G4double new_mom= std::sqrt(new_mom2);
if( std::fabs(new_mom - savedMom ) > perThousand * savedMom ) {
G4cerr << " Nystrom::Stepper WARNING: momentum magnitude is invalid / has changed "
<< G4endl
<< " new value (p-mag) = " << new_mom << G4endl
<< " cached value (p-mag) = " << savedMom << G4endl;
if( savedMom > 0.0 ) {
G4cerr << " ratio (new/old) = " << new_mom / savedMom << G4endl;
if( std::fabs(new_mom - savedMom ) > perThousand * savedMom )
{
std::ostringstream message;
message << "Momentum magnitude is invalid / has changed !" << G4endl
<< " new value (p-mag) = " << new_mom << G4endl
<< " cached value (p-mag) = " << savedMom;
if( savedMom > 0.0 )
{
message << "; ratio (new/old) = " << new_mom / savedMom;
}
G4Exception("G4NystromRK4::CheckCachedMomemtum()",
"GeomField1001", JustWarning, message);
ok= false;
}
return ok;
@@ -0,0 +1,54 @@
//
// ********************************************************************
// * License and Disclaimer *
// * *
// * The Geant4 software is copyright of the Copyright Holders of *
// * the Geant4 Collaboration. It is provided under the terms and *
// * conditions of the Geant4 Software License, included in the file *
// * LICENSE and available at http://cern.ch/geant4/license . These *
// * include a list of copyright holders. *
// * *
// * Neither the authors of this software system, nor their employing *
// * institutes,nor the agencies providing financial support for this *
// * work make any representation or warranty, express or implied, *
// * regarding this software system or assume any liability for its *
// * use. Please see the license in the file LICENSE and URL above *
// * for the full disclaimer and the limitation of liability. *
// * *
// * This code implementation is the result of the scientific and *
// * technical work of the GEANT4 collaboration. *
// * By using, copying, modifying or distributing the software (or *
// * any work based on the software) you agree to acknowledge its *
// * use in resulting scientific publications, and indicate your *
// * acceptance of all terms of the Geant4 Software license. *
// ********************************************************************
//
// $Id: G4VIntegrationDriver.cc 109569 2018-05-02 07:08:33Z gcosmo $
//
// class G4VIntegrationDriver
//
// Class description:
//
// Abstract base class for 'driver' classes which are responsible for
// undertaking integration of an state given an equation of motion and
// within acceptable error bound(s).
//
// Different integration methods are meant to be provided via this
// common interface, and can span the original type (explicit Runge Kutta
// methods), enhanced RK methods and alternatives such as the
// Bulirsch-Stoer and multi-step methods.
//
// The drivers' key mission is to insure that the error is below set values.
//
// Implementation by Dmitry Sorokin - GSoC 2017
// Work supported by Google as part of Google Summer of Code 2017.
// Supervision / code review: John Apostolakis
#include "G4VIntegrationDriver.hh"
void G4VIntegrationDriver::RenewStepperAndAdjust(G4MagIntegratorStepper *)
{
G4Exception("G4VIntegrationDriver::RenewStepperAndAdjust", "Geometry001", FatalException,
"This method exists only for the original G4MagIntegratorDriver class. "
" Not defined for other classes derived from G4VIntegrationDriver");
}