BLCMDtune Class Reference

Inheritance diagram for BLCMDtune:

BLCommand BLManager::ZSteppingAction BLManager::RunAction

List of all members.


Detailed Description

class BLCMDtune - tunes a variable used as argument(s) to other elements

Public Member Functions

 BLCMDtune ()
 Default constructor.
virtual ~BLCMDtune ()
 Destructor.
 BLCMDtune (const BLCMDtune &r)
 Copy constructor.
G4String commandName ()
 commandName() returns "tune".
int command (BLArgumentVector &argv, BLArgumentMap &namedArgs)
 command() implements the tune command.
void defineNamedArgs ()
 defineNamedArgs() defines the named arguments for the command.
void UserZSteppingAction (const G4Track *track)
 from BLManager::ZSteppingAction.
void BeginOfRunAction (const G4Run *run)
 from BLManager::RunAction
void EndOfRunAction (const G4Run *run)

Private Attributes

G4String name
G4double z0
G4double z1
G4double initial
G4double initialStep
G4String start
G4String expr
G4double tolerance
G4int maxIter
G4Track saveTrack
G4double minStep
BLEvaluator eval
State state
G4int count
G4double thisX
G4double prevX
G4double thisY
G4double prevY

Constructor & Destructor Documentation

BLCMDtune::BLCMDtune (  ) 

Default constructor.

References BLCMDTYPE_CONTROL, count, expr, INIT, initial, initialStep, maxIter, minStep, name, prevX, prevY, BLCommand::registerCommand(), BLCommand::setDescription(), BLCommand::setSynopsis(), start, state, thisX, thisY, tolerance, z0, and z1.

Referenced by command().

00081                      : BLCommand(), BLManager::ZSteppingAction(), 
00082                         BLManager::RunAction(), saveTrack(), eval()
00083 {
00084         registerCommand(BLCMDTYPE_CONTROL);
00085         setSynopsis("Tune a variable used as argument to other elements.");
00086         setDescription("This command samples the Tune particle track at z0, "
00087                 "samples it again at z1, and varies its tune variable in "
00088                 "order to bring the expression to zero. The tune variable "
00089                 "is the first positional argument, and "
00090                 "can be used in the argument expression(s) for tunable "
00091                 "arguments located after z0. Due to the simple "
00092                 "solver used, there should be an approximately linear "
00093                 "dependence between the tune variable and the expression. "
00094                 "This is suitable for tuning the By field of a genericbend "
00095                 "or the maxGradient of a pillbox. "
00096                 "At each solver step the saved Tune particle is re-started from "
00097                 "z0, and when it reaches z1 the next step in the solver is "
00098                 "taken.\n\n"
00099                 "Note that multiple tune commands can be used together, as long "
00100                 "as their z0-z1 regions are properly nested by at least 0.5 mm "
00101                 "(i.e. each pair of regions must either not overlap at all, or "
00102                 "one must be wholly contained in the other). "
00103                 "This command can be complicated to use; see the User's Guide "
00104                 "for more description and examples.");
00105         
00106         name = "";
00107         z0 = -DBL_MAX;
00108         z1 = -DBL_MAX;
00109         initial = 0.0;
00110         initialStep = 0.1;
00111         start = "1";
00112         expr = "";
00113         tolerance = 0.0001;
00114         maxIter = 10;
00115         minStep = 0.01*mm;
00116         state = INIT;
00117         count = 0;
00118         thisX = prevX = thisY = prevY = 0.0;
00119 }

BLCMDtune::~BLCMDtune (  )  [virtual]

Destructor.

00122 {
00123 }

BLCMDtune::BLCMDtune ( const BLCMDtune r  ) 

Copy constructor.

References count, expr, INIT, initial, initialStep, maxIter, minStep, name, prevX, prevY, start, state, thisX, thisY, tolerance, z0, and z1.

00125                                        : BLCommand(r), 
00126                         BLManager::ZSteppingAction(r),
00127                         BLManager::RunAction(r), saveTrack(), eval()
00128 {
00129         name = r.name;
00130         z0 = r.z0;
00131         z1 = r.z1;
00132         initial = r.initial;
00133         initialStep = r.initialStep;
00134         start = r.start;
00135         expr = r.expr;
00136         tolerance = r.tolerance;
00137         maxIter = r.maxIter;
00138         minStep = r.minStep;
00139         state = INIT;
00140         count = 0;
00141         thisX = prevX = thisY = prevY = 0.0;
00142 }


