/*******************************************************************************
*
* McStas, neutron ray-tracing package
*         Copyright 1997-2002, All rights reserved
*         Risoe National Laboratory, Roskilde, Denmark
*         Institut Laue Langevin, Grenoble, France
*
* Component: Adapt_check
*
* %I
* Written by: Kristian Nielsen
* Date: 1999
* Origin: Risoe
*
* Optimization specifier for the Source_adapt component.
*
* %D
* This components works together with the Source_adapt component, and
* is used to define the criteria for selecting which neutrons are
* considered "good" in the adaptive algorithm. The name of the
* associated Source_adapt component in the instrument definition is
* given as parameter. The component is special in that its position
* does not matter; all neutrons that have not been absorbed prior to
* the component are considered "good".
*
* Example: Adapt_check(source_comp="MySource")
*
* %P
* source_comp: [string]  The name of the Source_adapt component in the instrument definition.
*
* %E
*******************************************************************************/

DEFINE COMPONENT Adapt_check
SETTING PARAMETERS (string source_comp)
/* Neutron parameters: (x,y,z,vx,vy,vz,t,sx,sy,sz,p) */

DECLARE
%{
#ifndef ADAPT_TREE_LIB_H
#error Adapt_check : needs Source_adapt component and %include "adapt_tree-lib"
#endif
%}

TRACE
%{
  double new_v, psi;
  struct source_adapt *adpt = COMP_GETPAR3(Source_adapt, source_comp, adpt);

  if(p == 0)
    ABSORB;
  psi = p/adpt->pi;
  adpt->psi[adpt->idx] += psi;
  adpt->psi_tot += psi/adpt->n[adpt->idx];
  new_v = (1 - adpt->a_beta)*adpt->factor*adpt->psi[adpt->idx]/
                (adpt->n[adpt->idx]*adpt->psi_tot) +
          adpt->a_beta/adpt->num;
  adapt_tree_add(adpt->atree, adpt->idx, new_v - adpt->atree->v[adpt->idx]);
%}

END