Member Function Documentation

G4String BLCMDtune::commandName (  )  [inline, virtual]

commandName() returns "tune".

Implements BLCommand.

00063 { return "tune"; }

int BLCMDtune::command ( BLArgumentVector argv,
BLArgumentMap namedArgs 
) [virtual]

command() implements the tune command.

Implements BLCommand.

References BLCMDtune(), eval, FIXED, BLParam::getDouble(), BLManager::getObject(), BLCommand::handleNamedArgs(), INIT, initial, initialStep, BLTune::isSet(), minStep, name, Param, BLCommand::print(), BLCommand::printError(), BLManager::registerRunAction(), BLManager::registerZStep(), BLTune::set(), state, thisX, z0, and z1.

00145 {
00146         if(argv.size() != 1) {
00147                 printError("tune: Invalid command, must have name");
00148                 return -1;
00149         }
00150 
00151         if(argv[0] == "default") {
00152                 return defaultTune.handleNamedArgs(namedArgs);
00153         }
00154 
00155         if(BLTune::isSet(argv[0])) {
00156                 printError("tune: name '%s' is already in use!",name.c_str());
00157                 return -1;
00158         }
00159 
00160         BLCMDtune *t = new BLCMDtune(defaultTune);
00161         t->minStep = Param.getDouble("minStep");
00162         int retval = t->handleNamedArgs(namedArgs);
00163 
00164         t->name = argv[0];
00165         BLTune::set(t->name,t->initial);
00166         t->eval.setVariable(t->name,t->initial);
00167         t->thisX = t->initial;
00168         if(fabs(t->initialStep) < 1.0e-12) {
00169                 t->state = FIXED;
00170         } else {
00171                 t->state = INIT;
00172                 BLManager::getObject()->registerZStep(t->z0,t,1);
00173                 BLManager::getObject()->registerZStep(t->z1,t,1);
00174                 BLManager::getObject()->registerRunAction(t,false);
00175         }
00176 
00177         t->print(t->name);
00178 
00179         return retval;
00180 }

void BLCMDtune::defineNamedArgs (  )  [virtual]

defineNamedArgs() defines the named arguments for the command.

Reimplemented from BLCommand.

References BLCommand::argDouble(), BLCommand::argInt(), BLCommand::argString(), expr, initial, initialStep, maxIter, start, tolerance, z0, and z1.

00183 {
00184         argDouble(z0,"z0","The starting z position in CL coordinates.");
00185         argDouble(z1,"z1","The ending z position in CL coordinates.");
00186         argDouble(initial,"initial","Initial value of the variable 'name'");
00187         argDouble(initialStep,"initialStep","Initial step (0 to disable tuning)");
00188         argDouble(initialStep,"step","Synonym for initialStep");
00189         argString(start,"start","An expression that must be nonzero to start tuning (default=1)");
00190         argString(expr,"expr","The expression to tune to zero");
00191         argDouble(tolerance,"tolerance","The tolerance for expr to be zero");
00192         argInt(maxIter,"maxIter","The maximum number of iterations (10).");
00193 }

void BLCMDtune::UserZSteppingAction ( const G4Track *  track  )  [virtual]

from BLManager::ZSteppingAction.

Implements BLManager::ZSteppingAction.

References BLCOORD_CENTERLINE, count, DONE, eval, BLEvaluator::evaluate(), expr, FIXED, BLCoordinates::getCoords(), BLManager::getObject(), BLManager::getSteppingManager(), initialStep, maxIter, minStep, name, prevX, prevY, saveTrack, BLTune::set(), BLEvaluator::setTrackVariables(), start, state, thisX, thisY, tolerance, TUNING, z0, and z1.

00196 {
00197         if(state == FIXED) return;
00198 
00199         BLCoordinates *coord =  
00200                         (BLCoordinates *)track->GetUserInformation();
00201         G4ThreeVector clpos;
00202         coord->getCoords(BLCOORD_CENTERLINE,clpos);
00203 
00204         if(fabs(clpos[2]-z0) < minStep && state != TUNING) {
00205                 // we just entered the tuning region -- save the track
00206                 eval.setTrackVariables(track,BLCOORD_CENTERLINE,"0");
00207                 G4double t = eval.evaluate(start);
00208                 if(eval.status() != HepTool::Evaluator::OK) {
00209                         char tmp[128];
00210                         sprintf(tmp,"%s (variables are x0,y0,...,x1,y2,...)",
00211                                                                 start.c_str());
00212                         G4Exception("tune","Invalid Start Expr",FatalException,
00213                                 tmp);
00214                 }
00215                 if(t == 0.0) goto quit;
00216                 saveTrack.CopyTrackInfo(*track);
00217                 saveTrack.SetUserInformation(0);
00218                 state = TUNING;
00219                 count = 0;
00220                 printf("tune '%s' begun  %s=%.4f\n",name.c_str(),name.c_str(),
00221                                                                 thisX);
00222         } else if(fabs(clpos[2]-z1) < minStep && state == TUNING) {
00223                 // we just reached the end of the tuning region
00224                 if(++count > maxIter)
00225                         G4Exception("tune","Iteration Limit",FatalException,
00226                                                                 name.c_str());
00227                 eval.setTrackVariables(track,BLCOORD_CENTERLINE,"1");
00228                 thisY = eval.evaluate(expr);
00229                 if(eval.status() != HepTool::Evaluator::OK) {
00230                         char tmp[128];
00231                         sprintf(tmp,"%s (variables are x0,y0,...,x1,y2,...)",
00232                                                                 start.c_str());
00233                         G4Exception("tune","Invalid Tune Expr",FatalException,
00234                                 tmp);
00235                 }
00236                 if(fabs(thisY) < tolerance) {
00237                         state = DONE;
00238                         printf("tune '%s' complete in %d steps  expr=%.6f  %s=%.4f\n",
00239                                 name.c_str(),count,thisY,name.c_str(),thisX);
00240                         goto quit;
00241                 }
00242                 G4double newX=thisX+initialStep;
00243                 if(count > 1) {
00244                         G4double dydx = (prevY-thisY)/(prevX-thisX);
00245                         newX = thisX - thisY/dydx;
00246                 }
00247                 printf("tune '%s' step %d  %s=%.4f  expr=%.6f  new %s=%.4f\n",
00248                                 name.c_str(),count,name.c_str(),thisX,thisY,
00249                                 name.c_str(),newX);
00250                 prevX = thisX;
00251                 prevY = thisY;
00252                 thisX = newX;
00253                 BLTune::set(name,thisX);
00254                 eval.setVariable(name,thisX);
00255                 // restore saveTrack and kill the current track
00256                 BLManager::getObject()->getSteppingManager()->GetfSecondary()->
00257                                 push_back(new G4Track(saveTrack));
00258                 ((G4Track *)track)->SetTrackStatus(fStopAndKill);
00259         }
00260 quit:   ;
00261 }

void BLCMDtune::BeginOfRunAction ( const G4Run *  run  )  [inline, virtual]

from BLManager::RunAction

Implements BLManager::RunAction.

00075 { }

void BLCMDtune::EndOfRunAction ( const G4Run *  run  )  [virtual]

Implements BLManager::RunAction.

References DONE, FIXED, name, and state.

00264 {
00265         if(state != DONE && state != FIXED) {
00266                 G4Exception("tune","Failed to Converge",FatalException,
00267                                                         name.c_str());
00268         }
00269 }


Member Data Documentation

G4String BLCMDtune::name [private]

G4double BLCMDtune::z0 [private]

G4double BLCMDtune::z1 [private]

G4double BLCMDtune::initial [private]

Referenced by BLCMDtune(), command(), and defineNamedArgs().

G4double BLCMDtune::initialStep [private]

G4String BLCMDtune::start [private]

G4String BLCMDtune::expr [private]

G4double BLCMDtune::tolerance [private]

G4int BLCMDtune::maxIter [private]

G4Track BLCMDtune::saveTrack [private]

Referenced by UserZSteppingAction().

G4double BLCMDtune::minStep [private]

Referenced by command(), and UserZSteppingAction().

G4int BLCMDtune::count [private]

Referenced by BLCMDtune(), and UserZSteppingAction().

G4double BLCMDtune::thisX [private]

G4double BLCMDtune::prevX [private]

Referenced by BLCMDtune(), and UserZSteppingAction().

G4double BLCMDtune::thisY [private]

Referenced by BLCMDtune(), and UserZSteppingAction().

G4double BLCMDtune::prevY [private]

Referenced by BLCMDtune(), and UserZSteppingAction().


The documentation for this class was generated from the following file:
g4beamline