/* Automatically generated file. Do not edit. 
 * Format:     ANSI C source code
 * Creator:    McStas <http://www.mcstas.org>
 * Instrument: Conditional_test.instr (Conditional_test)
 * Date:       Thu Apr  9 06:22:40 2026
 * File:       ./Conditional_test.c
 * CFLAGS= -DFUNNEL 
 */

#ifndef WIN32
#  ifndef OPENACC
#    define _GNU_SOURCE
#  endif
#  define _POSIX_C_SOURCE 200809L
#endif
/* In case of cl.exe on Windows, supppress warnings about #pragma acc */
#ifdef _MSC_EXTENSIONS
#pragma warning(disable: 4068)
#endif

#define MCCODE_STRING " 3.99.99, git"
#define FLAVOR        "mcstas"
#define FLAVOR_UPPER  "MCSTAS"

#define MC_USE_DEFAULT_MAIN
#define MC_TRACE_ENABLED

#include <string.h>
#include <inttypes.h>

typedef double MCNUM;
typedef struct {MCNUM x, y, z;} Coords;
typedef MCNUM Rotation[3][3];
#define MCCODE_BASE_TYPES

/* available random number generators */
#define _RNG_ALG_MT         1
#define _RNG_ALG_KISS       2
/* selection of random number generator */
#ifndef RNG_ALG
#  define RNG_ALG  _RNG_ALG_KISS
#endif
#if RNG_ALG == _RNG_ALG_MT // MT 
#define randstate_t uint32_t
#elif RNG_ALG == _RNG_ALG_KISS  // KISS
#define randstate_t uint64_t
#endif

#ifndef MC_NUSERVAR
#define MC_NUSERVAR 10
#endif

/* Particle JUMP control logic */
struct particle_logic_struct {
int dummy;
};

struct _struct_particle {
  double x,y,z; /* position [m] */
  double vx,vy,vz; /* velocity [m/s] */
  double sx,sy,sz; /* spin [0-1] */
  int mcgravitation; /* gravity-state */
  void *mcMagnet;    /* precession-state */
  int allow_backprop; /* allow backprop */
  /* Generic Temporaries: */
  /* May be used internally by components e.g. for special */
  /* return-values from functions used in trace, thusreturned via */
  /* particle struct. (Example: Wolter Conics from McStas, silicon slabs.) */
  double _mctmp_a; /* temp a */
  double _mctmp_b; /* temp b */
  double _mctmp_c; /* temp c */
  randstate_t randstate[7];
  double t, p;     /* time, event weight */
  long long _uid;  /* Unique event ID */
  long _index;     /* component index where to send this event */
  long _absorbed;  /* flag set to TRUE when this event is to be removed/ignored */
  long _scattered; /* flag set to TRUE when this event has interacted with the last component instance */
  long _restore;   /* set to true if neutron event must be restored */
  long flag_nocoordschange;   /* set to true if particle is jumping */
  struct particle_logic_struct _logic;
};
typedef struct _struct_particle _class_particle;

_class_particle _particle_global_randnbuse_var;
_class_particle* _particle = &_particle_global_randnbuse_var;

#pragma acc routine
_class_particle mcgenstate(void);
#pragma acc routine
_class_particle mcsetstate(double x, double y, double z, double vx, double vy, double vz,
			   double t, double sx, double sy, double sz, double p, int mcgravitation, void *mcMagnet, int mcallowbackprop);
#pragma acc routine
_class_particle mcgetstate(_class_particle mcneutron, double *x, double *y, double *z,
                           double *vx, double *vy, double *vz, double *t,
                           double *sx, double *sy, double *sz, double *p);

extern int mcgravitation;      /* flag to enable gravitation */
#pragma acc declare create ( mcgravitation )

_class_particle mcgenstate(void) {
  _class_particle particle = mcsetstate(0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, mcgravitation, NULL, 0);
  return(particle);
}
/*Generated user variable handlers:*/

#pragma acc routine
double particle_getvar(_class_particle *p, char *name, int *suc);

#ifdef OPENACC
#pragma acc routine
int str_comp(char *str1, char *str2);
#endif

double particle_getvar(_class_particle *p, char *name, int *suc){
#ifndef OPENACC
#define str_comp strcmp
#endif
  int s=1;
  double rval=0;
  if(!str_comp("x",name)){rval=p->x;s=0;}
  if(!str_comp("y",name)){rval=p->y;s=0;}
  if(!str_comp("z",name)){rval=p->z;s=0;}
  if(!str_comp("vx",name)){rval=p->vx;s=0;}
  if(!str_comp("vy",name)){rval=p->vy;s=0;}
  if(!str_comp("vz",name)){rval=p->vz;s=0;}
  if(!str_comp("sx",name)){rval=p->sx;s=0;}
  if(!str_comp("sy",name)){rval=p->sy;s=0;}
  if(!str_comp("sz",name)){rval=p->sz;s=0;}
  if(!str_comp("t",name)){rval=p->t;s=0;}
  if(!str_comp("p",name)){rval=p->p;s=0;}
  if(!str_comp("_mctmp_a",name)){rval=p->_mctmp_a;s=0;}
  if(!str_comp("_mctmp_b",name)){rval=p->_mctmp_b;s=0;}
  if(!str_comp("_mctmp_c",name)){rval=p->_mctmp_c;s=0;}
  if (suc!=0x0) {*suc=s;}
  return rval;
}

#pragma acc routine
void* particle_getvar_void(_class_particle *p, char *name, int *suc);

#ifdef OPENACC
#pragma acc routine
int str_comp(char *str1, char *str2);
#endif

void* particle_getvar_void(_class_particle *p, char *name, int *suc){
#ifndef OPENACC
#define str_comp strcmp
#endif
  int s=1;
  void* rval=0;
  if(!str_comp("x",name)) {rval=(void*)&(p->x); s=0;}
  if(!str_comp("y",name)) {rval=(void*)&(p->y); s=0;}
  if(!str_comp("z",name)) {rval=(void*)&(p->z); s=0;}
  if(!str_comp("vx",name)){rval=(void*)&(p->vx);s=0;}
  if(!str_comp("vy",name)){rval=(void*)&(p->vy);s=0;}
  if(!str_comp("vz",name)){rval=(void*)&(p->vz);s=0;}
  if(!str_comp("sx",name)){rval=(void*)&(p->sx);s=0;}
  if(!str_comp("sy",name)){rval=(void*)&(p->sy);s=0;}
  if(!str_comp("sz",name)){rval=(void*)&(p->sz);s=0;}
  if(!str_comp("t",name)) {rval=(void*)&(p->t); s=0;}
  if(!str_comp("p",name)) {rval=(void*)&(p->p); s=0;}
  if (suc!=0x0) {*suc=s;}
  return rval;
}

#pragma acc routine
int particle_setvar_void(_class_particle *, char *, void*);

int particle_setvar_void(_class_particle *p, char *name, void* value){
#ifndef OPENACC
#define str_comp strcmp
#endif
  int rval=1;
  if(!str_comp("x",name)) {memcpy(&(p->x),  value, sizeof(double)); rval=0;}
  if(!str_comp("y",name)) {memcpy(&(p->y),  value, sizeof(double)); rval=0;}
  if(!str_comp("z",name)) {memcpy(&(p->z),  value, sizeof(double)); rval=0;}
  if(!str_comp("vx",name)){memcpy(&(p->vx), value, sizeof(double)); rval=0;}
  if(!str_comp("vy",name)){memcpy(&(p->vy), value, sizeof(double)); rval=0;}
  if(!str_comp("vz",name)){memcpy(&(p->vz), value, sizeof(double)); rval=0;}
  if(!str_comp("sx",name)){memcpy(&(p->sx), value, sizeof(double)); rval=0;}
  if(!str_comp("sy",name)){memcpy(&(p->sy), value, sizeof(double)); rval=0;}
  if(!str_comp("sz",name)){memcpy(&(p->sz), value, sizeof(double)); rval=0;}
  if(!str_comp("p",name)) {memcpy(&(p->p),  value, sizeof(double)); rval=0;}
  if(!str_comp("t",name)) {memcpy(&(p->t),  value, sizeof(double)); rval=0;}
  return rval;
}

#pragma acc routine
int particle_setvar_void_array(_class_particle *, char *, void*, int);

int particle_setvar_void_array(_class_particle *p, char *name, void* value, int elements){
#ifndef OPENACC
#define str_comp strcmp
#endif
  int rval=1;
  return rval;
}

#pragma acc routine
void particle_restore(_class_particle *p, _class_particle *p0);

void particle_restore(_class_particle *p, _class_particle *p0) {
  p->x  = p0->x;  p->y  = p0->y;  p->z  = p0->z;
  p->vx = p0->vx; p->vy = p0->vy; p->vz = p0->vz;
  p->sx = p0->sx; p->sy = p0->sy; p->sz = p0->sz;
  p->t = p0->t;  p->p  = p0->p;
  p->_absorbed=0; p->_restore=0;
}

#pragma acc routine
double particle_getuservar_byid(_class_particle *p, int id, int *suc){
  int s=1;
  double rval=0;
  switch(id){
  }
  if (suc!=0x0) {*suc=s;}
  return rval;
}

#pragma acc routine
void particle_uservar_init(_class_particle *p){
}

#define MC_EMBEDDED_RUNTIME
/* embedding file "mccode-r.h" */

/*******************************************************************************
*
* McCode, neutron/xray ray-tracing package
*         Copyright (C) 1997-2009, All rights reserved
*         Risoe National Laboratory, Roskilde, Denmark
*         Institut Laue Langevin, Grenoble, France
*
* Runtime: share/mccode-r.h
*
* %Identification
* Written by: KN
* Date:    Aug 29, 1997
* Release: mcstas 3.99.99
* Version: $Revision$
*
* Runtime system header for McStas/McXtrace.
*
* In order to use this library as an external library, the following variables
* and macros must be declared (see details in the code)
*
*   struct mcinputtable_struct mcinputtable[];
*   int numipar;
*   metadata_table_t metadata_table[];
*   int num_metadata;
*   char instrument_name[], instrument_source[];
*   int traceenabled, defaultmain;
*   extern MCNUM  mccomp_storein[];
*   extern MCNUM  mcAbsorbProp[];
*   extern MCNUM  mcScattered;
*   #define MCCODE_STRING "the McStas/McXtrace version"
*
* Usage: Automatically embbeded in the c code.
*
* $Id$
*
*******************************************************************************/

#ifndef MCCODE_R_H
#define MCCODE_R_H "$Revision$"

#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include <stdarg.h>
#include <limits.h>
#include <errno.h>
#include <time.h>
#ifndef _MSC_EXTENSIONS
#include <sys/time.h>
#endif
#include <float.h>
#include <inttypes.h>
#include <stdint.h>
#ifdef OPENACC
#include <openacc.h>
#ifndef GCCOFFLOAD
#include <accelmath.h>
#else
#include <math.h>
#endif
#pragma acc routine
int noprintf();
#pragma acc routine
size_t str_len(const char *s);
#else
#include <math.h>
#endif

/* In case of gcc / clang, ensure to use
   the built-in isnan/isinf functions */
#if defined(__GNUC__) || defined(__clang__)
#  ifdef isnan
#    undef isnan
#  endif
#  ifdef isinf
#    undef isinf
#  endif
#  define isnan(x) __builtin_isnan(x)
#  define isinf(x) __builtin_isinf(x)
#endif

#ifdef _MSC_EXTENSIONS
#ifndef _TIMES_H
#define _TIMES_H

#if defined(WIN32) || defined(_WIN32)
#include <sys/timeb.h>
#include <sys/types.h>
#include <winsock2.h>

int gettimeofday(struct timeval* t,void* timezone);

#define __need_clock_t
#include <time.h>


/* Structure describing CPU time used by a process and its children.  */
struct tms
  {
    clock_t tms_utime;          /* User CPU time.  */
    clock_t tms_stime;          /* System CPU time.  */

    clock_t tms_cutime;         /* User CPU time of dead children.  */
    clock_t tms_cstime;         /* System CPU time of dead children.  */
  };

/* Store the CPU time used by this process and all its
   dead children (and their dead children) in BUFFER.
   Return the elapsed real time, or (clock_t) -1 for errors.
   All times are in CLK_TCKths of a second.  */
clock_t times (struct tms *__buffer);

typedef long long suseconds_t ;



int gettimeofday(struct timeval* t,void* timezone)
{       struct _timeb timebuffer;
        _ftime( &timebuffer );
        t->tv_sec=timebuffer.time;
        t->tv_usec=1000*timebuffer.millitm;
		return 0;
}

clock_t times (struct tms *__buffer) {

	__buffer->tms_utime = clock();
	__buffer->tms_stime = 0;
	__buffer->tms_cstime = 0;
	__buffer->tms_cutime = 0;
	return __buffer->tms_utime;
}


#endif
#endif
#endif

/* If the runtime is embedded in the simulation program, some definitions can
   be made static. */

#ifdef MC_EMBEDDED_RUNTIME
#  define mcstatic
#else
#  define mcstatic
#endif

#ifdef __dest_os
#  if (__dest_os == __mac_os)
#    define MAC
#  endif
#endif

#ifdef __FreeBSD__
#  define NEED_STAT_H
#endif

#if defined(__APPLE__) && defined(__GNUC__)
#  define NEED_STAT_H
#endif

#if defined(WIN32) || defined(_WIN32)
#  define NEED_STAT_H
#  define NEED_TYPES_H
#endif

#ifdef NEED_STAT_H
#  include <sys/stat.h>
#endif

#ifdef NEED_TYPES_H
#  include <sys/types.h>
#endif

#ifndef MC_PATHSEP_C
#if defined(WIN32) || defined(_WIN32)
#    define MC_PATHSEP_C '\\'
#    define MC_PATHSEP_S "\\"
#  else  /* !WIN32 */
#    define MC_PATHSEP_C '/'
#    define MC_PATHSEP_S "/"
#  endif /* !WIN32 */
#endif /* MC_PATHSEP_C */

#if defined(WIN32) || defined(_WIN32)
#if defined _MSC_VER
#include <direct.h>
#elif defined __GNUC__
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#endif
#define mkdir(a,b) mkdir(a)
#define getpid() _getpid()
#endif

/* the version string is replaced when building distribution with mkdist */
#ifndef MCCODE_STRING
#  define MCCODE_STRING " 3.99.99, git"
#endif

#ifndef MCCODE_DATE
#  define MCCODE_DATE "git"
#endif

#ifndef MCCODE_VERSION
#  define MCCODE_VERSION "3.99.99"
#endif

#ifndef __MCCODE_VERSION__
#define __MCCODE_VERSION__ 399099L
#endif

#ifndef MCCODE_NAME
#  define MCCODE_NAME "mcstas"
#endif

#ifndef MCCODE_PARTICLE
#  define MCCODE_PARTICLE "neutron"
#endif

#ifndef MCCODE_PARTICLE_CODE
#  define MCCODE_PARTICLE_CODE 2112
#endif

#ifndef MCCODE_LIBENV
#  define MCCODE_LIBENV "MCSTAS"
#endif

#ifndef FLAVOR_UPPER
#  define FLAVOR_UPPER MCCODE_NAME
#endif

#ifdef MC_PORTABLE
#  ifndef NOSIGNALS
#    define NOSIGNALS 1
#  endif
#endif

#ifdef MAC
#  ifndef NOSIGNALS
#    define NOSIGNALS 1
#  endif
#endif

#if (USE_MPI == 0)
#  undef USE_MPI
#endif

#ifdef USE_MPI  /* default is to disable signals with MPI, as MPICH uses them to communicate */
#  ifndef NOSIGNALS
#    define NOSIGNALS 1
#  endif
#endif

#ifdef OPENACC  /* default is to disable signals with PGI/OpenACC */
#  ifndef NOSIGNALS
#    define NOSIGNALS 1
#  endif
#endif

#ifndef OPENACC
#  ifndef USE_OFF  /* default is to enable OFF when not using PGI/OpenACC */
#    define USE_OFF
#  endif
#  ifndef CPUFUNNEL  /* allow to enable FUNNEL-mode on CPU */
#  ifdef FUNNEL      /* by default disable FUNNEL-mode when not using PGI/OpenACC */
#    undef FUNNEL
#  endif
#  endif
#endif

#if (NOSIGNALS == 0)
#  undef NOSIGNALS
#endif

/** Header information for metadata-r.c ----------------------------------------------------------------------------- */
struct metadata_table_struct { /* stores metadata strings from components */
  char * source;  // component name which provided the metadata
  char * name;    // the name of the metadata
  char * type;    // the MIME type of the metadata (free form, valid identifier)
  char * value;   // the metadata string contents
};
typedef struct metadata_table_struct metadata_table_t;
char * metadata_table_key_component(char* key);
char * metadata_table_key_literal(char * key);
int metadata_table_defined(int, metadata_table_t *, char *);
char * metadata_table_name(int, metadata_table_t *, char *);
char * metadata_table_type(int, metadata_table_t *, char *);
char * metadata_table_literal(int, metadata_table_t *, char *);
void metadata_table_print_all_keys(int no, metadata_table_t * tab);
int metadata_table_print_all_components(int no, metadata_table_t * tab);
int metadata_table_print_component_keys(int no, metadata_table_t * tab, char * key);
/* -------------------------------------------------------------------------- Header information for metadata-r.c --- */

/* Note: the enum instr_formal_types definition MUST be kept
   synchronized with the one in mccode.h and with the
   instr_formal_type_names array in cogen.c. */
enum instr_formal_types
  {
    instr_type_int,
    instr_type_string, instr_type_char,
    instr_type_vector, instr_type_double
  };
struct mcinputtable_struct { /* defines instrument parameters */
  char *name; /* name of parameter */
  void *par;  /* pointer to instrument parameter (variable) */
  enum instr_formal_types type;
  char *val;  /* default value */
  char *unit; /* expected unit for parameter; informational only */
};


#ifndef MCCODE_BASE_TYPES
typedef double MCNUM;
typedef struct {MCNUM x, y, z;} Coords;
typedef MCNUM Rotation[3][3];
#endif

/* the following variables are defined in the McStas generated C code
   but should be defined externally in case of independent library usage */
#ifndef DANSE
extern struct mcinputtable_struct mcinputtable[];         /* list of instrument parameters */
extern int    numipar;                                    /* number of instrument parameters */
extern metadata_table_t metadata_table[];                 /* list of component-defined string metadata */
extern int    num_metadata;                               /* number of component-defined string metadata */
extern char   instrument_name[], instrument_source[]; /* instrument name and filename */
extern char  *instrument_exe;                           /* executable path = argv[0] or NULL */
extern char   instrument_code[];                        /* contains the initial 'instr' file */

#ifndef MC_ANCIENT_COMPATIBILITY
extern int traceenabled, defaultmain;
#endif
#endif


/* Useful macros ============================================================ */


/* SECTION: Dynamic Arrays */
typedef int* IArray1d;
IArray1d create_iarr1d(int n);
void destroy_iarr1d(IArray1d a);

typedef int** IArray2d;
IArray2d create_iarr2d(int nx, int ny);
void destroy_iarr2d(IArray2d a);

typedef int*** IArray3d;
IArray3d create_iarr3d(int nx, int ny, int nz);
void destroy_iarr3d(IArray3d a);

typedef double* DArray1d;
DArray1d create_darr1d(int n);
void destroy_darr1d(DArray1d a);

typedef double** DArray2d;
DArray2d create_darr2d(int nx, int ny);
void destroy_darr2d(DArray2d a);

typedef double*** DArray3d;
DArray3d create_darr3d(int nx, int ny, int nz);
void destroy_darr3d(DArray3d a);


/* MPI stuff */
#ifdef USE_MPI
#include "mpi.h"

#ifdef OMPI_MPI_H  /* openmpi does not use signals: we may install our sighandler */
#ifndef OPENACC    /* ... but only if we are not also running on GPU */
#undef NOSIGNALS
#endif
#endif

/*
 * MPI_MASTER(i):
 * execution of i only on master node
 */
#define MPI_MASTER(statement) { \
  if(mpi_node_rank == mpi_node_root)\
  { statement; } \
}

#ifndef MPI_REDUCE_BLOCKSIZE
#define MPI_REDUCE_BLOCKSIZE 100000
#endif

int mc_MPI_Sum(double* buf, long count);
int mc_MPI_Send(void *sbuf, long count, MPI_Datatype dtype, int dest);
int mc_MPI_Recv(void *rbuf, long count, MPI_Datatype dtype, int source);

/* MPI_Finalize exits gracefully and should be preferred to MPI_Abort */
#define exit(code) do {                                   \
    MPI_Finalize();                                       \
    exit(code);                                           \
  } while(0)

#else /* !USE_MPI */
#define MPI_MASTER(instr) instr
#endif /* USE_MPI */


#ifdef USE_MPI
static int mpi_node_count;
#endif

#ifdef USE_THREADS  /* user want threads */
#error Threading (USE_THREADS) support has been removed for very poor efficiency. Use MPI/SSH grid instead.
#endif


void   mcset_ncount(unsigned long long count);    /* wrapper to get mcncount */
#pragma acc routine
unsigned long long int mcget_ncount(void);            /* wrapper to set mcncount */
unsigned long long mcget_run_num(void);           /* wrapper to get mcrun_num=0:mcncount-1 */

/* Following part is only embedded when not redundant with mccode.h ========= */

#ifndef MCCODE_H

#ifndef NOSIGNALS
#include <signal.h>
char  *mcsig_message;
#define SIG_MESSAGE(msg) mcsig_message=(char *)(msg);
#else
#define SIG_MESSAGE(...)
#endif /* !NOSIGNALS */


/* Useful macros and constants ============================================== */


#ifndef FLT_MAX
#define FLT_MAX         3.40282347E+38F /* max decimal value of a "float" */
#endif

#ifndef MIN
#define MIN(a, b)  (((a) < (b)) ? (a) : (b))
#endif
#ifndef MAX
#define MAX(a, b)  (((a) > (b)) ? (a) : (b))
#endif
#ifndef SQR
#define SQR(x) ( (x) * (x) )
#endif
#ifndef SIGN
#define SIGN(x) (((x)>0.0)?(1):(-1))
#endif


#  ifndef M_E
#    define M_E        2.71828182845904523536  // e
#  endif
#  ifndef M_LOG2E
#    define M_LOG2E    1.44269504088896340736  //  log2(e)
#  endif
#  ifndef M_LOG10E
#    define M_LOG10E   0.434294481903251827651 //  log10(e)
#  endif
#  ifndef M_LN2
#    define M_LN2      0.693147180559945309417 //  ln(2)
#  endif
#  ifndef M_LN10
#    define M_LN10     2.30258509299404568402  //  ln(10)
#  endif
#  ifndef M_PI
#    define M_PI       3.14159265358979323846  //  pi
#  endif
#  ifndef PI
#    define PI       M_PI                      //  pi - also used in some places
#  endif
#  ifndef M_PI_2
#    define M_PI_2     1.57079632679489661923  //  pi/2
#  endif
#  ifndef M_PI_4
#    define M_PI_4     0.785398163397448309616 //  pi/4
#  endif
#  ifndef M_1_PI
#    define M_1_PI     0.318309886183790671538 //  1/pi
#  endif
#  ifndef M_2_PI
#    define M_2_PI     0.636619772367581343076 //  2/pi
#  endif
#  ifndef M_2_SQRTPI
#    define M_2_SQRTPI 1.12837916709551257390  //  2/sqrt(pi)
#  endif
#  ifndef M_SQRT2
#    define M_SQRT2    1.41421356237309504880  //  sqrt(2)
#  endif
#  ifndef M_SQRT1_2
#    define M_SQRT1_2  0.707106781186547524401 //  1/sqrt(2)
#  endif

#define RAD2MIN  ((180*60)/PI)
#define MIN2RAD  (PI/(180*60))
#define DEG2RAD  (PI/180)
#define RAD2DEG  (180/PI)
#define FWHM2RMS 0.424660900144    /* Convert between full-width-half-max and */
#define RMS2FWHM 2.35482004503     /* root-mean-square (standard deviation) */
#define HBAR     1.05457168e-34    /* [Js] h bar Planck constant CODATA 2002 */
#define MNEUTRON 1.67492728e-27    /* [kg] mass of neutron CODATA 2002 */
#define GRAVITY  9.81              /* [m/s^2] gravitational acceleration */
#define NA       6.02214179e23     /* [#atoms/g .mole] Avogadro's number*/


#define UNSET nan("0x6E6F74736574")
int nans_match(double, double);
int is_unset(double);
int is_valid(double);
int is_set(double);
int all_unset(int n, ...);
int all_set(int n, ...);
int any_unset(int n, ...);
int any_set(int n, ...);


/* wrapper to get absolute and relative position of comp */
/* mccomp_posa and mccomp_posr are defined in McStas generated C code */
#define POS_A_COMP_INDEX(index) (instrument->_position_absolute[index])
#define POS_R_COMP_INDEX(index) (instrument->_position_relative[index])

/* setting parameters based COMP_GETPAR (returned as pointer)         */
/* compname must be given as a string, type and par are symbols.      */
#define COMP_GETPAR3(type, compname, par) \
    &( ((_class_ ## type ##_parameters *) _getvar_parameters(compname))->par )
/* the body of this function depends on component instances, and is cogen'd */
void* _getvar_parameters(char* compname);

int _getcomp_index(char* compname);

/* Note: The two-stage approach to COMP_GETPAR is NOT redundant; without it,
* after #define C sample, COMP_GETPAR(C,x) would refer to component C, not to
* component sample. Such are the joys of ANSI C.

* Anyway the usage of COMP_GETPAR requires that we use sometimes bare names...
* NOTE: This can ONLY be used in instrument descriptions, not components.
*/
#define COMP_GETPAR2(comp, par) (_ ## comp ## _var._parameters.par)
#define COMP_GETPAR(comp, par) COMP_GETPAR2(comp,par)

#define INSTRUMENT_GETPAR(par) (_instrument_var._parameters.par)

/* Current component name, index, position and orientation */
/* These macros work because, using class-based functions, "comp" is usually
*  the local variable of the active/current component. */
#define INDEX_CURRENT_COMP (_comp->_index)
#define NAME_CURRENT_COMP (_comp->_name)
#define TYPE_CURRENT_COMP (_comp->_type)
#define POS_A_CURRENT_COMP (_comp->_position_absolute)
#define POS_R_CURRENT_COMP (_comp->_position_relative)
#define ROT_A_CURRENT_COMP (_comp->_rotation_absolute)
#define ROT_R_CURRENT_COMP (_comp->_rotation_relative)

#define NAME_INSTRUMENT (instrument->_name)


/* MCDISPLAY/trace and debugging message sent to stdout */
#ifdef MC_TRACE_ENABLED
#define DEBUG
#endif

#ifdef DEBUG
#define DEBUG_INSTR() if(!mcdotrace); else { printf("INSTRUMENT:\n"); printf("Instrument '%s' (%s)\n", instrument_name, instrument_source); }
#define DEBUG_COMPONENT(name,c,t) if(!mcdotrace); else {\
     printf("COMPONENT: \"%s\"\n"					  \
     "POS: %g, %g, %g, %g, %g, %g, %g, %g, %g, %g, %g, %g\n", \
     name, c.x, c.y, c.z, t[0][0], t[0][1], t[0][2], \
     t[1][0], t[1][1], t[1][2], t[2][0], t[2][1], t[2][2]); \
     printf("Component %30s AT (%g,%g,%g)\n", name, c.x, c.y, c.z); }
#define DEBUG_INSTR_END() if(!mcdotrace); else printf("INSTRUMENT END:\n");
#define DEBUG_ENTER() if(!mcdotrace); else printf("ENTER:\n");
#define DEBUG_COMP(c) if(!mcdotrace); else printf("COMP: \"%s\"\n", c);
#define DEBUG_LEAVE() if(!mcdotrace); else printf("LEAVE:\n");
#define DEBUG_ABSORB() if(!mcdotrace); else printf("ABSORB:\n");
#else
#define DEBUG_INSTR()
#define DEBUG_COMPONENT(name,c,t)
#define DEBUG_INSTR_END()
#define DEBUG_ENTER()
#define DEBUG_COMP(c)
#define DEBUG_LEAVE()
#define DEBUG_ABSORB()
#endif

// mcDEBUG_STATE and mcDEBUG_SCATTER are defined by mcstas-r.h and mcxtrace-r.h



#ifdef TEST
#define test_printf printf
#else
#define test_printf while(0) printf
#endif

/* send MCDISPLAY message to stdout to show gemoetry */
void mcdis_magnify(char *what);
void mcdis_line(double x1, double y1, double z1,
                double x2, double y2, double z2);
void mcdis_dashed_line(double x1, double y1, double z1,
		       double x2, double y2, double z2, int n);
void mcdis_multiline(int count, ...);
void mcdis_rectangle(char* plane, double x, double y, double z,
		     double width, double height);
void mcdis_box(double x, double y, double z,
	       double width, double height, double length, double thickness, double nx, double ny, double nz);
void mcdis_circle(char *plane, double x, double y, double z, double r);
void mcdis_Circle(double x, double y, double z, double r, double nx, double ny, double nz);
void mcdis_cylinder( double x, double y, double z,
		     double r, double height, double thickness, double nx, double ny, double nz);
void mcdis_cone( double x, double y, double z,
        double r, double height, double nx, double ny, double nz);
void mcdis_sphere(double x, double y, double z, double r);


/* random number generation. ================================================ */

#if RNG_ALG == _RNG_ALG_MT  // MT (currently not functional for GPU)
#  define MC_RAND_MAX ((uint32_t)0xffffffffUL)
#  define RANDSTATE_LEN 1
#  define srandom(seed) mt_srandom_empty()
#  define random() mt_random()
#  define _random() mt_random()
#elif RNG_ALG == _RNG_ALG_KISS  // KISS
#  ifndef UINT64_MAX
#    define UINT64_MAX ((uint64_t)0xffffffffffffffffULL)
#  endif
#  define MC_RAND_MAX UINT64_MAX
#  define RANDSTATE_LEN 7
#  define srandom(seed) kiss_srandom(_particle->randstate, seed)
#  define random() kiss_random(_particle->randstate)
#  define _random() kiss_random(state)
#endif

#pragma acc routine
double _randnorm2(randstate_t* state);

// Component writer interface
#define randnorm() _randnorm2(_particle->randstate)        // NOTE: can't use _randnorm on GPU
#define rand01() _rand01(_particle->randstate)
#define randpm1() _randpm1(_particle->randstate)
#define rand0max(p1) _rand0max(p1, _particle->randstate)
#define randminmax(p1, p2) _randminmax(p1, p2, _particle->randstate)
#define randtriangle() _randtriangle(_particle->randstate)

// Mersenne Twister rng
uint32_t mt_random(void);
void mt_srandom (uint32_t x);
void mt_srandom_empty();

// KISS rng
#pragma acc routine
uint64_t *kiss_srandom(uint64_t state[7], uint64_t seed);
#pragma acc routine
uint64_t kiss_random(uint64_t state[7]);

// Scrambler / hash function
#pragma acc routine seq
randstate_t _hash(randstate_t x);

// internal RNG (transforms) interface
#pragma acc routine
double _rand01(randstate_t* state);
#pragma acc routine
double _randpm1(randstate_t* state);
#pragma acc routine
double _rand0max(double max, randstate_t* state);
#pragma acc routine
double _randminmax(double min, double max, randstate_t* state);
#pragma acc routine
double _randtriangle(randstate_t* state);


#ifdef USE_OPENCL
#include "opencl-lib.h"
#include "opencl-lib.c"
#endif

#ifndef DANSE
int init(void);
int raytrace(_class_particle*);
int save(FILE *);
int finally(void);
int display(void);
#endif


/* GPU related algorithms =================================================== */

/*
*  Divide-and-conquer strategy for parallel sort absorbed last.
*/
#ifdef FUNNEL
long sort_absorb_last(_class_particle* particles, _class_particle* pbuffer, long len, long buffer_len, long flag_split, long* multiplier);
#endif
long sort_absorb_last_serial(_class_particle* particles, long len);


/* simple vector algebra ==================================================== */


#define vec_prod(x, y, z, x1, y1, z1, x2, y2, z2) \
	vec_prod_func(&x, &y, &z, x1, y1, z1, x2, y2, z2)
#pragma acc routine seq
mcstatic void vec_prod_func(double *x, double *y, double *z,
		double x1, double y1, double z1, double x2, double y2, double z2);

#pragma acc routine seq
mcstatic double scalar_prod(
		double x1, double y1, double z1, double x2, double y2, double z2);

#pragma acc routine seq
mcstatic void norm_func(double *x, double *y, double *z);
#define NORM(x,y,z)	norm_func(&x, &y, &z)

#pragma acc routine seq
void normal_vec(double *nx, double *ny, double *nz,
    double x, double y, double z);

/**
 * Rotate the vector vx,vy,vz psi radians around the vector ax,ay,az
 * and put the result in x,y,z.
 */
#define rotate(x, y, z, vx, vy, vz, phi, ax, ay, az) \
  do { \
    double mcrt_tmpx = (ax), mcrt_tmpy = (ay), mcrt_tmpz = (az); \
    double mcrt_vp, mcrt_vpx, mcrt_vpy, mcrt_vpz; \
    double mcrt_vnx, mcrt_vny, mcrt_vnz, mcrt_vn1x, mcrt_vn1y, mcrt_vn1z; \
    double mcrt_bx, mcrt_by, mcrt_bz; \
    double mcrt_cos, mcrt_sin; \
    NORM(mcrt_tmpx, mcrt_tmpy, mcrt_tmpz); \
    mcrt_vp = scalar_prod((vx), (vy), (vz), mcrt_tmpx, mcrt_tmpy, mcrt_tmpz); \
    mcrt_vpx = mcrt_vp*mcrt_tmpx; \
    mcrt_vpy = mcrt_vp*mcrt_tmpy; \
    mcrt_vpz = mcrt_vp*mcrt_tmpz; \
    mcrt_vnx = (vx) - mcrt_vpx; \
    mcrt_vny = (vy) - mcrt_vpy; \
    mcrt_vnz = (vz) - mcrt_vpz; \
    vec_prod(mcrt_bx, mcrt_by, mcrt_bz, \
             mcrt_tmpx, mcrt_tmpy, mcrt_tmpz, mcrt_vnx, mcrt_vny, mcrt_vnz); \
    mcrt_cos = cos((phi)); mcrt_sin = sin((phi)); \
    mcrt_vn1x = mcrt_vnx*mcrt_cos + mcrt_bx*mcrt_sin; \
    mcrt_vn1y = mcrt_vny*mcrt_cos + mcrt_by*mcrt_sin; \
    mcrt_vn1z = mcrt_vnz*mcrt_cos + mcrt_bz*mcrt_sin; \
    (x) = mcrt_vpx + mcrt_vn1x; \
    (y) = mcrt_vpy + mcrt_vn1y; \
    (z) = mcrt_vpz + mcrt_vn1z; \
  } while(0)

/**
 * Mirror (xyz) in the plane given by the point (rx,ry,rz) and normal (nx,ny,nz)
 *
 * TODO: This define is seemingly never used...
 */
#define mirror(x,y,z,rx,ry,rz,nx,ny,nz) \
  do { \
    double mcrt_tmpx= (nx), mcrt_tmpy = (ny), mcrt_tmpz = (nz); \
    double mcrt_tmpt; \
    NORM(mcrt_tmpx, mcrt_tmpy, mcrt_tmpz); \
    mcrt_tmpt=scalar_prod((rx),(ry),(rz),mcrt_tmpx,mcrt_tmpy,mcrt_tmpz); \
    (x) = rx -2 * mcrt_tmpt*mcrt_rmpx; \
    (y) = ry -2 * mcrt_tmpt*mcrt_rmpy; \
    (z) = rz -2 * mcrt_tmpt*mcrt_rmpz; \
  } while (0)

#pragma acc routine
Coords coords_set(MCNUM x, MCNUM y, MCNUM z);
#pragma acc routine
Coords coords_get(Coords a, MCNUM *x, MCNUM *y, MCNUM *z);
#pragma acc routine
Coords coords_add(Coords a, Coords b);
#pragma acc routine
Coords coords_sub(Coords a, Coords b);
#pragma acc routine
Coords coords_neg(Coords a);
#pragma acc routine
Coords coords_scale(Coords b, double scale);
#pragma acc routine
double coords_sp(Coords a, Coords b);
#pragma acc routine
Coords coords_xp(Coords b, Coords c);
#pragma acc routine
double coords_len(Coords a);
#pragma acc routine seq
void   coords_print(Coords a);
#pragma acc routine seq
mcstatic void coords_norm(Coords* c);

#pragma acc routine seq
void rot_set_rotation(Rotation t, double phx, double phy, double phz);
#pragma acc routine seq
int  rot_test_identity(Rotation t);
#pragma acc routine seq
void rot_mul(Rotation t1, Rotation t2, Rotation t3);
#pragma acc routine seq
void rot_copy(Rotation dest, Rotation src);
#pragma acc routine seq
void rot_transpose(Rotation src, Rotation dst);
#pragma acc routine seq
Coords rot_apply(Rotation t, Coords a);

#pragma acc routine seq
void mccoordschange(Coords a, Rotation t, _class_particle *particle);
#pragma acc routine seq
void mccoordschange_polarisation(Rotation t, double *sx, double *sy, double *sz);

double mcestimate_error(double N, double p1, double p2);
void mcreadparams(void);

/* this is now in mcstas-r.h and mcxtrace-r.h as the number of state parameters
is no longer equal */

_class_particle mcgenstate(void);

// trajectory/shape intersection routines
#pragma acc routine seq
int inside_rectangle(double, double, double, double);
#pragma acc routine seq
int box_intersect(double *dt_in, double *dt_out, double x, double y, double z,
      double vx, double vy, double vz, double dx, double dy, double dz);
#pragma acc routine seq
int cylinder_intersect(double *t0, double *t1, double x, double y, double z,
      double vx, double vy, double vz, double r, double h);
#pragma acc routine seq
int sphere_intersect(double *t0, double *t1, double x, double y, double z,
      double vx, double vy, double vz, double r);
// second order equation roots
#pragma acc routine seq
int solve_2nd_order(double *t1, double *t2,
      double A,  double B,  double C);

// random vector generation to shape
// defines silently introducing _particle as the last argument
#define randvec_target_circle(xo, yo, zo, solid_angle, xi, yi, zi, radius) \
  _randvec_target_circle(xo, yo, zo, solid_angle, xi, yi, zi, radius, _particle)
#define randvec_target_rect_angular(xo, yo, zo, solid_angle, xi, yi, zi, height, width, A) \
  _randvec_target_rect_angular(xo, yo, zo, solid_angle, xi, yi, zi, height, width, A, _particle)
#define randvec_target_rect_real(xo, yo, zo, solid_angle, xi, yi, zi, height, width, A, lx, ly, lz, order) \
  _randvec_target_rect_real(xo, yo, zo, solid_angle, xi, yi, zi, height, width, A, lx, ly, lz, order, _particle)
// defines forwarding to "inner" functions
#define randvec_target_sphere randvec_target_circle
#define randvec_target_rect(p0,p1,p2,p3,p4,p5,p6,p7,p8,p9) \
  randvec_target_rect_real(p0,p1,p2,p3,p4,p5,p6,p7,p8,p9,0,0,0,1)
// headers for randvec
#pragma acc routine seq
void _randvec_target_circle(double *xo, double *yo, double *zo,
  double *solid_angle, double xi, double yi, double zi, double radius,
  _class_particle* _particle);
#pragma acc routine seq
void _randvec_target_rect_angular(double *xo, double *yo, double *zo,
  double *solid_angle, double xi, double yi, double zi, double height,
  double width, Rotation A,
  _class_particle* _particle);
#pragma acc routine seq
void _randvec_target_rect_real(double *xo, double *yo, double *zo, double *solid_angle,
  double xi, double yi, double zi, double height, double width, Rotation A,
  double lx, double ly, double lz, int order,
  _class_particle* _particle);


// this is the main()
int mccode_main(int argc, char *argv[]);


#endif /* !MCCODE_H */

#ifndef MCCODE_R_IO_H
#define MCCODE_R_IO_H "$Revision$"

#if (USE_NEXUS == 0)
#undef USE_NEXUS
#endif

#ifndef CHAR_BUF_LENGTH
#define CHAR_BUF_LENGTH 1024
#endif


/* I/O section part ========================================================= */

/* ========================================================================== */

/*                               MCCODE_R_IO_C                                */

/* ========================================================================== */


/* main DETECTOR structure which stores most information to write to data files */
struct mcdetector_struct {
  char   filename[CHAR_BUF_LENGTH];   /* file name of monitor */
  double Position[3];                 /* position of detector component*/
  char   position[CHAR_BUF_LENGTH];   /* position of detector component (string)*/
  Rotation Rotation;                  /* position of detector component*/
  char   options[CHAR_BUF_LENGTH];    /* Monitor_nD style list-mode'options' (string)*/
  char   component[CHAR_BUF_LENGTH];  /* component instance name */
  char   nexuscomp[CHAR_BUF_LENGTH];  /* component naming in NeXus/HDF case */
  char   instrument[CHAR_BUF_LENGTH]; /* instrument name */
  char   type[CHAR_BUF_LENGTH];       /* data type, e.g. 0d, 1d, 2d, 3d */
  char   user[CHAR_BUF_LENGTH];       /* user name, e.g. HOME */
  char   date[CHAR_BUF_LENGTH];       /* date of simulation end/write time */
  char   title[CHAR_BUF_LENGTH];      /* title of detector */
  char   xlabel[CHAR_BUF_LENGTH];     /* X axis label */
  char   ylabel[CHAR_BUF_LENGTH];     /* Y axis label */
  char   zlabel[CHAR_BUF_LENGTH];     /* Z axis label */
  char   xvar[CHAR_BUF_LENGTH];       /* X variable name */
  char   yvar[CHAR_BUF_LENGTH];       /* Y variable name */
  char   zvar[CHAR_BUF_LENGTH];       /* Z variable name */
  char   ncount[CHAR_BUF_LENGTH];     /* number of events initially generated */
  char   limits[CHAR_BUF_LENGTH];     /* X Y Z limits, e.g. [xmin xmax ymin ymax zmin zmax] */
  char   variables[CHAR_BUF_LENGTH];  /* variables written into data block */
  char   statistics[CHAR_BUF_LENGTH]; /* center, mean and half width along axis */
  char   signal[CHAR_BUF_LENGTH];     /* min max and mean of signal (data block) */
  char   values[CHAR_BUF_LENGTH];     /* integrated values e.g. [I I_err N] */
  double xmin,xmax;                   /* min max of axes */
  double ymin,ymax;
  double zmin,zmax;
  double intensity;                   /* integrated values for data block */
  double error;
  double events;
  double min;                         /* statistics for data block */
  double max;
  double mean;
  double centerX;                     /* statistics for axes */
  double halfwidthX;
  double centerY;
  double halfwidthY;
  int    rank;                        /* dimensionaly of monitor, e.g. 0 1 2 3 */
  char   istransposed;                /* flag to transpose matrix for some formats */

  long   m,n,p;                       /* dimensions of data block and along axes */
  long   date_l;                      /* same as date, but in sec since 1970 */

  double *p0, *p1, *p2;               /* pointers to saved data, NULL when freed */
  char   format[CHAR_BUF_LENGTH];    /* format for file generation */
};

typedef struct mcdetector_struct MCDETECTOR;

static   char *dirname             = NULL;      /* name of output directory */
static   char *siminfo_name        = "mccode";  /* default output sim file name */
char    *mcformat                    = NULL;      /* NULL (default) or a specific format */

/* file I/O definitions and function prototypes */

#ifndef MC_EMBEDDED_RUNTIME /* the mcstatic variables (from mccode-r.c) */
extern FILE * siminfo_file;     /* handle to the output siminfo file */
extern int    mcgravitation;      /* flag to enable gravitation */
extern int    mcdotrace;          /* flag to print MCDISPLAY messages */
#else
mcstatic FILE *siminfo_file        = NULL;
#endif

/* I/O function prototypes ================================================== */

// from msysgit: https://code.google.com/p/msysgit/source/browse/compat/strcasestr.c
char *strcasestr(const char *haystack, const char *needle);

/* output functions */
MCDETECTOR mcdetector_out_0D(char *t, double p0, double p1, double p2, char *c, Coords pos, Rotation rot, int index);
MCDETECTOR mcdetector_out_1D(char *t, char *xl, char *yl,
                  char *xvar, double x1, double x2, long n,
                  double *p0, double *p1, double *p2, char *f, char *c, Coords pos, Rotation rot, int index);
MCDETECTOR mcdetector_out_2D(char *t, char *xl, char *yl,
                  double x1, double x2, double y1, double y2, long m,
                  long n, double *p0, double *p1, double *p2, char *f,
                  char *c, Coords pos, Rotation rot, int index);
MCDETECTOR mcdetector_out_list(char *t, char *xl, char *yl,
                  long m, long n,
                  double *p1, char *f,
	          char *c, Coords posa, Rotation rot,char* options, int index);

/* wrappers to output functions, that automatically set NAME and POSITION */
#define DETECTOR_OUT(p0,p1,p2) mcdetector_out_0D(NAME_CURRENT_COMP,p0,p1,p2,NAME_CURRENT_COMP,POS_A_CURRENT_COMP,ROT_A_CURRENT_COMP,INDEX_CURRENT_COMP)
#define DETECTOR_OUT_0D(t,p0,p1,p2) mcdetector_out_0D(t,p0,p1,p2,NAME_CURRENT_COMP,POS_A_CURRENT_COMP,ROT_A_CURRENT_COMP,INDEX_CURRENT_COMP)
#define DETECTOR_OUT_1D(t,xl,yl,xvar,x1,x2,n,p0,p1,p2,f) \
     mcdetector_out_1D(t,xl,yl,xvar,x1,x2,n,p0,p1,p2,f,NAME_CURRENT_COMP,POS_A_CURRENT_COMP,ROT_A_CURRENT_COMP,INDEX_CURRENT_COMP)
#define DETECTOR_OUT_2D(t,xl,yl,x1,x2,y1,y2,m,n,p0,p1,p2,f) \
     mcdetector_out_2D(t,xl,yl,x1,x2,y1,y2,m,n,p0,p1,p2,f,NAME_CURRENT_COMP,POS_A_CURRENT_COMP,ROT_A_CURRENT_COMP,INDEX_CURRENT_COMP)

#ifdef USE_NEXUS
#include "napi.h"
NXhandle nxhandle;
#endif

#endif /* ndef MCCODE_R_IO_H */

#endif /* MCCODE_R_H */
/* End of file "mccode-r.h". */

/* embedding file "mcstas-r.h" */

/*******************************************************************************
*
* McStas, neutron ray-tracing package
*         Copyright (C) 1997-2009, All rights reserved
*         Risoe National Laboratory, Roskilde, Denmark
*         Institut Laue Langevin, Grenoble, France
*
* Runtime: share/mcstas-r.h
*
* %Identification
* Written by: KN
* Date:    Aug 29, 1997
* Release: McStas X.Y
* Version: $Revision$
*
* Runtime system header for McStas.
*
* In order to use this library as an external library, the following variables
* and macros must be declared (see details in the code)
*
*   struct mcinputtable_struct mcinputtable[];
*   int mcnumipar;
*   char instrument_name[], instrument_source[];
*   int traceenabled, defaultmain;
*   extern MCNUM  mccomp_storein[];
*   extern MCNUM  instrument.counter_AbsorbProp[];
*   extern MCNUM  mcScattered;
*   #define MCCODE_STRING "the McStas version"
*
* Usage: Automatically embbeded in the c code.
*
* $Id$
*
*******************************************************************************/

#ifndef MCSTAS_R_H
#define MCSTAS_R_H "$Revision$"

/* Following part is only embedded when not redundent with mcstas.h */

#ifndef MCCODE_H

#define AA2MS    629.622368        /* Convert k[1/AA] to v[m/s] */
#define MS2AA    1.58825361e-3     /* Convert v[m/s] to k[1/AA] */
#define K2V      AA2MS
#define V2K      MS2AA
#define Q2V      AA2MS
#define V2Q      MS2AA
#define SE2V     437.393377        /* Convert sqrt(E)[meV] to v[m/s] */
#define VS2E     5.22703725e-6     /* Convert (v[m/s])**2 to E[meV] */

#define SCATTER0 do {DEBUG_SCATTER(); SCATTERED++;} while(0)
#define SCATTER SCATTER0

#define JUMPTOCOMP(comp) mcneutron->_index = INDEX_COMP(comp);

#define MAGNET_ON \
  do { \
    mcMagnet = 1; \
  } while(0)

#define MAGNET_OFF \
  do { \
    mcMagnet = 0; \
  } while(0)

#define ALLOW_BACKPROP \
  do { \
    allow_backprop = 1; \
  } while(0)

#define DISALLOW_BACKPROP \
  do { \
    allow_backprop = 0; \
  } while(0)

#define PROP_MAGNET(dt) \
  do { \
  } while (0)
    /* change coordinates from local system to magnet system */
/*    Rotation rotLM, rotTemp; \
      Coords   posLM = coords_sub(POS_A_CURRENT_COMP, mcMagnetPos); \
      rot_transpose(ROT_A_CURRENT_COMP, rotTemp); \
      rot_mul(rotTemp, mcMagnetRot, rotLM); \
      mcMagnetPrecession(x, y, z, t, vx, vy, vz, \
               &sx, &sy, &sz, dt, posLM, rotLM); \
      } while(0)
*/

#define mcPROP_DT(dt) \
  do { \
    if (mcMagnet && dt > 0) PROP_MAGNET(dt);\
    x += vx*(dt); \
    y += vy*(dt); \
    z += vz*(dt); \
    t += (dt); \
    if (isnan(p) || isinf(p)) { ABSORB; }\
  } while(0)

/* ADD: E. Farhi, Aug 6th, 2001 PROP_GRAV_DT propagation with acceleration */
#define PROP_GRAV_DT(dt, Ax, Ay, Az) \
  do { \
    if(dt < 0 && allow_backprop == 0) { ABSORB; }\
    if (mcMagnet) /*printf("Spin precession gravity\n")*/; \
    x  += vx*(dt) + (Ax)*(dt)*(dt)/2; \
    y  += vy*(dt) + (Ay)*(dt)*(dt)/2; \
    z  += vz*(dt) + (Az)*(dt)*(dt)/2; \
    vx += (Ax)*(dt); \
    vy += (Ay)*(dt); \
    vz += (Az)*(dt); \
    t  += (dt); \
    DISALLOW_BACKPROP;\
  } while(0)


#define PROP_DT(dt) \
  do { \
    if(dt < 0 && allow_backprop == 0) { RESTORE=1; ABSORB; }; \
    if (mcgravitation) { Coords mcLocG; double mc_gx, mc_gy, mc_gz; \
    mcLocG = rot_apply(ROT_A_CURRENT_COMP, coords_set(0,-GRAVITY,0)); \
    coords_get(mcLocG, &mc_gx, &mc_gy, &mc_gz); \
    PROP_GRAV_DT(dt, mc_gx, mc_gy, mc_gz); } \
    else mcPROP_DT(dt); \
    DISALLOW_BACKPROP;\
  } while(0)


#define PROP_Z0 \
  do { \
    if (mcgravitation) { Coords mcLocG; int mc_ret; \
    double mc_dt, mc_gx, mc_gy, mc_gz; \
    mcLocG = rot_apply(ROT_A_CURRENT_COMP, coords_set(0,-GRAVITY,0)); \
    coords_get(mcLocG, &mc_gx, &mc_gy, &mc_gz); \
    mc_ret = solve_2nd_order(&mc_dt, NULL, -mc_gz/2, -vz, -z); \
    if (mc_ret) {PROP_GRAV_DT(mc_dt, mc_gx, mc_gy, mc_gz); z=0;}\
    else if (allow_backprop == 0 && mc_dt < 0) { ABSORB; }; } \
    else mcPROP_Z0; \
    DISALLOW_BACKPROP;\
  } while(0)

#define mcPROP_Z0 \
  do { \
    double mc_dt; \
    if(vz == 0) { ABSORB; }; \
    mc_dt = -z/vz; \
    if(mc_dt < 0 && allow_backprop == 0) { ABSORB; }; \
    mcPROP_DT(mc_dt); \
    z = 0; \
    DISALLOW_BACKPROP;\
  } while(0)

#define PROP_X0 \
  do { \
    if (mcgravitation) { Coords mcLocG; int mc_ret; \
    double mc_dt, mc_gx, mc_gy, mc_gz; \
    mcLocG = rot_apply(ROT_A_CURRENT_COMP, coords_set(0,-GRAVITY,0)); \
    coords_get(mcLocG, &mc_gx, &mc_gy, &mc_gz); \
    mc_ret = solve_2nd_order(&mc_dt, NULL, -mc_gx/2, -vx, -x); \
    if (mc_ret) {PROP_GRAV_DT(mc_dt, mc_gx, mc_gy, mc_gz); x=0;}\
    else if (allow_backprop == 0 && mc_dt < 0) { ABSORB; }; } \
    else mcPROP_X0; \
    DISALLOW_BACKPROP;\
  } while(0)

#define mcPROP_X0 \
  do { \
    double mc_dt; \
    if(vx == 0) { ABSORB; }; \
    mc_dt = -x/vx; \
    if(mc_dt < 0 && allow_backprop == 0) { ABSORB; }; \
    mcPROP_DT(mc_dt); \
    x = 0; \
    DISALLOW_BACKPROP;\
  } while(0)

#define PROP_Y0 \
  do { \
    if (mcgravitation) { Coords mcLocG; int mc_ret; \
    double mc_dt, mc_gx, mc_gy, mc_gz; \
    mcLocG = rot_apply(ROT_A_CURRENT_COMP, coords_set(0,-GRAVITY,0)); \
    coords_get(mcLocG, &mc_gx, &mc_gy, &mc_gz); \
    mc_ret = solve_2nd_order(&mc_dt, NULL, -mc_gy/2, -vy, -y); \
    if (mc_ret) {PROP_GRAV_DT(mc_dt, mc_gx, mc_gy, mc_gz); y=0;}\
    else if (allow_backprop == 0 && mc_dt < 0) { ABSORB; }; } \
    else mcPROP_Y0; \
    DISALLOW_BACKPROP;\
  } while(0)


#define mcPROP_Y0 \
  do { \
    double mc_dt; \
    if(vy == 0) { ABSORB; }; \
    mc_dt = -y/vy; \
    if(mc_dt < 0 && allow_backprop == 0) { ABSORB; }; \
    mcPROP_DT(mc_dt); \
    y = 0; \
    DISALLOW_BACKPROP; \
  } while(0)


#ifdef DEBUG

#define DEBUG_STATE() if(!mcdotrace); else \
  printf("STATE: %g, %g, %g, %g, %g, %g, %g, %g, %g, %g, %g\n", \
         x,y,z,vx,vy,vz,t,sx,sy,sz,p);
#define DEBUG_SCATTER() if(!mcdotrace); else \
  printf("SCATTER: %g, %g, %g, %g, %g, %g, %g, %g, %g, %g, %g\n", \
         x,y,z,vx,vy,vz,t,sx,sy,sz,p);

#else

#define DEBUG_STATE()
#define DEBUG_SCATTER()

#endif

#endif /* !MCCODE_H */

#endif /* MCSTAS_R_H */
/* End of file "mcstas-r.h". */

/* embedding file "mccode-r.c" */

/*******************************************************************************
*
* McCode, neutron/xray ray-tracing package
*         Copyright (C) 1997-2009, All rights reserved
*         Risoe National Laboratory, Roskilde, Denmark
*         Institut Laue Langevin, Grenoble, France
*
* Runtime: share/mccode-r.c
*
* %Identification
* Written by: KN
* Date:    Aug 29, 1997
* Release: McStas X.Y/McXtrace X.Y
* Version: $Revision$
*
* Runtime system for McStas and McXtrace.
* Embedded within instrument in runtime mode.
* Contains SECTIONS:
*   MPI handling (sum, send, recv)
*   format definitions
*   I/O
*   mcdisplay support
*   random numbers
*   coordinates handling
*   vectors math (solve 2nd order, normals, randvec...)
*   parameter handling
*   signal and main handlers
*
* Usage: Automatically embbeded in the c code whenever required.
*
* $Id$
*
*******************************************************************************/

/*******************************************************************************
* The I/O format definitions and functions
*******************************************************************************/


/** Include header files to avoid implicit declarations (not allowed on LLVM) */
#include <ctype.h>
#include <sys/types.h>
#include <dirent.h>
#include <errno.h>

// UNIX specific headers (non-Windows)
#if defined(__unix__) || defined(__APPLE__)
#include <unistd.h>
#include <sys/stat.h>
#endif


#ifndef DANSE
#ifdef MC_ANCIENT_COMPATIBILITY
int traceenabled = 0;
int defaultmain  = 0;
#endif
/* else defined directly in the McCode generated C code */

static   long mcseed                 = 0; /* seed for random generator */
#pragma acc declare create ( mcseed )
static   long mcstartdate            = 0; /* start simulation time */
static   int  mcdisable_output_files = 0; /* --no-output-files */
mcstatic int  mcgravitation          = 0; /* use gravitation flag, for PROP macros */
mcstatic int  mcusedefaults          = 0; /* assume default value for all parameters */
mcstatic int  mcappend               = 0; /* flag to allow append mode on datasets/directories */
mcstatic int  mcdotrace              = 0; /* flag for --trace and messages for DISPLAY */
mcstatic int  mcnexus_embed_idf      = 0; /* flag to embed xml-formatted IDF file for Mantid */
#pragma acc declare create ( mcdotrace )
int      mcallowbackprop             = 0;         /* flag to enable negative/backprop */

/* OpenACC-related segmentation parameters: */
int vecsize = 128;
int numgangs = 7813;
long gpu_innerloop = 2147483647;

/* Monitor_nD list/buffer-size default */
/* Starting value may be defined using -DND_BUFFER=N */
/* Can further be controlled dynamically using --bufsiz input */
long MONND_BUFSIZ = 10000000;
#ifdef ND_BUFFER
MONND_BUFSIZ = ND_BUFFER;
#endif
 

/* Number of particle histories to simulate. */
#ifdef NEUTRONICS
mcstatic unsigned long long int mcncount             = 1;
mcstatic unsigned long long int mcrun_num            = 0;
#else
#ifdef MCDEFAULT_NCOUNT
mcstatic unsigned long long int mcncount             = MCDEFAULT_NCOUNT;
#else
mcstatic unsigned long long int mcncount             = 1000000;
#endif
#pragma acc declare create ( mcncount )
mcstatic unsigned long long int mcrun_num            = 0;
#pragma acc declare create ( mcrun_num )
#endif /* NEUTRONICS */

#else
#include "mcstas-globals.h"
#endif /* !DANSE */

#ifndef NX_COMPRESSION
#define NX_COMPRESSION NX_COMP_NONE
#endif

/* String nullification on GPU and other replacements */
#ifdef OPENACC
int noprintf() {
  return 0;
}

int str_comp(char *str1, char *str2) {
  while (*str1 && *str1 == *str2) {
    str1++;
    str2++;
  }
  return (*str1 - *str2);
}

size_t str_len(const char *s)
{
  size_t len = 0;
  if(s != NULL)
  {
    while(*s != '\0')
    {
      ++len;
      ++s;
    }
  }
  return len;
}

#endif

/* SECTION: Predefine (component) parameters ================================= */

int nans_match(double a, double b){
  return (*(uint64_t*)&a == *(uint64_t*)&b);
}
int is_unset(double x){
  return nans_match(x, UNSET);
}
int is_set(double x){
  return !nans_match(x, UNSET);
}
int is_valid(double x){
  return !isnan(x)||is_unset(x);
}
int all_unset(int n, ...){
  va_list ptr;
  va_start(ptr, n);
  int ret=1;
  for (int i=0; i<n; ++i) if(is_set(va_arg(ptr, double))) ret=0;
  va_end(ptr);
  return ret;
}
int all_set(int n, ...){
  va_list ptr;
  va_start(ptr, n);
  int ret=1;
  for (int i=0; i<n; ++i) if(is_unset(va_arg(ptr, double))) ret=0;
  va_end(ptr);
  return ret;
}
int any_unset(int n, ...){
  va_list ptr;
  va_start(ptr, n);
  int ret=0;
  for (int i=0; i<n; ++i) if(is_unset(va_arg(ptr, double))) ret=1;
  va_end(ptr);
  return ret;
}
int any_set(int n, ...){
  va_list ptr;
  va_start(ptr, n);
  int ret=0;
  for (int i=0; i<n; ++i) if(is_set(va_arg(ptr, double))) ret=1;
  va_end(ptr);
  return ret;
}


/* SECTION: Dynamic Arrays ================================================== */
IArray1d create_iarr1d(int n){
  IArray1d arr1d;
  arr1d = calloc(n, sizeof(int));
  if (!arr1d) {
    fprintf(stderr, "Error allocating IArray1d of dimension %i\n",n);
    exit(-1);
  }
  return arr1d;
}

void destroy_iarr1d(IArray1d a){
  free(a);
}

IArray2d create_iarr2d(int nx, int ny){
  IArray2d arr2d;
  arr2d = calloc(nx, sizeof(int *));
  if (!arr2d) {
    fprintf(stderr, "Error allocating IArray2d of dimension %i x %i\n",nx,ny);
    exit(-1);
  }

  int *p1;
  p1 = calloc(nx*ny, sizeof(int));

  if (!p1) {
    fprintf(stderr, "Error allocating int* array of dimension %i\n",nx*ny);
    exit(-1);
  }
  
  int i;
  for (i=0; i<nx; i++){
    arr2d[i] = &(p1[i*ny]);
  }
  return arr2d;
}

void destroy_iarr2d(IArray2d a){
  free(a[0]);
  free(a);
}

IArray3d create_iarr3d(int nx, int ny, int nz){
  IArray3d arr3d;
  int i, j;

  // 1d
  arr3d = calloc(nx, sizeof(int **));
  if (!arr3d) {
    fprintf(stderr, "Error allocating IArray3d of dimension %i x %i x %i\n",nx,ny,nz);
    exit(-1);
  }

  // d2
  int **p1;
  p1 = calloc(nx*ny, sizeof(int *));

  if (!p1) {
    fprintf(stderr, "Error allocating int** array of dimension %i\n",nx*ny);
    exit(-1);
  }
  
  for (i=0; i<nx; i++){
    arr3d[i] = &(p1[i*ny]);
  }

  // 3d
  int *p2;
  p2 = calloc(nx*ny*nz, sizeof(int));
  if (!p2) {
    fprintf(stderr, "Error allocating int* array of dimension %i\n",nx*ny*nz);
    exit(-1);
  }
  for (i=0; i<nx; i++){
    for (j=0; j<ny; j++){
      arr3d[i][j] = &(p2[(i*ny+j)*nz]);
    }
  }
  return arr3d;
}

void destroy_iarr3d(IArray3d a){
  free(a[0][0]);
  free(a[0]);
  free(a);
}

DArray1d create_darr1d(int n){
  DArray1d arr1d;
  arr1d = calloc(n, sizeof(double));
  if (!arr1d) {
    fprintf(stderr, "Error allocating DArray1d of dimension %i\n",n);
    exit(-1);
  }
  return arr1d;
}

void destroy_darr1d(DArray1d a){
  free(a);
}

DArray2d create_darr2d(int nx, int ny){
  DArray2d arr2d;
  arr2d = calloc(nx, sizeof(double *));
  if (!arr2d) {
    fprintf(stderr, "Error allocating DArray2d of dimension %i x %i\n",nx,ny);
    exit(-1);
  }
  double *p1;
  p1 = calloc(nx*ny, sizeof(double));
  if (!p1) {
    fprintf(stderr, "Error allocating double* array of dimension %i\n",nx*ny);
    exit(-1);
  }
  int i;
  for (i=0; i<nx; i++){
    arr2d[i] = &(p1[i*ny]);
  }
  return arr2d;
}

void destroy_darr2d(DArray2d a){
  free(a[0]);
  free(a);
}

DArray3d create_darr3d(int nx, int ny, int nz){
  DArray3d arr3d;

  int i, j;

  // 1d
  arr3d = calloc(nx, sizeof(double **));
  if (!arr3d) {
    fprintf(stderr, "Error allocating DArray3d of dimension %i x %i x %i\n",nx,ny,nz);
    exit(-1);
  }
  // d2
  double **p1;
  p1 = calloc(nx*ny, sizeof(double *));
  if (!p1) {
    fprintf(stderr, "Error allocating double** array of dimension %i\n",nx*ny);
    exit(-1);
  }
  for (i=0; i<nx; i++){
    arr3d[i] = &(p1[i*ny]);
  }

  // 3d
  double *p2;
  p2 = calloc(nx*ny*nz, sizeof(double));
  if (!p2) {
    fprintf(stderr, "Error allocating double* array of dimension %i\n",nx*ny*nz);
    exit(-1);
  }
  for (i=0; i<nx; i++){
    for (j=0; j<ny; j++){
      arr3d[i][j] = &(p2[(i*ny+j)*nz]);
    }
  }
  return arr3d;
}

void destroy_darr3d(DArray3d a){
  free(a[0][0]);
  free(a[0]);
  free(a);
}


/* SECTION: MPI handling ==================================================== */

#ifdef USE_MPI
/* MPI rank */
static int mpi_node_rank;
static int mpi_node_root = 0;


/*******************************************************************************
* mc_MPI_Reduce: Gathers arrays from MPI nodes using Reduce function.
*******************************************************************************/
int mc_MPI_Sum(double *sbuf, long count)
{
  if (!sbuf || count <= 0) return(MPI_SUCCESS); /* nothing to reduce */
  else {
    /* we must cut the buffer into blocks not exceeding the MPI max buffer size of 32000 */
    long   offset=0;
    double *rbuf=NULL;
    int    length=MPI_REDUCE_BLOCKSIZE; /* defined in mccode-r.h */
    int    i=0;
    rbuf = calloc(count, sizeof(double));
    if (!rbuf)
      exit(-fprintf(stderr, "Error: Out of memory %zi (mc_MPI_Sum)\n", count*sizeof(double)));
    while (offset < count) {
      if (!length || offset+length > count-1) length=count-offset;
      else length=MPI_REDUCE_BLOCKSIZE;
      if (MPI_Allreduce((double*)(sbuf+offset), (double*)(rbuf+offset),
              length, MPI_DOUBLE, MPI_SUM, MPI_COMM_WORLD) != MPI_SUCCESS)
        return MPI_ERR_COUNT;
      offset += length;
    }

    for (i=0; i<count; i++) sbuf[i] = rbuf[i];
    free(rbuf);
  }
  return MPI_SUCCESS;
} /* mc_MPI_Sum */

/*******************************************************************************
* mc_MPI_Send: Send array to MPI node by blocks to avoid buffer limit
*******************************************************************************/
int mc_MPI_Send(void *sbuf,
                  long count, MPI_Datatype dtype,
                  int dest)
{
  int dsize;
  long offset=0;
  int  tag=1;
  int  length=MPI_REDUCE_BLOCKSIZE; /* defined in mccode-r.h */

  if (!sbuf || count <= 0) return(MPI_SUCCESS); /* nothing to send */
  MPI_Type_size(dtype, &dsize);

  while (offset < count) {
    if (offset+length > count-1) length=count-offset;
    else length=MPI_REDUCE_BLOCKSIZE;
    if (MPI_Send((void*)((char*)sbuf+offset*dsize), length, dtype, dest, tag++, MPI_COMM_WORLD) != MPI_SUCCESS)
      return MPI_ERR_COUNT;
    offset += length;
  }

  return MPI_SUCCESS;
} /* mc_MPI_Send */

/*******************************************************************************
* mc_MPI_Recv: Receives arrays from MPI nodes by blocks to avoid buffer limit
*             the buffer must have been allocated previously.
*******************************************************************************/
int mc_MPI_Recv(void *sbuf,
                  long count, MPI_Datatype dtype,
                  int source)
{
  int dsize;
  long offset=0;
  int  tag=1;
  int  length=MPI_REDUCE_BLOCKSIZE; /* defined in mccode-r.h */

  if (!sbuf || count <= 0) return(MPI_SUCCESS); /* nothing to recv */
  MPI_Type_size(dtype, &dsize);

  while (offset < count) {
    if (offset+length > count-1) length=count-offset;
    else length=MPI_REDUCE_BLOCKSIZE;
    if (MPI_Recv((void*)((char*)sbuf+offset*dsize), length, dtype, source, tag++,
            MPI_COMM_WORLD, MPI_STATUS_IGNORE) != MPI_SUCCESS)
      return MPI_ERR_COUNT;
    offset += length;
  }

  return MPI_SUCCESS;
} /* mc_MPI_Recv */

#endif /* USE_MPI */

/* SECTION: parameters handling ============================================= */

/* Instrument input parameter type handling. */
/*******************************************************************************
* mcparm_double: extract double value from 's' into 'vptr'
*******************************************************************************/
static int
mcparm_double(char *s, void *vptr)
{
  char *p;
  double *v = (double *)vptr;

  if (!s) { *v = 0; return(1); }
  *v = strtod(s, &p);
  if(*s == '\0' || (p != NULL && *p != '\0') || errno == ERANGE)
    return 0;                        /* Failed */
  else
    return 1;                        /* Success */
}

/*******************************************************************************
* mcparminfo_double: display parameter type double
*******************************************************************************/
static char *
mcparminfo_double(char *parmname)
{
  return "double";
}

/*******************************************************************************
* mcparmerror_double: display error message when failed extract double
*******************************************************************************/
static void
mcparmerror_double(char *parm, char *val)
{
  fprintf(stderr, "Error: Invalid value '%s' for floating point parameter %s (mcparmerror_double)\n",
          val, parm);
}

/*******************************************************************************
* mcparmprinter_double: convert double to string
*******************************************************************************/
static void
mcparmprinter_double(char *f, void *vptr)
{
  double *v = (double *)vptr;
  sprintf(f, "%g", *v);
}

/*******************************************************************************
* mcparm_int: extract int value from 's' into 'vptr'
*******************************************************************************/
static int
mcparm_int(char *s, void *vptr)
{
  char *p;
  int *v = (int *)vptr;
  long x;

  if (!s) { *v = 0; return(1); }
  *v = 0;
  x = strtol(s, &p, 10);
  if(x < INT_MIN || x > INT_MAX)
    return 0;                        /* Under/overflow */
  *v = x;
  if(*s == '\0' || (p != NULL && *p != '\0') || errno == ERANGE)
    return 0;                        /* Failed */
  else
    return 1;                        /* Success */
}

/*******************************************************************************
* mcparminfo_int: display parameter type int
*******************************************************************************/
static char *
mcparminfo_int(char *parmname)
{
  return "int";
}

/*******************************************************************************
* mcparmerror_int: display error message when failed extract int
*******************************************************************************/
static void
mcparmerror_int(char *parm, char *val)
{
  fprintf(stderr, "Error: Invalid value '%s' for integer parameter %s (mcparmerror_int)\n",
          val, parm);
}

/*******************************************************************************
* mcparmprinter_int: convert int to string
*******************************************************************************/
static void
mcparmprinter_int(char *f, void *vptr)
{
  int *v = (int *)vptr;
  sprintf(f, "%d", *v);
}

/*******************************************************************************
* mcparm_string: extract char* value from 's' into 'vptr' (copy)
*******************************************************************************/
static int
mcparm_string(char *s, void *vptr)
{
  char **v = (char **)vptr;
  if (!s) { *v = NULL; return(1); }
  *v = (char *)malloc(strlen(s) + 1);
  if(*v == NULL)
  {
    exit(-fprintf(stderr, "Error: Out of memory %li (mcparm_string).\n", (long)strlen(s) + 1));
  }
  strcpy(*v, s);
  return 1;                        /* Success */
}

/*******************************************************************************
* mcparminfo_string: display parameter type string
*******************************************************************************/
static char *
mcparminfo_string(char *parmname)
{
  return "string";
}

/*******************************************************************************
* mcparmerror_string: display error message when failed extract string
*******************************************************************************/
static void
mcparmerror_string(char *parm, char *val)
{
  fprintf(stderr, "Error: Invalid value '%s' for string parameter %s (mcparmerror_string)\n",
          val, parm);
}

/*******************************************************************************
* mcparmprinter_string: convert string to string (including esc chars)
*******************************************************************************/
static void
mcparmprinter_string(char *f, void *vptr)
{
  char **v = (char **)vptr;
  char *p;

  if (!*v) { *f='\0'; return; }
  strcpy(f, "");
  for(p = *v; *p != '\0'; p++)
  {
    switch(*p)
    {
      case '\n':
        strcat(f, "\\n");
        break;
      case '\r':
        strcat(f, "\\r");
        break;
      case '"':
        strcat(f, "\\\"");
        break;
      case '\\':
        strcat(f, "\\\\");
        break;
      default:
        strncat(f, p, 1);
    }
  }
  /* strcat(f, "\""); */
} /* mcparmprinter_string */

/* now we may define the parameter structure, using previous functions */
static struct
  {
    int (*getparm)(char *, void *);
    char * (*parminfo)(char *);
    void (*error)(char *, char *);
    void (*printer)(char *, void *);
} mcinputtypes[] = {
  {
    mcparm_int, mcparminfo_int, mcparmerror_int,
    mcparmprinter_int
  }, {
    mcparm_string, mcparminfo_string, mcparmerror_string,
    mcparmprinter_string
  }, {
    mcparm_string, mcparminfo_string, mcparmerror_string,
    mcparmprinter_string
  }, {
    mcparm_double, mcparminfo_double, mcparmerror_double,
    mcparmprinter_double
  }, {
    mcparm_double, mcparminfo_double, mcparmerror_double,
    mcparmprinter_double
  }
};

/*******************************************************************************
* mcestimate_error: compute sigma from N,p,p2 in Gaussian large numbers approx
*******************************************************************************/
double mcestimate_error(double N, double p1, double p2)
{
  double pmean, n1;
  if(N <= 1)
    return p1;
  pmean = p1 / N;
  n1 = N - 1;
  /* Note: underflow may cause p2 to become zero; the fabs() below guards
     against this. */
  return sqrt((N/n1)*fabs(p2 - pmean*pmean));
}

double (*mcestimate_error_p)
  (double V2, double psum, double p2sum)=mcestimate_error;

/* ========================================================================== */

/*                               MCCODE_R_IO_C                                */

/* ========================================================================== */

#ifndef MCCODE_R_IO_C
#define MCCODE_R_IO_C "$Revision$"

/* SECTION: file i/o handling ================================================ */

#ifndef HAVE_STRCASESTR
// from msysgit: https://code.google.com/p/msysgit/source/browse/compat/strcasestr.c
char *strcasestr(const char *haystack, const char *needle)
{
  int nlen = strlen(needle);
  int hlen = strlen(haystack) - nlen + 1;
  int i;

  for (i = 0; i < hlen; i++) {
    int j;
    for (j = 0; j < nlen; j++) {
            unsigned char c1 = haystack[i+j];
            unsigned char c2 = needle[j];
            if (toupper(c1) != toupper(c2))
                    goto next;
    }
    return (char *) haystack + i;
  next:
    ;
  }
  return NULL;
}


#endif
#ifndef HAVE_STRCASECMP
int strcasecmp( const char *s1, const char *s2 )
{
  int c1, c2;
  do {
    c1 = tolower( (unsigned char) *s1++ );
    c2 = tolower( (unsigned char) *s2++ );
  } while (c1 == c2 && c1 != 0);
  return c2 > c1 ? -1 : c1 > c2;
}
#endif

#ifndef STRACPY
/* this is a replacement to strncpy, but ensures that the copy ends with NULL */
/* http://stracpy.blogspot.fr/2011/04/stracpy-strncpy-replacement.html */
#define STRACPY
char *stracpy(char *destination, const char *source, size_t amount)
{
        if (!destination || !source || !amount) return(NULL);
        while(amount--)
          if((*destination++ = *source++) == '\0') break;
        *destination = '\0';
        return destination;
}
#endif

/*******************************************************************************
* mcfull_file: allocates a full file name=dirname+file. Catenate extension if missing.
*******************************************************************************/
char *mcfull_file(char *name, char *ext)
{
  int   dirlen=0;
  char *mem   =NULL;

  dirlen = dirname ? strlen(dirname) : 0;
  mem = (char*)malloc(dirlen + strlen(name) + CHAR_BUF_LENGTH);
  if(!mem) {
    exit(-fprintf(stderr, "Error: Out of memory %li (mcfull_file)\n", (long)(dirlen + strlen(name) + 256)));
  }
  strcpy(mem, "");

  /* prepend directory name to path if name does not contain a path */
  if (dirlen > 0 && !strchr(name, MC_PATHSEP_C)) {
    strcat(mem, dirname);
    strcat(mem, MC_PATHSEP_S);
  } /* dirlen */

  strcat(mem, name);
  if (!strchr(name, '.') && ext && strlen(ext))
  { /* add extension if not in file name already */
    strcat(mem, ".");
    strcat(mem, ext);
  }
  return(mem);
} /* mcfull_file */

/*******************************************************************************
* mcnew_file: opens a new file within dirname if non NULL
*             the file is opened in "a" (append, create if does not exist)
*             the extension 'ext' is added if the file name does not include one.
*             the last argument is set to 0 if file did not exist, else to 1.
*******************************************************************************/
FILE *mcnew_file(char *name, char *ext, int *exists)
{
  char *mem;
  FILE *file=NULL;

  if (!name || strlen(name) == 0 || mcdisable_output_files) return(NULL);

  mem  = mcfull_file(name, ext); /* create dirname/name.ext */

  /* check for existence */
  file = fopen(mem, "r"); /* for reading -> fails if does not exist */
  if (file) {
    fclose(file);
    *exists=1;
  } else
    *exists=0;

  /* open the file for writing/appending */
#ifdef USE_NEXUS
  if (mcformat && strcasestr(mcformat, "NeXus")) {
    /* NXhandle nxhandle is defined in the .h with USE_NEXUS */
    NXaccess mode = (*exists ? NXACC_CREATE5 | NXACC_RDWR : NXACC_CREATE5);

    if (NXopen(mem, mode, &nxhandle) != NX_OK)
      file = NULL;
    else
      file = (FILE*)&nxhandle; /* to make it non NULL */
  } else
#endif
    file = fopen(mem, "a+");

  if(!file)
    fprintf(stderr, "Warning: could not open output file '%s' for %s (mcnew_file)\n",
      mem, *exists ? "append" : "create");
  free(mem);

  return file;
} /* mcnew_file */

/*******************************************************************************
* mcdetector_statistics: compute detector statistics, error bars, [x I I_err N] 1D
* RETURN:            updated detector structure
* Used by: detector_import
*******************************************************************************/
MCDETECTOR mcdetector_statistics(
  MCDETECTOR detector)
{

  if (!detector.p1 || !detector.m)
    return(detector);

  /* compute statistics and update MCDETECTOR structure ===================== */
  double sum_z  = 0, min_z  = 0, max_z  = 0;
  double fmon_x =0,  smon_x = 0, fmon_y =0, smon_y=0, mean_z=0;
  double Nsum=0, P2sum=0;

  double sum_xz = 0, sum_yz = 0, sum_x = 0, sum_y = 0, sum_x2z = 0, sum_y2z = 0;
  int    i,j;
  char   hasnan=0, hasinf=0;
  char   israw = ((char*)strcasestr(detector.format,"raw") != NULL);
  double *this_p1=NULL; /* new 1D McCode array [x I E N]. Freed after writing data */

  /* if McCode/PGPLOT and rank==1 we create a new m*4 data block=[x I E N] */
  if (detector.rank == 1 && strcasestr(detector.format,"McCode")) {
    this_p1 = (double *)calloc(detector.m*detector.n*detector.p*4, sizeof(double));
    if (!this_p1)
      exit(-fprintf(stderr, "Error: Out of memory creating %zi 1D " MCCODE_STRING " data set for file '%s' (detector_import)\n",
        detector.m*detector.n*detector.p*4*sizeof(double*), detector.filename));
  }

  max_z = min_z = detector.p1[0];

  /* compute sum and moments (not for lists) */
  if (!strcasestr(detector.format,"list") && detector.m)
  for(j = 0; j < detector.n*detector.p; j++)
  {
    for(i = 0; i < detector.m; i++)
    {
      double x,y,z;
      double N, E;
      long   index= !detector.istransposed ? i*detector.n*detector.p + j : i+j*detector.m;
      char   hasnaninf=0;

      if (detector.m)
        x = detector.xmin + (i + 0.5)/detector.m*(detector.xmax - detector.xmin);
      else x = 0;
      if (detector.n && detector.p)
        y = detector.ymin + (j + 0.5)/detector.n/detector.p*(detector.ymax - detector.ymin);
      else y = 0;
      z = detector.p1[index];
      N = detector.p0 ? detector.p0[index] : 1;
      E = detector.p2 ? detector.p2[index] : 0;
      if (detector.p2 && !israw)
        detector.p2[index] = (*mcestimate_error_p)(detector.p0[index],detector.p1[index],detector.p2[index]); /* set sigma */

      if (detector.rank == 1 && this_p1 && strcasestr(detector.format,"McCode")) {
        /* fill-in 1D McCode array [x I E N] */
        this_p1[index*4]   = x;
        this_p1[index*4+1] = z;
        this_p1[index*4+2] = detector.p2 ? detector.p2[index] : 0;
        this_p1[index*4+3] = N;
      }

      if (isnan(z) || isnan(E) || isnan(N)) hasnaninf=hasnan=1;
      if (isinf(z) || isinf(E) || isinf(N)) hasnaninf=hasinf=1;

      /* compute stats integrals */
      if (!hasnaninf) {
        sum_xz += x*z;
        sum_yz += y*z;
        sum_x  += x;
        sum_y  += y;
        sum_z  += z;
        sum_x2z += x*x*z;
        sum_y2z += y*y*z;
        if (z > max_z) max_z = z;
        if (z < min_z) min_z = z;

        Nsum += N;
        P2sum += E;
      }

    }
  } /* for j */

  /* compute 1st and 2nd moments. For lists, sum_z=0 so this is skipped. */
  if (sum_z && detector.n*detector.m*detector.p)
  {
    fmon_x = sum_xz/sum_z;
    fmon_y = sum_yz/sum_z;
    smon_x = sum_x2z/sum_z-fmon_x*fmon_x; smon_x = smon_x > 0 ? sqrt(smon_x) : 0;
    smon_y = sum_y2z/sum_z-fmon_y*fmon_y; smon_y = smon_y > 0 ? sqrt(smon_y) : 0;
    mean_z = sum_z/detector.n/detector.m/detector.p;
  }
  /* store statistics into detector */
  detector.intensity = sum_z;
  detector.error     = Nsum ? (*mcestimate_error_p)(Nsum, sum_z, P2sum) : 0;
  detector.events    = Nsum;
  detector.min       = min_z;
  detector.max       = max_z;
  detector.mean      = mean_z;
  detector.centerX   = fmon_x;
  detector.halfwidthX= smon_x;
  detector.centerY   = fmon_y;
  detector.halfwidthY= smon_y;

  /* if McCode/PGPLOT and rank==1 replace p1 with new m*4 1D McCode and clear others */
  if (detector.rank == 1 && this_p1 && strcasestr(detector.format,"McCode")) {

    detector.p1 = this_p1;
    detector.n  = detector.m; detector.m  = 4;
    detector.p0 = detector.p2 = NULL;
    detector.istransposed = 1;
  }

  if (detector.n*detector.m*detector.p > 1)
    snprintf(detector.signal, CHAR_BUF_LENGTH,
      "Min=%g; Max=%g; Mean=%g;", detector.min, detector.max, detector.mean);
  else
    strcpy(detector.signal, "None");
  snprintf(detector.values, CHAR_BUF_LENGTH,
    "%g %g %g", detector.intensity, detector.error, detector.events);

  switch (detector.rank) {
    case 1:  snprintf(detector.statistics, CHAR_BUF_LENGTH, "X0=%g; dX=%g;",
      detector.centerX, detector.halfwidthX); break;
    case 2:
    case 3:  snprintf(detector.statistics, CHAR_BUF_LENGTH, "X0=%g; dX=%g; Y0=%g; dY=%g;",
      detector.centerX, detector.halfwidthX, detector.centerY, detector.halfwidthY);
      break;
    default: strcpy(detector.statistics, "None");
  }

  if (hasnan)
    printf("WARNING: Nan detected in component/file %s %s\n",
      detector.component, strlen(detector.filename) ? detector.filename : "");
  if (hasinf)
    printf("WARNING: Inf detected in component/file %s %s\n",
      detector.component, strlen(detector.filename) ? detector.filename : "");

  return(detector);

} /* mcdetector_statistics */

/*******************************************************************************
* detector_import: build detector structure, merge non-lists from MPI
*                    compute basic stat, write "Detector:" line
* RETURN:            detector structure. Invalid data if detector.p1 == NULL
*                    Invalid detector sets m=0 and filename=""
*                    Simulation data  sets m=0 and filename=siminfo_name
* This function is equivalent to the old 'mcdetector_out', returning a structure
*******************************************************************************/
MCDETECTOR detector_import(
  char *format,
  char *component, char *title,
  long m, long n,  long p,
  char *xlabel, char *ylabel, char *zlabel,
  char *xvar, char *yvar, char *zvar,
  double x1, double x2, double y1, double y2, double z1, double z2,
  char *filename,
  double *p0, double *p1, double *p2,
  Coords position, Rotation rotation, int index)
{
  time_t t;       /* for detector.date */
  long   date_l;  /* date as a long number */
  char   istransposed=0;
  char   c[CHAR_BUF_LENGTH]; /* temp var for signal label */

  MCDETECTOR detector;

  /* build MCDETECTOR structure ============================================= */
  /* make sure we do not have NULL for char fields */

  /* these also apply to simfile */
  strncpy (detector.filename,  filename ? filename : "",        CHAR_BUF_LENGTH);
  strncpy (detector.format,    format   ? format   : "McCode" , CHAR_BUF_LENGTH);
  /* add extension if missing */
  if (strlen(detector.filename) && !strchr(detector.filename, '.'))
  { /* add extension if not in file name already */
    strcat(detector.filename, ".dat");
  }
  strncpy (detector.component, component ? component : MCCODE_STRING " component", CHAR_BUF_LENGTH);
  #ifdef USE_NEXUS
  char pref[5];
  if (index-1 < 10) {
    sprintf(pref,"000");
  } else if (index-1 < 100) {
    sprintf(pref,"00");
  } else if (index-1 < 1000) {
    sprintf(pref,"0");
  } else if (index-1 < 10000) {
    sprintf(pref,"");
  } else {
    fprintf(stderr,"Error, no support for > 10000 comps at the moment!\n");
    exit(-1);
  }
  sprintf(detector.nexuscomp,"%s%d_%s",pref,index-1,detector.component);
  #endif

  snprintf(detector.instrument, CHAR_BUF_LENGTH, "%s (%s)", instrument_name, instrument_source);
  snprintf(detector.user, CHAR_BUF_LENGTH,      "%s on %s",
        getenv("USER") ? getenv("USER") : MCCODE_NAME,
        getenv("HOST") ? getenv("HOST") : "localhost");
  time(&t);         /* get current write time */
  date_l = (long)t; /* same but as a long */
  snprintf(detector.date, CHAR_BUF_LENGTH, "%s", ctime(&t));
  if (strlen(detector.date))   detector.date[strlen(detector.date)-1] = '\0'; /* remove last \n in date */
  detector.date_l = date_l;

  if (!mcget_run_num() || mcget_run_num() >= mcget_ncount())
    snprintf(detector.ncount, CHAR_BUF_LENGTH, "%llu", mcget_ncount()
#ifdef USE_MPI
*mpi_node_count
#endif
  );
  else
    snprintf(detector.ncount, CHAR_BUF_LENGTH, "%g/%g", (double)mcget_run_num(), (double)mcget_ncount());

  detector.p0         = p0;
  detector.p1         = p1;
  detector.p2         = p2;

  /* handle transposition (not for NeXus) */
  if (!strcasestr(detector.format, "NeXus")) {
    if (m<0 || n<0 || p<0)             istransposed = !istransposed;
    if (strcasestr(detector.format, "transpose")) istransposed = !istransposed;
    if (istransposed) { /* do the swap once for all */
      long i=m; m=n; n=i;
    }
  }

  m=labs(m); n=labs(n); p=labs(p); /* make sure dimensions are positive */
  detector.istransposed = istransposed;

  /* determine detector rank (dimensionality) */
  if (!m || !n || !p || !p1) detector.rank = 4; /* invalid: exit with m=0 filename="" */
  else if (m*n*p == 1)       detector.rank = 0; /* 0D */
  else if (n == 1 || m == 1) detector.rank = 1; /* 1D */
  else if (p == 1)           detector.rank = 2; /* 2D */
  else                       detector.rank = 3; /* 3D */

  /* from rank, set type */
  switch (detector.rank) {
    case 0:  strcpy(detector.type,  "array_0d"); m=n=p=1; break;
    case 1:  snprintf(detector.type, CHAR_BUF_LENGTH, "array_1d(%ld)", m*n*p); m *= n*p; n=p=1; break;
    case 2:  snprintf(detector.type, CHAR_BUF_LENGTH, "array_2d(%ld, %ld)", m, n*p); n *= p; p=1; break;
    case 3:  snprintf(detector.type, CHAR_BUF_LENGTH, "array_3d(%ld, %ld, %ld)", m, n, p); break;
    default: m=0; strcpy(detector.type, ""); strcpy(detector.filename, "");/* invalid */
  }

  detector.m    = m;
  detector.n    = n;
  detector.p    = p;

  /* these only apply to detector files ===================================== */

  detector.Position[0]=position.x;
  detector.Position[1]=position.y;
  detector.Position[2]=position.z;
  rot_copy(detector.Rotation,rotation);
  snprintf(detector.position, CHAR_BUF_LENGTH, "%g %g %g", position.x, position.y, position.z);
  /* may also store actual detector orientation in the future */

  strncpy(detector.title,      title && strlen(title) ? title : component,       CHAR_BUF_LENGTH);
  strncpy(detector.xlabel,     xlabel && strlen(xlabel) ? xlabel : "X", CHAR_BUF_LENGTH); /* axis labels */
  strncpy(detector.ylabel,     ylabel && strlen(ylabel) ? ylabel : "Y", CHAR_BUF_LENGTH);
  strncpy(detector.zlabel,     zlabel && strlen(zlabel) ? zlabel : "Z", CHAR_BUF_LENGTH);
  strncpy(detector.xvar,       xvar && strlen(xvar) ? xvar :       "x", CHAR_BUF_LENGTH); /* axis variables */
  strncpy(detector.yvar,       yvar && strlen(yvar) ? yvar :       detector.xvar, CHAR_BUF_LENGTH);
  strncpy(detector.zvar,       zvar && strlen(zvar) ? zvar :       detector.yvar, CHAR_BUF_LENGTH);

  /* set "variables" as e.g. "I I_err N" */
  strcpy(c, "I ");
  if (strlen(detector.zvar))      strncpy(c, detector.zvar,32);
  else if (strlen(detector.yvar)) strncpy(c, detector.yvar,32);
  else if (strlen(detector.xvar)) strncpy(c, detector.xvar,32);

  if (detector.rank == 1)
    snprintf(detector.variables, CHAR_BUF_LENGTH, "%s %s %s_err N", detector.xvar, c, c);
  else
    snprintf(detector.variables, CHAR_BUF_LENGTH, "%s %s_err N", c, c);

  /* limits */
  detector.xmin = x1;
  detector.xmax = x2;
  detector.ymin = y1;
  detector.ymax = y2;
  detector.zmin = z1;
  detector.zmax = z2;
  if (abs(detector.rank) == 1)
    snprintf(detector.limits, CHAR_BUF_LENGTH, "%g %g", x1, x2);
  else if (detector.rank == 2)
    snprintf(detector.limits, CHAR_BUF_LENGTH, "%g %g %g %g", x1, x2, y1, y2);
  else
    snprintf(detector.limits, CHAR_BUF_LENGTH, "%g %g %g %g %g %g", x1, x2, y1, y2, z1, z2);

  /* if MPI and nodes_nb > 1: reduce data sets when using MPI =============== */
#ifdef USE_MPI
  if (!strcasestr(detector.format,"list") && mpi_node_count > 1 && m) {
    /* we save additive data: reduce everything into mpi_node_root */
    if (p0) mc_MPI_Sum(p0, m*n*p);
    if (p1) mc_MPI_Sum(p1, m*n*p);
    if (p2) mc_MPI_Sum(p2, m*n*p);
    if (!p0) {  /* additive signal must be then divided by the number of nodes */
      int i;
      for (i=0; i<m*n*p; i++) {
        p1[i] /= mpi_node_count;
        if (p2) p2[i] /= mpi_node_count;
      }
    }
  }
#endif /* USE_MPI */

  /* compute statistics, Nsum, intensity, Error bars */
  detector = mcdetector_statistics(detector);

#ifdef USE_MPI
  /* slaves are done */
  if(mpi_node_rank != mpi_node_root) {
    return detector;
  }
#endif

  /* output "Detector:" line ================================================ */
  /* when this is a detector written by a component (not the SAVE from instrument),
     not an event lists */
  if (!m) return(detector);
  if (!strcasestr(detector.format,"list")) {
    if (!strcmp(detector.component, instrument_name)) {
      if (strlen(detector.filename))  /* we name it from its filename, or from its title */
        strncpy(c, detector.filename, CHAR_BUF_LENGTH);
      else
        snprintf(c, CHAR_BUF_LENGTH, "%s", instrument_name);
    } else
      strncpy(c, detector.component, CHAR_BUF_LENGTH);  /* usual detectors written by components */

    printf("Detector: %s_I=%g %s_ERR=%g %s_N=%g",
           c, detector.intensity,
           c, detector.error,
           c, detector.events);
    printf(" \"%s\"\n", strlen(detector.filename) ? detector.filename : detector.component);
  }


  return(detector);
} /* detector_import */

/* end MCDETECTOR import section ============================================ */

















/* ========================================================================== */

/*                               ASCII output                                 */
/*     The SIM file is YAML based, the data files have '#' headers            */

/* ========================================================================== */


/*******************************************************************************
* mcinfo_out: output instrument tags/info (only in SIM)
* Used in: siminfo_init (ascii), mcinfo(stdout)
*******************************************************************************/
static void mcinfo_out(char *pre, FILE *f)
{
  char Parameters[CHAR_BUF_LENGTH] = "";
  int  i;

  if (!f || mcdisable_output_files) return;

  /* create parameter string ================================================ */
  for(i = 0; i < numipar; i++)
  {
    char ThisParam[CHAR_BUF_LENGTH];
    if (strlen(mcinputtable[i].name) > CHAR_BUF_LENGTH) break;
    snprintf(ThisParam, CHAR_BUF_LENGTH, " %s(%s)", mcinputtable[i].name,
            (*mcinputtypes[mcinputtable[i].type].parminfo)
                (mcinputtable[i].name));
    if (strlen(Parameters) + strlen(ThisParam) + 1 >= CHAR_BUF_LENGTH) break;
    strcat(Parameters, ThisParam);
  }

  /* output data ============================================================ */
  if (f != stdout)
    fprintf(f, "%sFile: %s%c%s\n",    pre, dirname, MC_PATHSEP_C, siminfo_name);
  else
    fprintf(f, "%sCreator: %s\n",     pre, MCCODE_STRING);

  fprintf(f, "%sSource: %s\n",   pre, instrument_source);
  fprintf(f, "%sParameters: %s\n",    pre, Parameters);

  fprintf(f, "%sTrace_enabled: %s\n", pre, traceenabled ? "yes" : "no");
  fprintf(f, "%sDefault_main: %s\n",  pre, defaultmain ?  "yes" : "no");
#ifdef MC_EMBEDDED_RUNTIME
  fprintf(f, "%sEmbedded_runtime: %s\n", pre, "yes");
#else
  fprintf(f, "%sEmbedded_runtime: %s\n", pre, "no");
#endif

  fflush(f);
} /* mcinfo_out */

/*******************************************************************************
* mcruninfo_out: output simulation tags/info (both in SIM and data files)
* Used in: siminfo_init (ascii case), mcdetector_out_xD_ascii
*******************************************************************************/
static void mcruninfo_out(char *pre, FILE *f)
{
  int i;
  char Parameters[CHAR_BUF_LENGTH];

  if (!f || mcdisable_output_files) return;

  fprintf(f, "%sFormat: %s%s\n",      pre,
    mcformat && strlen(mcformat) ? mcformat : MCCODE_NAME,
    mcformat && strcasestr(mcformat,"McCode") ? " with text headers" : "");
  fprintf(f, "%sURL: %s\n",         pre, "http://www.mccode.org");
  fprintf(f, "%sCreator: %s\n",     pre, MCCODE_STRING);
  fprintf(f, "%sInstrument: %s\n", pre, instrument_source);
  fprintf(f, "%sNcount: %llu\n",        pre, mcget_ncount());
  fprintf(f, "%sTrace: %s\n",       pre, mcdotrace ? "yes" : "no");
  fprintf(f, "%sGravitation: %s\n", pre, mcgravitation ? "yes" : "no");
  snprintf(Parameters, CHAR_BUF_LENGTH, "%ld", mcseed);
  fprintf(f, "%sSeed: %s\n",        pre, Parameters);
  fprintf(f, "%sDirectory: %s\n",        pre, dirname ? dirname : ".");
#ifdef USE_MPI
  if (mpi_node_count > 1)
    fprintf(f, "%sNodes: %i\n",        pre, mpi_node_count);
#endif

  // TODO Consider replacing this by a a call to `mcparameterinfo_out(pre+"Param: ", f)`
  /* output parameter string ================================================ */
  for(i = 0; i < numipar; i++) {
      if (mcinputtable[i].par){
	/* Parameters with a default value */
	if(mcinputtable[i].val && strlen(mcinputtable[i].val)){
	  (*mcinputtypes[mcinputtable[i].type].printer)(Parameters, mcinputtable[i].par);
	  fprintf(f, "%sParam: %s=%s\n", pre, mcinputtable[i].name, Parameters);
        /* ... and those without */
	}else{
	  fprintf(f, "%sParam: %s=NULL\n", pre, mcinputtable[i].name);
	}
      }
  }
  fflush(f);
} /* mcruninfo_out */

/*******************************************************************************
 * @brief Print parameter information to the specified file
 * @param pre any beginning-of-line padding
 * @param f the output file
 */
static void mcparameterinfo_out(char * pre, FILE *f){
  if (!f || mcdisable_output_files) return;

  unsigned int nchar = 4;
  for (int i=0; i < numipar; ++i){
    if (mcinputtable[i].par && mcinputtable[i].val && strlen(mcinputtable[i].val) > nchar)
      nchar = strlen(mcinputtable[i].val);
  }
  char * buffer = calloc(nchar+1, sizeof(char));

  if (!buffer) {
    exit(1);
  }

  for (int i=0; i < numipar; ++i) {
    if (mcinputtable[i].par) {
      char * name = mcinputtable[i].name;
      if (mcinputtable[i].val && strlen(mcinputtable[i].val)) {
        mcinputtypes[mcinputtable[i].type].printer(buffer, mcinputtable[i].par);
      } else {
        strcpy(buffer, "NULL");
      }
      if (strlen(mcinputtable[i].unit)){
        //fprintf(f, "%s%s %s (\"%s\") = %s\n", pre, mcinputtypes[mcinputtable[i].type].parminfo(name), name, mcinputtable[i].unit, buffer);
        fprintf(f, "%s%s %s/\"%s\" = %s\n", pre, mcinputtypes[mcinputtable[i].type].parminfo(name), name, mcinputtable[i].unit, buffer);
      } else {
        fprintf(f, "%s%s %s = %s\n", pre, mcinputtypes[mcinputtable[i].type].parminfo(name), name, buffer);
      }
    }
  }

  free(buffer);
}

/*******************************************************************************
* siminfo_out:    wrapper to fprintf(siminfo_file)
*******************************************************************************/
void siminfo_out(char *format, ...)
{
  va_list ap;

  if(siminfo_file && !mcdisable_output_files)
  {
    va_start(ap, format);
    vfprintf(siminfo_file, format, ap);
    va_end(ap);
  }
} /* siminfo_out */


/*******************************************************************************
* mcdatainfo_out: output detector header
*   mcdatainfo_out(prefix, file_handle, detector) writes info to data file
*******************************************************************************/
static void
mcdatainfo_out(char *pre, FILE *f, MCDETECTOR detector)
{
  if (!f || !detector.m || mcdisable_output_files) return;

  /* output data ============================================================ */
  fprintf(f, "%sDate: %s (%li)\n",       pre, detector.date, detector.date_l);
  fprintf(f, "%stype: %s\n",       pre, detector.type);
  fprintf(f, "%sSource: %s\n",     pre, detector.instrument);
  fprintf(f, "%scomponent: %s\n",  pre, detector.component);
  fprintf(f, "%sposition: %s\n",   pre, detector.position);

  fprintf(f, "%stitle: %s\n",      pre, detector.title);
  fprintf(f, !mcget_run_num() || mcget_run_num() >= mcget_ncount() ?
             "%sNcount: %s\n" :
             "%sratio: %s\n",  pre, detector.ncount);

  if (strlen(detector.filename)) {
    fprintf(f, "%sfilename: %s\n", pre, detector.filename);
  }

  fprintf(f, "%sstatistics: %s\n", pre, detector.statistics);
  fprintf(f, "%ssignal: %s\n",     pre, detector.signal);
  fprintf(f, "%svalues: %s\n",     pre, detector.values);

  if (detector.rank >= 1)
  {
    fprintf(f, "%sxvar: %s\n",     pre, detector.xvar);
    fprintf(f, "%syvar: %s\n",     pre, detector.yvar);
    fprintf(f, "%sxlabel: %s\n",   pre, detector.xlabel);
    fprintf(f, "%sylabel: %s\n",   pre, detector.ylabel);
    if (detector.rank > 1) {
      fprintf(f, "%szvar: %s\n",   pre, detector.zvar);
      fprintf(f, "%szlabel: %s\n", pre, detector.zlabel);
    }
  }

  fprintf(f,
    abs(detector.rank)==1 ?
             "%sxlimits: %s\n" :
             "%sxylimits: %s\n", pre, detector.limits);
  fprintf(f, "%svariables: %s\n", pre,
    strcasestr(detector.format, "list") ? detector.ylabel : detector.variables);

  fflush(f);

} /* mcdatainfo_out */

/* mcdetector_out_array_ascii: output a single array to a file
 *   m: columns
 *   n: rows
 *   p: array
 *   f: file handle (already opened)
 */
static void mcdetector_out_array_ascii(long m, long n, double *p, FILE *f, char istransposed)
{
  if(f)
  {
    int i,j;
    for(j = 0; j < n; j++)
    {
      for(i = 0; i < m; i++)
      {
          fprintf(f, "%.10g ", p[!istransposed ? i*n + j : j*m+i]);
      }
      fprintf(f,"\n");
    }
  }
} /* mcdetector_out_array_ascii */

/*******************************************************************************
* mcdetector_out_0D_ascii: called by mcdetector_out_0D for ascii output
*******************************************************************************/
MCDETECTOR mcdetector_out_0D_ascii(MCDETECTOR detector)
{
  int exists=0;
  FILE *outfile = NULL;

  /* Write data set information to simulation description file. */
  MPI_MASTER(
    siminfo_out("\nbegin data\n"); // detector.component
    mcdatainfo_out("  ", siminfo_file, detector);
    siminfo_out("end data\n");
    /* Don't write if filename is NULL: mcnew_file handles this (return NULL) */
    outfile = mcnew_file(detector.component, "dat", &exists);
    if(outfile)
    {
      /* write data file header and entry in simulation description file */
      mcruninfo_out( "# ", outfile);
      mcdatainfo_out("# ", outfile, detector);
      /* write I I_err N */
      fprintf(outfile, "%g %g %g\n",
        detector.intensity, detector.error, detector.events);
      fclose(outfile);
    }
  ); /* MPI_MASTER */
  return(detector);
} /* mcdetector_out_0D_ascii */

/*******************************************************************************
* mcdetector_out_1D_ascii: called by mcdetector_out_1D for ascii output
*******************************************************************************/
MCDETECTOR mcdetector_out_1D_ascii(MCDETECTOR detector)
{
  int exists=0;
  FILE *outfile = NULL;

  MPI_MASTER(
    /* Write data set information to simulation description file. */
    siminfo_out("\nbegin data\n"); // detector.filename
    mcdatainfo_out("  ", siminfo_file, detector);
    siminfo_out("end data\n");
    /* Loop over array elements, writing to file. */
    /* Don't write if filename is NULL: mcnew_file handles this (return NULL) */
    outfile = mcnew_file(detector.filename, "dat", &exists);
    if(outfile)
    {
      /* write data file header and entry in simulation description file */
      mcruninfo_out( "# ", outfile);
      mcdatainfo_out("# ", outfile, detector);
      /* output the 1D array columns */
      mcdetector_out_array_ascii(detector.m, detector.n, detector.p1, outfile, detector.istransposed);

      fclose(outfile);
    }
  ); /* MPI_MASTER */
  return(detector);

}  /* mcdetector_out_1D_ascii */

/*******************************************************************************
* mcdetector_out_2D_ascii: called by mcdetector_out_2D for ascii output
*******************************************************************************/
MCDETECTOR mcdetector_out_2D_ascii(MCDETECTOR detector)
{
  int exists=0;
  FILE *outfile = NULL;

  MPI_MASTER(
    /* Loop over array elements, writing to file. */
    /* Don't write if filename is NULL: mcnew_file handles this (return NULL) */
    outfile = mcnew_file(detector.filename, "dat", &exists);
    if(outfile)
    {
      /* write header only if file has just been created (not appending) */
      if (!exists) {
        /* Write data set information to simulation description file. */
        siminfo_out("\nbegin data\n"); // detector.filename
        mcdatainfo_out("  ", siminfo_file, detector);
        siminfo_out("end data\n");

        mcruninfo_out( "# ", outfile);
        mcdatainfo_out("# ", outfile,   detector);
      }
      /* Add # Data entry for any write to the file (e.g. via -USR2, see GitHub issue #2174 ) */
      fprintf(outfile, "# Data [%s/%s] %s:\n", detector.component, detector.filename, detector.zvar);
      mcdetector_out_array_ascii(detector.m, detector.n*detector.p, detector.p1,
        outfile, detector.istransposed);
      if (detector.p2) {
        fprintf(outfile, "# Errors [%s/%s] %s_err:\n", detector.component, detector.filename, detector.zvar);
        mcdetector_out_array_ascii(detector.m, detector.n*detector.p, detector.p2,
          outfile, detector.istransposed);
      }
      if (detector.p0) {
        fprintf(outfile, "# Events [%s/%s] N:\n", detector.component, detector.filename);
        mcdetector_out_array_ascii(detector.m, detector.n*detector.p, detector.p0,
          outfile, detector.istransposed);
      }
      fclose(outfile);

      if (!exists) {
        if (strcasestr(detector.format, "list"))
          printf("Events:   \"%s\"\n",
            strlen(detector.filename) ? detector.filename : detector.component);
      }
    } /* if outfile */
  ); /* MPI_MASTER */
#ifdef USE_MPI
  if (strcasestr(detector.format, "list") && mpi_node_count > 1) {
    int node_i=0;
    /* loop along MPI nodes to write sequentially */
    for(node_i=0; node_i<mpi_node_count; node_i++) {
      /* MPI: slaves wait for the master to write its block, then append theirs */
      MPI_Barrier(MPI_COMM_WORLD);
      if (node_i != mpi_node_root && node_i == mpi_node_rank) {
        if(strlen(detector.filename) && !mcdisable_output_files)	/* Don't write if filename is NULL */
          outfile = mcnew_file(detector.filename, "dat", &exists);
        if (!exists)
          fprintf(stderr, "Warning: [MPI node %i] file '%s' does not exist yet, "
                          "MASTER should have opened it before.\n",
            mpi_node_rank, detector.filename);
        if(outfile) {
          mcdetector_out_array_ascii(detector.m, detector.n*detector.p, detector.p1,
            outfile, detector.istransposed);
          fclose(outfile);
        }
      }
    }
  } /* if strcasestr list */
#endif
  return(detector);
} /* mcdetector_out_2D_ascii */

/*******************************************************************************
* strcpy_valid: makes a valid string for variable names.
*   copy 'original' into 'valid', replacing invalid characters by '_'
*   char arrays must be pre-allocated
*******************************************************************************/
static char *strcpy_valid(char *valid, char *original)
{
  long i;
  int  n=CHAR_BUF_LENGTH; /* max length of valid names */

  if (original == NULL || !strlen(original)) return(NULL);

  if (n > strlen(original)) n = strlen(original);
  else original += strlen(original)-n;
  strncpy(valid, original, n);

  for (i=0; i < n; i++)
  {
    if ( (valid[i] > 122)
      || (valid[i] < 32)
      || (strchr("!\"#$%&'()*+,-.:;<=>?@[\\]^`/ \n\r\t", valid[i]) != NULL) )
    {
      if (i) valid[i] = '_'; else valid[i] = 'm';
    }
  }
  valid[i] = '\0';

  return(valid);
} /* strcpy_valid */

/* end ascii output section ================================================= */







#ifdef USE_NEXUS

/* ========================================================================== */

/*                               NeXus output                                 */

/* ========================================================================== */

#define nxprintf(...)    nxstr('d', __VA_ARGS__)
#define nxprintattr(...) nxstr('a', __VA_ARGS__)

/*******************************************************************************
* nxstr: output a tag=value data set (char) in NeXus/current group
*   when 'format' is larger that 1024 chars it is used as value for the 'tag'
*   else the value is assembled with format and following arguments.
*   type='d' -> data set
*        'a' -> attribute for current data set
*******************************************************************************/
static int nxstr(char type, NXhandle *f, char *tag, char *format, ...)
{
  va_list ap;
  char value[CHAR_BUF_LENGTH];
  int  i;
  int  ret=NX_OK;

  if (!tag || !format || !strlen(tag) || !strlen(format)) return(NX_OK);

  /* assemble the value string */
  if (strlen(format) < CHAR_BUF_LENGTH) {
    va_start(ap, format);
    ret = vsnprintf(value, CHAR_BUF_LENGTH, format, ap);
    va_end(ap);

    i = strlen(value);
  } else {
    i = strlen(format);
  }

  if (type == 'd') {
    /* open/put/close data set */
    if (NXmakedata (f, tag, NX_CHAR, 1, &i) != NX_OK) return(NX_ERROR);
    NXopendata (f, tag);
    if (strlen(format) < CHAR_BUF_LENGTH)
      ret = NXputdata  (f, value);
    else
      ret = NXputdata  (f, format);
    NXclosedata(f);
  } else {
    if (strlen(format) < CHAR_BUF_LENGTH)
      ret = NXputattr  (f, tag, value, strlen(value), NX_CHAR);
    else
      ret = NXputattr  (f, tag, format, strlen(format), NX_CHAR);
  }

  return(ret);

} /* nxstr */

/*******************************************************************************
* mcinfo_readfile: read a full file into a string buffer which is allocated
*   Think to free the buffer after use.
* Used in: mcinfo_out_nexus (nexus)
*******************************************************************************/
char *mcinfo_readfile(char *filename)
{
  FILE *f = fopen(filename, "rb");
  if (!f) return(NULL);
  fseek(f, 0, SEEK_END);
  long fsize = ftell(f);
  rewind(f);
  char *string = malloc(fsize + 1);
  if (string) {
    int n = fread(string, fsize, 1, f);
    fclose(f);

    string[fsize] = 0;
  }
  return(string);
}

/*******************************************************************************
* mcinfo_out: output instrument/simulation groups in NeXus file
* Used in: siminfo_init (nexus)
*******************************************************************************/
static void mcinfo_out_nexus(NXhandle f)
{
  FILE  *fid;     /* for intrument source code/C/IDF */
  char  *buffer=NULL;
  time_t t     =time(NULL); /* for date */
  char   entry0[CHAR_BUF_LENGTH];
  int    count=0;
  char   name[CHAR_BUF_LENGTH];
  char   class[CHAR_BUF_LENGTH];

  if (!f || mcdisable_output_files) return;

  /* write NeXus NXroot attributes */
  /* automatically added: file_name, HDF5_Version, file_time, NeXus_version */
  nxprintattr(f, "creator",   "%s generated with " MCCODE_STRING, instrument_name);

  /* count the number of existing NXentry and create the next one */
  NXgetgroupinfo(f, &count, name, class);
  sprintf(entry0, "entry%i", count+1);

  /* create the main NXentry (mandatory in NeXus) */
  if (NXmakegroup(f, entry0, "NXentry") == NX_OK)
  if (NXopengroup(f, entry0, "NXentry") == NX_OK) {
    nxprintf(nxhandle, "program_name", MCCODE_STRING);
    nxprintf(f, "start_time", ctime(&t));
    nxprintf(f, "title", "%s%s%s simulation generated by instrument %s",
      dirname && strlen(dirname) ? dirname : ".", MC_PATHSEP_S, siminfo_name,
      instrument_name);
    nxprintattr(f, "program_name", MCCODE_STRING);
    nxprintattr(f, "instrument",   instrument_name);
    nxprintattr(f, "simulation",   "%s%s%s",
        dirname && strlen(dirname) ? dirname : ".", MC_PATHSEP_S, siminfo_name);

    /* write NeXus instrument group */
    if (NXmakegroup(f, "instrument", "NXinstrument") == NX_OK)
    if (NXopengroup(f, "instrument", "NXinstrument") == NX_OK) {
      int   i;
      char *string=NULL;

      /* write NeXus parameters(types) data =================================== */
      string = (char*)malloc(CHAR_BUF_LENGTH);
      if (string) {
        strcpy(string, "");
        for(i = 0; i < numipar; i++)
        {
          char ThisParam[CHAR_BUF_LENGTH];
          snprintf(ThisParam, CHAR_BUF_LENGTH, " %s(%s)", mcinputtable[i].name,
                  (*mcinputtypes[mcinputtable[i].type].parminfo)
                      (mcinputtable[i].name));
          if (strlen(string) + strlen(ThisParam) < CHAR_BUF_LENGTH)
            strcat(string, ThisParam);
        }
        nxprintattr(f, "Parameters",    string);
        free(string);
      }

      nxprintattr(f, "name",          instrument_name);
      nxprintf   (f, "name",          instrument_name);
      nxprintattr(f, "Source",        instrument_source);

      nxprintattr(f, "Trace_enabled", traceenabled ? "yes" : "no");
      nxprintattr(f, "Default_main",  defaultmain ?  "yes" : "no");
#ifdef MC_EMBEDDED_RUNTIME
      nxprintattr(f, "Embedded_runtime", "yes");
#else
      nxprintattr(f, "Embedded_runtime", "no");
#endif

      /* add instrument source code when available */
      buffer = mcinfo_readfile(instrument_source);
      if (buffer && strlen(buffer)) {
        long length=strlen(buffer);
        nxprintf (f, "description", buffer);
        NXopendata(f,"description");
        nxprintattr(f, "file_name", instrument_source);
        nxprintattr(f, "file_size", "%li", length);
        nxprintattr(f, "MCCODE_STRING", MCCODE_STRING);
        NXclosedata(f);
        nxprintf (f,"instrument_source", "%s " MCCODE_NAME " " MCCODE_PARTICLE " Monte Carlo simulation", instrument_name);
        free(buffer);
      } else
        nxprintf (f, "description", "File %s not found (instrument description %s is missing)",
          instrument_source, instrument_name);

      if (mcnexus_embed_idf) {
        /* add Mantid/IDF.xml when available */
        char *IDFfile=NULL;
        IDFfile = (char*)malloc(CHAR_BUF_LENGTH);
        sprintf(IDFfile,"%s%s",instrument_source,".xml");
        buffer = mcinfo_readfile(IDFfile);
        if (buffer && strlen(buffer)) {
          NXmakegroup (nxhandle, "instrument_xml", "NXnote");
          NXopengroup (nxhandle, "instrument_xml", "NXnote");
          nxprintf(f, "data", buffer);
          nxprintf(f, "description", "IDF.xml file found with instrument %s", instrument_source);
          nxprintf(f, "type", "text/xml");
          NXclosegroup(f); /* instrument_xml */
          free(buffer);
        }
        free(IDFfile);
      }

      /* Add "components" entry */
      if (NXmakegroup(f, "components", "NXdata") == NX_OK) {
        NXopengroup(f, "components", "NXdata");
        nxprintattr(f, "description", "Component list for instrument %s",  instrument_name);
	NXclosegroup(f); /* components */
      } else {
	printf("Failed to create NeXus component hierarchy\n");
      }
      NXclosegroup(f); /* instrument */
    } /* NXinstrument */

    /* write NeXus simulation group */
    if (NXmakegroup(f, "simulation", "NXnote") == NX_OK)
    if (NXopengroup(f, "simulation", "NXnote") == NX_OK) {

      nxprintattr(f, "name",   "%s%s%s",
        dirname && strlen(dirname) ? dirname : ".", MC_PATHSEP_S, siminfo_name);

      nxprintf   (f, "name",      "%s",     siminfo_name);
      nxprintattr(f, "Format",    mcformat && strlen(mcformat) ? mcformat : MCCODE_NAME);
      nxprintattr(f, "URL",       "http://www.mccode.org");
      nxprintattr(f, "program",   MCCODE_STRING);
      nxprintattr(f, "Instrument",instrument_source);
      nxprintattr(f, "Trace",     mcdotrace ?     "yes" : "no");
      nxprintattr(f, "Gravitation",mcgravitation ? "yes" : "no");
      nxprintattr(f, "Seed",      "%li", mcseed);
      nxprintattr(f, "Directory", dirname);
    #ifdef USE_MPI
      if (mpi_node_count > 1)
        nxprintf(f, "Nodes", "%i",        mpi_node_count);
    #endif

      /* output parameter string ================================================ */
      if (NXmakegroup(f, "Param", "NXparameters") == NX_OK) {
	NXopengroup(f,"Param", "NXparameters");
        int i;
        char string[CHAR_BUF_LENGTH];
        for(i = 0; i < numipar; i++) {
          if (mcget_run_num() || (mcinputtable[i].val && strlen(mcinputtable[i].val))) {
            if (mcinputtable[i].par == NULL)
              strncpy(string, (mcinputtable[i].val ? mcinputtable[i].val : ""), CHAR_BUF_LENGTH);
            else
              (*mcinputtypes[mcinputtable[i].type].printer)(string, mcinputtable[i].par);

            nxprintf(f,  mcinputtable[i].name, "%s", string);
            nxprintattr(f, mcinputtable[i].name, string);
          }
        }
        NXclosegroup(f); /* Param */
      } /* NXparameters */
      NXclosegroup(f); /* simulation */
    } /* NXsimulation */

    /* create a group to hold all links for all monitors */
    NXmakegroup(f, "data", "NXdetector");

    /* leave the NXentry opened (closed at exit) */
  } /* NXentry */
} /* mcinfo_out_nexus */

/*******************************************************************************
* mccomp_placement_type_nexus:
*   Places
*    - absolute (3x1) position
*    - absolute (3x3) rotation
*    - type / class of component instance into attributes under
*     entry<N>/instrument/compname
*   requires: NXentry to be opened
*******************************************************************************/
static void mccomp_placement_type_nexus(NXhandle nxhandle, char* component, Coords position, Rotation rotation, char* comptype)
{
  /* open NeXus instrument group */

  #ifdef USE_NEXUS
  if(nxhandle) {
    if (NXopengroup(nxhandle, "instrument", "NXinstrument") == NX_OK) {
      if (NXopengroup(nxhandle, "components", "NXdata") == NX_OK) {
	if (NXmakegroup(nxhandle, component, "NXdata") == NX_OK) {
	  if (NXopengroup(nxhandle, component, "NXdata") == NX_OK) {
	    int64_t pdims[3]; pdims[0]=3; pdims[1]=0; pdims[2]=0;
	    if (NXcompmakedata64(nxhandle, "Position", NX_FLOAT64, 1, pdims, NX_COMPRESSION, pdims) == NX_OK) {
	      if (NXopendata(nxhandle, "Position") == NX_OK) {
		double pos[3]; coords_get(position, &pos[0], &pos[1], &pos[2]);
		if (NXputdata (nxhandle, pos) == NX_OK) {
		  NXclosedata(nxhandle);
		} else {
		  fprintf(stderr, "COULD NOT PUT Position field for component %s\n",component);
		}
	      } else {
		fprintf(stderr, "Warning: could not open Position field for component %s\n",component);
	      }
	    }
	    int64_t rdims[3]; rdims[0]=3; rdims[1]=3; rdims[2]=0;
	    if (NXcompmakedata64(nxhandle, "Rotation", NX_FLOAT64, 2, rdims, NX_COMPRESSION, rdims) == NX_OK) {
	      if (NXopendata(nxhandle, "Rotation") == NX_OK) {
		if (NXputdata (nxhandle, rotation) == NX_OK) {
		  NXclosedata(nxhandle);
		} else {
		  fprintf(stderr, "COULD NOT PUT Rotation field for component %s\n",component);
		}
	      } else {
		fprintf(stderr, "Warning: could not open Rotation field for component %s\n",component);
	      }
	    }
	    nxprintf(nxhandle, "Component_type", comptype);
	    NXclosegroup(nxhandle); // component
	  } else {
	    printf("FAILED to open comp data group %s\n",component);
	  }
	} else {
	  printf("FAILED to create comp data group %s\n",component);
	}
	NXclosegroup(nxhandle); // components
      } else {
	printf("Failed to open NeXus component hierarchy\n");
      }
      NXclosegroup(nxhandle); // instrument
    } else {
      printf("Failed to open NeXus instrument hierarchy\n");
    }
  } else {
    fprintf(stderr,"NO NEXUS FILE\n");
  }
  #endif
} /* mccomp_placement_nexus */

/*******************************************************************************
* mccomp_param_nexus:
*   Output parameter/value pair for component instance into
*   the attribute
*     entry<N>/instrument/compname/parameter
*   requires: NXentry to be opened
*******************************************************************************/
static void mccomp_param_nexus(NXhandle nxhandle, char* component, char* parameter, char* defval, char* value, char* type)
{
  /* open NeXus instrument group */

  #ifdef USE_NEXUS
  if(nxhandle) {
    if (NXopengroup(nxhandle, "instrument", "NXinstrument") == NX_OK) {
      if (NXopengroup(nxhandle, "components", "NXdata") == NX_OK) {
	if (NXopengroup(nxhandle, component, "NXdata") == NX_OK) {
	  NXMDisableErrorReporting(); /* inactivate NeXus error messages, as creation may fail */
	  NXmakegroup(nxhandle, "parameters", "NXdata");
	  NXMEnableErrorReporting();  /* re-enable NeXus error messages */
	  if (NXopengroup(nxhandle, "parameters", "NXdata") == NX_OK) {
	    NXmakegroup(nxhandle, parameter, "NXnote");
	    if (NXopengroup(nxhandle, parameter, "NXnote") == NX_OK) {
	      nxprintattr(nxhandle, "type", type);
	      nxprintattr(nxhandle, "default",  defval);
	      nxprintattr(nxhandle, "value",  value);
	      NXclosegroup(nxhandle); // parameter
	    } else {
	      printf("FAILED to open parameters %s data group \n",parameter);
	    }
	    NXclosegroup(nxhandle); // "parameters"
	  } else {
	    printf("FAILED to open comp/parameters data group \n");
	  }
	  NXclosegroup(nxhandle); // component
	  } else {
	  printf("FAILED to open comp data group %s\n",component);
	}
	NXclosegroup(nxhandle); // components
      } else {
	printf("Failed to open NeXus component hierarchy\n");
      }
      NXclosegroup(nxhandle); // instrument
    } else {
      printf("Failed to open NeXus instrument hierarchy\n");
    }
  } else {
    fprintf(stderr,"NO NEXUS FILE\n");
  }
#endif
} /* mccomp_param_nexus */

/*******************************************************************************
* mcdatainfo_out_nexus: output detector header
*   mcdatainfo_out_nexus(detector) create group and write info to NeXus data file
*   open data:NXdetector then filename:NXdata and write headers/attributes
*   requires: NXentry to be opened
*******************************************************************************/
static void
mcdatainfo_out_nexus(NXhandle f, MCDETECTOR detector)
{
  char data_name[CHAR_BUF_LENGTH];
  if (!f || !detector.m || mcdisable_output_files) return;

  strcpy_valid(data_name,
    strlen(detector.filename) ?
      detector.filename : detector.component);

  /* the NXdetector group has been created in mcinfo_out_nexus (siminfo_init) */
  if (NXopengroup(f, "instrument", "NXinstrument") == NX_OK) {
    if (NXopengroup(f, "components", "NXdata") == NX_OK) {
      NXMDisableErrorReporting(); /* inactivate NeXus error messages, as creation may fail */
      NXmakegroup(f, detector.nexuscomp, "NXdata");
      if (NXopengroup(f, detector.nexuscomp, "NXdata") == NX_OK) {
	NXmakegroup(f, "output", "NXdetector");
	if (NXopengroup(f, "output", "NXdetector") == NX_OK) {
	  if (NXmakegroup(f, data_name, "NXdata") == NX_OK) {
	    if (NXopengroup(f, data_name, "NXdata") == NX_OK) {
	      /* output metadata (as attributes) ======================================== */
	      nxprintattr(f, "Date",       detector.date);
	      nxprintattr(f, "type",       detector.type);
	      nxprintattr(f, "Source",     detector.instrument);
	      nxprintattr(f, "component",  detector.component);
	      nxprintattr(f, "position",   detector.position);

	      nxprintattr(f, "title",      detector.title);
	      nxprintattr(f, !mcget_run_num() || mcget_run_num() >= mcget_ncount() ?
			  "Ncount" :
			  "ratio",  detector.ncount);

	      if (strlen(detector.filename)) {
		nxprintattr(f, "filename", detector.filename);
	      }

	      nxprintattr(f, "statistics", detector.statistics);
	      nxprintattr(f, "signal",     detector.signal);
	      nxprintattr(f, "values",     detector.values);

	      if (detector.rank >= 1)
		{
		  nxprintattr(f, "xvar",     detector.xvar);
		  nxprintattr(f, "yvar",     detector.yvar);
		  nxprintattr(f, "xlabel",   detector.xlabel);
		  nxprintattr(f, "ylabel",   detector.ylabel);
		  if (detector.rank > 1) {
		    nxprintattr(f, "zvar",   detector.zvar);
		    nxprintattr(f, "zlabel", detector.zlabel);
		  }
		}

	      nxprintattr(f, abs(detector.rank)==1 ?
			  "xlimits" :
			  "xylimits", detector.limits);
	      nxprintattr(f, "variables",
			  strcasestr(detector.format, "list") ? detector.ylabel : detector.variables);

	      NXclosegroup(f); // data_name
	    }
	  }
	}
	NXclosegroup(f); // output
	NXclosegroup(f); // detector.nexuscomp
      }
      NXclosegroup(f); // components
    }
    NXMEnableErrorReporting();  /* re-enable NeXus error messages */
    NXclosegroup(f); // instrument
  } /* NXdetector (instrument) */ 
} /* mcdatainfo_out_nexus */

/*******************************************************************************
* mcdetector_out_axis_nexus: write detector axis into current NXdata
*   requires: NXdata to be opened
*******************************************************************************/
int mcdetector_out_axis_nexus(NXhandle f, char *label, char *var, int rank, long length, double min, double max)
{
  if (!f || length <= 1 || mcdisable_output_files || max == min) return(NX_OK);
  else {
    double *axis;
    axis=malloc(sizeof(double)*length);
    if (!axis ) {
      printf("Fatal memory error allocating NeXus axis of length %li, exiting!\n", length);
      return(NX_ERROR);
    }
    char *valid;
    valid=malloc(sizeof(char)*CHAR_BUF_LENGTH);
    if (!valid ) {
      printf("Fatal memory error allocating label axis of length %li, exiting!\n", CHAR_BUF_LENGTH);
      free(axis);
      return(NX_ERROR);
    }
    int dim=(int)length;
    int i;
    int nprimary=1;
    /* create an axis from [min:max] */
    for(i = 0; i < length; i++)
      axis[i] = min+(max-min)*(i+0.5)/length;
    /* create the data set */
    strcpy_valid(valid, label);
    NXcompmakedata(f, valid, NX_FLOAT64, 1, &dim, NX_COMPRESSION, &dim);
    /* open it */
    if (NXopendata(f, valid) != NX_OK) {
      fprintf(stderr, "Warning: could not open axis rank %i '%s' (NeXus)\n",
        rank, valid);
      free(axis);
      free(valid);
      return(NX_ERROR);
    }
    /* put the axis and its attributes */
    NXputdata  (f, axis);
    nxprintattr(f, "long_name",  label);
    nxprintattr(f, "short_name", var);
    NXputattr  (f, "axis",       &rank,     1, NX_INT32);
    nxprintattr(f, "units",      var);
    NXputattr  (f, "primary",    &nprimary, 1, NX_INT32);
    NXclosedata(f);
    free(axis);
    free(valid);
    return(NX_OK);
  }
} /* mcdetector_out_axis_nexus */

/*******************************************************************************
* mcdetector_out_array_nexus: write detector array into current NXdata (1D,2D)
*   requires: NXdata to be opened
*******************************************************************************/
int mcdetector_out_array_nexus(NXhandle f, char *part, double *data, MCDETECTOR detector)
{

  int64_t dims[3]={detector.m,detector.n,detector.p};  /* number of elements to write */
  int64_t fulldims[3]={detector.m,detector.n,detector.p};
  int signal=1;
  int exists=0;
  int64_t current_dims[3]={0,0,0};
  int ret=NX_OK;

  if (!f || !data || !detector.m || mcdisable_output_files) return(NX_OK);

  /* when this is a list, we set 1st dimension to NX_UNLIMITED for creation */
  if (strcasestr(detector.format, "list")) fulldims[0] = NX_UNLIMITED;

  /* create the data set in NXdata group */
  NXMDisableErrorReporting(); /* inactivate NeXus error messages, as creation may fail */
  ret = NXcompmakedata64(f, part, NX_FLOAT64, detector.rank, fulldims, NX_COMPRESSION, dims);
  if (ret != NX_OK) {
    /* failed: data set already exists */
    int datatype=0;
    int rank=0;
    exists=1;
    /* inquire current size of data set (nb of events stored) */
    NXopendata(f, part);
    NXgetinfo64(f, &rank, current_dims, &datatype);
    NXclosedata(f);
  }
  NXMEnableErrorReporting();  /* re-enable NeXus error messages */

  /* open the data set */
  if (NXopendata(f, part) == NX_ERROR) {
    fprintf(stderr, "Warning: could not open DataSet %s '%s' (NeXus)\n",
      part, detector.title);
    return(NX_ERROR);
  }
  if (strcasestr(detector.format, "list")) {
    current_dims[1] = current_dims[2] = 0; /* set starting location for writing slab */
    NXputslab64(f, data, current_dims, dims);
    if (!exists)
      printf("Events:   \"%s\"\n",
        strlen(detector.filename) ? detector.filename : detector.component);
    else
      printf("Append:   \"%s\"\n",
	     strlen(detector.filename) ? detector.filename : detector.component);
  } else {
    NXputdata (f, data);
  }

  if (strstr(part,"data") || strstr(part, "events")) {
    NXputattr(f, "signal", &signal, 1, NX_INT32);
    nxprintattr(f, "short_name", strlen(detector.filename) ?
      detector.filename : detector.component);
  }
  nxprintattr(f, "long_name", "%s '%s'", part, detector.title);
  NXclosedata(f);

  return(NX_OK);
} /* mcdetector_out_array_nexus */

/*******************************************************************************
* mcdetector_out_data_nexus: write detector axes+data into current NXdata
*   The data:NXdetector is opened, then filename:NXdata
*   requires: NXentry to be opened
*******************************************************************************/
int mcdetector_out_data_nexus(NXhandle f, MCDETECTOR detector)
{
  char data_name[CHAR_BUF_LENGTH];

  if (!f || !detector.m || mcdisable_output_files) return(NX_OK);

  strcpy_valid(data_name,
    strlen(detector.filename) ?
      detector.filename : detector.component);
  NXlink pLink;
  /* the NXdetector group has been created in mcinfo_out_nexus (siminfo_init) */
  if (NXopengroup(f, "instrument", "NXinstrument") == NX_OK) {
    if (NXopengroup(f, "components", "NXdata") == NX_OK) {
      if (NXopengroup(f, detector.nexuscomp, "NXdata") == NX_OK) {
	if (NXopengroup(f, "output", "NXdetector") == NX_OK) {

	  /* the NXdata group has been created in mcdatainfo_out_nexus */
	  if (NXopengroup(f, data_name, "NXdata") == NX_OK) {
	    
	    MPI_MASTER(
		       nxprintattr(f, "options",
				   strlen(detector.options) ? detector.options : "None");
		       );
	    /* write axes, for histogram data sets, not for lists */
	    if (!strcasestr(detector.format, "list")) {
	      mcdetector_out_axis_nexus(f, detector.xlabel, detector.xvar,
					1, detector.m, detector.xmin, detector.xmax);
	      mcdetector_out_axis_nexus(f, detector.ylabel, detector.yvar,
					2, detector.n, detector.ymin, detector.ymax);
	      mcdetector_out_axis_nexus(f, detector.zlabel, detector.zvar,
					3, detector.p, detector.zmin, detector.zmax); 
	    } else {
	      	    MPI_MASTER(
			       nxprintattr(f, "dataset columns",
					   strlen(detector.ylabel) ? detector.ylabel : "None");
		    );
	    }

	    /* write the actual data (appended if already exists) */
	    if (!strcasestr(detector.format, "list") && !strcasestr(detector.format, "pixels")) {
	      mcdetector_out_array_nexus(f, "data", detector.p1, detector);
	      mcdetector_out_array_nexus(f, "errors", detector.p2, detector);
	      mcdetector_out_array_nexus(f, "ncount", detector.p0, detector);
	    } else if (strcasestr(detector.format, "pixels")) {
	      mcdetector_out_array_nexus(  f, "pixels", detector.p1, detector);
	    } else {
	      mcdetector_out_array_nexus(  f, "events", detector.p1, detector);
	    }
	    NXclosegroup(f);
	    NXopengroup(f, data_name, "NXdata");
	    NXgetgroupID(nxhandle, &pLink);
	    NXclosegroup(f);
	  } /* NXdata data_name*/
	  NXclosegroup(f);
	} /* NXdetector output */
	NXclosegroup(f);
      } /* NXdata detector.nexuscomp */
      NXclosegroup(f);
    } /* NXdata components */
    NXclosegroup(f);
  } /* NXdata instrument */
  
  if (!strcasestr(detector.format, "pixels")) {
    if (NXopengroup(f, "data", "NXdetector") == NX_OK) {
      NXmakelink(nxhandle, &pLink);
      NXclosegroup(f);
    }
  }
  return(NX_OK);
} /* mcdetector_out_array_nexus */

#ifdef USE_MPI
/*******************************************************************************
* mcdetector_out_list_slaves: slaves send their list data to master which writes
*   requires: NXentry to be opened
* WARNING: this method has a flaw: it requires all nodes to flush the lists
*   the same number of times. In case one node is just below the buffer size
*   when finishing (e.g. monitor_nd), it may not trigger save but others may.
*   Then the number of recv/send is not constant along nodes, and simulation stalls.
*******************************************************************************/
MCDETECTOR mcdetector_out_list_slaves(MCDETECTOR detector)
{
  int     node_i=0;
  MPI_MASTER(
	     printf("\n** MPI master gathering slave node list data ** \n");
  );

  if (mpi_node_rank != mpi_node_root) {
    /* MPI slave: slaves send their data to master: 2 MPI_Send calls */
    /* m, n, p must be sent first, since all slaves do not have the same number of events */
    int mnp[3]={detector.m,detector.n,detector.p};

    if (mc_MPI_Send(mnp, 3, MPI_INT, mpi_node_root)!= MPI_SUCCESS)
      fprintf(stderr, "Warning: proc %i to master: MPI_Send mnp list error (mcdetector_out_list_slaves)\n", mpi_node_rank);
    if (!detector.p1
     || mc_MPI_Send(detector.p1, mnp[0]*mnp[1]*mnp[2], MPI_DOUBLE, mpi_node_root) != MPI_SUCCESS)
      fprintf(stderr, "Warning: proc %i to master: MPI_Send p1 list error: mnp=%i (mcdetector_out_list_slaves)\n", mpi_node_rank, abs(mnp[0]*mnp[1]*mnp[2]));
    /* slaves are done: sent mnp and p1 */
  } /* end slaves */

  /* MPI master: receive data from slaves sequentially: 2 MPI_Recv calls */

  if (mpi_node_rank == mpi_node_root) {
    for(node_i=0; node_i<mpi_node_count; node_i++) {
      double *this_p1=NULL;                               /* buffer to hold the list from slaves */
      int     mnp[3]={0,0,0};  /* size of this buffer */
      if (node_i != mpi_node_root) { /* get data from slaves */
	if (mc_MPI_Recv(mnp, 3, MPI_INT, node_i) != MPI_SUCCESS)
	  fprintf(stderr, "Warning: master from proc %i: "
		  "MPI_Recv mnp list error (mcdetector_write_data)\n", node_i);
	if (mnp[0]*mnp[1]*mnp[2]) {
	  this_p1 = (double *)calloc(mnp[0]*mnp[1]*mnp[2], sizeof(double));
	  if (!this_p1 || mc_MPI_Recv(this_p1, abs(mnp[0]*mnp[1]*mnp[2]), MPI_DOUBLE, node_i)!= MPI_SUCCESS)
	    fprintf(stderr, "Warning: master from proc %i: "
		    "MPI_Recv p1 list error: mnp=%i (mcdetector_write_data)\n", node_i, mnp[0]*mnp[1]*mnp[2]);
	  else {
	    printf(". MPI master writing data for slave node %i\n",node_i);
	    detector.p1 = this_p1;
	    detector.m  = mnp[0]; detector.n  = mnp[1]; detector.p  = mnp[2];

	    mcdetector_out_data_nexus(nxhandle, detector);
	  }
	}
      } /* if not master */
      free(this_p1);
    } /* for */
  MPI_MASTER(
	     printf("\n** Done ** \n");
  );
  }
  // Common return statement for slaves / master alike
  return(detector);
}
#endif

MCDETECTOR mcdetector_out_0D_nexus(MCDETECTOR detector)
{
  /* Write data set information to NeXus file. */
  MPI_MASTER(
    mcdatainfo_out_nexus(nxhandle, detector);
  );

  return(detector);
} /* mcdetector_out_0D_ascii */

MCDETECTOR mcdetector_out_1D_nexus(MCDETECTOR detector)
{
  MPI_MASTER(
  mcdatainfo_out_nexus(nxhandle, detector);
  mcdetector_out_data_nexus(nxhandle, detector);
  );
  return(detector);
} /* mcdetector_out_1D_ascii */

MCDETECTOR mcdetector_out_2D_nexus(MCDETECTOR detector)
{
  MPI_MASTER(
  mcdatainfo_out_nexus(nxhandle, detector);
  mcdetector_out_data_nexus(nxhandle, detector);
  );

#ifdef USE_MPI // and USE_NEXUS
  /* NeXus: slave nodes have master write their lists */
  if (strcasestr(detector.format, "list") && mpi_node_count > 1) {
    mcdetector_out_list_slaves(detector);
  }
#endif /* USE_MPI */

  return(detector);
} /* mcdetector_out_2D_nexus */

MCDETECTOR mcdetector_out_3D_nexus(MCDETECTOR detector)
{
  printf("Received detector from %s\n",detector.component);
  MPI_MASTER(
  mcdatainfo_out_nexus(nxhandle, detector);
  mcdetector_out_data_nexus(nxhandle, detector);
  );
  return(detector);
} /* mcdetector_out_3D_nexus */


#endif /* USE_NEXUS*/








/* ========================================================================== */

/*                            Main input functions                            */
/*            DETECTOR_OUT_xD function calls -> ascii or NeXus                */

/* ========================================================================== */

/*******************************************************************************
* siminfo_init:   open SIM and write header
*******************************************************************************/
FILE *siminfo_init(FILE *f)
{
  int exists=0;

  /* check format */
  if (!mcformat || !strlen(mcformat)
   || !strcasecmp(mcformat, "MCSTAS") || !strcasecmp(mcformat, "MCXTRACE")
   || !strcasecmp(mcformat, "PGPLOT") || !strcasecmp(mcformat, "GNUPLOT") || !strcasecmp(mcformat, "MCCODE")
   || !strcasecmp(mcformat, "MATLAB")) {
    mcformat="McCode";
#ifdef USE_NEXUS
  } else if (strcasestr(mcformat, "NeXus")) {
    /* Do nothing */
#endif
  } else {
    fprintf(stderr,
	    "Warning: You have requested the output format %s which is unsupported by this binary. Resetting to standard %s format.\n",mcformat ,"McCode");
    mcformat="McCode";
  }

  /* open the SIM file if not defined yet */
  if (siminfo_file || mcdisable_output_files)
    return (siminfo_file);

#ifdef USE_NEXUS
  /* only master writes NeXus header: calls NXopen(nxhandle) */
  if (mcformat && strcasestr(mcformat, "NeXus")) {
	  MPI_MASTER(
	  siminfo_file = mcnew_file(siminfo_name, "h5", &exists);
    if(!siminfo_file)
      fprintf(stderr,
	      "Warning: could not open simulation description file '%s'\n",
	      siminfo_name);
	  else
	    mcinfo_out_nexus(nxhandle);
	  );
    return(siminfo_file); /* points to nxhandle */
  }
#endif

  /* write main description file (only MASTER) */
  MPI_MASTER(

  siminfo_file = mcnew_file(siminfo_name, "sim", &exists);
  if(!siminfo_file)
    fprintf(stderr,
	    "Warning: could not open simulation description file '%s'\n",
	    siminfo_name);
  else
  {
    /* write SIM header */
    time_t t=time(NULL);
    siminfo_out("%s simulation description file for %s.\n",
      MCCODE_NAME, instrument_name);
    siminfo_out("Date:    %s", ctime(&t)); /* includes \n */
    siminfo_out("Program: %s\n\n", MCCODE_STRING);

    siminfo_out("begin instrument: %s\n", instrument_name);
    mcinfo_out(   "  ", siminfo_file);
    siminfo_out("end instrument\n");

    siminfo_out("\nbegin simulation: %s\n", dirname);
    mcruninfo_out("  ", siminfo_file);
    siminfo_out("end simulation\n");

  }
  ); /* MPI_MASTER */
  return (siminfo_file);

} /* siminfo_init */

/*******************************************************************************
*   siminfo_close:  close SIM
*******************************************************************************/
void siminfo_close()
{
#ifdef USE_MPI
  if(mpi_node_rank == mpi_node_root) {
#endif
  if(siminfo_file && !mcdisable_output_files) {
#ifdef USE_NEXUS
    if (mcformat && strcasestr(mcformat, "NeXus")) {
      time_t t=time(NULL);
      nxprintf(nxhandle, "end_time", ctime(&t));
      nxprintf(nxhandle, "duration", "%li", (long)t-mcstartdate);
      NXclosegroup(nxhandle); /* NXentry */
      NXclose(&nxhandle);
    } else {
#endif
      fclose(siminfo_file);
#ifdef USE_NEXUS
    }
#endif
#ifdef USE_MPI
  }
#endif
    siminfo_file = NULL;
  }
} /* siminfo_close */

/*******************************************************************************
* mcdetector_out_0D: wrapper for 0D (single value).
*   Output single detector/monitor data (p0, p1, p2).
*   Title is t, component name is c.
*******************************************************************************/
MCDETECTOR mcdetector_out_0D(char *t, double p0, double p1, double p2,
			     char *c, Coords posa, Rotation rota, int index)
{
  /* import and perform basic detector analysis (and handle MPI reduce) */
  MCDETECTOR detector = detector_import(mcformat,
    c, (t ? t : MCCODE_STRING " data"),
    1, 1, 1,
    "I", "", "",
    "I", "", "",
    0, 0, 0, 0, 0, 0, c,
    &p0, &p1, &p2, posa, rota, index); /* write Detector: line */

#ifdef USE_NEXUS
  if (strcasestr(detector.format, "NeXus"))
    return(mcdetector_out_0D_nexus(detector));
  else
#endif
    return(mcdetector_out_0D_ascii(detector));

} /* mcdetector_out_0D */



/*******************************************************************************
* mcdetector_out_1D: wrapper for 1D.
*   Output 1d detector data (p0, p1, p2) for n bins linearly
*   distributed across the range x1..x2 (x1 is lower limit of first
*   bin, x2 is upper limit of last bin). Title is t, axis labels are xl
*   and yl. File name is f, component name is c.
*
*   t:    title
*   xl:   x-label
*   yl:   y-label
*   xvar: measured variable length
*   x1:   x axus min
*   x2:   x axis max
*   n:    1d data vector lenght
*   p0:   pntr to start of data block#0
*   p1:   pntr to start of data block#1
*   p2:   pntr to start of data block#2
*   f:    filename
*
*   Not included in the macro, and here forwarded to detector_import:
*   c:    ?
*   posa: ?
*******************************************************************************/
MCDETECTOR mcdetector_out_1D(char *t, char *xl, char *yl,
        char *xvar, double x1, double x2,
        long n,
        double *p0, double *p1, double *p2, char *f,
        char *c, Coords posa, Rotation rota, int index)
{
  /* import and perform basic detector analysis (and handle MPI_Reduce) */
  // detector_import calls mcdetector_statistics, which will return different
  // MCDETECTOR versions for 1-D data based on the value of mcformat.
  //
  MCDETECTOR detector = detector_import(mcformat,
    c, (t ? t : MCCODE_STRING " 1D data"),
    n, 1, 1,
    xl, yl, (n > 1 ? "Signal per bin" : " Signal"),
    xvar, "(I,I_err)", "I",
    x1, x2, 0, 0, 0, 0, f,
    p0, p1, p2, posa, rota, index); /* write Detector: line */
  if (!detector.p1 || !detector.m) return(detector);

#ifdef USE_NEXUS
  if (strcasestr(detector.format, "NeXus"))
    detector = mcdetector_out_1D_nexus(detector);
  else
#endif
    detector = mcdetector_out_1D_ascii(detector);
  if (detector.p1 != p1 && detector.p1) {
    // mcdetector_statistics allocated memory but it hasn't been freed.
    free(detector.p1);
    // plus undo the other damage done there:
    detector.p0 = p0; // was set to NULL
    detector.p1 = p1; // was set to this_p1
    detector.p2 = p2; // was set to NULL
    detector.m = detector.n; // (e.g., labs(n))
    detector.n = 1;  // not (n x n)
    detector.istransposed = n < 0 ? 1 : 0;
  }
  return detector;

} /* mcdetector_out_1D */

/*******************************************************************************
* mcdetector_out_2D: wrapper for 2D.
*   Special case for list: master creates file first, then slaves append their
*   blocks without header-
*
*   t:    title
*   xl:   x-label
*   yl:   y-label
*   x1:   x axus min
*   x2:   x axis max
*   y1:   y axis min
*   y2:   y axis max
*   m:    dim 1 (x) size
*   n:    dim 2 (y) size
*   p0:   pntr to start of data block#0
*   p1:   pntr to start of data block#1
*   p2:   pntr to start of data block#2
*   f:    filename
*
*   Not included in the macro, and here forwarded to detector_import:
*   c:    ?
*   posa: ?
*   rota: ?
*******************************************************************************/
MCDETECTOR mcdetector_out_2D(char *t, char *xl, char *yl,
                  double x1, double x2, double y1, double y2,
                  long m, long n,
                  double *p0, double *p1, double *p2, char *f,
		  char *c, Coords posa, Rotation rota, int index)
{
  char xvar[CHAR_BUF_LENGTH];
  char yvar[CHAR_BUF_LENGTH];

  /* create short axes labels */
  if (xl && strlen(xl)) { strncpy(xvar, xl, CHAR_BUF_LENGTH); xvar[2]='\0'; }
  else strcpy(xvar, "x");
  if (yl && strlen(yl)) { strncpy(yvar, yl, CHAR_BUF_LENGTH); yvar[2]='\0'; }
  else strcpy(yvar, "y");

  MCDETECTOR detector;

  /* import and perform basic detector analysis (and handle MPI_Reduce) */
  if (labs(m) == 1) {/* n>1 on Y, m==1 on X: 1D, no X axis*/
    detector = detector_import(mcformat,
      c, (t ? t : MCCODE_STRING " 1D data"),
      n, 1, 1,
      yl, "", "Signal per bin",
      yvar, "(I,Ierr)", "I",
      y1, y2, x1, x2, 0, 0, f,
      p0, p1, p2, posa, rota, index); /* write Detector: line */
  } else if (labs(n)==1) {/* m>1 on X, n==1 on Y: 1D, no Y axis*/
    detector = detector_import(mcformat,
      c, (t ? t : MCCODE_STRING " 1D data"),
      m, 1, 1,
      xl, "", "Signal per bin",
      xvar, "(I,Ierr)", "I",
      x1, x2, y1, y2, 0, 0, f,
      p0, p1, p2, posa, rota, index); /* write Detector: line */
  }else {
    detector = detector_import(mcformat,
      c, (t ? t : MCCODE_STRING " 2D data"),
      m, n, 1,
      xl, yl, "Signal per bin",
      xvar, yvar, "I",
      x1, x2, y1, y2, 0, 0, f,
      p0, p1, p2, posa, rota, index); /* write Detector: line */
  }

  if (!detector.p1 || !detector.m) return(detector);

#ifdef USE_NEXUS
  if (strcasestr(detector.format, "NeXus"))
    return(mcdetector_out_2D_nexus(detector));
  else
#endif
    return(mcdetector_out_2D_ascii(detector));

} /* mcdetector_out_2D */

/*******************************************************************************
* mcdetector_out_2D_list: List mode 2D including forwarding "options" from
* Monitor_nD
*
*   Special case for list: master creates file first, then slaves append their
*   blocks without header-
*
*   t:    title
*   xl:   x-label
*   yl:   y-label
*   x1:   x axus min
*   x2:   x axis max
*   y1:   y axis min
*   y2:   y axis max
*   m:    dim 1 (x) size
*   n:    dim 2 (y) size
*   p0:   pntr to start of data block#0
*   p1:   pntr to start of data block#1
*   p2:   pntr to start of data block#2
*   f:    filename
*
*   Not included in the macro, and here forwarded to detector_import:
*   c:    ?
*   posa: ?
*   rota: ?
*******************************************************************************/
MCDETECTOR mcdetector_out_2D_list(char *t, char *xl, char *yl,
                  double x1, double x2, double y1, double y2,
                  long m, long n,
                  double *p0, double *p1, double *p2, char *f,
		  char *c, Coords posa, Rotation rota, char* options, int index)
{
  char xvar[CHAR_BUF_LENGTH];
  char yvar[CHAR_BUF_LENGTH];

  /* create short axes labels */
  if (xl && strlen(xl)) { strncpy(xvar, xl, CHAR_BUF_LENGTH); xvar[2]='\0'; }
  else strcpy(xvar, "x");
  if (yl && strlen(yl)) { strncpy(yvar, yl, CHAR_BUF_LENGTH); yvar[2]='\0'; }
  else strcpy(yvar, "y");

  MCDETECTOR detector;

  /* import and perform basic detector analysis (and handle MPI_Reduce) */
  if (labs(m) == 1) {/* n>1 on Y, m==1 on X: 1D, no X axis*/
    detector = detector_import(mcformat,
      c, (t ? t : MCCODE_STRING " 1D data"),
      n, 1, 1,
      yl, "", "Signal per bin",
      yvar, "(I,Ierr)", "I",
      y1, y2, x1, x2, 0, 0, f,
      p0, p1, p2, posa, rota, index); /* write Detector: line */
  } else if (labs(n)==1) {/* m>1 on X, n==1 on Y: 1D, no Y axis*/
    detector = detector_import(mcformat,
      c, (t ? t : MCCODE_STRING " 1D data"),
      m, 1, 1,
      xl, "", "Signal per bin",
      xvar, "(I,Ierr)", "I",
      x1, x2, y1, y2, 0, 0, f,
      p0, p1, p2, posa, rota, index); /* write Detector: line */
  }else {
    detector = detector_import(mcformat,
      c, (t ? t : MCCODE_STRING " 2D data"),
      m, n, 1,
      xl, yl, "Signal per bin",
      xvar, yvar, "I",
      x1, x2, y1, y2, 0, 0, f,
     p0, p1, p2, posa, rota, index); /* write Detector: line */
  }

  MPI_MASTER(
  if (strlen(options)) {
    strcpy(detector.options,options);
  } else {
    strcpy(detector.options,"None");
  }
  );

  if (!detector.p1 || !detector.m) return(detector);

#ifdef USE_NEXUS
  if (strcasestr(detector.format, "NeXus"))
    return(mcdetector_out_2D_nexus(detector));
  else
#endif
    return(mcdetector_out_2D_ascii(detector));

} /* mcdetector_out_2D_list */

/*******************************************************************************
* mcdetector_out_list: wrapper for list output (calls out_2D with mcformat+"list").
*   m=number of events, n=size of each event
*******************************************************************************/
MCDETECTOR mcdetector_out_list(char *t, char *xl, char *yl,
                  long m, long n,
                  double *p1, char *f,
			       char *c, Coords posa, Rotation rota, char* options, int index)
{
  char       format_new[CHAR_BUF_LENGTH];
  char      *format_org;
  MCDETECTOR detector;

  format_org = mcformat;
  strcpy(format_new, mcformat);
  strcat(format_new, " list");
  mcformat = format_new;
  detector = mcdetector_out_2D_list(t, xl, yl,
                  1,labs(m),1,labs(n),
                  m,n,
                  NULL, p1, NULL, f,
		  c, posa,rota,options, index);

  mcformat = format_org;
  return(detector);
}

/*******************************************************************************
 * mcuse_dir: set data/sim storage directory and create it,
 * or exit with error if exists
 ******************************************************************************/
static void
mcuse_dir(char *dir)
{
  if (!dir || !strlen(dir)) return;
#ifdef MC_PORTABLE
  fprintf(stderr, "Error: "
          "Directory output cannot be used with portable simulation (mcuse_dir)\n");
  exit(1);
#else  /* !MC_PORTABLE */
  /* handle file://directory URL type */
  if (strncmp(dir, "file://", strlen("file://")))
    dirname = dir;
  else
    dirname = dir+strlen("file://");


#ifdef USE_MPI
  if(mpi_node_rank == mpi_node_root) {
#endif
    int exists=0;
    DIR* handle = opendir(dirname);
    if (handle) {
      /* Directory exists. */
      closedir(handle);
      exists=1;
    }
    if(mkdir(dirname, 0777)) {
#ifndef DANSE
      if(!mcappend) {
	fprintf(stderr, "Error: unable to create directory '%s' (mcuse_dir)\n", dir);
	fprintf(stderr, "(Maybe the directory already exists?)\n");
#endif
#ifdef USE_MPI
	MPI_Abort(MPI_COMM_WORLD, -1);
#endif
	exit(-1);
      }
    }
#ifdef USE_MPI
    }
#endif

  /* remove trailing PATHSEP (if any) */
  while (strlen(dirname) && dirname[strlen(dirname) - 1] == MC_PATHSEP_C)
    dirname[strlen(dirname) - 1]='\0';
#endif /* !MC_PORTABLE */
} /* mcuse_dir */

/*******************************************************************************
* mcinfo: display instrument simulation info to stdout and exit
*******************************************************************************/
static void
mcinfo(void)
{
  fprintf(stdout, "begin instrument: %s\n", instrument_name);
  mcinfo_out("  ", stdout);
  fprintf(stdout, "end instrument\n");
  fprintf(stdout, "begin simulation: %s\n", dirname ? dirname : ".");
  mcruninfo_out("  ", stdout);
  fprintf(stdout, "end simulation\n");
  exit(0); /* includes MPI_Finalize in MPI mode */
} /* mcinfo */

/*******************************************************************************
* mcparameterinfo: display instrument parameter info to stdout and exit
*******************************************************************************/
static void
mcparameterinfo(void)
{
  mcparameterinfo_out("  ", stdout);
  exit(0); /* includes MPI_Finalize in MPI mode */
} /* mcparameterinfo */



#endif /* ndef MCCODE_R_IO_C */

/* end of the I/O section =================================================== */







/*******************************************************************************
* mcset_ncount: set total number of rays to generate
*******************************************************************************/
void mcset_ncount(unsigned long long int count)
{
  mcncount = count;
}

/* mcget_ncount: get total number of rays to generate */
unsigned long long int mcget_ncount(void)
{
  return mcncount;
}

/* mcget_run_num: get curent number of rays */
/* Within the TRACE scope we are now using _particle->uid directly */
unsigned long long int mcget_run_num() // shuld be (_class_particle* _particle) somehow
{
  /* This function only remains for the few cases outside TRACE where we need to know
     the number of simulated particles */
  return mcrun_num;
}

/* mcsetn_arg: get ncount from a string argument */
static void
mcsetn_arg(char *arg)
{
  mcset_ncount((long long int) strtod(arg, NULL));
}

/* mcsetseed: set the random generator seed from a string argument */
static void
mcsetseed(char *arg)
{
  mcseed = atol(arg);
  if(!mcseed) {
  //  srandom(mcseed);
  //} else {
    fprintf(stderr, "Error: seed must not be zero (mcsetseed)\n");
    exit(1);
  }
}

/* Following part is only embedded when not redundent with mccode-r.h ========= */

#ifndef MCCODE_H

/* SECTION: MCDISPLAY support. =============================================== */

/*******************************************************************************
* Just output MCDISPLAY keywords to be caught by an external plotter client.
*******************************************************************************/

void mcdis_magnify(char *what){
  // Do nothing here, better use interactive zoom from the tools
}

void mcdis_line(double x1, double y1, double z1,
                double x2, double y2, double z2){
  printf("MCDISPLAY: multiline(2,%g,%g,%g,%g,%g,%g)\n",
         x1,y1,z1,x2,y2,z2);
}

void mcdis_dashed_line(double x1, double y1, double z1,
		       double x2, double y2, double z2, int n){
  int i;
  const double dx = (x2-x1)/(2*n+1);
  const double dy = (y2-y1)/(2*n+1);
  const double dz = (z2-z1)/(2*n+1);

  for(i = 0; i < n+1; i++)
    mcdis_line(x1 + 2*i*dx,     y1 + 2*i*dy,     z1 + 2*i*dz,
	       x1 + (2*i+1)*dx, y1 + (2*i+1)*dy, z1 + (2*i+1)*dz);
}

void mcdis_multiline(int count, ...){
  va_list ap;
  double x,y,z;

  printf("MCDISPLAY: multiline(%d", count);
  va_start(ap, count);
  while(count--)
    {
    x = va_arg(ap, double);
    y = va_arg(ap, double);
    z = va_arg(ap, double);
    printf(",%g,%g,%g", x, y, z);
    }
  va_end(ap);
  printf(")\n");
}

void mcdis_rectangle(char* plane, double x, double y, double z,
		     double width, double height){
  /* draws a rectangle in the plane           */
  /* x is ALWAYS width and y is ALWAYS height */
  if (strcmp("xy", plane)==0) {
    mcdis_multiline(5,
		    x - width/2, y - height/2, z,
		    x + width/2, y - height/2, z,
		    x + width/2, y + height/2, z,
		    x - width/2, y + height/2, z,
		    x - width/2, y - height/2, z);
  } else if (strcmp("xz", plane)==0) {
    mcdis_multiline(5,
		    x - width/2, y, z - height/2,
		    x + width/2, y, z - height/2,
		    x + width/2, y, z + height/2,
		    x - width/2, y, z + height/2,
		    x - width/2, y, z - height/2);
  } else if (strcmp("yz", plane)==0) {
    mcdis_multiline(5,
		    x, y - height/2, z - width/2,
		    x, y - height/2, z + width/2,
		    x, y + height/2, z + width/2,
		    x, y + height/2, z - width/2,
		    x, y - height/2, z - width/2);
  } else {

    fprintf(stderr, "Error: Definition of plane %s unknown\n", plane);
    exit(1);
  }
}

void mcdis_circle(char *plane, double x, double y, double z, double r){
  printf("MCDISPLAY: circle('%s',%g,%g,%g,%g)\n", plane, x, y, z, r);
}

void mcdis_new_circle(double x, double y, double z, double r, double nx, double ny, double nz){
  printf("MCDISPLAY: new_circle(%g,%g,%g,%g,%g,%g,%g)\n", x, y, z, r, nx, ny, nz);
}


/* Draws a circle with center (x,y,z), radius (r), and in the plane
 * with normal (nx,ny,nz)*/
void mcdis_Circle(double x, double y, double z, double r, double nx, double ny, double nz){
    int i;
    if(nx==0 && ny && nz==0){
        for (i=0;i<24; i++){
            mcdis_line(x+r*sin(i*2*PI/24),y,z+r*cos(i*2*PI/24),
                    x+r*sin((i+1)*2*PI/24),y,z+r*cos((i+1)*2*PI/24));
        }
    }else{
        double mx,my,mz;
        /*generate perpendicular vector using (nx,ny,nz) and (0,1,0)*/
        vec_prod(mx,my,mz, 0,1,0, nx,ny,nz);
        NORM(mx,my,mz);
        /*draw circle*/
        for (i=0;i<24; i++){
            double ux,uy,uz;
            double wx,wy,wz;
            rotate(ux,uy,uz, mx,my,mz, i*2*PI/24, nx,ny,nz);
            rotate(wx,wy,wz, mx,my,mz, (i+1)*2*PI/24, nx,ny,nz);
            mcdis_line(x+ux*r,y+uy*r,z+uz*r,
                    x+wx*r,y+wy*r,z+wz*r);
        }
    }
}


/*  OLD IMPLEMENTATION
    draws a box with center at (x, y, z) and
    width (deltax), height (deltay), length (deltaz) */
void mcdis_legacy_box(double x, double y, double z,
	       double width, double height, double length){

  mcdis_rectangle("xy", x, y, z-length/2, width, height);
  mcdis_rectangle("xy", x, y, z+length/2, width, height);
  mcdis_line(x-width/2, y-height/2, z-length/2,
	     x-width/2, y-height/2, z+length/2);
  mcdis_line(x-width/2, y+height/2, z-length/2,
	     x-width/2, y+height/2, z+length/2);
  mcdis_line(x+width/2, y-height/2, z-length/2,
	     x+width/2, y-height/2, z+length/2);
  mcdis_line(x+width/2, y+height/2, z-length/2,
	     x+width/2, y+height/2, z+length/2);
}

/*  NEW 3D IMPLEMENTATION OF BOX SUPPORTS HOLLOW ALSO
    draws a box with center at (x, y, z) and
    width (deltax), height (deltay), length (deltaz) */
void mcdis_box(double x, double y, double z,
	       double width, double height, double length, double thickness, double nx, double ny, double nz){
  if (mcdotrace==2) {
    printf("MCDISPLAY: box(%g,%g,%g,%g,%g,%g,%g,%g,%g,%g)\n", x, y, z, width, height, length, thickness, nx, ny, nz);
  } else {
    mcdis_legacy_box(x, y, z, width, height, length);
    if (thickness)
      mcdis_legacy_box(x, y, z, width-thickness, height-thickness, length);
  }
}


/* OLD IMPLEMENTATION
Draws a cylinder with center at (x,y,z) with extent (r,height).
 * The cylinder axis is along the vector nx,ny,nz. */
void mcdis_legacy_cylinder( double x, double y, double z,
        double r, double height, int N, double nx, double ny, double nz){
    int i;
    /*no lines make little sense - so trigger the default*/
    if(N<=0) N=5;

    NORM(nx,ny,nz);
    double h_2=height/2.0;
    mcdis_Circle(x+nx*h_2,y+ny*h_2,z+nz*h_2,r,nx,ny,nz);
    mcdis_Circle(x-nx*h_2,y-ny*h_2,z-nz*h_2,r,nx,ny,nz);

    double mx,my,mz;
    /*generate perpendicular vector using (nx,ny,nz) and (0,1,0)*/
    if(nx==0 && ny && nz==0){
        mx=my=0;mz=1;
    }else{
        vec_prod(mx,my,mz, 0,1,0, nx,ny,nz);
        NORM(mx,my,mz);
    }
    /*draw circle*/
    for (i=0; i<24; i++){
        double ux,uy,uz;
        rotate(ux,uy,uz, mx,my,mz, i*2*PI/24, nx,ny,nz);
        mcdis_line(x+nx*h_2+ux*r, y+ny*h_2+uy*r, z+nz*h_2+uz*r,
                 x-nx*h_2+ux*r, y-ny*h_2+uy*r, z-nz*h_2+uz*r);
    }
}

/* NEW 3D IMPLEMENTATION ALSO SUPPORTING HOLLOW
Draws a cylinder with center at (x,y,z) with extent (r,height).
 * The cylinder axis is along the vector nx,ny,nz.*/
void mcdis_cylinder( double x, double y, double z,
        double r, double height, double thickness, double nx, double ny, double nz){
  if (mcdotrace==2) {
      printf("MCDISPLAY: cylinder(%g, %g, %g, %g, %g, %g, %g, %g, %g)\n",
         x, y, z, r, height, thickness, nx, ny, nz);
  } else {
    mcdis_legacy_cylinder(x, y, z,
			  r, height, 12, nx, ny, nz);
  }
}

/* Draws a cone with center at (x,y,z) with extent (r,height).
 * The cone axis is along the vector nx,ny,nz.*/
void mcdis_cone( double x, double y, double z,
        double r, double height, double nx, double ny, double nz){
  if (mcdotrace==2) {
    printf("MCDISPLAY: cone(%g, %g, %g, %g, %g, %g, %g, %g)\n",
       x, y, z, r, height, nx, ny, nz);
  } else {
    mcdis_Circle(x, y, z, r, nx, ny, nz);
    mcdis_Circle(x+0.25*height*nx, y+0.25*height*ny, z+0.25*height*nz, 0.75*r, nx, ny, nz);
    mcdis_Circle(x+0.5*height*nx, y+0.5*height*ny, z+0.5*height*nz, 0.5*r, nx, ny, nz);
    mcdis_Circle(x+0.75*height*nx, y+0.75*height*ny, z+0.75*height*nz, 0.25*r, nx, ny, nz);
    mcdis_line(x, y, z, x+height*nx, y+height*ny, z+height*nz);
  }
}

/* Draws a disc with center at (x,y,z) with extent (r).
 * The disc axis is along the vector nx,ny,nz.*/
void mcdis_disc( double x, double y, double z,
        double r, double nx, double ny, double nz){
  printf("MCDISPLAY: disc(%g, %g, %g, %g, %g, %g, %g)\n",
     x, y, z, r, nx, ny, nz);
}

/* Draws a annulus with center at (x,y,z) with extent (outer_radius) and remove inner_radius.
 * The annulus axis is along the vector nx,ny,nz.*/
void mcdis_annulus( double x, double y, double z,
        double outer_radius, double inner_radius, double nx, double ny, double nz){
  printf("MCDISPLAY: annulus(%g, %g, %g, %g, %g, %g, %g, %g)\n",
     x, y, z, outer_radius, inner_radius, nx, ny, nz);
}

/* draws a sphere with center at (x,y,z) with extent (r)*/
void mcdis_sphere(double x, double y, double z, double r){
  if (mcdotrace==2) {
    printf("MCDISPLAY: sphere(%g,%g,%g,%g)\n", x, y, z, r);
  } else {
    double nx,ny,nz;
    int i;
    int N=12;

    nx=0;ny=0;nz=1;
    mcdis_Circle(x,y,z,r,nx,ny,nz);
    for (i=1;i<N;i++){
        rotate(nx,ny,nz, nx,ny,nz, PI/N, 0,1,0);
        mcdis_Circle(x,y,z,r,nx,ny,nz);
    }
    /*lastly draw a great circle perpendicular to all N circles*/
    //mcdis_Circle(x,y,z,radius,1,0,0);

    for (i=1;i<=N;i++){
        double yy=-r+ 2*r*((double)i/(N+1));
        mcdis_Circle(x,y+yy ,z,  sqrt(r*r-yy*yy) ,0,1,0);
    }
  }
}
/* POLYHEDRON IMPLEMENTATION*/

void mcdis_polyhedron(char *vertices_faces){
  printf("MCDISPLAY: polyhedron %s\n", vertices_faces);
}

/* POLYGON IMPLEMENTATION */
void mcdis_polygon(int count, ...){
  va_list ap;
  double *x,*y,*z;

  double x0=0,y0=0,z0=0; /* Used for centre-of-mass in trace==2 */

  x=malloc(count*sizeof(double));
  y=malloc(count*sizeof(double));
  z=malloc(count*sizeof(double));
  if (!x || !y || !z) {
    fprintf(stderr,"Error initializing polygon set size %i\n",count);
    exit(-1);
  }
  va_start(ap, count);
  // Fallback for trace==1 is multiline, one rank higher
  if (mcdotrace==1) {
    printf("MCDISPLAY: multiline(%i,",count+1);
  }
  
  int j;
  for (j=0; j<count; j++) {
    x[j] = va_arg(ap, double);
    y[j] = va_arg(ap, double);
    z[j] = va_arg(ap, double);
    if (mcdotrace==1) {
      printf("%g,%g,%g,",x[j],y[j],z[j]);
    } else {
      // Calculation of polygon centre of mass
      x0 += x[j]; y0 += y[j]; z0 += z[j];
    }
  }
  va_end(ap);

  /* Patch data for multiline(count+1, ... use 0th point*/
  if (mcdotrace==1) {
    printf("%g,%g,%g)\n",x[0],y[0],z[0]);
  } else {
    x0 /= count; y0 /= count; z0 /= count;
    /* Build up a json string for a "polyhedron" */
    // Estimate size of the JSON string
    const int VERTEX_OVERHEAD = 30;
    const int FACE_OVERHEAD_BASE = 20;
    const int FACE_INDEX_OVERHEAD = 15;
    int estimated_size = 256; // Base size
    estimated_size += count * VERTEX_OVERHEAD;

    int faceSize;
    int vtxSize;
    if (count > 3) {
      /* Split in triangles - as many as polygon rank */
      faceSize=count;
      vtxSize=count+1;
    } else {
      faceSize=1;
      vtxSize=count;
    }
    
    for (int i = 0; i < faceSize;) {
        int num_indices = 3;
        estimated_size += FACE_OVERHEAD_BASE + num_indices * FACE_INDEX_OVERHEAD;
        i += num_indices + 1;
    }

    char *json_string = malloc(estimated_size);
    if (json_string == NULL) {
        fprintf(stderr, "Memory allocation failed.\n");
        return;
    }

    char *ptr = json_string;
    ptr += sprintf(ptr, "{ \"vertices\": [");

    if (count==3) { // Single, basic triangle
      ptr += sprintf(ptr, "[%g, %g, %g], [%g, %g, %g], [%g, %g, %g]", x[0], y[0], z[0], x[1], y[1], z[1], x[2], y[2], z[2]);
    } else {
      for (int i = 0; i < vtxSize-1; i++) {
        ptr += sprintf(ptr, "[%g, %g, %g]", x[i], y[i], z[i]);
        if (i < vtxSize - 2) {
	  ptr += sprintf(ptr, ", ");
        } else {
	  ptr += sprintf(ptr, ", [%g, %g, %g]", x0, y0, z0);
	}
      }
    }
    ptr += sprintf(ptr, "], \"faces\": [");
    if (count==3) { // Single, basic triangle, 1 face...
      ptr += sprintf(ptr, "{ \"face\": [");
      ptr += sprintf(ptr, "0, 1, 2");
      ptr += sprintf(ptr, "]}");
    } else {
      for (int i = 0; i < faceSize; i++) {
        int num = 3;
        ptr += sprintf(ptr, "{ \"face\": [");
	if (i < faceSize - 1) {
	  ptr += sprintf(ptr, "%d, %d, %d",i,i+1,count);
	} else {
	  ptr += sprintf(ptr, "%d, %d, %d",i,count,0);
	}
	ptr += sprintf(ptr, "]}");
	if (i < faceSize-1) {
	  ptr += sprintf(ptr, ", ");
	}
      }
    }
    ptr += sprintf(ptr, "]}");
    mcdis_polyhedron(json_string);

    free(json_string);
  }
  free(x);free(y);free(z);
}
/* END NEW POLYGON IMPLEMENTATION*/

/*
void mcdis_polygon(double x1, double y1, double z1,
                double x2, double y2, double z2){
  printf("MCDISPLAY: polygon(2,%g,%g,%g,%g,%g,%g)\n",
         x1,y1,z1,x2,y2,z2);
}
*/

/* SECTION: coordinates handling ============================================ */

/*******************************************************************************
* Since we use a lot of geometric calculations using Cartesian coordinates,
* we collect some useful routines here. However, it is also permissible to
* work directly on the underlying struct coords whenever that is most
* convenient (that is, the type Coords is not abstract).
*
* Coordinates are also used to store rotation angles around x/y/z axis.
*
* Since coordinates are used much like a basic type (such as double), the
* structure itself is passed and returned, rather than a pointer.
*
* At compile-time, the values of the coordinates may be unknown (for example
* a motor position). Hence coordinates are general expressions and not simple
* numbers. For this we used the type Coords_exp which has three CExp
* fields. For runtime (or calculations possible at compile time), we use
* Coords which contains three double fields.
*******************************************************************************/

/* coords_set: Assign coordinates. */
Coords coords_set(MCNUM x, MCNUM y, MCNUM z)
{
  Coords a;

  a.x = x;
  a.y = y;
  a.z = z;
  return a;
}

/* coords_get: get coordinates. Required when 'x','y','z' are #defined as ray pars */
Coords coords_get(Coords a, MCNUM *x, MCNUM *y, MCNUM *z)
{
  *x = a.x;
  *y = a.y;
  *z = a.z;
  return a;
}

/* coords_add: Add two coordinates. */
Coords coords_add(Coords a, Coords b)
{
  Coords c;

  c.x = a.x + b.x;
  c.y = a.y + b.y;
  c.z = a.z + b.z;
  if (fabs(c.z) < 1e-14) c.z=0.0;
  return c;
}

/* coords_sub: Subtract two coordinates. */
Coords coords_sub(Coords a, Coords b)
{
  Coords c;

  c.x = a.x - b.x;
  c.y = a.y - b.y;
  c.z = a.z - b.z;
  if (fabs(c.z) < 1e-14) c.z=0.0;
  return c;
}

/* coords_neg: Negate coordinates. */
Coords coords_neg(Coords a)
{
  Coords b;

  b.x = -a.x;
  b.y = -a.y;
  b.z = -a.z;
  return b;
}

/* coords_scale: Scale a vector. */
Coords coords_scale(Coords b, double scale) {
  Coords a;

  a.x = b.x*scale;
  a.y = b.y*scale;
  a.z = b.z*scale;
  return a;
}

/* coords_sp: Scalar product: a . b */
double coords_sp(Coords a, Coords b) {
  double value;

  value = a.x*b.x + a.y*b.y + a.z*b.z;
  return value;
}

/* coords_xp: Cross product: a = b x c. */
Coords coords_xp(Coords b, Coords c) {
  Coords a;

  a.x = b.y*c.z - c.y*b.z;
  a.y = b.z*c.x - c.z*b.x;
  a.z = b.x*c.y - c.x*b.y;
  return a;
}

/* coords_len: Gives length of coords set. */
double coords_len(Coords a) {
  return sqrt(a.x*a.x + a.y*a.y + a.z*a.z);
}

/* coords_mirror: Mirror a in plane (through the origin) defined by normal n*/
Coords coords_mirror(Coords a, Coords n) {
  double t = scalar_prod(n.x, n.y, n.z, n.x, n.y, n.z);
  Coords b;
  if (t!=1) {
    t = sqrt(t);
    n.x /= t;
    n.y /= t;
    n.z /= t;
  }
  t=scalar_prod(a.x, a.y, a.z, n.x, n.y, n.z);
  b.x = a.x-2*t*n.x;
  b.y = a.y-2*t*n.y;
  b.z = a.z-2*t*n.z;
  return b;
}

/* coords_print: Print out vector values. */
void coords_print(Coords a) {
  #ifndef OPENACC
  fprintf(stdout, "(%f, %f, %f)\n", a.x, a.y, a.z);
  #endif
  return;
}

mcstatic void coords_norm(Coords* c) {
	double temp = coords_sp(*c,*c);

	// Skip if we will end dividing by zero
	if (temp == 0) return;

	temp = sqrt(temp);

	c->x /= temp;
	c->y /= temp;
	c->z /= temp;
}

/* coords_test_zero: check if zero vector*/
int coords_test_zero(Coords a){
  return ( a.x==0 && a.y==0 && a.z==0 );
}

/*******************************************************************************
* The Rotation type implements a rotation transformation of a coordinate
* system in the form of a double[3][3] matrix.
*
* Contrary to the Coords type in coords.c, rotations are passed by
* reference. Functions that yield new rotations do so by writing to an
* explicit result parameter; rotations are not returned from functions. The
* reason for this is that arrays cannot by returned from functions (though
* structures can; thus an alternative would have been to wrap the
* double[3][3] array up in a struct). Such are the ways of C programming.
*
* A rotation represents the tranformation of the coordinates of a vector when
* changing between coordinate systems that are rotated with respect to each
* other. For example, suppose that coordinate system Q is rotated 45 degrees
* around the Z axis with respect to coordinate system P. Let T be the
* rotation transformation representing a 45 degree rotation around Z. Then to
* get the coordinates of a vector r in system Q, apply T to the coordinates
* of r in P. If r=(1,0,0) in P, it will be (sqrt(1/2),-sqrt(1/2),0) in
* Q. Thus we should be careful when interpreting the sign of rotation angles:
* they represent the rotation of the coordinate systems, not of the
* coordinates (which has opposite sign).
*******************************************************************************/

/*******************************************************************************
* rot_set_rotation: Get transformation for rotation first phx around x axis,
* then phy around y, then phz around z.
*******************************************************************************/
void rot_set_rotation(Rotation t, double phx, double phy, double phz)
{
  if ((phx == 0) && (phy == 0) && (phz == 0)) {
    t[0][0] = 1.0;
    t[0][1] = 0.0;
    t[0][2] = 0.0;
    t[1][0] = 0.0;
    t[1][1] = 1.0;
    t[1][2] = 0.0;
    t[2][0] = 0.0;
    t[2][1] = 0.0;
    t[2][2] = 1.0;
  } else {
    double cx = cos(phx);
    double sx = sin(phx);
    double cy = cos(phy);
    double sy = sin(phy);
    double cz = cos(phz);
    double sz = sin(phz);

    t[0][0] = cy*cz;
    t[0][1] = sx*sy*cz + cx*sz;
    t[0][2] = sx*sz - cx*sy*cz;
    t[1][0] = -cy*sz;
    t[1][1] = cx*cz - sx*sy*sz;
    t[1][2] = sx*cz + cx*sy*sz;
    t[2][0] = sy;
    t[2][1] = -sx*cy;
    t[2][2] = cx*cy;
  }
}

/*******************************************************************************
* rot_test_identity: Test if rotation is identity
*******************************************************************************/
int rot_test_identity(Rotation t)
{
  return (t[0][0] + t[1][1] + t[2][2] == 3);
}

/*******************************************************************************
* rot_mul: Matrix multiplication of transformations (this corresponds to
* combining transformations). After rot_mul(T1, T2, T3), doing T3 is
* equal to doing first T2, then T1.
* Note that T3 must not alias (use the same array as) T1 or T2.
*******************************************************************************/
void rot_mul(Rotation t1, Rotation t2, Rotation t3)
{
  if (rot_test_identity(t1)) {
    rot_copy(t3, t2);
  } else if (rot_test_identity(t2)) {
    rot_copy(t3, t1);
  } else {
    int i,j;
    for(i = 0; i < 3; i++)
      for(j = 0; j < 3; j++)
	t3[i][j] = t1[i][0]*t2[0][j] + t1[i][1]*t2[1][j] + t1[i][2]*t2[2][j];
  }
}

/*******************************************************************************
* rot_copy: Copy a rotation transformation (arrays cannot be assigned in C).
*******************************************************************************/
void rot_copy(Rotation dest, Rotation src)
{
  int i,j;
  for(i = 0; i < 3; i++)
    for(j = 0; j < 3; j++)
      dest[i][j] = src[i][j];
}

/*******************************************************************************
* rot_transpose: Matrix transposition, which is inversion for Rotation matrices
*******************************************************************************/
void rot_transpose(Rotation src, Rotation dst)
{
  dst[0][0] = src[0][0];
  dst[0][1] = src[1][0];
  dst[0][2] = src[2][0];
  dst[1][0] = src[0][1];
  dst[1][1] = src[1][1];
  dst[1][2] = src[2][1];
  dst[2][0] = src[0][2];
  dst[2][1] = src[1][2];
  dst[2][2] = src[2][2];
}

/*******************************************************************************
* rot_apply: returns t*a
*******************************************************************************/
Coords rot_apply(Rotation t, Coords a)
{
  Coords b;
  if (rot_test_identity(t)) {
    return a;
  } else {
    b.x = t[0][0]*a.x + t[0][1]*a.y + t[0][2]*a.z;
    b.y = t[1][0]*a.x + t[1][1]*a.y + t[1][2]*a.z;
    b.z = t[2][0]*a.x + t[2][1]*a.y + t[2][2]*a.z;
    return b;
  }
}

/**
 * Pretty-printing of rotation matrices.
 */
void rot_print(Rotation rot) {
	printf("[ %4.2f %4.2f %4.2f ]\n",
			rot[0][0], rot[0][1], rot[0][2]);
	printf("[ %4.2f %4.2f %4.2f ]\n",
			rot[1][0], rot[1][1], rot[1][2]);
	printf("[ %4.2f %4.2f %4.2f ]\n\n",
			rot[2][0], rot[2][1], rot[2][2]);
}

/**
 * Vector product: used by vec_prod (mccode-r.h). Use coords_xp for Coords.
 */
void vec_prod_func(double *x, double *y, double *z,
		double x1, double y1, double z1,
		double x2, double y2, double z2) {
    *x = (y1)*(z2) - (y2)*(z1);
    *y = (z1)*(x2) - (z2)*(x1);
    *z = (x1)*(y2) - (x2)*(y1);
}

/**
 * Scalar product: use coords_sp for Coords.
 */
double scalar_prod(
		double x1, double y1, double z1,
		double x2, double y2, double z2) {
	return ((x1 * x2) + (y1 * y2) + (z1 * z2));
}

mcstatic void norm_func(double *x, double *y, double *z) {
	double temp = (*x * *x) + (*y * *y) + (*z * *z);
	if (temp != 0) {
		temp = sqrt(temp);
		*x /= temp;
		*y /= temp;
		*z /= temp;
	}
}


/* SECTION: GPU algorithms ================================================== */


/*
*  Divide-and-conquer strategy for parallelizing this task: Sort absorbed
*  particles last.
*
*   particles:  the particle array, required to checking _absorbed
*   pbuffer:    same-size particle buffer array required for parallel sort
*   len:        sorting area-of-interest size (e.g. from previous calls)
*   buffer_len: total array size
*   flag_split: if set, multiply live particles into absorbed slots, up to buffer_len
*   multiplier: output arg, becomes the  SPLIT multiplier if flag_split is set
*/
#ifdef FUNNEL
long sort_absorb_last(_class_particle* particles, _class_particle* pbuffer, long len, long buffer_len, long flag_split, long* multiplier) {
  #define SAL_THREADS 1024 // num parallel sections
  if (len<SAL_THREADS) return sort_absorb_last_serial(particles, len);

  if (multiplier != NULL) *multiplier = -1; // set default out value for multiplier
  long newlen = 0;
  long los[SAL_THREADS]; // target array startidxs
  long lens[SAL_THREADS]; // target array sublens
  long l = floor(len/(SAL_THREADS-1)); // subproblem_len
  long ll = len - l*(SAL_THREADS-1); // last_subproblem_len

  // TODO: The l vs ll is too simplistic, since ll can become much larger
  // than l, resulting in idling. We should distribute lengths more evenly.

  // step 1: sort sub-arrays
  #pragma acc parallel loop present(particles[0:buffer_len], pbuffer[0:buffer_len])
  for (unsigned long tidx=0; tidx<SAL_THREADS; tidx++) {
    long lo = l*tidx;
    long loclen = l;
    if (tidx==(SAL_THREADS-1)) loclen = ll; // last sub-problem special case
    long i = lo;
    long j = lo + loclen - 1;

    // write into pbuffer at i and j
    #pragma acc loop seq
    while (i < j) {
      #pragma acc loop seq
      while (!particles[i]._absorbed && i<j) {
        pbuffer[i] = particles[i];
        i++;
      }
      #pragma acc loop seq
      while (particles[j]._absorbed && i<j) {
        pbuffer[j] = particles[j];
        j--;
      }
      if (i < j) {
        pbuffer[j] = particles[i];
        pbuffer[i] = particles[j];
        i++;
        j--;
      }
    }
    // transfer edge case
    if (i==j)
      pbuffer[i] = particles[i];

    lens[tidx] = i - lo;
    if (i==j && !particles[i]._absorbed) lens[tidx]++;
  }

  // determine lo's
  long accumlen = 0;
  #pragma acc loop seq
  for (long idx=0; idx<SAL_THREADS; idx++) {
    los[idx] = accumlen;
    accumlen = accumlen + lens[idx];
  }

  // step 2: write non-absorbed sub-arrays to psorted/output from the left
  #pragma acc parallel loop present(pbuffer[0:buffer_len])
  for (unsigned long tidx=0; tidx<SAL_THREADS; tidx++) {
    long j, k;
    #pragma acc loop seq
    for (long i=0; i<lens[tidx]; i++) {
      j = i + l*tidx;
      k = i + los[tidx];
      particles[k] = pbuffer[j];
    }
  }
  //for (int ii=0;ii<accumlen;ii++) printf("%ld ", (psorted[ii]->_absorbed));

  // return (no SPLIT)
  if (flag_split != 1)
    return accumlen;

  // SPLIT - repeat the non-absorbed block N-1 times, where len % accumlen = N + R
  int mult = buffer_len / accumlen; // TODO: possibly use a new arg, bufferlen, rather than len

  // not enough space for full-block split, return
  if (mult <= 1)
    return accumlen;

  // copy non-absorbed block
  #pragma acc parallel loop present(particles[0:buffer_len])
  for (long tidx = 0; tidx < accumlen; tidx++) { // tidx: thread index
    randstate_t randstate[7];
    _class_particle sourcebuffer;
    _class_particle targetbuffer;
    // assign reduced weight to all particles
    particles[tidx].p=particles[tidx].p/mult;
    #pragma acc loop seq
    for (long bidx = 1; bidx < mult; bidx++) { // bidx: block index
      // preserve absorbed particle (for randstate)
      sourcebuffer = particles[bidx*accumlen + tidx];
      // buffer full particle struct
      targetbuffer = particles[tidx];
      // reassign previous randstate
      targetbuffer.randstate[0] = sourcebuffer.randstate[0];
      targetbuffer.randstate[1] = sourcebuffer.randstate[1];
      targetbuffer.randstate[2] = sourcebuffer.randstate[2];
      targetbuffer.randstate[3] = sourcebuffer.randstate[3];
      targetbuffer.randstate[4] = sourcebuffer.randstate[4];
      targetbuffer.randstate[5] = sourcebuffer.randstate[5];
      targetbuffer.randstate[6] = sourcebuffer.randstate[6];
      // apply
      particles[bidx*accumlen + tidx] = targetbuffer;
    }
  }

  // set out split multiplier value
  *multiplier = mult;

  // return expanded array size
  return accumlen * mult;
}

#endif

/*
*  Fallback serial version of the one above.
*/
long sort_absorb_last_serial(_class_particle* particles, long len) {
  long i = 0;
  long j = len - 1;
  _class_particle pbuffer;

  // bubble
  while (i < j) {
    while (!particles[i]._absorbed && i<j) i++;
    while (particles[j]._absorbed && i<j) j--;
    if (i < j) {
      pbuffer = particles[j];
      particles[j] = particles[i];
      particles[i] = pbuffer;
      i++;
      j--;
    }
  }

  // return new length
  if (i==j && !particles[i]._absorbed)
    return i + 1;
  else
    return i;
}

/*******************************************************************************
* mccoordschange: applies rotation to (x y z) and (vx vy vz) and Spin (sx,sy,sz)
*******************************************************************************/
void mccoordschange(Coords a, Rotation t, _class_particle *particle)
{
  Coords b, c;

  b.x = particle->x;
  b.y = particle->y;
  b.z = particle->z;
  c = rot_apply(t, b);
  b = coords_add(c, a);
  particle->x = b.x;
  particle->y = b.y;
  particle->z = b.z;

#if MCCODE_PARTICLE_CODE == 2112
    if (particle->vz != 0.0 || particle->vx != 0.0 || particle->vy != 0.0)
      mccoordschange_polarisation(t, &(particle->vx), &(particle->vy), &(particle->vz));

    if (particle->sz != 0.0 || particle->sx != 0.0 || particle->sy != 0.0)
      mccoordschange_polarisation(t, &(particle->sx), &(particle->sy), &(particle->sz));
#elif MCCODE_PARTICLE_CODE == 22
    if (particle->kz != 0.0 || particle->kx != 0.0 || particle->ky != 0.0)
      mccoordschange_polarisation(t, &(particle->kx), &(particle->ky), &(particle->kz));

    if (particle->Ez != 0.0 || particle->Ex != 0.0 || particle->Ey != 0.0)
      mccoordschange_polarisation(t, &(particle->Ex), &(particle->Ey), &(particle->Ez));
#endif
}

/*******************************************************************************
* mccoordschange_polarisation: applies rotation to vector (sx sy sz)
*******************************************************************************/
void mccoordschange_polarisation(Rotation t, double *sx, double *sy, double *sz)
{
  Coords b, c;

  b.x = *sx;
  b.y = *sy;
  b.z = *sz;
  c = rot_apply(t, b);
  *sx = c.x;
  *sy = c.y;
  *sz = c.z;
}

/* SECTION: vector math  ==================================================== */

/* normal_vec_func: Compute normal vector to (x,y,z). */
void normal_vec(double *nx, double *ny, double *nz,
                double x, double y, double z)
{
  double ax = fabs(x);
  double ay = fabs(y);
  double az = fabs(z);
  double l;
  if(x == 0 && y == 0 && z == 0)
  {
    *nx = 0;
    *ny = 0;
    *nz = 0;
    return;
  }
  if(ax < ay)
  {
    if(ax < az)
    {                           /* Use X axis */
      l = sqrt(z*z + y*y);
      *nx = 0;
      *ny = z/l;
      *nz = -y/l;
      return;
    }
  }
  else
  {
    if(ay < az)
    {                           /* Use Y axis */
      l = sqrt(z*z + x*x);
      *nx = z/l;
      *ny = 0;
      *nz = -x/l;
      return;
    }
  }
  /* Use Z axis */
  l = sqrt(y*y + x*x);
  *nx = y/l;
  *ny = -x/l;
  *nz = 0;
} /* normal_vec */

/*******************************************************************************
 * solve_2nd_order: second order equation solve: A*t^2 + B*t + C = 0
 * solve_2nd_order(&t1, NULL, A,B,C)
 *   returns 0 if no solution was found, or set 't1' to the smallest positive
 *   solution.
 * solve_2nd_order(&t1, &t2, A,B,C)
 *   same as with &t2=NULL, but also returns the second solution.
 * EXAMPLE usage for intersection of a trajectory with a plane in gravitation
 * field (gx,gy,gz):
 * The neutron starts at point r=(x,y,z) with velocityv=(vx vy vz). The plane
 * has a normal vector n=(nx,ny,nz) and contains the point W=(wx,wy,wz).
 * The problem consists in solving the 2nd order equation:
 *      1/2.n.g.t^2 + n.v.t + n.(r-W) = 0
 * so that A = 0.5 n.g; B = n.v; C = n.(r-W);
 * Without acceleration, t=-n.(r-W)/n.v
 ******************************************************************************/
int solve_2nd_order_old(double *t1, double *t2,
                  double A,  double B,  double C)
{
  int ret=0;

  if (!t1) return 0;
  *t1 = 0;
  if (t2) *t2=0;

  if (fabs(A) < 1E-10) /* approximate to linear equation: A ~ 0 */
  {
    if (B) {  *t1 = -C/B; ret=1; if (t2) *t2=*t1; }
    /* else no intersection: A=B=0 ret=0 */
  }
  else
  {
    double D;
    D = B*B - 4*A*C;
    if (D >= 0) /* Delta > 0: two solutions */
    {
      double sD, dt1, dt2;
      sD = sqrt(D);
      dt1 = (-B + sD)/2/A;
      dt2 = (-B - sD)/2/A;
      /* we identify very small values with zero */
      if (fabs(dt1) < 1e-10) dt1=0.0;
      if (fabs(dt2) < 1e-10) dt2=0.0;

      /* now we choose the smallest positive solution */
      if      (dt1<=0.0 && dt2>0.0) ret=2; /* dt2 positive */
      else if (dt2<=0.0 && dt1>0.0) ret=1; /* dt1 positive */
      else if (dt1> 0.0 && dt2>0.0)
      {  if (dt1 < dt2) ret=1; else ret=2; } /* all positive: min(dt1,dt2) */
      /* else two solutions are negative. ret=-1 */
      if (ret==1) { *t1 = dt1;  if (t2) *t2=dt2; }
      else        { *t1 = dt2;  if (t2) *t2=dt1; }
      ret=2;  /* found 2 solutions and t1 is the positive one */
    } /* else Delta <0: no intersection. ret=0 */
  }
  return(ret);
} /* solve_2nd_order */

int solve_2nd_order(double *t0, double *t1, double A, double B, double C){
  int retval=0;
  double sign=copysign(1.0,B);
  double dt0,dt1;

  dt0=0;
  dt1=0;
  if(t1){ *t1=0;}

  /*protect against rounding errors by locally equating DBL_EPSILON with 0*/
  if (fabs(A)<DBL_EPSILON){
    A=0;
  }
  if (fabs(B)<DBL_EPSILON){
    B=0;
  }
  if (fabs(C)<DBL_EPSILON){
    C=0;
  }

  /*check if coefficient are sane*/
  if( A==0  && B==0){
    retval=0;
  }else{
    if(A==0){
      /*equation is linear*/
      dt0=-C/B;
      retval=1;
    }else if (C==0){
      /*one root is 0*/
      if(sign<0){
        dt0=0;dt1=-B/A;
      }else{
        dt0=-B/A;dt1=0;
      }
      retval=2;
    }else{
      /*a regular 2nd order eq. Also works out fine for B==0.*/
      double D;
      D=B*B-4*A*C;
      if (D>=0){
        dt0=(-B - sign*sqrt(B*B-4*A*C))/(2*A);
        dt1=C/(A*dt0);
        retval=2;
      }else{
        /*no real roots*/
        retval=0;
      }
    }
    /*sort the solutions*/
    if (retval==1){
      /*put both solutions in t0 and t1*/
      *t0=dt0;
      if(t1) *t1=dt1;
    }else{
      /*we have two solutions*/
      /*swap if both are positive and t1 smaller than t0 or t1 the only positive*/
      int swap=0;
      if(dt1>0 && ( dt1<dt0 || dt0<=0) ){
        swap=1;
      }
      if (swap){
        *t0=dt1;
        if(t1) *t1=dt0;
      }else{
        *t0=dt0;
        if(t1) *t1=dt0;
      }
    }

  }
  return retval;

} /*solve_2nd_order_improved*/


/*******************************************************************************
 * randvec_target_circle: Choose random direction towards target at (x,y,z)
 * with given radius.
 * If radius is zero, choose random direction in full 4PI, no target.
 ******************************************************************************/
void _randvec_target_circle(double *xo, double *yo, double *zo, double *solid_angle,
        double xi, double yi, double zi, double radius,
        _class_particle* _particle)
{
  double l2, phi, theta, nx, ny, nz, xt, yt, zt, xu, yu, zu;

  if(radius == 0.0)
  {
    /* No target, choose uniformly a direction in full 4PI solid angle. */
    theta = acos(1 - rand0max(2));
    phi = rand0max(2 * PI);
    if(solid_angle)
      *solid_angle = 4*PI;
    nx = 1;
    ny = 0;
    nz = 0;
    yi = sqrt(xi*xi+yi*yi+zi*zi);
    zi = 0;
    xi = 0;
  }
  else
  {
    double costheta0;
    l2 = xi*xi + yi*yi + zi*zi; /* sqr Distance to target. */
    costheta0 = sqrt(l2/(radius*radius+l2));
    if (radius < 0) costheta0 *= -1;
    if(solid_angle)
    {
      /* Compute solid angle of target as seen from origin. */
        *solid_angle = 2*PI*(1 - costheta0);
    }

    /* Now choose point uniformly on circle surface within angle theta0 */
    theta = acos (1 - rand0max(1 - costheta0)); /* radius on circle */
    phi = rand0max(2 * PI); /* rotation on circle at given radius */
    /* Now, to obtain the desired vector rotate (xi,yi,zi) angle theta around a
       perpendicular axis u=i x n and then angle phi around i. */
    if(xi == 0 && zi == 0)
    {
      nx = 1;
      ny = 0;
      nz = 0;
    }
    else
    {
      nx = -zi;
      nz = xi;
      ny = 0;
    }
  }

  /* [xyz]u = [xyz]i x n[xyz] (usually vertical) */
  vec_prod(xu,  yu,  zu, xi, yi, zi,        nx, ny, nz);
  /* [xyz]t = [xyz]i rotated theta around [xyz]u */
  rotate  (xt,  yt,  zt, xi, yi, zi, theta, xu, yu, zu);
  /* [xyz]o = [xyz]t rotated phi around n[xyz] */
  rotate (*xo, *yo, *zo, xt, yt, zt, phi, xi, yi, zi);
}
/* randvec_target_circle */

/*******************************************************************************
 * randvec_target_rect_angular: Choose random direction towards target at
 * (xi,yi,zi) with given ANGULAR dimension height x width. height=phi_x=[0,PI],
 * width=phi_y=[0,2*PI] (radians)
 * If height or width is zero, choose random direction in full 4PI, no target.
 *******************************************************************************/
void _randvec_target_rect_angular(double *xo, double *yo, double *zo, double *solid_angle,
        double xi, double yi, double zi, double width, double height, Rotation A,
        _class_particle* _particle)
{
  double theta, phi, nx, ny, nz, xt, yt, zt, xu, yu, zu;
  Coords tmp;
  Rotation Ainverse;

  rot_transpose(A, Ainverse);

  if(height == 0.0 || width == 0.0)
  {
    randvec_target_circle(xo, yo, zo, solid_angle, xi, yi, zi, 0);
    return;
  }
  else
  {
    if(solid_angle)
    {
      /* Compute solid angle of target as seen from origin. */
      *solid_angle = 2*fabs(width*sin(height/2));
    }

    /* Go to global coordinate system */

    tmp = coords_set(xi, yi, zi);
    tmp = rot_apply(Ainverse, tmp);
    coords_get(tmp, &xi, &yi, &zi);

    /* Now choose point uniformly on the unit sphere segment with angle theta/phi */
    phi   = width*randpm1()/2.0;
    theta = asin(randpm1()*sin(height/2.0));
    /* Now, to obtain the desired vector rotate (xi,yi,zi) angle theta around
       n, and then phi around u. */
    if(xi == 0 && zi == 0)
    {
      nx = 1;
      ny = 0;
      nz = 0;
    }
    else
    {
      nx = -zi;
      nz = xi;
      ny = 0;
    }
  }

  /* [xyz]u = [xyz]i x n[xyz] (usually vertical) */
  vec_prod(xu,  yu,  zu, xi, yi, zi,        nx, ny, nz);
  /* [xyz]t = [xyz]i rotated theta around [xyz]u */
  rotate  (xt,  yt,  zt, xi, yi, zi, theta, nx, ny, nz);
  /* [xyz]o = [xyz]t rotated phi around n[xyz] */
  rotate (*xo, *yo, *zo, xt, yt, zt, phi, xu,  yu,  zu);

  /* Go back to local coordinate system */
  tmp = coords_set(*xo, *yo, *zo);
  tmp = rot_apply(A, tmp);
  coords_get(tmp, &*xo, &*yo, &*zo);
}
/* randvec_target_rect_angular */

/*******************************************************************************
 * randvec_target_rect_real: Choose random direction towards target at (xi,yi,zi)
 * with given dimension height x width (in meters !).
 *
 * Local emission coordinate is taken into account and corrected for 'order' times.
 * (See remarks posted to mcstas-users by George Apostolopoulus <gapost@ipta.demokritos.gr>)
 *
 * If height or width is zero, choose random direction in full 4PI, no target.
 *
 * Traditionally, this routine had the name randvec_target_rect - this is now a
 * a define (see mcstas-r.h) pointing here. If you use the old rouine, you are NOT
 * taking the local emmission coordinate into account.
*******************************************************************************/
void _randvec_target_rect_real(double *xo, double *yo, double *zo, double *solid_angle,
        double xi, double yi, double zi,
        double width, double height, Rotation A,
        double lx, double ly, double lz, int order,
        _class_particle* _particle)
{
  double dx, dy, dist, dist_p, nx, ny, nz, mx, my, mz, n_norm, m_norm;
  double cos_theta;
  Coords tmp;
  Rotation Ainverse;

  rot_transpose(A, Ainverse);

  if(height == 0.0 || width == 0.0)
  {
    randvec_target_circle(xo, yo, zo, solid_angle,
               xi, yi, zi, 0);
    return;
  }
  else
  {
    /* Now choose point uniformly on rectangle within width x height */
    dx = width*randpm1()/2.0;
    dy = height*randpm1()/2.0;

    /* Determine distance to target plane*/
    dist = sqrt(xi*xi + yi*yi + zi*zi);
    /* Go to global coordinate system */

    tmp = coords_set(xi, yi, zi);
    tmp = rot_apply(Ainverse, tmp);
    coords_get(tmp, &xi, &yi, &zi);

    /* Determine vector normal to trajectory axis (z) and gravity [0 1 0] */
    vec_prod(nx, ny, nz, xi, yi, zi, 0, 1, 0);

    /* This now defines the x-axis, normalize: */
    n_norm=sqrt(nx*nx + ny*ny + nz*nz);
    nx = nx/n_norm;
    ny = ny/n_norm;
    nz = nz/n_norm;

    /* Now, determine our y-axis (vertical in many cases...) */
    vec_prod(mx, my, mz, xi, yi, zi, nx, ny, nz);
    m_norm=sqrt(mx*mx + my*my + mz*mz);
    mx = mx/m_norm;
    my = my/m_norm;
    mz = mz/m_norm;

    /* Our output, random vector can now be defined by linear combination: */

    *xo = xi + dx * nx + dy * mx;
    *yo = yi + dx * ny + dy * my;
    *zo = zi + dx * nz + dy * mz;

    /* Go back to local coordinate system */
    tmp = coords_set(*xo, *yo, *zo);
    tmp = rot_apply(A, tmp);
    coords_get(tmp, &*xo, &*yo, &*zo);

    /* Go back to local coordinate system */
    tmp = coords_set(xi, yi, zi);
    tmp = rot_apply(A, tmp);
    coords_get(tmp, &xi, &yi, &zi);

    if (solid_angle) {
      /* Calculate vector from local point to remote random point */
      lx = *xo - lx;
      ly = *yo - ly;
      lz = *zo - lz;
      dist_p = sqrt(lx*lx + ly*ly + lz*lz);

      /* Adjust the 'solid angle' */
      /* 1/r^2 to the chosen point times cos(\theta) between the normal */
      /* vector of the target rectangle and direction vector of the chosen point. */
      cos_theta = (xi * lx + yi * ly + zi * lz) / (dist * dist_p);
      *solid_angle = width * height / (dist_p * dist_p);
      int counter;
      for (counter = 0; counter < order; counter++) {
        *solid_angle = *solid_angle * cos_theta;
      }
    }
  }
}
/* randvec_target_rect_real */


/* SECTION: random numbers ==================================================

  How to add a new RNG:

  - Use an rng with a manegable state vector, e.g. of lengt 4 or 7. The state
  will sit on the particle struct as a "randstate_t state[RANDSTATE_LEN]"
  - If the rng has a long state (as MT), set an empty "srandom" and initialize
  it explicitly using the appropriate define (RNG_ALG)
  - Add a seed and a random function (the transforms will be reused)
  - Write the proper defines in mccode-r.h, e.g. randstate_t and RANDSTATE_LEN,
  srandom and random.
  - Compile using -DRNG_ALG=<selector int value>

============================================================================= */


/* "Mersenne Twister", by Makoto Matsumoto and Takuji Nishimura. */
/* See http://www.math.keio.ac.jp/~matumoto/emt.html for original source. */
/*
   A C-program for MT19937, with initialization improved 2002/1/26.
   Coded by Takuji Nishimura and Makoto Matsumoto.

   Before using, initialize the state by using mt_srandom(seed)
   or init_by_array(init_key, key_length).

   Copyright (C) 1997 - 2002, Makoto Matsumoto and Takuji Nishimura,
   All rights reserved.

   Redistribution and use in source and binary forms, with or without
   modification, are permitted provided that the following conditions
   are met:

     1. Redistributions of source code must retain the above copyright
        notice, this list of conditions and the following disclaimer.

     2. Redistributions in binary form must reproduce the above copyright
        notice, this list of conditions and the following disclaimer in the
        documentation and/or other materials provided with the distribution.

     3. The names of its contributors may not be used to endorse or promote
        products derived from this software without specific prior written
        permission.

   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
   "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
   LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
   A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT OWNER OR
   CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
   EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
   PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
   PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
   LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
   NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
   SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.


   Any feedback is very welcome.
   http://www.math.keio.ac.jp/matumoto/emt.html
   email: matumoto@math.keio.ac.jp
*/
#include <stdio.h>
#include <stdint.h>   // for uint32_t
#include <stddef.h>   // for size_t

/* Period parameters */
#define N 624
#define M 397
#define MATRIX_A 0x9908b0dfU   /* constant vector a */
#define UPPER_MASK 0x80000000U /* most significant w-r bits */
#define LOWER_MASK 0x7fffffffU /* least significant r bits */

static uint32_t mt[N]; /* the array for the state vector  */
static int mti = N + 1; /* mti==N+1 means mt[N] is not initialized */

// Required for compatibility with common RNG interface (e.g., kiss/mt polymorphism)
void mt_srandom_empty(void) {}

// Initializes mt[N] with a seed
void mt_srandom(uint32_t seed) {
    mt[0] = seed;
    for (mti = 1; mti < N; mti++) {
        mt[mti] = 1812433253U * (mt[mti-1] ^ (mt[mti-1] >> 30)) + mti;
        /* See Knuth TAOCP Vol2. 3rd Ed. P.106 for multiplier. */
        /* In the previous versions, MSBs of the seed affect   */
        /* only MSBs of the array mt[].                        */
        /* 2002/01/09 modified by Makoto Matsumoto             */
        mt[mti] &= 0xffffffffU;
        /* for >32 bit machines */
    }
}
/* Initialize by an array with array-length.
   Init_key is the array for initializing keys.
   key_length is its length. */
void init_by_array(uint32_t init_key[], size_t key_length) {
    size_t i = 1, j = 0, k;
    mt_srandom(19650218U);
    k = (N > key_length ? N : key_length);
    for (; k; k--) {
        mt[i] = (mt[i] ^ ((mt[i-1] ^ (mt[i-1] >> 30)) * 1664525U))
              + init_key[j] + (uint32_t)j;
        mt[i] &= 0xffffffffU;
        i++; j++;
        if (i >= N) { mt[0] = mt[N - 1]; i = 1; }
        if (j >= key_length) j = 0;
    }
    for (k = N - 1; k; k--) {
        mt[i] = (mt[i] ^ ((mt[i-1] ^ (mt[i-1] >> 30)) * 1566083941U))
              - (uint32_t)i;
        mt[i] &= 0xffffffffU;
        i++;
        if (i >= N) { mt[0] = mt[N - 1]; i = 1; }
    }
    mt[0] = 0x80000000U; /* MSB is 1; ensuring non-zero initial array */
}

// Generates a random number on [0, 0xffffffff]-interval
uint32_t mt_random(void) {
    uint32_t y;
    static const uint32_t mag01[2] = { 0x0U, MATRIX_A };
    /* mag01[x] = x * MATRIX_A  for x=0,1 */

    if (mti >= N) { /* generate N words at one time */
        int kk;

        if (mti == N + 1)   /* if mt_srandom() has not been called, */ 
            mt_srandom(5489U);  /* a default initial seed is used */

        for (kk = 0; kk < N - M; kk++) {
            y = (mt[kk] & UPPER_MASK) | (mt[kk + 1] & LOWER_MASK);
            mt[kk] = mt[kk + M] ^ (y >> 1) ^ mag01[y & 0x1U];
        }
        for (; kk < N - 1; kk++) {
            y = (mt[kk] & UPPER_MASK) | (mt[kk + 1] & LOWER_MASK);
            mt[kk] = mt[kk + (M - N)] ^ (y >> 1) ^ mag01[y & 0x1U];
        }
        y = (mt[N - 1] & UPPER_MASK) | (mt[0] & LOWER_MASK);
        mt[N - 1] = mt[M - 1] ^ (y >> 1) ^ mag01[y & 0x1U];

        mti = 0;
    }

    y = mt[mti++];

    /* Tempering */
    y ^= (y >> 11);
    y ^= (y << 7) & 0x9d2c5680U;
    y ^= (y << 15) & 0xefc60000U;
    y ^= (y >> 18);

    return y;
}
#undef N
#undef M
#undef MATRIX_A
#undef UPPER_MASK
#undef LOWER_MASK
/* End of "Mersenne Twister". */


/*
KISS

 From: http://www.helsbreth.org/random/rng_kiss.html
 Scott Nelson 1999

 Based on Marsaglia's KISS or (KISS+SWB) <http://www.cs.yorku.ca/~oz/marsaglia-
rng.html>

 KISS - Keep it Simple Stupid PRNG

 the idea is to use simple, fast, individually promising
 generators to get a composite that will be fast, easy to code
 have a very long period and pass all the tests put to it.
 The three components of KISS are
        x(n)=a*x(n-1)+1 mod 2^32
        y(n)=y(n-1)(I+L^13)(I+R^17)(I+L^5),
        z(n)=2*z(n-1)+z(n-2) +carry mod 2^32
 The y's are a shift register sequence on 32bit binary vectors
 period 2^32-1;
 The z's are a simple multiply-with-carry sequence with period
 2^63+2^32-1.  The period of KISS is thus
      2^32*(2^32-1)*(2^63+2^32-1) > 2^127

 In 2025 adapted for consistent 64-bit behavior across platforms.
*/

/* the KISS state is stored as a vector of 7 uint64_t        */
/*   0  1  2  3  4      5  6   */
/* [ x, y, z, w, carry, k, m ] */

uint64_t *kiss_srandom(uint64_t state[7], uint64_t seed) {
    if (seed == 0) seed = 1ull;
    state[0] = seed | 1ull; // x
    state[1] = seed | 2ull; // y
    state[2] = seed | 4ull; // z
    state[3] = seed | 8ull; // w
    state[4] = 0ull;        // carry
    state[5] = 0ull;        // k
    state[6] = 0ull;        // m
    return state;
}

uint64_t kiss_random(uint64_t state[7]) {
    // Linear congruential generator
    state[0] = state[0] * 69069ull + 1ull;

    // Xorshift
    state[1] ^= state[1] << 13ull;
    state[1] ^= state[1] >> 17ull;
    state[1] ^= state[1] << 5ull;

    // Multiply-with-carry
    state[5] = (state[2] >> 2ull) + (state[3] >> 3ull) + (state[4] >> 2ull);
    state[6] = state[3] + state[3] + state[2] + state[4];
    state[2] = state[3];
    state[3] = state[6];
    state[4] = state[5] >> 62ull;  // Top bit of carry (adjusted for 64-bit)

    return state[0] + state[1] + state[3];
}
/* end of "KISS" rng */


/* FAST KISS in another implementation (Hundt) */

//////////////////////////////////////////////////////////////////////////////
// fast keep it simple stupid generator
//////////////////////////////////////////////////////////////////////////////

/////////////////////////////////////////////////////////////////////////////
// Thomas Mueller hash for initialization of rngs
// http://stackoverflow.com/questions/664014/
//        what-integer-hash-function-are-good-that-accepts-an-integer-hash-key
//////////////////////////////////////////////////////////////////////////////
randstate_t _hash(randstate_t x) {
  x = ((x >> 16) ^ x) * (randstate_t)0x45d9f3b;
  x = ((x >> 16) ^ x) * (randstate_t)0x45d9f3b;
  x = ((x >> 16) ^ x);
  return x;
}


// SECTION: random number transforms ==========================================



// generate a random number from normal law
double _randnorm(randstate_t* state)
{
  static double v1, v2, s; /* removing static breaks comparison with McStas <= 2.5 */
  static int phase = 0;
  double X, u1, u2;

  if(phase == 0)
  {
    do
    {
      u1 = _rand01(state);
      u2 = _rand01(state);
      v1 = 2*u1 - 1;
      v2 = 2*u2 - 1;
      s = v1*v1 + v2*v2;
    } while(s >= 1 || s == 0);

    X = v1*sqrt(-2*log(s)/s);
  }
  else
  {
    X = v2*sqrt(-2*log(s)/s);
  }

  phase = 1 - phase;
  return X;
}
// another one
double _randnorm2(randstate_t* state) {
  double x, y, r;
  do {
      x = 2.0 * _rand01(state) - 1.0;
      y = 2.0 * _rand01(state) - 1.0;
      r = x*x + y*y;
  } while (r == 0.0 || r >= 1.0);
  return x * sqrt((-2.0 * log(r)) / r);
}

// Generate a random number from -1 to 1 with triangle distribution
double _randtriangle(randstate_t* state) {
	double randnum = _rand01(state);
	if (randnum>0.5) return(1-sqrt(2*(randnum-0.5)));
	else return(sqrt(2*randnum)-1);
}
double _rand01(randstate_t* state) {
	double randnum;
	randnum = (double) _random();
  // TODO: can we mult instead of div?
	randnum /= (double) MC_RAND_MAX + 1;
	return randnum;
}
// Return a random number between 1 and -1
double _randpm1(randstate_t* state) {
	double randnum;
	randnum = (double) _random();
	randnum /= ((double) MC_RAND_MAX + 1) / 2;
	randnum -= 1;
	return randnum;
}
// Return a random number between 0 and max.
double _rand0max(double max, randstate_t* state) {
	double randnum;
	randnum = (double) _random();
	randnum /= ((double) MC_RAND_MAX + 1) / max;
	return randnum;
}
// Return a random number between min and max.
double _randminmax(double min, double max, randstate_t* state) {
	return _rand0max(max - min, state) + max;
}


/* SECTION: main and signal handlers ======================================== */

/*******************************************************************************
* mchelp: displays instrument executable help with possible options
*******************************************************************************/
static void
mchelp(char *pgmname)
{
  int i;

  fprintf(stderr, "%s (%s) instrument simulation, generated with " MCCODE_STRING " (" MCCODE_DATE ")\n", instrument_name, instrument_source);
  fprintf(stderr, "Usage: %s [options] [parm=value ...]\n", pgmname);
  fprintf(stderr,
"Options are:\n"
"  -s SEED   --seed=SEED      Set random seed (must be != 0)\n"
"  -n COUNT  --ncount=COUNT   Set number of particles to simulate.\n"
"  -d DIR    --dir=DIR        Put all data files in directory DIR.\n"
"  -a        --append         Append data files to those in directory DIR.\n"	  
"  -t        --trace          Enable trace of " MCCODE_PARTICLE "s through instrument.\n"
"                             (Use -t=2 or --trace=2 for modernised mcdisplay rendering)\n"
"  -g        --gravitation    Enable gravitation for all trajectories.\n"
"  --no-output-files          Do not write any data files.\n"
"  -h        --help           Show this help message.\n"
"  -i        --info           Detailed instrument information.\n"
"  --list-parameters          Print the instrument parameters to standard out\n"
"  -y        --yes            Assume default values for all parameters with a default\n"
"  --meta-list                Print names of components which defined metadata\n"
"  --meta-defined COMP[:NAME] Print component defined metadata names, or (0,1) if NAME provided\n"
"  --meta-type COMP:NAME      Print metadata format type specified in definition\n"
"  --meta-data COMP:NAME      Print the metadata text\n"
"  --source                   Show the instrument code which was compiled.\n"
#ifdef OPENACC
"\n"
"  --vecsize                  OpenACC vector-size (default: 128)\n"
"  --numgangs                 Number of OpenACC gangs (default: 7813)\n"
"  --gpu_innerloop            Maximum rays to process pr. OpenACC \n"
"                             kernel run (default: 2147483647)\n"
"\n"
#endif
"\n"
"  --bufsiz                   Monitor_nD list/buffer-size (default: 1000000)\n"
"  --format=FORMAT            Output data files using FORMAT="
   FLAVOR_UPPER
#ifdef USE_NEXUS
   " NEXUS\n"
"  --IDF                      Embed an xml-formatted IDF instrument definition\n"
"                             in the NeXus file (if existent in .)\n\n"
#else
"\n\n"
#endif
);
#ifdef USE_MPI
  fprintf(stderr,
  "This instrument has been compiled with MPI support.\n  Use 'mpirun %s [options] [parm=value ...]'.\n", pgmname);
#endif
#ifdef OPENACC
  fprintf(stderr,
  "This instrument has been compiled with NVIDIA GPU support through OpenACC.\n  Running on systems without such devices will lead to segfaults.\nFurter, fprintf, sprintf and printf have been removed from any component TRACE.\n");
#endif

  if(numipar > 0)
  {
    fprintf(stderr, "Instrument parameters are:\n");
    for(i = 0; i < numipar; i++)
      if (mcinputtable[i].val && strlen(mcinputtable[i].val))
        fprintf(stderr, "  %-16s(%s) [default='%s']\n", mcinputtable[i].name,
        (*mcinputtypes[mcinputtable[i].type].parminfo)(mcinputtable[i].name),
        mcinputtable[i].val);
      else
        fprintf(stderr, "  %-16s(%s)\n", mcinputtable[i].name,
        (*mcinputtypes[mcinputtable[i].type].parminfo)(mcinputtable[i].name));
  }

#ifndef NOSIGNALS
  fprintf(stderr, "Known signals are: "
#ifdef SIGUSR1
  "USR1 (status) "
#endif
#ifdef SIGUSR2
  "USR2 (save) "
#endif
#ifdef SIGBREAK
  "BREAK (save) "
#endif
#ifdef SIGTERM
  "TERM (save and exit)"
#endif
  "\n");
#endif /* !NOSIGNALS */
} /* mchelp */


/* mcshowhelp: show help and exit with 0 */
static void
mcshowhelp(char *pgmname)
{
  mchelp(pgmname);
  exit(0);
}

/* mcusage: display usage when error in input arguments and exit with 1 */
static void
mcusage(char *pgmname)
{
  fprintf(stderr, "Error: incorrect command line arguments\n");
  mchelp(pgmname);
  exit(1);
}

/* mcenabletrace: enable trace/mcdisplay or error if requires recompile */
static void
mcenabletrace(int mode)
{
 if(traceenabled) {
  mcdotrace = mode;
  #pragma acc update device ( mcdotrace )
 } else {
   if (mode>0) {
     fprintf(stderr,
	     "Error: trace not enabled (mcenabletrace)\n"
	     "Please re-run the " MCCODE_NAME " compiler "
	     "with the --trace option, or rerun the\n"
	     "C compiler with the MC_TRACE_ENABLED macro defined.\n");
     exit(1);
   }
 }
}

/*******************************************************************************
* mcreadparams: request parameters from the prompt (or use default)
*******************************************************************************/
void
mcreadparams(void)
{
  int i,j,status;
  static char buf[CHAR_BUF_LENGTH];
  char *p;
  int len;

  MPI_MASTER(printf("Instrument parameters for %s (%s)\n",
                    instrument_name, instrument_source));

  for(i = 0; mcinputtable[i].name != 0; i++)
  {
    do
    {
      MPI_MASTER(
                 if (mcinputtable[i].val && strlen(mcinputtable[i].val))
                   printf("Set value of instrument parameter %s (%s) [default='%s']:\n",
                          mcinputtable[i].name,
                          (*mcinputtypes[mcinputtable[i].type].parminfo)
                          (mcinputtable[i].name), mcinputtable[i].val);
                 else
                   printf("Set value of instrument parameter %s (%s):\n",
                          mcinputtable[i].name,
                          (*mcinputtypes[mcinputtable[i].type].parminfo)
                          (mcinputtable[i].name));
                 fflush(stdout);
                 );
#ifdef USE_MPI
      if(mpi_node_rank == mpi_node_root)
        {
          p = fgets(buf, CHAR_BUF_LENGTH, stdin);
          if(p == NULL)
            {
              fprintf(stderr, "Error: empty input for paramater %s (mcreadparams)\n", mcinputtable[i].name);
              exit(1);
            }
        }
      else
        p = buf;
      MPI_Bcast(buf, CHAR_BUF_LENGTH, MPI_CHAR, mpi_node_root, MPI_COMM_WORLD);
#else /* !USE_MPI */
      p = fgets(buf, CHAR_BUF_LENGTH, stdin);
      if(p == NULL)
        {
          fprintf(stderr, "Error: empty input for paramater %s (mcreadparams)\n", mcinputtable[i].name);
          exit(1);
        }
#endif /* USE_MPI */
      len = strlen(buf);
      if (!len || (len == 1 && (buf[0] == '\n' || buf[0] == '\r')))
      {
        if (mcinputtable[i].val && strlen(mcinputtable[i].val)) {
          strncpy(buf, mcinputtable[i].val, CHAR_BUF_LENGTH);  /* use default value */
          len = strlen(buf);
        }
      }
      for(j = 0; j < 2; j++)
      {
        if(len > 0 && (buf[len - 1] == '\n' || buf[len - 1] == '\r'))
        {
          len--;
          buf[len] = '\0';
        }
      }

      status = (*mcinputtypes[mcinputtable[i].type].getparm)
                   (buf, mcinputtable[i].par);
      if(!status)
      {
        (*mcinputtypes[mcinputtable[i].type].error)(mcinputtable[i].name, buf);
        if (!mcinputtable[i].val || strlen(mcinputtable[i].val)) {
          fprintf(stderr, "       Change %s default value in instrument definition.\n", mcinputtable[i].name);
          exit(1);
        }
      }
    } while(!status);
  }
} /* mcreadparams */

/*******************************************************************************
* mcparseoptions: parse command line arguments (options, parameters)
*******************************************************************************/
void
mcparseoptions(int argc, char *argv[])
{
  int i, j;
  char *p;
  int paramset = 0, *paramsetarray;
  char *usedir=NULL;

  /* Add one to numipar to avoid allocating zero size memory block. */
  paramsetarray = (int*)malloc((numipar + 1)*sizeof(*paramsetarray));
  if(paramsetarray == NULL)
  {
    fprintf(stderr, "Error: insufficient memory (mcparseoptions)\n");
    exit(1);
  }
  for(j = 0; j < numipar; j++)
    {
      paramsetarray[j] = 0;
      if (mcinputtable[j].val != NULL && strlen(mcinputtable[j].val))
      {
        int  status;
        char buf[CHAR_BUF_LENGTH];
        strncpy(buf, mcinputtable[j].val, CHAR_BUF_LENGTH);
        status = (*mcinputtypes[mcinputtable[j].type].getparm)
                   (buf, mcinputtable[j].par);
        if(!status) fprintf(stderr, "Invalid '%s' default value %s in instrument definition (mcparseoptions)\n", mcinputtable[j].name, buf);
        else paramsetarray[j] = 1;
      } else {
        (*mcinputtypes[mcinputtable[j].type].getparm)
          (NULL, mcinputtable[j].par);
        paramsetarray[j] = 0;
      }
    }
  for(i = 1; i < argc; i++)
  {
    if(!strcmp("-s", argv[i]) && (i + 1) < argc)
      mcsetseed(argv[++i]);
    else if(!strncmp("-s", argv[i], 2))
      mcsetseed(&argv[i][2]);
    else if(!strcmp("--seed", argv[i]) && (i + 1) < argc)
      mcsetseed(argv[++i]);
    else if(!strncmp("--seed=", argv[i], 7))
      mcsetseed(&argv[i][7]);
    else if(!strcmp("-n", argv[i]) && (i + 1) < argc)
      mcsetn_arg(argv[++i]);
    else if(!strncmp("-n", argv[i], 2))
      mcsetn_arg(&argv[i][2]);
    else if(!strcmp("--ncount", argv[i]) && (i + 1) < argc)
      mcsetn_arg(argv[++i]);
    else if(!strncmp("--ncount=", argv[i], 9))
      mcsetn_arg(&argv[i][9]);
    else if(!strcmp("-d", argv[i]) && (i + 1) < argc)
      usedir=argv[++i];  /* will create directory after parsing all arguments (end of this function) */
    else if(!strncmp("-d", argv[i], 2))
      usedir=&argv[i][2];
    else if(!strcmp("--dir", argv[i]) && (i + 1) < argc)
      usedir=argv[++i];
    else if(!strncmp("-a", argv[i], 2))
      mcappend = 1;
    else if(!strcmp("--append", argv[i]))
      mcappend = 1;
    else if(!strncmp("--dir=", argv[i], 6))
      usedir=&argv[i][6];
    else if(!strcmp("-h", argv[i]))
      mcshowhelp(argv[0]);
    else if(!strcmp("--help", argv[i]) || !strcmp("--version", argv[i]))
      mcshowhelp(argv[0]);
    else if(!strcmp("-i", argv[i])) {
      mcformat=FLAVOR_UPPER;
      mcinfo();
    }
    else if(!strcmp("--info", argv[i]))
      mcinfo();
    else if (!strcmp("--list-parameters", argv[i]))
      mcparameterinfo();
    else if (!strcmp("--meta-list", argv[i]) && ((i+1) >= argc || argv[i+1][0] == '-')){
      //printf("Components with metadata defined:\n");
      exit(metadata_table_print_all_components(num_metadata, metadata_table) == 0);
    }
    else if (!strcmp("--meta-defined", argv[i]) && (i+1) < argc){
      exit(metadata_table_print_component_keys(num_metadata, metadata_table, argv[i+1]) == 0);
    }
    else if (!strcmp("--meta-type", argv[i]) && (i+1) < argc){
      char * literal_type = metadata_table_type(num_metadata, metadata_table, argv[i+1]);
      if (literal_type == NULL) exit(1);
      printf("%s\n", literal_type);
      exit(0);
    }
    else if (!strcmp("--meta-data", argv[i]) && (i+1) < argc){
      char * literal = metadata_table_literal(num_metadata, metadata_table, argv[i+1]);
      if (literal == NULL) exit(1);
      printf("%s\n", literal);
      exit(0);
    }
    else if(!strncmp("--trace=", argv[i], 8)) {
      mcenabletrace(atoi(&argv[i][8]));
    } else if(!strncmp("-t=", argv[i], 3) || !strcmp("--verbose", argv[i])) {
      mcenabletrace(atoi(&argv[i][3]));
    } else if(!strcmp("-t", argv[i]))
      mcenabletrace(1);
    else if(!strcmp("--trace", argv[i]) || !strcmp("--verbose", argv[i]))
      mcenabletrace(1);
    else if(!strcmp("--gravitation", argv[i]))
      mcgravitation = 1;
    else if(!strcmp("-g", argv[i]))
      mcgravitation = 1;
    else if(!strcmp("--yes", argv[i]))
      mcusedefaults = 1;
    else if(!strcmp("-y", argv[i]))
      mcusedefaults = 1;
    else if(!strncmp("--format=", argv[i], 9)) {
      mcformat=&argv[i][9];
    }
    else if(!strcmp("--format", argv[i]) && (i + 1) < argc) {
      mcformat=argv[++i];
    }
#ifdef USE_NEXUS
    else if(!strcmp("--IDF", argv[i])) {
      mcnexus_embed_idf = 1;
    }
#endif
    else if(!strncmp("--vecsize=", argv[i], 10)) {
      vecsize=atoi(&argv[i][10]);
    }    
    else if(!strcmp("--vecsize", argv[i]) && (i + 1) < argc) {
      vecsize=atoi(argv[++i]);
    }
    else if(!strncmp("--bufsiz=", argv[i], 9)) {
      MONND_BUFSIZ=atoi(&argv[i][9]);
    }
    else if(!strcmp("--bufsiz", argv[i]) && (i + 1) < argc) {
      MONND_BUFSIZ=atoi(argv[++i]);
    }
    else if(!strncmp("--numgangs=", argv[i], 11)) {
      numgangs=atoi(&argv[i][11]);
    }
    else if(!strcmp("--numgangs", argv[i]) && (i + 1) < argc) {
      numgangs=atoi(argv[++i]);
    }
    else if(!strncmp("--gpu_innerloop=", argv[i], 16)) {
      gpu_innerloop=(long)strtod(&argv[i][16], NULL);
    }
    else if(!strcmp("--gpu_innerloop", argv[i]) && (i + 1) < argc) {
      gpu_innerloop=(long)strtod(argv[++i], NULL);
    }

    else if(!strcmp("--no-output-files", argv[i]))
      mcdisable_output_files = 1;
    else if(!strcmp("--source", argv[i])) {
      printf("/* Source code %s from %s: */\n"
        "/******************************************************************************/\n"
        "%s\n"
        "/******************************************************************************/\n"
        "/* End of source code %s from %s */\n",
        instrument_name, instrument_source, instrument_code,
        instrument_name, instrument_source);
      exit(1);
    }
    else if(argv[i][0] != '-' && (p = strchr(argv[i], '=')) != NULL)
    {
      *p++ = '\0';

      for(j = 0; j < numipar; j++)
        if(!strcmp(mcinputtable[j].name, argv[i]))
        {
          int status;
          status = (*mcinputtypes[mcinputtable[j].type].getparm)(p,
                        mcinputtable[j].par);
          if(!status || !strlen(p))
          {
            (*mcinputtypes[mcinputtable[j].type].error)
              (mcinputtable[j].name, p);
            exit(1);
          }
          paramsetarray[j] = 1;
          paramset = 1;
          break;
        }
      if(j == numipar)
      {                                /* Unrecognized parameter name */
        fprintf(stderr, "Error: unrecognized parameter %s (mcparseoptions)\n", argv[i]);
        exit(1);
      }
    }
    else if(argv[i][0] == '-') {
      fprintf(stderr, "Error: unrecognized option argument %s (mcparseoptions). Ignored.\n", argv[i++]);
    }
    else {
      fprintf(stderr, "Error: unrecognized argument %s (mcparseoptions). Aborting.\n", argv[i]);
      mcusage(argv[0]);
    }
  }
  if (mcusedefaults) {
    MPI_MASTER(
     printf("Using all default parameter values\n");
    );
    for(j = 0; j < numipar; j++) {
      int status;
      if(mcinputtable[j].val && strlen(mcinputtable[j].val)){
	status = (*mcinputtypes[mcinputtable[j].type].getparm)(mcinputtable[j].val,
                        mcinputtable[j].par);
	paramsetarray[j] = 1;
	paramset = 1;
      }
    }
  }
  if(!paramset)
    mcreadparams();                /* Prompt for parameters if not specified. */
  else
  {
    for(j = 0; j < numipar; j++)
      if(!paramsetarray[j])
      {
        fprintf(stderr, "Error: Instrument parameter %s left unset (mcparseoptions)\n",
                mcinputtable[j].name);
        exit(1);
      }
  }
  free(paramsetarray);
#ifdef USE_MPI
  if (mcdotrace) mpi_node_count=1; /* disable threading when in trace mode */
#endif
  if (usedir && strlen(usedir) && !mcdisable_output_files) mcuse_dir(usedir);
} /* mcparseoptions */

#ifndef NOSIGNALS
/*******************************************************************************
* sighandler: signal handler that makes simulation stop, and save results
*******************************************************************************/
void sighandler(int sig)
{
  /* MOD: E. Farhi, Sep 20th 2001: give more info */
  time_t t1, t0;
#define SIG_SAVE 0
#define SIG_TERM 1
#define SIG_STAT 2
#define SIG_ABRT 3

  printf("\n# " MCCODE_STRING ": [pid %i] Signal %i detected", getpid(), sig);
#ifdef USE_MPI
  printf(" [proc %i]", mpi_node_rank);
#endif
#if defined(SIGUSR1) && defined(SIGUSR2) && defined(SIGKILL)
  if (!strcmp(mcsig_message, "sighandler") && (sig != SIGUSR1) && (sig != SIGUSR2))
  {
    printf("\n# Fatal : unrecoverable loop ! Suicide (naughty boy).\n");
    kill(0, SIGKILL); /* kill myself if error occurs within sighandler: loops */
  }
#endif
  switch (sig) {
#ifdef SIGINT
    case SIGINT : printf(" SIGINT (interrupt from terminal, Ctrl-C)"); sig = SIG_TERM; break;
#endif
#ifdef SIGILL
    case SIGILL  : printf(" SIGILL (Illegal instruction)"); sig = SIG_ABRT; break;
#endif
#ifdef SIGFPE
    case SIGFPE  : printf(" SIGFPE (Math Error)"); sig = SIG_ABRT; break;
#endif
#ifdef SIGSEGV
    case SIGSEGV : printf(" SIGSEGV (Mem Error)"); sig = SIG_ABRT; break;
#endif
#ifdef SIGTERM
    case SIGTERM : printf(" SIGTERM (Termination)"); sig = SIG_TERM; break;
#endif
#ifdef SIGABRT
    case SIGABRT : printf(" SIGABRT (Abort)"); sig = SIG_ABRT; break;
#endif
#ifdef SIGQUIT
    case SIGQUIT : printf(" SIGQUIT (Quit from terminal)"); sig = SIG_TERM; break;
#endif
#ifdef SIGTRAP
    case SIGTRAP : printf(" SIGTRAP (Trace trap)"); sig = SIG_ABRT; break;
#endif
#ifdef SIGPIPE
    case SIGPIPE : printf(" SIGPIPE (Broken pipe)"); sig = SIG_ABRT; break;
#endif
#ifdef SIGUSR1
    case SIGUSR1 : printf(" SIGUSR1 (Display info)"); sig = SIG_STAT; break;
#endif
#ifdef SIGUSR2
    case SIGUSR2 : printf(" SIGUSR2 (Save simulation)"); sig = SIG_SAVE; break;
#endif
#ifdef SIGHUP
    case SIGHUP  : printf(" SIGHUP (Hangup/update)"); sig = SIG_SAVE; break;
#endif
#ifdef SIGBUS
    case SIGBUS  : printf(" SIGBUS (Bus error)"); sig = SIG_ABRT; break;
#endif
#ifdef SIGURG
    case SIGURG  : printf(" SIGURG (Urgent socket condition)"); sig = SIG_ABRT; break;
#endif
#ifdef SIGBREAK
    case SIGBREAK: printf(" SIGBREAK (Break signal, Ctrl-Break)"); sig = SIG_SAVE; break;
#endif
    default : printf(" (look at signal list for signification)"); sig = SIG_ABRT; break;
  }
  printf("\n");
  printf("# Simulation: %s (%s) \n", instrument_name, instrument_source);
  printf("# Breakpoint: %s ", mcsig_message);
  if (strstr(mcsig_message, "Save") && (sig == SIG_SAVE))
    sig = SIG_STAT;
  SIG_MESSAGE("sighandler");
  if (mcget_ncount() == 0)
    printf("(0 %%)\n" );
  else
  {
    printf("%.2f %% (%10.1f/%10.1f)\n", 100.0*mcget_run_num()/mcget_ncount(), 1.0*mcget_run_num(), 1.0*mcget_ncount());
  }
  t0 = (time_t)mcstartdate;
  t1 = time(NULL);
  printf("# Date:      %s", ctime(&t1));
  printf("# Started:   %s", ctime(&t0));

  if (sig == SIG_STAT)
  {
    printf("# " MCCODE_STRING ": Resuming simulation (continue)\n");
    fflush(stdout);
    return;
  }
  else
  if (sig == SIG_SAVE)
  {
    printf("# " MCCODE_STRING ": Saving data and resume simulation (continue)\n");
    save(NULL);
    fflush(stdout);
    return;
  }
  else
  if (sig == SIG_TERM)
  {
    printf("# " MCCODE_STRING ": Finishing simulation (save results and exit)\n");
    finally();
    exit(0);
  }
  else
  {
    fflush(stdout);
    perror("# Last I/O Error");
    printf("# " MCCODE_STRING ": Simulation stop (abort).\n");
// This portion of the signal handling only works on UNIX
#if defined(__unix__) || defined(__APPLE__)
    signal(sig, SIG_DFL); /* force to use default sighandler now */
    kill(getpid(), sig);  /* and trigger it with the current signal */
#endif
    exit(-1);
  }
#undef SIG_SAVE
#undef SIG_TERM
#undef SIG_STAT
#undef SIG_ABRT

} /* sighandler */
#endif /* !NOSIGNALS */

#ifdef NEUTRONICS
/*Main neutronics function steers the McStas calls, initializes parameters etc */
/* Only called in case NEUTRONICS = TRUE */
void neutronics_main_(float *inx, float *iny, float *inz, float *invx, float *invy, float *invz, float *intime, float *insx, float *insy, float *insz, float *inw, float *outx, float *outy, float *outz, float *outvx, float *outvy, float *outvz, float *outtime, float *outsx, float *outsy, float *outsz, float *outwgt)
{

  extern double mcnx, mcny, mcnz, mcnvx, mcnvy, mcnvz;
  extern double mcnt, mcnsx, mcnsy, mcnsz, mcnp;

  /* External code governs iteration - McStas is iterated once per call to neutronics_main. I.e. below counter must be initiancated for each call to neutronics_main*/
  mcrun_num=0;

  time_t t;
  t = (time_t)mcstartdate;
  mcstartdate = t;  /* set start date before parsing options and creating sim file */
  init();

  /* *** parse options *** */
  SIG_MESSAGE("[" __FILE__ "] main START");
  mcformat=getenv(FLAVOR_UPPER "_FORMAT") ?
           getenv(FLAVOR_UPPER "_FORMAT") : FLAVOR_UPPER;

  /* Set neutron state based on input from neutronics code */
  mcsetstate(*inx,*iny,*inz,*invx,*invy,*invz,*intime,*insx,*insy,*insz,*inw);

  /* main neutron event loop - runs only one iteration */

  //mcstas_raytrace(&mcncount); /* prior to McStas 1.12 */

  mcallowbackprop = 1; //avoid absorbtion from negative dt
  int argc=1;
  char *argv[0];
  int dummy = mccode_main(argc, argv);

  *outx =  mcnx;
  *outy =  mcny;
  *outz =  mcnz;
  *outvx =  mcnvx;
  *outvy =  mcnvy;
  *outvz =  mcnvz;
  *outtime =  mcnt;
  *outsx =  mcnsx;
  *outsy =  mcnsy;
  *outsz =  mcnsz;
  *outwgt =  mcnp;

  return;
} /* neutronics_main */

#endif /*NEUTRONICS*/

#endif /* !MCCODE_H */
/* End of file "mccode-r.c". */
/* End of file "mccode-r.c". */

/* embedding file "mcstas-r.c" */

/*******************************************************************************
*
* McStas, neutron ray-tracing package
*         Copyright (C) 1997-2009, All rights reserved
*         Risoe National Laboratory, Roskilde, Denmark
*         Institut Laue Langevin, Grenoble, France
*
* Runtime: share/mcstas-r.c
*
* %Identification
* Written by: KN
* Date:    Aug 29, 1997
* Release: McStas X.Y
* Version: $Revision$
*
* Runtime system for McStas.
* Embedded within instrument in runtime mode.
*
* Usage: Automatically embbeded in the c code whenever required.
*
* $Id$
*
*******************************************************************************/

#ifndef MCSTAS_R_H
#include "mcstas-r.h"
#endif
#ifdef DANSE
#include "mcstas-globals.h"
#endif

/*******************************************************************************
* The I/O format definitions and functions
*******************************************************************************/

/*the magnet stack*/
#ifdef MC_POL_COMPAT
void (*mcMagnetPrecession) (double, double, double, double, double, double,
    double, double*, double*, double*, double, Coords, Rotation)=NULL;
Coords   mcMagnetPos;
Rotation mcMagnetRot;
double*  mcMagnetData                = NULL;
/* mcMagneticField(x, y, z, t, Bx, By, Bz) */
int (*mcMagneticField) (double, double, double, double,
    double*, double*, double*, void *) = NULL;
#endif

#ifndef MCSTAS_H

/*******************************************************************************
* mcsetstate: transfer parameters into global McStas variables
*******************************************************************************/
_class_particle mcsetstate(double x, double y, double z, double vx, double vy, double vz,
			   double t, double sx, double sy, double sz, double p, int mcgravitation, void *mcMagnet, int mcallowbackprop)
{
  _class_particle mcneutron;

  mcneutron.x  = x;
  mcneutron.y  = y;
  mcneutron.z  = z;
  mcneutron.vx = vx;
  mcneutron.vy = vy;
  mcneutron.vz = vz;
  mcneutron.t  = t;
  mcneutron.sx = sx;
  mcneutron.sy = sy;
  mcneutron.sz = sz;
  mcneutron.p  = p;
  mcneutron.mcgravitation = mcgravitation;
  mcneutron.mcMagnet = mcMagnet;
  mcneutron.allow_backprop = mcallowbackprop;
  mcneutron._uid       = 0;
  mcneutron._index     = 1;
  mcneutron._absorbed  = 0;
  mcneutron._restore   = 0;
  mcneutron._scattered = 0;
  mcneutron.flag_nocoordschange = 0;
  
  /* init tmp-vars - FIXME are they used? */
  mcneutron._mctmp_a = mcneutron._mctmp_b =  mcneutron._mctmp_c = 0;
  // what about mcneutron._logic ?
  mcneutron._logic.dummy=1;
  // init uservars via cogen'd-function
  particle_uservar_init(&mcneutron);

  return(mcneutron);
} /* mcsetstate */

/*******************************************************************************
* mcgetstate: get neutron parameters from particle structure
*******************************************************************************/
_class_particle mcgetstate(_class_particle mcneutron, double *x, double *y, double *z,
               double *vx, double *vy, double *vz, double *t,
               double *sx, double *sy, double *sz, double *p)
{
  *x  =  mcneutron.x;
  *y  =  mcneutron.y;
  *z  =  mcneutron.z;
  *vx =  mcneutron.vx;
  *vy =  mcneutron.vy;
  *vz =  mcneutron.vz;
  *t  =  mcneutron.t;
  *sx =  mcneutron.sx;
  *sy =  mcneutron.sy;
  *sz =  mcneutron.sz;
  *p  =  mcneutron.p;

  return(mcneutron);
} /* mcgetstate */


/*******************************************************************************
* mcgenstate: set default neutron parameters
*******************************************************************************/
// Moved to generated code
/* #pragma acc routine seq */
/* _class_particle mcgenstate(void) */
/* { */
/*   return(mcsetstate(0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, mcgravitation, mcMagnet, mcallowbackprop)); */
/* } */

/*******************************************************************************
* mccoordschanges: old style rotation routine rot -> (x y z) ,(vx vy vz),(sx,sy,sz)
*******************************************************************************/
void
mccoordschanges(Coords a, Rotation t, double *x, double *y, double *z,
               double *vx, double *vy, double *vz, double *sx, double *sy, double *sz)
{
  Coords b, c;

  b.x = *x;
  b.y = *y;
  b.z = *z;
  c = rot_apply(t, b);
  b = coords_add(c, a);
  *x = b.x;
  *y = b.y;
  *z = b.z;

  if ( (vz && vy  && vx) && (*vz != 0.0 || *vx != 0.0 || *vy != 0.0) )
    mccoordschange_polarisation(t, vx, vy, vz);

  if ( (sz && sy  && sx) && (*sz != 0.0 || *sx != 0.0 || *sy != 0.0) )
    mccoordschange_polarisation(t, sx, sy, sz);

}

/* intersection routines ==================================================== */

/*******************************************************************************
* inside_rectangle: Check if (x,y) is inside rectangle (xwidth, yheight)
* return 0 if outside and 1 if inside
*******************************************************************************/
int inside_rectangle(double x, double y, double xwidth, double yheight)
{
  if (x>-xwidth/2 && x<xwidth/2 && y>-yheight/2 && y<yheight/2)
    return 1;
  else
    return 0;
}

/*******************************************************************************
 * box_intersect: compute time intersection with a box
 * returns 0 when no intersection is found
 *      or 1 in case of intersection with resulting times dt_in and dt_out
 * This function written by Stine Nyborg, 1999.
 *******************************************************************************/
int box_intersect(double *dt_in, double *dt_out,
                  double x, double y, double z,
                  double vx, double vy, double vz,
                  double dx, double dy, double dz)
{
  double x_in, y_in, z_in, tt, t[6], a, b;
  int i, count, s;

      /* Calculate intersection time for each of the six box surface planes
       *  If the box surface plane is not hit, the result is zero.*/

  if(vx != 0)
   {
    tt = -(dx/2 + x)/vx;
    y_in = y + tt*vy;
    z_in = z + tt*vz;
    if( y_in > -dy/2 && y_in < dy/2 && z_in > -dz/2 && z_in < dz/2)
      t[0] = tt;
    else
      t[0] = 0;

    tt = (dx/2 - x)/vx;
    y_in = y + tt*vy;
    z_in = z + tt*vz;
    if( y_in > -dy/2 && y_in < dy/2 && z_in > -dz/2 && z_in < dz/2)
      t[1] = tt;
    else
      t[1] = 0;
   }
  else
    t[0] = t[1] = 0;

  if(vy != 0)
   {
    tt = -(dy/2 + y)/vy;
    x_in = x + tt*vx;
    z_in = z + tt*vz;
    if( x_in > -dx/2 && x_in < dx/2 && z_in > -dz/2 && z_in < dz/2)
      t[2] = tt;
    else
      t[2] = 0;

    tt = (dy/2 - y)/vy;
    x_in = x + tt*vx;
    z_in = z + tt*vz;
    if( x_in > -dx/2 && x_in < dx/2 && z_in > -dz/2 && z_in < dz/2)
      t[3] = tt;
    else
      t[3] = 0;
   }
  else
    t[2] = t[3] = 0;

  if(vz != 0)
   {
    tt = -(dz/2 + z)/vz;
    x_in = x + tt*vx;
    y_in = y + tt*vy;
    if( x_in > -dx/2 && x_in < dx/2 && y_in > -dy/2 && y_in < dy/2)
      t[4] = tt;
    else
      t[4] = 0;

    tt = (dz/2 - z)/vz;
    x_in = x + tt*vx;
    y_in = y + tt*vy;
    if( x_in > -dx/2 && x_in < dx/2 && y_in > -dy/2 && y_in < dy/2)
      t[5] = tt;
    else
      t[5] = 0;
   }
  else
    t[4] = t[5] = 0;

  /* The intersection is evaluated and *dt_in and *dt_out are assigned */

  a = b = s = 0;
  count = 0;

  for( i = 0; i < 6; i = i + 1 )
    if( t[i] == 0 )
      s = s+1;
    else if( count == 0 )
    {
      a = t[i];
      count = 1;
    }
    else
    {
      b = t[i];
      count = 2;
    }

  if ( a == 0 && b == 0 )
    return 0;
  else if( a < b )
  {
    *dt_in = a;
    *dt_out = b;
    return 1;
  }
  else
  {
    *dt_in = b;
    *dt_out = a;
    return 1;
  }

} /* box_intersect */

/*******************************************************************************
 * cylinder_intersect: compute intersection with a cylinder
 * returns 0 when no intersection is found
 *      or 2/4/8/16 bits depending on intersection,
 *     and resulting times t0 and t1
 * Written by: EM,NB,ABA 4.2.98
  *******************************************************************************/
int cylinder_intersect(double *t0, double *t1, double x, double y, double z,
                   double vx, double vy, double vz, double r, double h)
{
  double D, t_in, t_out, y_in, y_out;
  int ret=1;

  D = (2*vx*x + 2*vz*z)*(2*vx*x + 2*vz*z)
    - 4*(vx*vx + vz*vz)*(x*x + z*z - r*r);

  if (D>=0)
  {
    if (vz*vz + vx*vx) {
      t_in  = (-(2*vz*z + 2*vx*x) - sqrt(D))/(2*(vz*vz + vx*vx));
      t_out = (-(2*vz*z + 2*vx*x) + sqrt(D))/(2*(vz*vz + vx*vx));
    } else if (vy) { /* trajectory parallel to cylinder axis */
      t_in = (-h/2-y)/vy;
      t_out = (h/2-y)/vy;
      if (t_in>t_out){
        double tmp=t_in;
        t_in=t_out;t_out=tmp;
      }
    } else return 0;
    y_in = vy*t_in + y;
    y_out =vy*t_out + y;

    if ( (y_in > h/2 && y_out > h/2) || (y_in < -h/2 && y_out < -h/2) )
      return 0;
    else
    {
      if (y_in > h/2)
        { t_in = ((h/2)-y)/vy; ret += 2; }
      else if (y_in < -h/2)
        { t_in = ((-h/2)-y)/vy; ret += 4; }
      if (y_out > h/2)
        { t_out = ((h/2)-y)/vy; ret += 8; }
      else if (y_out < -h/2)
        { t_out = ((-h/2)-y)/vy; ret += 16; }
    }
    *t0 = t_in;
    *t1 = t_out;
    return ret;
  }
  else
  {
    *t0 = *t1 = 0;
    return 0;
  }
} /* cylinder_intersect */


/*******************************************************************************
 * sphere_intersect: Calculate intersection between a line and a sphere.
 * returns 0 when no intersection is found
 *      or 1 in case of intersection with resulting times t0 and t1
 *******************************************************************************/
int sphere_intersect(double *t0, double *t1, double x, double y, double z,
                 double vx, double vy, double vz, double r)
{
  double A, B, C, D, v;

  v = sqrt(vx*vx + vy*vy + vz*vz);
  A = v*v;
  B = 2*(x*vx + y*vy + z*vz);
  C = x*x + y*y + z*z - r*r;
  D = B*B - 4*A*C;
  if(D < 0)
    return 0;
  D = sqrt(D);
  *t0 = (-B - D) / (2*A);
  *t1 = (-B + D) / (2*A);
  return 1;
} /* sphere_intersect */

/*******************************************************************************
 * plane_intersect: Calculate intersection between a plane and a line.
 * returns 0 when no intersection is found (i.e. line is parallel to the plane)
 * returns 1 or -1 when intersection time is positive and negative respectively
 *******************************************************************************/
int plane_intersect(double *t, double x, double y, double z,
                 double vx, double vy, double vz, double nx, double ny, double nz, double wx, double wy, double wz)
{
  double s;
  if (fabs(s=scalar_prod(nx,ny,nz,vx,vy,vz))<FLT_EPSILON) return 0;
  *t = - scalar_prod(nx,ny,nz,x-wx,y-wy,z-wz)/s;
  if (*t<0) return -1;
  else return 1;
} /* plane_intersect */

#endif /* !MCSTAS_H */
/* End of file "mcstas-r.c". */


/* *****************************************************************************
* Start of instrument 'Conditional_test' generated code
***************************************************************************** */

#ifdef MC_TRACE_ENABLED
int traceenabled = 1;
#else
int traceenabled = 0;
#endif
#define MCSTAS "/u/data/pkwi/WORK/micromamba/envs/mcstas-3.x/share/mcstas/resources/"
int   defaultmain         = 1;
char  instrument_name[]   = "Conditional_test";
char  instrument_source[] = "Conditional_test.instr";
char *instrument_exe      = NULL; /* will be set to argv[0] in main */
char  instrument_code[]   = "Instrument Conditional_test source code Conditional_test.instr is not embedded in this executable.\n  Use --source option when running mcstas.\n";

int main(int argc, char *argv[]){return mccode_main(argc, argv);}

/* *****************************************************************************
* instrument 'Conditional_test' and components DECLARE
***************************************************************************** */

/* Instrument parameters: structure and a table for the initialisation
   (Used in e.g. inputparse and I/O function (e.g. detector_out) */

struct _struct_instrument_parameters {
  MCNUM dummy;
};
typedef struct _struct_instrument_parameters _class_instrument_parameters;

struct _instrument_struct {
  char   _name[256]; /* the name of this instrument e.g. 'Conditional_test' */
/* Counters per component instance */
  double counter_AbsorbProp[31]; /* absorbed events in PROP routines */
  double counter_N[31], counter_P[31], counter_P2[31]; /* event counters after each component instance */
  _class_particle _trajectory[31]; /* current trajectory for STORE/RESTORE */
/* Components position table (absolute and relative coords) */
  Coords _position_relative[31]; /* positions of all components */
  Coords _position_absolute[31];
  _class_instrument_parameters _parameters; /* instrument parameters */
} _instrument_var;
struct _instrument_struct *instrument = & _instrument_var;
#pragma acc declare create ( _instrument_var )
#pragma acc declare create ( instrument )

int numipar = 1;
struct mcinputtable_struct mcinputtable[] = {
  "dummy", &(_instrument_var._parameters.dummy), instr_type_double, "1", "",
  NULL, NULL, instr_type_double, ""
};

struct metadata_table_struct metadata_table[] = {
  "", "", "", ""
};
int num_metadata = 0;

/* ************************************************************************** */
/*             SHARE user declarations for all components                     */
/* ************************************************************************** */

/* Shared user declarations for all components types 'Union_init'. */

  #ifdef Union
  #error "The Union_init component needs to be the first Union component!"
  // printf("ERROR: The Union_init component needs to be the first Union component!\n");
  // exit(1);
  #else
  #define Union $Revision: 0.8 $
/*******************************************************************************
*
*  McStas, neutron ray-tracing package
*  Copyright(C) 2007 Risoe National Laboratory.
*
* %I
* Written by: Mads Bertelsen
* Date: 20.08.15
* Version: $Revision: 0.1 $
* Origin: University of Copenhagen
*
* Functions and structure definitons for Union components.
*
******************************************************************************/

// -------------    Definition of data structures   ---------------------------------------------
// GPU
enum shape {
  surroundings,
  box,
  sphere,
  cylinder,
  cone,
  mesh
};

enum process {
  Incoherent,
  Powder,
  Single_crystal,
  AF_HB_1D,
  PhononSimple,
  Texture,
  IncoherentPhonon,
  NCrystal,
  Non,
  Template
};

enum surface {
  Mirror,
  SurfaceTemplate  	
};

enum in_or_out {
	inward_bound,
	outward_bound
};

struct intersection_time_table_struct {
int num_volumes;
int *calculated;
int *n_elements;
double **intersection_times;
double **normal_vector_x;
double **normal_vector_y;
double **normal_vector_z;
int **surface_index;
};

struct line_segment{
Coords point1;
Coords point2;
int number_of_dashes;
};

struct pointer_to_1d_int_list {
int num_elements;
int *elements;
#pragma acc shape(elements[0:num_elements]) init_needed(num_elements)
};

struct pointer_to_1d_double_list {
int num_elements;
double *elements;
#pragma acc shape(elements[0:num_elements]) init_needed(num_elements)
};

struct pointer_to_1d_coords_list {
int num_elements;
Coords *elements;
#pragma acc shape(elements[0:num_elements]) init_needed(num_elements)
};

struct lines_to_draw{
int number_of_lines;
struct line_segment *lines;
#pragma acc shape(lines[0:number_of_lines]) init_needed(number_of_lines)
};

// Todo: see if the union geometry_parameter_union and other geometry structs can be here
union geometry_parameter_union{
    struct sphere_storage   *p_sphere_storage;
    struct cylinder_storage *p_cylinder_storage;
    struct box_storage      *p_box_storage;
    struct cone_storage     *p_cone_storage;
    struct mesh_storage     *p_mesh_storage;
    // add as many pointers to structs as wanted, without increasing memory footprint.
};


struct rotation_struct{
double x;
double y;
double z;
};

struct focus_data_struct {
Coords RayAim; // Vector from ray position (within geometry) to target
Coords Aim; // Vector from geometry to target
double angular_focus_width;
double angular_focus_height;
double spatial_focus_width;
double spatial_focus_height;
double spatial_focus_radius;
Rotation absolute_rotation;
// focusing_function creates a vector per selected criteria of focus_data_struct / selected focus function and returns solid angle
void (*focusing_function)(Coords*, double*, struct focus_data_struct*);
//                        v_out  , solid_a,
};

struct focus_data_array_struct
{
struct focus_data_struct *elements;
int num_elements;
#pragma acc shape(elements[0:num_elements]) init_needed(num_elements)
};

struct Detector_3D_struct {
  char title_string[256];
  char string_axis_1[256];
  char string_axis_2[256];
  char string_axis_3[256];
  char Filename[256];
  double D1min;
  double D1max;
  double D2min;
  double D2max;
  double D3min;
  double D3max;
  double bins_1; // McStas uses doubles for bin numbers for some reason
  double bins_2;
  double bins_3;
  double ***Array_N; // McStas uses doubles for number of rays in each bin for some reason
  double ***Array_p;
  double ***Array_p2;
};

struct Detector_2D_struct {
  char title_string[256];
  char string_axis_1[256];
  char string_axis_2[256];
  char Filename[256];
  double D1min;
  double D1max;
  double D2min;
  double D2max;
  double bins_1; // McStas uses doubles for bin numbers for some reason
  double bins_2;
  double **Array_N; // McStas uses doubles for number of rays in each bin for some reason
  double **Array_p;
  double **Array_p2;
};

struct Detector_1D_struct {
  char title_string[256];
  char string_axis[256];
  char string_axis_short[64];
  char string_axis_value[256];
  char Filename[256];
  double min;
  double max;
  double bins; // McStas uses doubles for bin numbers for some reason
  double *Array_N; // McStas uses doubles for number of rays in each bin for some reason
  double *Array_p;
  double *Array_p2;
};


union logger_data_union{
  struct a_2DQ_storage_struct *p_2DQ_storage;
  struct a_2DS_storage_struct *p_2DS_storage;
  struct a_3DS_storage_struct *p_3DS_storage;
  struct a_1D_storage_struct *p_1D_storage;
  struct a_2DS_t_storage_struct *p_2DS_t_storage;
  struct a_2D_kf_storage_struct *p_2D_kf_storage;
  struct a_2D_kf_t_storage_struct *p_2D_kf_t_storage;
  // Additional logger storage structs to be addedd
};

struct logger_with_data_struct {
  int used_elements;
  int allocated_elements;
  struct logger_struct **logger_pointers;
};

// logger_pointer_struct
// contains pointers to the different logger functions and it's data union
struct logger_pointer_set_struct {
  // The logger has two record functions, an active and an inactive. Normally the active one will be to permanent storage,
  //  but if a conditional has been defined, it can switch the two, making the active one recording to temporary, which
  //  can then be filtered based on the future path of the ray
  
  // function input Coords position, k[3], k_old[3], p, p_old, NV, NPV, N, logger_data_union, logger_with_data_struct
  void (*active_record_function)(Coords*, double*, double*, double, double, double, int, int, int, struct logger_struct*, struct logger_with_data_struct*);
  void (*inactive_record_function)(Coords*, double*, double*, double, double, double, int, int, int, struct logger_struct*, struct logger_with_data_struct*);
  
  // A clear temporary data function (for new ray)
  void (*clear_temp)(union logger_data_union*);
  
  // Write temporary to permanent is used when the record to temp function is active, and the condition is met.
  void (*temp_to_perm)(union logger_data_union*);
  
  // Write temporary final_p to permanent is used when the record to temp function is active, and the condition is met
  //  and the final weight is given to be used for all stored events in the logger.
  void (*temp_to_perm_final_p)(union logger_data_union*, double);
  
  // Select which temp_to_perm function to use
  int select_t_to_p; // 1: temp_to_perm, 2: temp_to_perm_final_p
  
};

union abs_logger_data_union{
  struct a_2D_abs_storage_struct *p_2D_abs_storage;
  struct a_1D_abs_storage_struct *p_1D_abs_storage;
  struct a_1D_time_abs_storage_struct *p_1D_time_abs_storage;
  struct a_1D_time_to_lambda_abs_storage_struct *p_1D_time_to_lambda_abs_storage;
  struct a_event_abs_storage_struct *p_event_abs_storage;
  struct a_1D_event_abs_storage_struct *p_1D_event_abs_storage;
  struct a_nD_abs_storage_struct *p_nD_abs_storage;
  struct a_time_abs_storage_struct  *p_time_abs_storage;
  // Additional logger storage structs to be addedd
};

struct abs_logger_with_data_struct {
  int used_elements;
  int allocated_elements;
  struct abs_logger_struct **abs_logger_pointers;
};

struct abs_logger_pointer_set_struct {
  // The logger has two record functions, an active and an inactive. Normally the active one will be to permanent storage,
  //  but if a conditional has been defined, it can switch the two, making the active one recording to temporary, which
  //  can then be filtered based on the future path of the ray
  
  // function input Coords position, k[3], p, NV, N, logger_data_union, logger_with_data_struct
  void (*active_record_function)(Coords*, double*, double,  double, int, int, struct abs_logger_struct*, struct abs_logger_with_data_struct*);
  void (*inactive_record_function)(Coords*, double*, double,  double, int, int, struct abs_logger_struct*, struct abs_logger_with_data_struct*);
  
  // A clear temporary data function (for new ray)
  void (*clear_temp)(union abs_logger_data_union*);
  
  // Write temporary to permanent is used when the record to temp function is active, and the condition is met.
  void (*temp_to_perm)(union abs_logger_data_union*);
  
  // Write temporary final_p to permanent is used when the record to temp function is active, and the condition is met
  //  and the final weight is given to be used for all stored events in the logger.
  void (*temp_to_perm_final_p)(union abs_logger_data_union*, double);
  
  // Select which temp_to_perm function to use
  //int select_t_to_p; // 1: temp_to_perm, 2: temp_to_perm_final_p
  
};


struct conditional_standard_struct{
  // Data to be transfered to the conditional function
  double Emax;
  double Emin;
  int E_limit;
  
  double Tmin;
  double Tmax;
  int T_limit;
  
  int volume_index;
  
  double Total_scat_max;
  double Total_scat_min;
  int Total_scat_limit;
  
  double exit_volume_index;
  
  // Test
  Coords test_position;
  Rotation test_rotation;
  Rotation test_t_rotation;
};

struct conditional_PSD_struct{
  double PSD_half_xwidth;
  double PSD_half_yheight;
  
  double Tmin;
  double Tmax;
  int T_limit;
  
  // Position of the PSD
  Coords PSD_position;
  Rotation PSD_rotation;
  Rotation PSD_t_rotation;
};

union conditional_data_union {
  struct conditional_standard_struct *p_standard;
  struct conditional_PSD_struct *p_PSD;
  // Add more as conditional components are made
};

// General input for conditional functions: Position, Velocity/Wavevector, weight, time, total_scat, scattered_flag, scattered_flag_VP,
// Optional extras: data_union? tree base(s)? tree base for current ray?
typedef int (*conditional_function_pointer)(union conditional_data_union*,Coords*, Coords*, double*, double*, int*, int*, int*, int**);
//typedef int (**conditional_function_pointer_array)(union *conditional_data_union,Coords*, Coords*, double*, double*, int*, int*, int**);

struct conditional_list_struct{
  int num_elements;
  
  union conditional_data_union **p_data_unions;
  conditional_function_pointer *conditional_functions;
  //int (**conditional_functions)(Coords*, Coords*, double*, double*, int*, int*, int**);
};

struct logger_struct {
  char name[256];
  // Contains ponters to all the functions assosiated with this logger
  struct logger_pointer_set_struct function_pointers;
  // Contains hard copy of logger_data_union since the size is the same as a pointer.
  union logger_data_union data_union;
  
  int logger_extend_index; // Contain index conditional_extend_array defined in master that can be acsessed from extend section.
  
  struct conditional_list_struct conditional_list;
};


// To be stored in volume, a list of pointers to the relevant loggers corresponding to each process
struct logger_for_each_process_list {
  int num_elements;
  struct logger_struct **p_logger_process;
};

// List of logger_for_each_process_list
struct loggers_struct {
  int num_elements;
  struct logger_for_each_process_list *p_logger_volume;
  #pragma acc shape(p_logger_volume[0:num_elements]) init_needed(num_elements)
};


struct abs_logger_struct {
  char name[256];
  // Contains ponters to all the functions assosiated with this logger
  struct abs_logger_pointer_set_struct function_pointers;
  // Contains hard copy of logger_data_union since the size is the same as a pointer.
  union abs_logger_data_union data_union;
  
  // Position and rotation of the abs_logger
  Coords position;
  Rotation rotation;
  Rotation t_rotation;
  
  int abs_logger_extend_index; // Contain index conditional_extend_array defined in master that can be acsessed from extend section.
  
  struct conditional_list_struct conditional_list;
};

// To be stored in volume, a list of pointers to the relevant abs loggers corresponding to each process
/*
struct abs_logger_for_each_process_list {
  int num_elements;
  struct abs_logger_struct **p_abs_logger_process;
};
*/

// List of abs logger_for_each_process_list
struct abs_loggers_struct {
  int num_elements;
  //struct abs_logger_for_each_process_list *p_abs_logger_volume;
  struct abs_logger_struct **p_abs_logger;
};



struct geometry_struct
{
char shape[64];     // name of shape used (sphere, cylinder, box, off, ...)
enum shape eShape;  // enum with shape for flexible functions GPU
double priority_value;    // priority of the geometry
Coords center;      // Center position of volume, reported by components in global frame, updated to main frame in initialize
// Rotation of this volume
Rotation rotation_matrix; // rotation matrix of volume, reported by component in global frame, updated to main frame in initialize
Rotation transpose_rotation_matrix; // As above
// Array of prrotation matrixes for processes assigned to this volume (indexed by non_isotropic_rot_index in the processes)
Rotation *process_rot_matrix_array;             // matrix that transforms from main coordinate system to local process in this specific volume
Rotation *transpose_process_rot_matrix_array;   // matrix that transforms from local process in this specific volume to main coordinate system
int process_rot_allocated;  // Keeps track of allocation status of rot_matrix_array

struct rotation_struct rotation; // Not used, is the x y and z rotation angles.
int visualization_on; // If visualization_on is true, the volume will be drawn in mcdisplay, otherwise not
int is_exit_volume; // If is exit volume = 1, the ray will exit the component when it enters this volume.
int is_mask_volume; // 1 if volume itself is a mask (masking the ones in it's mask list), otherwise 0
int mask_index;
int is_masked_volume; // 1 if this volume is being masked by another volume, the volumes that mask it is in masked_by_list
int mask_mode; // ALL/ANY 1/2. In ALL mode, only parts covered by all masks is simulated, in ANY mode, area covered by just one mask is simulated
int skip_hierarchy_optimization;
double geometry_p_interact; // fraction of rays that interact with this volume for each scattering (between 0 and 1, 0 for disable)
union geometry_parameter_union geometry_parameters; // relevant parameters for this shape
union geometry_parameter_union (*copy_geometry_parameters)(union geometry_parameter_union*);

struct focus_data_array_struct focus_data_array; // Focusing specified by user is element 0 and used for isotropic processes, rotated versions are added by master
struct pointer_to_1d_int_list focus_array_indices; // Add 1D integer array with indecies for correct focus_data for each process


// intersect_function takes position/velocity of ray and parameters, returns time list
int (*intersect_function)(double*, double*, double*, double*, int*, int*, double*, double*, struct geometry_struct*);
//                        t_array, nx array, ny array, nz array, surface_index array, n arary, r ,v

// within_function that checks if the ray origin is within this volume
int (*within_function)(Coords,struct geometry_struct*);
//                     r,      parameters

// mcdisplay function, draws the geometry
//void (*mcdisplay_function)(struct lines_to_draw*,int,struct Volume_struct**,int);
void (*mcdisplay_function)(struct lines_to_draw*,int,struct geometry_struct**,int);
//                                lines          index             Geometries   N

void (*initialize_from_main_function)(struct geometry_struct*);

struct pointer_to_1d_coords_list (*shell_points)(struct geometry_struct*, int maximum_number_of_points);

// List of other volumes to be check when ray starts within this volume.
struct pointer_to_1d_int_list intersect_check_list;
// List of other volumes the ray may enter, if the ray intersects the volume itself.
struct pointer_to_1d_int_list destinations_list;
// The destinations list stored as a logic list which makes some tasks quicker. OBSOLETE
//struct pointer_to_1d_int_list destinations_logic_list;
// Reduced list of other volumes the ray may enter, if the ray intersects the volume itself.
struct pointer_to_1d_int_list reduced_destinations_list;
// List of other volumes that are within this volume
struct pointer_to_1d_int_list children;
// List of other volumes that are within this volume, but does not have any parents that are children of this volume
struct pointer_to_1d_int_list direct_children;
// List of next possible volumes (only used in tagging)
struct pointer_to_1d_int_list next_volume_list;
// List of volumes masked by this volume (usually empty)
struct pointer_to_1d_int_list mask_list;
// List of volumes masking this volume
struct pointer_to_1d_int_list masked_by_list;
// List of masks masking this volume (global mask indices)
struct pointer_to_1d_int_list masked_by_mask_index_list;
// Additional intersect lists dependent on mask status
//struct indexed_mask_lists_struct mask_intersect_lists;
// Simpler way of storing the mask_intersect_lists
struct pointer_to_1d_int_list mask_intersect_list;

// Surfaces
// Could make structure for this and support functions?
int number_of_faces;
struct surface_stack_struct **surface_stack_for_each_face;
struct surface_stack_struct *internal_cut_surface_stack;
};

struct physics_struct
{
char name[256]; // User defined material name
int interact_control;
int is_vacuum;
int any_process_needs_cross_section_focus;
double my_a;
int number_of_processes;
// pointer to array of pointers to physics_sub structures that each describe a scattering process
struct scattering_process_struct *p_scattering_array;

// refraction related
int has_refraction_info;
double refraction_scattering_length_density; // [AA^-2]
double refraction_Qc;
};

union data_transfer_union{
    // List of pointers to storage structs for all supported physical processes
    struct Incoherent_physics_storage_struct  *pointer_to_a_Incoherent_physics_storage_struct;
    struct Powder_physics_storage_struct *pointer_to_a_Powder_physics_storage_struct;
    struct Single_crystal_physics_storage_struct *pointer_to_a_Single_crystal_physics_storage_struct;
    struct AF_HB_1D_physics_storage_struct *pointer_to_a_AF_HB_1D_physics_storage_struct;
    struct IncoherentPhonon_physics_storage_struct *pointer_to_a_IncoherentPhonon_physics_storage_struct;
    struct PhononSimpleNumeric_physics_storage_struct *pointer_to_a_PhononSimpleNumeric_storage_struct;
    struct PhononSimple_physics_storage_struct *pointer_to_a_PhononSimple_storage_struct;
    struct MagnonSimple_physics_storage_struct *pointer_to_a_MagnonSimple_storage_struct;
    struct Sans_spheres_physics_storage_struct *pointer_to_a_Sans_spheres_physics_storage_struct;
    struct Texture_physics_storage_struct *pointer_to_a_Texture_physics_storage_struct;
    struct NCrystal_physics_storage_struct *pointer_to_a_NCrystal_physics_storage_struct;
    struct Non_physics_storage_struct *pointer_to_a_Non_physics_storage_struct;
    struct Template_physics_storage_struct *pointer_to_a_Template_physics_storage_struct;
    // possible to add as many structs as wanted, without increasing memory footprint.
};



struct scattering_process_struct
{
char name[256];                // User defined process name
enum process eProcess;         // enum value corresponding to this process GPU
double process_p_interact;     // double between 0 and 1 that describes the fraction of events forced to undergo this process. -1 for disable
int non_isotropic_rot_index;   // -1 if process is isotrpic, otherwise is the index of the process rotation matrix in the volume
int needs_cross_section_focus; // 1 if physics_my needs to call focus functions, otherwise -1
Rotation rotation_matrix;      // rotation matrix of process, reported by component in local frame, transformed and moved to volume struct in main

union data_transfer_union data_transfer; // The way to reach the storage space allocated for this process (see examples in process.comp files)

// probability_for_scattering_functions calculates this probability given k_i and parameters
int (*probability_for_scattering_function)(double*,double*,union data_transfer_union,struct focus_data_struct*, _class_particle *_particle);
//                                         prop,   k_i,   ,parameters               , focus data / function

// A scattering_function takes k_i and parameters, returns k_f
int (*scattering_function)(double*,double*,double*,union data_transfer_union,struct focus_data_struct*, _class_particle *_particle);
//                         k_f,    k_i,    weight, parameters               , focus data / function
};

//Utility function for initialising a scattering_process_struct with default
//values:
void scattering_process_struct_init( struct scattering_process_struct * sps )
{
  memset(sps,0,sizeof(struct scattering_process_struct));//catch all
  sps->name[0] = '\0';
  sps->probability_for_scattering_function = NULL;
  sps->scattering_function = NULL;
  sps->non_isotropic_rot_index = -1;
  sps->needs_cross_section_focus = -1;
}

union surface_data_transfer_union 
{
	struct Mirror_surface_storage_struct *pointer_to_a_Mirror_surface_storage_struct;
	struct Template_surface_storage_struct *pointer_to_a_Template_surface_storage_struct;	
};

struct surface_process_struct
{
char name[256];
enum surface eSurface;
union surface_data_transfer_union data_transfer;
};

struct surface_stack_struct
{
int number_of_surfaces;
struct surface_process_struct **p_surface_array;
};

struct Volume_struct
{
char name[256]; // User defined volume name
struct geometry_struct geometry;        // Geometry properties (including intersect functions, generated lists)
struct physics_struct *p_physics;       // Physical properties (list of scattering processes, absorption)
struct loggers_struct loggers;          // Loggers assosiated with this volume
struct abs_loggers_struct abs_loggers;  // Loggers assosiated with this volume
};

// example of calling a scattering process
// volume_pointer_list[3]->physics.scattering_process[5].probability_for_scattering_function(input,volume_pointer_list[3]->physics.scattering_process[5])

struct starting_lists_struct
{
struct pointer_to_1d_int_list allowed_starting_volume_logic_list;
struct pointer_to_1d_int_list reduced_start_list;
struct pointer_to_1d_int_list start_logic_list;
struct pointer_to_1d_int_list starting_destinations_list;
};

struct global_positions_to_transform_list_struct
{
int num_elements;
Coords **positions;
};

struct global_rotations_to_transform_list_struct
{
int num_elements;
Rotation **rotations;
};

struct global_surface_element_struct
{
char name[256]; // Name of the process
int component_index;
struct surface_process_struct *p_surface_process;
};

struct pointer_to_global_surface_list 
{
int num_elements;
struct global_surface_element_struct *elements;
};

struct global_process_element_struct
{
char name[256]; // Name of the process
int component_index;
struct scattering_process_struct *p_scattering_process;
};

struct pointer_to_global_process_list {
int num_elements;
struct global_process_element_struct *elements;
};

struct global_material_element_struct
{
char name[128];
int component_index;
struct physics_struct *physics;
};

struct pointer_to_global_material_list {
int num_elements;
struct global_material_element_struct *elements;
};

struct global_geometry_element_struct
{
char name[128];
int component_index;
int activation_counter;
int stored_copies;
int active;
struct Volume_struct *Volume;
};

struct pointer_to_global_geometry_list {
int num_elements;
struct global_geometry_element_struct *elements;
};

struct global_logger_element_struct {
char name[128];
int component_index;
struct logger_struct *logger;
};

struct pointer_to_global_logger_list {
int num_elements;
struct global_logger_element_struct *elements;
};

struct global_abs_logger_element_struct {
char name[128];
int component_index;
struct abs_logger_struct *abs_logger;
};

struct pointer_to_global_abs_logger_list {
int num_elements;
struct global_abs_logger_element_struct *elements;
};

struct global_tagging_conditional_element_struct {
struct conditional_list_struct conditional_list;
int extend_index;
char name[1024];
int use_status;
};

struct global_tagging_conditional_list_struct {
int num_elements;
int current_index;
struct global_tagging_conditional_element_struct *elements;
};


struct global_master_element_struct {
char name[128];
int component_index;
int stored_number_of_scattering_events; // TEST
struct conditional_list_struct *tagging_conditional_list_pointer;
};

struct pointer_to_global_master_list {
int num_elements;
struct global_master_element_struct *elements;
};


void geometry_struct_init(struct geometry_struct *geometry){
  memset(geometry, 0, sizeof(struct geometry_struct));
  geometry->skip_hierarchy_optimization = 0;
}
// -------------    Physics functions   ---------------------------------------------------------

//#include "Test_physics.c"
//#include "Incoherent_test.c"

// -------------    General functions   ---------------------------------------------------------
double distance_between(Coords position1,Coords position2) {
    return sqrt((position1.x-position2.x)*(position1.x-position2.x) +
                (position1.y-position2.y)*(position1.y-position2.y) +
                (position1.z-position2.z)*(position1.z-position2.z));
};



double length_of_3vector(double *r) {
        return sqrt(r[0]*r[0]+r[1]*r[1]+r[2]*r[2]);
    };

double length_of_position_vector(Coords point) {
        return sqrt(point.x*point.x+point.y*point.y+point.z*point.z);
    };

Coords make_position(double *r) {
    Coords temp;
    
    temp.x = r[0];temp.y = r[1];temp.z = r[2];
    return temp;
};

Coords coords_scalar_mult(Coords input,double scalar) {
    return coords_set(scalar*input.x,scalar*input.y,scalar*input.z);
};

double union_coords_dot(Coords vector1,Coords vector2) {
    return vector1.x*vector2.x + vector1.y*vector2.y + vector1.z*vector2.z;
}


int sum_int_list(struct pointer_to_1d_int_list list) {
    int iterate,sum = 0;
    for (iterate = 0;iterate < list.num_elements;iterate++) sum += list.elements[iterate];
    return sum;
    };
    
int on_int_list(struct pointer_to_1d_int_list list,int target) {
    int iterate,output=0;
    for (iterate = 0; iterate<list.num_elements ;iterate++) {
        if (list.elements[iterate] == target) output = 1;
    }
    return output;
    };

int find_on_int_list(struct pointer_to_1d_int_list list,int target) {
    int iterate;
    for (iterate = 0;iterate < list.num_elements;iterate++) {
        if (list.elements[iterate] == target) return iterate;
    }
    return -1;
    };
    
void on_both_int_lists(struct pointer_to_1d_int_list *list1, struct pointer_to_1d_int_list *list2, struct pointer_to_1d_int_list *common) {
    // Assume common.elements is allocated to a number of int's large enough to hold the resulting list
    int iterate,used_elements=0;
    for (iterate=0;iterate<list1->num_elements;iterate++) {
        if (on_int_list(*list2,list1->elements[iterate])) common->elements[used_elements++] = list1->elements[iterate];
    }
    common->num_elements = used_elements;
    };

void remove_element_in_list_by_index(struct pointer_to_1d_int_list *list,int index) {
    if (index >= list->num_elements) {
      printf("ERROR(remove_element_in_list_by_index): trying to remove an index that wasn't allocated to begin with");
      exit(EXIT_FAILURE);
    }
    else {
        int iterate;
        int *temp;
        for (iterate = index;iterate < list->num_elements -1;iterate++) {
            list->elements[iterate] = list->elements[iterate+1];
        }
        list->num_elements--;
        //if (list->num_elements==0) printf("Making empty list!\n");
        temp = malloc(list->num_elements * sizeof(int));
	if (!temp) {
	  fprintf(stderr,"Failure allocating list in Union function remove_element_in_list_by_index 1/2 - Exit!\n");
	  exit(EXIT_FAILURE);
	}
        for (iterate = 0;iterate < list->num_elements;iterate++) temp[iterate] = list->elements[iterate];
        free(list->elements);
        list->elements = malloc(list->num_elements * sizeof(int));
	if (!list->elements) {
	  fprintf(stderr,"Failure allocating list in Union function remove_element_in_list_by_index 2/2 - Exit!\n");
	  exit(EXIT_FAILURE);
	}
        for (iterate = 0;iterate < list->num_elements;iterate++) list->elements[iterate] = temp[iterate];
        free(temp);

    }
    };
    
void remove_element_in_list_by_value(struct pointer_to_1d_int_list *list,int value) {
    int iterate;
    for (iterate = 0;iterate < list->num_elements;iterate++) {
        if (list->elements[iterate] == value) remove_element_in_list_by_index(list,iterate);
    }
    };
    
void merge_lists(struct pointer_to_1d_int_list *result,struct pointer_to_1d_int_list *list1,struct pointer_to_1d_int_list *list2) {
    if (result->num_elements > 0) free(result->elements);
    result->num_elements = list1->num_elements + list2->num_elements;
    if (result->num_elements != 0) {
      result->elements = malloc(result->num_elements*sizeof(int));
      if (!result->elements) {
	fprintf(stderr,"Failure allocating list in Union function merge_lists- Exit!\n");
	  exit(EXIT_FAILURE);
      }
      int iterate;
      for (iterate = 0;iterate < list1->num_elements;iterate++)
          result->elements[iterate] = list1->elements[iterate];
      for (iterate = 0;iterate < list2->num_elements;iterate++)
          result->elements[list1->num_elements+iterate] = list2->elements[iterate];
    }
    };

void add_element_to_double_list(struct pointer_to_1d_double_list *list,double value) {
    if (list->num_elements == 0) {
      list->num_elements++;
      list-> elements = malloc(list->num_elements*sizeof(double));
      if (!list->elements) {
	fprintf(stderr,"Failure allocating list in Union function add_element_to_double_list 1/3 - Exit!\n");
	  exit(EXIT_FAILURE);
      }
      list-> elements[0] = value;
    } else {
      double *temp=malloc(list->num_elements*sizeof(double));
      if (!temp) {
	fprintf(stderr,"Failure allocating list in Union function add_element_to_double_list 2/3 - Exit!\n");
	  exit(EXIT_FAILURE);
      }
      int iterate;
      for (iterate=0;iterate<list->num_elements;iterate++) temp[iterate] = list->elements[iterate];
      free(list->elements);
      list->num_elements++;
      list-> elements = malloc(list->num_elements*sizeof(double));
      if (!list->elements) {
	fprintf(stderr,"Failure allocating list in Union function add_element_to_double_list 3/3 - Exit!\n");
	exit(EXIT_FAILURE);
      }
      for (iterate=0;iterate<list->num_elements-1;iterate++) list->elements[iterate] = temp[iterate];
      free(temp);
      list->elements[list->num_elements-1] = value;
    }
    };

void add_element_to_int_list(struct pointer_to_1d_int_list *list,int value) {
    if (list->num_elements == 0) {
      list->num_elements++;
      list-> elements = malloc(list->num_elements*sizeof(int));
      if (!list->elements) {
	fprintf(stderr,"Failure allocating list in Union function add_element_to_int_list 1/3 - Exit!\n");
	exit(EXIT_FAILURE);
      }
      list-> elements[0] = value;
    } else {
      double *temp=malloc(list->num_elements*sizeof(double));
      if (!temp) {
	fprintf(stderr,"Failure allocating list in Union function add_element_to_int_list 2/3 - Exit!\n");
	exit(EXIT_FAILURE);
      }
      int iterate;
      for (iterate=0;iterate<list->num_elements;iterate++) temp[iterate] = list->elements[iterate];
      free(list->elements);
      list->num_elements++;
      list-> elements = malloc(list->num_elements*sizeof(int));
      if (!list->elements) {
	fprintf(stderr,"Failure allocating list in Union function add_element_to_int_list 3/3 - Exit!\n");
	exit(EXIT_FAILURE);
      }
      for (iterate=0;iterate<list->num_elements-1;iterate++) list->elements[iterate] = temp[iterate];
      free(temp);
      list->elements[list->num_elements-1] = value;
    }
    };

// Need to check if absolute_rotation is preserved correctly.
void add_element_to_focus_data_array(struct focus_data_array_struct *focus_data_array,struct focus_data_struct focus_data) {
    if (focus_data_array->num_elements == 0) {
      focus_data_array->num_elements++;
      focus_data_array->elements = malloc(focus_data_array->num_elements*sizeof(struct focus_data_struct));
      if (!focus_data_array->elements) {
	fprintf(stderr,"Failure allocating list in Union function add_element_to_focus_data_array 1/3- Exit!\n");
	exit(EXIT_FAILURE);
      }
      focus_data_array->elements[0] = focus_data;
    } else {
      struct focus_data_struct *temp=malloc(focus_data_array->num_elements*sizeof(struct focus_data_struct));
      if (!temp) {
	fprintf(stderr,"Failure allocating list in Union function add_element_to_focus_data_array 2/3- Exit!\n");
	exit(EXIT_FAILURE);
      }
      int iterate;
      for (iterate=0;iterate<focus_data_array->num_elements;iterate++) temp[iterate] = focus_data_array->elements[iterate];
      free(focus_data_array->elements);
      focus_data_array->num_elements++;
      focus_data_array-> elements = malloc(focus_data_array->num_elements*sizeof(struct focus_data_struct));
      if (!focus_data_array->elements) {
	fprintf(stderr,"Failure allocating list in Union function add_element_to_focus_data_array 3/3- Exit!\n");
	exit(EXIT_FAILURE);
      }
      for (iterate=0;iterate<focus_data_array->num_elements-1;iterate++) focus_data_array->elements[iterate] = temp[iterate];
      free(temp);
      focus_data_array->elements[focus_data_array->num_elements-1] = focus_data;
    }
    };
	
void copy_focus_data_array(struct focus_data_array_struct *original_array, struct focus_data_array_struct *new_array) {
	
	new_array->num_elements = original_array->num_elements;
	new_array->elements = malloc(new_array->num_elements*sizeof(struct focus_data_struct));

	if (new_array->elements == NULL) {
		fprintf(stderr, "Memory allocation failed in copy_focus_data_struct \n");
		exit(EXIT_FAILURE);
	}
		
	int iterate;
	for (iterate=0;iterate<original_array->num_elements;iterate++) {
		new_array->elements[iterate] = original_array->elements[iterate];
	}

	};



void add_to_logger_with_data(struct logger_with_data_struct *logger_with_data, struct logger_struct *logger) {
    // May reorder the order of the if conditions to avoid checking the == 0 for every single ray
    if (logger_with_data->allocated_elements == 0) {
        logger_with_data->allocated_elements = 5;
        logger_with_data->logger_pointers = malloc(logger_with_data->allocated_elements*sizeof(struct logger_struct*));
	if (!logger_with_data->logger_pointers) {
	  fprintf(stderr,"Failure allocating list in Union function add_to_logger_with_data 1/3- Exit!\n");
	  exit(EXIT_FAILURE);
	}
        logger_with_data->used_elements = 1;
        logger_with_data->logger_pointers[0] = logger;
    } else if (logger_with_data->used_elements > logger_with_data->allocated_elements-1) {
        struct logger_with_data_struct temp_logger_with_data;
        temp_logger_with_data.logger_pointers = malloc((logger_with_data->used_elements)*sizeof(struct logger_struct*));
	if (!temp_logger_with_data.logger_pointers) {
	  fprintf(stderr,"Failure allocating list in Union function add_to_logger_with_data 2/3- Exit!\n");
	  exit(EXIT_FAILURE);
	}
        int iterate;
        for (iterate=0;iterate<logger_with_data->used_elements;iterate++) {
            temp_logger_with_data.logger_pointers[iterate]  = logger_with_data->logger_pointers[iterate];
        }
        free(logger_with_data->logger_pointers);
        logger_with_data->allocated_elements = logger_with_data->allocated_elements+5;
        logger_with_data->logger_pointers = malloc(logger_with_data->allocated_elements*sizeof(struct logger_struct*));
	if (!logger_with_data->logger_pointers) {
	  fprintf(stderr,"Failure allocating list in Union function add_to_logger_with_data 3/3- Exit!\n");
	  exit(EXIT_FAILURE);
	}
        for (iterate=0;iterate<logger_with_data->used_elements;iterate++) {
            logger_with_data->logger_pointers[iterate]  = temp_logger_with_data.logger_pointers[iterate];
        }

        logger_with_data->logger_pointers[logger_with_data->used_elements++] = logger;

	// Clear up temporary memory
	free(temp_logger_with_data.logger_pointers);

    } else {
        logger_with_data->logger_pointers[logger_with_data->used_elements++] = logger;
    }
    
};

void add_to_abs_logger_with_data(struct abs_logger_with_data_struct *abs_logger_with_data, struct abs_logger_struct *abs_logger) {
    // May reorder the order of the if conditions to avoid checking the == 0 for every single ray
    if (abs_logger_with_data->allocated_elements == 0) {
        abs_logger_with_data->allocated_elements = 5;
        abs_logger_with_data->abs_logger_pointers = malloc(abs_logger_with_data->allocated_elements*sizeof(struct abs_logger_struct*));
	if (!abs_logger_with_data->abs_logger_pointers) {
	  fprintf(stderr,"Failure allocating list in Union function add_to_abs_logger_with_data 1/3- Exit!\n");
	  exit(EXIT_FAILURE);
	}
        abs_logger_with_data->used_elements = 1;
        abs_logger_with_data->abs_logger_pointers[0] = abs_logger;
    } else if (abs_logger_with_data->used_elements > abs_logger_with_data->allocated_elements-1) {
        struct abs_logger_with_data_struct temp_abs_logger_with_data;
        temp_abs_logger_with_data.abs_logger_pointers = malloc((abs_logger_with_data->used_elements)*sizeof(struct abs_logger_struct*));
	if (!temp_abs_logger_with_data.abs_logger_pointers) {
	  fprintf(stderr,"Failure allocating list in Union function add_to_abs_logger_with_data 2/3- Exit!\n");
	  exit(EXIT_FAILURE);
	}
        int iterate;
        for (iterate=0;iterate<abs_logger_with_data->used_elements;iterate++) {
            temp_abs_logger_with_data.abs_logger_pointers[iterate]  = abs_logger_with_data->abs_logger_pointers[iterate];
        }
        free(abs_logger_with_data->abs_logger_pointers);
        abs_logger_with_data->allocated_elements = abs_logger_with_data->allocated_elements+5;
        abs_logger_with_data->abs_logger_pointers = malloc(abs_logger_with_data->allocated_elements*sizeof(struct abs_logger_struct*));
	if (!abs_logger_with_data->abs_logger_pointers) {
	  fprintf(stderr,"Failure allocating list in Union function add_to_abs_logger_with_data 3/3- Exit!\n");
	  exit(EXIT_FAILURE);
	}
        for (iterate=0;iterate<abs_logger_with_data->used_elements;iterate++) {
            abs_logger_with_data->abs_logger_pointers[iterate] = temp_abs_logger_with_data.abs_logger_pointers[iterate];
        }

        abs_logger_with_data->abs_logger_pointers[abs_logger_with_data->used_elements++] = abs_logger;

	// Clear up temporary memory
	free(temp_abs_logger_with_data.abs_logger_pointers);

    } else {
        abs_logger_with_data->abs_logger_pointers[abs_logger_with_data->used_elements++] = abs_logger;
    }
    
};


// Used typedef to avoid having to change this function later. May update others to use same phillosphy.
void add_function_to_conditional_list(struct conditional_list_struct *list,conditional_function_pointer new, union conditional_data_union *data_union) {
    if (list->num_elements == 0) {
      list->num_elements++;
      list->conditional_functions = malloc(list->num_elements*sizeof(conditional_function_pointer));
      if (!list->conditional_functions) {
	fprintf(stderr,"Failure allocating list in Union function add_function_to_conditional_list 1/6 - Exit!\n");
	exit(EXIT_FAILURE);
      }
      list->p_data_unions = malloc(list->num_elements*sizeof(union conditional_data_union*));
      if (!list->p_data_unions) {
	fprintf(stderr,"Failure allocating list in Union function add_function_to_conditional_list 2/6 - Exit!\n");
	exit(EXIT_FAILURE);
      }
      list->conditional_functions[0] = new;
      list->p_data_unions[0] = data_union;
    }
    else {
    conditional_function_pointer *temp_fp=malloc(list->num_elements*sizeof(conditional_function_pointer));
    if (!temp_fp) {
      fprintf(stderr,"Failure allocating list in Union function add_function_to_conditional_list 3/6 - Exit!\n");
      exit(EXIT_FAILURE);
    }
    union conditional_data_union **temp_du=malloc(list->num_elements*sizeof(union conditional_data_union));
    if (!temp_du) {
      fprintf(stderr,"Failure allocating list in Union function add_function_to_conditional_list 4/6 - Exit!\n");
      exit(EXIT_FAILURE);
    }
    int iterate;
    // Could even get away with a shallow copy here instead of the loop, but it is not relevant for performance.
    for (iterate=0;iterate<list->num_elements;iterate++) {
      temp_fp[iterate] = list->conditional_functions[iterate];
      temp_du[iterate] = list->p_data_unions[iterate];
    }
    free(list->conditional_functions);
    free(list->p_data_unions);
    list->num_elements++;
    list->conditional_functions = malloc(list->num_elements*sizeof(conditional_function_pointer));
    if (!list->conditional_functions) {
      fprintf(stderr,"Failure allocating list in Union function add_function_to_conditional_list 5/6 - Exit!\n");
      exit(EXIT_FAILURE);
    }
    list->p_data_unions = malloc(list->num_elements*sizeof(union conditional_data_union*));
    if (!list->p_data_unions) {
      fprintf(stderr,"Failure allocating list in Union function add_function_to_conditional_list 6/6 - Exit!\n");
      exit(EXIT_FAILURE);
    }
    
    for (iterate=0;iterate<list->num_elements-1;iterate++) {
      list->conditional_functions[iterate] = temp_fp[iterate];
      list->p_data_unions[iterate] = temp_du[iterate];
    }
    list->conditional_functions[list->num_elements-1] = new;
    list->p_data_unions[list->num_elements-1] = data_union;
    }
};


// could make function that removes a element from a 1d_int_list, and have each list generation as a function that takes a copy of an overlap list

void print_1d_int_list(struct pointer_to_1d_int_list list,char *name) {
        int iterate;
        printf("LIST: ");printf("%s",name);printf(" = [");
        for (iterate = 0; iterate < list.num_elements; iterate++) {
            printf("%d",list.elements[iterate]);
            if (iterate < list.num_elements - 1) printf(",");
        }
        printf("]\n");
    };

void print_1d_double_list(struct pointer_to_1d_double_list list,char *name) {
        int iterate;
        printf("LIST: ");printf("%s",name);printf(" = [");
        for (iterate = 0; iterate < list.num_elements; iterate++) {
            printf("%f",list.elements[iterate]);
            if (iterate < list.num_elements - 1) printf(",");
        }
        printf("]\n");
    };

void print_position(Coords pos,char *name) {
    printf("POSITION: ");printf("%s",name);printf(" = (%f,%f,%f)\n",pos.x,pos.y,pos.z);
    };

void print_rotation(Rotation rot, char *name) {
    printf("ROT MATRIX: %s \n",name);
    printf("[%f %f %f]\n",rot[0][0],rot[0][1],rot[0][2]);
    printf("[%f %f %f]\n",rot[1][0],rot[1][1],rot[1][2]);
    printf("[%f %f %f]\n\n",rot[2][0],rot[2][1],rot[2][2]);
};
    
void allocate_list_from_temp(int num_elements,struct pointer_to_1d_int_list original,struct pointer_to_1d_int_list *new) {
        int iterate;
    
        new->num_elements = num_elements;
        if (num_elements > 0) {
            new->elements = malloc(num_elements*sizeof(int));
	    if (!new->elements) {
	      fprintf(stderr,"Failure allocating list in Union function allocate_list_from_temp - Exit!\n");
	      exit(EXIT_FAILURE);
	    }
            for (iterate = 0;iterate < num_elements; iterate++) new->elements[iterate] = original.elements[iterate];
        } else new->elements = NULL;
    
    };

void allocate_logic_list_from_temp(int num_elements,struct pointer_to_1d_int_list original, struct pointer_to_1d_int_list *new) {
        // A logic list shares the same structure of a normal list, but instead of listing numbers, it is a list of yes / no (1/0)
        // Giving this function a list of [1 3 5] (and num_elements = 9) would return [0 1 0 1 0 1 0 0 0];
        int iterate;
    
        new->num_elements = num_elements;
        if (num_elements > 0) {
            new->elements = malloc(num_elements*sizeof(int));
	    if (!new->elements) {
	      fprintf(stderr,"Failure allocating list in Union function allocate_logic_list_from_temp - Exit!\n");
	      exit(EXIT_FAILURE);
	    }
            for (iterate = 0;iterate < num_elements;iterate++) new->elements[iterate] = 0;
            for (iterate = 0;iterate < original.num_elements;iterate++) {
                if (original.elements[iterate] < num_elements)
                    new->elements[original.elements[iterate]] = 1;
                else printf("Trying to allocate logical list without enough memory\n");
            }
        } else new->elements = NULL;
    };

/*
struct global_positions_to_transform_list_struct {
int num_elements;
Coords **positions;
}

struct global_rotations_to_transform_list_struct {
int num_elements;
Rotation **rotations;
}
*/

void add_position_pointer_to_list(struct global_positions_to_transform_list_struct *list, Coords *new_position_pointer) {
    if (list->num_elements == 0) {
      list->num_elements++;
      list->positions = malloc(list->num_elements*sizeof(Coords*));
      if (!list->positions) {
	fprintf(stderr,"Failure allocating list in Union function add_position_pointer_to_list - Exit!\n");
	exit(EXIT_FAILURE);
      }
      list->positions[0] = new_position_pointer;
    } else {
      Coords **temp;
      temp = malloc(list->num_elements*sizeof(Coords*));
      if (temp == NULL) {
	fprintf(stderr,"malloc failed in add_position_pointer_to_list for temp\n");
	exit(EXIT_FAILURE);
      }
      int iterate;
      for (iterate=0;iterate<list->num_elements;iterate++)
        temp[iterate] = list->positions[iterate];
      free(list->positions);
      list->num_elements++;
      list->positions = malloc(list->num_elements*sizeof(Coords*));
      if (list->positions == NULL) {
	fprintf(stderr,"malloc failed in add_position_pointer_to_list for list->positions\n");
	exit(EXIT_FAILURE);
      }
      
      for (iterate=0;iterate<list->num_elements-1;iterate++)
        list->positions[iterate] = temp[iterate];
      free(temp);
      list->positions[list->num_elements-1] = new_position_pointer;
    }
};

void add_rotation_pointer_to_list(struct global_rotations_to_transform_list_struct *list, Rotation *new_rotation_pointer) {
    if (list->num_elements == 0) {
      list->num_elements++;
      list->rotations = malloc(list->num_elements*sizeof(Rotation*));
      if (!list->rotations) {
	fprintf(stderr,"Failure allocating list in Union function add_rotation_pointer_to_list 1 - Exit!\n");
	exit(EXIT_FAILURE);
      }
      list->rotations[0] = new_rotation_pointer;
    } else {
      Rotation **temp;
      temp = malloc(list->num_elements*sizeof(Rotation*));
      if (!temp) {
	fprintf(stderr,"Failure allocating list in Union function add_rotation_pointer_to_list 2 - Exit!\n");
	exit(EXIT_FAILURE);
      }
      int iterate;
      for (iterate=0;iterate<list->num_elements;iterate++)
        temp[iterate] = list->rotations[iterate];
      free(list->rotations);
      list->num_elements++;
      list->rotations = malloc(list->num_elements*sizeof(Rotation*));
      if (!list->rotations) {
	fprintf(stderr,"Failure allocating list in Union function add_rotation_pointer_to_list 3 - Exit!\n");
	exit(EXIT_FAILURE);
      }
      for (iterate=0;iterate<list->num_elements-1;iterate++)
        list->rotations[iterate] = temp[iterate];
      free(temp);
      list->rotations[list->num_elements-1] = new_rotation_pointer;
    }
};

void add_element_to_process_list(struct pointer_to_global_process_list *list,struct global_process_element_struct new_element) {
    if (list->num_elements == 0) {
    list->num_elements++;
    list-> elements = malloc(list->num_elements*sizeof(struct global_process_element_struct));
    if (!list->elements) {
      fprintf(stderr,"Failure allocating list in Union function add_element_to_process_list 1 - Exit!\n");
      exit(EXIT_FAILURE);
    }
    list-> elements[0] = new_element;
    }
    else {
      struct global_process_element_struct *temp=malloc(list->num_elements*sizeof(struct global_process_element_struct));
      if (!temp) {
	fprintf(stderr,"Failure allocating list in Union function add_element_to_process_list 1 - Exit!\n");
	exit(EXIT_FAILURE);
      }
      int iterate;
      for (iterate=0;iterate<list->num_elements;iterate++) temp[iterate] = list->elements[iterate];
      free(list->elements);
      list->num_elements++;
      list-> elements = malloc(list->num_elements*sizeof(struct global_process_element_struct));
      if (!list->elements) {
	fprintf(stderr,"Failure allocating list in Union function add_element_to_process_list 3 - Exit!\n");
	exit(EXIT_FAILURE);
      }
      for (iterate=0;iterate<list->num_elements-1;iterate++) list->elements[iterate] = temp[iterate];
      free(temp);
      list->elements[list->num_elements-1] = new_element;
    }
};

void add_element_to_material_list(struct pointer_to_global_material_list *list,struct global_material_element_struct new_element) {
    if (list->num_elements == 0) {
      list->num_elements++;
      list->elements = malloc(list->num_elements*sizeof(struct global_material_element_struct));
      if (!list->elements) {
	fprintf(stderr,"Failure allocating list in Union function add_element_to_material_list 1 - Exit!\n");
	exit(EXIT_FAILURE);
      }
      list->elements[0] = new_element;
    }
    else {
      struct global_material_element_struct *temp=malloc(list->num_elements*sizeof(struct global_material_element_struct));
      if (!temp) {
	fprintf(stderr,"Failure allocating list in Union function add_element_to_material_list 2 - Exit!\n");
	exit(EXIT_FAILURE);
      }
      int iterate;
      for (iterate=0;iterate<list->num_elements;iterate++) temp[iterate] = list->elements[iterate];
      free(list->elements);
      list->num_elements++;
      list-> elements = malloc(list->num_elements*sizeof(struct global_material_element_struct));
      if (!list->elements) {
	fprintf(stderr,"Failure allocating list in Union function add_element_to_material_list 3 - Exit!\n");
	exit(EXIT_FAILURE);
      }
      for (iterate=0;iterate<list->num_elements-1;iterate++) list->elements[iterate] = temp[iterate];
      free(temp);
      list->elements[list->num_elements-1] = new_element;
    }
};

void add_element_to_surface_list(struct pointer_to_global_surface_list *list, struct global_surface_element_struct new_element) {
    if (list->num_elements == 0) {
      list->num_elements++;
      list->elements = malloc(list->num_elements*sizeof(struct global_surface_element_struct));
      if (!list->elements) {
	fprintf(stderr,"Failure allocating list in Union function add_element_to_surface_list 1 - Exit!\n");
	exit(EXIT_FAILURE);
      }
      list->elements[0] = new_element;
    }
    else {
      struct global_surface_element_struct *temp=malloc(list->num_elements*sizeof(struct global_surface_element_struct));
      if (!temp) {
	fprintf(stderr,"Failure allocating list in Union function add_element_to_surface_list 2 - Exit!\n");
	exit(EXIT_FAILURE);
      }
      int iterate;
      for (iterate=0;iterate<list->num_elements;iterate++) temp[iterate] = list->elements[iterate];
      free(list->elements);
      list->num_elements++;
      list-> elements = malloc(list->num_elements*sizeof(struct global_surface_element_struct));
      if (!list->elements) {
	fprintf(stderr,"Failure allocating list in Union function add_element_to_surface_list 3 - Exit!\n");
	exit(EXIT_FAILURE);
      }
      for (iterate=0;iterate<list->num_elements-1;iterate++) list->elements[iterate] = temp[iterate];
      free(temp);
      list->elements[list->num_elements-1] = new_element;
    }
};

void add_element_to_surface_stack(struct surface_stack_struct *list, struct surface_process_struct *new_element) {
    if (list->number_of_surfaces == 0) {
        if (!list->p_surface_array) {
            fprintf(stderr, "Memory allocation failed\n");
            exit(EXIT_FAILURE);
        }
        list->p_surface_array[0] = new_element;
        list->number_of_surfaces = 1;
    } else {
        // Reallocate with space for one more element
        struct surface_process_struct **temp = realloc(list->p_surface_array, 
            (list->number_of_surfaces + 1) * sizeof(struct surface_process_struct*));
        if (!temp) {
            fprintf(stderr, "Memory reallocation failed\n");
            exit(EXIT_FAILURE);
        }
        list->p_surface_array = temp;
        list->p_surface_array[list->number_of_surfaces] = new_element;
        list->number_of_surfaces++;
    }
};

void add_element_to_geometry_list(struct pointer_to_global_geometry_list *list,struct global_geometry_element_struct new_element) {
    if (list->num_elements == 0) {
      list->num_elements++;
      list->elements = malloc(list->num_elements*sizeof(struct global_geometry_element_struct));
      if (!list->elements) {
	fprintf(stderr,"Failure allocating list in Union function add_element_to_geometry_list 1 - Exit!\n");
	exit(EXIT_FAILURE);
      }
      list->elements[0] = new_element;
    }
    else {
      struct global_geometry_element_struct *temp=malloc(list->num_elements*sizeof(struct global_geometry_element_struct));
      if (!temp) {
	fprintf(stderr,"Failure allocating list in Union function add_element_to_geometry_list 2 - Exit!\n");
	exit(EXIT_FAILURE);
      }
      int iterate;
      for (iterate=0;iterate<list->num_elements;iterate++) temp[iterate] = list->elements[iterate];
      free(list->elements);
      list->num_elements++;
      list-> elements = malloc(list->num_elements*sizeof(struct global_geometry_element_struct));
      if (!list->elements) {
	fprintf(stderr,"Failure allocating list in Union function add_element_to_geometry_list 3 - Exit!\n");
	exit(EXIT_FAILURE);
      }
      for (iterate=0;iterate<list->num_elements-1;iterate++) list->elements[iterate] = temp[iterate];
      free(temp);
      list->elements[list->num_elements-1] = new_element;
    }
};

void add_element_to_logger_list(struct pointer_to_global_logger_list *list,struct global_logger_element_struct new_element) {
    if (list->num_elements == 0) {
    list->num_elements++;
    list->elements = malloc(list->num_elements*sizeof(struct global_logger_element_struct));
    if (!list->elements) {
      fprintf(stderr,"Failure allocating list in Union function add_element_to_logger_list 1 - Exit!\n");
      exit(EXIT_FAILURE);
    }
    list->elements[0] = new_element;
    }
    else {
    struct global_logger_element_struct *temp=malloc(list->num_elements*sizeof(struct global_logger_element_struct));
    if (!temp) {
      fprintf(stderr,"Failure allocating list in Union function add_element_to_logger_list 2 - Exit!\n");
      exit(EXIT_FAILURE);
    }
    int iterate;
    for (iterate=0;iterate<list->num_elements;iterate++) temp[iterate] = list->elements[iterate];
    free(list->elements);
    list->num_elements++;
    list-> elements = malloc(list->num_elements*sizeof(struct global_logger_element_struct));
    if (!list->elements) {
      fprintf(stderr,"Failure allocating list in Union function add_element_to_logger_list 3 - Exit!\n");
      exit(EXIT_FAILURE);
    }
    for (iterate=0;iterate<list->num_elements-1;iterate++) list->elements[iterate] = temp[iterate];
    free(temp);
    list->elements[list->num_elements-1] = new_element;
    }
};

void add_element_to_abs_logger_list(struct pointer_to_global_abs_logger_list *list, struct global_abs_logger_element_struct new_element) {
    if (list->num_elements == 0) {
      list->num_elements++;
      list->elements = malloc(list->num_elements*sizeof(struct global_abs_logger_element_struct));
      if (!list->elements) {
	fprintf(stderr,"Failure allocating list in Union function add_element_to_abs_logger_list 1 - Exit!\n");
	exit(EXIT_FAILURE);
      }
      list->elements[0] = new_element;
    }
    else {
      struct global_abs_logger_element_struct *temp=malloc(list->num_elements*sizeof(struct global_abs_logger_element_struct));
      if (!temp) {
	fprintf(stderr,"Failure allocating list in Union function add_element_to_abs_logger_list 2 - Exit!\n");
	exit(EXIT_FAILURE);
      }
      int iterate;
      for (iterate=0;iterate<list->num_elements;iterate++) temp[iterate] = list->elements[iterate];
      free(list->elements);
      list->num_elements++;
      list-> elements = malloc(list->num_elements*sizeof(struct global_abs_logger_element_struct));
      if (!list->elements) {
	fprintf(stderr,"Failure allocating list in Union function add_element_to_abs_logger_list 3 - Exit!\n");
	exit(EXIT_FAILURE);
      }
      for (iterate=0;iterate<list->num_elements-1;iterate++) list->elements[iterate] = temp[iterate];
      free(temp);
      list->elements[list->num_elements-1] = new_element;
    }
};

void add_element_to_tagging_conditional_list(struct global_tagging_conditional_list_struct *list,struct global_tagging_conditional_element_struct new_element) {
    if (list->num_elements == 0) {
      list->num_elements++;
      list->elements = malloc(list->num_elements*sizeof(struct global_tagging_conditional_element_struct));
      if (!list->elements) {
	fprintf(stderr,"Failure allocating list in Union function add_element_to_tagging_conditional_list 1 - Exit!\n");
	exit(EXIT_FAILURE);
      }
      list->elements[0] = new_element;
    }
    else {
      struct global_tagging_conditional_element_struct *temp=malloc(list->num_elements*sizeof(struct global_tagging_conditional_element_struct));
      if (!temp) {
	fprintf(stderr,"Failure allocating list in Union function add_element_to_tagging_conditional_list 2 - Exit!\n");
	exit(EXIT_FAILURE);
      }
      int iterate;
      for (iterate=0;iterate<list->num_elements;iterate++) temp[iterate] = list->elements[iterate];
      free(list->elements);
      list->num_elements++;
      list->elements = malloc(list->num_elements*sizeof(struct global_tagging_conditional_element_struct));
      if (!list->elements) {
	fprintf(stderr,"Failure allocating list in Union function add_element_to_tagging_conditional_list 3 - Exit!\n");
	exit(EXIT_FAILURE);
      }
      for (iterate=0;iterate<list->num_elements-1;iterate++) list->elements[iterate] = temp[iterate];
      free(temp);
      list->elements[list->num_elements-1] = new_element;
    }
};

void add_element_to_master_list(struct pointer_to_global_master_list *list,struct global_master_element_struct new_element) {
    if (list->num_elements == 0) {
      list->num_elements++;
      list->elements = malloc(list->num_elements*sizeof(struct global_master_element_struct));
      if (!list->elements) {
	fprintf(stderr,"Failure allocating list in Union function add_element_to_master_list 1 - Exit!\n");
	exit(EXIT_FAILURE);
      }
      list->elements[0] = new_element;
    }
    else {
      struct global_master_element_struct *temp=malloc(list->num_elements*sizeof(struct global_master_element_struct));
      if (!temp) {
	fprintf(stderr,"Failure allocating list in Union function add_element_to_master_list 2 - Exit!\n");
	exit(EXIT_FAILURE);
      }
      int iterate;
      for (iterate=0;iterate<list->num_elements;iterate++) temp[iterate] = list->elements[iterate];
      free(list->elements);
      list->num_elements++;
      list-> elements = malloc(list->num_elements*sizeof(struct global_master_element_struct));
      if (!list->elements) {
	fprintf(stderr,"Failure allocating list in Union function add_element_to_master_list 3 - Exit!\n");
	exit(EXIT_FAILURE);
      }
      for (iterate=0;iterate<list->num_elements-1;iterate++) list->elements[iterate] = temp[iterate];
      free(temp);
      list->elements[list->num_elements-1] = new_element;
    }
};

void add_initialized_logger_in_volume(struct loggers_struct *loggers,int number_of_processes) {
  int iterate;
  if (loggers->num_elements == 0) {
    loggers->num_elements++;
    loggers->p_logger_volume = malloc(loggers->num_elements * sizeof(struct logger_for_each_process_list));
    if (!loggers->p_logger_volume) {
      fprintf(stderr,"Failure allocating list in Union function add_initialized_logger_in_volume 1 - Exit!\n");
      exit(EXIT_FAILURE);
    }
    loggers->p_logger_volume[0].num_elements = number_of_processes;
    loggers->p_logger_volume[0].p_logger_process = malloc(number_of_processes * sizeof(struct logger_struct**));
    if (!loggers->p_logger_volume[0].p_logger_process) {
      fprintf(stderr,"Failure allocating list in Union function add_initialized_logger_in_volume 2 - Exit!\n");
      exit(EXIT_FAILURE);
    }
    for (iterate=0;iterate<number_of_processes;iterate++)
      loggers->p_logger_volume[0].p_logger_process[iterate] = NULL;
  } else {
    // Already some elements, store them in temp, free main, transfer back and add newest.
    struct logger_for_each_process_list *temp=malloc(loggers->num_elements*sizeof(struct logger_for_each_process_list));
    if (!temp) {
      fprintf(stderr,"Failure allocating list in Union function add_initialized_logger_in_volume 3 - Exit!\n");
      exit(EXIT_FAILURE);
    }
    for (iterate=0;iterate<loggers->num_elements;iterate++) temp[iterate] = loggers->p_logger_volume[iterate];
    free(loggers->p_logger_volume);
    loggers->num_elements++;
    loggers->p_logger_volume = malloc(loggers->num_elements*sizeof(struct logger_for_each_process_list));
    if (!loggers->p_logger_volume) {
      fprintf(stderr,"Failure allocating list in Union function add_initialized_logger_in_volume 4 - Exit!\n");
      exit(EXIT_FAILURE);
    }
    for (iterate=0;iterate<loggers->num_elements-1;iterate++) loggers->p_logger_volume[iterate] = temp[iterate];
    free(temp);
    loggers->p_logger_volume[loggers->num_elements-1].num_elements = number_of_processes;
    loggers->p_logger_volume[loggers->num_elements-1].p_logger_process = malloc(number_of_processes * sizeof(struct logger_struct**));
    if (!loggers->p_logger_volume[loggers->num_elements-1].p_logger_process) {
      fprintf(stderr,"Failure allocating list in Union function add_initialized_logger_in_volume 5 - Exit!\n");
      exit(EXIT_FAILURE);
    }
    for (iterate=0;iterate<number_of_processes;iterate++) {
      loggers->p_logger_volume[loggers->num_elements-1].p_logger_process[iterate] = NULL;
    }
  }
};


void add_initialized_abs_logger_in_volume(struct abs_loggers_struct *abs_loggers) {
  int iterate;
  if (abs_loggers->num_elements == 0) {
    abs_loggers->num_elements++;
    abs_loggers->p_abs_logger = malloc(abs_loggers->num_elements * sizeof(struct abs_logger_struct*));
    if (!abs_loggers->p_abs_logger) {
      fprintf(stderr,"Failure allocating list in Union function add_initialized_abs_logger_in_volume 1 - Exit!\n");
      exit(EXIT_FAILURE);
    }
  } else {
    // Already some elements, store them in temp, free main, transfer back and add newest.
    struct abs_logger_struct **temp=malloc(abs_loggers->num_elements*sizeof(struct abs_logger_struct *));
    if (!temp) {
      fprintf(stderr,"Failure allocating list in Union function add_initialized_abs_logger_in_volume 2 - Exit!\n");
      exit(EXIT_FAILURE);
    }
    for (iterate=0;iterate<abs_loggers->num_elements;iterate++) temp[iterate] = abs_loggers->p_abs_logger[iterate];
    free(abs_loggers->p_abs_logger);
    
    abs_loggers->num_elements++;
    abs_loggers->p_abs_logger = malloc(abs_loggers->num_elements*sizeof(struct abs_logger_struct*));
    if (!abs_loggers->p_abs_logger) {
      fprintf(stderr,"Failure allocating list in Union function add_initialized_abs_logger_in_volume 3 - Exit!\n");
      exit(EXIT_FAILURE);
    }
    for (iterate=0;iterate<abs_loggers->num_elements-1;iterate++) abs_loggers->p_abs_logger[iterate] = temp[iterate];
    free(temp);
    abs_loggers->p_abs_logger[abs_loggers->num_elements-1] = NULL;
  }
};


// -------------    Functions used to shorten master trace    ---------------------------------------------


void update_current_mask_intersect_status(struct pointer_to_1d_int_list *current_mask_intersect_list_status, struct pointer_to_1d_int_list *mask_status_list, struct Volume_struct **Volumes, int *current_volume) {
  // This function is to be executed whenever the current volume changes, or the mask status changes
  // It updates the effective mask status for each element of a volumes mask_intersect_list
  // The effective mask takes the ALL/ANY mode assosiated with each volume into account,
  //  meaning a volume needs to be within ALL/ANY of it's masks to have a status of 1
  // In most cases this mask_intersect_list will be empty, and memory operations are avoided
  //printf("Number of elements to be check: %d \n",Volumes[*current_volume]->geometry.mask_intersect_list.num_elements);
  
  if (Volumes[*current_volume]->geometry.mask_intersect_list.num_elements > 0) {
    int iterate,this_element,*mask_start,*mask_check;
    for (iterate=0;iterate<Volumes[*current_volume]->geometry.mask_intersect_list.num_elements;iterate++) {
      this_element = Volumes[*current_volume]->geometry.mask_intersect_list.elements[iterate];
      //printf("We are investigating volume number %d from the mask_intersect_list of volume %d",this_element,*current_volume);
      if (Volumes[this_element]->geometry.mask_mode == 2) { // ANY mask mode
        //printf("We are in ANY mask mode! \n");
        current_mask_intersect_list_status->elements[iterate] = 0; // Assume the mask status is 0, but if any are 1, take that instead
        for (mask_start=mask_check=Volumes[this_element]->geometry.masked_by_mask_index_list.elements;mask_check-mask_start<Volumes[this_element]->geometry.masked_by_mask_index_list.num_elements;mask_check++) {
          //printf("Checking all the mask statuses of volumes masking %d, now mask with global mask index %d ! \n",this_element,*mask_check);
          if (mask_status_list->elements[*mask_check] == 1) {
            //printf("The status was 1, so the effective status is set to 1 and the loop is stopped");
            current_mask_intersect_list_status->elements[iterate] = 1;
            break;
          }
        }
      } else { // ALL mask mode
        //printf("We are in ALL mask mode! \n");
        current_mask_intersect_list_status->elements[iterate] = 1; // Assume the mask status is 1, but if any one is 0, take that instead
        for (mask_start=mask_check=Volumes[this_element]->geometry.masked_by_mask_index_list.elements;mask_check-mask_start<Volumes[this_element]->geometry.masked_by_mask_index_list.num_elements;mask_check++) {
          //printf("Checking all the mask statuses of volumes masking %d, now mask with global mask index %d ! \n",this_element,*mask_check);
          if (mask_status_list->elements[*mask_check] == 0) {
            //printf("The status was 0, so the effective status is set to 0 and the loop is stopped \n");
            current_mask_intersect_list_status->elements[iterate] = 0;
            break;
          }
        }
      }
    }
  }
    
  /* Original version compatible with trace scope
  for (iterate=0;iterate<Volumes[current_volume]->geometry.mask_intersect_list.num_elements;iterate++) {
    this_element = Volumes[current_volume]->geometry.mask_intersect_list.elements[iterate];
    if (Volumes[this_element]->geometry.mask_mode == 2) { // ANY mask mode
      current_mask_intersect_list_status.elements[iterate] = 0; // Assume the mask status is 0, but if any are 1, take that instead
      for (mask_start=mask_check=Volumes[this_element]->geometry.masked_by_mask_index_list.elements;mask_check-mask_start<Volumes[this_element]->geometry.masked_by_mask_index_list.num_elements;mask_check++) {
        if (mask_status_list[*mask_check] == 1) {
          current_mask_intersect_list_status.elements[iterate] = 1;
          break;
        }
      }
    } else { // ALL mask mode
      current_mask_intersect_list_status.elements[iterate] = 1; // Assume the mask status is 1, but if any one is 0, take that instead
      for (mask_start=mask_check=Volumes[this_element]->geometry.masked_by_mask_index_list.elements;mask_check-mask_start<Volumes[this_element]->geometry.masked_by_mask_index_list.num_elements;mask_check++) {
        if (mask_status_list[*mask_check] == 0) {
          current_mask_intersect_list_status.elements[iterate] = 0;
          break;
        }
      }
    }
  }
  */
}

// -------------    Tagging functions    ------------------------------------------------------------------

struct tagging_tree_node_struct {
  // Statistics:
  double intensity;
  int number_of_rays;
  // tree pointers
  struct tagging_tree_node_struct *above;   // Pointer to node above
  struct tagging_tree_node_struct **volume_branches;
  struct tagging_tree_node_struct **process_branches;
};

struct list_of_tagging_tree_node_pointers {
  struct tagging_tree_node_struct **elements;
  int num_elements;
};

struct tagging_tree_node_struct *make_tagging_tree_node(void) {
    return (struct tagging_tree_node_struct *) malloc(sizeof(struct tagging_tree_node_struct));
}


struct tagging_tree_node_struct  *simple_initialize_tagging_tree_node(struct tagging_tree_node_struct *new_node) {
    new_node = make_tagging_tree_node();
    
    if (new_node == NULL) printf("ERROR, Union tagging system could not allocate memory\n");
    new_node->intensity = 4.2; // (double) 4.2;
    new_node->number_of_rays = 42; //(int) 42;
    
    printf("new_node->intensity = %f, new_node->number_of_rays = %d \n",new_node->intensity,new_node->number_of_rays);
    return new_node;
};


struct tagging_tree_node_struct *initialize_tagging_tree_node(struct tagging_tree_node_struct *new_node, struct tagging_tree_node_struct *above_node, struct Volume_struct *this_volume) {
    new_node = make_tagging_tree_node();
    
    new_node->intensity = (double) 0;
    new_node->number_of_rays = (int) 0;
    new_node->above = above_node;
    
    int next_volume_list_length = this_volume->geometry.next_volume_list.num_elements;
    new_node->volume_branches = malloc(next_volume_list_length*sizeof(struct tagging_tree_node_struct*));
    if (!new_node->volume_branches) {
      fprintf(stderr,"Failure allocating list in Union function tagging_tree_node_struct 1 - Exit!\n");
      exit(EXIT_FAILURE);
    }
    int iterate;
    // Initializing pointers so that they can be checked for NULL later. Is this redundant? Does malloc return null pointers?
    for (iterate=0;iterate<next_volume_list_length;iterate++) new_node->volume_branches[iterate] = NULL;
    
    int number_of_processes;
    if (this_volume->p_physics == NULL) number_of_processes = 0;
    else number_of_processes = this_volume->p_physics->number_of_processes;
    
    new_node->process_branches = malloc(number_of_processes*sizeof(struct tagging_tree_node_struct*));
    if (!new_node->process_branches) {
      fprintf(stderr,"Failure allocating list in Union function tagging_tree_node_struct 2 - Exit!\n");
      exit(EXIT_FAILURE);
    }
    // Initializing pointers so that they can be checked for NULL later. Is this redundant? Does malloc return null pointers?
    for (iterate=0;iterate<number_of_processes;iterate++) new_node->process_branches[iterate] = NULL;

    return new_node;
};

struct tagging_tree_node_struct *goto_process_node(struct tagging_tree_node_struct *current_node, int process_index, struct Volume_struct *this_volume, int *stop_tagging_ray, int stop_creating_nodes) {
    // Either create a new node if it has not been created yet, or travel down the tree
    if (current_node->process_branches[process_index] == NULL) {
      if (stop_creating_nodes == 0) {
        current_node->process_branches[process_index] = initialize_tagging_tree_node(current_node->process_branches[process_index],current_node,this_volume);
        return current_node->process_branches[process_index];
      } else {
        // This stops the ray from using more goto node functions and being counted in the statistics.
        // Happens because the history limit is reached, and no new histories should be started.
        *stop_tagging_ray = 1;
        return current_node;
      }
    } else {
        current_node = current_node->process_branches[process_index];
        return current_node;
    }
    
    //return current_node;
};

struct tagging_tree_node_struct *goto_volume_node(struct tagging_tree_node_struct *current_node,int current_volume, int next_volume, struct Volume_struct **Volumes, int *stop_tagging_ray, int stop_creating_nodes) {
    // I have only allocated the number of node branches that corresponds to the current_volumes next_volume_list
    // The problem is to find where on the destination list the current volume is without doing a manual search
    
    // With the new mask system there is a risk of going from and to the same volume, which should be ignored by the tagging system
    if (current_volume == next_volume) return current_node;
    
    struct tagging_tree_node_struct *output;
    int next_volume_list_index = -1;
    // Temporary slow method for finding the correct index on the destination list
    int iterate;
    
    for (iterate=0;iterate<Volumes[current_volume]->geometry.next_volume_list.num_elements;iterate++)
        if (Volumes[current_volume]->geometry.next_volume_list.elements[iterate] == next_volume) {
            next_volume_list_index = iterate;
            break;
        }
    
    // Debug phase
    //printf("Tagging: going from volume %d to volume %d, which is index number %d on it's next volume list \n",current_volume,next_volume,next_volume_list_index);
    #ifndef OPENACC
    if (next_volume_list_index == -1) {
        printf("ERROR in Union component, tagging or destination system failed, next volume was not on next volume list\n");
        printf("current_volume = %d, next_volume = %d \n",current_volume,next_volume);
        print_1d_int_list(Volumes[current_volume]->geometry.destinations_list,"destinations_list for current volume");
        exit(EXIT_FAILURE);
    }
    #endif

    // Either create a new node if it has not been created yet, or travel down the tree
    if (current_node->volume_branches[next_volume_list_index] == NULL) {
      if (stop_creating_nodes == 0) {
        current_node->volume_branches[next_volume_list_index] =  initialize_tagging_tree_node(current_node->volume_branches[next_volume_list_index],current_node,Volumes[next_volume]);
        return current_node->volume_branches[next_volume_list_index];
      } else {
        // This stops the ray from using more goto node functions and being counted in the statistics.
        // Happens because the history limit is reached, and no new histories should be started.
        *stop_tagging_ray = 1;
        return current_node;
      }
    } else {
        //current_node = current_node->volume_branches[next_volume_list_index];
        //return current_node;
        current_node = current_node->volume_branches[next_volume_list_index];
        //printf("used allocated node \n");
        return current_node;
    }
};

void add_statistics_to_node(struct tagging_tree_node_struct *current_node, Coords *r, Coords *v, double *weight, int *counter) {
    if (current_node->number_of_rays == 0) (*counter)++;
    current_node->number_of_rays = current_node->number_of_rays + 1;
    current_node->intensity = current_node->intensity + *weight;
};

struct history_node_struct {
    int volume_index;
    int process_index;
};

struct dynamic_history_list {
  struct history_node_struct *elements;
  int used_elements;
  int allocated_elements;
};

struct saved_history_struct {
    struct history_node_struct *elements;
    int used_elements;
    double intensity;
    int number_of_rays;
};

struct total_history_struct {
    struct saved_history_struct *saved_histories;
    int used_elements;
    int allocated_elements;
};


void add_to_history(struct dynamic_history_list *history, int volume_index, int process_index) {
    //printf("Adding to history[%d]: volume_index = %d, process_index = %d \n",history->used_elements,volume_index,process_index);
    if (history->allocated_elements == 0) {
        history->elements = malloc(5*sizeof(struct history_node_struct));
	if (!history->elements) {
	  fprintf(stderr,"Failure allocating list in Union function add_to_history 1 - Exit!\n");
	  exit(EXIT_FAILURE);
	}
        history->used_elements = 1;
        history->allocated_elements = 5;
        history->elements[0].volume_index = volume_index;
        history->elements[0].process_index = process_index;
    } else if (history->used_elements > history->allocated_elements-1) {
        struct dynamic_history_list temp_history;
        temp_history.elements = malloc((history->used_elements)*sizeof(struct history_node_struct));
	if (!temp_history.elements) {
	  fprintf(stderr,"Failure allocating list in Union function add_to_history 2 - Exit!\n");
	  exit(EXIT_FAILURE);
	}
        int iterate;
        for (iterate=0;iterate<history->used_elements;iterate++) {
            temp_history.elements[iterate].volume_index  = history->elements[iterate].volume_index;
            temp_history.elements[iterate].process_index = history->elements[iterate].process_index;
        }
        free(history->elements);
        history->elements = malloc((history->allocated_elements+5)*sizeof(struct history_node_struct));
	if (!history->elements) {
	  fprintf(stderr,"Failure allocating list in Union function add_to_history 3 - Exit!\n");
	  exit(EXIT_FAILURE);
	}
        for (iterate=0;iterate<history->used_elements;iterate++) {
            history->elements[iterate].volume_index  = temp_history.elements[iterate].volume_index;
            history->elements[iterate].process_index = temp_history.elements[iterate].process_index;
        }
        
        history->allocated_elements = history->allocated_elements+5;
        
        history->elements[history->used_elements].volume_index = volume_index;
        history->elements[history->used_elements].process_index = process_index;
        history->used_elements = history->used_elements+1;

	// Clear up temporary memory
	free(temp_history.elements);

    } else {
        history->elements[history->used_elements].volume_index = volume_index;
        history->elements[history->used_elements].process_index = process_index;
        history->used_elements++;
    }
    
};

void printf_history(struct dynamic_history_list *history) {
    int history_iterate;
          //printf("History number %d, intensity = %f, number of rays = %d:",hist_num, search_node->intensity, search_node->number_of_rays);
          for (history_iterate=0;history_iterate<history->used_elements-1;history_iterate++) {
            if (history->elements[history_iterate].process_index == -1) {
                printf(" V%d ->",history->elements[history_iterate].volume_index);
            } else {
                printf(" P%d ->",history->elements[history_iterate].process_index);
            }
          }
          if (history->elements[history_iterate].process_index == -1) {
                printf(" V%d \n",history->elements[history_iterate].volume_index);
          } else {
                printf(" P%d \n",history->elements[history_iterate].process_index);
          }
}

void fprintf_total_history(struct saved_history_struct *history, FILE *fp) {
    fprintf(fp,"%d\t N I=%E \t", history->number_of_rays, history->intensity);
        int history_iterate;
          for (history_iterate=0;history_iterate<history->used_elements-1;history_iterate++) {
            if (history->elements[history_iterate].process_index == -1) {
                fprintf(fp," V%d ->",history->elements[history_iterate].volume_index);
            } else {
                fprintf(fp," P%d ->",history->elements[history_iterate].process_index);
            }
          }
          if (history->elements[history_iterate].process_index == -1) {
                fprintf(fp," V%d \n",history->elements[history_iterate].volume_index);
          } else {
                fprintf(fp," P%d \n",history->elements[history_iterate].process_index);
          }
}

int Sample_compare_history_intensities (const void* a, const void* b) {
  const double da = ((const struct saved_history_struct *)a)->intensity;
  const double db = ((const struct saved_history_struct *)b)->intensity;
  return (da < db) - (da > db);
}

void write_tagging_tree(struct list_of_tagging_tree_node_pointers *master_list, struct Volume_struct **Volumes, int total_history_counter, int number_of_volumes) {
  // Start from top of tree, go to extremeties, take results and add to disk / database, free that node
  int volume_index,done,volume_iterate,process_iterate,current_volume,next_node_found,current_number_of_processes,history_iterate,hist_num;
  
  struct tagging_tree_node_struct *search_node;
  struct tagging_tree_node_struct **kill_candidate;
  struct dynamic_history_list history_data; // Allocate the history list struct
  struct dynamic_history_list *history;     // Use this pointer in the algorithm
  
  struct total_history_struct total_history;
  total_history.saved_histories = malloc(total_history_counter * sizeof(struct saved_history_struct));
  if (!total_history.saved_histories) {
    fprintf(stderr,"Failure allocating list in Union function write_tagging_tree 1 - Exit!\n");
    exit(EXIT_FAILURE);
  }
  total_history.allocated_elements = total_history_counter;
  total_history.used_elements = 0;
  
  history = &history_data;
  
  history->used_elements = 0;
  history->allocated_elements = 0;
  
  hist_num = 0;
  for (volume_index=0;volume_index<master_list->num_elements;volume_index++) {
    
    search_node = master_list->elements[volume_index];
    
    if (volume_index != 0)
        current_number_of_processes = Volumes[volume_index]->p_physics->number_of_processes;
    else
        current_number_of_processes = 0;
    
    current_volume = volume_index;
    done = 0;
    
    history->used_elements = 0;
    
    add_to_history(history,current_volume,-1);
    while(done == 0) {
        next_node_found=0;
        for (volume_iterate=0;volume_iterate<Volumes[current_volume]->geometry.next_volume_list.num_elements;volume_iterate++) {
            //printf("searc_node->volume_branches[0]->intensity = %f\n",search_node->volume_branches[0]->intensity);
            if (search_node->volume_branches[volume_iterate] != NULL) {
                current_volume = Volumes[current_volume]->geometry.next_volume_list.elements[volume_iterate];
                if (current_volume != 0)
                    current_number_of_processes = Volumes[current_volume]->p_physics->number_of_processes;
                else
                    current_number_of_processes = 0;
                
                //search_node = &(search_node->volume_branches[volume_iterate]);
                kill_candidate = &(search_node->volume_branches[volume_iterate]);
                search_node = search_node->volume_branches[volume_iterate];
                next_node_found = 1;
                add_to_history(history,current_volume,-1);
                //printf_history(history);
                break;
            }
        }
        if (next_node_found == 0) {
          //printf("doing process loop with %d steps \n",current_number_of_processes);
          for (process_iterate=0;process_iterate<current_number_of_processes;process_iterate++) {
            //if (&(search_node->process_branches[volume_iterate]) != NULL) {
            if (search_node->process_branches[process_iterate] != NULL) {
                //printf("was not NULL (process)\n");
                //search_node = &(search_node->process_branches[process_iterate]);
                kill_candidate = &(search_node->process_branches[process_iterate]);
                search_node = search_node->process_branches[process_iterate];
                next_node_found = 1;
                add_to_history(history,current_volume,process_iterate);
                //printf_history(history);
                break;
            }
          }
        }
        
        if (next_node_found == 0) {
          // write this history to disk / memory
          hist_num++;
          //printf("Reached next_node_found == 0 \n");
          if (history->used_elements > 0 && search_node->number_of_rays > 0) {
            //printf("%d rays (I=%E) with history: \t", search_node->number_of_rays, search_node->intensity);
            //printf_history(history);
            
            total_history.saved_histories[total_history.used_elements].used_elements = history->used_elements;
            total_history.saved_histories[total_history.used_elements].elements = malloc(total_history.saved_histories[total_history.used_elements].used_elements*sizeof(struct history_node_struct));
	    if (!total_history.saved_histories[total_history.used_elements].elements) {
	      fprintf(stderr,"Failure allocating list in Union function write_tagging_tree 2 - Exit!\n");
	      exit(EXIT_FAILURE);
	    }
            for (history_iterate = 0;history_iterate<history->used_elements;history_iterate++) {
                total_history.saved_histories[total_history.used_elements].elements[history_iterate] = history->elements[history_iterate];
                //printf("total_history.saved_histories[total_history.used_elements].elements[%d].volume_index \n",history_iterate,total_history.saved_histories[total_history.used_elements].elements[history_iterate].volume_index);
            }
            //total_history.saved_histories[total_history.used_elements].elements = history->elements;
            total_history.saved_histories[total_history.used_elements].intensity = search_node->intensity;
            total_history.saved_histories[total_history.used_elements].number_of_rays = search_node->number_of_rays;
            total_history.used_elements++;
          }
          
          history->used_elements = 0;
          
          // end of tree, no new nodes
          if (search_node->above == NULL) {
            done = 1;
          } else {
            // reset to the root of the tree
            *kill_candidate = NULL;
            free(search_node);
            search_node = master_list->elements[volume_index];
            
            if (volume_index != 0)
              current_number_of_processes = Volumes[volume_index]->p_physics->number_of_processes;
            else
              current_number_of_processes = 0;
    
            current_volume = volume_index;
            add_to_history(history,current_volume,-1);
          }
          
        }
    }
  }
  
  if (history->allocated_elements > 0) free(history->elements);

  qsort(total_history.saved_histories,total_history.used_elements,sizeof (struct saved_history_struct), Sample_compare_history_intensities);
  
  
  
  MPI_MASTER(
  printf("\n\n");
  printf("Top 20 most common histories. Shows the index of volumes entered (VX), and the scattering processes (PX)\n");
  for (history_iterate=0;history_iterate<total_history.used_elements && history_iterate<20;history_iterate++) {
    printf("%d\t N I=%E \t", total_history.saved_histories[history_iterate].number_of_rays, total_history.saved_histories[history_iterate].intensity);
    /*TK Changed from
      printf_history(&total_history.saved_histories[history_iterate]); to the
      following (of course this cast is only safe because the initial layout of
      the two struct types is the same... I am not 100% sure it is actually
      entirely guaranteed by the standard): */
    printf_history((struct dynamic_history_list *)(&total_history.saved_histories[history_iterate]));
  }
   
  FILE *fp;
  
  fp = fopen("union_history.dat","w");
  if(!fp) {
    fprintf(stderr,"WARNING: Could not write to logging output file union_history.dat\n");
  } else {
    fprintf(fp,"History file written by the McStas component Union_master \n");
    fprintf(fp,"When running with MPI, the results may be from just a single thread, meaning intensities are divided by number of threads\n");
    fprintf(fp,"----- Description of the used volumes -----------------------------------------------------------------------------------\n");
    
    fprintf(fp,"V0: Surrounding vacuum \n");
    for (volume_iterate=1;volume_iterate<number_of_volumes;volume_iterate++) {
      fprintf(fp,"V%d: %s  ",volume_iterate,Volumes[volume_iterate]->name);
      fprintf(fp,"Material: %s  ",Volumes[volume_iterate]->p_physics->name);
      for (process_iterate=0;process_iterate<Volumes[volume_iterate]->p_physics->number_of_processes;process_iterate++) {
	fprintf(fp," P%d: %s",process_iterate,Volumes[volume_iterate]->p_physics->p_scattering_array[process_iterate].name);
      }
      fprintf(fp,"\n");
    }
    fprintf(fp,"----- Histories sorted after intensity ----------------------------------------------------------------------------------\n");
    
    for (history_iterate=0;history_iterate<total_history.used_elements;history_iterate++) {
      fprintf_total_history(&total_history.saved_histories[history_iterate],fp);
      // Garbage collection
      if (total_history.saved_histories[history_iterate].used_elements > 0) free(total_history.saved_histories[history_iterate].elements);
    }
    fclose(fp);
  }
  )
  
  // Garbage collection
  if (total_history.allocated_elements > 0) free(total_history.saved_histories);
  
  
};


// -------------    Intersection table functions   --------------------------------------------------------
int clear_intersection_table(struct intersection_time_table_struct *intersection_time_table) {
    // Resets the intersection table when a scattering have occured.
    int iterate_volumes,iterate_solutions;
    
    // Start at one because vacuum (0) does not have a listing in the intersection table
    for (iterate_volumes = 1;iterate_volumes < intersection_time_table->num_volumes;iterate_volumes++) {
        intersection_time_table->calculated[iterate_volumes] = 0;
        // This second loop is added for safty in debugging phase, but can be removed as the information should never be accesed when calculated = 0
        for (iterate_solutions = 0;iterate_solutions < intersection_time_table->n_elements[iterate_volumes];iterate_solutions++) {
            intersection_time_table->intersection_times[iterate_volumes][iterate_solutions] = -1;
        }
    }
    
    return 1;
};

void print_intersection_table(struct intersection_time_table_struct *intersection_time_table) {

    int num_volumes,iterate,solutions;
    int max_number_of_solutions = 0;
    
    num_volumes = intersection_time_table->num_volumes;
    
    for (iterate = 0;iterate < num_volumes;iterate++) {
        if (max_number_of_solutions < intersection_time_table->n_elements[iterate])
            max_number_of_solutions = intersection_time_table->n_elements[iterate];
    }
    
    printf("------------------ INTERSECTION_TIME_TABLE -----------------");
    for (solutions = 2;solutions < max_number_of_solutions;solutions++) printf("------------");
    printf("\n");
    
    // printf("iterate      |");

    printf("           ");
    printf("| CALCULATED  |");
    for (solutions = 0;solutions < max_number_of_solutions;solutions++) {
        printf(" - SOLUTION %d - |", solutions);
    }
    for (solutions = 0;solutions < max_number_of_solutions;solutions++) {
        printf(" - SURFACE %d - |", solutions);
    }

    printf("\n");
    for (iterate = 0;iterate < num_volumes;iterate++){
    // print iterate number
    printf("Volume %d   |",iterate);
    printf(" ---- %d ---- |",intersection_time_table->calculated[iterate]);
    
        for (solutions = 0;solutions < max_number_of_solutions;solutions++) {
          if (intersection_time_table->n_elements[iterate] > solutions && intersection_time_table->calculated[iterate] == 1)
            if (intersection_time_table->intersection_times[iterate][solutions] > 0)
             printf("   %1.8f   |",intersection_time_table->intersection_times[iterate][solutions]);
            else
             printf("   %1.7f   |",intersection_time_table->intersection_times[iterate][solutions]);
          else
            printf("                |");
        }
        for (solutions = 0;solutions < max_number_of_solutions;solutions++) {
          if (intersection_time_table->n_elements[iterate] > solutions && intersection_time_table->calculated[iterate] == 1)
             printf("   %1.9d   |",intersection_time_table->surface_index[iterate][solutions]);
          else
            printf("               |");
        }
    printf("\n");
    }
    printf("------------------------------------------------------------");
    for (solutions = 2;solutions < max_number_of_solutions;solutions++) printf("------------");
    printf("\n");
    
    
    };
    
// -------------    Drawing functions   --------------------------------------------------------
void merge_lines_to_draw(struct lines_to_draw *lines_master,struct lines_to_draw *lines_new) {
    if (lines_master->number_of_lines == 0) {
    lines_master->number_of_lines = lines_new->number_of_lines;
    if (!lines_master->number_of_lines) {
      return;
    }
    lines_master->lines = malloc(lines_master->number_of_lines*sizeof(struct line_segment));
    if (!lines_master->lines) {
      fprintf(stderr,"Failure allocating list in Union function merge_lines_to_draw 2 - Exit!\n");
      exit(EXIT_FAILURE);
    }
    lines_master->lines = lines_new->lines;
    // One could free lines_new->lines;
    } else {
    int iterate;
    struct line_segment *temp_lines;
    temp_lines = malloc(lines_master->number_of_lines*sizeof(struct line_segment));
    if (!temp_lines) {
      fprintf(stderr,"Failure allocating list in Union function merge_lines_to_draw 3 - Exit!\n");
      exit(EXIT_FAILURE);
    }
    for (iterate = 0;iterate < lines_master->number_of_lines;iterate++) temp_lines[iterate] = lines_master->lines[iterate];
    free(lines_master->lines);
    lines_master->lines = malloc((lines_master->number_of_lines+lines_new->number_of_lines)*sizeof(struct line_segment));
    if (!lines_master->lines) {
      fprintf(stderr,"Failure allocating list in Union function merge_lines_to_draw 4 - Exit!\n");
      exit(EXIT_FAILURE);
    }
    for (iterate = 0;iterate < lines_master->number_of_lines;iterate++) lines_master->lines[iterate] = temp_lines[iterate];
    for (iterate = 0;iterate < lines_new->number_of_lines;iterate++) lines_master->lines[iterate+lines_master->number_of_lines] = lines_new->lines[iterate];
    lines_master->number_of_lines = lines_master->number_of_lines + lines_new->number_of_lines;
    free(temp_lines);
    }
    };

int r_has_highest_priority(Coords point,int N,struct geometry_struct **Geometries,int number_of_volumes) {
    // Function that test if a point in Volume N has the highest priority.
    // Returns 0 if another volume has higher priority at the point, and it's mask status is 1
    // Returns 1 if no other volume has higher priority at the point
    // (not active) Returns a larger integer if the volume N is a mask, and the point is not in the volume it is masking,
    //  which results in the drawing function making that number of dashes
    
    
    int mask_status,*mask_start,*mask_check;
    // If the volume is a mask, it does not have a priority, and is always on top
    if (Geometries[N]->is_mask_volume == 1) {
      return 1;
      // Can draw parts of the mask that is outside the volume it masks as dashed lines, but it just looks messy
      /*
      mask_status = 1;
      for (mask_check=mask_start=Geometries[N]->mask_list.elements;mask_check-mask_start<Geometries[N]->mask_list.num_elements;mask_check++) {
        if (Geometries[*mask_check]->within_function(point,Geometries[*mask_check]) == 1) {
          return 1; // If the point is within a volume the mask is masking, draw it as a solid line
        }
      }
      // As it was not inside any of the volumes the mask is masking, draw it as a dashed line
      return 5;
      */
    }
    
    // If the volume is a masked volume, check if the point is within it's masks
    if (Geometries[N]->is_masked_volume == 1) {
      if (Geometries[N]->mask_mode == 1) { // ALL mode, need to be within ALL masks
        for (mask_check=mask_start=Geometries[N]->masked_by_list.elements;mask_check-mask_start<Geometries[N]->masked_by_list.num_elements;mask_check++) {
          if (Geometries[*mask_check]->within_function(point,Geometries[*mask_check]) == 0) {
            return 0; // If the point is just outside one mask, the mask status is 0 and the point does not have highest priority
          }
        }
      } else { // ANY mode, need to be within at least one mask
        mask_status = 0;
        for (mask_check=mask_start=Geometries[N]->masked_by_list.elements;mask_check-mask_start<Geometries[N]->masked_by_list.num_elements;mask_check++) {
          if (Geometries[*mask_check]->within_function(point,Geometries[*mask_check]) == 1) {
            mask_status = 1;
            break;
          }
        }
        if (mask_status == 0) {
          return 0; // If it was not in a single of it's masks, the point did not have highest priority
        }
      }
    }
    
    int volume_index;
    double self_priority;
    self_priority = Geometries[N]->priority_value;
    
    
    for (volume_index = 1;volume_index<number_of_volumes;volume_index++) {
      if (volume_index != N && Geometries[volume_index]->is_mask_volume == 0) {
        if (Geometries[volume_index]->within_function(point,Geometries[volume_index])) {
          if (Geometries[volume_index]->is_masked_volume == 1) {
            // Since this volume is masked, the mask status need to be checked
            if (Geometries[volume_index]->mask_mode == 1) { //ALL mode, need to be within ALL masks
              mask_status = 1;
              for (mask_check=mask_start=Geometries[volume_index]->masked_by_list.elements;mask_check-mask_start<Geometries[volume_index]->masked_by_list.num_elements;mask_check++) {
                if (Geometries[*mask_check]->within_function(point,Geometries[*mask_check]) == 0) {
                  mask_status = 0;
                  break;
                }
              }
            } else { // ANY mode, need to be within at least one mask
              mask_status = 0;
              for (mask_check=mask_start=Geometries[volume_index]->masked_by_list.elements;mask_check-mask_start<Geometries[volume_index]->masked_by_list.num_elements;mask_check++) {
                if (Geometries[*mask_check]->within_function(point,Geometries[*mask_check]) == 1) {
                  mask_status = 1;
                  break;
                }
              }
            }
          } else mask_status = 1;
          if (Geometries[volume_index]->priority_value > self_priority && mask_status == 1) {
            // printf("Volume %d did not have highest priority at (%f,%f,%f) (Volume number %d was above)\n",N,point.x,point.y,point.z,volume_index);
            return 0;
          }
        }
      }
    }
    // printf("Volume %d did have highest priority at (%f,%f,%f) \n",N,point.x,point.y,point.z);
    return 1;
    }
    
void draw_line_positions(Coords point1,Coords point2) {
        //line(point1.x,point1.y,point1.z,point2.x,point2.y,point2.z);
        // sigh, can not use line in share. Need to save the information and pass to the mcdisplay part.
    }

int Sample_compare_doubles (const void *a, const void *b) {
  const double *da = (const double *) a;
  const double *db = (const double *) b;

  return (*da > *db) - (*da < *db);
}

struct lines_to_draw draw_line_with_highest_priority(Coords position1,Coords position2,int N,struct geometry_struct **Geometries,int number_of_volumes,int max_number_of_solutions) {
    
    int volume_index,iterate,permanent_list_length = 0;
    int number_of_solutions;
    double *temp_intersection=malloc(max_number_of_solutions*sizeof(double));
    if (!temp_intersection) {
      fprintf(stderr,"Failure allocating list in Union function lines_to_draw 1 - Exit!\n");
      exit(EXIT_FAILURE);
    }
    double r1[3],r2[3],direction[3];
    struct pointer_to_1d_double_list intersection_list;
    
    intersection_list.num_elements = 0;
    
    // double *permanent_intersection_list,*storage; // old ways
    
    r1[0] = position1.x;
    r1[1] = position1.y;
    r1[2] = position1.z;
    r2[0] = position2.x;
    r2[1] = position2.y;
    r2[2] = position2.z;
    
    // printf("r1 = (%f,%f,%f) \n",r1[0],r1[1],r1[2]);
    // printf("r2 = (%f,%f,%f) \n",r2[0],r2[1],r2[2]);
    
    direction[0] = r2[0] - r1[0];
    direction[1] = r2[1] - r1[1];
    direction[2] = r2[2] - r1[2];
    int geometry_output;
    
    // Todo: switch to nicer intersect function call
    double *double_dummy = malloc(max_number_of_solutions*sizeof(double));
    int *int_dummy = malloc(max_number_of_solutions*sizeof(int));
    // We need a storing pointer for the reallocs, to ensure that on realloc fail
    // All is handled correctly
    double *tmp;
    int *tmpint;
	
    // Find intersections
    for (volume_index = 1;volume_index < number_of_volumes; volume_index++) {
        if (volume_index != N) {
         if (Geometries[volume_index]->eShape==mesh){
            tmp = realloc(double_dummy, sizeof(double)*1000);
            tmpint = realloc(int_dummy, sizeof(double)*1000);
            if ( tmp==NULL || tmpint==NULL ) { 
              free(tmp); 
              free(tmpint);
              printf("\nERROR: Realloc failed on double dummy");
              exit(1);
            } else {
              double_dummy = tmp;
              int_dummy = tmpint;
              tmp = realloc(temp_intersection, sizeof(double)*1000);
              if ( tmp == NULL){
                free(tmp);
                printf("\nERROR: Realloc failed on temp intersection");
                exit(1);
              } else{ temp_intersection = tmp;}
            }
         }
            geometry_output = Geometries[volume_index]->intersect_function(temp_intersection, double_dummy, double_dummy, double_dummy, int_dummy, 
			                                                               &number_of_solutions, r1, direction, Geometries[volume_index]);
                for (iterate=0;iterate<number_of_solutions;iterate++) {
                    if (temp_intersection[iterate] > 0 && temp_intersection[iterate] < 1) {
		      add_element_to_double_list(&intersection_list,temp_intersection[iterate]);
		    }
                }
        }
    }
    free(double_dummy);
    free(temp_intersection);
    // Now we have a list of intersection distances between r1 and r2 and all volumes.
    // This list needs to be sorted before we continue!
    
    qsort(intersection_list.elements,intersection_list.num_elements,sizeof (double), Sample_compare_doubles);
    
    Coords *points=malloc((intersection_list.num_elements+2)*sizeof(Coords));
    if (!points) {
      fprintf(stderr,"Failure allocating list in Union function lines_to_draw 2 - Exit!\n");
      exit(EXIT_FAILURE);
    }
    points[0] = coords_set(r1[0],r1[1],r1[2]);
    points[intersection_list.num_elements+1] = coords_set(r2[0],r2[1],r2[2]);
    for (iterate = 1;iterate < intersection_list.num_elements+1;iterate++) {
        points[iterate].x = r1[0] + direction[0]*intersection_list.elements[iterate-1];
        points[iterate].y = r1[1] + direction[1]*intersection_list.elements[iterate-1];
        points[iterate].z = r1[2] + direction[2]*intersection_list.elements[iterate-1];
    }
  
    struct line_segment *lines=malloc((intersection_list.num_elements+1)*sizeof(struct line_segment));
    if (!lines) {
      fprintf(stderr,"Failure allocating list in Union function lines_to_draw 3 - Exit!\n");
      exit(EXIT_FAILURE);
    }
    int *draw_logic=malloc((intersection_list.num_elements+1)*sizeof(int));
    if (!draw_logic) {
      fprintf(stderr,"Failure allocating list in Union function lines_to_draw 4 - Exit!\n");
      exit(EXIT_FAILURE);
    }
    Coords midpoint;
    struct lines_to_draw draw_order;
    draw_order.number_of_lines = 0;
    draw_order.lines=NULL;

    int number_of_dashes;
    
    for (iterate = 0;iterate < intersection_list.num_elements + 1;iterate++) {
        lines[iterate].point1 = points[iterate];
        lines[iterate].point2 = points[iterate+1];
        midpoint.x = 0.5*(lines[iterate].point1.x + lines[iterate].point2.x);
        midpoint.y = 0.5*(lines[iterate].point1.y + lines[iterate].point2.y);
        midpoint.z = 0.5*(lines[iterate].point1.z + lines[iterate].point2.z);
        
        if ((number_of_dashes = r_has_highest_priority(midpoint,N,Geometries,number_of_volumes)) != 0) {
            draw_order.number_of_lines++;
            draw_logic[iterate] = number_of_dashes;
        } else draw_logic[iterate] = 0;
    }
       
    if (draw_order.number_of_lines > 0) {
        draw_order.lines = malloc(draw_order.number_of_lines*sizeof(struct line_segment));
	if (!draw_order.lines) {
	  fprintf(stderr,"Failure allocating list in Union function lines_to_draw 5 - Exit!\n");
	  exit(EXIT_FAILURE);
	}
        draw_order.number_of_lines = 0;
        for (iterate = 0;iterate < intersection_list.num_elements + 1;iterate++) {
            if (draw_logic[iterate] != 0) {
              lines[iterate].number_of_dashes = draw_logic[iterate];
              draw_order.lines[draw_order.number_of_lines++] = lines[iterate];
            }
        }
        if (intersection_list.num_elements > 0) free(intersection_list.elements);
    }
    
    free(points);
    free(lines);
    free(draw_logic);
    return draw_order;
    }

struct lines_to_draw draw_circle_with_highest_priority(Coords center,Coords vector,double radius,int N,struct geometry_struct **Geometries,int number_of_volumes,int max_number_of_solutions) {
    int number_of_positions = 100;

    // normalize vector
    double vector_length = length_of_position_vector(vector);
    // print_position(vector,"start vector");
    vector.x /= vector_length;
    vector.y /= vector_length;
    vector.z /= vector_length;
    // print_position(vector,"start vector normalized");
    
    // Create a vector from the center of the circle to a point on the circumference by cross product
    double cross_input[3] = {0,1,0};
    // In case the cross input is parallel with the vector, a new is chosen. Both can't be parallel.
    if (scalar_prod(cross_input[0],cross_input[1],cross_input[2],vector.x,vector.y,vector.z) > 0.99) {
        cross_input[0] = 1; cross_input[1] = 0; cross_input[2] = 0;
    }
    // print_position(make_position(cross_input),"cross input");
    
    double cross_product1[3] = {0,0,0};
    vec_prod(cross_product1[0],cross_product1[1],cross_product1[2],vector.x,vector.y,vector.z,cross_input[0],cross_input[1],cross_input[2]);
    
    // print_position(make_position(cross_product1),"cross_product1");
    
    double cross_length = length_of_3vector(cross_product1);
    
    // printf("cross_length = %f \n",cross_length);
    
    cross_product1[0] /= cross_length;
    cross_product1[1] /= cross_length;
    cross_product1[2] /= cross_length;
    
    cross_product1[0] *= radius;
    cross_product1[1] *= radius;
    cross_product1[2] *= radius;
    
    // printf("cross_product1 = (%f,%f,%f) \n",cross_product1[0],cross_product1[1],cross_product1[2]);
    
    int iterate;
    double rotate_angle;
    Coords radial_position,old_radial_position;
    struct lines_to_draw temp_draw_order,return_draw_order;
    return_draw_order.number_of_lines = 0;
    // Generate an array of positions by rotating this vector around the given vector, this corresponds to points on the circle
    old_radial_position.x = center.x + cross_product1[0];
    old_radial_position.y = center.y + cross_product1[1];
    old_radial_position.z = center.z + cross_product1[2];

    // printf("old_radial_position = (%f,%f,%f) \n",old_radial_position.x,old_radial_position.y,old_radial_position.z);
    
    for (iterate = 0;iterate < number_of_positions-1;iterate++) {
        rotate_angle = 2*3.14159*((double) iterate + 1.0)/((double) number_of_positions);
        rotate(radial_position.x,radial_position.y,radial_position.z,cross_product1[0],cross_product1[1],cross_product1[2],rotate_angle,vector.x,vector.y,vector.z);
        radial_position.x += center.x;
        radial_position.y += center.y;
        radial_position.z += center.z;
        
        // Use the draw_lines_with_highest_priority to draw get draw_orders for each line piece
        temp_draw_order = draw_line_with_highest_priority(radial_position,old_radial_position,N,Geometries,number_of_volumes,max_number_of_solutions);
       // Assemble these draw_orders to one large draw order
        merge_lines_to_draw(&return_draw_order,&temp_draw_order);

        old_radial_position.x = radial_position.x;
        old_radial_position.y = radial_position.y;
        old_radial_position.z = radial_position.z;
    }
    
    radial_position.x = center.x + cross_product1[0];
    radial_position.y = center.y + cross_product1[1];
    radial_position.z = center.z + cross_product1[2];
    // Use the draw_lines_with_highest_priority to draw get draw_orders for each line piece
    temp_draw_order = draw_line_with_highest_priority(radial_position,old_radial_position,N,Geometries,number_of_volumes,max_number_of_solutions);
    // Assemble these draw_orders to one large draw order
    merge_lines_to_draw(&return_draw_order,&temp_draw_order);
    
    // clean up
    if (temp_draw_order.number_of_lines > 0) free(temp_draw_order.lines);
    
    // return the large draw order.
    return return_draw_order;
    }

// -------------    Geometry functions   -----------------------------------------------------------------------
/*
 * This file section contains functions used for geometry.
 *
 * For each geometry A type there are:
 *  intersection function: determines intersection between straight line and the geometry type
 *  within function: determines wether a point is within the geometry, or outside
 *  geometryA_overlaps_geometryB: determines if geometry A overlaps with geometry B
 *  geometryA_inside_geometryB: determines if geometry A is completely inside geometry B
 *
 * At the end of the file, there is functions describing the logic for determining if one geometry
 *  is inside/overlaps another. It is placed here, so that all code to be expanded by adding a new
 *  geometry is within the same file.
 *
 * To add a new geometry one needs to:
 *  Write a geometry_storage_struct that contains the paramters needed to describe the geometry
 *  Add a pointer to this storage type in the geometry_parameter_union
 *  Write a function for intersection with line, using the same input scheme as for the others
 *  Write a function checking if a point is within the geometry
 *  Write a function checking if one instance of the geometry overlaps with another
 *  Write a function checking if one instance of the geometry is inside another
 *  For each exsisting geometry: 
 *      Write a function checking if an instance of this geometry overlaps with an instance of the exsisting
 *      Write a function checking if an instance of this geometry is inside an instance of the exsisting
 *      Write a function checking if an instance of an existing geometry is inside an instance of this geometry
 *
 *  Add these functions to geometry to the logic at the end of this file
 *  Write a component file similar to the exsisting ones, taking the input from the instrument file, and sending
 *   it on to the master component.
*/

struct sphere_storage{
double sph_radius;
};

struct cylinder_storage{
double cyl_radius;
double height;
Coords direction_vector;
};

struct box_storage{
double x_width1;
double y_height1;
double z_depth;
double x_width2;
double y_height2;
int is_rectangle; // Is rectangle = 1 if x_width1 = x_width2 / h1 = h2
Coords x_vector; // In main component frame
Coords y_vector;
Coords z_vector;
Coords normal_vectors[6]; // In local frame
};

struct cone_storage{
double cone_radius_top;
double cone_radius_bottom;
double height;
Coords direction_vector;
};

struct mesh_storage{
int n_facets;
int counter;
double *v1_x;
double *v1_y;
double *v1_z;
double *v2_x;
double *v2_y;
double *v2_z;
double *v3_x;
double *v3_y;
double *v3_z;
double *normal_x;
double *normal_y;
double *normal_z;
Coords direction_vector;
Coords Bounding_Box_Center;
double Bounding_Box_Radius;
};

// A number of functions below use Dot() as scalar product, replace by coords_sp define
#define Dot(a, b) coords_sp(a, b)

// Function for transforming a ray position / velocity to a local frame
Coords transform_position(Coords ray_position, Coords component_position, Rotation component_t_rotation) {

    Coords non_rotated_position = coords_sub(ray_position,component_position);
    
    // Rotate the position of the neutron around the center of the cylinder
    Coords rotated_coordinates = rot_apply(component_t_rotation,non_rotated_position);
    
    return rotated_coordinates;
}


union geometry_parameter_union allocate_box_storage_copy(union geometry_parameter_union *union_input) {
  union geometry_parameter_union union_output;
  // Allocate the space for a cylinder_storage structe in the new union_output (union as the c structre)
  union_output.p_box_storage = malloc(sizeof(struct box_storage));
  if (!union_output.p_box_storage) {
    fprintf(stderr,"Failure allocating list in Union function allocate_box_storage_copy - Exit!\n");
    exit(EXIT_FAILURE);
  }
  // Copy the input storage to the output
  *union_output.p_box_storage = *union_input->p_box_storage;
  
  return union_output;
}


union geometry_parameter_union allocate_cylinder_storage_copy(union geometry_parameter_union *union_input) {
  union geometry_parameter_union union_output;
  // Allocate the space for a cylinder_storage structe in the new union_output (union as the c structre)
  union_output.p_cylinder_storage = malloc(sizeof(struct cylinder_storage));
  if (!union_output.p_cylinder_storage) {
    fprintf(stderr,"Failure allocating list in Union function allocate_cylinder_storage_copy - Exit!\n");
    exit(EXIT_FAILURE);
  }
  // Copy the input storage to the output
  *union_output.p_cylinder_storage = *union_input->p_cylinder_storage;
  
  return union_output;
}

union geometry_parameter_union allocate_sphere_storage_copy(union geometry_parameter_union *union_input) {
  union geometry_parameter_union union_output;
  // Allocate the space for a cylinder_storage structe in the new union_output (union as the c structre)
  union_output.p_sphere_storage = malloc(sizeof(struct sphere_storage));
  if (!union_output.p_sphere_storage) {
    fprintf(stderr,"Failure allocating list in Union function allocate_sphere_storage_copy - Exit!\n");
    exit(EXIT_FAILURE);
  }
  // Copy the input storage to the output
  *union_output.p_sphere_storage = *union_input->p_sphere_storage;
  
  return union_output;
}

union geometry_parameter_union allocate_cone_storage_copy(union geometry_parameter_union *union_input) {
  union geometry_parameter_union union_output;
  // Allocate the space for a cone_storage structe in the new union_output (union as the c structre)
  union_output.p_cone_storage = malloc(sizeof(struct cone_storage));
  if (!union_output.p_cone_storage) {
    fprintf(stderr,"Failure allocating list in Union function allocate_cone_storage_copy - Exit!\n");
    exit(EXIT_FAILURE);
  }
  // Copy the input storage to the output
  *union_output.p_cone_storage = *union_input->p_cone_storage;
  
  return union_output;
}

union geometry_parameter_union allocate_mesh_storage_copy(union geometry_parameter_union *union_input) {
  union geometry_parameter_union union_output;
  // Allocate the space for a mesh_storage structe in the new union_output (union as the c structre)
  union_output.p_mesh_storage = malloc(sizeof(struct mesh_storage));
  if (!union_output.p_mesh_storage) {
    fprintf(stderr,"Failure allocating list in Union function allocate_mesh_storage_copy - Exit!\n");
    exit(EXIT_FAILURE);
  }
  // Copy the input storage to the output
  *union_output.p_mesh_storage = *union_input->p_mesh_storage;
  
  return union_output;
}

// -------------    Surroundings  ---------------------------------------------------------------
int r_within_surroundings(Coords pos,struct geometry_struct *geometry) {
    // The surroundings are EVERYWHERE
        return 1;
    }

// -------------    General geometry ------------------------------------------------------------

Coords point_on_circle(Coords center, Coords direction, double radius, int point_nr, int number_of_points) {

    Coords output;
    Coords cross_input = coords_set(0,1,0);
    
    if (scalar_prod(cross_input.x,cross_input.y,cross_input.z,direction.x,direction.y,direction.z) > 0.9) {
            cross_input.x = 1; cross_input.y = 0; cross_input.z = 0;
    }
    
    Coords cross_product;
    vec_prod(cross_product.x,cross_product.y,cross_product.z,direction.x,direction.y,direction.z,cross_input.x,cross_input.y,cross_input.z);

    double cross_length = length_of_position_vector(cross_product);
    double radius_over_cross_length = radius/cross_length;
    cross_product = coords_scalar_mult(cross_product,radius_over_cross_length);
    
    
    double rotate_angle = 2*PI*((double) point_nr)/((double) number_of_points);
    
    rotate(output.x,output.y,output.z,cross_product.x,cross_product.y,cross_product.z,rotate_angle,direction.x,direction.y,direction.z);
    
    output = coords_add(output,center);
    
    return output;
};

void points_on_circle(Coords *output, Coords center, Coords direction, double radius, int number_of_points) {

    Coords cross_input = coords_set(0,1,0);
    
    if (scalar_prod(cross_input.x,cross_input.y,cross_input.z,direction.x,direction.y,direction.z) > 0.9) {
            cross_input.x = 1; cross_input.y = 0; cross_input.z = 0;
    }
    
    Coords cross_product;
    vec_prod(cross_product.x,cross_product.y,cross_product.z,direction.x,direction.y,direction.z,cross_input.x,cross_input.y,cross_input.z);

    double cross_length = length_of_position_vector(cross_product);
    double radius_over_cross_length = radius/cross_length;
    cross_product = coords_scalar_mult(cross_product,radius_over_cross_length);
    
    int point_nr;
    double rotate_angle;
    for (point_nr = 0;point_nr<number_of_points;point_nr++) {
        rotate_angle = 2*PI*((double) point_nr)/((double) number_of_points);
        rotate(output[point_nr].x,output[point_nr].y,output[point_nr].z,cross_product.x,cross_product.y,cross_product.z,rotate_angle,direction.x,direction.y,direction.z);
        output[point_nr] = coords_add(output[point_nr],center);
    }
};

// -------- Brute force last resorts for within / overlap -------------------------------------

int A_within_B(struct geometry_struct *child, struct geometry_struct *parent, int resolution) {
  // This function assumes the parent (B) is a convex geoemtry
  // If all points on the shell of geometry A is within B, so are all lines between them.
  
  // Should be done first, but takes so little time and may save a longer computation
  if (parent->within_function(child->center,parent) == 0) return 0;
  
  // resolution selects the number of points to be generated on the shell.
  struct pointer_to_1d_coords_list shell_points;
  shell_points = child->shell_points(child,resolution);
  // Shell_points.elements need to be freed before leaving this function
  
  if (shell_points.num_elements > resolution || shell_points.num_elements < 0) {
    printf("\nERROR: Shell point function used in A_within_B return garbage num_elements. \n");
    exit(EXIT_FAILURE);
  }
  
  int iterate;
  
  for (iterate=0;iterate<shell_points.num_elements;iterate++) {
    if (parent->within_function(shell_points.elements[iterate],parent) == 0) {
      free(shell_points.elements);
      return 0;
    }
  }
  
  free(shell_points.elements);
  
  // If all points are inside, the entire geometry is assumed inside as parent should be convex
  return 1;
}

int mesh_A_within_B(struct geometry_struct *child, struct geometry_struct *parent) {
  // This function assumes the parent (B) is a convex geoemtry
  // If all points on the shell of geometry A is within B, so are all lines between them.
  // This is modified so resolution is not set manually, but all mesh shell points are taken
  
  // resolution selects the number of points to be generated on the shell.
  struct pointer_to_1d_coords_list shell_points;
  int resolution = 300;
  shell_points = child->shell_points(child, resolution); // mesh shell points do not use max points
  // Shell_points.elements need to be freed before leaving this function
  //printf("\n GOT OUT TEST");
  int iterate;
  
  for (iterate=0;iterate<shell_points.num_elements;iterate++) {
    if (parent->within_function(shell_points.elements[iterate],parent) == 0) {
      free(shell_points.elements);
      return 0;
    }
  }
  
  
  // If all points are inside, the entire geometry is assumed inside as parent should be convex
  free(shell_points.elements);
  return 1;
}

/*
// Turned out to be harder to generalize the overlap functions, but at least within was doable.
int A_overlaps_B(struct geometry_struct *child, struct geometry_struct *parent) {
  // This function assumes the parent (B) is a convex geoemtry
  // Does not work, need to check lines between points
  
  // Starting this system with a simple constant 64 point generation.
  struct pointer_to_1d_coords_list shell_points;
  shell_points = child.shell_points(child,64);
  
  int iterate;
  
  for (iterate=0;iterate<shell_points.num_elements;iterate++) {
    if (parent.within_function(shell_points.elements[iterate],parent) == 1) return 1;
  }
  
  
  // This requires that the points on the shell are saved pair wise in such a way that
  //  the lines between them would cover the entire surface when the resolution goes to
  //  infinity. NOT GOING TO WORK FOR BOX
  
  
  for (iterate=0;iterate<floor(shell_points.num_elements/2);iterate = iterate + 2) {
    // check intersections with parent between the two child points
    if (existence_of_intersection(shell_points.elements[iterate],shell_points.elements[iterate+1],parent) == 1) return 1;
  }
  
  
  // If no points were inside, the geometries are assumed not to overlap as parent should be convex
  return 0;
}
*/



// -------------    Functions for box ray tracing used in trace ---------------------------------
// These functions needs to be fast, as they may be used many times for each ray
int sample_box_intersect_advanced(double *t, double *nx, double *ny, double *nz, int *surface_index, int *num_solutions,double *r,double *v,struct geometry_struct *geometry) {
    // possible approaches
    // rotate to a simple coordinate system by rotating the ray (easier to switch to McStas standard)
    
    // There are still many variables here that can be pre calculated and saved in the box_storage.
    
    double depth = geometry->geometry_parameters.p_box_storage->z_depth;
    double width1 = geometry->geometry_parameters.p_box_storage->x_width1;
    double width2 = geometry->geometry_parameters.p_box_storage->x_width2;
    double height1 = geometry->geometry_parameters.p_box_storage->y_height1;
    double height2 = geometry->geometry_parameters.p_box_storage->y_height2;
    
    Coords x_vector = geometry->geometry_parameters.p_box_storage->x_vector;
    Coords y_vector = geometry->geometry_parameters.p_box_storage->y_vector;
    Coords z_vector = geometry->geometry_parameters.p_box_storage->z_vector;
    
    Coords normal_vectors;
    
    // Declare variables for the function
    Coords coordinates;
    
    // Coordinate transformation
    coordinates.x = r[0] - geometry->center.x;
    coordinates.y = r[1] - geometry->center.y;
    coordinates.z = r[2] - geometry->center.z;
    
    Coords rotated_coordinates;
    // Rotate the position of the neutron around the center of the cylinder
    rotated_coordinates = rot_apply(geometry->transpose_rotation_matrix,coordinates);
    
    Coords velocity = coords_set(v[0],v[1],v[2]);
    Coords rotated_velocity;
    
    // Rotate the position of the neutron around the center of the cylinder
    rotated_velocity = rot_apply(geometry->transpose_rotation_matrix,velocity);
    
    double x_result,y_result,z_result;
    
    *num_solutions = 0;
    // normal_vectors 0 and 1 have x and y = 0;
    // normal vectors 0: point in plane [0 0 -0.5*depth]
    normal_vectors = geometry->geometry_parameters.p_box_storage->normal_vectors[0];
    t[*num_solutions] = (-0.5*depth - rotated_coordinates.z)*normal_vectors.z/(normal_vectors.z*rotated_velocity.z);
    //printf("Intersection_time for face 0 = %f\n",t[*num_solutions]);
    x_result = rotated_coordinates.x + t[*num_solutions]*rotated_velocity.x;
    y_result = rotated_coordinates.y + t[*num_solutions]*rotated_velocity.y;
    z_result = rotated_coordinates.z + t[*num_solutions]*rotated_velocity.z; // only for debug
    //printf("Test solution for face number 0: (x,y) = (%f,%f,%f)\n",x_result,y_result,z_result);
    if (x_result >= -0.5*width1 && x_result <= 0.5*width1 && y_result >= -0.5*height1 && y_result <= 0.5*height1) {
		nx[*num_solutions] = normal_vectors.x;
		ny[*num_solutions] = normal_vectors.y;
		nz[*num_solutions] = -normal_vectors.z;
		surface_index[*num_solutions] = 0;
        (*num_solutions)++;		
        //printf("Solution found for face number 0\n");
    }
    
    // normal vectors 1: point in plane [0 0 0.5*depth]
    normal_vectors = geometry->geometry_parameters.p_box_storage->normal_vectors[1];
    t[*num_solutions] = (0.5*depth - rotated_coordinates.z)*normal_vectors.z/(normal_vectors.z*rotated_velocity.z);
    //printf("Intersection_time for face 1 = %f\n",t[*num_solutions]);
    x_result = rotated_coordinates.x + t[*num_solutions]*rotated_velocity.x;
    y_result = rotated_coordinates.y + t[*num_solutions]*rotated_velocity.y;
    //z_result = rotated_coordinates.z + t[*num_solutions]*rotated_velocity.z; // only for debug
    //printf("Test solution for face number 1: (x,y) = (%f,%f,%f)\n",x_result,y_result,z_result);
    if (x_result >= -0.5*width2 && x_result <= 0.5*width2 && y_result >= -0.5*height2 && y_result <= 0.5*height2) {
		nx[*num_solutions] = normal_vectors.x;
		ny[*num_solutions] = normal_vectors.y;
		nz[*num_solutions] = normal_vectors.z;
		surface_index[*num_solutions] = 1;				
        (*num_solutions)++;		
        //printf("Solution found for face number 1\n");
    }
    // These were done first as they are fastest, and most likely to be the solutions (normal to do small depth and large width/height), and standard orientation is to have one of these faces towards the source. When the fastest and most likely are done first, there is larger chance to skip more and slower calculations
    
    if (*num_solutions != 2) {
        // normal vectors 2 and 3 have y = 0
        normal_vectors = geometry->geometry_parameters.p_box_storage->normal_vectors[2];
        t[*num_solutions] = ((0.5*width1 - rotated_coordinates.x)*normal_vectors.x + (-0.5*depth - rotated_coordinates.z)*normal_vectors.z)/(normal_vectors.x*rotated_velocity.x+normal_vectors.z*rotated_velocity.z);
        // x_result = rotated_coordinates.x + t[*num_solutions]*rotated_velocity.x;
        y_result = rotated_coordinates.y + t[*num_solutions]*rotated_velocity.y;
        z_result = rotated_coordinates.z + t[*num_solutions]*rotated_velocity.z;
        if (z_result > -0.5*depth && z_result < 0.5*depth && y_result >= -0.5*(height1+(height2-height1)*(0.5*depth+z_result)/depth) && y_result < 0.5*(height1+(height2-height1)*(0.5*depth+z_result)/depth)) {
			nx[*num_solutions] = normal_vectors.x;
			ny[*num_solutions] = normal_vectors.y;
			nz[*num_solutions] = normal_vectors.z;	
			surface_index[*num_solutions] = 2;					
            (*num_solutions)++;			
            //printf("Solution found for face number 2\n");
        }
    }
    
    if (*num_solutions != 2) {
        // normal vectors 2 and 3 have y = 0
        normal_vectors = geometry->geometry_parameters.p_box_storage->normal_vectors[3];
        t[*num_solutions] = ((-0.5*width1 - rotated_coordinates.x)*normal_vectors.x + (-0.5*depth - rotated_coordinates.z)*normal_vectors.z)/(normal_vectors.x*rotated_velocity.x+normal_vectors.z*rotated_velocity.z);
        // x_result = rotated_coordinates.x + t[*num_solutions]*rotated_velocity.x;
        y_result = rotated_coordinates.y + t[*num_solutions]*rotated_velocity.y;
        z_result = rotated_coordinates.z + t[*num_solutions]*rotated_velocity.z;
        if (z_result > -0.5*depth && z_result < 0.5*depth && y_result > -0.5*(height1+(height2-height1)*(0.5*depth+z_result)/depth) && y_result <= 0.5*(height1+(height2-height1)*(0.5*depth+z_result)/depth)) {
			nx[*num_solutions] = normal_vectors.x;
			ny[*num_solutions] = normal_vectors.y;
			nz[*num_solutions] = normal_vectors.z;
			surface_index[*num_solutions] = 3;						
            (*num_solutions)++;			
            //printf("Solution found for face number 3\n");
        }
    }
    
    if (*num_solutions != 2) {
        // normal vectors 4 and 5 have x = 0
        normal_vectors = geometry->geometry_parameters.p_box_storage->normal_vectors[4];
        t[*num_solutions] = ((0.5*height1 - rotated_coordinates.y)*normal_vectors.y + (-0.5*depth - rotated_coordinates.z)*normal_vectors.z)/(normal_vectors.y*rotated_velocity.y+normal_vectors.z*rotated_velocity.z);
        x_result = rotated_coordinates.x + t[*num_solutions]*rotated_velocity.x;
        //y_result = rotated_coordinates.y + t[*num_solutions]*rotated_velocity.y;
        z_result = rotated_coordinates.z + t[*num_solutions]*rotated_velocity.z;
        if (z_result > -0.5*depth && z_result < 0.5*depth && x_result >= -0.5*(width1+(width2-width1)*(0.5*depth+z_result)/depth) && x_result < 0.5*(width1+(width2-width1)*(0.5*depth+z_result)/depth)) {
			nx[*num_solutions] = normal_vectors.x;
			ny[*num_solutions] = normal_vectors.y;
			nz[*num_solutions] = normal_vectors.z;
			surface_index[*num_solutions] = 4;			
            (*num_solutions)++;			
            //printf("Solution found for face number 4\n");
        }
    }
    
    if (*num_solutions != 2) {
        // normal vectors 4 and 5 have x = 0
        normal_vectors = geometry->geometry_parameters.p_box_storage->normal_vectors[5];
        t[*num_solutions] = ((-0.5*height1 - rotated_coordinates.y)*normal_vectors.y + (-0.5*depth - rotated_coordinates.z)*normal_vectors.z)/(normal_vectors.y*rotated_velocity.y+normal_vectors.z*rotated_velocity.z);
        x_result = rotated_coordinates.x + t[*num_solutions]*rotated_velocity.x;
        //y_result = rotated_coordinates.y + t[*num_solutions]*rotated_velocity.y;
        z_result = rotated_coordinates.z + t[*num_solutions]*rotated_velocity.z;
        if (z_result > -0.5*depth && z_result < 0.5*depth && x_result > -0.5*(width1+(width2-width1)*(0.5*depth+z_result)/depth) && x_result <= 0.5*(width1+(width2-width1)*(0.5*depth+z_result)/depth)) {
			nx[*num_solutions] = normal_vectors.x;
			ny[*num_solutions] = normal_vectors.y;
			nz[*num_solutions] = normal_vectors.z;
			surface_index[*num_solutions] = 5;						
            (*num_solutions)++;			
            //printf("Solution found for face number 5\n");
        }
    }
	
	Coords normal_vector_rotated;
	Coords normal_vector;
	
	// Sort solution according to intersection time and rotate normal vectors to master coordinate system 
    switch(*num_solutions) {
    case 2:
        if (t[0] > t[1]) {
            double temp = t[1];
            t[1] = t[0];
            t[0] = temp;
			
			// Also switch the normal vectors
			temp = nx[1];
			nx[1] = nx[0];
			nx[0] = temp;
			
			temp = ny[1];
			ny[1] = ny[0];
			ny[0] = temp;
			
			temp = nz[1];
			nz[1] = nz[0];
			nz[0] = temp;
			
			// Switch surface_index
			int temp_int = surface_index[1];
			surface_index[1] = surface_index[0];
			surface_index[0] = temp_int;
        }
		
		// Rotate back to master coordinate system	
		normal_vector_rotated = coords_set(nx[0], ny[0], nz[0]);
	    normal_vector = rot_apply(geometry->rotation_matrix, normal_vector_rotated);
		nx[0] = normal_vector.x; ny[0] = normal_vector.y; nz[0] = normal_vector.z;
		
		normal_vector_rotated = coords_set(nx[1], ny[1], nz[1]);
	    normal_vector = rot_apply(geometry->rotation_matrix, normal_vector_rotated);
		nx[1] = normal_vector.x; ny[1] = normal_vector.y; nz[1] = normal_vector.z;		
		
        return 1;
    case 1:
        t[1] = -1;
		
		// Rotate back to master coordinate system	
		normal_vector_rotated = coords_set(nx[0], ny[0], nz[0]);
	    normal_vector = rot_apply(geometry->rotation_matrix, normal_vector_rotated);
		nx[0] = normal_vector.x; ny[0] = normal_vector.y; nz[0] = normal_vector.z;
		
        return 1;
    case 0:
        t[0] = -1;
        t[1] = -1;
        return 0;
    }
    
    // Above switch will catch all solutions, but the return 0 here silences a compiler warning.
    return 0;
};

void box_corners_global_frame(Coords *corner_points, struct geometry_struct *geometry) {
    // Returns a pointer to an array containing the 8 positions of the corners of the box.
    double depth = geometry->geometry_parameters.p_box_storage->z_depth;
    double width1 = geometry->geometry_parameters.p_box_storage->x_width1;
    double width2 = geometry->geometry_parameters.p_box_storage->x_width2;
    double height1 = geometry->geometry_parameters.p_box_storage->y_height1;
    double height2 = geometry->geometry_parameters.p_box_storage->y_height2;
    
    Coords x_vector = geometry->geometry_parameters.p_box_storage->x_vector;
    Coords y_vector = geometry->geometry_parameters.p_box_storage->y_vector;
    Coords z_vector = geometry->geometry_parameters.p_box_storage->z_vector;
    
    Coords center = geometry->center;
    
    corner_points[0] = coords_add(coords_add(coords_add(center,coords_scalar_mult(z_vector,-0.5*depth)),coords_scalar_mult(x_vector,-0.5*width1)),coords_scalar_mult(y_vector,-0.5*height1));
    
    corner_points[1] = coords_add(corner_points[0],coords_scalar_mult(x_vector,width1));
    corner_points[2] = coords_add(corner_points[1],coords_scalar_mult(y_vector,height1));
    corner_points[3] = coords_add(corner_points[0],coords_scalar_mult(y_vector,height1));
    
    corner_points[4] = coords_add(coords_add(coords_add(center,coords_scalar_mult(z_vector,0.5*depth)),coords_scalar_mult(x_vector,-0.5*width2)),coords_scalar_mult(y_vector,-0.5*height2));
    
    corner_points[5] = coords_add(corner_points[4],coords_scalar_mult(x_vector,width2));
    corner_points[6] = coords_add(corner_points[5],coords_scalar_mult(y_vector,height2));
    corner_points[7] = coords_add(corner_points[4],coords_scalar_mult(y_vector,height2));
};

void box_corners_local_frame(Coords *corner_points, struct geometry_struct *geometry) {
    double depth = geometry->geometry_parameters.p_box_storage->z_depth;
    double width1 = geometry->geometry_parameters.p_box_storage->x_width1;
    double width2 = geometry->geometry_parameters.p_box_storage->x_width2;
    double height1 = geometry->geometry_parameters.p_box_storage->y_height1;
    double height2 = geometry->geometry_parameters.p_box_storage->y_height2;
    
    Coords center = geometry->center;
    Coords x_vector = coords_set(1,0,0);
    Coords y_vector = coords_set(0,1,0);
    Coords z_vector = coords_set(0,0,1);
    Coords origo = coords_set(0,0,0);

    // Bug fixed on 25/11, center was used instead of origo
    corner_points[0] = coords_add(coords_add(coords_add(origo,coords_scalar_mult(z_vector,-0.5*depth)),coords_scalar_mult(x_vector,-0.5*width1)),coords_scalar_mult(y_vector,-0.5*height1));
    
    corner_points[1] = coords_add(corner_points[0],coords_scalar_mult(x_vector,width1));
    corner_points[2] = coords_add(corner_points[1],coords_scalar_mult(y_vector,height1));
    corner_points[3] = coords_add(corner_points[0],coords_scalar_mult(y_vector,height1));
    
    corner_points[4] = coords_add(coords_add(coords_add(origo,coords_scalar_mult(z_vector,0.5*depth)),coords_scalar_mult(x_vector,-0.5*width2)),coords_scalar_mult(y_vector,-0.5*height2));
    
    corner_points[5] = coords_add(corner_points[4],coords_scalar_mult(x_vector,width2));
    corner_points[6] = coords_add(corner_points[5],coords_scalar_mult(y_vector,height2));
    corner_points[7] = coords_add(corner_points[4],coords_scalar_mult(y_vector,height2));
};

int sample_box_intersect_simple(double *t, double *nx, double *ny, double *nz, int *surface_index, int *num_solutions, double *r, double *v, struct geometry_struct *geometry) {
    double width = geometry->geometry_parameters.p_box_storage->x_width1;
    double height = geometry->geometry_parameters.p_box_storage->y_height1;
    double depth = geometry->geometry_parameters.p_box_storage->z_depth;
	
    // Declare variables for the function
    double x_new,y_new,z_new;
    
    // Coordinate transformation
    x_new = r[0] - geometry->center.x;
    y_new = r[1] - geometry->center.y;
    z_new = r[2] - geometry->center.z;
    
    Coords coordinates = coords_set(x_new,y_new,z_new);
    Coords rotated_coordinates;
    //printf("Cords coordinates = (%f,%f,%f)\n",coordinates.x,coordinates.y,coordinates.z);
    
    // Rotate the position of the neutron around the center of the cylinder
    rotated_coordinates = rot_apply(geometry->transpose_rotation_matrix,coordinates);
    // rotated_coordinates = rot_apply(rotation_matrix_debug,coordinates);
    //printf("Cords rotated_coordinates = (%f,%f,%f)\n",rotated_coordinates.x,rotated_coordinates.y,rotated_coordinates.z);
    
    Coords velocity = coords_set(v[0],v[1],v[2]);
    Coords rotated_velocity;
    //printf("Cords velocity = (%f,%f,%f)\n",velocity.x,velocity.y,velocity.z);
    
    // Rotate the position of the neutron around the center of the cylinder
    rotated_velocity = rot_apply(geometry->transpose_rotation_matrix,velocity);
    // rotated_velocity = rot_apply(rotation_matrix_debug,velocity);
    //printf("Cords rotated_velocity = (%f,%f,%f)\n",rotated_velocity.x,rotated_velocity.y,rotated_velocity.z);
    
    int output = 0;
    // Run McStas built in box intersect funtion (box centered around origin)
    if ((output = box_intersect(&t[0],&t[1],rotated_coordinates.x,rotated_coordinates.y,rotated_coordinates.z,rotated_velocity.x,rotated_velocity.y,rotated_velocity.z,width,height,depth)) == 0) {
        *num_solutions = 0;t[0]=-1;t[1]=-1;
	}
    else if (t[1] != 0) *num_solutions = 2;
    else {*num_solutions = 1;t[1]=-1;} // t[2] is a memory error!
	
    // Rewritten code from refractor.comp
    int index;
    double x, y, z, dt;
    double rotated_nx, rotated_ny, rotated_nz;
    Coords normal_vector_rotated;
    Coords normal_vector;
    for (index=0; index<*num_solutions; index++) {

        dt = t[index];
        
        // Intersection point in box coordinate system
        x = rotated_coordinates.x + dt*rotated_velocity.x;
        y = rotated_coordinates.y + dt*rotated_velocity.y;
        z = rotated_coordinates.z + dt*rotated_velocity.z;
        
        // determine hit face: difference to plane is closest to 0 (in box coordinate system)
		// A deviation of 0 means its on that surface, due to finite accuracy it will never be exact, so the closest is chosen
		double x_deviation = fabs(fabs(x/width) - 0.5); 
	    double y_deviation =  fabs(fabs(y/height) - 0.5);
		double z_deviation = fabs(fabs(z/depth) - 0.5);
		
		if (x_deviation <= y_deviation && x_deviation <= z_deviation) {
		    normal_vector_rotated = coords_set(x > 0 ? 1.0 : -1.0, 0.0, 0.0);
		} else if (y_deviation <= x_deviation && y_deviation <= z_deviation) {
		    normal_vector_rotated = coords_set(0.0, y > 0 ? 1.0 : -1.0, 0.0);
		} else {
		    normal_vector_rotated = coords_set(0.0, 0.0, z > 0 ? 1.0 : -1.0);
		}
		
		// Set surface index
		if (normal_vector_rotated.z < -0.5)
			// back
			surface_index[index] = 0;
		else if(normal_vector_rotated.z > 0.5)
			// front
			surface_index[index] = 1;
		else if(normal_vector_rotated.x > 0.5)
			// left
			surface_index[index] = 2;			
		else if(normal_vector_rotated.x < -0.5)
			// right
			surface_index[index] = 3;			
		else if(normal_vector_rotated.y > 0.5)
			// top
			surface_index[index] = 4;			
		else if(normal_vector_rotated.y < -0.5)
			// bottom
			surface_index[index] = 5;
		
        // Rotate back to master coordinate system
        normal_vector = rot_apply(geometry->rotation_matrix, normal_vector_rotated);
		
        // Set the normal vector components
        nx[index] = normal_vector.x;
        ny[index] = normal_vector.y;
        nz[index] = normal_vector.z;
		
		NORM(nx[index], ny[index], nz[index]);
    }
    
    return output;
};

int r_within_box_simple(Coords pos,struct geometry_struct *geometry) {
    // Unpack parameters
    double width = geometry->geometry_parameters.p_box_storage->x_width1;
    double height = geometry->geometry_parameters.p_box_storage->y_height1;
    double depth = geometry->geometry_parameters.p_box_storage->z_depth;
    
    //Coords coordinates = coords_set(x_new,y_new,z_new);
    Coords coordinates = coords_sub(pos,geometry->center);
    
    Coords rotated_coordinates;
    // printf("Cords coordinates = (%f,%f,%f)\n",coordinates.x,coordinates.y,coordinates.z);
    
    // Rotate the position of the neutron around the center of the cylinder
    rotated_coordinates = rot_apply(geometry->transpose_rotation_matrix,coordinates);
    
    // May be faster to check for one at a time to get an early return 0
    return (rotated_coordinates.x > -0.5*width && rotated_coordinates.x < 0.5*width && rotated_coordinates.y > -0.5*height && rotated_coordinates.y < 0.5*height && rotated_coordinates.z > -0.5*depth && rotated_coordinates.z < 0.5*depth);
};

int r_within_cone(Coords pos,struct geometry_struct *geometry) {
    // Is point inside cone?
// Unpack parameters
    double radius_top = geometry->geometry_parameters.p_cone_storage->cone_radius_top;
    double radius_bottom = geometry->geometry_parameters.p_cone_storage->cone_radius_bottom;
    double height = geometry->geometry_parameters.p_cone_storage->height;
    Coords center = geometry->center;
    
    double x_new,y_new,z_new;
    
    // Coordinate transformation
    x_new = pos.x - geometry->center.x;
    y_new = pos.y - geometry->center.y;
    z_new = pos.z - geometry->center.z;
    
    Coords coordinates = coords_set(x_new,y_new,z_new);
    Coords rotated_coordinates;
   
    rotated_coordinates = rot_apply(geometry->transpose_rotation_matrix,coordinates);

    
    int verbal = 0;
    // Generate unit direction vector along center axis of cones
    
    // Start with vector that points along the cone in the simple frame, and rotate to global
    Coords simple_vector = coords_set(0,1,0);
    
    int inside = 1;
    
    if (height*0.5 < fabs(rotated_coordinates.y)) {
            if (verbal == 1) printf("point sticks out height wise \n");
            inside = 0;
    } else {

        // Test for separation radially
        // if (rSum − |Delta − Dot(W0,Delta)∗W0| < 0) seperated = 1;
        // double vector_between_cone_axis[3];
        // vector_between_cone_axis[0] = delta.x - scalar_prod1*vector1.x;
        // vector_between_cone_axis[1] = delta.y - scalar_prod1*vector1.y;
        // vector_between_cone_axis[2] = delta.z - scalar_prod1*vector1.z;

        Coords vector1 = coords_sub(rotated_coordinates,center);
        //printf("\nrotated coordinates = [%f,%f,%f]",rotated_coordinates.x,rotated_coordinates.y,rotated_coordinates.z);

        // Calculate radius at the y height of the tested point
        double cone_slope = (radius_top-radius_bottom)/height;
        double radius_pos = radius_bottom + cone_slope * (rotated_coordinates.y + (height/2.0)); // Here delta.y is used. Make sure that y is in the height direction of cone...
        
        //printf("\nradius_pos = %f",radius_pos);
        //printf("\nradius_pos distance = %f",sqrt(rotated_coordinates.x*rotated_coordinates.x+rotated_coordinates.z*rotated_coordinates.z));

        

        if (radius_pos *radius_pos < (rotated_coordinates.x*rotated_coordinates.x+rotated_coordinates.z*rotated_coordinates.z)) {
            if (verbal == 1) printf("Point sticks out radially \n");
            inside = 0;
        }
        //printf("\n IS INSIDE? %i",inside);
    }

    if (inside == 0) return 0;
    else return 1;
    };

int sample_cone_intersect(double *t, double *nx, double *ny, double *nz, int *surface_index, int *num_solutions, double *r, double *v, struct geometry_struct *geometry) {

    /*
    double radius_top = geometry->geometry_parameters.p_cone_storage->cone_radius_top;
    double radius_bottom = geometry->geometry_parameters.p_cone_storage->cone_radius_bottom;
    double height = geometry->geometry_parameters.p_cone_storage->height;
    */

    double radius_top = geometry->geometry_parameters.p_cone_storage->cone_radius_top;
    double radius_bottom = geometry->geometry_parameters.p_cone_storage->cone_radius_bottom;
    double height = geometry->geometry_parameters.p_cone_storage->height;
    
    
    
    //Coords direction = geometry->geometry_parameters.p_cone_storage->direction_vector;
    Coords center = geometry->center;

    Coords direction = coords_set(0,1,0);



    //Coords bottom_point = coords_add(center,coords_scalar_mult(direction,-0.5*height));
    //Coords top_point = coords_add(center,coords_scalar_mult(direction,0.5*height));

    // Declare variables for the function
    double x_new,y_new,z_new;
    
    // Coordinate transformation
    x_new = r[0] - geometry->center.x;
    y_new = r[1] - geometry->center.y;
    z_new = r[2] - geometry->center.z;
    
    Coords coordinates = coords_set(x_new,y_new,z_new);
    Coords rotated_coordinates;
    // printf("Cords coordinates = (%f,%f,%f)\n",coordinates.x,coordinates.y,coordinates.z);
    
    // debug
    // Rotation rotation_matrix_debug[3][3];
    // rot_set_rotation(rotation_matrix_debug,-1.0*geometry->rotation.x,-1.0*geometry->rotation.y,-1.0*geometry->rotation.z);
    // rot_transpose(geometry->rotation_matrix,rotation_matrix_debug);

    // Rotate the position of the neutron around the center of the cone
    rotated_coordinates = rot_apply(geometry->transpose_rotation_matrix,coordinates);
    // rotated_coordinates = rot_apply(rotation_matrix_debug,coordinates);
    //     printf("Cords rotated_coordinates = (%f,%f,%f)\n",rotated_coordinates.x,rotated_coordinates.y,rotated_coordinates.z);
    
    Coords velocity = coords_set(v[0],v[1],v[2]);
    Coords rotated_velocity;
    //     printf("Cords velocity = (%f,%f,%f)\n",velocity.x,velocity.y,velocity.z);
    
    // Rotate the position of the neutron around the center of the cone
    rotated_velocity = rot_apply(geometry->transpose_rotation_matrix,velocity);
    // rotated_velocity = rot_apply(rotation_matrix_debug,velocity);
    //     printf("Cords rotated_velocity = (%f,%f,%f)\n",rotated_velocity.x,rotated_velocity.y,rotated_velocity.z);
    

    Coords normal_vector_rotated;
    Coords normal_vector;
	    
    // Test if the ray gets close to the cone by making a sphere around cone and check intersection
    double Y;
    double max_r;

    Y = -(0.5*height)-(radius_top*radius_top-radius_bottom*radius_bottom)/(2*height);
    if (radius_top > radius_bottom){
        max_r = radius_top;
    }else{
        max_r = radius_bottom;
    }
    double sphere_radius =  sqrt((Y+(1/2)*height)*(Y+(1/2)*height)+max_r*max_r);
    Coords sphere_pos = coords_set(center.x+direction.x*Y,center.y+direction.y*Y,center.z+direction.z*Y);
    
    double x_sphere = sphere_pos.x - geometry->center.x;
    double y_sphere = sphere_pos.y - geometry->center.y;
    double z_sphere = sphere_pos.z - geometry->center.z;
    double sphere_t[2];
    
    
    int output = 0;
    // Run McStas built in sphere intersect funtion (sphere centered around origin)
    if ((output = sphere_intersect(&sphere_t[0],&sphere_t[1],x_sphere,y_sphere,z_sphere,v[0],v[1],v[2],sphere_radius)) == 0)
    

    if (sphere_t[0] > -1){
        if (sphere_t[1] > -1){
            t[0] = -1;
            t[1] = -1;
            return 0;
        }
    }
    
    //Coords normal_vector_rotated;
	//Coords normal_vector;
    
    double tmp;
    

    // Check if the ray intersects with the top and bottom circles
    double t_plane[2];
    t_plane[0] = (height/2 - rotated_coordinates.y) / rotated_velocity.y;
    t_plane[1] = (-height/2 - rotated_coordinates.y) / rotated_velocity.y;
    *num_solutions = 2;
    // Reduce from infinite plane to circles
            //  sqrt(xpos^2 + zpos^2) > r  => t = -1
    double xpos;
    double zpos;
    xpos=rotated_coordinates.x+t_plane[0]*rotated_velocity.x;
    zpos=rotated_coordinates.z+t_plane[0]*rotated_velocity.z;
 

    if ((xpos*xpos + zpos*zpos) > radius_top*radius_top){
        t_plane[0] = -1;
        *num_solutions = *num_solutions-1;
    } else {
		normal_vector_rotated = coords_set(0,1,0);
			
	    normal_vector = rot_apply(geometry->rotation_matrix, normal_vector_rotated);
		
	    // Set the normal vector components
	    nx[0] = normal_vector.x;
	    ny[0] = normal_vector.y;
	    nz[0] = normal_vector.z;
		
		surface_index[0] = 1; // top index
    }
    xpos=rotated_coordinates.x+t_plane[1]*rotated_velocity.x;
    zpos=rotated_coordinates.z+t_plane[1]*rotated_velocity.z;

    if ((xpos*xpos + zpos*zpos) > radius_bottom*radius_bottom){
        t_plane[1] = -1;
        *num_solutions = *num_solutions-1;
    } else {
		normal_vector_rotated = coords_set(0,-1,0);
			
	    normal_vector = rot_apply(geometry->rotation_matrix, normal_vector_rotated);
		
	    // Set the normal vector components
	    nx[1] = normal_vector.x;
	    ny[1] = normal_vector.y;
	    nz[1] = normal_vector.z;
		
		surface_index[1] = 2; // bottom index
    }
    
    // sort solutions:
    if (t_plane[0]>t_plane[1]){
       tmp = t_plane[1];
       t_plane[1] = t_plane[0];
       t_plane[0] = tmp;
	   
	   // Also switch the normal vectors
	   tmp = nx[1];
 	   nx[1] = nx[0];
	   nx[0] = tmp;
	
	   tmp = ny[1];
	   ny[1] = ny[0];
	   ny[0] = tmp;
	
	   tmp = nz[1];
	   nz[1] = nz[0];
	   nz[0] = tmp;
	
	   // Switch surface_index
	   int temp_int = surface_index[1];
	   surface_index[1] = surface_index[0];
	   surface_index[0] = temp_int;
    }

    
	double nx_cone[2], ny_cone[2], nz_cone[2];
	int surface_index_cone[2];
	double r_current;
	double x, y, z, dt;
	
    if (*num_solutions == 2){
        // Intersect only on planes
        t[0] = t_plane[0];
        t[1] = t_plane[1];
        
    } else {
        // Intersects with cone
            // Intersection with cone:
            // solve the equation: t*A+sqrt(t*B)+C = 0
        tmp = (rotated_velocity.y*radius_top/height-rotated_velocity.y*radius_bottom/height);
        double A = rotated_velocity.x*rotated_velocity.x+rotated_velocity.z*rotated_velocity.z-tmp*tmp;
        double B = 2*rotated_velocity.x*rotated_coordinates.x+2*rotated_velocity.z*rotated_coordinates.z-(2*(rotated_velocity.y*radius_top/height-rotated_velocity.y*radius_bottom/height))*((0.5)*radius_bottom+rotated_coordinates.y*radius_top/height-rotated_coordinates.y*radius_bottom/height+radius_top/2);
        tmp = (radius_bottom/2+rotated_coordinates.y*radius_top/height-rotated_coordinates.y*radius_bottom/height+radius_top/2);
        double C = rotated_coordinates.x*rotated_coordinates.x+rotated_coordinates.z*rotated_coordinates.z-tmp*tmp;
        double t_cone[2];
               t_cone[1]= -(B+sqrt(-4*A*C+B*B))/(2*A);
            t_cone[0]= -(B-sqrt(-4*A*C+B*B))/(2*A);
        //solve_2nd_order(&t_cone[0], &t_cone[1], A, B, C);

        // remove solutions on cone over top and under bottom
        if (fabs(t_cone[0]*rotated_velocity.y+rotated_coordinates.y) > height/2) {
            t_cone[0] = -1;
        } else {
	        dt = t_cone[0];
        
	        // Intersection point in cylinder coordinate system
	        x = rotated_coordinates.x + dt*rotated_velocity.x;
	        y = rotated_coordinates.y + dt*rotated_velocity.y;
	        z = rotated_coordinates.z + dt*rotated_velocity.z;
			
			r_current = radius_top + ((y-0.5*height)/height)*(radius_top - radius_bottom);
				
			
			if (radius_bottom==radius_top) {
 			    normal_vector_rotated = coords_set(x/r_current, 0.0, z/r_current);
			} else {
			    normal_vector_rotated = coords_set(x/r_current, (radius_bottom - radius_top)/height, z/r_current);
			}

			NORM(normal_vector_rotated.x, normal_vector_rotated.y, normal_vector_rotated.z);
			
	        // Rotate back to master coordinate system
	        normal_vector = rot_apply(geometry->rotation_matrix, normal_vector_rotated);
		
	        // Set the normal vector components
	        nx_cone[0] = normal_vector.x;
	        ny_cone[0] = normal_vector.y;
	        nz_cone[0] = normal_vector.z;
			
			surface_index_cone[0] = 0;
			
        }
        if (fabs(t_cone[1]*rotated_velocity.y+rotated_coordinates.y) > height/2) {
            t_cone[1] = -1;
		} else { 
			
		    dt = t_cone[1];
			
	        // Intersection point in cylinder coordinate system
	        x = rotated_coordinates.x + dt*rotated_velocity.x;
	        y = rotated_coordinates.y + dt*rotated_velocity.y;
	        z = rotated_coordinates.z + dt*rotated_velocity.z;
			
			r_current = radius_top + ((y-0.5*height)/height)*(radius_top - radius_bottom);
				
			
			if (radius_bottom==radius_top) {
 			    normal_vector_rotated = coords_set(x/r_current, 0.0, z/r_current);
			} else {
			    normal_vector_rotated = coords_set(x/r_current, (radius_bottom - radius_top)/height, z/r_current);
			}

			NORM(normal_vector_rotated.x, normal_vector_rotated.y, normal_vector_rotated.z);
			
	        // Rotate back to master coordinate system
	        normal_vector = rot_apply(geometry->rotation_matrix, normal_vector_rotated);
		
	        // Set the normal vector components
	        nx_cone[1] = normal_vector.x;
	        ny_cone[1] = normal_vector.y;
	        nz_cone[1] = normal_vector.z;
			
			surface_index_cone[1] = 0;			
        }

        
        // sort solutions:
        if (t_cone[0]>t_cone[1]){
            tmp = t_cone[1];
            t_cone[1] = t_cone[0];
            t_cone[0] = tmp;
			
	 	   // Also switch the normal vectors
	 	   tmp = nx_cone[1];
	  	   nx_cone[1] = nx_cone[0];
	 	   nx_cone[0] = tmp;
	
	 	   tmp = ny_cone[1];
	 	   ny_cone[1] = ny_cone[0];
	 	   ny_cone[0] = tmp;
	
	 	   tmp = nz_cone[1];
	 	   nz_cone[1] = nz_cone[0];
	 	   nz_cone[0] = tmp;
	
	 	   // Switch surface_index
	 	   int temp_int = surface_index_cone[1];
	 	   surface_index_cone[1] = surface_index_cone[0];
	 	   surface_index_cone[0] = temp_int;
        }
        
        if (*num_solutions == 1){
            t[0] = t_cone[1];
			nx[0] = nx_cone[1];
			ny[0] = ny_cone[1];
			nz[0] = nz_cone[1];
			surface_index[0] = surface_index_cone[1];
			
            t[1] = t_plane[1];
        }
        if (*num_solutions == 0){
            t[0] = t_cone[0];
			nx[0] = nx_cone[0];
			ny[0] = ny_cone[0];
			nz[0] = nz_cone[0];
			surface_index[0] = surface_index_cone[0];
			
            t[1] = t_cone[1];
			nx[1] = nx_cone[1];
			ny[1] = ny_cone[1];
			nz[1] = nz_cone[1];
			surface_index[1] = surface_index_cone[1];
        }
    }


    /*
    // Count solutions
    *num_solutions = 0;
    if (t[0] > 0){
        *num_solutions += 1;
    }else {
        t[0]=-1;
    }
    if (t[1] > 0){
        *num_solutions += 1;
    }else {
        t[1]=-1;
    }
	*/
	
	*num_solutions = 2;
    
    
    if (t[0] > t[1]) {
        tmp = t[1];
        t[1] = t[0];
        t[0] = tmp;
		
 	   tmp = nx[1];
  	   nx[1] = nx[0];
 	   nx[0] = tmp;
	
 	   tmp = ny[1];
 	   ny[1] = ny[0];
 	   ny[0] = tmp;
	
 	   tmp = nz[1];
 	   nz[1] = nz[0];
 	   nz[0] = tmp;
	
 	   // Switch surface_index
 	   int temp_int = surface_index[1];
 	   surface_index[1] = surface_index[0];
 	   surface_index[0] = temp_int;
    }
switch(*num_solutions) {
    case 2:
	    //printf("case 2 t[0]=%lf (%lf, %lf, %lf) t[1]=%lf (%lf, %lf, %lf) \n", t[0], nx[0], ny[0], nz[0], t[1], nx[1], ny[1], nz[1]);
        return 1;
    case 1:
        t[0] = -1;
	    //printf("case 1 t[0]=%lf t[1]=%lf \n", t[0], t[1]);		
        return 1;
    case 0:
        t[0] = -1;
        t[1] = -1;
	    //printf("case 0 t[0]=%lf t[1]=%lf \n", t[0], t[1]);		
        return 0;
}

 // FIXME should we ever reach / return here?
 return -2;
};

int cone_intersect(double *t,int *num_solutions,double *r,double *v,struct geometry_struct *geometry){
/*
This function takes the inputs from a neutron and calculates all intersections with the cone geometry.
Output is true or false depending on intersections will occour, and a list of time-stapms of all possible intersections.

Math used here is based on the math found on:
http://lousodrome.net/blog/light/2017/01/03/intersection-of-a-ray-and-a-cone/
But has been modified to sollve the problem within the syntax needed in Union

This function was created by Martin Olsen at NBI on september 20, 2018.
*/


    double radius_top = geometry->geometry_parameters.p_cone_storage->cone_radius_top;
    double radius_bottom = geometry->geometry_parameters.p_cone_storage->cone_radius_bottom;
    double height = geometry->geometry_parameters.p_cone_storage->height;
    Coords direction = geometry->geometry_parameters.p_cone_storage->direction_vector;
    Coords center = geometry->center;
    
    Coords bottom_point = coords_add(center,coords_scalar_mult(direction,-0.5*height));
    Coords top_point = coords_add(center,coords_scalar_mult(direction,0.5*height));

/*
    // check if this is a cylinder
    int isCylinder = 0;
    if (radius_top==radius_bottom){
        isCylinder = 1;
    }


    // Intersection with a cone
    if (isCylinder == 0){
 

    }
    // Intersection with a cylinder
    if (isCylinder == 1){


    }
*/
    // FIXME Is it meaningful that this function is of int type? Anyone requesting output?
    return 0;
};

int r_within_mesh(Coords pos,struct geometry_struct *geometry) {
  //TODO: Make a single intersection algorithm that all the three mesh intersections
  // Can use
// Unpack parameters

    Coords center = geometry->center;
    int n_facets = geometry->geometry_parameters.p_mesh_storage->n_facets;
    double *normal_x = geometry->geometry_parameters.p_mesh_storage->normal_x;
    double *normal_y = geometry->geometry_parameters.p_mesh_storage->normal_y;
    double *normal_z = geometry->geometry_parameters.p_mesh_storage->normal_z;
    double *v1_x = geometry->geometry_parameters.p_mesh_storage->v1_x;
    double *v1_y = geometry->geometry_parameters.p_mesh_storage->v1_y;
    double *v1_z = geometry->geometry_parameters.p_mesh_storage->v1_z;
    double *v2_x = geometry->geometry_parameters.p_mesh_storage->v2_x;
    double *v2_y = geometry->geometry_parameters.p_mesh_storage->v2_y;
    double *v2_z = geometry->geometry_parameters.p_mesh_storage->v2_z;
    double *v3_x = geometry->geometry_parameters.p_mesh_storage->v3_x;
    double *v3_y = geometry->geometry_parameters.p_mesh_storage->v3_y;
    double *v3_z = geometry->geometry_parameters.p_mesh_storage->v3_z;
    
    double x_new,y_new,z_new;
    
    // Coordinate transformation
    x_new = pos.x - geometry->center.x;
    y_new = pos.y - geometry->center.y;
    z_new = pos.z - geometry->center.z;
    
    Coords coordinates = coords_set(x_new,y_new,z_new);
    Coords rotated_coordinates;
   
    rotated_coordinates = rot_apply(geometry->transpose_rotation_matrix,coordinates);

    
    int verbal = 0;
    // Generate unit direction vector along center axis of meshs
    
    // Start with vector that points along the mesh in the simple frame, and rotate to global
    Coords simple_vector = coords_set(0,1,0);
    Coords test_vector = coords_set(0,1,0);
    // Check intersections with every single facet:
    int iter =0;
    int counter=0; int neg_counter=0;
    Coords edge1,edge2,h,s,q,tmp,intersect_pos;
    double UNION_EPSILON = 1e-27;
    double this_facet_t;
    double a,f,u,V;
    //////printf("\n RWITHIN TEST 1ste");
    for (iter = 0 ; iter < n_facets ; iter++){
        // Intersection with face plane (Möller–Trumbore)
        edge1 = coords_set(*(v2_x+iter)-*(v1_x+iter),*(v2_y+iter)-*(v1_y+iter),*(v2_z+iter)-*(v1_z+iter));
        edge2 = coords_set(*(v3_x+iter)-*(v1_x+iter),*(v3_y+iter)-*(v1_y+iter),*(v3_z+iter)-*(v1_z+iter));
        
        vec_prod(h.x,h.y,h.z,test_vector.x,test_vector.y,test_vector.z,edge2.x,edge2.y,edge2.z);
        
        a = Dot(edge1,h);
        if (a > -UNION_EPSILON && a < UNION_EPSILON){
            //////printf("\n UNION_EPSILON fail");
        } else{
            f = 1.0/a;
            s = coords_sub(rotated_coordinates, coords_set(*(v1_x+iter),*(v1_y+iter),*(v1_z+iter)));
            u = f * (Dot(s,h));
            if (u < 0.0 || u > 1.0){
            }else{
                //q = vec_prod(s,edge1);
                vec_prod(q.x,q.y,q.z,s.x,s.y,s.z,edge1.x,edge1.y,edge1.z);
                V = f * Dot(test_vector,q);
                if (V < 0.0 || u + V > 1.0){
                } else {
                    // At this stage we can compute t to find out where the intersection point is on the line.
                    if (f* Dot(q,edge2) > 0){
                        counter++;

                    } else {
                        neg_counter++;

                    }
                    if (fabs(f* Dot(q,edge2)) > UNION_EPSILON){
                    } else { //printf("\n [%f %f %f] Failed due to being close to surface, E = %f",rotated_coordinates.x,rotated_coordinates.y,rotated_coordinates.z,f* Dot(q,edge2));
                     }
                    
                    
                    
                }
            }
        }
    }
    int C1 = counter;
    
    int maxC; int sameNr =0;
    if (counter % 2 == neg_counter % 2){
        maxC = counter;
        sameNr = 1;
    } else {
        //printf("\n not the same intersection numbers (%i , %i)",counter,neg_counter);
        maxC = counter;
        sameNr = 0;
    }

 if (sameNr == 0){
     test_vector = coords_set(0,0,1);
    iter =0;
    counter=0;
    for (iter = 0 ; iter < n_facets ; iter++){
        // Intersection with face plane (Möller–Trumbore)
        edge1 = coords_set(*(v2_x+iter)-*(v1_x+iter),*(v2_y+iter)-*(v1_y+iter),*(v2_z+iter)-*(v1_z+iter));
        edge2 = coords_set(*(v3_x+iter)-*(v1_x+iter),*(v3_y+iter)-*(v1_y+iter),*(v3_z+iter)-*(v1_z+iter));
        
        vec_prod(h.x,h.y,h.z,test_vector.x,test_vector.y,test_vector.z,edge2.x,edge2.y,edge2.z);
        
        a = Dot(edge1,h);
        if (a > -UNION_EPSILON && a < UNION_EPSILON){
            //////printf("\n UNION_EPSILON fail");
        } else{
            f = 1.0/a;
            s = coords_sub(rotated_coordinates , coords_set(*(v1_x+iter),*(v1_y+iter),*(v1_z+iter)));
            u = f * (Dot(s,h));
            if (u < 0.0 || u > 1.0){
            }else{
                //q = vec_prod(s,edge1);
                vec_prod(q.x,q.y,q.z,s.x,s.y,s.z,edge1.x,edge1.y,edge1.z);
                V = f * Dot(test_vector,q);
                if (V < 0.0 || u + V > 1.0){
                } else {
                    // At this stage we can compute t to find out where the intersection point is on the line.

                    if (f* Dot(q,edge2) > 0){
                        counter++;

                    } else {
                        neg_counter++;

                    }
                    if (fabs(f* Dot(q,edge2)) > UNION_EPSILON){
                    } else { printf("\n [%f %f %f] Failed due to being close to surface (2. iteration), E = %f",rotated_coordinates.x,rotated_coordinates.y,rotated_coordinates.z,f* Dot(q,edge2));
                     }
                    
                    
                    
                }
            }
        }
    }
    }
    
    if (counter % 2 == neg_counter % 2){
        maxC = counter;
        sameNr = 1;
    } else {
        printf("\n not the same intersection numbers (%i , %i) second iteration",counter,neg_counter);
        maxC = counter;
        sameNr = 0;
    }

    if (sameNr == 0){
     test_vector = coords_set(1,0,0);
    iter =0;
    counter=0;
    for (iter = 0 ; iter < n_facets ; iter++){
        // Intersection with face plane (Möller–Trumbore)
        edge1 = coords_set(*(v2_x+iter)-*(v1_x+iter),*(v2_y+iter)-*(v1_y+iter),*(v2_z+iter)-*(v1_z+iter));
        edge2 = coords_set(*(v3_x+iter)-*(v1_x+iter),*(v3_y+iter)-*(v1_y+iter),*(v3_z+iter)-*(v1_z+iter));
        
        vec_prod(h.x,h.y,h.z,test_vector.x,test_vector.y,test_vector.z,edge2.x,edge2.y,edge2.z);
        
        a = Dot(edge1,h);
        if (a > -UNION_EPSILON && a < UNION_EPSILON){
            //////printf("\n UNION_EPSILON fail");
        } else{
            f = 1.0/a;
            s = coords_sub(rotated_coordinates , coords_set(*(v1_x+iter),*(v1_y+iter),*(v1_z+iter)));
            u = f * (Dot(s,h));
            if (u < 0.0 || u > 1.0){
                //////printf("\n Nope 1");
            }else{
                //q = vec_prod(s,edge1);
                vec_prod(q.x,q.y,q.z,s.x,s.y,s.z,edge1.x,edge1.y,edge1.z);
                V = f * Dot(test_vector,q);
                if (V < 0.0 || u + V > 1.0){
                } else {
                    // At this stage we can compute t to find out where the intersection point is on the line.

                    if (f* Dot(q,edge2) > 0){
                        counter++;

                    } else {
                        neg_counter++;

                    }
                    if (fabs(f* Dot(q,edge2)) > UNION_EPSILON){
                    } else { printf("\n [%f %f %f] Failed due to being close to surface (3. iteration), E = %f",rotated_coordinates.x,rotated_coordinates.y,rotated_coordinates.z,f* Dot(q,edge2));
                    }
                    
                    
                    
                }
            }
        }
    }

    }
   
    
    if (counter % 2 == neg_counter % 2){
        maxC = counter;
    } else {
        return 0;
    }
    if ( maxC % 2 == 0) {
        return 0;
    }else{
        return 1;
        
    }
    
    return 0;
    };
	

// Type for holding intersection and normal	
typedef struct {
    double t;
    double nx, ny, nz;
    int surface_index;
} Intersection;

// Function to sort intersection structs according to time
int compare_intersections(const void *a, const void *b) {
	const Intersection *ia = a;
	const Intersection *ib = b;
	if (ia->t < ib->t) return -1;
	if (ia->t > ib->t) return 1;
	return 0;
}

int sample_mesh_intersect(double *t,
                          double *nx, double *ny, double*nz,
                          int *surface_index,
                          int *num_solutions,double *r,double *v,
                          struct geometry_struct *geometry) {

    int n_facets = geometry->geometry_parameters.p_mesh_storage->n_facets;
    double *normal_x = geometry->geometry_parameters.p_mesh_storage->normal_x;
    double *normal_y = geometry->geometry_parameters.p_mesh_storage->normal_y;
    double *normal_z = geometry->geometry_parameters.p_mesh_storage->normal_z;
    double *v1_x = geometry->geometry_parameters.p_mesh_storage->v1_x;
    double *v1_y = geometry->geometry_parameters.p_mesh_storage->v1_y;
    double *v1_z = geometry->geometry_parameters.p_mesh_storage->v1_z;
    double *v2_x = geometry->geometry_parameters.p_mesh_storage->v2_x;
    double *v2_y = geometry->geometry_parameters.p_mesh_storage->v2_y;
    double *v2_z= geometry->geometry_parameters.p_mesh_storage->v2_z;
    double *v3_x = geometry->geometry_parameters.p_mesh_storage->v3_x;
    double *v3_y = geometry->geometry_parameters.p_mesh_storage->v3_y;
    double *v3_z = geometry->geometry_parameters.p_mesh_storage->v3_z;
    Coords Bounding_Box_Center = geometry->geometry_parameters.p_mesh_storage->Bounding_Box_Center;
    double Bounding_Box_Radius = geometry->geometry_parameters.p_mesh_storage->Bounding_Box_Radius;
    int i;

    double x_new,y_new,z_new;
    // Coordinate transformation
    x_new = r[0] - geometry->center.x;
    y_new = r[1] - geometry->center.y;
    z_new = r[2] - geometry->center.z;
    
    double x_bb,y_bb,z_bb;
    x_bb = r[0] - Bounding_Box_Center.x - geometry->center.x;
    y_bb = r[1] - Bounding_Box_Center.y - geometry->center.y;
    z_bb = r[2] - Bounding_Box_Center.z - geometry->center.z;
    
    Coords coordinates = coords_set(x_new,y_new,z_new);
    Coords rotated_coordinates;

    // Rotate the position of the neutron around the center of the mesh
    rotated_coordinates = rot_apply(geometry->transpose_rotation_matrix,coordinates);
    
    Coords bounding_box_coordinates = coords_set(x_bb, y_bb, z_bb);
    Coords bounding_box_rotated_coordinates;
    
    bounding_box_rotated_coordinates = rot_apply(geometry->transpose_rotation_matrix,bounding_box_coordinates);
    
    Coords velocity = coords_set(v[0],v[1],v[2]);
    Coords rotated_velocity;
    
    // Rotate the position of the neutron around the center of the mesh
    rotated_velocity = rot_apply(geometry->transpose_rotation_matrix,velocity);
    
    int output = 0;
    double tmpres[2];
    // Test intersection with bounding sphere 
    if ((output = sphere_intersect(&tmpres[0],&tmpres[1],
                                   bounding_box_rotated_coordinates.x,
                                   bounding_box_rotated_coordinates.y,
                                   bounding_box_rotated_coordinates.z,
                                   rotated_velocity.x,
                                   rotated_velocity.y,
                                   rotated_velocity.z,
                                   Bounding_Box_Radius)) == 0) {
        t[0] = -1;
        t[1] = -1;
        *num_solutions = 0;
        return 0;
    }
    // Check intersections with every single facet:
    int iter =0;
    int counter=0;
    Coords edge1,edge2,h,s,q,tmp,intersect_pos;
    double UNION_EPSILON = 0.0000001;
    double this_facet_t;
    double a,f,u,V;
    double *t_intersect=malloc(n_facets*sizeof(double));
    int *facet_index = malloc(n_facets*sizeof(int));
    if (!t_intersect || !facet_index) {
      fprintf(stderr,"Failure allocating list in Union function sample_mesh_intersect - Exit!\n");
      exit(EXIT_FAILURE);
    }
    *num_solutions = 0;
    for (iter = 0 ; iter < n_facets ; iter++){
        // Intersection with face plane (Möller–Trumbore)
        edge1 = coords_set(*(v2_x+iter)-*(v1_x+iter),*(v2_y+iter)-*(v1_y+iter),*(v2_z+iter)-*(v1_z+iter));
        edge2 = coords_set(*(v3_x+iter)-*(v1_x+iter),*(v3_y+iter)-*(v1_y+iter),*(v3_z+iter)-*(v1_z+iter));
        vec_prod(h.x,h.y,h.z,rotated_velocity.x,rotated_velocity.y,rotated_velocity.z,edge2.x,edge2.y,edge2.z);
        a = Dot(edge1,h);
        //if (a > -UNION_EPSILON && a < UNION_EPSILON){
            ////printf("\n UNION_EPSILON fail");
        //}
        f = 1.0/a;
        s = coords_sub(rotated_coordinates, coords_set(*(v1_x+iter),*(v1_y+iter),*(v1_z+iter)));
        u = f * (Dot(s,h));
        if (u < 0.0 || u > 1.0){
        } else {
            //q = vec_prod(s,edge1);
            vec_prod(q.x,q.y,q.z,s.x,s.y,s.z,edge1.x,edge1.y,edge1.z);
            V = f * Dot(rotated_velocity,q);
            if (V < 0.0 || u + V > 1.0){
            } else {
                // At this stage we can compute t to find out where the intersection point is on the line.    
                t_intersect[counter] = f* Dot(q,edge2);
                facet_index[counter] = iter;
                //printf("\nIntersects at time: t= %f\n",t_intersect[counter] );
                counter++;
            }
        }
    }
	
	*num_solutions = counter;
	
	// Early exit if there are not solutions
    if (*num_solutions == 0){
        free(t_intersect);
        free(facet_index);
        return 0;
    }
	
	// Move times and normal's into structs to be sorted
    Intersection *hits = malloc(*num_solutions * sizeof(Intersection));
    if (!hits) {
      fprintf(stderr,"Failure allocating Intersection list struct in Union function sample_mesh_intersect - Exit!\n");
      exit(EXIT_FAILURE);
    }
	    
    for (iter=0; iter < *num_solutions; iter++){
	    hits[iter].t  = t_intersect[iter];;
	    hits[iter].nx = normal_x[facet_index[iter]];
	    hits[iter].ny = normal_y[facet_index[iter]];
	    hits[iter].nz = normal_z[facet_index[iter]];				
	    hits[iter].surface_index = 0;
    }

    // Sort structs according to time
    qsort(hits, *num_solutions, sizeof(Intersection), compare_intersections);
	
	// Place the solutions into the pointers given in the function parameters for return
	for (int i = 0; i < *num_solutions; i++) {
	    t[i]  = hits[i].t;
	    nx[i] = hits[i].nx;
	    ny[i] = hits[i].ny;
	    nz[i] = hits[i].nz;
	    surface_index[i] = hits[i].surface_index;
	}
	
    free(facet_index);
    free(t_intersect);
	free(hits);
    return 1;
};


int r_within_box_advanced(Coords pos,struct geometry_struct *geometry) {
    // Unpack parameters
    double width1 = geometry->geometry_parameters.p_box_storage->x_width1;
    double height1 = geometry->geometry_parameters.p_box_storage->y_height1;
    double width2 = geometry->geometry_parameters.p_box_storage->x_width2;
    double height2 = geometry->geometry_parameters.p_box_storage->y_height2;
    double depth = geometry->geometry_parameters.p_box_storage->z_depth;
    
    // Transform to the center
    Coords coordinates = coords_sub(pos,geometry->center);
    
    Coords rotated_coordinates;
    
    // Rotate the position of the neutron around the center of the cylinder
    rotated_coordinates = rot_apply(geometry->transpose_rotation_matrix,coordinates);
    
    if (rotated_coordinates.z < -0.5*depth || rotated_coordinates.z > 0.5*depth) return 0;
    
    double depth_ratio = ((rotated_coordinates.z+0.5*depth)/depth);
    double width_at_depth = width1 + (width2-width1)*depth_ratio;
    if (rotated_coordinates.x < -0.5*width_at_depth || rotated_coordinates.x > 0.5*width_at_depth) return 0;
    
    double height_at_depth = height1 + (height2-height1)*depth_ratio;
    if (rotated_coordinates.y < -0.5*height_at_depth || rotated_coordinates.y > 0.5*height_at_depth) return 0;
    
    return 1;
};

// -------------    Functions for box ray tracing used in initialize ----------------------------
// These functions does not need to be fast, as they are only used once
int box_within_box(struct geometry_struct *geometry_child,struct geometry_struct *geometry_parent) {
    // Is geometry child inside geometry parent?
    // For box child to be inside of box parent, all corners of box child must be inside of box parent.
    
    // Generate coordinates of corners of box2
    Coords corner_points[8];
    box_corners_global_frame(corner_points,geometry_child);
    
    // Check earch corner seperatly
    int iterate;
    for (iterate=0;iterate<8;iterate++) {
        if (geometry_parent->within_function(corner_points[iterate],geometry_parent) == 0) {
            return 0; // If a corner is outside, box 2 is not within box 1
        }
    }
    return 1; // If no corner was outside, box 2 is inside box 1
};

int existence_of_intersection(Coords point1, Coords point2, struct geometry_struct *geometry) {
	// Checks if a line segment between point1 and point2 intersects the given geometry
	
    Coords vector_between = coords_sub(point2,point1);
    double start_point[3],vector_between_v[3];
    double temp_solution[2];
    int number_of_solutions;

    start_point[0] = point1.x;start_point[1] = point1.y;start_point[2] = point1.z;
    vector_between_v[0] = vector_between.x;vector_between_v[1] = vector_between.y;vector_between_v[2] = vector_between.z;
	
	// todo: Switch to nicer intersect call
	double dummy_double[2];
	int dummy_int[2];
	
    //printf("\nChecking the existence of intersections");	
    geometry->intersect_function(temp_solution, dummy_double, dummy_double, dummy_double, dummy_int, &number_of_solutions, start_point, vector_between_v, geometry);
    if (number_of_solutions > 0) {
      //printf("\nMore than 1 solution has been found");
        if (temp_solution[0] > 0 && temp_solution[0] < 1) return 1;
        if (number_of_solutions == 2) {
            if (temp_solution[1] > 0 && temp_solution[1] < 1) return 1;
        }
    }
    return 0;
};

int box_overlaps_box(struct geometry_struct *geometry1,struct geometry_struct *geometry2) {
    // Algorithm for checking if two boxes overlap
    
    // First check if one is inside the other by checking corners and within the other
    
    // Generate coordinates of corners of box1
    Coords corner_points1[8];
    box_corners_global_frame(corner_points1,geometry1);
    
    // Check earch corner seperatly
    int iterate;
    for (iterate=0;iterate<8;iterate++) {
        if (geometry2->within_function(corner_points1[iterate],geometry2) == 1) {
            return 1; // If a corner of box 1 is inside box 2, the two boxes overlaps
        }
    }
    
    Coords corner_points2[8];
    box_corners_global_frame(corner_points2,geometry2);
    for (iterate=0;iterate<8;iterate++) {
        if (geometry1->within_function(corner_points2[iterate],geometry1) == 1) {
            return 1; // If a corner of box 2 is inside box 1, the two boxes overlaps
        }
    }
    
    // Check intersections for the lines between the corners of box1 and the box2 geometry
    // 12 sides to a box, if any one of them intersects, the volumes overlaps
    for (iterate=0;iterate<3;iterate++) { //
        if (existence_of_intersection(corner_points1[iterate],corner_points1[iterate+1],geometry2) == 1) return 1;
    }
    if (existence_of_intersection(corner_points1[3],corner_points1[0],geometry2) == 1) return 1;
    for (iterate=4;iterate<7;iterate++) {
        if (existence_of_intersection(corner_points1[iterate],corner_points1[iterate+1],geometry2) == 1) return 1;
    }
    if (existence_of_intersection(corner_points1[7],corner_points1[4],geometry2) == 1) return 1;
    for (iterate=0;iterate<4;iterate++) {
        if (existence_of_intersection(corner_points1[iterate],corner_points1[iterate+4],geometry2) == 1) return 1;
    }
    
    // Check intersections for the lines between the corners of box2 and the box1 geometry
    // 12 sides to a box, if any one of them intersects, the volumes overlaps
    for (iterate=0;iterate<3;iterate++) { //
        if (existence_of_intersection(corner_points2[iterate],corner_points2[iterate+1],geometry1) == 1) return 1;
    }
    if (existence_of_intersection(corner_points2[3],corner_points2[0],geometry1) == 1) return 1;
    for (iterate=4;iterate<7;iterate++) {
        if (existence_of_intersection(corner_points2[iterate],corner_points2[iterate+1],geometry1) == 1) return 1;
    }
    if (existence_of_intersection(corner_points2[7],corner_points2[4],geometry1) == 1) return 1;
    for (iterate=0;iterate<4;iterate++) {
        if (existence_of_intersection(corner_points2[iterate],corner_points2[iterate+4],geometry1) == 1) return 1;
    }
    
    // If none of the boxes corners are inside the other box, and none of the sides of the boxes intersect the other box, they do not overlap.
    return 0;
};


// -------------    Functions for sphere ray tracing used in trace ------------------------------
// These functions needs to be fast, as they may be used many times for each ray
int sample_sphere_intersect(double *t, double *nx, double *ny, double *nz, int *surface_index, int *num_solutions,double *r,double *v,struct geometry_struct *geometry) {
    double radius = geometry->geometry_parameters.p_sphere_storage->sph_radius;
    
    // Declare variables for the function
    double x_new,y_new,z_new;
    
    // Coordinate transformation
    x_new = r[0] - geometry->center.x;
    y_new = r[1] - geometry->center.y;
    z_new = r[2] - geometry->center.z;
    
    int output = 0;
    // Run McStas built in sphere intersect funtion (sphere centered around origin)
    if ((output = sphere_intersect(&t[0],&t[1],x_new,y_new,z_new,v[0],v[1],v[2],radius)) == 0) {
        *num_solutions = 0;t[0]=-1;t[1]=-1;}
    else if (t[1] != 0) *num_solutions = 2;
    else {*num_solutions = 1;t[1]=-1;}
	
	// Calculate normals
	int iterator;
	double x_intersect, y_intersect, z_intersect; // relative to sphere center
	Coords coordinates;
	Coords rotated_coordinates;	
	
	for (iterator=0;iterator<*num_solutions;iterator++) {
		x_intersect = t[iterator]*v[0] + x_new;
		y_intersect = t[iterator]*v[1] + y_new;
		z_intersect = t[iterator]*v[2] + z_new;
		
	    coordinates = coords_set(x_intersect,y_intersect,z_intersect);
		NORM(coordinates.x, coordinates.y, coordinates.z);
		nx[iterator] = coordinates.x;
		ny[iterator] = coordinates.y;
		nz[iterator] = coordinates.z;				
		surface_index[iterator] = 0;
		
		/*
		// Since the ray was never rotated into the sphere coordinate system (due to symmetry)
		//  rotating back is not necessary
			
	    // printf("Cords coordinates = (%f,%f,%f)\n",coordinates.x,coordinates.y,coordinates.z);
    
	    // debug
	    // Rotation rotation_matrix_debug[3][3];
	    // rot_set_rotation(rotation_matrix_debug,-1.0*geometry->rotation.x,-1.0*geometry->rotation.y,-1.0*geometry->rotation.z);
	    // rot_transpose(geometry->rotation_matrix,rotation_matrix_debug);

	    // Rotate the position of the neutron around the center of the cylinder
	    rotated_coordinates = rot_apply(geometry->transpose_rotation_matrix,coordinates);
		*/
			
	}
    
    return output;
};

int r_within_sphere(Coords pos,struct geometry_struct *geometry)
    {
    // Unpack parameters
    double radius = geometry->geometry_parameters.p_sphere_storage->sph_radius;
    
    // Calculate the distance between the center and the sphere, and the current position
    double distance = distance_between(pos,geometry->center);

    //printf("distance = %f\n",distance);
    //printf("radius   = %f\n",radius);
    //printf("current_position.x = %f,current_position.y = %f,current_position.z = %f\n",current_position.x,current_position.y,current_position.z);
    //printf("geometry.x = %f,geometry.y = %f,geometry.z = %f\n",geometry->center.x,geometry->center.y,geometry->center.z);
    //printf("return   = %d\n",(distance <= radius));
    
    return (distance < radius);
    };

// -------------    Functions for sphere ray tracing used in initialize -------------------------
// These functions does not need to be fast, as they are only used once
int sphere_overlaps_sphere(struct geometry_struct *geometry1,struct geometry_struct *geometry2) {
    // Unpack parameters
    double radius1 = geometry1->geometry_parameters.p_sphere_storage->sph_radius;
    double radius2 = geometry2->geometry_parameters.p_sphere_storage->sph_radius;

    // Calculate distance
    double distance = distance_between(geometry1->center,geometry2->center);
    
    // Return 0 if the spheres does not overlap, 1 if they do.
    // printf("Output from sphere_overlaps_sphere = %d \n",(distance <= (radius1 + radius2)));
    return (distance <= (radius1 + radius2));
};

int sphere_within_sphere(struct geometry_struct *geometry_child,struct geometry_struct *geometry_parent) {
    // Unpack parameters
    double radius_child = geometry_child->geometry_parameters.p_sphere_storage->sph_radius;
    double radius_parent = geometry_parent->geometry_parameters.p_sphere_storage->sph_radius;

    // Calculate distance
    double distance = distance_between(geometry_child->center,geometry_parent->center);
    
    // Return 1 if sphere child is within sphere parent, 0 if they do not.
    return (distance + radius_child <= radius_parent);
};

// -------------    Functions for cylinder ray tracing used in trace ------------------------------
// These functions needs to be fast, as they may be used many times for each ray
int sample_cylinder_intersect(double *t, double *nx, double *ny, double *nz, int *surface_index, int *num_solutions,double *r,double *v,struct geometry_struct *geometry) {
    double radius = geometry->geometry_parameters.p_cylinder_storage->cyl_radius;
    double height = geometry->geometry_parameters.p_cylinder_storage->height;
    
    // Declare position variables for the local coordinate system
    double x_new,y_new,z_new;
    
    // Coordinate transformation
    x_new = r[0] - geometry->center.x;
    y_new = r[1] - geometry->center.y;
    z_new = r[2] - geometry->center.z;
    
    Coords coordinates = coords_set(x_new,y_new,z_new);
    Coords rotated_coordinates;

    // Rotate the position of the neutron around the center of the cylinder
    rotated_coordinates = rot_apply(geometry->transpose_rotation_matrix,coordinates);
    
    Coords velocity = coords_set(v[0],v[1],v[2]);
    Coords rotated_velocity;
    
    // Rotate the position of the neutron around the center of the cylinder
    rotated_velocity = rot_apply(geometry->transpose_rotation_matrix,velocity);
    
	int output = 0;    
	// Cases where the velocity is parallel with the cylinder axis have given problems, and is checked for explicitly
	if (sqrt(rotated_velocity.x*rotated_velocity.x+rotated_velocity.z*rotated_velocity.z)/fabs(rotated_velocity.y) < 0.00001) {
	  // The velocity is parallel with the cylinder axis. Either there are no solutions or two solutions
	  if (sqrt(rotated_coordinates.x*rotated_coordinates.x+rotated_coordinates.z*rotated_coordinates.z) > radius) {
	    *num_solutions = 0;
	    return 0;
	  } else {
	    *num_solutions = 2;
	    t[0] = (0.5*height - rotated_coordinates.y)/rotated_velocity.y;
		surface_index[0] = 1; // index indicating top
		
	    t[1] = (-0.5*height - rotated_coordinates.y)/rotated_velocity.y;
		surface_index[1] = 2; // index indicating bottom
		
		// sort solutions
		if (t[0] > t[1]) {
		  double d_temp;
		  
		  d_temp = t[0];
		  t[0] = t[1];
		  t[1] = d_temp;
		  
		  int i_temp;
		  
		  i_temp = surface_index[0];
		  surface_index[0] = surface_index[1];
		  surface_index[1] = i_temp;
		}
	  }
	} else {
	  // velocity not parallel to cylinder axis, call standard mcstas cylinder intersect 

	  // Run McStas built in sphere intersect funtion (sphere centered around origin)
	  if ((output = cylinder_intersect(&t[0],&t[1],
	                                   rotated_coordinates.x,rotated_coordinates.y,rotated_coordinates.z,
	                                   rotated_velocity.x,rotated_velocity.y,rotated_velocity.z,radius,height)) == 0) {
	      *num_solutions = 0;t[0]=-1;t[1]=-1;
	  }
	  else if (t[1] != 0) *num_solutions = 2;
	  else {*num_solutions = 1; t[1]=-1;}
	
	  // decode output value
	  // Check the bitmask for entry and exit
	  if (*num_solutions > 0) {
	    int entry_index = 0;
	    if (output & 2) entry_index = 1; // Entry intersects top cap
	    if (output & 4) entry_index = 2; // Entry intersects bottom cap
	    surface_index[0] = entry_index;
	  }
	
	  if (*num_solutions > 1) {
	    int exit_index = 0;		
	    if (output & 8) exit_index = 1;  // Exit intersects top cap
	    if (output & 16) exit_index = 2; // Exit intersects bottom cap
	    surface_index[1] = exit_index;
	  }
	}
    
	
	// Calculate normal vectors from surface index and cylinder geometry
    int index;
    double x, y, z, dt;
    Coords normal_vector_rotated;
    Coords normal_vector;
	
    for (index=0; index<*num_solutions; index++) {
		
		// top and bottom easy
		if (surface_index[index] == 1) {
			normal_vector_rotated = coords_set(0,1,0);
			
		} else if (surface_index[index] == 2) {
			normal_vector_rotated = coords_set(0,-1,0);			
		} else {
	        dt = t[index];
        
	        // Intersection point in cylinder coordinate system
	        x = rotated_coordinates.x + dt*rotated_velocity.x;
	        y = rotated_coordinates.y + dt*rotated_velocity.y;
	        z = rotated_coordinates.z + dt*rotated_velocity.z;
			
			normal_vector_rotated = coords_set(x,0,z);			
			NORM(normal_vector_rotated.x, normal_vector_rotated.y, normal_vector_rotated.z);
		}
		
        // Rotate back to master coordinate system
        normal_vector = rot_apply(geometry->rotation_matrix, normal_vector_rotated);
		
        // Set the normal vector components
        nx[index] = normal_vector.x;
        ny[index] = normal_vector.y;
        nz[index] = normal_vector.z;
    }
    
    return output;
};

int r_within_cylinder(Coords pos,struct geometry_struct *geometry) {
	// Unpack parameters
    double radius = geometry->geometry_parameters.p_cylinder_storage->cyl_radius;
    double height = geometry->geometry_parameters.p_cylinder_storage->height;

    
    int verbal = 0;
    // Generate unit direction vector along center axis of cylinders
    
    // Start with vector that points along the cylinder in the simple frame, and rotate to global
    Coords simple_vector = coords_set(0,1,0);
    Coords vector1;
    if (verbal == 1) printf("Cords start_vector = (%f,%f,%f)\n",simple_vector.x,simple_vector.y,simple_vector.z);

    // Rotate the position of the neutron around the center of the cylinder
    vector1 = rot_apply(geometry->rotation_matrix,simple_vector);
    if (verbal == 1) printf("Cords vector1 = (%f,%f,%f)\n",vector1.x,vector1.y,vector1.z);
    
    // The cylinders are parallel.
    int seperated = 0;
    //double delta[3];
    //delta[0] = geometry->center.x - r[0];
    //delta[1] = geometry->center.y - r[1];
    //delta[2] = geometry->center.z - r[2];
    
    Coords delta = coords_sub(geometry->center,pos);

    // Test for separation by height
    // if (h0Div2 + h1Div2 − |Dot(W0, Delta )| < 0) seperated = 1;
    
    if (verbal == 1) printf("vector1 = (%f,%f,%f)\n",vector1.x,vector1.y,vector1.z);
    if (verbal == 1) printf("delta1 = (%f,%f,%f)\n",delta.x,delta.y,delta.z);
    double scalar_prod1 = scalar_prod(vector1.x,vector1.y,vector1.z,delta.x,delta.y,delta.z);
    if (verbal == 1) printf("scalar product = %f \n",scalar_prod1);
    if (verbal == 1) printf("height 1 = %f \n",height);
    
    int inside = 1;
    
    if (height*0.5 < fabs(scalar_prod1)) {
            if (verbal == 1) printf("point sticks out height wise \n");
            inside = 0;
    }

    // Test for separation radially
    // if (rSum − |Delta − Dot(W0,Delta)∗W0| < 0) seperated = 1;
    double vector_between_cyl_axis[3];
    vector_between_cyl_axis[0] = delta.x - scalar_prod1*vector1.x;
    vector_between_cyl_axis[1] = delta.y - scalar_prod1*vector1.y;
    vector_between_cyl_axis[2] = delta.z - scalar_prod1*vector1.z;
    if (verbal == 1) printf("vector_between = (%f,%f,%f)\n",vector_between_cyl_axis[0],vector_between_cyl_axis[1],vector_between_cyl_axis[2]);
    if (verbal == 1) printf("length of vector between = %f\n",length_of_3vector(vector_between_cyl_axis));
    
    if (radius < length_of_3vector(vector_between_cyl_axis)) {
            if (verbal == 1) printf("Point sticks out radially \n");
            inside = 0;
    }
    
    if (inside == 0) return 0;
    else return 1;
    };

// -------------    Functions for cylinder ray tracing used in initialize -------------------------
// These functions does not need to be fast, as they are only used once
int cylinder_overlaps_cylinder(struct geometry_struct *geometry1,struct geometry_struct *geometry2) {
    // Unpack parameters
    double radius1 = geometry1->geometry_parameters.p_cylinder_storage->cyl_radius;
    double height1 = geometry1->geometry_parameters.p_cylinder_storage->height;
    double radius2 = geometry2->geometry_parameters.p_cylinder_storage->cyl_radius;
    double height2 = geometry2->geometry_parameters.p_cylinder_storage->height;
    
    int verbal = 0;
    // Generate unit direction vector along center axis of cylinders
    
    // Start with vector that points along the cylinder in the simple frame, and rotate to global
    Coords simple_vector = coords_set(0,1,0);
    Coords vector1,vector2;
    if (verbal == 1) printf("Cords start_vector = (%f,%f,%f)\n",simple_vector.x,simple_vector.y,simple_vector.z);

    // Rotate the position of the neutron around the center of the cylinder
    vector1 = rot_apply(geometry1->rotation_matrix,simple_vector);
    if (verbal == 1) printf("Cords vector1 = (%f,%f,%f)\n",vector1.x,vector1.y,vector1.z);
    // Rotate the position of the neutron around the center of the cylinder
    vector2 = rot_apply(geometry2->rotation_matrix,simple_vector);
    if (verbal == 1) printf("Cords vector2 = (%f,%f,%f)\n",vector2.x,vector2.y,vector2.z);
    
    // if vector1 and vector2 are parallel, the problem is simple, but if not complicated
    double cross_product1[3] = {0,0,0};
    // printf("%f\n",cross_product1[0]);
    // vec prod(&ax,&ay,&az,bx,by,bz, cx,cy,cz)
    vec_prod(cross_product1[0],cross_product1[1],cross_product1[2],vector1.x,vector1.y,vector1.z,vector2.x,vector2.y,vector2.z);
    // I get an error taking the adress of cross_product1[0], &cross_product1[0]. Took the pointer adresses instead. Works fine.
    if (verbal == 1) printf("cross_product = (%f,%f,%f)\n",cross_product1[0],cross_product1[1],cross_product1[2]);
    double cross_product_length = length_of_3vector(cross_product1);
    
    if (cross_product_length == 0) {
        // The cylinders are parallel.
        int seperated = 0;
        double delta[3];
        delta[0] = geometry1->center.x - geometry2->center.x;
        delta[1] = geometry1->center.y - geometry2->center.y;
        delta[2] = geometry1->center.z - geometry2->center.z;

        // Test for separation by height
        // if (h0Div2 + h1Div2 − |Dot(W0, Delta )| < 0) seperated = 1;
        
        if (verbal == 1) printf("vector1 = (%f,%f,%f)\n",vector1.x,vector1.y,vector2.z);
        if (verbal == 1) printf("delta1 = (%f,%f,%f)\n",delta[0],delta[1],delta[2]);
        double scalar_prod1 = scalar_prod(vector1.x,vector1.y,vector1.z,delta[0],delta[1],delta[2]);
        if (verbal == 1) printf("scalar product = %f \n",scalar_prod1);
        if (verbal == 1) printf("height 1 = %f, height 2 = %f \n",height1,height2);
        if (height1*0.5 + height2*0.5 - fabs(scalar_prod1) < 0) {
                if (verbal == 1) printf("seperated by height \n");
                return 0;
                }
    
        // Test for separation radially
        // if (rSum − |Delta − Dot(W0,Delta)∗W0| < 0) seperated = 1;
        double vector_between_cyl_axis[3];
        vector_between_cyl_axis[0] = delta[0] - scalar_prod1*vector1.x;
        vector_between_cyl_axis[1] = delta[1] - scalar_prod1*vector1.y;
        vector_between_cyl_axis[2] = delta[2] - scalar_prod1*vector1.z;
        if (verbal == 1) printf("vector_between = (%f,%f,%f)\n",vector_between_cyl_axis[0],vector_between_cyl_axis[1],vector_between_cyl_axis[2]);
        if (verbal == 1) printf("length of vector between = %f\n",length_of_3vector(vector_between_cyl_axis));
        
        if (radius1+radius2 - length_of_3vector(vector_between_cyl_axis) < 0) {
                if (verbal == 1) printf("seperated radially \n");
                return 0;
                }
    
        if (verbal == 1) printf("cylinders not seperated\n");
        return 1;
        
    } else {
    
        // Todo: Speed up analysis by starting with a bounding sphere approach to avoid brute force in many cases
        
        
        // printf("The component uses a raytracing method for non parallel cylinders.\n");
        // printf("  Make sure not to give this algorithm edge cases, where cylinders just touch.\n");
        Coords cyl_direction1 = geometry1->geometry_parameters.p_cylinder_storage->direction_vector;
        
        // Doing a simple but not perfect overlap test

        // Checking cylinder sides.
        
        // Taking cylinder 1, making a vector at the base center.
        Coords base_point;
        
        base_point.x = geometry1->center.x - 0.5*height1*cyl_direction1.x;
        base_point.y = geometry1->center.y - 0.5*height1*cyl_direction1.y;
        base_point.z = geometry1->center.z - 0.5*height1*cyl_direction1.z;
        
        // Making a point at the circumference of the bottom circle of the cylinder
        double cross_input[3] = {0,1,0};
        // In case the cross input is parallel with the vector, a new is chosen. Both can't be parallel.
        if (scalar_prod(cross_input[0],cross_input[1],cross_input[2],cyl_direction1.x,cyl_direction1.y,cyl_direction1.z) > 0.99) {
            cross_input[0] = 1; cross_input[1] = 0; cross_input[2] = 0;
        }
        // print_position(make_position(cross_input),"cross input");
        
        double cross_product1[3] = {0,0,0};
        vec_prod(cross_product1[0],cross_product1[1],cross_product1[2],cyl_direction1.x,cyl_direction1.y,cyl_direction1.z,cross_input[0],cross_input[1],cross_input[2]);
        
        // print_position(make_position(cross_product1),"cross_product1");
        double cross_length = length_of_3vector(cross_product1);
        
        // printf("cross_length = %f \n",cross_length);
        cross_product1[0] /= cross_length;
        cross_product1[1] /= cross_length;
        cross_product1[2] /= cross_length;
        
        cross_product1[0] *= radius1;
        cross_product1[1] *= radius1;
        cross_product1[2] *= radius1;
        
        Coords circ_point;
        double radial_position[3],cyl_direction_pointer[3],base_point_vector[3],cyl_radial_direction[3];
        
        cyl_direction_pointer[0] = cyl_direction1.x;
        cyl_direction_pointer[1] = cyl_direction1.y;
        cyl_direction_pointer[2] = cyl_direction1.z;
        
        int iterate,number_of_solutions,solutions,number_of_positions = 300;
        double rotate_angle,temp_solution[2];
        
        // printf("length of cyl_direction_pointer = %f \n",length_of_3vector(cyl_direction_pointer));
        
        // Check intersection with cylinder 2 with that point, and cyl_direction, if there is an intersection before height1, they overlap.
        // Rotate the circumference point around the cyl_direction for a full circle to detect intersections all the way around.
        
        // Here cross_product1 is a vector from the base point to a point n the circumference
        // circ_point is a vector from the base point to the circumference rotated an angle.
        // radial_position is the actual position on the circumference on the cylinder as a vector from origo.
        for (iterate = 0;iterate < number_of_positions;iterate++) {
                rotate_angle = 2*3.14159*((double) iterate)/((double) number_of_positions);
                rotate(circ_point.x,circ_point.y,circ_point.z,cross_product1[0],cross_product1[1],cross_product1[2],rotate_angle,cyl_direction1.x,cyl_direction1.y,cyl_direction1.z);
            
                radial_position[0] = base_point.x + circ_point.x;
                radial_position[1] = base_point.y + circ_point.y;
                radial_position[2] = base_point.z + circ_point.z;
                // sample_cylinder_intersect(double *t,int *num_solutions,double *r,double *v,struct geometry_struct *geometry) {
				double nx_dummy[2], ny_dummy[2], nz_dummy[2];
				int surface_index_dummy[2];
                sample_cylinder_intersect(temp_solution, nx_dummy, ny_dummy, nz_dummy, surface_index_dummy,
				                          &number_of_solutions,radial_position,cyl_direction_pointer,geometry2);
                for (solutions = 0;solutions < number_of_solutions;solutions++) {
                    if (temp_solution[solutions] > 0 && temp_solution[solutions] < height1) {
                        // cylinders must overlap.
                        return 1;
                    }
                }
                if (number_of_solutions == 2) {
                    if (temp_solution[0] < 0 && temp_solution[1] > 0) return 1; // cylinder 1 inside cylinder 2
                    if (temp_solution[0] > 0 && temp_solution[1] < 0) return 1; // cylinder 1 inside cylinder 2
                }
            
                cyl_radial_direction[0] = circ_point.x;
                cyl_radial_direction[1] = circ_point.y;
                cyl_radial_direction[2] = circ_point.z;
                // Note it has length radius1
            
                base_point_vector[0] = base_point.x;
                base_point_vector[1] = base_point.y;
                base_point_vector[2] = base_point.z;
            
                // The vector circ_point is from the base to the circumference. This is used to check the bottom cap.
                sample_cylinder_intersect(temp_solution,nx_dummy, ny_dummy, nz_dummy, surface_index_dummy, 
				                          &number_of_solutions,base_point_vector,cyl_radial_direction,geometry2);
                for (solutions = 0;solutions < number_of_solutions;solutions++) {
                    if (temp_solution[solutions] > 0 && temp_solution[solutions] < 1) {
                        // cylinders must overlap.
                        return 1;
                    }
                }
                if (number_of_solutions == 2) {
                    if (temp_solution[0] < 0 && temp_solution[1] > 0) return 1; // cylinder 1 inside cylinder 2
                    if (temp_solution[0] > 0 && temp_solution[1] < 0) return 1; // cylinder 1 inside cylinder 2
                }
            
                // Now check the top
                base_point_vector[0] = base_point.x + height1*cyl_direction1.x;
                base_point_vector[1] = base_point.y + height1*cyl_direction1.y;
                base_point_vector[2] = base_point.z + height1*cyl_direction1.z;
            
                // The vector circ_point is from the base to the circumference. This is used to check the bottom cap.
                sample_cylinder_intersect(temp_solution, nx_dummy, ny_dummy, nz_dummy, surface_index_dummy,
				                          &number_of_solutions,base_point_vector,cyl_radial_direction,geometry2);
                for (solutions = 0;solutions < number_of_solutions;solutions++) {
                    if (temp_solution[solutions] > 0 && temp_solution[solutions] < 1) {
                        // cylinders must overlap.
                        return 1;
                    }
                }
                if (number_of_solutions == 2) {
                    if (temp_solution[0] < 0 && temp_solution[1] > 0) return 1; // cylinder 1 inside cylinder 2
                    if (temp_solution[0] > 0 && temp_solution[1] < 0) return 1; // cylinder 1 inside cylinder 2
                }
        }
        // The above method is not perfect as it basicly tests a mesh grid of the cylinder aginst another perfect cylinder.
        // Can be improved.
        
        // If the entire perfect cylinder (cylinder 2) is within the meshgrid one, there will not be a solution to anything.
        // Check with a simple call to r_within_cylinder
        
        // r_within_cylinder(double *r,struct geometry_struct *geometry) {
        
        // if the center of cylinder 2 is within cylinder 1;
        
        if (r_within_cylinder(geometry2->center,geometry1)) return 1;  // if cylinder 2 is within cylinder 1, they clearly overlap.
        
        return 0;
    }

};

int cylinder_within_cylinder(struct geometry_struct *geometry_child,struct geometry_struct *geometry_parent) {
    // Unpack parameters
    double radius1 = geometry_parent->geometry_parameters.p_cylinder_storage->cyl_radius;
    double height1 = geometry_parent->geometry_parameters.p_cylinder_storage->height;
    double radius2 = geometry_child->geometry_parameters.p_cylinder_storage->cyl_radius;
    double height2 = geometry_child->geometry_parameters.p_cylinder_storage->height;
    
    int verbal = 0;
    // Generate unit direction vector along center axis of cylinders
    
    // Start with vector that points along the cylinder in the simple frame, and rotate to global
    Coords simple_vector = coords_set(0,1,0);
    Coords vector1,vector2;
    if (verbal == 1) printf("Cords start_vector = (%f,%f,%f)\n",simple_vector.x,simple_vector.y,simple_vector.z);

    // Rotate the position of the ray around the center of the cylinder
    vector1 = rot_apply(geometry_parent->rotation_matrix,simple_vector);
    if (verbal == 1) printf("Cords vector1 = (%f,%f,%f)\n",vector1.x,vector1.y,vector1.z);
    // Rotate the position of the ray around the center of the cylinder
    vector2 = rot_apply(geometry_child->rotation_matrix,simple_vector);
    if (verbal == 1) printf("Cords vector2 = (%f,%f,%f)\n",vector2.x,vector2.y,vector2.z);
    
    // if vector1 and vector2 are parallel, the problem is simple, but if not complicated
    double cross_product1[3] = {0,0,0};
    // printf("%f\n",cross_product1[0]);
    // vec prod(&ax,&ay,&az,bx,by,bz, cx,cy,cz)
    vec_prod(cross_product1[0],cross_product1[1],cross_product1[2],vector1.x,vector1.y,vector1.z,vector2.x,vector2.y,vector2.z);
    // I get an error taking the adress of cross_product1[0], &cross_product1[0]. Took the pointer adresses instead. Works fine.
    if (verbal == 1) printf("cross_product = (%f,%f,%f)\n",cross_product1[0],cross_product1[1],cross_product1[2]);
    double cross_product_length = length_of_3vector(cross_product1);
    
    if (cross_product_length == 0) {
        // The cylinders are parallel.
        int seperated = 0;
        double delta[3];
        delta[0] = geometry_parent->center.x - geometry_child->center.x;
        delta[1] = geometry_parent->center.y - geometry_child->center.y;
        delta[2] = geometry_parent->center.z - geometry_child->center.z;

        // Test for separation by height
        // if (h0Div2 + h1Div2 − |Dot(W0, Delta )| < 0) seperated = 1;
        
        if (verbal == 1) printf("vector1 = (%f,%f,%f)\n",vector1.x,vector1.y,vector2.z);
        if (verbal == 1) printf("delta1 = (%f,%f,%f)\n",delta[0],delta[1],delta[2]);
        double scalar_prod1 = scalar_prod(vector1.x,vector1.y,vector1.z,delta[0],delta[1],delta[2]);
        if (verbal == 1) printf("scalar product = %f \n",scalar_prod1);
        if (verbal == 1) printf("height 1 = %f, height 2 = %f \n",height1,height2);
        
        int inside = 1;
        
        if (height1*0.5 < height2*0.5 + fabs(scalar_prod1)) {
                if (verbal == 1) printf("Cylinder sticks out height wise \n");
                inside = 0;
                }
    
        // Test for separation radially
        // if (rSum − |Delta − Dot(W0,Delta)∗W0| < 0) seperated = 1;
        double vector_between_cyl_axis[3];
        vector_between_cyl_axis[0] = delta[0] - scalar_prod1*vector1.x;
        vector_between_cyl_axis[1] = delta[1] - scalar_prod1*vector1.y;
        vector_between_cyl_axis[2] = delta[2] - scalar_prod1*vector1.z;
        if (verbal == 1) printf("vector_between = (%f,%f,%f)\n",vector_between_cyl_axis[0],vector_between_cyl_axis[1],vector_between_cyl_axis[2]);
        if (verbal == 1) printf("length of vector between = %f\n",length_of_3vector(vector_between_cyl_axis));
        if (verbal == 1) printf("radius1 = %f , radius2=%f\n",radius1,radius2);
        if (radius1 < radius2 + length_of_3vector(vector_between_cyl_axis)) { // Answers: Does cylinder 2 stick out of cylinder 1?
        //if (radius1 + length_of_3vector(vector_between_cyl_axis) > radius2 ) { // Answers: Does cylinder 1 stick out of cylinder 2 radially?
                if (verbal == 1) printf("Cylinder sticks out radially \n");
                inside = 0;
                }
        
        if (inside == 0) return 0;
        else return 1;
        
    } else {
        // printf("The component uses a raytracing method for non parallel cylinders.\n");
        // printf("  Make sure not to give this algorithm edge cases, where cylinders just touch.\n");
        Coords cyl_direction2 = geometry_child->geometry_parameters.p_cylinder_storage->direction_vector;
        
        // The center point of the perfect cylinder (cylinder 2) needs to be within the meshgrid one (cylinder 1), otherwise it can not be within
        // Check with a simple call to r_within_cylinder
        
        // if the center of cylinder 2 is within cylinder 1;
        if (r_within_cylinder(geometry_child->center,geometry_parent) == 0) return 0;  // if cylinder 2 center is not within cylinder 1, it is clearly not within
        
        
        // Doing a simple but not perfect overlap test

        // Checking cylinder sides.
        
        // Taking cylinder 1, making a vector at the base center.
        Coords base_point;
        
        base_point.x = geometry_child->center.x - 0.5*height2*cyl_direction2.x;
        base_point.y = geometry_child->center.y - 0.5*height2*cyl_direction2.y;
        base_point.z = geometry_child->center.z - 0.5*height2*cyl_direction2.z;
        
        if (verbal==1) print_position(base_point,"Base point position (for inside cylinder)");
        
        // Making a point at the circumference of the bottom circle of the cylinder
        double cross_input[3] = {0,1,0};
        // In case the cross input is parallel with the vector, a new is chosen. Both can't be parallel.
        if (scalar_prod(cross_input[0],cross_input[1],cross_input[2],cyl_direction2.x,cyl_direction2.y,cyl_direction2.z) > 0.99) {
            cross_input[0] = 1; cross_input[1] = 0; cross_input[2] = 0;
        }
        // print_position(make_position(cross_input),"cross input");
        
        double cross_product1[3] = {0,0,0};
        vec_prod(cross_product1[0],cross_product1[1],cross_product1[2],cyl_direction2.x,cyl_direction2.y,cyl_direction2.z,cross_input[0],cross_input[1],cross_input[2]);
        
        // print_position(make_position(cross_product1),"cross_product1");
        double cross_length = length_of_3vector(cross_product1);
        
        // printf("cross_length = %f \n",cross_length);
        cross_product1[0] /= cross_length;
        cross_product1[1] /= cross_length;
        cross_product1[2] /= cross_length;
        
        cross_product1[0] *= radius2;
        cross_product1[1] *= radius2;
        cross_product1[2] *= radius2;
        
        Coords circ_point;
        double radial_position[3],cyl_direction_pointer[3],base_point_vector[3],cyl_radial_direction[3];
        
        cyl_direction_pointer[0] = cyl_direction2.x;
        cyl_direction_pointer[1] = cyl_direction2.y;
        cyl_direction_pointer[2] = cyl_direction2.z;
        
        //print_position(coords_set(cyl_direction_pointer[0],cyl_direction_pointer[1],cyl_direction_pointer[2]),"cylinder direction vector");
        //print_position(coords_set(cross_product1[0],cross_product1[1],cross_product1[2]),"cross product (before rotation)");
        
        int iterate,number_of_solutions,solutions,number_of_positions = 30;
        double rotate_angle,temp_solution[2],positive_solution,negative_solution;
        
        // printf("length of cyl_direction_pointer = %f \n",length_of_3vector(cyl_direction_pointer));
        
        // Check intersection with cylinder 2 with that point, and cyl_direction, if there is an intersection before height1, they overlap.
        // Rotate the circumference point around the cyl_direction for a full circle to detect intersections all the way around.
        
        // Here cross_product1 is a vector from the base point to a point n the circumference
        // circ_point is a vector from the base point to the circumference rotated an angle.
        // radial_position is the actual position on the circumference on the cylinder as a vector from origo.
        Coords radial_coords,top_coords;
        
        for (iterate = 0;iterate < number_of_positions;iterate++) {
                rotate_angle = 2*3.14159*((double) iterate)/((double) number_of_positions);
                rotate(circ_point.x,circ_point.y,circ_point.z,cross_product1[0],cross_product1[1],cross_product1[2],rotate_angle,cyl_direction2.x,cyl_direction2.y,cyl_direction2.z);
            
                radial_position[0] = base_point.x + circ_point.x;
                radial_position[1] = base_point.y + circ_point.y;
                radial_position[2] = base_point.z + circ_point.z;
                // Debug check
                radial_coords = coords_add(base_point,circ_point);
                if (r_within_cylinder(radial_coords,geometry_parent) == 0) {
                    //printf("Radial pointer number %d was not inside cylinder 1 (%f %f %f)\n",iterate,radial_position[0],radial_position[1],radial_position[2]);
                    return 0;
                }
            
                // sample_cylinder_intersect(double *t,int *num_solutions,double *r,double *v,struct geometry_struct *geometry) {
				double nx_dummy[2], ny_dummy[2], nz_dummy[2];
				int surface_index_dummy[2];
                sample_cylinder_intersect(temp_solution, nx_dummy, ny_dummy, nz_dummy, surface_index_dummy, 
				                          &number_of_solutions,radial_position,cyl_direction_pointer,geometry_parent);
            
                if (number_of_solutions == 2) {
                    if (temp_solution[0]*temp_solution[1] > 0) {
                        // If both solutions are in the future or past, the point is outside the cylinder
                        if (verbal == 1) printf("Along axis: Not inside, as the solutions have the same sign: %f %f\n",temp_solution[0],temp_solution[1]);
                        return 0;
                    } else {
                        // The solutions have different signs
                        if (temp_solution[0] < 0) {
                            negative_solution = temp_solution[0];
                            positive_solution = temp_solution[1];
                        } else {
                            negative_solution = temp_solution[1];
                            positive_solution = temp_solution[0];
                        }
                        // If there is a solution before the cylinder ends, cylinder 2 can not be within cylinder 1
                        if (positive_solution < height2) {
                            if (verbal == 1) printf("Along axis: Not inside, as the positive solutions is less than the cylinder height: %f %f\n",temp_solution[0],temp_solution[1]);
                            if (verbal == 1) printf("Radial position = (%f,%f,%f) \n",radial_position[0],radial_position[1],radial_position[2]);
                            return 0;
                        }
                    }
                } else {
                    if (verbal == 1) printf("Along axis: 0 or 1 solution!\n");
                    return 0; // If there are 1 or 0 solutions, the radial position (on cylinder 2) would not be inside cylinder 1
                }
            
                cyl_radial_direction[0] = circ_point.x;
                cyl_radial_direction[1] = circ_point.y;
                cyl_radial_direction[2] = circ_point.z;
                // Note it has length radius1

                // Debug check
                if (r_within_cylinder(base_point,geometry_parent) == 0) {
                    //printf("Base point number %d was not inside cylinder 1 (%f %f %f)\n",iterate,base_point_vector[0],base_point_vector[1],base_point_vector[2]);
                    return 0;
                }
            
                // Base point in vector notation needed for intersect function.
                base_point_vector[0] = base_point.x;
                base_point_vector[1] = base_point.y;
                base_point_vector[2] = base_point.z;
            
                // The vector circ_point is from the base to the circumference. This is used to check the bottom cap.
                sample_cylinder_intersect(temp_solution, nx_dummy, ny_dummy, nz_dummy, surface_index_dummy,
				                          &number_of_solutions,base_point_vector,cyl_radial_direction,geometry_parent);
                
                if (number_of_solutions == 2) {
                    if (temp_solution[0]*temp_solution[1] > 0) {
                        // If both solutions are in the future or past, the point is outside the cylinder
                        if (verbal == 1) printf("Radial bottom: Not inside, as the solutions have the same sign: %f %f\n",temp_solution[0],temp_solution[1]);
                        return 0;
                    } else {
                        // The solutions have different signs
                        if (temp_solution[0] < 0) {
                            negative_solution = temp_solution[0];
                            positive_solution = temp_solution[1];
                        } else {
                            negative_solution = temp_solution[1];
                            positive_solution = temp_solution[0];
                        }
                        // If there is a solution before the line reaches the circumference from the center, cylinder 2 can not be within cylinder 1
                        if (positive_solution < 1 || negative_solution > -1) {
                            if (verbal == 1) printf("Radial bottom: Not inside, as the positive solutions is less than the cylinder radius: %f %f\n",temp_solution[0],temp_solution[1]);
                            return 0;
                        }
                    }
                } else {
                    if (verbal == 1) printf("Radially bottom: 0 or 1 solution!\n");
                    if (verbal == 1) print_position(circ_point,"current circ point");
                    if (verbal == 1) print_position(coords_set(cyl_radial_direction[0],cyl_radial_direction[1],cyl_radial_direction[2]),"current cyl_radial_direction (should be same as above)");
                    return 0; // If there are 1 or 0 solutions, the radial position (on cylinder 2) would not be inside cylinder 1
                }
            
                // Now check the top
                base_point_vector[0] = base_point.x + height2*cyl_direction2.x;
                base_point_vector[1] = base_point.y + height2*cyl_direction2.y;
                base_point_vector[2] = base_point.z + height2*cyl_direction2.z;
            
                top_coords = coords_set(base_point_vector[0],base_point_vector[1],base_point_vector[2]);
                // Debug check
                if (r_within_cylinder(top_coords,geometry_parent) == 0) {
                    //printf("Top point number %d was not inside cylinder 1 (%f %f %f)\n",iterate,base_point_vector[0],base_point_vector[1],base_point_vector[2]);
                    return 0;
                }
            
                // The vector circ_point is from the base to the circumference. This is used to check the bottom cap.
                sample_cylinder_intersect(temp_solution, nx_dummy, ny_dummy, nz_dummy, surface_index_dummy,
				                          &number_of_solutions,base_point_vector,cyl_radial_direction,geometry_parent);
                if (number_of_solutions == 2) {
                    if (temp_solution[0]*temp_solution[1] > 0) {
                        // If both solutions are in the future or past, the point is outside the cylinder
                        if (verbal == 1) printf("Radial top: Not inside, as the solutions have the same sign: %f %f\n",temp_solution[0],temp_solution[1]);
                        return 0;
                    } else {
                        // The solutions have different signs
                        if (temp_solution[0] < 0) {
                            negative_solution = temp_solution[0];
                            positive_solution = temp_solution[1];
                        } else {
                            negative_solution = temp_solution[1];
                            positive_solution = temp_solution[0];
                        }
                        // If there is a solution before the line reaches the circumference from the center, cylinder 2 can not be within cylinder 1
                        if (positive_solution < 1 || negative_solution > -1) {
                            if (verbal == 1) printf("Radial top: Not inside, as the positive solutions is less than the cylinder radius: %f %f\n",temp_solution[0],temp_solution[1]);
                            return 0;
                        }
                    }
                } else {
                    if (verbal == 1) printf("Radially top: 0 or 1 solution!\n");
                    return 0; // If there are 1 or 0 solutions, the radial position (on cylinder 2) would not be inside cylinder 1
                }

        }
        // The above method is not perfect as it basicly tests a mesh grid of the cylinder aginst another perfect cylinder.
        // Can be improved.
        
        // If no intersections is found and the center of cylinder 2 is within cylinder 1, cylinder 2 must be within cylinder 1.
        return 1;
    
    }

};

int cylinder_within_cylinder_backup(struct geometry_struct *geometry_child,struct geometry_struct *geometry_parent) {
//int cylinder_within_cylinder(struct geometry_struct *geometry_parent,struct geometry_struct *geometry_child) {
    // Unpack parameters
    double radius1 = geometry_parent->geometry_parameters.p_cylinder_storage->cyl_radius;
    double height1 = geometry_parent->geometry_parameters.p_cylinder_storage->height;
    double radius2 = geometry_child->geometry_parameters.p_cylinder_storage->cyl_radius;
    double height2 = geometry_child->geometry_parameters.p_cylinder_storage->height;
    
    int verbal = 1;
    // Generate unit direction vector along center axis of cylinders
    
    // Start with vector that points along the cylinder in the simple frame, and rotate to global
    Coords simple_vector = coords_set(0,1,0);
    Coords vector1,vector2;
    if (verbal == 1) printf("Cords start_vector = (%f,%f,%f)\n",simple_vector.x,simple_vector.y,simple_vector.z);

    // Rotate the position of the ray around the center of the cylinder
    vector1 = rot_apply(geometry_parent->rotation_matrix,simple_vector);
    if (verbal == 1) printf("Cords vector1 = (%f,%f,%f)\n",vector1.x,vector1.y,vector1.z);
    // Rotate the position of the ray around the center of the cylinder
    vector2 = rot_apply(geometry_child->rotation_matrix,simple_vector);
    if (verbal == 1) printf("Cords vector2 = (%f,%f,%f)\n",vector2.x,vector2.y,vector2.z);
    
    // if vector1 and vector2 are parallel, the problem is simple, but if not complicated
    double cross_product1[3] = {0,0,0};
    // printf("%f\n",cross_product1[0]);
    // vec prod(&ax,&ay,&az,bx,by,bz, cx,cy,cz)
    vec_prod(cross_product1[0],cross_product1[1],cross_product1[2],vector1.x,vector1.y,vector1.z,vector2.x,vector2.y,vector2.z);
    // I get an error taking the adress of cross_product1[0], &cross_product1[0]. Took the pointer adresses instead. Works fine.
    if (verbal == 1) printf("cross_product = (%f,%f,%f)\n",cross_product1[0],cross_product1[1],cross_product1[2]);
    double cross_product_length = length_of_3vector(cross_product1);
    
    if (cross_product_length == 0) {
        // The cylinders are parallel.
        int seperated = 0;
        double delta[3];
        delta[0] = geometry_parent->center.x - geometry_child->center.x;
        delta[1] = geometry_parent->center.y - geometry_child->center.y;
        delta[2] = geometry_parent->center.z - geometry_child->center.z;

        // Test for separation by height
        // if (h0Div2 + h1Div2 − |Dot(W0, Delta )| < 0) seperated = 1;
        
        if (verbal == 1) printf("vector1 = (%f,%f,%f)\n",vector1.x,vector1.y,vector2.z);
        if (verbal == 1) printf("delta1 = (%f,%f,%f)\n",delta[0],delta[1],delta[2]);
        double scalar_prod1 = scalar_prod(vector1.x,vector1.y,vector1.z,delta[0],delta[1],delta[2]);
        if (verbal == 1) printf("scalar product = %f \n",scalar_prod1);
        if (verbal == 1) printf("height 1 = %f, height 2 = %f \n",height1,height2);
        
        int inside = 1;
        
        if (height1*0.5 < height2*0.5 + fabs(scalar_prod1)) {
                if (verbal == 1) printf("Cylinder sticks out height wise \n");
                inside = 0;
                }
    
        // Test for separation radially
        // if (rSum − |Delta − Dot(W0,Delta)∗W0| < 0) seperated = 1;
        double vector_between_cyl_axis[3];
        vector_between_cyl_axis[0] = delta[0] - scalar_prod1*vector1.x;
        vector_between_cyl_axis[1] = delta[1] - scalar_prod1*vector1.y;
        vector_between_cyl_axis[2] = delta[2] - scalar_prod1*vector1.z;
        if (verbal == 1) printf("vector_between = (%f,%f,%f)\n",vector_between_cyl_axis[0],vector_between_cyl_axis[1],vector_between_cyl_axis[2]);
        if (verbal == 1) printf("length of vector between = %f\n",length_of_3vector(vector_between_cyl_axis));
        if (verbal == 1) printf("radius1 = %f , radius2=%f\n",radius1,radius2);
        if (radius1 < radius2 + length_of_3vector(vector_between_cyl_axis)) { // Answers: Does cylinder 2 stick out of cylinder 1?
        //if (radius1 + length_of_3vector(vector_between_cyl_axis) > radius2 ) { // Answers: Does cylinder 1 stick out of cylinder 2 radially?
                if (verbal == 1) printf("Cylinder sticks out radially \n");
                inside = 0;
                }
        
        if (inside == 0) return 0;
        else return 1;
        
    } else {
        // printf("The component uses a raytracing method for non parallel cylinders.\n");
        // printf("  Make sure not to give this algorithm edge cases, where cylinders just touch.\n");
        Coords cyl_direction1 = geometry_parent->geometry_parameters.p_cylinder_storage->direction_vector;
        
        // The center point of the perfect cylinder (cylinder 2) needs to be within the meshgrid one (cylinder 1), otherwise it can not be within
        // Check with a simple call to r_within_cylinder
        
        // if the center of cylinder 2 is within cylinder 1;
        if (r_within_cylinder(geometry_child->center,geometry_parent) == 0) return 0;  // if cylinder 2 center is not within cylinder 1, it is clearly not within
        
        
        // Doing a simple but not perfect overlap test

        // Checking cylinder sides.
        
        // Taking cylinder 1, making a vector at the base center.
        Coords base_point;
        
        base_point.x = geometry_parent->center.x - 0.5*height1*cyl_direction1.x;
        base_point.y = geometry_parent->center.y - 0.5*height1*cyl_direction1.y;
        base_point.z = geometry_parent->center.z - 0.5*height1*cyl_direction1.z;
        
        // Making a point at the circumference of the bottom circle of the cylinder
        double cross_input[3] = {0,1,0};
        // In case the cross input is parallel with the vector, a new is chosen. Both can't be parallel.
        if (scalar_prod(cross_input[0],cross_input[1],cross_input[2],cyl_direction1.x,cyl_direction1.y,cyl_direction1.z) > 0.99) {
            cross_input[0] = 1; cross_input[1] = 0; cross_input[2] = 0;
        }
        // print_position(make_position(cross_input),"cross input");
        
        double cross_product1[3] = {0,0,0};
        vec_prod(cross_product1[0],cross_product1[1],cross_product1[2],cyl_direction1.x,cyl_direction1.y,cyl_direction1.z,cross_input[0],cross_input[1],cross_input[2]);
        
        // print_position(make_position(cross_product1),"cross_product1");
        double cross_length = length_of_3vector(cross_product1);
        
        // printf("cross_length = %f \n",cross_length);
        cross_product1[0] /= cross_length;
        cross_product1[1] /= cross_length;
        cross_product1[2] /= cross_length;
        
        cross_product1[0] *= radius1;
        cross_product1[1] *= radius1;
        cross_product1[2] *= radius1;
        
        Coords circ_point;
        double radial_position[3],cyl_direction_pointer[3],base_point_vector[3],cyl_radial_direction[3];
        
        cyl_direction_pointer[0] = cyl_direction1.x;
        cyl_direction_pointer[1] = cyl_direction1.y;
        cyl_direction_pointer[2] = cyl_direction1.z;
        
        print_position(make_position(cyl_direction_pointer),"cylinder direction vector");
        
        int iterate,number_of_solutions,solutions,number_of_positions = 30;
        double rotate_angle,temp_solution[2];
        
        // printf("length of cyl_direction_pointer = %f \n",length_of_3vector(cyl_direction_pointer));
        
        // Check intersection with cylinder 2 with that point, and cyl_direction, if there is an intersection before height1, they overlap.
        // Rotate the circumference point around the cyl_direction for a full circle to detect intersections all the way around.
        
        // Here cross_product1 is a vector from the base point to a point n the circumference
        // circ_point is a vector from the base point to the circumference rotated an angle.
        // radial_position is the actual position on the circumference on the cylinder as a vector from origo.
        for (iterate = 0;iterate < number_of_positions;iterate++) {
                rotate_angle = 2*3.14159*((double) iterate)/((double) number_of_positions);
                rotate(circ_point.x,circ_point.y,circ_point.z,cross_product1[0],cross_product1[1],cross_product1[2],rotate_angle,cyl_direction1.x,cyl_direction1.y,cyl_direction1.z);
            
                radial_position[0] = base_point.x + circ_point.x;
                radial_position[1] = base_point.y + circ_point.y;
                radial_position[2] = base_point.z + circ_point.z;
                // sample_cylinder_intersect(double *t,int *num_solutions,double *r,double *v,struct geometry_struct *geometry) {
				double nx_dummy[2], ny_dummy[2], nz_dummy[2];
				int surface_index_dummy[2];
                sample_cylinder_intersect(temp_solution, nx_dummy, ny_dummy, nz_dummy, surface_index_dummy,
				                          &number_of_solutions,radial_position,cyl_direction_pointer,geometry_child);
                for (solutions = 0;solutions < number_of_solutions;solutions++) {
                    if (temp_solution[solutions] > 0 && temp_solution[solutions] < height1) {
                        // cylinders must overlap.
                        return 0;
                    }
                }
                // if (number_of_solutions == 2) {
                //    if (temp_solution[0] < 0 && temp_solution[1] < 0) return 0; // cylinder 2 outside cylinder 1
                //    if (temp_solution[0] > 0 && temp_solution[1] > 0) return 0; // cylinder 2 outside cylinder 1
                //}
            
                cyl_radial_direction[0] = circ_point.x;
                cyl_radial_direction[1] = circ_point.y;
                cyl_radial_direction[2] = circ_point.z;
                // Note it has length radius1
            
                base_point_vector[0] = base_point.x;
                base_point_vector[1] = base_point.y;
                base_point_vector[2] = base_point.z;
            
                // The vector circ_point is from the base to the circumference. This is used to check the bottom cap.
                sample_cylinder_intersect(temp_solution, nx_dummy, ny_dummy, nz_dummy, surface_index_dummy,
				                          &number_of_solutions,base_point_vector,cyl_radial_direction,geometry_child);
                for (solutions = 0;solutions < number_of_solutions;solutions++) {
                    if (temp_solution[solutions] > 0 && temp_solution[solutions] < 1) {
                        // cylinders must overlap.
                        return 0;
                    }
                }
                // if (number_of_solutions == 2) {
                //    if (temp_solution[0] < 0 && temp_solution[1] < 0) return 0; // cylinder 2 outside cylinder 1
                //    if (temp_solution[0] > 0 && temp_solution[1] > 0) return 0; // cylinder 2 outside cylinder 1
                //}
            
                // Now check the top
                base_point_vector[0] = base_point.x + height1*cyl_direction1.x;
                base_point_vector[1] = base_point.y + height1*cyl_direction1.y;
                base_point_vector[2] = base_point.z + height1*cyl_direction1.z;
            
                // The vector circ_point is from the base to the circumference. This is used to check the bottom cap.
                sample_cylinder_intersect(temp_solution, nx_dummy, ny_dummy, nz_dummy, surface_index_dummy,
				                          &number_of_solutions,base_point_vector,cyl_radial_direction,geometry_child);
                for (solutions = 0;solutions < number_of_solutions;solutions++) {
                    if (temp_solution[solutions] > 0 && temp_solution[solutions] < 1) {
                        // cylinders must overlap.
                        return 0;
                    }
                }
                // if (number_of_solutions == 2) {
                //    if (temp_solution[0] < 0 && temp_solution[1] < 0) return 0; // cylinder 2 outside cylinder 1
                //    if (temp_solution[0] > 0 && temp_solution[1] > 0) return 0; // cylinder 2 outside cylinder 1
                //}
        }
        // The above method is not perfect as it basicly tests a mesh grid of the cylinder aginst another perfect cylinder.
        // Can be improved.
        
        // If no intersections is found and the center of cylinder 2 is within cylinder 1, cylinder 2 must be within cylinder 1.
        return 1;
    
    }

};

int cone_overlaps_cone(struct geometry_struct *geometry1,struct geometry_struct *geometry2) {
  // Overlap function should return 1 if the to geometries both cover some volume
  // Temporary function

    // Load Variables:
    Coords direction_1 = geometry1->geometry_parameters.p_cone_storage->direction_vector;
    Coords center_1 = geometry1->center;
    double radius_top_1 = geometry1->geometry_parameters.p_cone_storage->cone_radius_top;
    double radius_bottom_1 = geometry1->geometry_parameters.p_cone_storage->cone_radius_bottom;
    double height_1 = geometry1->geometry_parameters.p_cone_storage->height;

    Coords direction_2 = geometry2->geometry_parameters.p_cone_storage->direction_vector;
    Coords center_2 = geometry2->center;
    double radius_top_2 = geometry2->geometry_parameters.p_cone_storage->cone_radius_top;
    double radius_bottom_2 = geometry2->geometry_parameters.p_cone_storage->cone_radius_bottom;
    double height_2 = geometry2->geometry_parameters.p_cone_storage->height;

    double Y;
    double max_r;


    // // Simple test to see if they are far away (with smallest spheres outside)
    // Create Spheres

    /*
    Y = -(0.5*height_1)-(radius_top_1*radius_top_1-radius_bottom_1*radius_bottom_1)/(2.0*height_1);
    if (radius_top_1 > radius_bottom_1){
        max_r = radius_top_1;
    }else{
        max_r = radius_bottom_1;
    }
    double sphere_1_radius =  sqrt((Y+0.5*height_1)*(Y+0.5*height_1)+max_r*max_r);
    Coords sphere_1_pos = coords_set(center_1.x+direction_1.x*Y,center_1.y+direction_1.y*Y,center_1.z+direction_1.z*Y);
    */

    // Not sure above works, writing own version.
    
    double dist_above_bottom = 0.5*(radius_top_1*radius_top_1+height_1*height_1-radius_bottom_1*radius_bottom_1)/height_1;
    double dist_from_center = dist_above_bottom - 0.5*height_1;
    Coords sphere_1_pos = coords_set(center_1.x+direction_1.x*dist_from_center,
                                     center_1.y+direction_1.y*dist_from_center,
                                     center_1.z+direction_1.z*dist_from_center);
    double sphere_1_radius = sqrt(radius_bottom_1*radius_bottom_1+dist_above_bottom*dist_above_bottom);
    
    /*
    Y = -(0.5*height_2)-(radius_top_2*radius_top_2-radius_bottom_2*radius_bottom_2)/(2.0*height_2);
    if (radius_top_2 > radius_bottom_2){
        max_r = radius_top_2;
    }else{
        max_r = radius_bottom_2;
    }
    double sphere_2_radius =  sqrt((Y+0.5*height_2)*(Y+0.5*height_2)+max_r*max_r);
    Coords sphere_2_pos = coords_set(center_2.x+direction_2.x*Y,center_2.y+direction_2.y*Y,center_2.z+direction_2.z*Y);
    */
    
    dist_above_bottom = 0.5*(radius_top_2*radius_top_2+height_2*height_2-radius_bottom_2*radius_bottom_2)/height_2;
    dist_from_center = dist_above_bottom - 0.5*height_2;
    Coords sphere_2_pos = coords_set(center_2.x+direction_2.x*dist_from_center,
                                     center_2.y+direction_2.y*dist_from_center,
                                     center_2.z+direction_2.z*dist_from_center);
    double sphere_2_radius = sqrt(radius_bottom_2*radius_bottom_2+dist_above_bottom*dist_above_bottom);

    // Test if spheres are too long apart to have any chance of intersecting

    double dist_spheres = sqrt((sphere_1_pos.x-sphere_2_pos.x)*(sphere_1_pos.x-sphere_2_pos.x)+(sphere_1_pos.y-sphere_2_pos.y)*(sphere_1_pos.y-sphere_2_pos.y)+(sphere_1_pos.z-sphere_2_pos.z)*(sphere_1_pos.z-sphere_2_pos.z));


    if (dist_spheres > sphere_1_radius + sphere_2_radius){
        //printf("\nSpherical method determined that cones are too far away for intersection to be relevant\n");
        return 0;
    }

    // // Simple test to see if they are inside (with largest spheres inside)
    // Brute force in two steps.
    // 1. Check if any points on 1 lies within 2
    // 2. Check if any transversal lines on the mesh of 1 intersects with 2

    // Calculate needed information
    Coords cone_1_bottom_point = coords_add(center_1,coords_scalar_mult(direction_1,-0.5*height_1));
    Coords cone_1_top_point = coords_add(center_1,coords_scalar_mult(direction_1,0.5*height_1));
    Coords cone_2_bottom_point = coords_add(center_2,coords_scalar_mult(direction_2,-0.5*height_2));
    Coords cone_2_top_point = coords_add(center_2,coords_scalar_mult(direction_2,0.5*height_2));


    // Create two circles for both geometries
    int resoultuion = 500;

    struct pointer_to_1d_coords_list cone_1_points = geometry1->shell_points(geometry1,resoultuion);

    //points_on_circle(cone_1_top,cone_1_top_point,direction_1,radius_top_1,resoultuion);
    //points_on_circle(cone_1_bottom,cone_1_bottom_point,direction_1,radius_bottom_1,resoultuion);

    //printf("\nTEST\n");
    int i;
    // Test geometry 1 points inside geometry 2

    for (i = 0 ; i < cone_1_points.num_elements ; i++){
        
        if (r_within_cone(cone_1_points.elements[i],geometry2) == 1){
            //printf("\nOne point on cone 1 is inside cone 2\n");
            return 1;
        }
    }
    
    struct pointer_to_1d_coords_list cone_2_points = geometry2->shell_points(geometry2,resoultuion);

    // Test geometry 2 points inside geometry 1
    for (i = 0 ; i < cone_2_points.num_elements ; i++){
        
        if (r_within_cone(cone_2_points.elements[i],geometry1) == 1){
            //printf("\nOne point on cone 2 is inside cone 1\n");
            return 1;
        }
    }

    // Test 1 within 2


    // // Test if there is any intersection (intersection function or eqation?)

    // This is an implementation of brute force. Maybe do this with a calculated function?
    int circ_resolution = 50;  // how many lines will the be checked for
    int height_resolution = 150;

    double length_of_cone_side_1 = sqrt(pow(radius_top_1-radius_bottom_1,2)+pow(height_1,2));
    double length_of_cone_side_2 = sqrt(pow(radius_top_2-radius_bottom_2,2)+pow(height_2,2));

    double slope_1 = (radius_top_1-radius_bottom_1)/height_1;
    double slope_2 = (radius_top_2-radius_bottom_2)/height_2;

    double local_radius;

    Coords cone_1_direction = geometry1->geometry_parameters.p_cone_storage->direction_vector;
    Coords cone_2_direction = geometry2->geometry_parameters.p_cone_storage->direction_vector;

    //printf("\nlength_of_cone_side_1 = %f\n",length_of_cone_side_1);

    Coords circ_points[50];
    double circ_offset;
    Coords circ_center;

    int j;

    for (i = 0 ; i < height_resolution ; i++){
        // Calculate circ offset
        //circ_offset = i * length_of_cone_side_1 / height_resolution; // Possible bug
        circ_offset = i * height_1 / height_resolution;

        // Calculate middle point
        circ_center = coords_add(cone_1_bottom_point,coords_set(cone_1_direction.x * circ_offset,cone_1_direction.y * circ_offset,cone_1_direction.z * circ_offset));

        // Calculate radius
        local_radius = circ_offset * slope_1 + radius_bottom_1;

        // Make points on circle
        //printf("points on circle: circ_center = [%f,%f,%f] , cone_1_direction = [%f,%f,%f] , local_radius = %f , circ_resolution = %i",circ_center.x,circ_center.y,circ_center.z,cone_1_direction.x,cone_1_direction.y,cone_1_direction.z,local_radius,circ_resolution);
        points_on_circle(circ_points,circ_center,cone_1_direction,local_radius,circ_resolution);

        // Test if any points lies within geomtry 2
        for (j = 0 ; j < circ_resolution; j++){
            //printf("\ntested if point [%i] [%f,%f,%f] is inside",j,circ_points[j].x,circ_points[j].y,circ_points[j].z);
            if (r_within_cone(circ_points[j],geometry2) == 1){
                //printf("\nOne point on cone 1 is inside cone 2\n");
                return 1;
            }
        }
    }


    for (i = 0 ; i < height_resolution ; i++){
        // Calculate circ offset
        // circ_offset = i * length_of_cone_side_1 / height_resolution; // Possible bug
        circ_offset = i * height_2 / height_resolution; // Possible bug

        // Calculate middle point
        circ_center = coords_add(cone_2_bottom_point,coords_set(cone_2_direction.x * circ_offset,cone_2_direction.y * circ_offset,cone_2_direction.z * circ_offset));

        // Calculate radius
        local_radius = circ_offset * slope_2 + radius_bottom_2;

        // Make points on circle
        //printf("points on circle: circ_center = [%f,%f,%f] , cone_1_direction = [%f,%f,%f] , local_radius = %f , circ_resolution = %i",circ_center.x,circ_center.y,circ_center.z,cone_2_direction.x,cone_2_direction.y,cone_2_direction.z,local_radius,circ_resolution);
        points_on_circle(circ_points,circ_center,cone_2_direction,local_radius,circ_resolution);

        // Test if any points lies within geomtry 2
        for (j = 0 ; j < circ_resolution; j++){
            //printf("\ntested if point [%i] [%f,%f,%f] is inside",j,circ_points[j].x,circ_points[j].y,circ_points[j].z);
            if (r_within_cone(circ_points[j],geometry1) == 1){
                //printf("\nOne point on cone 2 is inside cone 1\n");
                return 1;
            }
        }
    }

    return 0;

};

int cone_within_cone(struct geometry_struct *geometry_child,struct geometry_struct *geometry_parent) {
    // Function returns 1 if the cone is completely within the cylinder, 0 otherwise
    // Brute force place holder
    return A_within_B(geometry_child,geometry_parent,(int) 300); // 150 points on each end cap
};

int mesh_overlaps_mesh(struct geometry_struct *geometry1,struct geometry_struct *geometry2) {
  // Overlap function should return 1 if the to geometries both cover some volume
  // Temporary function

    // Brute force check if there is one point of geometry 1 in 2 and 2 in 1.

    // Should also have a secondary check with edges intersecting on faces.
    // Could be made faster with a bounding box (or sphere)


    // Load Variables:
    struct pointer_to_1d_coords_list shell_points1 = geometry1->shell_points(geometry1,144);
    struct pointer_to_1d_coords_list shell_points2 = geometry2->shell_points(geometry2,144);

    int i;
    for (i = 0 ; i < shell_points1.num_elements ; i++){
        if (geometry2->within_function(shell_points1.elements[i],geometry2)){
            free(shell_points1.elements);
            free(shell_points2.elements);
            return 1;
        }
    }
    for (i = 0 ; i < shell_points2.num_elements ; i++){
        if (geometry1->within_function(shell_points2.elements[i],geometry1)){
            free(shell_points1.elements);
            free(shell_points2.elements);
            return 1;
        }
    }
    
    free(shell_points1.elements);
    free(shell_points2.elements);

    return 0;

};
int mesh_within_box(struct geometry_struct *geometry_child,struct geometry_struct *geometry_parent) {
    // Function returns 1 if the cone is completely within the cylinder, 0 otherwise
    // Brute force place holder
    return mesh_A_within_B(geometry_child,geometry_parent); // 30 points on each end cap
};

int box_within_mesh(struct geometry_struct *geometry_child,struct geometry_struct *geometry_parent) {
    // Function returns 1 if the cone is completely within the cylinder, 0 otherwise
    // Brute force place holder
    return mesh_A_within_B(geometry_child,geometry_parent); // 30 points on each end cap
};

int mesh_within_sphere(struct geometry_struct *geometry_child,struct geometry_struct *geometry_parent) {
    // Function returns 1 if the cone is completely within the cylinder, 0 otherwise
    // Brute force place holder
    return mesh_A_within_B(geometry_child,geometry_parent); // 30 points on each end cap
};
int sphere_within_mesh(struct geometry_struct *geometry_child,struct geometry_struct *geometry_parent) {
    // Function returns 1 if the cone is completely within the cylinder, 0 otherwise
    // Brute force place holder
    return mesh_A_within_B(geometry_child,geometry_parent); // 30 points on each end cap
};
int cone_within_mesh(struct geometry_struct *geometry_child,struct geometry_struct *geometry_parent) {
    // Function returns 1 if the cone is completely within the cylinder, 0 otherwise
    // Brute force place holder
    return mesh_A_within_B(geometry_child,geometry_parent); // 30 points on each end cap
};


int mesh_within_cone(struct geometry_struct *geometry_child,struct geometry_struct *geometry_parent) {
    // Function returns 1 if the cone is completely within the cylinder, 0 otherwise
    // Brute force place holder
    return mesh_A_within_B(geometry_child,geometry_parent); // 30 points on each end cap
};

int mesh_within_cylinder(struct geometry_struct *geometry_child,struct geometry_struct *geometry_parent) {
    // Function returns 1 if the cone is completely within the cylinder, 0 otherwise
    // Brute force place holder
    return mesh_A_within_B(geometry_child,geometry_parent); // 30 points on each end cap
};


int cylinder_within_mesh(struct geometry_struct *geometry_child,struct geometry_struct *geometry_parent) {
    // Function returns 1 if the cone is completely within the cylinder, 0 otherwise
    // Brute force place holder
    return mesh_A_within_B(geometry_child,geometry_parent); // 30 points on each end cap
};

int mesh_within_mesh(struct geometry_struct *geometry_child,struct geometry_struct *geometry_parent) {
    // WARNING: This may fail as one or both of the meshes may not be convex
    // Function returns 1 if the cone is completely within the cylinder, 0 otherwise
    // Brute force place holder
    return mesh_A_within_B(geometry_child,geometry_parent); // 30 points on each end cap
};

// -------------    Overlap functions for two different geometries --------------------------------

int box_overlaps_cylinder(struct geometry_struct *geometry_box,struct geometry_struct *geometry_cyl) {
    // Checking if the box and cylinder described by geometry_box and geometry_cyl overlaps.
    // Done in steps:
    // If any corner points of the box is within the cylinder, they do overlap
    // If any points on the cylinders end caps are within the box, they do overlap
    // If any of the lines describing the sides of the box intersect the cylinder, they do overlap
    // If the symmetry line of the cylinder intersect the box, they do overlap
    // If none of the above are true, they do not overlap
    
    // A problem with this algorithm is a lack of a quick exit if the volumes obviously does not overlap

    // Generate coordinates of corners of box
    Coords corner_points[8];
    box_corners_global_frame(corner_points,geometry_box);
    
    // Check earch corner seperatly
    int iterate;
    for (iterate=0;iterate<8;iterate++) {
        if (geometry_cyl->within_function(corner_points[iterate],geometry_cyl) == 1) {
            return 1; // If a corner of the box is inside the cylinder, the two volumes overlap
        }
    }
    
    Coords cyl_direction = geometry_cyl->geometry_parameters.p_cylinder_storage->direction_vector;
    Coords center = geometry_cyl->center;
    double radius = geometry_cyl->geometry_parameters.p_cylinder_storage->cyl_radius;
    double height = geometry_cyl->geometry_parameters.p_cylinder_storage->height;
    
    Coords cyl_top_point = coords_add(center,coords_scalar_mult(cyl_direction,0.5*height));
    Coords cyl_bottom_point = coords_add(center,coords_scalar_mult(cyl_direction,-0.5*height));
    
    // Generate 100 points on the circle describing the top of the cylinder
    Coords *circle_point_array;
    int number_of_points = 150;
    circle_point_array = malloc(number_of_points * sizeof(Coords));
    if (!circle_point_array) {
      fprintf(stderr,"Failure allocating list in Union function box_overlaps_cylinder - Exit!\n");
      exit(EXIT_FAILURE);
    }
    points_on_circle(circle_point_array,cyl_top_point,cyl_direction,radius,number_of_points);
    
    // Check parts of cylinder top seperatly
    for (iterate=0;iterate<number_of_points;iterate++) {
        if (geometry_box->within_function(circle_point_array[iterate],geometry_box) == 1) {
            return 1; // If part of the cylinder is inside the box, the volumes overlap
        }
    }
    
    // Check parts of cylinder bottom seperatly
    points_on_circle(circle_point_array,cyl_bottom_point,cyl_direction,radius,number_of_points);
    for (iterate=0;iterate<number_of_points;iterate++) {
        if (geometry_box->within_function(circle_point_array[iterate],geometry_box) == 1) {
            return 1; // If part of the cylinder is inside the box, the volumes overlap
        }
    }
    free(circle_point_array);

    // Check intersections for the lines between the corners of the box and the cylinder
    // 12 sides to a box, if any one of them intersects, the volumes overlaps
    for (iterate=0;iterate<3;iterate++) { //
        if (existence_of_intersection(corner_points[iterate],corner_points[iterate+1],geometry_cyl) == 1) return 1;
    }
    if (existence_of_intersection(corner_points[3],corner_points[0],geometry_cyl) == 1) return 1;
    for (iterate=4;iterate<7;iterate++) {
        if (existence_of_intersection(corner_points[iterate],corner_points[iterate+1],geometry_cyl) == 1) return 1;
    }
    if (existence_of_intersection(corner_points[7],corner_points[4],geometry_cyl) == 1) return 1;
    for (iterate=0;iterate<4;iterate++) {
        if (existence_of_intersection(corner_points[iterate],corner_points[iterate+4],geometry_cyl) == 1) return 1;
    }
    
    // Only need to test the intersection between the symetry line of the cylinder and the box
    if (existence_of_intersection(cyl_top_point,cyl_bottom_point,geometry_box) == 1) return 1;
    
    // If all the tests change, the volumes do not overlap
    return 0;
};

int cylinder_overlaps_box(struct geometry_struct *geometry_cyl,struct geometry_struct *geometry_box) {
    // overlap functions are symetrical, but it is convinient to have both defined
    return box_overlaps_cylinder(geometry_box,geometry_cyl);
};

int cylinder_overlaps_sphere(struct geometry_struct *geometry_cyl,struct geometry_struct *geometry_sph) {
 
    // If the sphere center is inside, one can exit fast
    Coords sph_center = geometry_sph->center;
    if (geometry_cyl->within_function(sph_center,geometry_cyl) == 1) return 1;
    
    // If cylinder center is inside, one can exit fast
    Coords cyl_center = geometry_cyl->center;
    if (geometry_sph->within_function(cyl_center,geometry_sph) == 1) return 1;
    
    double cyl_radius = geometry_cyl->geometry_parameters.p_cylinder_storage->cyl_radius;
    double cyl_height = geometry_cyl->geometry_parameters.p_cylinder_storage->height;
    Coords cyl_direction = geometry_cyl->geometry_parameters.p_cylinder_storage->direction_vector;
    
    // Or cylinder top / bottom point
    Coords cyl_top_point = coords_add(cyl_center,coords_scalar_mult(cyl_direction,0.5*cyl_height));
    if (geometry_sph->within_function(cyl_top_point,geometry_sph) == 1) return 1;
    
    Coords cyl_bottom_point = coords_add(cyl_center,coords_scalar_mult(cyl_direction,-0.5*cyl_height));
    if (geometry_sph->within_function(cyl_bottom_point,geometry_sph) == 1) return 1;
    
    // Calculate distance
    double distance = distance_between(geometry_cyl->center,geometry_sph->center);
    
    double sph_radius = geometry_sph->geometry_parameters.p_sphere_storage->sph_radius;
    
    // Return 0 if the bounding sphere and the sphere do not overlap, otherwise do brute force
    if (distance > sph_radius + sqrt(cyl_radius*cyl_radius+0.25*cyl_height*cyl_height)) return 0;
    
    // Could check "inner sphere" of cylinder against sphere, if they overlap, the geometries overlap
    if (cyl_height >= 2.0*cyl_radius) {
        if (distance < sph_radius + cyl_radius) return 1;
    } else {
        if (distance < sph_radius + 0.5*cyl_height) return 1;
    }
    
    // Projection method
    // Find the distance between cylinder and sphere perpendicular to the cylinder direction.
    Coords difference = coords_sub(sph_center,cyl_center);
    
    // projection is simple as the cylinder direction vector is a normal vector
    Coords projection = coords_scalar_mult(cyl_direction,union_coords_dot(difference,cyl_direction));
    Coords perpendicular = coords_sub(difference,projection);
    
    if (length_of_position_vector(perpendicular) > sph_radius + cyl_radius) return 0;
    
    // Brute force
    // Consider enlarging the sphere slightly to decrease the probability for false negatives
    //  at the cost of some false positives. This is acceptable as false positives will not
    //  have any severe effect, but false negatives causes errors.
    
    // Random tests shows no issues with this approach, false negatives disapeared.
    struct sphere_storage temp_sph_storage;
    temp_sph_storage.sph_radius = 1.02*sph_radius;

    struct geometry_struct temp_sph;
    temp_sph.geometry_parameters.p_sphere_storage = &temp_sph_storage;
    temp_sph.center = geometry_sph->center;
    
    // temp_sph is not fully initialized, it just has geometrical information
    
    struct pointer_to_1d_coords_list shell_points;
    shell_points = geometry_sph->shell_points(&temp_sph,300*300); // using 300 rings with 300 points, works but is slow
    //shell_points = geometry_sph->shell_points(&temp_sph,70*70); // using 50 rings with 50 points
    //shell_points = geometry_sph->shell_points(&temp_sph,50*50); // using 50 rings with 50 points
    
    int iterate;
    for (iterate=0;iterate<shell_points.num_elements;iterate++) {
      if (geometry_cyl->within_function(shell_points.elements[iterate],geometry_cyl) == 1) {
        free(shell_points.elements);
        return 1;
      }
    }
    
    free(shell_points.elements);
    
    shell_points = geometry_cyl->shell_points(geometry_cyl,400); // 200 on each ring
    for (iterate=0;iterate<shell_points.num_elements;iterate++) {
      if (geometry_sph->within_function(shell_points.elements[iterate],&temp_sph) == 1) {
        free(shell_points.elements);
        return 1;
      }
    }
    
    free(shell_points.elements);
    return 0;
    
    
    /*
    // Using the actual sphere size and position
    struct pointer_to_1d_coords_list shell_points;
    shell_points = geometry_sph->shell_points(geometry_sph,250000); // using 500 rings with 500 points
    
    
    int iterate;
    for (iterate=0;iterate<shell_points.num_elements;iterate++) {
      if (geometry_cyl->within_function(shell_points.elements[iterate],geometry_cyl) == 1) {
        free(shell_points.elements);
        return 1;
      }
    }
    
    free(shell_points.elements);
    
    shell_points = geometry_cyl->shell_points(geometry_cyl,400); // 200 on each ring
    for (iterate=0;iterate<shell_points.num_elements;iterate++) {
      if (geometry_sph->within_function(shell_points.elements[iterate],geometry_sph) == 1) {
        free(shell_points.elements);
        return 1;
      }
    }
    
    free(shell_points.elements);
    return 0;
    */
};

int box_overlaps_sphere(struct geometry_struct *geometry_box,struct geometry_struct *geometry_sph) {
    
    //printf("\n checking sphere center in box\n");
    // If the sphere center is inside box, one can exit fast
    Coords sph_center = geometry_sph->center;
    if (geometry_box->within_function(sph_center,geometry_box) == 1) return 1;
    
    //printf("\n checking box center in sphere\n");
    // If the box center is inside sphere, one can exit fast
    Coords box_center = geometry_box->center;
    if (geometry_sph->within_function(box_center,geometry_sph) == 1) return 1;
    
    // Check if box corners are inside the sphere
    int iterate;
    struct pointer_to_1d_coords_list shell_points;
    shell_points = geometry_box->shell_points(geometry_box,8);
    
    for (iterate=0;iterate<shell_points.num_elements;iterate++) {
      if (geometry_sph->within_function(shell_points.elements[iterate],geometry_sph) == 1) {
        free(shell_points.elements);
        return 1;
      }
    }
    free(shell_points.elements);
    
    // Can not find elegant solution to this problem. Will use brute force.
    
    // Before brute forcing, find negative solutions for obvious cases.
    // Use circle - circle overlap algorithm, find bounding circle for box.
    
    //printf("\n checking bounding sphere approach\n");
    Coords corner_ps[8];
    
    double this_length,max_length = 0;
    
    box_corners_local_frame(corner_ps,geometry_box); // Local frame: center in (0,0,0)
    for (iterate=0;iterate<8;iterate++) {
      this_length = length_of_position_vector(corner_ps[iterate]);
      if (this_length > max_length) max_length = this_length;
    }
    // Box has a bounding circle with radius max_length and it's normal center.
    //printf("bounding sphere for box has radius = %f \n",max_length);
    
    double radius = geometry_sph->geometry_parameters.p_sphere_storage->sph_radius;

    // Calculate distance
    double distance = distance_between(geometry_box->center,geometry_sph->center);
    
    
    // Return 0 if the bounding sphere and the sphere do not overlap, otherwise do brute force
    if (distance > radius + max_length) {
      //printf("\n Bounding sphere avoided brute force method in sphere / box overlap\n");
      return 0;
    }
    
    //printf("\n doing brute force method in box overlaps sphere\n");
    // Brute force
    
    // Slightly increase size of the sphere to avoid edgecases, original value already saved
    geometry_sph->geometry_parameters.p_sphere_storage->sph_radius = 1.02*radius;
    
    // Shell points must be free'ed before leaving this function
    shell_points = geometry_sph->shell_points(geometry_sph,100*100); // using 100 rings with 100 points
  
    for (iterate=0;iterate<shell_points.num_elements;iterate++) {
      if (geometry_box->within_function(shell_points.elements[iterate],geometry_box) == 1) {
        free(shell_points.elements);
        geometry_sph->geometry_parameters.p_sphere_storage->sph_radius = radius;
        return 1;
      }
    }
    
    // Reset sphere radius to correct value
    geometry_sph->geometry_parameters.p_sphere_storage->sph_radius = radius;
    
    free(shell_points.elements);
    return 0;
    
};


// sym sphere
int sphere_overlaps_cylinder(struct geometry_struct *geometry_sph,struct geometry_struct *geometry_cyl) {
  return cylinder_overlaps_sphere(geometry_cyl,geometry_sph);
};

int sphere_overlaps_box(struct geometry_struct *geometry_sph,struct geometry_struct *geometry_box) {
  return box_overlaps_sphere(geometry_box,geometry_sph);
};

int cone_overlaps_sphere(struct geometry_struct *geometry_cone,struct geometry_struct *geometry_sph) {
  // Overlap function should return 1 if the to geometries both cover some volume
  // Temporary function
  // Load Variables:
    /*
    Coords direction_1 = geometry_cone->geometry_parameters.p_cone_storage->direction_vector;
    Coords center_1 = geometry_cone->center;
    double radius_top_1 = geometry_cone->geometry_parameters.p_cone_storage->cone_radius_top;
    double radius_bottom_1 = geometry_cone->geometry_parameters.p_cone_storage->cone_radius_bottom;
    double height_1 = geometry_cone->geometry_parameters.p_cone_storage->height;

    Coords direction_2 = geometry_sph->geometry_parameters.p_sphere_storage->direction_vector;
    Coords center_2 = geometry_sph->center;
    double radius_2 = geometry_sph->geometry_parameters.p_sphere_storage->sph_radius;
    */

    double Y;
    double max_r;
    int resolution = 300;


    // This function is a rewritten verstion of the A_within_B.

    // This function assumes the parent (B) is a convex geoemtry
      // If all points on the shell of geometry A is within B, so are all lines between them.

    
    // FIRST CHECK IF POINTS N CONE IS INSIDE SPHERE:

      // resolution selects the number of points to be generated on the shell.
      struct pointer_to_1d_coords_list shell_points;
      shell_points = geometry_cone->shell_points(geometry_cone,resolution);
      // Shell_points.elements need to be freed before leaving this function
    
      if (shell_points.num_elements > resolution || shell_points.num_elements < 0) {
        printf("\nERROR: Shell point function used in A_within_B return garbage num_elements. \n");
        exit(EXIT_FAILURE);
      }
    
      int iterate;

      for (iterate=0;iterate<shell_points.num_elements;iterate++) {
        if (geometry_sph->within_function(shell_points.elements[iterate],geometry_sph) == 1) {
          free(shell_points.elements);
          //printf("\n ONE POINT OF SPH IS INSIDE CONE\n");
          return 1;
        }
      }
    
      free(shell_points.elements);

    // CHECK IF SPHERE POINTS ARE INSIDE CONE

      // resolution selects the number of points to be generated on the shell.
      shell_points = geometry_sph->shell_points(geometry_sph,resolution);
      // Shell_points.elements need to be freed before leaving this function
    
      if (shell_points.num_elements > resolution || shell_points.num_elements < 0) {
        printf("\nERROR: Shell point function used in A_within_B return garbage num_elements. \n");
        exit(EXIT_FAILURE);
      }
    


      for (iterate=0;iterate<shell_points.num_elements;iterate++) {
        if (geometry_cone->within_function(shell_points.elements[iterate],geometry_cone) == 1) {
          free(shell_points.elements);
          //printf("\n ONE POINT OF CONE IS INSIDE SPH\n");
          return 1;
        }
      }
    
      free(shell_points.elements);


    
      // If just one points is inside, the entire geometry is assumed inside as parent should be convex
      return 0;

};

int sphere_overlaps_cone(struct geometry_struct *geometry_sph,struct geometry_struct *geometry_cone) {
  // This problem is symetrical.
  return cone_overlaps_sphere(geometry_cone,geometry_sph);
};

int cone_overlaps_cylinder(struct geometry_struct *geometry_cone,struct geometry_struct *geometry_cylinder) {
  // Overlap function should return 1 if the to geometries both cover some volume
  // This now works for the simple case where the two directions are parallel. Otherwise it uses A within B.

    // Load Variables:
    Coords direction_1 = geometry_cone->geometry_parameters.p_cone_storage->direction_vector;
    Coords center_1 = geometry_cone->center;
    double radius_top_1 = geometry_cone->geometry_parameters.p_cone_storage->cone_radius_top;
    double radius_bottom_1 = geometry_cone->geometry_parameters.p_cone_storage->cone_radius_bottom;
    double height_1 = geometry_cone->geometry_parameters.p_cone_storage->height;

    Coords direction_2 = geometry_cylinder->geometry_parameters.p_cylinder_storage->direction_vector;
    Coords center_2 = geometry_cylinder->center;
    double radius_2 = geometry_cylinder->geometry_parameters.p_cylinder_storage->cyl_radius;
    double height_2 = geometry_cylinder->geometry_parameters.p_cylinder_storage->height;

    double radius_bottom_2 = radius_2;
    double radius_top_2 = radius_2;

    // // Simple test to see if they are far away (with smallest spheres outside)
    // Create Spheres
    
    double dist_above_bottom = 0.5*(radius_top_1*radius_top_1+height_1*height_1-radius_bottom_1*radius_bottom_1)/height_1;
    double dist_from_center = dist_above_bottom - 0.5*height_1;
    Coords sphere_1_pos = coords_set(center_1.x+direction_1.x*dist_from_center,
                                     center_1.y+direction_1.y*dist_from_center,
                                     center_1.z+direction_1.z*dist_from_center);
    double sphere_1_radius = sqrt(radius_bottom_1*radius_bottom_1+dist_above_bottom*dist_above_bottom);

    double sphere_2_radius = sqrt(radius_2*radius_2+height_2*height_2);
    Coords sphere_2_pos = center_2;

    //print_position(sphere_1_pos,"sphere_1 pos");
    //printf("sphere_1 radius = %lf \n", sphere_1_radius);
    //print_position(sphere_2_pos,"sphere_2 pos");
    //printf("sphere_2 radius = %lf \n", sphere_2_radius);
    // Test if spheres are too long apart to have any chance of intersecting

    double dist_spheres = sqrt((sphere_1_pos.x-sphere_2_pos.x)*(sphere_1_pos.x-sphere_2_pos.x)+(sphere_1_pos.y-sphere_2_pos.y)*(sphere_1_pos.y-sphere_2_pos.y)+(sphere_1_pos.z-sphere_2_pos.z)*(sphere_1_pos.z-sphere_2_pos.z));


    if (dist_spheres > sphere_1_radius + sphere_2_radius){
        printf("\nSpherical method determined that cones are too far away for intersection to be relevant\n");
        return 0;
    }

    // // Simple test to see if they are inside (with largest spheres inside)
    // Brute force in two steps.
    // 1. Check if any points on 1 lies within 2
    // 2. Check if any transversal lines on the mesh of 1 intersects with 2

    // Calculate needed information
    Coords cone_1_bottom_point = coords_add(center_1,coords_scalar_mult(direction_1,-0.5*height_1));
    Coords cone_1_top_point = coords_add(center_1,coords_scalar_mult(direction_1,0.5*height_1));
    Coords cone_2_bottom_point = coords_add(center_2,coords_scalar_mult(direction_2,-0.5*height_2));
    Coords cone_2_top_point = coords_add(center_2,coords_scalar_mult(direction_2,0.5*height_2));


    // Create two circles for both geometries
    int resoultuion = 300;

    struct pointer_to_1d_coords_list cone_1_points = geometry_cone->shell_points(geometry_cone,resoultuion);
    

    //points_on_circle(cone_1_top,cone_1_top_point,direction_1,radius_top_1,resoultuion);
    //points_on_circle(cone_1_bottom,cone_1_bottom_point,direction_1,radius_bottom_1,resoultuion);


    //printf("\nTEST\n");
    int i;
    // Test geometry 1 points inside geometry 2

    for (i = 0 ; i < cone_1_points.num_elements ; i++){
        
        if (r_within_cylinder(cone_1_points.elements[i],geometry_cylinder) == 1){
            //printf("\nOne point on cone 1 is inside cone 2\n");
            return 1;
        }
    }

    struct pointer_to_1d_coords_list cone_2_points = geometry_cylinder->shell_points(geometry_cylinder,resoultuion);

    // Test geometry 2 points inside geometry 1
    for (i = 0 ; i < cone_2_points.num_elements ; i++){
        
        if (r_within_cone(cone_2_points.elements[i],geometry_cone) == 1){
            //printf("\nOne point on cone 2 is inside cone 1\n");
            return 1;
        }
    }


    // Test 1 within 2


    // // Test if there is any intersection (intersection function or eqation?)

    // This is an implementation of brute force. Maybe do this with a calculated function?
    int circ_resolution = 150;  // how many lines will the be checked for
    int height_resolution = 300;

    double length_of_cone_side_1 = sqrt(pow(radius_top_1-radius_bottom_1,2)+pow(height_1,2));
    double length_of_cone_side_2 = sqrt(pow(radius_top_2-radius_bottom_2,2)+pow(height_2,2));

    double slope_1 = (radius_top_1-radius_bottom_1)/height_1;
    double slope_2 = (radius_top_2-radius_bottom_2)/height_2;

    double local_radius;

    Coords cone_1_direction = geometry_cone->geometry_parameters.p_cone_storage->direction_vector;
    Coords cone_2_direction = geometry_cylinder->geometry_parameters.p_cylinder_storage->direction_vector;

    //printf("\nlength_of_cone_side_1 = %f\n",length_of_cone_side_1);

    Coords circ_points[150];
    double circ_offset;
    Coords circ_center;

    int j;

    for (i = 0 ; i < height_resolution ; i++){
        // Calculate circ offset
        circ_offset = i * height_1 / height_resolution;

        // Calculate middle point
        circ_center = coords_add(cone_1_bottom_point,coords_set(cone_1_direction.x * circ_offset,cone_1_direction.y * circ_offset,cone_1_direction.z * circ_offset));

        // Calculate radius
        local_radius = circ_offset * slope_1 + radius_bottom_1;

        // Make points on circle
        //printf("points on circle: circ_center = [%f,%f,%f] , cone_1_direction = [%f,%f,%f] , local_radius = %f , circ_resolution = %i",circ_center.x,circ_center.y,circ_center.z,cone_1_direction.x,cone_1_direction.y,cone_1_direction.z,local_radius,circ_resolution);
        points_on_circle(circ_points,circ_center,cone_1_direction,local_radius,circ_resolution);

        // Test if any points lies within geomtry 2
        for (j = 0 ; j < circ_resolution; j++){
            //printf("\ntested if point [%i] [%f,%f,%f] is inside",j,circ_points[j].x,circ_points[j].y,circ_points[j].z);
            if (r_within_cylinder(circ_points[j],geometry_cylinder) == 1){
                //printf("\nOne point on cone 1 is inside cone 2\n");
                return 1;
            }
        }
    }


    for (i = 0 ; i < height_resolution ; i++){
        // Calculate circ offset
        circ_offset = i * height_2 / height_resolution;

        // Calculate middle point
        circ_center = coords_add(cone_2_bottom_point,coords_set(cone_2_direction.x * circ_offset,cone_2_direction.y * circ_offset,cone_2_direction.z * circ_offset));

        // Calculate radius
        local_radius = circ_offset * slope_2 + radius_bottom_2;

        // Make points on circle
        //printf("points on circle: circ_center = [%f,%f,%f] , cone_1_direction = [%f,%f,%f] , local_radius = %f , circ_resolution = %i",circ_center.x,circ_center.y,circ_center.z,cone_2_direction.x,cone_2_direction.y,cone_2_direction.z,local_radius,circ_resolution);
        points_on_circle(circ_points,circ_center,cone_2_direction,local_radius,circ_resolution);

        // Test if any points lies within geomtry 2
        for (j = 0 ; j < circ_resolution; j++){
            //printf("\ntested if point [%i] [%f,%f,%f] is inside",j,circ_points[j].x,circ_points[j].y,circ_points[j].z);
            if (r_within_cone(circ_points[j],geometry_cone) == 1){
                //printf("\nOne point on cone 2 is inside cone 1\n");
                return 1;
            }
        }
    }

    return 0;

};

int cylinder_overlaps_cone(struct geometry_struct *geometry_cylinder,struct geometry_struct *geometry_cone) {
  // This problem is symetrical.
  return cone_overlaps_cylinder(geometry_cone,geometry_cylinder);
};

int cone_overlaps_box(struct geometry_struct *geometry_cone,struct geometry_struct *geometry_box) {
  // Overlap function should return 1 if the to geometries both cover some volume
    //  cone_overlaps_box(struct geometry_struct *geometry_cone,struct geometry_struct *geometry_box)

    // Load Variables:
    Coords direction_cone = geometry_cone->geometry_parameters.p_cone_storage->direction_vector;
    Coords center_cone = geometry_cone->center;
    double radius_top_cone = geometry_cone->geometry_parameters.p_cone_storage->cone_radius_top;
    double radius_bottom_cone = geometry_cone->geometry_parameters.p_cone_storage->cone_radius_bottom;
    double height_cone = geometry_cone->geometry_parameters.p_cone_storage->height;

    //Coords normal_vectors_box[6] = geometry_box->geometry_parameters.p_box_storage->normal_vectors;
    int is_rectangle =  geometry_box->geometry_parameters.p_box_storage->is_rectangle;
    double x_width1 = geometry_box->geometry_parameters.p_box_storage->x_width1;
    double y_height1 = geometry_box->geometry_parameters.p_box_storage->y_height1;
    double z_depth= geometry_box->geometry_parameters.p_box_storage->z_depth;
    double x_width2 = geometry_box->geometry_parameters.p_box_storage->x_width2;
    double y_height2 = geometry_box->geometry_parameters.p_box_storage->y_height2;
    Coords x_vector = geometry_box->geometry_parameters.p_box_storage->x_vector;
    Coords y_vector = geometry_box->geometry_parameters.p_box_storage->y_vector;
    Coords z_vector = geometry_box->geometry_parameters.p_box_storage->z_vector;
    Coords center_box = geometry_box->center;
    //Coords direction_box = geometry_cone->geometry_parameters.p_box_storage->direction;



    double Y;
    double max_r;


    // // Simple test to see if they are far away (with smallest spheres outside)
    // Create Spheres

    /*
    Y = -(0.5*height_cone)-(radius_top_cone*radius_top_cone-radius_bottom_cone*radius_bottom_cone)/(2*height_cone);
    if (radius_top_cone > radius_bottom_cone){
        max_r = radius_top_cone;
    }else{
        max_r = radius_bottom_cone;
    }
    double sphere_1_radius =  sqrt((Y+(1/2)*height_cone)*(Y+(1/2)*height_cone)+max_r*max_r);
    Coords sphere_1_pos = coords_set(center_cone.x+direction_cone.x*Y,center_cone.y+direction_cone.y*Y,center_cone.z+direction_cone.z*Y);
    */

    double dist_above_bottom = 0.5*(radius_top_cone*radius_top_cone+height_cone*height_cone-radius_bottom_cone*radius_bottom_cone)/height_cone;
    double dist_from_center = dist_above_bottom - 0.5*height_cone;
    Coords sphere_1_pos = coords_set(center_cone.x+direction_cone.x*dist_from_center,
                                     center_cone.y+direction_cone.y*dist_from_center,
                                     center_cone.z+direction_cone.z*dist_from_center);
    double sphere_1_radius = sqrt(radius_bottom_cone*radius_bottom_cone+dist_above_bottom*dist_above_bottom);


    double dist_to_corner;
    double sphere_2_radius = 0;

    dist_to_corner = sqrt(pow(x_width1,2)+pow(x_width1,2));
    if (dist_to_corner > sphere_2_radius) { sphere_2_radius = dist_to_corner ; }
    dist_to_corner = sqrt(pow(x_width1,2)+pow(x_width2,2));
    if (dist_to_corner > sphere_2_radius) { sphere_2_radius = dist_to_corner ; }
    dist_to_corner = sqrt(pow(x_width2,2)+pow(x_width1,2));
    if (dist_to_corner > sphere_2_radius) { sphere_2_radius = dist_to_corner ; }
    dist_to_corner = sqrt(pow(x_width2,2)+pow(x_width2,2));
    if (dist_to_corner > sphere_2_radius) { sphere_2_radius = dist_to_corner ; }

    Coords sphere_2_pos = center_box;


    // Test if spheres are too long apart to have any chance of intersecting

    double dist_spheres = sqrt((sphere_1_pos.x-sphere_2_pos.x)*(sphere_1_pos.x-sphere_2_pos.x)+(sphere_1_pos.y-sphere_2_pos.y)*(sphere_1_pos.y-sphere_2_pos.y)+(sphere_1_pos.z-sphere_2_pos.z)*(sphere_1_pos.z-sphere_2_pos.z));


    if (dist_spheres > sphere_1_radius + sphere_2_radius){
        //printf("\nSpherical method determined that cones are too far away for intersection to be relevant\n");
        return 0;
    }

    // // Simple test to see if they are inside (with largest spheres inside)
    // Brute force in two steps.
    // 1. Check if any points on 1 lies within 2
    // 2. Check if any transversal lines on the mesh of 1 intersects with 2

    // Calculate needed information
    Coords cone_bottom_point = coords_add(center_cone,coords_scalar_mult(direction_cone,-0.5*height_cone));
    Coords cone_top_point = coords_add(center_cone,coords_scalar_mult(direction_cone,0.5*height_cone));



    // Create two circles for both geometries
    int resoultuion = 300;



    struct pointer_to_1d_coords_list cone_points = geometry_cone->shell_points(geometry_cone,resoultuion);
    struct pointer_to_1d_coords_list box_points = geometry_box->shell_points(geometry_box,resoultuion);

    //points_on_circle(cone_1_top,cone_top_point,direction_cone,radius_top_cone,resoultuion);
    //points_on_circle(cone_1_bottom,cone_bottom_point,direction_cone,radius_bottom_cone,resoultuion);


    //printf("\nTEST\n");
    int i;
    // Test cone points inside box

    for (i = 0 ; i < cone_points.num_elements ; i++){
        
        if (r_within_box_advanced(cone_points.elements[i],geometry_box) == 1){
            //printf("\nOne point on cone is inside box\n");
            return 1;
        }
    }

    // Test box points inside cone
    for (i = 0 ; i < box_points.num_elements ; i++){
        
        if (r_within_cone(box_points.elements[i],geometry_cone) == 1){
            //printf("\nOne point on box is inside cone\n");
            return 1;
        }
    }


    // Test 1 within 2


    // // Test if there is any intersection (intersection function or eqation?)


    // // Add more points
    // This is an implementation of brute force. Maybe do this with a calculated function?
    int circ_resolution = 50;  // how many lines will the be checked for
    int height_resolution = 150;

    double length_of_cone_side = sqrt(pow(radius_top_cone-radius_bottom_cone,2)+pow(height_cone,2));
    double length_of_box_side = z_depth;

    double slope_1 = (radius_top_cone-radius_bottom_cone)/height_cone;

    double local_radius;

    Coords cone_direction = geometry_cone->geometry_parameters.p_cone_storage->direction_vector;
    //Coords box_direction = geometry_box->geometry_parameters.p_box_storage->direction_vector;

    //printf("\nlength_of_cone_side = %f\n",length_of_cone_side);

    Coords circ_points[50];
    double circ_offset;
    Coords circ_center;

    Coords square_points[8];
    double square_offset;
    // PW FIXME: square_center needs init - probably no to 0, but certainly not to "random stuff on memory"
    Coords square_center=coords_set(0,0,0);
    Coords box_end_point = coords_sub(coords_set(0,0,-z_depth/2),square_center);

    int j;

    for (i = 0 ; i < height_resolution ; i++){
        // Calculate circ offset
        //circ_offset = i * length_of_cone_side / height_resolution; // Possible bug
        circ_offset = i * height_cone / height_resolution; // Possible bug

        // Calculate middle point
        circ_center = coords_add(cone_bottom_point,coords_set(cone_direction.x * circ_offset,cone_direction.y * circ_offset,cone_direction.z * circ_offset));

        // Calculate radius
        local_radius = circ_offset * slope_1 + radius_bottom_cone;

        // Make points on circle
        //printf("points on circle: circ_center = [%f,%f,%f] , cone_direction = [%f,%f,%f] , local_radius = %f , circ_resolution = %i",circ_center.x,circ_center.y,circ_center.z,cone_direction.x,cone_direction.y,cone_direction.z,local_radius,circ_resolution);
        points_on_circle(circ_points,circ_center,cone_direction,local_radius,circ_resolution);

        // Test if any points lies within geomtry 2
        for (j = 0 ; j < circ_resolution; j++){
            //printf("\ntested if point [%i] [%f,%f,%f] is inside",j,circ_points[j].x,circ_points[j].y,circ_points[j].z);
            if (r_within_box_advanced(circ_points[j],geometry_box) == 1){
                //printf("\nOne point on cone 1 is inside cone 2\n");
                return 1;
            }
        }
    }

    double box_offset;

    for (i = 0 ; i < height_resolution ; i++){
        // Calculate circ offset
        box_offset = i * length_of_box_side / height_resolution;

        // Calculate middle point
        square_center = coords_add(box_end_point,coords_set(z_vector.x * box_offset,z_vector.y * box_offset,z_vector.z * box_offset));

        // Calculate radius

        // Make points on square
        square_points[0]=coords_add(square_center,coords_set(x_width1/2,0,0)); // A point on the side of the box
        square_points[1]=coords_add(square_points[0],coords_set(0,y_height1/2,0)); // Corner
        square_points[2]=coords_add(square_points[0],coords_set(0,-y_height1/2,0)); // Corner
        square_points[3]=coords_add(square_center,coords_set(-x_width1/2,0,0)); // A point on the side of the box
        square_points[4]=coords_add(square_points[3],coords_set(0,y_height1/2,0)); // Corner
        square_points[5]=coords_add(square_points[3],coords_set(0,-y_height1/2,0)); // Corner
        
        square_points[6]=coords_add(square_center,coords_set(0,y_height1/2,0)); // A point on the side of
        square_points[7]=coords_add(square_center,coords_set(0,-y_height1/2,0)); // A point on the side of

        // Test if any points lies within geomtry 2
        for (j = 0 ; j < 3; j++){
            
            if (r_within_cone(square_points[j],geometry_cone) == 1){
                //printf("\nOne point on cone 2 is inside cone 1\n");
                return 1;
            }
        }
    }
    
    return 0;

};

int box_overlaps_cone(struct geometry_struct *geometry_box,struct geometry_struct *geometry_cone) {
  // This problem is symetrical.
  return cone_overlaps_box(geometry_cone,geometry_box);
}

int mesh_overlaps_box(struct geometry_struct *geometry1, struct geometry_struct *geometry2){
   return mesh_overlaps_mesh(geometry1, geometry2);
}
int mesh_overlaps_cone(struct geometry_struct *geometry1, struct geometry_struct *geometry2){
   return mesh_overlaps_mesh(geometry1, geometry2);
}
int mesh_overlaps_sphere(struct geometry_struct *geometry1,struct geometry_struct *geometry2) {
   return mesh_overlaps_mesh(geometry1, geometry2);
};
int mesh_overlaps_cylinder(struct geometry_struct *geometry1,struct geometry_struct *geometry2) {
   return mesh_overlaps_mesh(geometry1, geometry2);
};
int box_overlaps_mesh(struct geometry_struct *geometry1, struct geometry_struct *geometry2){
   return mesh_overlaps_mesh(geometry1, geometry2);
}
int cone_overlaps_mesh(struct geometry_struct *geometry1, struct geometry_struct *geometry2){
   return mesh_overlaps_mesh(geometry1, geometry2);
}
int sphere_overlaps_mesh(struct geometry_struct *geometry1,struct geometry_struct *geometry2) {
   return mesh_overlaps_mesh(geometry1, geometry2);
};
int cylinder_overlaps_mesh(struct geometry_struct *geometry1,struct geometry_struct *geometry2) {
   return mesh_overlaps_mesh(geometry1, geometry2);
};
// -------------    Within functions for two different geometries ---------------------------------

double dist_from_point_to_plane(Coords point,Coords plane_p1, Coords plane_p2, Coords plane_p3) {

  /*
  printf("Dist from point to plane stuff ---- \n");
  print_position(point,"point");
  print_position(plane_p1,"plane_p1");
  print_position(plane_p2,"plane_p2");
  print_position(plane_p3,"plane_p3");
  */
  // transform three points into normal vector
  Coords vector_1 = coords_sub(plane_p2,plane_p1);
  Coords vector_2 = coords_sub(plane_p3,plane_p1);
  
  Coords normal_vector;
  
  vec_prod(normal_vector.x,normal_vector.y,normal_vector.z,vector_1.x,vector_1.y,vector_1.z,vector_2.x,vector_2.y,vector_2.z);
  
  double denominator = length_of_position_vector(normal_vector);
  
  normal_vector = coords_scalar_mult(normal_vector,1.0/denominator);

  //print_position(normal_vector,"normal vector in dist from point to plane");
  
  Coords diff = coords_sub(point,plane_p1);
  
  return fabs(scalar_prod(normal_vector.x,normal_vector.y,normal_vector.z,diff.x,diff.y,diff.z));
};

int box_within_cylinder(struct geometry_struct *geometry_child,struct geometry_struct *geometry_parent) {
    // Is geometry child inside geometry parent?
    // For box child to be inside of cylinder parent, all corners of box child must be inside of box parent.
    
    // Generate coordinates of corners of the box
    Coords corner_points[8];
    box_corners_global_frame(corner_points,geometry_child);
    
    // Check earch corner seperatly
    int iterate;
    for (iterate=0;iterate<8;iterate++) {
        if (geometry_parent->within_function(corner_points[iterate],geometry_parent) == 0) {
            return 0; // If a corner is outside, box child is not within cylinder parent
        }
    }
    return 1; // If no corner was outside, the box is inside the cylinder
};

int cylinder_within_box(struct geometry_struct *geometry_child,struct geometry_struct *geometry_parent) {
    // Is geometry child inside geometry parent?
    // For box child to be inside of cylinder parent, all corners of box child must be inside of box parent.
    
    Coords cyl_direction = geometry_child->geometry_parameters.p_cylinder_storage->direction_vector;
    Coords center = geometry_child->center;
    double radius = geometry_child->geometry_parameters.p_cylinder_storage->cyl_radius;
    double height = geometry_child->geometry_parameters.p_cylinder_storage->height;
    
    Coords cyl_top_point = coords_add(center,coords_scalar_mult(cyl_direction,0.5*height));
    Coords cyl_bottom_point = coords_add(center,coords_scalar_mult(cyl_direction,-0.5*height));
    
    // quick escape: if end points of cylinder not in box, return 0
    if (geometry_parent->within_function(cyl_top_point,geometry_parent) == 0) return 0;
    if (geometry_parent->within_function(cyl_bottom_point,geometry_parent) == 0) return 0;
    
    // Generate 30 points on the circle describing the top of the cylinder
    Coords *circle_point_array;
    int number_of_points = 30;
    circle_point_array = malloc(number_of_points * sizeof(Coords));
    if (!circle_point_array) {
      fprintf(stderr,"Failure allocating list in Union function cylinder_within_box - Exit!\n");
      exit(EXIT_FAILURE);
    }
    points_on_circle(circle_point_array,cyl_top_point,cyl_direction,radius,number_of_points);
    
    // Check parts of cylinder top seperatly
    int iterate;
    for (iterate=0;iterate<number_of_points;iterate++) {
        if (geometry_parent->within_function(circle_point_array[iterate],geometry_parent) == 0) {
            return 0; // If part of the cylinder is outside the box, the cylinder is not inside the box
        }
    }
    
    // Check parts of cylinder bottom seperatly
    points_on_circle(circle_point_array,cyl_bottom_point,cyl_direction,radius,number_of_points);
    for (iterate=0;iterate<number_of_points;iterate++) {
        if (geometry_parent->within_function(circle_point_array[iterate],geometry_parent) == 0) {
            return 0; // If part of the cylinder is outside the box, the cylinder is not inside the box
        }
    }
    
    free(circle_point_array);
    
    return 1; // If no part of the cylinders end caps was outside, the cylinder is inside box 1
};

int cylinder_within_sphere(struct geometry_struct *geometry_child,struct geometry_struct *geometry_parent) {
    // Is a cylinder within a sphere?
    
    double cyl_radius = geometry_child->geometry_parameters.p_cylinder_storage->cyl_radius;
    double cyl_height = geometry_child->geometry_parameters.p_cylinder_storage->height;
    double sph_radius = geometry_parent->geometry_parameters.p_sphere_storage->sph_radius;
    
    // Quick checks to avoid overhead from A_within_B
    // Is the height of the cylinder larger than diameter of the sphere?
    if (cyl_height > 2.0*sph_radius) return 0;
    
    // Is the radius of the cylidner larger than the radius of the sphere?
    if (cyl_radius > sph_radius) return 0;
    
    // Is the center of the cylinder so far from the center of the sphere that it cant fit?
    double distance = distance_between(geometry_child->center,geometry_parent->center);
    if (0.5*cyl_height > cyl_radius) {
        if (sqrt(distance*distance + 0.25*cyl_height*cyl_height) > sph_radius)
            return 0;
    } else {
        if (sqrt(distance*distance + cyl_radius*cyl_radius) > sph_radius)
            return 0;
    }
    
    // Reasonable to brute force solution here
    return A_within_B(geometry_child,geometry_parent,(int) 400); // 200 points on each end cap
};

int sphere_within_cylinder(struct geometry_struct *geometry_child,struct geometry_struct *geometry_parent) {
    // Is a sphere (child) within a cylinder (parent)?
    
    // If the center is not inside, one can exit fast
    Coords sph_center = geometry_child->center;
    if (geometry_parent->within_function(sph_center,geometry_parent) == 0) return 0;
    
    // Generate cylinder with height = height - r_s and r_c = r_c - 2*r_s and check if point is within.
    
    // Done by modifying parent cylinder.
    double original_radius = geometry_parent->geometry_parameters.p_cylinder_storage->cyl_radius;
    double original_height = geometry_parent->geometry_parameters.p_cylinder_storage->height;
    
    // Need sphere
    double sph_radius = geometry_child->geometry_parameters.p_sphere_storage->sph_radius;
    
    if (original_radius - sph_radius > 0 && original_height - 2.0*sph_radius > 0) {
      geometry_parent->geometry_parameters.p_cylinder_storage->cyl_radius = original_radius - sph_radius;
      geometry_parent->geometry_parameters.p_cylinder_storage->height = original_height - 2.0*sph_radius;
    } else return 0;
    
    int return_value = geometry_parent->within_function(sph_center,geometry_parent);
    
    // Reset the cylinder to it's original values (important not to return before)
    geometry_parent->geometry_parameters.p_cylinder_storage->cyl_radius = original_radius;
    geometry_parent->geometry_parameters.p_cylinder_storage->height = original_height;
    
    return return_value;
};

int box_within_sphere(struct geometry_struct *geometry_child,struct geometry_struct *geometry_parent) {
    // If all 8 corners of the box are inside the sphere, the entire box is inside
    
    return A_within_B(geometry_child,geometry_parent,8);
};

int sphere_within_box(struct geometry_struct *geometry_child,struct geometry_struct *geometry_parent) {
    // Distance from any of the box sides must be greater than radius, and the center inside.
    
    // debug test, use A_within_B
    //return A_within_B(geometry_child,geometry_parent,100*100);
    
    // If the center is not inside, one can exit fast
    Coords sph_center = geometry_child->center;
    if (geometry_parent->within_function(sph_center,geometry_parent) == 0) {
      //printf("sphere not child of box because it's center is not in the box \n");
      return 0;
    }
    
    double radius = geometry_child->geometry_parameters.p_sphere_storage->sph_radius;
    
    // 6 planes
    // +z -z easy as are parallel and simple in the box's coordinate system
    Coords coordinates = coords_sub(sph_center,geometry_parent->center);
    
    // Rotate the position around the center of the box
    Coords rotated_coordinates;
    rotated_coordinates = rot_apply(geometry_parent->transpose_rotation_matrix,coordinates);
    
    double depth = geometry_parent->geometry_parameters.p_box_storage->z_depth;
    if (rotated_coordinates.z < -0.5*depth+radius || rotated_coordinates.z > 0.5*depth-radius) {
      //printf("sphere not child of box because it's center to close to z plane \n");
      return 0;
    }
    
    Coords corner_ps[8];
    box_corners_global_frame(corner_ps,geometry_parent);
    
    // The first 4 points are in the -z plane, the last 4 in the +z plane.
    
    // In the -z plane, 0 has neighbors 1 and 3, in the opposite 4
    // In the -z plane, 2 has neighbors 1 and 3, in the opposite 6
    
    // Then these are the four necessary calls for the two plans described by each group.
    double debug_dist;
    if ((debug_dist = dist_from_point_to_plane(sph_center,corner_ps[0],corner_ps[4],corner_ps[1])) < radius ) {
      //printf("sphere not child of box because it's center too close to plane 1, as distance was %f\n",debug_dist);
      return 0;
    }
    if ((debug_dist = dist_from_point_to_plane(sph_center,corner_ps[0],corner_ps[4],corner_ps[3])) < radius ) {
      //printf("sphere not child of box because it's center too close to plane 2, as distance was %f\n",debug_dist);
      return 0;
    }
    if ((debug_dist = dist_from_point_to_plane(sph_center,corner_ps[2],corner_ps[6],corner_ps[1])) < radius ) {
      //printf("sphere not child of box because it's center too close to plane 3, as distance was %f\n",debug_dist);
      return 0;
    }
    if ((debug_dist = dist_from_point_to_plane(sph_center,corner_ps[2],corner_ps[6],corner_ps[3])) < radius ) {
      //printf("sphere not child of box because it's center too close to plane 4, as distance was %f\n",debug_dist);
      return 0;
    }
    
    return 1; // If the cylinder center is inside, and more than radius away from all walls, it is inside
};

int cone_within_sphere(struct geometry_struct *geometry_child,struct geometry_struct *geometry_parent) {
    // Function returns 1 if the cone is completely within the sphere, 0 otherwise
    // Brute force place holder
    return A_within_B(geometry_child,geometry_parent,(int) 300); // 150 points on each end cap
};

int cone_within_cylinder(struct geometry_struct *geometry_child,struct geometry_struct *geometry_parent) {
    // Function returns 1 if the cone is completely within the cylinder, 0 otherwise
    // Brute force place holder
    //return A_within_B(geometry_child,geometry_parent,(int) 60); // 30 points on each end cap
    // This now works for the simple case where the two directions are parallel. Otherwise it uses A within B.

    // Unpack parameters
    double radius1 = geometry_parent->geometry_parameters.p_cylinder_storage->cyl_radius;
    double height1 = geometry_parent->geometry_parameters.p_cylinder_storage->height;
    double radius2_top = geometry_child->geometry_parameters.p_cone_storage->cone_radius_top;
    double radius2_bottom = geometry_child->geometry_parameters.p_cone_storage->cone_radius_bottom;
    double height2 = geometry_child->geometry_parameters.p_cone_storage->height;
    
    int verbal = 0;
    // Generate unit direction vector along center axis of cylinders
    
    // Start with vector that points along the cylinder in the simple frame, and rotate to global
    Coords simple_vector = coords_set(0,1,0);
    Coords vector1,vector2;
    if (verbal == 1) printf("Cords start_vector = (%f,%f,%f)\n",simple_vector.x,simple_vector.y,simple_vector.z);

    // Rotate the position of the ray around the center of the cylinder
    vector1 = rot_apply(geometry_parent->rotation_matrix,simple_vector);
    if (verbal == 1) printf("Cords vector1 = (%f,%f,%f)\n",vector1.x,vector1.y,vector1.z);
    // Rotate the position of the ray around the center of the cylinder
    vector2 = rot_apply(geometry_child->rotation_matrix,simple_vector);
    if (verbal == 1) printf("Cords vector2 = (%f,%f,%f)\n",vector2.x,vector2.y,vector2.z);
    
    // if vector1 and vector2 are parallel, the problem is simple, but if not complicated
    double cross_product1[3] = {0,0,0};
    // printf("%f\n",cross_product1[0]);
    // vec prod(&ax,&ay,&az,bx,by,bz, cx,cy,cz)
    vec_prod(cross_product1[0],cross_product1[1],cross_product1[2],vector1.x,vector1.y,vector1.z,vector2.x,vector2.y,vector2.z);
    // I get an error taking the adress of cross_product1[0], &cross_product1[0]. Took the pointer adresses instead. Works fine.
    if (verbal == 1) printf("cross_product = (%f,%f,%f)\n",cross_product1[0],cross_product1[1],cross_product1[2]);
    double cross_product_length = length_of_3vector(cross_product1);
    
    if (cross_product_length == 0) {
        // The cylinders are parallel.
        int seperated = 0;
        double delta[3];
        delta[0] = geometry_parent->center.x - geometry_child->center.x;
        delta[1] = geometry_parent->center.y - geometry_child->center.y;
        delta[2] = geometry_parent->center.z - geometry_child->center.z;

        // Test for separation by height
        // if (h0Div2 + h1Div2 − |Dot(W0, Delta )| < 0) seperated = 1;
        
        if (verbal == 1) printf("vector1 = (%f,%f,%f)\n",vector1.x,vector1.y,vector2.z);
        if (verbal == 1) printf("delta1 = (%f,%f,%f)\n",delta[0],delta[1],delta[2]);
        double scalar_prod1 = scalar_prod(vector1.x,vector1.y,vector1.z,delta[0],delta[1],delta[2]);
        if (verbal == 1) printf("scalar product = %f \n",scalar_prod1);
        if (verbal == 1) printf("height 1 = %f, height 2 = %f \n",height1,height2);
        
        int inside = 1;
        
        if (height1*0.5 < height2*0.5 + fabs(scalar_prod1)) {
                if (verbal == 1) printf("Cylinder sticks out height wise \n");
                inside = 0;
                }
    
        // Test for separation radially
        // if (rSum − |Delta − Dot(W0,Delta)∗W0| < 0) seperated = 1;
        double vector_between_cyl_axis[3];
        vector_between_cyl_axis[0] = delta[0] - scalar_prod1*vector1.x;
        vector_between_cyl_axis[1] = delta[1] - scalar_prod1*vector1.y;
        vector_between_cyl_axis[2] = delta[2] - scalar_prod1*vector1.z;
        if (verbal == 1) printf("vector_between = (%f,%f,%f)\n",vector_between_cyl_axis[0],vector_between_cyl_axis[1],vector_between_cyl_axis[2]);
        if (verbal == 1) printf("length of vector between = %f\n",length_of_3vector(vector_between_cyl_axis));
        if (verbal == 1) printf("radius1 = %f , radius2_top=%f , radius2_bottom=%f\n",radius1,radius2_top,radius2_bottom);


        if (radius1 < fmax(radius2_top,radius2_bottom) + length_of_3vector(vector_between_cyl_axis)) {
                if (verbal == 1) printf("Cylinder sticks out radially \n");
                inside = 0;
                }
        
        if (inside == 0) return 0;
        else return 1;
        
    } else {
        
    // Make shell points and check if they are inside
    return A_within_B(geometry_child,geometry_parent,(int) 200); // 100 points on each end cap

    }

};

int cone_within_box(struct geometry_struct *geometry_child,struct geometry_struct *geometry_parent) {
    // Function returns 1 if the cone is completely within the box, 0 otherwise
    // Brute force place holder
    return A_within_B(geometry_child,geometry_parent,(int) 300); // 150 points on each end cap
};

int sphere_within_cone(struct geometry_struct *geometry_child,struct geometry_struct *geometry_parent) {
    // Function returns 1 if the sphere is completely within the cone, 0 otherwise
    // Brute force place holder
    return A_within_B(geometry_child,geometry_parent,(int) 300); // 150 points on each end cap
};

int cylinder_within_cone(struct geometry_struct *geometry_child,struct geometry_struct *geometry_parent) {
    // Function returns 1 if the cylinder is completely within the cone, 0 otherwise
    // Brute force place holder
    return A_within_B(geometry_child,geometry_parent,(int) 300); // 150 points on each end cap
};

int box_within_cone(struct geometry_struct *geometry_child,struct geometry_struct *geometry_parent) {
    // Function returns 1 if the box is completely within the cone, 0 otherwise
    // Brute force place holder
    return A_within_B(geometry_child,geometry_parent,(int) 300); // 150 points on each end cap
};


// Flexible intersection function
int intersect_function(double *t, double *nx, double *ny, double *nz, int *surface_index, int *num_solutions, double *r, double *v, struct geometry_struct *geometry) {
    int output = 0;
    switch(geometry->eShape) {
        case box:
            if (geometry->geometry_parameters.p_box_storage->is_rectangle == 1) {
                output = sample_box_intersect_simple(t, nx, ny, nz, surface_index, num_solutions, r, v, geometry);
			} else {
                output = sample_box_intersect_advanced(t, nx, ny, nz, surface_index, num_solutions, r, v, geometry);
			}
            break;
        case sphere:
            output = sample_sphere_intersect(t, nx, ny, nz, surface_index, num_solutions, r, v, geometry);
            break;
        case cylinder:
            output = sample_cylinder_intersect(t, nx, ny, nz, surface_index, num_solutions, r, v, geometry);
            break;
        case cone:
            output = sample_cone_intersect(t, nx, ny, nz, surface_index, num_solutions, r, v, geometry);
            break;
        #ifndef OPENACC
        case mesh:
            output = sample_mesh_intersect(t, nx, ny, nz, surface_index, num_solutions, r, v, geometry);
            break;
        #endif
        default:
            printf("Intersection function: No matching geometry found!");
            break;
    }
	
    return output;
};

// Flexible within function
int r_within_function(Coords pos,struct geometry_struct *geometry) {
    int output = 0;
    switch(geometry->eShape) {
        case box:
            if (geometry->geometry_parameters.p_box_storage->is_rectangle == 1)
                output = r_within_box_simple(pos, geometry);
            else
                output = r_within_box_advanced(pos, geometry);
            break;
        case sphere:
            output = r_within_sphere(pos, geometry);
            break;
        case cylinder:
            output = r_within_cylinder(pos, geometry);
            break;
        case cone:
            output = r_within_cone(pos, geometry);
            break;
        #ifndef OPENACC
        case mesh:
            output = r_within_mesh(pos, geometry);
            break;
        #endif
        case surroundings:
            output = 1;
            break;
        default:
            printf("Within function: No matching geometry found!");
            break;
    }
    
    return output;
};


// -------------    List generator functions   --------------------------------------------------


int within_which_volume(Coords pos, struct pointer_to_1d_int_list input_list, struct pointer_to_1d_int_list destinations_list, struct Volume_struct **Volumes, struct pointer_to_1d_int_list *mask_status_list, int number_of_volumes, int *volume_logic_copy, int *ListA, int *ListB) {
    // This function identifies in which of the volumes of the input list the position pos lies in.
    // pos: position for which the current volume should be found for
    // input list: list of potential volumes, reduced to the ones without parents (their children will be checked)
    // OLD VERSION: volume logic: A logic list of allowed volumes for lookup, volume 1 3 and 5 in a case of 10 volumes would be [0 1 0 1 0 1 0 0 0 0]
    // destinations_list: list of allowed destinations (the original destinations list)
    // Volumes: Main volumes array
    // volume_logic_copy: A pointer to a integer array with at least length "number_of_volumes" (pre alocated for speed)
    // ListA: A pointer to a integer array with at least length "number_of_volumes" (pre alocated for speed)
    // ListB: A pointer to a integer array with at least length "number_of_volumes" (pre alocated for speed)
    
    // Algorithm description
    // Check all of input list for pos being within them, those that have it within them are:
    //      checked against the current highest priority
    //          if higher, is the new highest priority and the new pick for volume
    //      have all their direct children added to the next list to be checked (but any volume can only be added to that list once)
    // Once a run have been made where the next list to check is empty, the answer for new pick for volume is taken.
    
    // Mask update:
    // Algorithm description
    // Check all of input list for pos being within them, those that have it within them are:
    //      checked against the current highest priority and mask status
    //          if higher, this is the new highest priority and the new pick for volume
    //      have all their direct children added to the next list to be checked (but any volume can only be added to that list once)
    // Once a run have been made where the next list to check is empty, the answer for new pick for volume is taken.
    
    // The advantage of the method is that potentially large numbers of children are skipped when their parents do not contain the position.
    // The overhead cost is low, as all the lists are prealocated.
    // Should be checked which of the two implementations is faster, as this is much more complicated than simply checking all possibilities.
    // No within_function call should be made twice, as the same volume number will not be checked twice because of the properties of the direct_children list and the volume_logic that removes duplicates on each level.
    
    
    // This function uses too much memory, the memory required for the volume logic list is n_volumes^2 ints, or for a MACS monochromator 127000 ints.
    // Instead the original destinations list must be used, and a function for quick lookup in a (sorted) destinations list made.
    // Still need a list of n_volumes length for control to avoid adding the same volume to the list twice.
    
    
    int ListA_length=0,ListB_length=0;
    int done = 0;
    int i,direct_children_index;
    int *temp_pointer;
    double max_priority=-1000000;
    int residing_volume=0; // 0 can be removed from the input list if default is 0
    int this_mask_status,mask_index,mask_global_index;
    
    // volume_logic_copy
    //for (i=0;i<volume_logic.num_elements;i++) volume_logic_copy[i] = volume_logic.elements[i];
    
    // low memory version of volume_logic_copy
    for (i=0;i<number_of_volumes;i++) volume_logic_copy[i] = 0;
    for (i=0;i<destinations_list.num_elements;i++) volume_logic_copy[destinations_list.elements[i]] = 1;
    printf("within_which_volume debug %i\n",input_list.num_elements);

    // Does one loop through the algorithm first to set up ListA instead of copying it from input_list, which takes time
    for (i=0;i<input_list.num_elements;i++) {
            if (Volumes[input_list.elements[i]]->geometry.within_function(pos,&Volumes[input_list.elements[i]]->geometry) == 1) {
                printf("The position is inside of volume %d\n",input_list.elements[i]);
                if (Volumes[input_list.elements[i]]->geometry.is_masked_volume == 1) {
                    // if the volume is masked, I need to know if it can be a destination volume from the mask_status_list.
                    // if the masked volume is in ANY mode,
                    this_mask_status=1;
                    //print_1d_int_list(*mask_status_list,"mask status list from within_which_volume");
                    for (mask_index=0;mask_index<Volumes[input_list.elements[i]]->geometry.masked_by_mask_index_list.num_elements;mask_index++) {
                        //printf("Looking at the mask with global index %d \n",Volumes[input_list.elements[i]]->geometry.masked_by_mask_index_list.elements[mask_index]);
                        if (mask_status_list->elements[Volumes[input_list.elements[i]]->geometry.masked_by_mask_index_list.elements[mask_index]] == 1) {
                            if (Volumes[input_list.elements[i]]->geometry.mask_mode == 2) { // ANY (break if any one in)
                                this_mask_status=1;
                                break;
                            }
                            //printf("global index %d had mask status = 1 \n",Volumes[input_list.elements[i]]->geometry.masked_by_mask_index_list.elements[mask_index]);
                        } else {
                          //printf("global index %d had mask status = 0 \n",Volumes[input_list.elements[i]]->geometry.masked_by_mask_index_list.elements[mask_index]);
                          this_mask_status = 0;
                          if(Volumes[input_list.elements[i]]->geometry.mask_mode == 1) break; // ALL (break if any one out)
                        }
                    }
                    //printf("This volume is masked, and the mask status is %d\n",this_mask_status);
                } else this_mask_status = 1; // if the volume is not masked
            
                if (Volumes[input_list.elements[i]]->geometry.priority_value > max_priority && this_mask_status == 1) {
                    max_priority = Volumes[input_list.elements[i]]->geometry.priority_value;
                    residing_volume = input_list.elements[i];
                    //printf("residing volume set to %d\n",residing_volume);
                }
                    for (direct_children_index = 0;direct_children_index < Volumes[input_list.elements[i]]->geometry.direct_children.num_elements;direct_children_index++) {
                        if (volume_logic_copy[Volumes[input_list.elements[i]]->geometry.direct_children.elements[direct_children_index]] == 1) {
                            ListA[ListA_length++] = Volumes[input_list.elements[i]]->geometry.direct_children.elements[direct_children_index];
                            volume_logic_copy[Volumes[input_list.elements[i]]->geometry.direct_children.elements[direct_children_index]] = 0;
                        }
                    }
            }
    }
    //printf("Completed first loop, continued in while loop\n");
    if (ListA_length > 0) {
        while (done == 0) {
            for (i=0;i<ListA_length;i++) {
              //printf("checking element number %d of list A which is volume number %d \n",i,ListA[i]);
                if (Volumes[ListA[i]]->geometry.within_function(pos,&Volumes[ListA[i]]->geometry) == 1) {
                  //printf("ray was inside this volume \n");
                    if (Volumes[ListA[i]]->geometry.is_masked_volume == 1) {
                      //printf("it is a mask and thus need check of mask status \n");
                        // if the volume is masked, I need to know if it can be a destination volume from the mask_status_list.
                        // if the masked volume is in ANY mode,
                        this_mask_status=1;
                        for (mask_index=0;mask_index<Volumes[ListA[i]]->geometry.masked_by_mask_index_list.num_elements;mask_index++) {
                            if (mask_status_list->elements[Volumes[ListA[i]]->geometry.masked_by_mask_index_list.elements[mask_index]] == 1) {
                                if (Volumes[ListA[i]]->geometry.mask_mode == 2) { // ANY (break if any one in)
                                    this_mask_status=1;
                                    break;
                                }
                            } else {
                              this_mask_status = 0;
                              if(Volumes[ListA[i]]->geometry.mask_mode == 1) break; // ALL (break if any one out)
                            }
                        }
                    } else this_mask_status = 1;
                    //printf("the mask status is %d \n",this_mask_status);
                    if (Volumes[ListA[i]]->geometry.priority_value > max_priority && this_mask_status == 1) {
                        max_priority = Volumes[ListA[i]]->geometry.priority_value;
                        residing_volume = ListA[i];
                    }
                    //printf("Adding direct children to list B \n");
                        for (direct_children_index = 0;direct_children_index < Volumes[ListA[i]]->geometry.direct_children.num_elements;direct_children_index++) {
                            //printf("Checking direct_child number %d which is %d \n",direct_children_index,Volumes[ListA[i]]->geometry.direct_children.elements[direct_children_index]);
                            if (volume_logic_copy[Volumes[ListA[i]]->geometry.direct_children.elements[direct_children_index]] == 1) {
                                //printf("It's volume_logic was 1, and it is thus added to listB with index %d \n",ListB_length);
                                ListB[ListB_length++] = Volumes[ListA[i]]->geometry.direct_children.elements[direct_children_index];
                                volume_logic_copy[Volumes[ListA[i]]->geometry.direct_children.elements[direct_children_index]] = 0;
                            }
                        }
                    //printf("List B is now: ");
                    //for (direct_children_index=0;direct_children_index<ListB_length;direct_children_index++) printf("%d ",ListB[direct_children_index]);
                    //printf("\n");
                    
                    
                }
            }
            if (ListB_length==0) done = 1;
            else {
                
                for (i=0;i<ListB_length;i++) ListA[i] = ListB[i];
                ListA_length = ListB_length;
                ListB_length = 0;
                
                /*
                // Could do this with pointers instead to avoid this for loop (and needless copy)
                // This code block fails on the cluster in rare circumstances
                ListA = temp_pointer;
                ListA = ListB;
                ListB = temp_pointer;
                ListA_length=ListB_length;
                ListB_length=0;
                */
            }
        }
    }
    //printf("residing volume returned %d\n",residing_volume);
    return residing_volume;
};

int within_which_volume_GPU(Coords pos, struct pointer_to_1d_int_list input_list, struct pointer_to_1d_int_list destinations_list, struct Volume_struct **Volumes, struct pointer_to_1d_int_list *mask_status_list, int number_of_volumes, int *volume_logic_copy, int *ListA, int *ListB) {
    // This function identifies in which of the volumes of the input list the position pos lies in.
    // pos: position for which the current volume should be found for
    // input list: list of potential volumes, reduced to the ones without parents (their children will be checked)
    // OLD VERSION: volume logic: A logic list of allowed volumes for lookup, volume 1 3 and 5 in a case of 10 volumes would be [0 1 0 1 0 1 0 0 0 0]
    // destinations_list: list of allowed destinations (the original destinations list)
    // Volumes: Main volumes array
    // volume_logic_copy: A pointer to a integer array with at least length "number_of_volumes" (pre alocated for speed)
    // ListA: A pointer to a integer array with at least length "number_of_volumes" (pre alocated for speed)
    // ListB: A pointer to a integer array with at least length "number_of_volumes" (pre alocated for speed)
    
    // Algorithm description
    // Check all of input list for pos being within them, those that have it within them are:
    //      checked against the current highest priority
    //          if higher, is the new highest priority and the new pick for volume
    //      have all their direct children added to the next list to be checked (but any volume can only be added to that list once)
    // Once a run have been made where the next list to check is empty, the answer for new pick for volume is taken.
    
    // Mask update:
    // Algorithm description
    // Check all of input list for pos being within them, those that have it within them are:
    //      checked against the current highest priority and mask status
    //          if higher, this is the new highest priority and the new pick for volume
    //      have all their direct children added to the next list to be checked (but any volume can only be added to that list once)
    // Once a run have been made where the next list to check is empty, the answer for new pick for volume is taken.
    
    // The advantage of the method is that potentially large numbers of children are skipped when their parents do not contain the position.
    // The overhead cost is low, as all the lists are prealocated.
    // Should be checked which of the two implementations is faster, as this is much more complicated than simply checking all possibilities.
    // No within_function call should be made twice, as the same volume number will not be checked twice because of the properties of the direct_children list and the volume_logic that removes duplicates on each level.
    
    
    // This function uses too much memory, the memory required for the volume logic list is n_volumes^2 ints, or for a MACS monochromator 127000 ints.
    // Instead the original destinations list must be used, and a function for quick lookup in a (sorted) destinations list made.
    // Still need a list of n_volumes length for control to avoid adding the same volume to the list twice.
    
    
    int ListA_length=0,ListB_length=0;
    int done = 0;
    int i,direct_children_index;
    int *temp_pointer;
    double max_priority=-1000000;
    int residing_volume=0; // 0 can be removed from the input list if default is 0
    int this_mask_status,mask_index,mask_global_index;
    
    // volume_logic_copy
    //for (i=0;i<volume_logic.num_elements;i++) volume_logic_copy[i] = volume_logic.elements[i];
    
    // low memory version of volume_logic_copy
    for (i=0;i<number_of_volumes;i++) volume_logic_copy[i] = 0;
    for (i=0;i<destinations_list.num_elements;i++) volume_logic_copy[destinations_list.elements[i]] = 1;
    //printf("within_which_volume debug\n");

    // Does one loop through the algorithm first to set up ListA instead of copying it from input_list, which takes time
    for (i=0;i<input_list.num_elements;i++) {
            if (r_within_function(pos, &Volumes[input_list.elements[i]]->geometry) == 1) {
                //printf("The position is inside of volume %d\n",input_list.elements[i]);
                if (Volumes[input_list.elements[i]]->geometry.is_masked_volume == 1) {
                    // if the volume is masked, I need to know if it can be a destination volume from the mask_status_list.
                    // if the masked volume is in ANY mode,
                    this_mask_status=1;
                    //print_1d_int_list(*mask_status_list,"mask status list from within_which_volume");
                    for (mask_index=0;mask_index<Volumes[input_list.elements[i]]->geometry.masked_by_mask_index_list.num_elements;mask_index++) {
                        //printf("Looking at the mask with global index %d \n",Volumes[input_list.elements[i]]->geometry.masked_by_mask_index_list.elements[mask_index]);
                        if (mask_status_list->elements[Volumes[input_list.elements[i]]->geometry.masked_by_mask_index_list.elements[mask_index]] == 1) {
                            if (Volumes[input_list.elements[i]]->geometry.mask_mode == 2) { // ANY (break if any one in)
                                this_mask_status=1;
                                break;
                            }
                            //printf("global index %d had mask status = 1 \n",Volumes[input_list.elements[i]]->geometry.masked_by_mask_index_list.elements[mask_index]);
                        } else {
                          //printf("global index %d had mask status = 0 \n",Volumes[input_list.elements[i]]->geometry.masked_by_mask_index_list.elements[mask_index]);
                          this_mask_status = 0;
                          if(Volumes[input_list.elements[i]]->geometry.mask_mode == 1) break; // ALL (break if any one out)
                        }
                    }
                    //printf("This volume is masked, and the mask status is %d\n",this_mask_status);
                } else this_mask_status = 1; // if the volume is not masked
            
                if (Volumes[input_list.elements[i]]->geometry.priority_value > max_priority && this_mask_status == 1) {
                    max_priority = Volumes[input_list.elements[i]]->geometry.priority_value;
                    residing_volume = input_list.elements[i];
                    //printf("residing volume set to %d\n",residing_volume);
                }
                    for (direct_children_index = 0;direct_children_index < Volumes[input_list.elements[i]]->geometry.direct_children.num_elements;direct_children_index++) {
                        if (volume_logic_copy[Volumes[input_list.elements[i]]->geometry.direct_children.elements[direct_children_index]] == 1) {
                            ListA[ListA_length++] = Volumes[input_list.elements[i]]->geometry.direct_children.elements[direct_children_index];
                            volume_logic_copy[Volumes[input_list.elements[i]]->geometry.direct_children.elements[direct_children_index]] = 0;
                        }
                    }
            }
    }
    //printf("Completed first loop, continued in while loop\n");
    if (ListA_length > 0) {
        while (done == 0) {
            for (i=0;i<ListA_length;i++) {
              //printf("checking element number %d of list A which is volume number %d \n",i,ListA[i]);
                if (r_within_function(pos,&Volumes[ListA[i]]->geometry) == 1) {
                  //printf("ray was inside this volume \n");
                    if (Volumes[ListA[i]]->geometry.is_masked_volume == 1) {
                      //printf("it is a mask and thus need check of mask status \n");
                        // if the volume is masked, I need to know if it can be a destination volume from the mask_status_list.
                        // if the masked volume is in ANY mode,
                        this_mask_status=1;
                        for (mask_index=0;mask_index<Volumes[ListA[i]]->geometry.masked_by_mask_index_list.num_elements;mask_index++) {
                            if (mask_status_list->elements[Volumes[ListA[i]]->geometry.masked_by_mask_index_list.elements[mask_index]] == 1) {
                                if (Volumes[ListA[i]]->geometry.mask_mode == 2) { // ANY (break if any one in)
                                    this_mask_status=1;
                                    break;
                                }
                            } else {
                              this_mask_status = 0;
                              if(Volumes[ListA[i]]->geometry.mask_mode == 1) break; // ALL (break if any one out)
                            }
                        }
                    } else this_mask_status = 1;
                    //printf("the mask status is %d \n",this_mask_status);
                    if (Volumes[ListA[i]]->geometry.priority_value > max_priority && this_mask_status == 1) {
                        max_priority = Volumes[ListA[i]]->geometry.priority_value;
                        residing_volume = ListA[i];
                    }
                    //printf("Adding direct children to list B \n");
                        for (direct_children_index = 0;direct_children_index < Volumes[ListA[i]]->geometry.direct_children.num_elements;direct_children_index++) {
                            //printf("Checking direct_child number %d which is %d \n",direct_children_index,Volumes[ListA[i]]->geometry.direct_children.elements[direct_children_index]);
                            if (volume_logic_copy[Volumes[ListA[i]]->geometry.direct_children.elements[direct_children_index]] == 1) {
                                //printf("It's volume_logic was 1, and it is thus added to listB with index %d \n",ListB_length);
                                ListB[ListB_length++] = Volumes[ListA[i]]->geometry.direct_children.elements[direct_children_index];
                                volume_logic_copy[Volumes[ListA[i]]->geometry.direct_children.elements[direct_children_index]] = 0;
                            }
                        }
                    //printf("List B is now: ");
                    //for (direct_children_index=0;direct_children_index<ListB_length;direct_children_index++) printf("%d ",ListB[direct_children_index]);
                    //printf("\n");
                    
                    
                }
            }
            if (ListB_length==0) done = 1;
            else {
                
                for (i=0;i<ListB_length;i++) ListA[i] = ListB[i];
                ListA_length = ListB_length;
                ListB_length = 0;
                
                /*
                // Could do this with pointers instead to avoid this for loop (and needless copy)
                // This code block fails on the cluster in rare circumstances
                ListA = temp_pointer;
                ListA = ListB;
                ListB = temp_pointer;
                ListA_length=ListB_length;
                ListB_length=0;
                */
            }
        }
    }
    //printf("residing volume returned %d\n",residing_volume);
    return residing_volume;
};




int within_which_volume_debug(Coords pos, struct pointer_to_1d_int_list input_list, struct pointer_to_1d_int_list volume_logic, struct Volume_struct **Volumes, int *volume_logic_copy, int *ListA, int *ListB) {
    // This function identifies in which of the volumes of the input list the position pos lies in.
    // pos: position for which the current volume should be found for
    // input list: list of potential volumes, reduced to the ones without parents (their children will be checked)
    // volume logic: A logic list of allowed volumes for lookup, volume 1 3 and 5 in a case of 10 volumes would be [0 1 0 1 0 1 0 0 0 0]
    // Volumes: Main volumes array
    // volume_logic_copy: A pointer to a integer array with at least length "number_of_volumes" (pre alocated for speed)
    // ListA: A pointer to a integer array with at least length "number_of_volumes" (pre alocated for speed)
    // ListB: A pointer to a integer array with at least length "number_of_volumes" (pre alocated for speed)
    
    // Algorithm description
    // Check all of input list for pos being within them, those that have it within them are:
    //      checked against the current highest priority
    //          if higher, is the new highest priority and the new pick for volume
    //      have all their direct children added to the next list to be checked (but any volume can only be added to that list once)
    // Once a run have been made where the next list to check is empty, the answer for new pick for volume is taken.
    
    // The advantage of the method is that potentially large numbers of children are skipped when their parents do not contain the position.
    // The overhead cost is low, as all the lists are prealocated.
    // Should be checked which of the two implementations is faster, as this is much more complicated than simply checking all possibilities.
    // No within_function call should be made twice, as the same volume number will not be checked twice because of the properties of the direct_children list and the volume_logic that removes duplicates on each level.
    
    
    int ListA_length=0,ListB_length=0;
    int done = 0;
    int i,direct_children_index;
    int *temp_pointer;
    double max_priority=-1000000;
    int residing_volume=0; // 0 can be removed from the input list if default is 0
    
    // volume_logic_copy
    for (i=0;i<volume_logic.num_elements;i++) volume_logic_copy[i] = volume_logic.elements[i];
    
    // Does one loop through the algorithm first to set up ListA instead of copying it from input_list, which takes time
    for (i=0;i<input_list.num_elements;i++) {
            if (Volumes[input_list.elements[i]]->geometry.within_function(pos,&Volumes[input_list.elements[i]]->geometry) == 1) {
                if (Volumes[input_list.elements[i]]->geometry.priority_value > max_priority) {
                    max_priority = Volumes[input_list.elements[i]]->geometry.priority_value;
                    residing_volume = input_list.elements[i];
                }
                    for (direct_children_index = 0;direct_children_index < Volumes[input_list.elements[i]]->geometry.direct_children.num_elements;direct_children_index++) {
                        if (volume_logic_copy[Volumes[input_list.elements[i]]->geometry.direct_children.elements[direct_children_index]] == 1) {
                            ListA[ListA_length++] = Volumes[input_list.elements[i]]->geometry.direct_children.elements[direct_children_index];
                            volume_logic_copy[Volumes[input_list.elements[i]]->geometry.direct_children.elements[direct_children_index]] = 0;
                        }
                    }
            }
    }
    if (ListA_length > 0) {
        while (done == 0) {
        
            printf("ListA = [");
            for (i=0;i<ListA_length;i++) {
                printf("%d,",ListA[i]);
            }
            printf("]\n");
            for (i=0;i<ListA_length;i++) {
                if (Volumes[ListA[i]]->geometry.within_function(pos,&Volumes[ListA[i]]->geometry) == 1) {
                    if (Volumes[ListA[i]]->geometry.priority_value > max_priority) {
                        max_priority = Volumes[ListA[i]]->geometry.priority_value;
                        residing_volume = ListA[i];
                    }
                    //if (ListA[i]!=0) {
                        for (direct_children_index = 0;direct_children_index < Volumes[ListA[i]]->geometry.direct_children.num_elements;direct_children_index++) {
                            if (volume_logic_copy[Volumes[ListA[i]]->geometry.direct_children.elements[direct_children_index]] == 1) {
                                ListB[ListB_length++] = Volumes[ListA[i]]->geometry.direct_children.elements[direct_children_index];
                                volume_logic_copy[Volumes[ListA[i]]->geometry.direct_children.elements[direct_children_index]] = 0;
                            }
                        }
                    //}
                }
            }
            if (ListB_length==0) done = 1;
            else {
                temp_pointer = ListA;
                ListA = ListB;
                ListB = temp_pointer;
                ListA_length=ListB_length;
                ListB_length=0;
            }
        }
    }
    printf("Volume number %d had the highest priority of checked volumes\n",residing_volume);
    return residing_volume;
};

int inside_function(struct Volume_struct *parent_volume, struct Volume_struct *child_volume) {
    // Function that calls the correct within function depending on the shapes of the two volumes
    if (strcmp("sphere",parent_volume->geometry.shape) == 0 && strcmp("sphere",child_volume->geometry.shape) == 0) {
        if (sphere_within_sphere(&child_volume->geometry,&parent_volume->geometry)) return 1;
    }
    else if (strcmp("cylinder",parent_volume->geometry.shape) == 0 && strcmp("cylinder",child_volume->geometry.shape) == 0) {
        if (cylinder_within_cylinder(&child_volume->geometry,&parent_volume->geometry)) return 1;
    }
    else if (strcmp("box",parent_volume->geometry.shape) == 0 && strcmp("box",child_volume->geometry.shape) == 0) {
        if (box_within_box(&child_volume->geometry,&parent_volume->geometry)) return 1;
    }
    else if (strcmp("cone",parent_volume->geometry.shape) == 0 && strcmp("cone",child_volume->geometry.shape) == 0) {
        if (cone_within_cone(&child_volume->geometry,&parent_volume->geometry)) return 1;
    }
    else if (strcmp("mesh",parent_volume->geometry.shape) == 0 && strcmp("mesh",child_volume->geometry.shape) == 0) {
        if (mesh_within_mesh(&child_volume->geometry,&parent_volume->geometry)) return 1;
    }
    else if (strcmp("box",parent_volume->geometry.shape) == 0 && strcmp("cylinder",child_volume->geometry.shape) == 0) {
        if (cylinder_within_box(&child_volume->geometry,&parent_volume->geometry)) return 1;
    }
    else if (strcmp("cylinder",parent_volume->geometry.shape) == 0 && strcmp("box",child_volume->geometry.shape) == 0) {
        if (box_within_cylinder(&child_volume->geometry,&parent_volume->geometry)) return 1;
    }
    else if (strcmp("box",parent_volume->geometry.shape) == 0 && strcmp("sphere",child_volume->geometry.shape) == 0) {
        if (sphere_within_box(&child_volume->geometry,&parent_volume->geometry)) return 1;
    }
    else if (strcmp("sphere",parent_volume->geometry.shape) == 0 && strcmp("box",child_volume->geometry.shape) == 0) {
        if (box_within_sphere(&child_volume->geometry,&parent_volume->geometry)) return 1;
    }
    else if (strcmp("sphere",parent_volume->geometry.shape) == 0 && strcmp("cylinder",child_volume->geometry.shape) == 0) {
        if (cylinder_within_sphere(&child_volume->geometry,&parent_volume->geometry)) return 1;
    }
    else if (strcmp("cylinder",parent_volume->geometry.shape) == 0 && strcmp("sphere",child_volume->geometry.shape) == 0) {
        if (sphere_within_cylinder(&child_volume->geometry,&parent_volume->geometry)) return 1;
    }
    else if (strcmp("cone",parent_volume->geometry.shape) == 0 && strcmp("sphere",child_volume->geometry.shape) == 0) {
        if (sphere_within_cone(&child_volume->geometry,&parent_volume->geometry)) return 1;
    }
    else if (strcmp("sphere",parent_volume->geometry.shape) == 0 && strcmp("cone",child_volume->geometry.shape) == 0) {
        if (cone_within_sphere(&child_volume->geometry,&parent_volume->geometry)) return 1;
    }
    else if (strcmp("cone",parent_volume->geometry.shape) == 0 && strcmp("cylinder",child_volume->geometry.shape) == 0) {
        if (cylinder_within_cone(&child_volume->geometry,&parent_volume->geometry)) return 1;
    }
    else if (strcmp("cylinder",parent_volume->geometry.shape) == 0 && strcmp("cone",child_volume->geometry.shape) == 0) {
        if (cone_within_cylinder(&child_volume->geometry,&parent_volume->geometry)) return 1;
    }
    else if (strcmp("cone",parent_volume->geometry.shape) == 0 && strcmp("box",child_volume->geometry.shape) == 0) {
        if (box_within_cone(&child_volume->geometry,&parent_volume->geometry)) return 1;
    }
    else if (strcmp("box",parent_volume->geometry.shape) == 0 && strcmp("cone",child_volume->geometry.shape) == 0) {
        if (cone_within_box(&child_volume->geometry,&parent_volume->geometry)) return 1;
    }
    else if (strcmp("mesh",parent_volume->geometry.shape) == 0 && strcmp("cylinder",child_volume->geometry.shape) == 0) {
        if (cylinder_within_mesh(&child_volume->geometry,&parent_volume->geometry)) return 1;
    }
    else if (strcmp("mesh",parent_volume->geometry.shape) == 0 && strcmp("sphere",child_volume->geometry.shape) == 0) {
        if (sphere_within_mesh(&child_volume->geometry,&parent_volume->geometry)) return 1;
    }
    else if (strcmp("cone",parent_volume->geometry.shape) == 0 && strcmp("mesh",child_volume->geometry.shape) == 0) {
        if (mesh_within_cone(&child_volume->geometry,&parent_volume->geometry)) return 1;
    }
    else if (strcmp("mesh",parent_volume->geometry.shape) == 0 && strcmp("cone",child_volume->geometry.shape) == 0) {
        if (cone_within_mesh(&child_volume->geometry,&parent_volume->geometry)) return 1;
    }
    else if (strcmp("cylinder",parent_volume->geometry.shape) == 0 && strcmp("mesh",child_volume->geometry.shape) == 0) {
        if (cylinder_within_mesh(&child_volume->geometry,&parent_volume->geometry)) return 1;
    }
    else if (strcmp("sphere",parent_volume->geometry.shape) == 0 && strcmp("mesh",child_volume->geometry.shape) == 0) {
        if (mesh_within_sphere(&child_volume->geometry,&parent_volume->geometry)) return 1;
    }
    else if (strcmp("mesh",parent_volume->geometry.shape) == 0 && strcmp("box",child_volume->geometry.shape) == 0) {
        if (box_within_mesh(&child_volume->geometry,&parent_volume->geometry)) return 1;
    }
    else if (strcmp("box",parent_volume->geometry.shape) == 0 && strcmp("mesh",child_volume->geometry.shape) == 0) {
        if (mesh_within_box(&child_volume->geometry,&parent_volume->geometry)) return 1;
    }
    else {
        #ifndef OPENACC
        printf("Need within function for type: ");
        printf("%s",parent_volume->geometry.shape);
        printf(" and type: ");
        printf("%s",child_volume->geometry.shape);
        printf(".\n");
        printf("It is not yet supported to mix mesh geometries with the basic shapes, but several mesh geometries are allowed.\n");
        exit(EXIT_FAILURE);
	#endif
    }
    
    return 0;
};

void generate_children_lists(struct Volume_struct **Volumes, struct pointer_to_1d_int_list **true_children_lists, int number_of_volumes, int verbal) {
  // This function generates a list of children for each volume.
  // A volume m is a child of volume n, if the entire space ocupied by volume m is inside of the space ocupied by volume n
  // A volume m is a true child of volume n, if the entire space coupied by volume m after it's masks are applied is inside the volume ocupied by volume n after it's masks are applied
  
  
  MPI_MASTER(
  if (verbal) printf("\nGenerating children lists --------------------------- \n");
  )

  // Mask update: Creating a temporary list for each volume
  struct pointer_to_1d_int_list *temporary_children_lists;
  temporary_children_lists = malloc(number_of_volumes*sizeof(struct pointer_to_1d_int_list));
  if (!temporary_children_lists) {
    fprintf(stderr,"Failure allocating list in Union function generate_children_lists 1 - Exit!\n");
    exit(EXIT_FAILURE);
  }
  // The surrounding vacuum, volume 0, done outside of for loop.
  temporary_children_lists[0].num_elements = number_of_volumes - 1;
  temporary_children_lists[0].elements = malloc(temporary_children_lists[0].num_elements*sizeof(int));
  
  int parent;
  for (parent=1;parent<number_of_volumes;parent++) {
      temporary_children_lists[0].elements[parent-1] = parent;
  }

  // Hardcoding that every volume is a child of the surrounding vacuum
  Volumes[0]->geometry.children.num_elements = number_of_volumes-1;
  Volumes[0]->geometry.children.elements = malloc((number_of_volumes-1)*sizeof(int));
  if (!Volumes[0]->geometry.children.elements) {
    fprintf(stderr,"Failure allocating list in Union function generate_children_lists 2 - Exit!\n");
    exit(EXIT_FAILURE);
  }
  true_children_lists[0] = malloc(sizeof(struct pointer_to_1d_int_list));
  if (!true_children_lists[0]) {
    fprintf(stderr,"Failure allocating list in Union function generate_children_lists 3 - Exit!\n");
    exit(EXIT_FAILURE);
  }
  true_children_lists[0]->num_elements = number_of_volumes - 1;
  true_children_lists[0]->elements = malloc((number_of_volumes-1)*sizeof(int));
  if (!true_children_lists[0]->elements) {
    fprintf(stderr,"Failure allocating list in Union function generate_children_lists 4 - Exit!\n");
    exit(EXIT_FAILURE);
  }
  
  for (parent=1;parent<number_of_volumes;parent++) {
      Volumes[0]->geometry.children.elements[parent-1] = parent;
      true_children_lists[0]->elements[parent-1] = parent;
  }
  
  char string_output[128];
  MPI_MASTER(
  if (verbal) sprintf(string_output,"Children for Volume %d",0);
  if (verbal) print_1d_int_list(Volumes[0]->geometry.children,string_output);
  )
  
  
  // Generating the children lists for all other volumes using the appropriate geometry functions
  struct pointer_to_1d_int_list temp_list_local;
  temp_list_local.num_elements = number_of_volumes;
  temp_list_local.elements = malloc(number_of_volumes*sizeof(int));
  if (!temp_list_local.elements) {
    fprintf(stderr,"Failure allocating list in Union function generate_children_lists 5 - Exit!\n");
    exit(EXIT_FAILURE);
  }
  
  struct pointer_to_1d_int_list true_temp_list_local;
  true_temp_list_local.num_elements = number_of_volumes;
  true_temp_list_local.elements = malloc(number_of_volumes*sizeof(int));
  if (!true_temp_list_local.elements) {
    fprintf(stderr,"Failure allocating list in Union function generate_children_lists 6 - Exit!\n");
    exit(EXIT_FAILURE);
  }
  
  
  int child,used_elements,used_elements_true;
  for (parent=1;parent<number_of_volumes;parent++) {
      used_elements = 0;used_elements_true = 0;
      for (child=1;child<number_of_volumes;child++) {
        if (child != parent) {
          // Call inside_function that selects the proper within function for the two volumes
          if (1 == inside_function(Volumes[parent],Volumes[child])) {
            temp_list_local.elements[used_elements++] = child;
            true_temp_list_local.elements[used_elements_true++] = child;
          }
        } else true_temp_list_local.elements[used_elements_true++] = child; // Needed when children list takes mask into account
      }
      // Temp test
      allocate_list_from_temp(used_elements,temp_list_local,&Volumes[parent]->geometry.children);
      // Assing the children list to a temporary list as the masks have yet to be taken into account
      temporary_children_lists[parent].num_elements=0;
      allocate_list_from_temp(used_elements_true,true_temp_list_local,&temporary_children_lists[parent]);
      
      
      MPI_MASTER(
      if (verbal) sprintf(string_output,"Children for Volume %d (temporary_list)",parent);
      if (verbal) print_1d_int_list(temporary_children_lists[parent],string_output);
      )
      
      MPI_MASTER(
      if (verbal) sprintf(string_output,"Children for Volume %d (permanent_list)",parent);
      if (verbal) print_1d_int_list(Volumes[parent]->geometry.children,string_output);
      )
      
  }
  
  // mask update:
  // The logical expression: (child c parent AND child c parent_mask) OR (child_mask c parent AND child_mask c parent_mask)
  //  needs to be evaluated for each child / parent combination in order to take the masks of each into account
  
  int logic_var1,logic_var2,logic_var_ANY,logic_var_ALL;
  int mask_index,mask_index_child,mask_index_parent;
  int volume_C,volume_P;
  // Loop that takes masks into account
  for (parent=1;parent<number_of_volumes;parent++) {
    used_elements = 0;
    for (child=1;child<number_of_volumes;child++) {
     if (child != parent && 0 == on_int_list(Volumes[parent]->geometry.masked_by_list,child)) {
        // The children list for each volume does not need to contain the volume itself
        //  And a parent masked by it's child can not have that mask as a child
      
      // Here c means within in the sense of a set being part of another set
      // Logical expression to be evaluated: (child c parent AND child c parent_mask) OR (child_mask c parent AND child_mask c parent_mask)
      logic_var1 = on_int_list(temporary_children_lists[parent],child);
      if (logic_var1 == 1 && Volumes[parent]->geometry.is_masked_volume == 1) {
        // if the parent volume is masked, the child also need to be inclosed in these masks to fulfill this side of the logical expression
        logic_var_ANY = 0;
        for (mask_index=0;mask_index<Volumes[parent]->geometry.masked_by_list.num_elements;mask_index++) {
          if (0 == on_int_list(temporary_children_lists[Volumes[parent]->geometry.masked_by_list.elements[mask_index]],child)) {
            if (Volumes[parent]->geometry.mask_mode == 1) {
              logic_var1 = 0;
              break;
            }
          } else logic_var_ANY = 1;
        }
        if (Volumes[parent]->geometry.mask_mode == 2) logic_var1 = logic_var_ANY;
      }
      
      if (logic_var1 == 1) true_temp_list_local.elements[used_elements++] = child;
      else if (Volumes[child]->geometry.is_masked_volume == 1) {
        // If the first side of the logical expression is false, evalute the other side
        // The other side is only relevant if the child volume is masked, otherwise it is ignored
        //printf("Second side of logical expression \n");
        
        logic_var1 = 1; // Assume true
          
        // child_mask c parent
        logic_var_ALL = 0;
        for (mask_index=0;mask_index<Volumes[child]->geometry.masked_by_list.num_elements;mask_index++) {
          if (0 == on_int_list(temporary_children_lists[parent],Volumes[child]->geometry.masked_by_list.elements[mask_index])){
            if (Volumes[child]->geometry.mask_mode == 2) {
              logic_var1 = 0;
              break;
            }
          } else logic_var_ALL = 1;
        }
        if (Volumes[child]->geometry.mask_mode == 1) logic_var1 = logic_var_ALL;
        
        // This line allows the second part of the logical expression to be true in cases where the parent volume is not masked
        if (logic_var1 == 1 && Volumes[parent]->geometry.is_masked_volume == 0) true_temp_list_local.elements[used_elements++] = child;
        
        // There is no reason to check the other part (child_mask c parent_mask) if the first part was not true
        if (logic_var1 == 1 && Volumes[parent]->geometry.is_masked_volume == 1) {
          // This last part requires both the child and the parent to be masked
          // Need to evaluate (child_mask c parent_mask), where both can be a be a list of volume with ALL/ANY modes
          
          if (Volumes[parent]->geometry.mask_mode == 1) {
            logic_var2 = 1; // Assume the logical expression (child_mask c parent_mask) is true
            for (mask_index_parent=0;mask_index_parent<Volumes[parent]->geometry.masked_by_list.num_elements;mask_index_parent++) {
              // As the parent is in ALL mode, the child masks must be within ALL parent masks
              logic_var_ANY = 0; // Assume not a single child is within this parent
              for (mask_index_child=0;mask_index_child<Volumes[child]->geometry.masked_by_list.num_elements;mask_index_child++) {
                volume_P = Volumes[parent]->geometry.masked_by_list.elements[mask_index_parent];
                volume_C = Volumes[child]->geometry.masked_by_list.elements[mask_index_child];
                // Is volume C inside volume P? If yes, volume C must be on volume P's temporary children list
                if (0 == on_int_list(temporary_children_lists[volume_P],volume_C)) {
                  if (Volumes[child]->geometry.mask_mode == 2) {
                    // If child is in ANY mode, any one mask outside is enough to make the expression false
                    logic_var2 = 0;
                    break;
                  }
                } else logic_var_ANY = 1;
              }
              // If child is in ALL mode, then if any one child were within this mask, the logic expression holds true
              if (Volumes[child]->geometry.mask_mode == 1) logic_var2 = logic_var_ANY;
              if (logic_var2 == 0) break; // No need to continue
            }
          } else if (Volumes[parent]->geometry.mask_mode == 2) {
            // If the parent is in ANY mode, it is enough if the child masks are within just 1 of the parent masks
            for (mask_index_parent=0;mask_index_parent<Volumes[parent]->geometry.masked_by_list.num_elements;mask_index_parent++) {
              logic_var2 = 1; // Assume the logical expression (child_mask c parent_mask) is true
              logic_var_ANY = 0; // Assume not a single child is within this parent
              for (mask_index_child=0;mask_index_child<Volumes[child]->geometry.masked_by_list.num_elements;mask_index_child++) {
                volume_P = Volumes[parent]->geometry.masked_by_list.elements[mask_index_parent];
                volume_C = Volumes[child]->geometry.masked_by_list.elements[mask_index_child];
                // Is volume C inside volume P? If yes, volume C must be on volume P's temporary children list
                if (0 == on_int_list(temporary_children_lists[volume_P],volume_C)) {
                  if (Volumes[child]->geometry.mask_mode == 2) {
                    // If child is in ANY mode, any one mask outside is enough to make the expression false
                    logic_var2 = 0;
                    break;
                  }
                } else logic_var_ANY = 1;
              }
              // If child is in ALL mode, then if any one child were within this mask, the logic expression holds true
              if (Volumes[child]->geometry.mask_mode == 1) logic_var2 = logic_var_ANY;
              if (logic_var2 == 1) break; // No need to continue
            }
          }
          
          // if this point is reached, and logic_var2 is true, volume[child] is a child of volume[parent]
          if (logic_var2 == 1) true_temp_list_local.elements[used_elements++] = child;
          
        }
      }
     }
    }
    
    true_children_lists[parent] = malloc(sizeof(struct pointer_to_1d_int_list));
    if (!true_children_lists[parent]) {
      fprintf(stderr,"Failure allocating list in Union function generate_children_lists 7 - Exit!\n");
      exit(EXIT_FAILURE);
    }
    true_children_lists[parent]->num_elements = 0;
    allocate_list_from_temp(used_elements,true_temp_list_local,true_children_lists[parent]);
    
    MPI_MASTER(
      if (verbal) sprintf(string_output,"True children for Volume (post mask) %d",parent);
      if (verbal) print_1d_int_list(*true_children_lists[parent],string_output);
    )
  }
  
  
  // Clean up of dynamically allocated memory
  
  for(child=0;child<number_of_volumes;child++) free(temporary_children_lists[child].elements);
  free(temporary_children_lists);
  free(true_temp_list_local.elements);
  free(temp_list_local.elements);
};


void generate_overlap_lists(struct pointer_to_1d_int_list **true_overlap_lists, struct pointer_to_1d_int_list **raw_overlap_lists, struct Volume_struct **Volumes, int number_of_volumes, int verbal) {
  // This function generates overlap lists for each volume
  // Volume m overlaps volume n if there is some subset of space they both ocupy (this is called raw overlap)
  // the true overlap list takes mask into account, meaning that the masks are applied before searching for overlap

  MPI_MASTER(
  if (verbal) printf("\nGenerating overlap lists ---------------------------- \n");
  )
  
  // Manually create the overlap list for the surrounding Vacuum, as it overlaps all other volumes
  
  // temporary_overlap_lists are used to save the calculated overlaps to avoid evaluating the heavy functions more than once (twice) for each possible volume pair
  struct pointer_to_1d_int_list *temporary_overlap_lists;
  temporary_overlap_lists = malloc(number_of_volumes*sizeof(struct pointer_to_1d_int_list));
  if (!temporary_overlap_lists) {
    fprintf(stderr,"Failure allocating list in Union function generate_overlap_lists 1 - Exit!\n");
    exit(EXIT_FAILURE);
  }
  
  // Overlap_lists are the final result of the function, and for the surrounding volume it can be set immediatly
  true_overlap_lists[0] = malloc(sizeof(struct pointer_to_1d_int_list));
  if (!true_overlap_lists[0]) {
    fprintf(stderr,"Failure allocating list in Union function generate_overlap_lists 2 - Exit!\n");
    exit(EXIT_FAILURE);
  }
  true_overlap_lists[0]->num_elements = number_of_volumes-1;
  true_overlap_lists[0]->elements = malloc((number_of_volumes-1)*sizeof(int));
  if (!true_overlap_lists[0]->elements) {
    fprintf(stderr,"Failure allocating list in Union function generate_overlap_lists 3 - Exit!\n");
    exit(EXIT_FAILURE);
  }
  raw_overlap_lists[0] =  malloc(sizeof(struct pointer_to_1d_int_list));
  if (!raw_overlap_lists[0]) {
    fprintf(stderr,"Failure allocating list in Union function generate_overlap_lists 4 - Exit!\n");
    exit(EXIT_FAILURE);
  }
  raw_overlap_lists[0]->num_elements = number_of_volumes;
  raw_overlap_lists[0]->elements = malloc(number_of_volumes*sizeof(int));
  if (!raw_overlap_lists[0]->elements) {
    fprintf(stderr,"Failure allocating list in Union function generate_overlap_lists 5 - Exit!\n");
    exit(EXIT_FAILURE);
  }
  raw_overlap_lists[0]->elements[0] = 0; // Volume 0 overlaps itself
  
  int parent;
  for (parent=1;parent<number_of_volumes;parent++) {
      true_overlap_lists[0]->elements[parent-1] = parent;
      raw_overlap_lists[0]->elements[parent] = parent;
  }
  
  char string_output[128];
  MPI_MASTER(
  if (verbal) sprintf(string_output,"Overlaps for Volume %d",0);
  if (verbal) print_1d_int_list(*true_overlap_lists[0],string_output);
  )
  
  // Generate the overlap lists for the remaining volumes
  struct pointer_to_1d_int_list temp_list_local;
  temp_list_local.num_elements = number_of_volumes;
  temp_list_local.elements = malloc(number_of_volumes*sizeof(int));
  if (!temp_list_local.elements) {
    fprintf(stderr,"Failure allocating list in Union function generate_overlap_lists 6 - Exit!\n");
    exit(EXIT_FAILURE);
  }
  
  int child,used_elements;
  // Create overlap for the remaining volumes
  for (parent=1;parent<number_of_volumes;parent++) {
      true_overlap_lists[parent] = malloc(sizeof(struct pointer_to_1d_int_list));
      if (!true_overlap_lists[parent]) {
	fprintf(stderr,"Failure allocating list in Union function generate_overlap_lists 7 - Exit!\n");
	exit(EXIT_FAILURE);
      }
      used_elements = 0;
      temp_list_local.elements[used_elements++] = 0; // Alwasy overlaps with the surrounding vacuum.
      
      for (child=1;child<number_of_volumes;child++) {
        if (child == parent) temp_list_local.elements[used_elements++] = child;
        else {
        if (strcmp("sphere",Volumes[parent]->geometry.shape) == 0 && strcmp("sphere",Volumes[child]->geometry.shape) == 0) {
            if (sphere_overlaps_sphere(&Volumes[parent]->geometry,&Volumes[child]->geometry)) temp_list_local.elements[used_elements++] = child;
        }
        else if (strcmp("cylinder",Volumes[parent]->geometry.shape) == 0 && strcmp("cylinder",Volumes[child]->geometry.shape) == 0) {
            if (cylinder_overlaps_cylinder(&Volumes[parent]->geometry,&Volumes[child]->geometry)) temp_list_local.elements[used_elements++] = child;
        }
        else if (strcmp("box",Volumes[parent]->geometry.shape) == 0 && strcmp("box",Volumes[child]->geometry.shape) == 0) {
            if (box_overlaps_box(&Volumes[parent]->geometry,&Volumes[child]->geometry)) temp_list_local.elements[used_elements++] = child;
        }
        else if (strcmp("cone",Volumes[parent]->geometry.shape) == 0 && strcmp("cone",Volumes[child]->geometry.shape) == 0) {
            if (cone_overlaps_cone(&Volumes[parent]->geometry,&Volumes[child]->geometry)) temp_list_local.elements[used_elements++] = child;
        }
        else if (strcmp("mesh",Volumes[parent]->geometry.shape) == 0 && strcmp("mesh",Volumes[child]->geometry.shape) == 0) {
            if (mesh_overlaps_mesh(&Volumes[parent]->geometry,&Volumes[child]->geometry)) temp_list_local.elements[used_elements++] = child;
        }
        else if (strcmp("box",Volumes[parent]->geometry.shape) == 0 && strcmp("cylinder",Volumes[child]->geometry.shape) == 0) {
            if (box_overlaps_cylinder(&Volumes[parent]->geometry,&Volumes[child]->geometry)) temp_list_local.elements[used_elements++] = child;
        }
        else if (strcmp("cylinder",Volumes[parent]->geometry.shape) == 0 && strcmp("box",Volumes[child]->geometry.shape) == 0) {
            if (cylinder_overlaps_box(&Volumes[parent]->geometry,&Volumes[child]->geometry)) temp_list_local.elements[used_elements++] = child;
        }
        else if (strcmp("box",Volumes[parent]->geometry.shape) == 0 && strcmp("sphere",Volumes[child]->geometry.shape) == 0) {
            if (box_overlaps_sphere(&Volumes[parent]->geometry,&Volumes[child]->geometry)) temp_list_local.elements[used_elements++] = child;
        }
        else if (strcmp("sphere",Volumes[parent]->geometry.shape) == 0 && strcmp("box",Volumes[child]->geometry.shape) == 0) {
            if (sphere_overlaps_box(&Volumes[parent]->geometry,&Volumes[child]->geometry)) temp_list_local.elements[used_elements++] = child;
        }
        else if (strcmp("sphere",Volumes[parent]->geometry.shape) == 0 && strcmp("cylinder",Volumes[child]->geometry.shape) == 0) {
            if (sphere_overlaps_cylinder(&Volumes[parent]->geometry,&Volumes[child]->geometry)) temp_list_local.elements[used_elements++] = child;
        }
        else if (strcmp("cylinder",Volumes[parent]->geometry.shape) == 0 && strcmp("sphere",Volumes[child]->geometry.shape) == 0) {
            if (cylinder_overlaps_sphere(&Volumes[parent]->geometry,&Volumes[child]->geometry)) temp_list_local.elements[used_elements++] = child;
        }
        else if (strcmp("cone",Volumes[parent]->geometry.shape) == 0 && strcmp("sphere",Volumes[child]->geometry.shape) == 0) {
            if (cone_overlaps_sphere(&Volumes[parent]->geometry,&Volumes[child]->geometry)) temp_list_local.elements[used_elements++] = child;
        }
        else if (strcmp("sphere",Volumes[parent]->geometry.shape) == 0 && strcmp("cone",Volumes[child]->geometry.shape) == 0) {
            if (sphere_overlaps_cone(&Volumes[parent]->geometry,&Volumes[child]->geometry)) temp_list_local.elements[used_elements++] = child;
        }
        else if (strcmp("cone",Volumes[parent]->geometry.shape) == 0 && strcmp("cylinder",Volumes[child]->geometry.shape) == 0) {
            if (cone_overlaps_cylinder(&Volumes[parent]->geometry,&Volumes[child]->geometry)) temp_list_local.elements[used_elements++] = child;
        }
        else if (strcmp("cylinder",Volumes[parent]->geometry.shape) == 0 && strcmp("cone",Volumes[child]->geometry.shape) == 0) {
            if (cylinder_overlaps_cone(&Volumes[parent]->geometry,&Volumes[child]->geometry)) temp_list_local.elements[used_elements++] = child;
        }
        else if (strcmp("cone",Volumes[parent]->geometry.shape) == 0 && strcmp("box",Volumes[child]->geometry.shape) == 0) {
            if (cone_overlaps_box(&Volumes[parent]->geometry,&Volumes[child]->geometry)) temp_list_local.elements[used_elements++] = child;
        }
        else if (strcmp("box",Volumes[parent]->geometry.shape) == 0 && strcmp("cone",Volumes[child]->geometry.shape) == 0) {
            if (box_overlaps_cone(&Volumes[parent]->geometry,&Volumes[child]->geometry)) temp_list_local.elements[used_elements++] = child;
        }
        else if (strcmp("mesh",Volumes[parent]->geometry.shape) == 0 && strcmp("sphere",Volumes[child]->geometry.shape) == 0) {
            if (mesh_overlaps_sphere(&Volumes[parent]->geometry,&Volumes[child]->geometry)) temp_list_local.elements[used_elements++] = child;
        }
        else if (strcmp("mesh",Volumes[parent]->geometry.shape) == 0 && strcmp("cylinder",Volumes[child]->geometry.shape) == 0) {
            if (mesh_overlaps_cylinder(&Volumes[parent]->geometry,&Volumes[child]->geometry)) temp_list_local.elements[used_elements++] = child;
        }
        else if (strcmp("mesh",Volumes[parent]->geometry.shape) == 0 && strcmp("box",Volumes[child]->geometry.shape) == 0) {
            if (mesh_overlaps_box(&Volumes[parent]->geometry,&Volumes[child]->geometry)) temp_list_local.elements[used_elements++] = child;
        }
        else if (strcmp("mesh",Volumes[parent]->geometry.shape) == 0 && strcmp("cone",Volumes[child]->geometry.shape) == 0) {
            if (mesh_overlaps_cone(&Volumes[parent]->geometry,&Volumes[child]->geometry)) temp_list_local.elements[used_elements++] = child;
        }
        else if (strcmp("sphere",Volumes[parent]->geometry.shape) == 0 && strcmp("mesh",Volumes[child]->geometry.shape) == 0) {
            if (sphere_overlaps_mesh(&Volumes[parent]->geometry,&Volumes[child]->geometry)) temp_list_local.elements[used_elements++] = child;
        }
        else if (strcmp("cylinder",Volumes[parent]->geometry.shape) == 0 && strcmp("mesh",Volumes[child]->geometry.shape) == 0) {
            if (cylinder_overlaps_mesh(&Volumes[parent]->geometry,&Volumes[child]->geometry)) temp_list_local.elements[used_elements++] = child;
        }
        else if (strcmp("box",Volumes[parent]->geometry.shape) == 0 && strcmp("mesh",Volumes[child]->geometry.shape) == 0) {
            if (box_overlaps_mesh(&Volumes[parent]->geometry,&Volumes[child]->geometry)) temp_list_local.elements[used_elements++] = child;
        }
        else if (strcmp("cone",Volumes[parent]->geometry.shape) == 0 && strcmp("mesh",Volumes[child]->geometry.shape) == 0) {
            if (cone_overlaps_mesh(&Volumes[parent]->geometry,&Volumes[child]->geometry)) temp_list_local.elements[used_elements++] = child;
        }
        else {
            printf("Need overlap function for type: ");
            printf("%s",Volumes[parent]->geometry.shape);
            printf(" and type: ");
            printf("%s",Volumes[child]->geometry.shape);
            printf(".\n");
            exit(1);
        }
        }
      }
      //allocate_list_from_temp(used_elements,temp_list_local,overlap_lists[parent]);
      allocate_list_from_temp(used_elements,temp_list_local,&temporary_overlap_lists[parent]);
      
      // Save the raw overlap data to the raw_overlap_lists[parent] list
      raw_overlap_lists[parent] = malloc(sizeof(struct pointer_to_1d_int_list));
      if (!raw_overlap_lists[parent]) {
	fprintf(stderr,"Failure allocating list in Union function generate_overlap_lists 8 - Exit!\n");
	exit(EXIT_FAILURE);
      }
      raw_overlap_lists[parent]->num_elements = 0;
      allocate_list_from_temp(used_elements,temp_list_local,raw_overlap_lists[parent]);
      
      if (verbal) sprintf(string_output,"Overlaps for Volume (pre mask) %d",parent);
      MPI_MASTER(
      if (verbal) print_1d_int_list(temporary_overlap_lists[parent],string_output);
      )
  }
  
  
  // The temporary_overlap_lists gives the raw overlap data for all volume pairs
  // The next tasks is to take the masks into account, so that a volume is only said to overlap another if all of these statements are true:
  // The volumes overlap each other
  // The mask's of volume 1 overlap volume 2
  // The mask's of volume 2 overlap volume 1
  // The mask's of volume 1 overlap the masks of volume 2
  
  
  int logic_var;
  int overlap_ANY,overlap_ANY_p,overlap_ANY_c;
  int mask_index,mask_index_c,mask_index_p;
  int mask_volume_index,mask_volume_index_p,mask_volume_index_c;

  for (parent=1;parent<number_of_volumes;parent++) {
    used_elements = 0;
    temp_list_local.elements[used_elements++] = 0; // Alwasy overlaps with the surrounding vacuum.
    for (child=1;child<number_of_volumes;child++) {
      if (child != parent) {
        logic_var = 1; // Assume the volumes overlap, and search for evidence that they do not.
      
        // First check if the volumes overlap
        if (0 == on_int_list(temporary_overlap_lists[parent],child)) logic_var = 0;
        
      
        // Check if child overlap with parents masks
        if (logic_var == 1 && Volumes[parent]->geometry.is_masked_volume == 1) {
          overlap_ANY = 0;
          for (mask_index=0;mask_index<Volumes[parent]->geometry.masked_by_list.num_elements;mask_index++) {
            mask_volume_index = Volumes[parent]->geometry.masked_by_list.elements[mask_index];
            if (0 == on_int_list(temporary_overlap_lists[mask_volume_index],child)) {
              if (Volumes[parent]->geometry.mask_mode == 1) {
                logic_var = 0;
                break;
              }
            } else overlap_ANY = 1;
          }
          if (Volumes[parent]->geometry.mask_mode == 2) logic_var = overlap_ANY;
        }
      
        // Check if parent overlap with childs masks
        if (logic_var == 1 && Volumes[child]->geometry.is_masked_volume == 1) {
          overlap_ANY = 0;
          for (mask_index=0;mask_index<Volumes[child]->geometry.masked_by_list.num_elements;mask_index++) {
            mask_volume_index = Volumes[child]->geometry.masked_by_list.elements[mask_index];
            if (0 == on_int_list(temporary_overlap_lists[mask_volume_index],parent)) {
              if (Volumes[child]->geometry.mask_mode == 1) {
                logic_var = 0;
                break;
              }
            } else overlap_ANY = 1;
          }
          if (Volumes[child]->geometry.mask_mode == 2) logic_var = overlap_ANY;
        }
      
        // Check if parents masks overlap childrens masks
        if (logic_var == 1 && Volumes[parent]->geometry.is_masked_volume == 1 && Volumes[child]->geometry.is_masked_volume == 1) {
          overlap_ANY = 0;
          for (mask_index_p=0;mask_index_p<Volumes[parent]->geometry.masked_by_list.num_elements;mask_index_p++) {
            mask_volume_index_p = Volumes[parent]->geometry.masked_by_list.elements[mask_index_p];
            overlap_ANY_p = 1;
            overlap_ANY_c = 0;
            for (mask_index_c=0;mask_index_c<Volumes[child]->geometry.masked_by_list.num_elements;mask_index_c++) {
              mask_volume_index_c = Volumes[child]->geometry.masked_by_list.elements[mask_index_c];
              if (0 == on_int_list(temporary_overlap_lists[mask_volume_index_p],mask_volume_index_c)) {
                if (Volumes[parent]->geometry.mask_mode == 1 && Volumes[child]->geometry.mask_mode == 1) {
                  // If both are in ALL mode and just one combination of masks does not overlap, neither does the common set
                  logic_var = 0;
                  break;
                }
                if (Volumes[parent]->geometry.mask_mode == 2 && Volumes[child]->geometry.mask_mode == 1) {
                  // If the parent is in ANY mode, but the child is in ALL, any one child not overlapping this parent mask, stops the chance for this parent mask
                  overlap_ANY_p = 0;
                  break;
                }
                
              } else {
                // Here because mask_volume_index_p and mask_volume_index_c does overlap
                if (Volumes[parent]->geometry.mask_mode == 1 && Volumes[child]->geometry.mask_mode == 2) {
                  // If the parent is in ALL mode and the child is in ANY mode, stop if a single parent volume does not overlap any child
                  overlap_ANY_c = 1;
                }
                if (Volumes[parent]->geometry.mask_mode == 2 && Volumes[child]->geometry.mask_mode == 2) {
                  // If both parent and child are in any mode, any one overlap between the masks is sufficient
                  overlap_ANY = 1;
                  // Could actually just commit to the overlap list here, and stop all loops.
                }
              }
              
            }
            if (Volumes[parent]->geometry.mask_mode == 1 && Volumes[child]->geometry.mask_mode == 2) logic_var = overlap_ANY_c;
            if (Volumes[parent]->geometry.mask_mode == 2 && Volumes[child]->geometry.mask_mode == 1 && overlap_ANY_p == 1) {
              // When parent is in any mode, and child is in ALL mode, any parent mask that overlaps all children masks is enough to end the parent loop
              logic_var = 1;
              break; // Without this break, only the last parent will matter
            }
            // if (overlap_ANY == 1) break; would speed things up a bit, but only after testing has been started and then it will be repeated
          }
          if (Volumes[parent]->geometry.mask_mode == 2 && Volumes[child]->geometry.mask_mode == 2) logic_var = overlap_ANY;
          // If both volumes have the ANY mode, just one case of overlap is enough.
        }
        
        // If all of the 4 statements above evaluate to true, the two volumes parent and child do overlap and it is added to the list.
        if (logic_var == 1) temp_list_local.elements[used_elements++] = child;
        
      }
    }
    // Allocate the actual overlap list with the new temp_list_local
    allocate_list_from_temp(used_elements,temp_list_local,true_overlap_lists[parent]);
    if (verbal) sprintf(string_output,"Overlaps for Volume (post mask) %d",parent);
    MPI_MASTER(
    if (verbal) print_1d_int_list(*true_overlap_lists[parent],string_output);
    )
  }
  
  // Clean up of dynamically allocated memory
  for(child=1;child<number_of_volumes;child++) free(temporary_overlap_lists[child].elements);
  free(temporary_overlap_lists);
  free(temp_list_local.elements);
  
};

void add_to_mask_intersect_list(struct pointer_to_1d_int_list *mask_intersect_list, int given_volume_index) {
    // A bit simpler than before
    if (on_int_list(*mask_intersect_list,given_volume_index) == 0)
      add_element_to_int_list(mask_intersect_list,given_volume_index);
}


void generate_intersect_check_lists(struct pointer_to_1d_int_list **overlap_lists,struct Volume_struct **Volumes, int number_of_volumes, int verbal) {
  // Generate intersection list for each volume
  MPI_MASTER(
  if (verbal) printf("\nGenerating intersect check lists -------------------- \n");
  )
  // Take overlap list for volume n
  // Remove entries with p < p(n)
  // Remove entries with parents on the list
  
  // Mask update: Mask volumes does not have priorities, do not do step 2 for mask volumes
  // Mask update: Instead of step 3 (Remove entries with parents on the list), move these entries to a Mask intersect list if that parent is masking the entry, otherwise remove
  
  // 1) Mask update: Take overlap list for volume n
  // 2) Mask update: Remove entries with p < p(n), except mask volumes, that are moved to another list A
  // 3) Mask update: Entries with parents on the list are removed (if these parents are masked, only remove the entry if it is a child of their parents masks)
  // 4) Possible optimization: Entries with parents on the A list, are moved to the appropriate mask list for this volume
  // 5) Mask update: Entries on list A are added again, if they are not parents of volume n
  // 6) Mask update: Entries on the main list that are masked are moved to the appropriate mask list for this volume, if their mask is on list A
  
  // Justification of the top algorithm:
  // No need to check intersections with something that does not overlap this volume, so start with intersect list and remove from that.
  // Entries with p < p(n) are "beneath" this volume, and thus doesn't cut it, mask volumes are however always "above" and are treated with care (moved to list A)
  // Entries with parents still on the list is removed, as it is impossible to intersect with them without first going through the parent
  // Entries with parents on the A list can only be seen if the ray is in that particular mask (the parent on the A list), so it can be added to the appropriate mask list for this volume
  // Now all masks that cut the volume are transfered back to the main list, the only masks that overlap and does not cut are parents of this volume, and these are thus not added
  // The entries on the list that are masked are added to the appropriate mask lists of this volume, but only if their mask is on the A list, as only masks on list A can have mask status 1 which is required for this to be relevant (this was true automatically for the ones added to the appropriate lists in the optimization step).
  
  
  // The intersect check list for volume 0 is done manually
  // Declare list for holding logic data for each index in the overlap list of this volume
  struct pointer_to_1d_int_list logic_list;
  logic_list.num_elements = overlap_lists[0]->num_elements;
  logic_list.elements = malloc(logic_list.num_elements * sizeof(int));
  if (!logic_list.elements) {
    fprintf(stderr,"Failure allocating list in Union function generate_overlap_lists 9 - Exit!\n");
    exit(EXIT_FAILURE);
  }
  // Declare similar list for List A
  struct pointer_to_1d_int_list mask_logic_list;
  mask_logic_list.num_elements = overlap_lists[0]->num_elements;
  mask_logic_list.elements = malloc(mask_logic_list.num_elements * sizeof(int));
  if (!mask_logic_list.elements) {
    fprintf(stderr,"Failure allocating list in Union function generate_overlap_lists 10 - Exit!\n");
    exit(EXIT_FAILURE);
  }
  
  int iterate;
  int overlap_index;
  
  // The intersect check lists for the remaining volumes are generated
  int volume_index,mask_volume_number,mask_index;
  for (volume_index = 0;volume_index < number_of_volumes;volume_index++) {
    
      // 1) Take overlap list for volume n
      logic_list.num_elements = overlap_lists[volume_index]->num_elements;
      logic_list.elements = malloc(logic_list.num_elements * sizeof(int));
      if (!logic_list.elements) {
	fprintf(stderr,"Failure allocating list in Union function generate_overlap_lists 11 - Exit!\n");
	exit(EXIT_FAILURE);
      }
      mask_logic_list.num_elements = overlap_lists[volume_index]->num_elements;
      mask_logic_list.elements = malloc(mask_logic_list.num_elements * sizeof(int));
      if (!mask_logic_list.elements) {
	fprintf(stderr,"Failure allocating list in Union function generate_overlap_lists 11 - Exit!\n");
	exit(EXIT_FAILURE);
      }
      
      
      if (Volumes[volume_index]->geometry.is_mask_volume == 1) {
        // Masks do not have any entries on their intersect check list, as they are never the current volume
        for (iterate=0;iterate<logic_list.num_elements;iterate++) logic_list.elements[iterate] = 0;
        for (iterate=0;iterate<mask_logic_list.num_elements;iterate++) mask_logic_list.elements[iterate] = 0;
      } else {
        for (iterate=0;iterate<logic_list.num_elements;iterate++) logic_list.elements[iterate] = 1;
        for (iterate=0;iterate<mask_logic_list.num_elements;iterate++) mask_logic_list.elements[iterate] = 0;
      }
      
      //2) Remove entries with p < p(n), except mask volumes, that are moved to another list A
      for (overlap_index = 0;overlap_index < overlap_lists[volume_index]->num_elements;overlap_index++) {
        
        if (overlap_lists[volume_index]->elements[overlap_index] == 0) logic_list.elements[overlap_index] = 0; // The surrounding vacuum has lower priority (and is not a mask)
        else if (Volumes[overlap_lists[volume_index]->elements[overlap_index]]->geometry.is_mask_volume == 1) {
           logic_list.elements[overlap_index] = 0;      // Remove this volume from the main list
           mask_logic_list.elements[overlap_index] = 1; // Add mask volumes to seperat list
        }
        else if (volume_index != 0) { // Only relevant to remove elements because of priority if the volume_index is different from 0, meaning the surrounding vacuum skips this step
           if (Volumes[overlap_lists[volume_index]->elements[overlap_index]]->geometry.priority_value < Volumes[volume_index]->geometry.priority_value) {
           // If the investigated volume on the overlap_list have lower priority than the volume with volume_index, remove it
           logic_list.elements[overlap_index] = 0; // Remove this volume from the list
           }
        }
      }
      
      // 3) Entries with parents on the list are removed
      for (overlap_index = 0;overlap_index < overlap_lists[volume_index]->num_elements;overlap_index++) {
        // Check if this overlap_lists[0]->elements[overlap_index] is a child of another member of the overlap list
        for (iterate = 0;iterate < overlap_lists[volume_index]->num_elements;iterate++) {
             if (iterate != overlap_index) { // Only necessary if a volume determines that it has itself as child, but a nice safety.
                // We are now checking if Volumes[overlap_lists[volume_index]->elements[iterate]]->geometry.children contains overlap_lists[overlap_index]
                // The && part is needed because we do not remove children of elements that have allready been removed because of their priority
                
                // if (on_int_list(Volumes[overlap_lists[volume_index]->elements[iterate]]->geometry.children,overlap_lists[volume_index]->elements[overlap_index]) && logic_list.elements[overlap_lists[volume_index]->elements[iterate]] == 1) {logic_list.elements[overlap_index] = 0; bug fixed on 3/4 2016
                if (on_int_list(Volumes[overlap_lists[volume_index]->elements[iterate]]->geometry.children,overlap_lists[volume_index]->elements[overlap_index]) && logic_list.elements[iterate] == 1) logic_list.elements[overlap_index] = 0;
                
                /*
                Explanation with simpler notation
                
                Overlap list of volume i = O_i
                Element j of overlap list i = O_i(j)
                
                Children list of volume i = C_i
                Element j on children list i = C_i(j)
                
                Priority of volume i p(i)
                
                for i = volumes
                    for j in O_i(j) 
                        logic(j) = 1;
                        if p(i) > p(O_i(j)) logic(j) = 0; // Remove if lower priority than the currently checked
                        
                        for k in O_i(k)
                            if (O_i(j) is contained on the list C_k && logic(k) == 1) logic(j) = 0
                */
                
                // 4) Entries with parents on the A list, are moved to the appropriate mask list for this volume
                if (on_int_list(Volumes[overlap_lists[volume_index]->elements[iterate]]->geometry.children,overlap_lists[volume_index]->elements[overlap_index]) && mask_logic_list.elements[iterate] == 1) {
                   logic_list.elements[overlap_index] = 0; // Remove from main list (Not strictly needed as it will be removed already if it is on list A)
                   // Add overlap_lists[volume_index]->elements[overlap_index] to volumes mask intersect list for mask with index overlap_lists[volume_index]->elements[iterate]
                   //add_to_mask_intersect_lists(&Volumes[volume_index]->geometry.mask_intersect_lists,overlap_lists[volume_index]->elements[iterate],overlap_lists[volume_index]->elements[overlap_index]);
                   // Simpler method that just collects all the masked intersect parts on a 1d_int list instead of seperating them for each mask
                   add_to_mask_intersect_list(&Volumes[volume_index]->geometry.mask_intersect_list,overlap_lists[volume_index]->elements[overlap_index]);
                }
                
             }
        }
      }
      
      // 5) Entries on list A are added again, if they are not parents of volume n
      // Time to add mask volumes removed back to the intersect list, if they are not parents of the current volume, meaning the current volume should not be a child of the mask
      for (overlap_index = 0;overlap_index < overlap_lists[volume_index]->num_elements;overlap_index++) {
        if (mask_logic_list.elements[overlap_index] == 1) {
          mask_volume_number = overlap_lists[volume_index]->elements[overlap_index];
          if (on_int_list(Volumes[mask_volume_number]->geometry.children,volume_index) == 0) logic_list.elements[overlap_index] = 1; // Add it back to the list
        }
      }
      
      // 6) Entries on the main list that are masked are moved to the appropriate mask list for this volume, if their mask is on list A
      int mask_index,mask_mode,found_index,logic_ALL;
      for (overlap_index = 0;overlap_index < overlap_lists[volume_index]->num_elements;overlap_index++) {
        if (logic_list.elements[overlap_index] == 1 && Volumes[overlap_lists[volume_index]->elements[overlap_index]]->geometry.is_masked_volume == 1) {
          logic_list.elements[overlap_index] = 0; // If the volume is masked, remove it from the intersect check list
          // Could actually keep it on the intersect list if the volume's mask is a parent of the current volume, now it will just be added in all cases
          
          // When ALL setting is used, all masks should overlap the current volume, otherwise the user probably made a mistake, this should be checked in error checking
          // When ANY setting is used, at least one mask should overlap the current volume, otherwise the user probably made a mistake, this should be checked in error checking
          // mask_mode == 1 => ALL / mask_mode == 2 => ANY
          mask_mode = Volumes[overlap_lists[volume_index]->elements[overlap_index]]->geometry.mask_mode;
          
          if (mask_mode == 1) {
            logic_ALL = 1;
            for (iterate=0;iterate<Volumes[overlap_lists[volume_index]->elements[overlap_index]]->geometry.masked_by_list.num_elements;iterate++) {
              mask_index = Volumes[overlap_lists[volume_index]->elements[overlap_index]]->geometry.masked_by_list.elements[iterate];
              
              if (on_int_list(*overlap_lists[volume_index],mask_index) == 0) {
                // If any one of the volumes masks do not overlap with this volume, there is no reason check intersections with the volume regardless of the mask status's
                logic_ALL = 0;
                break;
              }
            }
          if (logic_ALL == 1) {
            // If all of it's masks are overlapping the current volume, add it to all relevant mask intersect lists of this volume
            for (iterate=0;iterate<Volumes[overlap_lists[volume_index]->elements[overlap_index]]->geometry.masked_by_list.num_elements;iterate++) {
              mask_index = Volumes[overlap_lists[volume_index]->elements[overlap_index]]->geometry.masked_by_list.elements[iterate];
              //add_to_mask_intersect_lists(&Volumes[volume_index]->geometry.mask_intersect_lists,mask_index,overlap_lists[volume_index]->elements[overlap_index]);
              // Adding the masked intersect list elements to another list
              add_to_mask_intersect_list(&Volumes[volume_index]->geometry.mask_intersect_list,overlap_lists[volume_index]->elements[overlap_index]);
            }
          }
            
            
          } else if (mask_mode == 2) {
          // When in ANY mode, the problem is easier, as not all masks have to overlap the volume, and we can add each one to the list
            for (iterate=0;iterate<Volumes[overlap_lists[volume_index]->elements[overlap_index]]->geometry.masked_by_list.num_elements;iterate++) {
              mask_index = Volumes[overlap_lists[volume_index]->elements[overlap_index]]->geometry.masked_by_list.elements[iterate];
            
              if (on_int_list(*overlap_lists[volume_index],mask_index) == 1) {
                //add_to_mask_intersect_lists(&Volumes[volume_index]->geometry.mask_intersect_lists,mask_index,overlap_lists[volume_index]->elements[overlap_index]);
                // Adding the masked intersect list elements to another list
                add_to_mask_intersect_list(&Volumes[volume_index]->geometry.mask_intersect_list,overlap_lists[volume_index]->elements[overlap_index]);
              }
            }
            
          }
        }
      }
      
      
      Volumes[volume_index]->geometry.intersect_check_list.num_elements = sum_int_list(logic_list);
      Volumes[volume_index]->geometry.intersect_check_list.elements = malloc(Volumes[volume_index]->geometry.intersect_check_list.num_elements * sizeof(int));
      if (!Volumes[volume_index]->geometry.intersect_check_list.elements) {
	fprintf(stderr,"Failure allocating list in Union function generate_overlap_lists 12 - Exit!\n");
	exit(EXIT_FAILURE);
      }
	
      iterate = 0;
      for (overlap_index = 0;overlap_index < overlap_lists[volume_index]->num_elements;overlap_index++) {
            if (logic_list.elements[overlap_index])   Volumes[volume_index]->geometry.intersect_check_list.elements[iterate++] = overlap_lists[volume_index]->elements[overlap_index];
      }
      free(logic_list.elements); // Need to be careful with names for variables to be freed, as they can collide with names in main because of automatic declaration of external variables
      free(mask_logic_list.elements);
      
      
      char string_output[128];
      MPI_MASTER(
      if (verbal) sprintf(string_output,"Intersect check list for Volume %d",volume_index);
      if (verbal) print_1d_int_list(Volumes[volume_index]->geometry.intersect_check_list,string_output);
      if (verbal) sprintf(string_output,"Mask intersect check list for Volume %d",volume_index);
      if (verbal) print_1d_int_list(Volumes[volume_index]->geometry.mask_intersect_list,string_output)
      );
  }
};

void generate_parents_lists(struct pointer_to_1d_int_list **parents_lists, struct Volume_struct **Volumes, int number_of_volumes, int verbal, int mask_mode) {
  // Function for generating parent lists for all volumes
  // A volume m has n as a parent, if volume n has volume m as a child
  
  // if mask_mode == 0, masks are ignored, if mask_mode == 1, masks are taken into account.
  
  MPI_MASTER(
  if (verbal) {
    if (mask_mode == 1)
        printf("\nGenerating parents lists ---------------------------- \n");
    else if (mask_mode == 0)
        printf("\nGenerating parents lists (ignoring masks) ----------- \n");
    else {
        printf("Error, the function parents_lists got a non defined mask_mode");
        exit(EXIT_FAILURE);
    }
  }
  
  )
  // Volume iterate has volume p as a parent, if volume p has volume iterate as child.
  
  struct pointer_to_1d_int_list temp_list_local;
  temp_list_local.num_elements = number_of_volumes;
  temp_list_local.elements = malloc(number_of_volumes*sizeof(int));
  if (!temp_list_local.elements) {
    fprintf(stderr,"Failure allocating list in Union function generate_parents_lists 1 - Exit!\n");
    exit(EXIT_FAILURE);
  }
  // Loop over
  int iterate,parent,used_elements;
  for (iterate = 0;iterate < number_of_volumes;iterate++) {
    // clear temp list
    used_elements = 0;
    parents_lists[iterate] = malloc(sizeof(struct pointer_to_1d_int_list));
    if (!parents_lists[iterate]) {
      fprintf(stderr,"Failure allocating list in Union function generate_parents_lists 2 - Exit!\n");
      exit(EXIT_FAILURE);
    }
    for (parent = 0;parent < number_of_volumes;parent++) {
        if (on_int_list(Volumes[parent]->geometry.children,iterate))
          if (mask_mode == 1 || (Volumes[parent]->geometry.is_mask_volume == 0 && Volumes[iterate]->geometry.is_mask_volume == 0))
            temp_list_local.elements[used_elements++] = parent;
    }
      allocate_list_from_temp(used_elements,temp_list_local,parents_lists[iterate]);
      
      char string_output[128];
      MPI_MASTER(
      if (verbal) sprintf(string_output,"Parents for Volume %d",iterate);
      if (verbal) print_1d_int_list(*parents_lists[iterate],string_output);
      )
  }
  free(temp_list_local.elements);

};

void generate_true_parents_lists(struct pointer_to_1d_int_list **parents_lists, struct pointer_to_1d_int_list **true_children_lists, struct Volume_struct **Volumes, int number_of_volumes, int verbal, int mask_mode) {
  // Function for generating parent lists for all volumes
  // A volume m has n as a parent, if volume n has volume m as a child
  
  // if mask_mode == 0, masks are ignored, if mask_mode == 1, masks are taken into account.
  
  MPI_MASTER(
  if (verbal) {
    if (mask_mode == 1)
        printf("\nGenerating parents lists ---------------------------- \n");
    else if (mask_mode == 0)
        printf("\nGenerating parents lists (ignoring masks) ----------- \n");
    else {
        printf("Error, the function parents_lists got a non defined mask_mode");
        exit(EXIT_FAILURE);
    }
  }
  
  )
  // Volume iterate has volume p as a parent, if volume p has volume iterate as child.
  
  struct pointer_to_1d_int_list temp_list_local;
  temp_list_local.num_elements = number_of_volumes;
  temp_list_local.elements = malloc(number_of_volumes*sizeof(int));
  if (!temp_list_local.elements) {
    fprintf(stderr,"Failure allocating list in Union function generate_true_parents_lists 1 - Exit!\n");
    exit(EXIT_FAILURE);
  }
  
  // Loop over
  int iterate,parent,used_elements;
  for (iterate = 0;iterate < number_of_volumes;iterate++) {
    // clear temp list
    used_elements = 0;
    parents_lists[iterate] = malloc(sizeof(struct pointer_to_1d_int_list)); // allocate_list_from_temp allocates
    if (!parents_lists[iterate]) {
      fprintf(stderr,"Failure allocating list in Union function generate_true_parents_lists 2 - Exit!\n");
      exit(EXIT_FAILURE);
    }
    for (parent = 0;parent < number_of_volumes;parent++) {
        //if (on_int_list(Volumes[parent]->geometry.children,iterate))
        if (on_int_list(*true_children_lists[parent],iterate))
          if (mask_mode == 1 || (Volumes[parent]->geometry.is_mask_volume == 0 && Volumes[iterate]->geometry.is_mask_volume == 0))
            temp_list_local.elements[used_elements++] = parent;
    }
      allocate_list_from_temp(used_elements,temp_list_local,parents_lists[iterate]);
      
      char string_output[128];
      MPI_MASTER(
      if (verbal) sprintf(string_output,"Parents for Volume %d",iterate);
      if (verbal) print_1d_int_list(*parents_lists[iterate],string_output);
      )
  }
  free(temp_list_local.elements);

};

void generate_intersect_check_lists_experimental(struct pointer_to_1d_int_list **true_overlap_lists, struct pointer_to_1d_int_list **raw_overlap_lists, struct pointer_to_1d_int_list **parents_lists, struct pointer_to_1d_int_list **true_parents_lists , struct Volume_struct **Volumes, int number_of_volumes, int verbal) {
  // Generates the intersect_check_list and mask_intersect_list for each Volume.
  /*
  Description of needed lists for volume n:
  Children list: List of volumes that is contained within volume n (is stored in the Volumes struct)
  True Children list: List of volumes that when masked by their masks is contained within volume n when masked by it's masks
  
  Parents list: List of volumes that contains volume n
  True parents list: List of volumes that when masked by their masks contains volume n when it is masked by it's masks
  
  raw overlap list: List of volumes whos geometry overlaps the geometry of volume n
  true overlap list: List of volumes whos geometry overlaps the geometry of volume n when the masks of both volumes are applied
  
  
  
  The algorithm:
  
  1) Take the true overlap list for volume n
  2) remove parents of n (normal parent list, with masks)
  3) remove volumes that do not mask n and are true parents of n
  4) remove volumes that are not masks and have lower priority than n
  5) remove volumes that have at least one true parent on the list (in step 4) that is not a mask volume
  6) split the list into two, the intersect_check_list which is all non masked volumes still on the list, and the mask_intersect_list which is the masked volumes
  7) remove volumes on the mask_intersect_list whos mask does not overlap (standard overlap list) n
  
  In step 5 the order in which the volumes are tested and removed may matter, so it is specifically stated that it is as the list looked in step 4.
  */
  
  MPI_MASTER(
  if (verbal) printf("\nGenerating intersect check lists -------------------- \n");
  )
  
  struct pointer_to_1d_int_list work_list;
  struct pointer_to_1d_int_list logic_list;
  
  int volume_index,iterate,parent,mask_index,masked_volume_index,ANY_logic,true_parent_volume_number;
  int *mask_check,*mask_start;
  for (volume_index = 0;volume_index < number_of_volumes;volume_index++) {
    
    // 1) Take the true overlap list for volume n
    // Create copy of true_overlap_lists to work with
    
    if (Volumes[volume_index]->geometry.is_mask_volume == 1) {
      // Bug fixed on 26/11/2016, do not create intersection lists for masks as they are not used, and affects destinations lists in a problematic way
      Volumes[volume_index]->geometry.intersect_check_list.num_elements = 0;
      Volumes[volume_index]->geometry.mask_intersect_list.num_elements = 0;
      
    } else {
      work_list.num_elements = true_overlap_lists[volume_index]->num_elements;
      work_list.elements = malloc(work_list.num_elements * sizeof(int));
      if (!work_list.elements) {
	fprintf(stderr,"Failure allocating list in Union function generate_intersect_check_lists_experimental 1 - Exit!\n");
	exit(EXIT_FAILURE);
      }
      for (iterate=0;iterate<work_list.num_elements;iterate++) work_list.elements[iterate] = true_overlap_lists[volume_index]->elements[iterate];
    
    //if (verbal) print_1d_int_list(work_list,"After 1)");
    
    
    //2) remove parents of n (normal parent list, with masks)
    for (iterate=work_list.num_elements-1;iterate>-1;iterate--) {
      if (on_int_list(*parents_lists[volume_index],work_list.elements[iterate]))
        remove_element_in_list_by_index(&work_list,iterate);
    }
    
    //if (verbal) print_1d_int_list(work_list,"After 2)");
    
    //3) remove volumes that do not mask n and are true parents of n
    for (iterate=work_list.num_elements-1;iterate>-1;iterate--) {
      if (on_int_list(Volumes[volume_index]->geometry.masked_by_list,work_list.elements[iterate]) == 0 && on_int_list(*true_parents_lists[volume_index],work_list.elements[iterate]))
        remove_element_in_list_by_index(&work_list,iterate);
    }
    
    //if (verbal) print_1d_int_list(work_list,"After 3)");
    
    //4) remove volumes that are not masks and have lower priority than n
    for (iterate=work_list.num_elements-1;iterate>-1;iterate--) {
      if (Volumes[work_list.elements[iterate]]->geometry.is_mask_volume == 0 && Volumes[work_list.elements[iterate]]->geometry.priority_value < Volumes[volume_index]->geometry.priority_value)
        remove_element_in_list_by_index(&work_list,iterate);
    }
    
    //if (verbal) print_1d_int_list(work_list,"After 4)");
    
    //5) remove volumes that have at least one true parent on the list (in step 4) that is not a mask volume
    // Here a logic_list is used to not have the order of removal matter
    logic_list.num_elements = work_list.num_elements;
    logic_list.elements = malloc(logic_list.num_elements * sizeof(int));
    if (!logic_list.elements) {
      fprintf(stderr,"Failure allocating list in Union function generate_intersect_check_lists_experimental 2 - Exit!\n");
      exit(EXIT_FAILURE);
    }
    for(iterate=0;iterate<logic_list.num_elements;iterate++) logic_list.elements[iterate] = 1;
    
    for (iterate=0;iterate<work_list.num_elements;iterate++) {
      for (parent=0;parent<true_parents_lists[work_list.elements[iterate]]->num_elements;parent++) {
        true_parent_volume_number = true_parents_lists[work_list.elements[iterate]]->elements[parent];
        if (on_int_list(work_list,true_parent_volume_number) && Volumes[true_parent_volume_number]->geometry.is_mask_volume == 0) {
          // Since element number iterate on the work list have a true parent on the work list, it can be removed
          logic_list.elements[iterate] = 0;
          break;
        }
      }
    }
    
    // Now the elements marked for removal can be removed without interfering in the operation
    for (iterate=work_list.num_elements-1;iterate>-1;iterate--) {
      if (logic_list.elements[iterate] == 0) remove_element_in_list_by_index(&work_list,iterate);
    }
    free(logic_list.elements);
    
    //if (verbal) print_1d_int_list(work_list,"After 5)");
      
    //6) split the list into two, the intersect_check_list which is all non masked volumes still on the list, and the mask_intersect_list which is the masked volumes
    
    Volumes[volume_index]->geometry.intersect_check_list.num_elements = 0;
    Volumes[volume_index]->geometry.mask_intersect_list.num_elements = 0;
    
    for (iterate=0;iterate<work_list.num_elements;iterate++) {
      if (Volumes[work_list.elements[iterate]]->geometry.is_masked_volume == 1) {
        add_element_to_int_list(&Volumes[volume_index]->geometry.mask_intersect_list,work_list.elements[iterate]);
      } else {
        add_element_to_int_list(&Volumes[volume_index]->geometry.intersect_check_list,work_list.elements[iterate]);
      }
    }
    
    //if (verbal) print_1d_int_list(Volumes[volume_index]->geometry.intersect_check_list,"After 6) intersect_check_list");
    //if (verbal) print_1d_int_list(Volumes[volume_index]->geometry.mask_intersect_list,"After 6) mask_intersect_list");
    
    //7) remove volumes on the mask_intersect_list whos masks does not overlap (standard overlap list) n
    for (iterate=Volumes[volume_index]->geometry.mask_intersect_list.num_elements-1;iterate>-1;iterate--) {
      // Need to check if the volumes masking Volumes[volume_index]->geometry.mask_intersect_list.elements[iterate] overlaps with n
      masked_volume_index = Volumes[volume_index]->geometry.mask_intersect_list.elements[iterate];
      
      if (Volumes[masked_volume_index]->geometry.mask_mode == 1) {// All mode, if just one does not overlap, remove the element
        for (mask_start=mask_check=Volumes[masked_volume_index]->geometry.masked_by_list.elements;mask_check-mask_start<Volumes[masked_volume_index]->geometry.masked_by_list.num_elements;mask_check++) {
          if (on_int_list(*raw_overlap_lists[volume_index],*mask_check) == 0) {
            remove_element_in_list_by_index(&Volumes[volume_index]->geometry.mask_intersect_list,iterate);
            break;
          }
        }
      } else { // ANY mode, just one need to overlap in order to keep the element
        ANY_logic = 0;
        for (mask_start=mask_check=Volumes[masked_volume_index]->geometry.masked_by_list.elements;mask_check-mask_start<Volumes[masked_volume_index]->geometry.masked_by_list.num_elements;mask_check++) {
          if (on_int_list(*raw_overlap_lists[volume_index],*mask_check) == 1) {
            ANY_logic = 1;
            break;
          }
        }
        if (ANY_logic == 0) remove_element_in_list_by_index(&Volumes[volume_index]->geometry.mask_intersect_list,iterate);
      }
    }
    
    if (work_list.num_elements > 0) free(work_list.elements);
    }
    
    
    //if (verbal) print_1d_int_list(Volumes[volume_index]->geometry.mask_intersect_list,"After 7) mask_intersect_list");
    
    char string_output[128];
    MPI_MASTER(
    if (verbal) sprintf(string_output,"Intersect check list for Volume %d",volume_index);
    if (verbal) print_1d_int_list(Volumes[volume_index]->geometry.intersect_check_list,string_output);
    if (verbal) sprintf(string_output,"Mask intersect check list for Volume %d",volume_index);
    if (verbal) print_1d_int_list(Volumes[volume_index]->geometry.mask_intersect_list,string_output);
    )
    
    
  }
}

void generate_grandparents_lists(struct pointer_to_1d_int_list **grandparents_lists, struct pointer_to_1d_int_list **parents_lists, int number_of_volumes, int verbal) {
  // Function for generating grandparents lists
  // Volume iterate has volume p as a grandparent, if volume p has a parent T, who has volume iterate as parent.
  // Alternertively:
  // Volume iterate has volume p as a grandparent, if volume p have a child that is volume iterate's parent.
  
  MPI_MASTER(
  if (verbal) printf("\nGenerating grandparents lists ----------------------- \n");
  )

  struct pointer_to_1d_int_list common;
  common.num_elements = number_of_volumes;
  common.elements = malloc(common.num_elements*sizeof(int)); // Maximum needed space.
  if (!common.elements) {
    fprintf(stderr,"Failure allocating list in Union function generate_grandparents_lists 1 - Exit!\n");
    exit(EXIT_FAILURE);
  }
  struct pointer_to_1d_int_list temp_list_local;
  temp_list_local.num_elements = number_of_volumes;
  temp_list_local.elements = malloc(number_of_volumes*sizeof(int));
  if (!temp_list_local.elements) {
    fprintf(stderr,"Failure allocating list in Union function generate_grandparents_lists 2 - Exit!\n");
    exit(EXIT_FAILURE);
  }

  int iterate,reset_int,parent,child,used_elements;
  for (iterate = 0;iterate < number_of_volumes;iterate++) {
    // clear temp list
    used_elements = 0;

    for (reset_int=0; reset_int<number_of_volumes; reset_int++) common.elements[reset_int] = -1; // Initialize to impossible volume ids
    for (reset_int=0; reset_int<number_of_volumes; reset_int++) temp_list_local.elements[reset_int] = -1; // Initialize to impossible volume ids

    grandparents_lists[iterate] = malloc(sizeof(struct pointer_to_1d_int_list));
    if (!grandparents_lists[iterate]) {
      fprintf(stderr,"Failure allocating list in Union function generate_grandparents_lists 3 - Exit!\n");
      exit(EXIT_FAILURE);
    }

    
    for (parent = 0; parent<parents_lists[iterate]->num_elements; parent++) {
        // parent number p parents_lists[iterate].elements.[p] in the parent_list for iterate.
        on_both_int_lists(parents_lists[parents_lists[iterate]->elements[parent]], parents_lists[iterate], &common);
        // returns a pointer_to_1d_list, with all the elements that are in common.
        for (child = 0;child < common.num_elements;child++) {
            // Need to make sure the element is not already on the list
            if (0 == on_int_list(temp_list_local, common.elements[child])) {
              temp_list_local.elements[used_elements++] = common.elements[child];
            }
        }
    }
    allocate_list_from_temp(used_elements, temp_list_local, grandparents_lists[iterate]);
  
    char string_output[128];
    MPI_MASTER(
      if (verbal) sprintf(string_output,"Grandparents for Volume %d", iterate);
      if (verbal) print_1d_int_list(*grandparents_lists[iterate], string_output);
    )
  }
  free(temp_list_local.elements);
  free(common.elements);
};


void generate_destinations_lists_experimental(struct pointer_to_1d_int_list **true_overlap_lists, struct pointer_to_1d_int_list **true_children_lists, struct pointer_to_1d_int_list **true_parents_lists, struct pointer_to_1d_int_list **true_grandparents_lists, struct Volume_struct **Volumes, int number_of_volumes, int verbal) {
  // Generates destinations list for for all volumes
  // Current implementation uses true_parents_lists and true_grandparents_lists that are generated as if no masks were defined

  MPI_MASTER(
  if (verbal) printf("\nGenerating destinations lists ----------------------- \n");
  )
  // Volume 0 has an hardcoded empty destinations list
  Volumes[0]->geometry.destinations_list.num_elements = 0;

  struct pointer_to_1d_int_list work_list;
  int volume_index,iterate,iterate2,found_index,I_index,I_volume;
  
  for (volume_index=1;volume_index<number_of_volumes;volume_index++) {
    
    // 1) Take the true overlap list for volume n
    // Create copy of true_overlap_lists to work with
    work_list.num_elements = true_overlap_lists[volume_index]->num_elements;
    work_list.elements = malloc(work_list.num_elements * sizeof(int));
    if (!work_list.elements) {
      fprintf(stderr,"Failure allocating list in Union function generate_destinations_lists_experimental 1 - Exit!\n");
      exit(EXIT_FAILURE);
    }
    for (iterate=0;iterate<work_list.num_elements;iterate++) work_list.elements[iterate] = true_overlap_lists[volume_index]->elements[iterate];
    
    //if (verbal) print_1d_int_list(work_list,"After 1)");
    
    // 2) Remove elements from n's intersection list
    for (iterate=work_list.num_elements-1;iterate>-1;iterate--) {
      if (on_int_list(Volumes[volume_index]->geometry.intersect_check_list,work_list.elements[iterate]))
        remove_element_in_list_by_index(&work_list,iterate);
    }
    
    //if (verbal) print_1d_int_list(work_list,"After 2)");
  
    // 3) Remove true children of non-mask elements on n's intersection list
    for (I_index=0;I_index<Volumes[volume_index]->geometry.intersect_check_list.num_elements;I_index++) {
      I_volume = Volumes[volume_index]->geometry.intersect_check_list.elements[I_index];
      if (Volumes[I_volume]->geometry.is_mask_volume == 0) {
        for (iterate=0;iterate<true_children_lists[I_volume]->num_elements;iterate++) {
          remove_element_in_list_by_value(&work_list,true_children_lists[I_volume]->elements[iterate]);
        }
      }
    }
    
    //if (verbal) print_1d_int_list(work_list,"After 3)");
    
    // 4) Remove elements from mask intersection list
    for (iterate=work_list.num_elements-1;iterate>-1;iterate--) {
      if (on_int_list(Volumes[volume_index]->geometry.mask_intersect_list,work_list.elements[iterate]))
        remove_element_in_list_by_index(&work_list,iterate);
    }
    
    //if (verbal) print_1d_int_list(work_list,"After 4)");
    
    // 5) Remove true children of elements on n's mask intersection list
    for (I_index=0;I_index<Volumes[volume_index]->geometry.mask_intersect_list.num_elements;I_index++) {
      I_volume = Volumes[volume_index]->geometry.mask_intersect_list.elements[I_index];
      if (Volumes[I_volume]->geometry.is_mask_volume == 0) {
        for (iterate=0;iterate<true_children_lists[I_volume]->num_elements;iterate++) {
          remove_element_in_list_by_value(&work_list,true_children_lists[I_volume]->elements[iterate]);
        }
      }
    }
    
    //if (verbal) print_1d_int_list(work_list,"After 5)");
    
    // 6) Remove true children of n
    for (iterate=0;iterate<true_children_lists[volume_index]->num_elements;iterate++)
      remove_element_in_list_by_value(&work_list,true_children_lists[volume_index]->elements[iterate]);
      
    //if (verbal) print_1d_int_list(work_list,"After 6)");
      
    // 7) Remove true grandparents of n
    for (iterate=0;iterate<true_grandparents_lists[volume_index]->num_elements;iterate++)
      remove_element_in_list_by_value(&work_list,true_grandparents_lists[volume_index]->elements[iterate]);
      
    //if (verbal) print_1d_int_list(work_list,"After 7)");
      
    // 8) Remove mask volumes
    for (iterate=work_list.num_elements-1;iterate>-1;iterate--) {
      if (Volumes[work_list.elements[iterate]]->geometry.is_mask_volume == 1)
        remove_element_in_list_by_index(&work_list,iterate);
    }
    
    //if (verbal) print_1d_int_list(work_list,"After 8)");
    
    // 9) Remove true parents of n on the list that has other true parents of n on the list with higher priority
    for (iterate=work_list.num_elements-1;iterate>-1;iterate--) {
      if (on_int_list(*true_parents_lists[volume_index],work_list.elements[iterate])){
        // work_list.elements[iterate] is the volume index of a volume that is a true parent of n
        for (iterate2=0;iterate2<work_list.num_elements;iterate2++) {
          if (on_int_list(*true_parents_lists[volume_index],work_list.elements[iterate2]) && iterate != iterate2) {
            if (Volumes[work_list.elements[iterate]]->geometry.priority_value < Volumes[work_list.elements[iterate2]]->geometry.priority_value) {
              //printf("Removing element number %d (V%d) because element number %d (V%d) had higher priority \n",iterate,work_list.elements[iterate],iterate2,work_list.elements[iterate2]);
              remove_element_in_list_by_index(&work_list,iterate);
              break; // Missing break inserted on 14/9/2016
            }
          }
        }
      }
    }
    
    //if (verbal) print_1d_int_list(work_list,"After 9)");
    
    // 10) The remaining list is the destinations_list
    Volumes[volume_index]->geometry.destinations_list.num_elements = work_list.num_elements;
    Volumes[volume_index]->geometry.destinations_list.elements = malloc(Volumes[volume_index]->geometry.destinations_list.num_elements*sizeof(int));
    if (!Volumes[volume_index]->geometry.destinations_list.elements) {
      fprintf(stderr,"Failure allocating list in Union function generate_destinations_lists_experimental 2 - Exit!\n");
      exit(EXIT_FAILURE);
    }
    
    for (iterate=0;iterate<work_list.num_elements;iterate++)
      Volumes[volume_index]->geometry.destinations_list.elements[iterate] = work_list.elements[iterate];
    
    // Clean up after work_list so the next can be allocated
    free(work_list.elements);
  
  
    char string_output[128];
    MPI_MASTER(
    if (verbal) sprintf(string_output,"Destinations list for Volume %d",volume_index);
    if (verbal) print_1d_int_list(Volumes[volume_index]->geometry.destinations_list,string_output);
    )

  }

};

void generate_destinations_list(int N_volume,struct Volume_struct **Volumes,struct pointer_to_1d_int_list original_overlap_list,struct pointer_to_1d_int_list *parent_list, struct pointer_to_1d_int_list *grandparent_list) {
    // This function generates the destinations_list for a single volume index, N_volume
    // The destination list describes which volumes a ray can enter when leaving N_volume
    // Each of the 6 steps for the algorithm is commented individually, and debug print statements are available
    
    // Mask update: Mask volumes are to be removed from all destinations_list as they can not be the current volume
    // It is not that simple, as some volume will then get empty destination lists. Need to revise this algorithm.
    
    // 1) Start with the overlap list of volume N
    // 2) remove all Volumes from the overlap list of N, which is also on the intersect_check_list
    // 3) remove the children of volumes removed in step 2)
    // 4) remove the children of N
    // 5) remove the grandparents of N
    // 6) remove volumes with lower priority than parents of N still on the list
    // 7) The remaing list is the destinations list
    
    
    // The destination list system should run without masks altogether, meaning parent and grandparent lists should not use them,
    //  and all masks should be removed in step 2
    
    // 1) Start with the overlap list of volume N
    // 2) remove all masks on the list
    // 3) remove all Volumes from the overlap list of N, which is also on the intersect_check_list
    // 4) remove the children of volumes removed in step 2) (if the removed volume was not a mask )
    // 5) remove the children of N
    // 6) remove the grandparents of N
    // 7) remove volumes with lower priority than parents of N still on the list
    // 8) The remaing list is the destinations list
    
    

    // 1) Start with the overlap list of volume N
    struct pointer_to_1d_int_list overlap_list;
    overlap_list.num_elements = original_overlap_list.num_elements;
    overlap_list.elements = malloc(overlap_list.num_elements*sizeof(int));
    if (!overlap_list.elements) {
      fprintf(stderr,"Failure allocating list in Union function generate_destinations_lists 1 - Exit!\n");
      exit(EXIT_FAILURE);
    }
    int iterate;
    for (iterate=0;iterate<overlap_list.num_elements;iterate++) overlap_list.elements[iterate] = original_overlap_list.elements[iterate];
    
    char string_output[124];
    // sprintf(string_output,"Destinations list for Volume %d step 1",N_volume);
    // print_1d_int_list(overlap_list,string_output);
    
    // 2) remove all masks
    for (iterate=overlap_list.num_elements-1;iterate > -1;iterate--) {
        if (Volumes[overlap_list.elements[iterate]]->geometry.is_mask_volume == 1) {
            remove_element_in_list_by_index(&overlap_list,iterate);
        }
    }
    
    // 3) remove all Volumes from the overlap list of N, which is also on the intersect_check_list
    struct pointer_to_1d_int_list removed_under_2;
    removed_under_2.num_elements = 0;
    int to_check;
    removed_under_2.elements = malloc( Volumes[N_volume]->geometry.intersect_check_list.num_elements * sizeof(int));
    if (!removed_under_2.elements) {
      fprintf(stderr,"Failure allocating list in Union function generate_destinations_lists 2 - Exit!\n");
      exit(EXIT_FAILURE);
    }
    for (iterate=0;iterate < Volumes[N_volume]->geometry.intersect_check_list.num_elements;iterate++) {
        to_check = Volumes[N_volume]->geometry.intersect_check_list.elements[iterate];
        if (on_int_list(overlap_list,to_check)) {
            removed_under_2.elements[removed_under_2.num_elements++] = to_check;
            remove_element_in_list_by_value(&overlap_list,to_check);
        }
    }
    
    // sprintf(string_output,"Destinations list for Volume %d step 2",N_volume);
    // print_1d_int_list(overlap_list,string_output);
    
    // 4) remove the children of volumes removed in step 2)
    int children;
    for (iterate=0;iterate<removed_under_2.num_elements;iterate++){
        for (children = 0;children < Volumes[removed_under_2.elements[iterate]]->geometry.children.num_elements;children++) {
          remove_element_in_list_by_value(&overlap_list,Volumes[removed_under_2.elements[iterate]]->geometry.children.elements[children]);
        }
    }
    
    // sprintf(string_output,"Destinations list for Volume %d step 3",N_volume);
    // print_1d_int_list(overlap_list,string_output);
    
    // 5) remove the children of N
    for (children = 0;children < Volumes[N_volume]->geometry.children.num_elements;children++) {
        remove_element_in_list_by_value(&overlap_list,Volumes[N_volume]->geometry.children.elements[children]);
    }
    
    // sprintf(string_output,"Destinations list for Volume %d step 4",N_volume);
    // print_1d_int_list(overlap_list,string_output);
    
    // 6) remove the grandparents of N
    int grandparent;
    for (grandparent = 0;grandparent < grandparent_list->num_elements;grandparent++) {
        remove_element_in_list_by_value(&overlap_list,grandparent_list->elements[grandparent]);
    }
    
    // sprintf(string_output,"Destinations list for Volume %d step 5",N_volume);
    // print_1d_int_list(overlap_list,string_output);
    
    // 7) remove volumes with lower priority than parents of N still on the list
    struct pointer_to_1d_int_list logic_list;
    logic_list.num_elements = overlap_list.num_elements;
    logic_list.elements=NULL;
    if (logic_list.num_elements>0) {
      logic_list.elements = malloc(logic_list.num_elements*sizeof(int));
    }
    if (!logic_list.elements) {
      fprintf(stderr,"Failure allocating list in Union function generate_destinations_lists 3 - Exit!\n");
      exit(EXIT_FAILURE);
    }
    for (iterate=0;iterate<logic_list.num_elements;iterate++) logic_list.elements[iterate] = 1;
    
    int parent;
    for (parent=0;parent<parent_list->num_elements;parent++) {
      if (on_int_list(overlap_list,parent_list->elements[parent])) {
        // Found a parent to N on the list, now check all other elements on the list
        for (iterate=0;iterate<overlap_list.num_elements;iterate++) {
          if (parent_list->elements[parent] != overlap_list.elements[iterate]) {
            // if the element iterate have lower priority than the parent, remove it from the list
            if (Volumes[overlap_list.elements[iterate]]->geometry.priority_value < Volumes[parent_list->elements[parent]]->geometry.priority_value) logic_list.elements[iterate] = 0;
          }
        }
      }
    }
    
    // 8) The remaing list is the destinations list
    Volumes[N_volume]->geometry.destinations_list.num_elements = sum_int_list(logic_list);
    Volumes[N_volume]->geometry.destinations_list.elements = malloc(Volumes[N_volume]->geometry.destinations_list.num_elements * sizeof(int));
    if (!Volumes[N_volume]->geometry.destinations_list.elements) {
      fprintf(stderr,"Failure allocating list in Union function generate_destinations_lists 4 - Exit!\n");
      exit(EXIT_FAILURE);
    }
    
    int overlap_index,used_elements=0;
    for (overlap_index=0;overlap_index < overlap_list.num_elements;overlap_index++)
      if (logic_list.elements[overlap_index] == 1)
        Volumes[N_volume]->geometry.destinations_list.elements[used_elements++] = overlap_list.elements[overlap_index];
    
    if (overlap_list.num_elements>0) free(overlap_list.elements);
    if (logic_list.num_elements>0) free(logic_list.elements);

    // Clean up memory
    free(removed_under_2.elements);

    };

void generate_destinations_lists(struct pointer_to_1d_int_list **grandparents_lists, struct pointer_to_1d_int_list **parents_lists, struct pointer_to_1d_int_list **overlap_lists,struct Volume_struct **Volumes, int number_of_volumes, int verbal) {
    // Because of the complexity of the algortithm for generating the destinations list, the function is made for a single volume at the time to keep the notation simpler
    // This funtion runs the destinations list function for each volume
    MPI_MASTER(
    if (verbal) printf("\nGenerating destinations lists ----------------------- \n");
    )
    
    int volume_index;
    for (volume_index = 0;volume_index < number_of_volumes;volume_index++) {
      generate_destinations_list(volume_index,Volumes,*overlap_lists[volume_index],parents_lists[volume_index],grandparents_lists[volume_index]);
      
      char string_output[128];
      if (verbal) sprintf(string_output,"Destinations list for Volume %d",volume_index);
      MPI_MASTER(
      if (verbal) print_1d_int_list(Volumes[volume_index]->geometry.destinations_list,string_output);
      )
    }
};


void generate_reduced_destinations_lists(struct pointer_to_1d_int_list **parents_lists, struct Volume_struct **Volumes,int number_of_volumes,int verbal) {
    // The reduced destination list is the destination list of a volume, where each element that has a parent on the same destination list is removed
    // This list is to be fed to the which_volume function, as this funtion will automatically search through the direct children in a tree like manner
    // The optimization reduces the number of calculations of within functions in nested geometries.
    
    MPI_MASTER(
    if (verbal) printf("\nGenerating reduced destination lists ----------------------- \n");
    )
    
    struct pointer_to_1d_int_list logic_list;
  
    int volume_index,checked_dest_index,checked_dest_volume,rest_dest_index,rest_dest_volume,dest_index,iterate;
    for (volume_index = 0;volume_index < number_of_volumes;volume_index++) {
      
      //printf("Generating reduced destinations lists for volume %d\n",volume_index);
      logic_list.num_elements = Volumes[volume_index]->geometry.destinations_list.num_elements;
      logic_list.elements = malloc( (int) logic_list.num_elements * sizeof(int));
      if (!logic_list.elements) {
	fprintf(stderr,"Failure allocating list in Union function generate_reduced_destinations_lists 1 - Exit!\n");
	exit(EXIT_FAILURE);
      }
      
      //printf("Sucsessfully allocated space for %d integers in logic list %d\n",logic_list.num_elements,volume_index);
      for (iterate=0;iterate<logic_list.num_elements;iterate++) logic_list.elements[iterate] = 1;
      
      for (checked_dest_index=0;checked_dest_index<Volumes[volume_index]->geometry.destinations_list.num_elements;checked_dest_index++) {
            checked_dest_volume = Volumes[volume_index]->geometry.destinations_list.elements[checked_dest_index];
            for (rest_dest_index=0;rest_dest_index<Volumes[volume_index]->geometry.destinations_list.num_elements;rest_dest_index++) {
                rest_dest_volume = Volumes[volume_index]->geometry.destinations_list.elements[rest_dest_index];
                // As every volume has 0 as a parent, these are ignored. It would work to include this, but would add an extra trivial step to within_which_volume
                if (rest_dest_volume != 0) {
                    if (on_int_list(*parents_lists[checked_dest_volume],rest_dest_volume) == 1) {
                        // In this case, do not include element checked_dest_index on the reduced destinations list
                        // ADD mask check
                        if (Volumes[rest_dest_volume]->geometry.is_masked_volume == 0) {
                          logic_list.elements[checked_dest_index] = 0;
                        }
                    }
                }
            }
      }
      
      Volumes[volume_index]->geometry.reduced_destinations_list.num_elements = sum_int_list(logic_list);
      Volumes[volume_index]->geometry.reduced_destinations_list.elements = malloc((int)Volumes[volume_index]->geometry.reduced_destinations_list.num_elements * sizeof(int));
      if (!Volumes[volume_index]->geometry.reduced_destinations_list.elements) {
	fprintf(stderr,"Failure allocating list in Union function generate_reduced_destinations_lists 2 - Exit!\n");
	exit(EXIT_FAILURE);
      }
      
      iterate = 0;
      for (dest_index = 0;dest_index < Volumes[volume_index]->geometry.destinations_list.num_elements;dest_index++) {
            if (logic_list.elements[dest_index] == 1)   Volumes[volume_index]->geometry.reduced_destinations_list.elements[iterate++] = Volumes[volume_index]->geometry.destinations_list.elements[dest_index];
      }
      
      free(logic_list.elements);
      
      // Testing an optimization
      remove_element_in_list_by_value(&Volumes[volume_index]->geometry.reduced_destinations_list,0);
      
      char string_output[128];
      MPI_MASTER(
      if (verbal) sprintf(string_output,"Reduced destinations list for Volume %d",volume_index);
      if (verbal) print_1d_int_list(Volumes[volume_index]->geometry.reduced_destinations_list,string_output);
      )
    }
};

void generate_direct_children_lists(struct pointer_to_1d_int_list **parents_lists, struct Volume_struct **Volumes,int number_of_volumes,int verbal) {

  MPI_MASTER(
  if (verbal) printf("\nGenerating direct children lists ----------------------- \n");
  )
  // A direct children of volume n is a volume that is a child of n, but no other child of n is its parent
  
  // Mask update: Need to check that this step does not bug out when the mask system interferes with child/parent systems
  
  struct pointer_to_1d_int_list logic_list;
  int volume_index,child,parent,iterate;
  for (volume_index = 0;volume_index < number_of_volumes;volume_index++) {
        // Temp elements is used, and its actual number of elements is edited even though the memory allocated is not changed. This is so that the list functions handles it correctly.
        // The free function will free all the allocated memory regardless of the value of the .num_elements structure field, it is just there for convinience.
      
        logic_list.num_elements = Volumes[volume_index]->geometry.children.num_elements;
        logic_list.elements = malloc(logic_list.num_elements * sizeof(int));
	if (!logic_list.elements) {
	  fprintf(stderr,"Failure allocating list in Union function generate_direct_children_lists 1 - Exit!\n");
	  exit(EXIT_FAILURE);
	}
        for (iterate=0;iterate<logic_list.num_elements;iterate++) logic_list.elements[iterate] = 1;
      
        // Look through each child of volume n, and for each of those look through all the other children of n and search for one of them being a parent to the child
        for (child=0;child<Volumes[volume_index]->geometry.children.num_elements;child++) {
            for (parent=0;parent<parents_lists[Volumes[volume_index]->geometry.children.elements[child]]->num_elements;parent++) {
                if (on_int_list(Volumes[volume_index]->geometry.children,parents_lists[Volumes[volume_index]->geometry.children.elements[child]]->elements[parent]))
                    // If such a parent is found, remove that child from the list
                    logic_list.elements[child] = 0;
            }
        }
      
      Volumes[volume_index]->geometry.direct_children.num_elements = sum_int_list(logic_list);
      Volumes[volume_index]->geometry.direct_children.elements = malloc(Volumes[volume_index]->geometry.direct_children.num_elements*sizeof(int));
      if (!Volumes[volume_index]->geometry.direct_children.elements) {
	fprintf(stderr,"Failure allocating list in Union function generate_direct_children_lists 2 - Exit!\n");
	exit(EXIT_FAILURE);
      }
      
      iterate = 0;
      for (child = 0;child < Volumes[volume_index]->geometry.children.num_elements;child++) {
            if (logic_list.elements[child])   Volumes[volume_index]->geometry.direct_children.elements[iterate++] = Volumes[volume_index]->geometry.children.elements[child];
      }
      // Be careful with names in both main and a function, as they are automatically declared as external variables, which would then also free the main.
      free(logic_list.elements);
      
      char string_output[128];
      MPI_MASTER(
      if (verbal) sprintf(string_output,"Children list for Volume        %d",volume_index);
      if (verbal) print_1d_int_list(Volumes[volume_index]->geometry.children,string_output);
      if (verbal) sprintf(string_output,"Direct_children list for Volume %d",volume_index);
      if (verbal) print_1d_int_list(Volumes[volume_index]->geometry.direct_children,string_output);
      )
      
  }

};

void generate_starting_logic_list(struct starting_lists_struct *starting_lists, struct Volume_struct **Volumes, int number_of_volumes, int verbal) {
    // Function for generating logic list of volumes the ray can start in without an error.
    // Start with a list of all vacuum volumes
    // Remove all volumes that are children of non-vacuum volumes
    // It is still possible to have a volume on this list that is surrounded by non-vacuum volumes, but it is hard to detect these situations,
    //  meaning that it is ultimately partly the users responsibility to not send neutrons directly into materials.
    
    int volume_index,*start,*check;
    
    struct pointer_to_1d_int_list temp_list_local;
    temp_list_local.num_elements = number_of_volumes;
    temp_list_local.elements = malloc(number_of_volumes*sizeof(int));
    if (!temp_list_local.elements) {
      fprintf(stderr,"Failure allocating list in Union function generate_starting_logic_list - Exit!\n");
      exit(EXIT_FAILURE);
    }
    
    temp_list_local.elements[0] = 1; // Volume 0 is a vacuum volume.
    for (volume_index = 1;volume_index < number_of_volumes;volume_index++) {
        if (Volumes[volume_index]->p_physics->is_vacuum == 1) temp_list_local.elements[volume_index] = 1;
        else temp_list_local.elements[volume_index] = 0;
    }
    // temp_list_local is now a logic list of all vacuum volumes
    for (volume_index = 1;volume_index < number_of_volumes;volume_index++) { // All volumes ...
        if (temp_list_local.elements[volume_index] == 0) { // ... that are not vacuum ...
            for (start = check = Volumes[volume_index]->geometry.children.elements;check - start < Volumes[volume_index]->geometry.children.num_elements;check++) { // ... have all their children ...
                temp_list_local.elements[*check] = 0; // .. removed from the allowed_start_logic_list
            }
        }
    }
    allocate_list_from_temp(number_of_volumes,temp_list_local,&starting_lists->allowed_starting_volume_logic_list);
    
    free(temp_list_local.elements);
    //if (verbal==1) printf("sucessfully freed temp_list_local.elements, generate starting lists done\n");
    
    char string_output[128];
    MPI_MASTER(
    if (verbal) sprintf(string_output,"Allowed starting volume logic list");
    if (verbal) print_1d_int_list(starting_lists->allowed_starting_volume_logic_list,string_output);
    )
    
    
};

void generate_reduced_starting_destinations_list(struct starting_lists_struct *starting_lists, struct pointer_to_1d_int_list **parents_lists, struct Volume_struct **Volumes,int number_of_volumes,int verbal) {
    // The starting_destinations_list is trivial, as it contains all volumes that are not masks.


    struct pointer_to_1d_int_list logic_list;
    //printf("Generating reduced destinations lists for volume %d\n",volume_index);
    logic_list.num_elements = number_of_volumes;
    logic_list.elements = malloc( (int) logic_list.num_elements * sizeof(int));
    if (!logic_list.elements) {
      fprintf(stderr,"Failure allocating list in Union function generate_reduced_starting_destinations_list 1 - Exit!\n");
      exit(EXIT_FAILURE);
    }
    int iterate;
    for (iterate=0;iterate<logic_list.num_elements;iterate++) logic_list.elements[iterate] = 0;
    
    
    for (iterate=1;iterate<number_of_volumes;iterate++)
      if (Volumes[iterate]->geometry.is_mask_volume == 0) logic_list.elements[iterate] = 1;
    
    starting_lists->starting_destinations_list.num_elements = sum_int_list(logic_list);
    starting_lists->starting_destinations_list.elements = malloc(starting_lists->starting_destinations_list.num_elements*sizeof(int));
    if (!starting_lists->starting_destinations_list.elements) {
      fprintf(stderr,"Failure allocating list in Union function generate_reduced_starting_destinations_list 2 - Exit!\n");
      exit(EXIT_FAILURE);
    }
    int used_elements=0;
    for (iterate=1;iterate<number_of_volumes;iterate++)
      if (logic_list.elements[iterate] == 1) starting_lists->starting_destinations_list.elements[used_elements++] = iterate;
    
    MPI_MASTER(
    if (verbal) printf("\nGenerating start destinations list ------------------------------ \n");
    if (verbal) print_1d_int_list(starting_lists->starting_destinations_list,"Starting destinations list");
    )

    // The reduced starting destination list is used when a ray enters the component in the search for which volume it starts in.
    // It facilitates the same optimization as the regular destination list.
    // The start logic list is also generated, as it is very simple and does not need a seperate function
    
    // Mask update: Need to remove mask volumes from the reduced starting destination list
    
    MPI_MASTER(
    if (verbal) printf("\nGenerating reduced start destination list ----------------------- \n");
    )
    
    int checked_dest_index,checked_dest_volume,rest_dest_index,rest_dest_volume,dest_index;
        
    logic_list.num_elements = starting_lists->starting_destinations_list.num_elements;
    free(logic_list.elements);
    logic_list.elements = malloc( (int) logic_list.num_elements * sizeof(int));
    
    for (iterate=0;iterate<logic_list.num_elements;iterate++) logic_list.elements[iterate] = 1;
  
    for (checked_dest_index=0;checked_dest_index<starting_lists->starting_destinations_list.num_elements;checked_dest_index++) {
        checked_dest_volume = starting_lists->starting_destinations_list.elements[checked_dest_index];
        for (rest_dest_index=0;rest_dest_index<starting_lists->starting_destinations_list.num_elements;rest_dest_index++) {
            rest_dest_volume = starting_lists->starting_destinations_list.elements[rest_dest_index];
            // As every volume has 0 as a parent, these are ignored. It would work to include this, but would add an extra trivial step to within_which_volume
            if (rest_dest_volume != 0) {
                if (on_int_list(*parents_lists[checked_dest_volume],rest_dest_volume) == 1) {
                    // In this case, do not include element checked_dest_index on the reduced destinations list
                    logic_list.elements[checked_dest_index] = 0;
                }
            }
        }
  }
  
  starting_lists->reduced_start_list.num_elements = sum_int_list(logic_list);
  starting_lists->reduced_start_list.elements = malloc((int)starting_lists->reduced_start_list.num_elements * sizeof(int));
  if (!starting_lists->reduced_start_list.elements) {
    fprintf(stderr,"Failure allocating list in Union function generate_reduced_starting_destinations_list 3 - Exit!\n");
    exit(EXIT_FAILURE);
  }
  iterate = 0;
  for (dest_index = 0;dest_index < starting_lists->starting_destinations_list.num_elements;dest_index++) {
        if (logic_list.elements[dest_index] == 1) starting_lists->reduced_start_list.elements[iterate++] = starting_lists->starting_destinations_list.elements[dest_index];
  }
  
  free(logic_list.elements);
  
  MPI_MASTER(
  if (verbal) print_1d_int_list(starting_lists->reduced_start_list,"Reduced start destinations list");
  )
    
  // Making the start_logic_list.
  starting_lists->start_logic_list.num_elements = number_of_volumes;
  starting_lists->start_logic_list.elements = malloc ( starting_lists->start_logic_list.num_elements * sizeof(int));
  if (!starting_lists->start_logic_list.elements) {
    fprintf(stderr,"Failure allocating list in Union function generate_reduced_starting_destinations_list 4 - Exit!\n");
    exit(EXIT_FAILURE);
  }
  starting_lists->start_logic_list.elements[0] = 0;
  for (iterate=1;iterate<number_of_volumes;iterate++) starting_lists->start_logic_list.elements[iterate] = 1; // All volumes to be checked for starting volume
  MPI_MASTER(
  if (verbal) print_1d_int_list(starting_lists->start_logic_list,"Start logic list");
  )
};


void generate_next_volume_list(struct Volume_struct **Volumes, int number_of_volumes, int verbal) {
    // Generate list of volumes that can be the next volume which the ray enters. It is used for tagging, not the simulation / propagation

    // Mask update: These lists should be done as if all mask statuses are on, meaning they include all possible next volumes (will include more input to this function)
    
    // bug: next volume list is not complete and contains duplicates

    MPI_MASTER(
    if (verbal) printf("\nGenerating next volume list ------------------------------------- \n");
    )
    // Merge destinations list, intersection list and mask_intersection_list
    int volume_index,iterate,mask_index;
    struct pointer_to_1d_int_list full_intersection_list;
    full_intersection_list.num_elements=0;
    
    for (volume_index=0;volume_index<number_of_volumes;volume_index++) {
        Volumes[volume_index]->geometry.next_volume_list.num_elements = 0;
        // Before mask update
        //merge_lists(&Volumes[volume_index]->geometry.next_volume_list, &Volumes[volume_index]->geometry.destinations_list, &Volumes[volume_index]->geometry.intersect_check_list);
        
        merge_lists(&full_intersection_list, &Volumes[volume_index]->geometry.mask_intersect_list, &Volumes[volume_index]->geometry.intersect_check_list);
        merge_lists(&Volumes[volume_index]->geometry.next_volume_list,&Volumes[volume_index]->geometry.destinations_list,&full_intersection_list);
        
        /* // This complication is taken into account by adding the mask_intersect list instead
        // It is possible that the next volume is still not on this list, as when masks are encountered the next volume can be a volume they mask.
        // For each on the list, add the volumes masked by that mask (do not iterate over this, as masks can not be masked regardless)
        for (iterate=Volumes[volume_index]->geometry.next_volume_list.num_elements-1;iterate>-1;iterate--) {
          if (Volumes[Volumes[volume_index]->geometry.next_volume_list.elements[iterate]]->geometry.is_mask_volume == 1) {
            for (mask_index=0;mask_index<Volumes[Volumes[volume_index]->geometry.next_volume_list.elements[iterate]]->geometry.mask_list.num_elements;mask_index++) {
              add_element_to_int_list(&Volumes[volume_index]->geometry.next_volume_list,Volumes[Volumes[volume_index]->geometry.next_volume_list.elements[iterate]]->geometry.mask_list.elements[mask_index]);
            }
          }
        }
        */
        
        // Remove mask volumes from the next volume list, as they never occur as current_volume, and thus just create dead branches that takes up memory
        for (iterate=Volumes[volume_index]->geometry.next_volume_list.num_elements-1;iterate>-1;iterate--)
          if (Volumes[Volumes[volume_index]->geometry.next_volume_list.elements[iterate]]->geometry.is_mask_volume == 1)
            remove_element_in_list_by_index(&Volumes[volume_index]->geometry.next_volume_list,iterate);
        
        
        if (full_intersection_list.num_elements>0) free(full_intersection_list.elements);
        full_intersection_list.num_elements=0;
        
        char string_output[128];
        MPI_MASTER(
        if (verbal) sprintf(string_output,"Next volume list                %d",volume_index);
        if (verbal) print_1d_int_list(Volumes[volume_index]->geometry.next_volume_list,string_output);
        )
    }
};

void generate_lists(struct Volume_struct **Volumes, struct starting_lists_struct *starting_lists, int number_of_volumes, int verbal) {
    // Function to control the generation of lists
    // Some lists are only needed temporary, and are thus declared here to keep them out of the main scope
    // Others are stored in the volume structs as they are needed in the trace algorithm (or tagging)
    
    
    struct pointer_to_1d_int_list **true_children_lists;
    true_children_lists = malloc(number_of_volumes*sizeof(struct pointer_to_1d_int_list*));
    if (!true_children_lists) {
      fprintf(stderr,"Failure allocating list in Union function generate_lists 1 - Exit!\n");
      exit(EXIT_FAILURE);
    }
    // generate_children_lists both generate the normal children list for each volume, but also the true children list needed locally.
    generate_children_lists(Volumes, true_children_lists, number_of_volumes,verbal);
    
    
    struct pointer_to_1d_int_list **true_overlap_lists;
    true_overlap_lists = malloc(number_of_volumes*sizeof(struct pointer_to_1d_int_list*));
    if (!true_overlap_lists) {
      fprintf(stderr,"Failure allocating list in Union function generate_lists 2 - Exit!\n");
      exit(EXIT_FAILURE);
    }
    struct pointer_to_1d_int_list **raw_overlap_lists;
    raw_overlap_lists = malloc(number_of_volumes*sizeof(struct pointer_to_1d_int_list*));
    if (!raw_overlap_lists) {
      fprintf(stderr,"Failure allocating list in Union function generate_lists 3 - Exit!\n");
      exit(EXIT_FAILURE);
    }
    generate_overlap_lists(true_overlap_lists, raw_overlap_lists, Volumes,number_of_volumes,verbal);
    
    
    //generate_intersect_check_lists(true_overlap_lists, Volumes, number_of_volumes, verbal);
    
    
    struct pointer_to_1d_int_list **parents_lists;
    parents_lists = malloc(number_of_volumes*sizeof(struct pointer_to_1d_int_list*));
    if (!parents_lists) {
      fprintf(stderr,"Failure allocating list in Union function generate_lists 4 - Exit!\n");
      exit(EXIT_FAILURE);
    }
    generate_parents_lists(parents_lists,Volumes,number_of_volumes,verbal,1); // The last 1 means masks are taken into account
    
    // Generate version of parent list as it would be without masks
    struct pointer_to_1d_int_list **parents_lists_no_masks;
    parents_lists_no_masks = malloc(number_of_volumes*sizeof(struct pointer_to_1d_int_list*));
    if (!parents_lists_no_masks) {
      fprintf(stderr,"Failure allocating list in Union function generate_lists 5 - Exit!\n");
      exit(EXIT_FAILURE);
    }
    generate_parents_lists(parents_lists_no_masks,Volumes,number_of_volumes,verbal,0); // The last 0 means masks are NOT taken into account
    
    // Generate version of parent list using true_children instead
    struct pointer_to_1d_int_list **true_parents_lists;
    true_parents_lists = malloc(number_of_volumes*sizeof(struct pointer_to_1d_int_list*));
    if (!true_parents_lists) {
      fprintf(stderr,"Failure allocating list in Union function generate_lists 6 - Exit!\n");
      exit(EXIT_FAILURE);
    }
    generate_true_parents_lists(true_parents_lists, true_children_lists, Volumes, number_of_volumes, verbal, 1);
    
    // Generate version of parent list no masks using true_children instead
    struct pointer_to_1d_int_list **true_parents_lists_no_masks;
    true_parents_lists_no_masks = malloc(number_of_volumes*sizeof(struct pointer_to_1d_int_list*));
    if (!true_parents_lists_no_masks) {
      fprintf(stderr,"Failure allocating list in Union function generate_lists 7 - Exit!\n");
      exit(EXIT_FAILURE);
    }
    generate_true_parents_lists(true_parents_lists_no_masks, true_children_lists, Volumes, number_of_volumes, verbal, 0);
    
    // New version of generate intersect lists
    generate_intersect_check_lists_experimental(true_overlap_lists, raw_overlap_lists, parents_lists, true_parents_lists, Volumes, number_of_volumes, verbal);
    
    struct pointer_to_1d_int_list **grandparents_lists;
    grandparents_lists = malloc(number_of_volumes*sizeof(struct pointer_to_1d_int_list*));
    if (!grandparents_lists) {
      fprintf(stderr,"Failure allocating list in Union function generate_lists 8 - Exit!\n");
      exit(EXIT_FAILURE);
    }
    generate_grandparents_lists(grandparents_lists,parents_lists,number_of_volumes,verbal);
    
    // Generate version of grandparents list as it would have been if no masks were defined
    struct pointer_to_1d_int_list **grandparents_lists_no_masks;
    grandparents_lists_no_masks = malloc(number_of_volumes*sizeof(struct pointer_to_1d_int_list*));
    if (!grandparents_lists_no_masks) {
      fprintf(stderr,"Failure allocating list in Union function generate_lists 9 - Exit!\n");
      exit(EXIT_FAILURE);
    }
    generate_grandparents_lists(grandparents_lists_no_masks,parents_lists_no_masks,number_of_volumes,verbal);
    
    // Generate true_grandparents_lists
    struct pointer_to_1d_int_list **true_grandparents_lists;
    true_grandparents_lists = malloc(number_of_volumes*sizeof(struct pointer_to_1d_int_list*));
    if (!true_grandparents_lists) {
      fprintf(stderr,"Failure allocating list in Union function generate_lists 10 - Exit!\n");
      exit(EXIT_FAILURE);
    }
    generate_grandparents_lists(true_grandparents_lists,true_parents_lists,number_of_volumes,verbal);
    
    struct pointer_to_1d_int_list **true_grandparents_lists_no_masks;
    true_grandparents_lists_no_masks = malloc(number_of_volumes*sizeof(struct pointer_to_1d_int_list*));
    if (!true_grandparents_lists_no_masks) {
      fprintf(stderr,"Failure allocating list in Union function generate_lists 11 - Exit!\n");
      exit(EXIT_FAILURE);
    }
    generate_grandparents_lists(true_grandparents_lists_no_masks,true_parents_lists_no_masks,number_of_volumes,verbal);
    
    // The destinations lists are generated without taking masks into account (they are removed from the overlap list in an early step)
    //generate_destinations_lists(grandparents_lists_no_masks,parents_lists_no_masks,true_overlap_lists,Volumes,number_of_volumes,verbal);
    generate_destinations_lists_experimental(true_overlap_lists, true_children_lists, true_parents_lists_no_masks, true_grandparents_lists_no_masks, Volumes, number_of_volumes, verbal);
    
    // Obsolete, found a way around them in within_which_volume, but need to test the performance difference
    // generate_destinations_logic_lists(Volumes,number_of_volumes,verbal);
    
    generate_reduced_destinations_lists(parents_lists,Volumes,number_of_volumes,verbal);
    
    generate_direct_children_lists(parents_lists,Volumes,number_of_volumes,verbal);
    
    //generate_starting_list(starting_lists,Volumes,number_of_volumes,verbal);
    generate_starting_logic_list(starting_lists,Volumes,number_of_volumes,verbal);
    
    generate_reduced_starting_destinations_list(starting_lists,parents_lists,Volumes,number_of_volumes,verbal);
    
    // This list is stored with the volumes for convinience, but is only used for tagging
    generate_next_volume_list(Volumes,number_of_volumes,verbal);
    
    // Garbage collection for temporary dynamically allocated lists. (Permanent lists freed from FINALLY)
    int iterate;
    for (iterate=0;iterate<number_of_volumes;iterate++) {
        //printf("freeing for volume nr %d\n",iterate);
        //printf("true_overlap_lists[iterate]->num_elements = %d \n",true_overlap_lists[iterate]->num_elements);
        if (true_overlap_lists[iterate]->num_elements > 0) free(true_overlap_lists[iterate]->elements);
        free(true_overlap_lists[iterate]);
        
        //printf("raw_overlap_lists[iterate]->num_elements = %d \n",raw_overlap_lists[iterate]->num_elements);
        if (raw_overlap_lists[iterate]->num_elements > 0) free(raw_overlap_lists[iterate]->elements);
        free(raw_overlap_lists[iterate]);
        
        //printf("parents_lists[iterate]->num_elements = %d \n",parents_lists[iterate]->num_elements);
        if (parents_lists[iterate]->num_elements > 0) free(parents_lists[iterate]->elements);
        free(parents_lists[iterate]);
        
        //printf("parents_lists_no_masks[iterate]->num_elements = %d \n",parents_lists_no_masks[iterate]->num_elements);
        if (parents_lists_no_masks[iterate]->num_elements > 0) free(parents_lists_no_masks[iterate]->elements);
        free(parents_lists_no_masks[iterate]);
        
        //printf("true_parents_lists[iterate]->num_elements = %d \n",true_parents_lists[iterate]->num_elements);
        if (true_parents_lists[iterate]->num_elements > 0) free(true_parents_lists[iterate]->elements);
        free(true_parents_lists[iterate]);
        
        //printf("true_parents_lists_no_masks[iterate]->num_elements = %d \n",true_parents_lists_no_masks[iterate]->num_elements);
        if (true_parents_lists_no_masks[iterate]->num_elements > 0) free(true_parents_lists_no_masks[iterate]->elements);
        free(true_parents_lists_no_masks[iterate]);
        
        //printf("grandparents_lists[iterate]->num_elements = %d \n",grandparents_lists[iterate]->num_elements);
        if (grandparents_lists[iterate]->num_elements > 0) free(grandparents_lists[iterate]->elements);
        free(grandparents_lists[iterate]);
        
        //printf("true_grandparents_lists[iterate]->num_elements = %d \n",true_grandparents_lists[iterate]->num_elements);
        if (true_grandparents_lists[iterate]->num_elements > 0) free(true_grandparents_lists[iterate]->elements);
        free(true_grandparents_lists[iterate]);
        
        //printf("grandparents_lists_no_masks[iterate]->num_elements = %d \n",grandparents_lists_no_masks[iterate]->num_elements);
        if (grandparents_lists_no_masks[iterate]->num_elements > 0) free(grandparents_lists_no_masks[iterate]->elements);
        free(grandparents_lists_no_masks[iterate]);
        
        //printf("true_grandparents_lists_no_masks[iterate]->num_elements = %d \n",true_grandparents_lists_no_masks[iterate]->num_elements);
        if (true_grandparents_lists_no_masks[iterate]->num_elements > 0) free(true_grandparents_lists_no_masks[iterate]->elements);
        free(true_grandparents_lists_no_masks[iterate]);
        
        //printf("true_children_lists[iterate]->num_elements = %d \n",true_children_lists[iterate]->num_elements);
        if (true_children_lists[iterate]->num_elements > 0) free(true_children_lists[iterate]->elements);
        free(true_children_lists[iterate]);
    }
    //printf("generate lists volume specific free completed\n");
    free(true_overlap_lists);free(raw_overlap_lists);free(parents_lists);free(true_parents_lists);free(true_parents_lists_no_masks);
    free(parents_lists_no_masks);free(true_grandparents_lists);free(grandparents_lists);free(grandparents_lists_no_masks);free(true_grandparents_lists_no_masks);
    free(true_children_lists);
    //printf("generate lists free completed\n");
};

// -------------    Focusing functions   --------------------------------------------------------

// The focusing_data structure is set up by the geometry component, and a pointer to the appropriate
//  focusing function is added to the Volume structure (for this reason the input of all the functions
//  need to be identical, at least in terms of types). In this way there are no if statements to check
//  which of these to be used in the trace, but the focus_data_struct will carry some redundant
//  information, as only the appropriate parameters are set:
// Angular focus on a rectangle (angular_focus_height / angular_focus_width)
// Spatial focus on a rectangle (spatial_focus_height / spatial_focus_width)
// Spatial focus on a disk (sptial_focus_radius)
// No focus (randvec in 4pi) (all set to zero, will select randvec circle as it is slightly faster
//
// When adding a new physical process focusing becomes very easy, as one just calls the master focusing
//  function assosiated with the volume (placed in the geometry struct), using the focus_data_struct
//  also found in the geometry struct, and the process then supports all the focusing modes. It is even
//  possible to add new focusing modes in the future by updating just the geometry components, and this
//  section.

// focus_data_struct definitioon shown here, defined at the start of this file
//struct focus_data_struct {
//Coords Aim;
//double angular_focus_width;
//double angular_focus_height;
//double spatial_focus_width;
//double spatial_focus_height;
//double spatial_focus_radius;
//Rotation absolute_rotation;
//// focusing_function creates a vector per selected criteria of focus_data_struct / selected focus function and returns solid angle
//void (*focusing_function)(Coords*, double*, struct focus_data_struct*);
////                        v_out  , solid_a,
//};

void randvec_target_rect_angular_union(Coords *v_out,double *solid_angle_out, struct focus_data_struct *focus_data) {
    // Calls the standard McStas randvec_target_rect_angular focusing function, but is with the new data input format.
    randvec_target_rect_angular(&v_out->x, &v_out->y, &v_out->z, solid_angle_out, focus_data->RayAim.x,focus_data->RayAim.y, focus_data->RayAim.z, focus_data->angular_focus_width, focus_data->angular_focus_height,focus_data->absolute_rotation);
    //randvec_target_rect_angular(&vx, &vy, &vz, &solid_angle,aim_x, aim_y, aim_z, VarsInc.aw, VarsInc.ah, ROT_A_CURRENT_COMP);
};

void randvec_target_rect_union(Coords *v_out,double *solid_angle_out, struct focus_data_struct *focus_data) {
// Calls the standard McStas randvec_target_rect focusing function, but is with the new data input format.
    randvec_target_rect(&v_out->x, &v_out->y, &v_out->z, solid_angle_out, focus_data->RayAim.x,focus_data->RayAim.y, focus_data->RayAim.z, focus_data->spatial_focus_width, focus_data->spatial_focus_height,focus_data->absolute_rotation);
    // randvec_target_rect(&vx, &vy, &vz, &solid_angle,aim_x, aim_y, aim_z, VarsInc.xw, VarsInc.yh, ROT_A_CURRENT_COMP);
};

void randvec_target_circle_union(Coords *v_out,double *solid_angle_out, struct focus_data_struct *focus_data) {
// Calls the standard McStas randvec_target_circle focusing function, but is with the new data input format.

    // debug input into randvec_target_circle
    //print_position(focus_data->Aim,"Aim vector input for randvec_target_circle");
    //printf("Radius input %f\n",focus_data->spatial_focus_radius);

    randvec_target_circle(&v_out->x, &v_out->y, &v_out->z, solid_angle_out, focus_data->RayAim.x,focus_data->RayAim.y, focus_data->RayAim.z, focus_data->spatial_focus_radius);
    //randvec_target_circle(&vx, &vy, &vz, &solid_angle, aim_x, aim_y, aim_z, focus_r);
};



void focus_initialize(struct geometry_struct *geometry, Coords POS_A_TARGET, Coords POS_A_CURRENT, Rotation ROT_A_CURRENT, int target_index, double target_x, double target_y, double target_z, double angular_focus_width, double angular_focus_height, double spatial_focus_width, double spatial_focus_height, double spatial_focus_radius, char *component_name) {
    // Initialize focusing system
    // target_x/y/z needs to be double setting parameters with default value 0
    // target_index needs to be int setting parameter with default value 0
    // angular_focus_width, angular_focus_height, spatial_focus_width, spatial_focus_height, spatial_focus_radius nneds to be double setting parameters with default value 0
    // When those conditions are met, this code will identify which settings have been entered by the user and select the appropriate focusing parameters, which are loaded into the focus_data struct and the geometry struct.
    // The aim vector in the focus_data struct will be transformed from the local coordinate system of the geometry component to the coordinate system of the master component during the master component initialize

  // Input sanitation
  if (angular_focus_width < 0) {
    printf("\nERROR in Union geometry component named \"%s\", angular focus width focus_aw < 0! \n",component_name);
    exit(EXIT_FAILURE);
  }
  if (angular_focus_height < 0) {
    printf("\nERROR in Union geometry component named \"%s\", angular focus width focus_ah < 0! \n",component_name);
    exit(EXIT_FAILURE);
  }
  if (spatial_focus_width < 0) {
    printf("\nERROR in Union geometry component named \"%s\", spatial focus width focus_xw < 0! \n",component_name);
    exit(EXIT_FAILURE);
  }
  if (spatial_focus_height < 0) {
    printf("\nERROR in Union geometry component named \"%s\", spatial focus height focus_xh < 0! \n",component_name);
    exit(EXIT_FAILURE);
  }
  if (spatial_focus_radius < 0) {
    printf("\nERROR in Union geometry component named \"%s\", spatial focus radius focus_r < 0! \n",component_name);
    exit(EXIT_FAILURE);
  }
  
  struct focus_data_struct focus_data;

  // Initialize focus_data_struct
  focus_data.Aim = coords_set(0,0,0);
  focus_data.RayAim = coords_set(0,0,0);
  focus_data.angular_focus_width = 0;
  focus_data.angular_focus_height = 0;
  focus_data.spatial_focus_width = 0;
  focus_data.spatial_focus_height = 0;
  focus_data.spatial_focus_radius = 0;
  rot_copy(focus_data.absolute_rotation,ROT_A_CURRENT);

  // Built on code from Incoherent.comp by Kim Lefmann and Kristian Nielsen
  if (target_index != 0 && !target_x && !target_y && !target_z)
  {
    Coords ToTarget;
    //ToTarget = coords_sub(POS_A_COMP_INDEX(INDEX_CURRENT_COMP+target_index),POS_A_CURRENT_COMP);
    ToTarget = coords_sub(POS_A_TARGET, POS_A_CURRENT);
    ToTarget = rot_apply(ROT_A_CURRENT, ToTarget);
    coords_get(ToTarget, &focus_data.Aim.x, &focus_data.Aim.y, &focus_data.Aim.z);
  }
  else
  { focus_data.Aim.x = target_x; focus_data.Aim.y = target_y; focus_data.Aim.z = target_z; }
  
  if (!(focus_data.Aim.x || focus_data.Aim.y || focus_data.Aim.z)) {
    // Somehow set a variable to signify scattering into 4pi
    // printf("Union %s: The target is not defined. Using scattering into 4pi.\n",NAME_CURRENT_COMP);
    focus_data.Aim.z=1; // set aim to one so that the randvec output vector has length 1 instead of 0
  }

  int focusing_model_selected = 0;
  if (angular_focus_width != 0 && angular_focus_height != 0) {
    focus_data.focusing_function = &randvec_target_rect_angular_union;
    focus_data.angular_focus_width = DEG2RAD*angular_focus_width; // Convert to radians here
    focus_data.angular_focus_height = DEG2RAD*angular_focus_height;
    focusing_model_selected = 1;
  }
  if (spatial_focus_width != 0 && spatial_focus_height != 0) {
    focus_data.focusing_function = &randvec_target_rect_union;
    focus_data.spatial_focus_width = spatial_focus_width;
    focus_data.spatial_focus_height = spatial_focus_height;
    if (focusing_model_selected) {
        printf("ERROR %s: Select either angular or spatial focusing, not both! Exiting \n",component_name);
        exit(EXIT_FAILURE);
    }
    focusing_model_selected = 1;
  }
  if (spatial_focus_radius != 0) {
    focus_data.focusing_function = &randvec_target_circle_union;
    focus_data.spatial_focus_radius = spatial_focus_radius;
    if (focusing_model_selected) {
        printf("ERROR %s: Select a maximum of one focusing method (spatial rectangle or cicle, or angular rectangle! Exiting \n",component_name);
        exit(EXIT_FAILURE);
    }
    focusing_model_selected = 1;
  }
  if (focusing_model_selected == 0) {
    // Select 4pi focusing
    focus_data.spatial_focus_radius = 0;
    focus_data.focusing_function = &randvec_target_circle_union;
  }
  
  // Allocate the isotropic focus_data struct
  geometry->focus_data_array.num_elements = 0;
  add_element_to_focus_data_array(&geometry->focus_data_array,focus_data);
};


struct abs_event{
    double time1;
    double position1[3];
    double time2;
    double position2[3];
    double weight_change;
    int volume_index;
    int neutron_id;
};

// Functions for recording absorption
void initialize_absorption_file() {
  FILE *fp;
  fp = fopen("Union_absorption.dat","w");
  if(!fp) {
    fprintf(stderr,"WARNING: Could not write initial output to Union_absorption.dat\n");
  } else {
    fprintf(fp,"r_old x, r_old y, r_old z, old t, r x, r y, r z, new t, weight change, volume index, neutron id \n");
    fclose(fp);
  }
}

void write_events_to_file(int last_index, struct abs_event *events) {

  FILE *fp;
  fp = fopen("Union_absorption.dat","a");
  if(!fp) {
    fprintf(stderr,"WARNING: Could not write logging output to Union_absorption.dat\n");
  } else {
    struct abs_event *this_event;
    int iterate;
    for (iterate=0; iterate<last_index; iterate++) {
      
      this_event = &events[iterate];
      fprintf(fp,"%g, %g, %g, %g, %g, %g, %g, %g, %e, %i, %i \n",
	      this_event->position1[0], this_event->position1[1], this_event->position1[2], this_event->time1,
	      this_event->position2[0], this_event->position2[1], this_event->position2[2], this_event->time2,
	      this_event->weight_change,
	      this_event->volume_index,
	      this_event->neutron_id);
    }
    fclose(fp);
  }
}

void record_abs_to_file(double *r, double t1, double *r_old, double t2, double weight_change, int volume, int neutron_id, int *data_index, struct abs_event *events) {
  
  
  
  struct abs_event *this_event;
  
  this_event = &events[(*data_index)++];
  
  //printf("Recording something! %i\n", *data_index);
  
  this_event->position1[0] = r[0];
  this_event->position1[1] = r[1];
  this_event->position1[2] = r[2];
  this_event->time1 = t1;
  this_event->position2[0] = r_old[0];
  this_event->position2[1] = r_old[1];
  this_event->position2[2] = r_old[2];
  this_event->time2 = t2;
  this_event->weight_change = weight_change;
  this_event->volume_index = volume;
  this_event->neutron_id = neutron_id;
  
  
  if (*data_index == 999) {
    
      write_events_to_file(*data_index, events);
      *data_index = 0;

  }

};

void manual_linking_function_surface(char *input_string, struct pointer_to_global_surface_list *global_surface_list, struct pointer_to_1d_int_list *accepted_surfaces, char *component_name) {

   char *token;
   int loop_index;
   char local_string[256];
   
   strcpy(local_string, input_string);
   
   // get the first token
   token = strtok(local_string,",");
   
   // walk through tokens
   while(token != NULL) 
   {
      //printf( " %s\n", token );
      for (loop_index=0; loop_index<global_surface_list->num_elements; loop_index++) {
        if (strcmp(token, global_surface_list->elements[loop_index].name) == 0) {
          add_element_to_int_list(accepted_surfaces, loop_index);
          break;
        } 

        if (loop_index == global_surface_list->num_elements - 1) {
          // All possible surface names have been looked through, and the break was not executed.
          // Alert the user to this problem by showing the surface name that was not found and the currently available surface definitions
            printf("\n");
            printf("ERROR: The surface string \"%s\" in Union geometry \"%s\" had an entry that did not match a specified surface definition. \n", input_string, component_name);
            printf("       The unrecoignized surface name was: \"%s\" \n",token);
            printf("       The surfaces available at this point (need to be defined before the geometry): \n");
            for (loop_index=0; loop_index<global_surface_list->num_elements; loop_index++)
              printf("         %s\n",global_surface_list->elements[loop_index].name);
            exit(EXIT_FAILURE);
        }
      }
      
      // Updates the token
      token = strtok(NULL,",");
   }
}

void fill_surface_stack(char *input_string, struct pointer_to_global_surface_list *global_surface_list, char *component_name,
                        struct surface_stack_struct *surface_stack) {
	// Takes empty surface_stack struct, allocates the memory and fills it with appropriate pointers to the surfaces requested in input_string
				   
	if (input_string && strlen(input_string) && strcmp(input_string, "NULL") && strcmp(input_string, "0") && strcmp(input_string, "None")) {
		
	  struct pointer_to_1d_int_list accepted_surfaces;
	  accepted_surfaces.num_elements = 0;
	  
	  manual_linking_function_surface(input_string, global_surface_list, &accepted_surfaces, component_name);
	  
	  surface_stack->number_of_surfaces = accepted_surfaces.num_elements;
	  surface_stack->p_surface_array = malloc(surface_stack->number_of_surfaces*sizeof(struct surface_process_struct*));
	  if (!surface_stack->p_surface_array) {
	    fprintf(stderr,"Failure allocating list in Union function fill_surface_stack - Exit!\n");
	    exit(EXIT_FAILURE);
	  }
	
	  int loop_index;
  	  for (loop_index=0; loop_index<accepted_surfaces.num_elements; loop_index++) {
	      surface_stack->p_surface_array[loop_index]=global_surface_list->elements[accepted_surfaces.elements[loop_index]].p_surface_process;
	  }
		
	} else {
	  surface_stack->number_of_surfaces = 0;
	}

}

void overwrite_if_empty(char *input_string, char *overwrite) {
   if (!(input_string && strlen(input_string) && strcmp(input_string, "NULL") && strcmp(input_string, "0"))) {
	   strcpy(input_string, overwrite);
   }
}


  #endif

/* Shared user declarations for all components types 'Incoherent_process'. */
  #ifndef Union
  #error "The Union_init component must be included before this Incoherent_process component"
  #endif

  struct Incoherent_physics_storage_struct {
    // Variables that needs to be transfered between any of the following places:
    // The initialize in this component
    // The function for calculating my
    // The function for calculating scattering

    double my_scattering;
    double QE_sampling_frequency;
    double lorentzian_width;
  };

  // Function for calculating my in Incoherent case
  int
  Incoherent_physics_my (double* my, double* k_initial, union data_transfer_union data_transfer, struct focus_data_struct* focus_data,
                         _class_particle* _particle) {
    *my = data_transfer.pointer_to_a_Incoherent_physics_storage_struct->my_scattering;
    return 1;
  };

  // Function for basic incoherent scattering event
  int
  Incoherent_physics_scattering (double* k_final, double* k_initial, double* weight, union data_transfer_union data_transfer,
                                 struct focus_data_struct* focus_data, _class_particle* _particle) {

    // New version of incoherent scattering
    double k_length = sqrt (k_initial[0] * k_initial[0] + k_initial[1] * k_initial[1] + k_initial[2] * k_initial[2]);

    Coords k_out;
    // Here is the focusing system in action, get a vector
    double solid_angle;
    focus_data->focusing_function (&k_out, &solid_angle, focus_data);
    NORM (k_out.x, k_out.y, k_out.z);
    *weight *= solid_angle * 0.25 / PI;

    double v_i, v_f, E_i, dE, E_f;

    if (rand01 () < data_transfer.pointer_to_a_Incoherent_physics_storage_struct->QE_sampling_frequency) {
      v_i = k_length * K2V;
      E_i = VS2E * v_i * v_i;
      dE = data_transfer.pointer_to_a_Incoherent_physics_storage_struct->lorentzian_width * tan (PI / 2 * randpm1 ());
      E_f = E_i + dE;
      if (E_f <= 0)
        return 0;
      v_f = SE2V * sqrt (E_f);
      k_length = v_f * V2K;
    }

    k_final[0] = k_out.x * k_length;
    k_final[1] = k_out.y * k_length;
    k_final[2] = k_out.z * k_length;
    return 1;
  };

  #ifndef PROCESS_DETECTOR
  #define PROCESS_DETECTOR dummy
  #endif

  #ifndef PROCESS_INCOHERENT_DETECTOR
  #define PROCESS_INCOHERENT_DETECTOR dummy
  #endif

/* Shared user declarations for all components types 'Union_make_material'. */

  #ifndef Union
  #error "The Union_init component must be included before this Union_make_material component"
  #endif

  // This function checks if global_process_element should be included in this material when using automatic linking, returns 1 if yes, 0 if no.
  int
  automatic_linking_materials_function (struct global_process_element_struct global_process_element, struct pointer_to_global_material_list global_material_list,
                                        int current_index) {
    // Remember this function is used before the current material is added to global_material_list
    // debug info
    // MPI_MASTER(
    // printf("Checking if process with index %d should be automatically linked to material with index
    // %d\n",global_process_element.component_index,current_index);
    //)

    // Check if this is the first make_material, which makes the problem simpler.
    if (global_material_list.num_elements == 0) {
      if (global_process_element.component_index < current_index)
        return 1;
      else
        return 0;
    }
    // In case there are more than 1 make_material, global_material_list.elements[global_material_list.num_elements-1].component_index makes sense.
    if (global_process_element.component_index < current_index
        && global_process_element.component_index > global_material_list.elements[global_material_list.num_elements - 1].component_index)
      return 1;
    else
      return 0;
  }

  void
  manual_linking_function_material (char* input_string, struct pointer_to_global_process_list* global_process_list,
                                    struct pointer_to_1d_int_list* accepted_processes, char* component_name) {
    // Need to check a input_string of text for an occurance of name. If it is in the inputstring, yes return 1, otherwise 0.
    char* token;
    int loop_index;
    char local_string[256];

    strcpy (local_string, input_string);
    // get the first token
    token = strtok (local_string, ",");

    // walk through tokens
    while (token != NULL) {
      // printf( " %s\n", token );
      for (loop_index = 0; loop_index < global_process_list->num_elements; loop_index++) {
        if (strcmp (token, global_process_list->elements[loop_index].name) == 0) {
          add_element_to_int_list (accepted_processes, loop_index);
          break;
        }

        if (loop_index == global_process_list->num_elements - 1) {
          // All possible process names have been looked through, and the break was not executed.
          // Alert the user to this problem by showing the process name that was not found and the currently available processes
          printf ("\n");
          printf ("ERROR: The process string \"%s\" in Union material \"%s\" had an entry that did not match a specified process. \n", input_string,
                  component_name);
          printf ("       The unrecoignized process name was: \"%s\" \n", token);
          printf ("       The processes available at this point (need to be defined before the material): \n");
          for (loop_index = 0; loop_index < global_process_list->num_elements; loop_index++)
            printf ("         %s\n", global_process_list->elements[loop_index].name);
          exit (1);
        }
      }

      // Updates the token
      token = strtok (NULL, ",");
    }
  }

  // This function is needed in initialize of all geometry components
  // Possible to insert these functions in make material, as they are only compiled once instead of many times
  int
  manual_linking_function (char* name, char* input_string) {
    // Need to check a input_string of text for an occurance of name. If it is in the inputstring, yes return 1, otherwise 0.
    char* token;
    int return_integer = 0;
    char local_string[124];

    strcpy (local_string, input_string);
    /* get the first token */
    token = strtok (local_string, ",");

    /* walk through other tokens */
    while (token != NULL) {
      if (strcmp (token, name) == 0)
        return_integer = 1;

      token = strtok (NULL, ",");
    }

    return return_integer;
  }

  #ifndef MATERIAL_DETECTOR
  #define MATERIAL_DETECTOR dummy
  #endif

/* Shared user declarations for all components types 'Powder_process'. */
  #ifndef Union
  #error "The Union_init component must be included before this Powder_process component"
  #endif

  // Share section of PowderN 8/3 2016 from McStas.org

  /* used for reading data table from file */
/*******************************************************************************
*
* McStas, neutron ray-tracing package
*         Copyright 1997-2002, All rights reserved
*         Risoe National Laboratory, Roskilde, Denmark
*         Institut Laue Langevin, Grenoble, France
*
* Library: share/read_table-lib.h
*
* %Identification
* Written by: EF
* Date: Aug 28, 2002
* Origin: ILL
* Release: McStas 1.6
* Version: $Revision$
*
* This file is to be imported by components that may read data from table files
* It handles some shared functions.
*
* This library may be used directly as an external library. It has no dependency
*
* Usage: within SHARE
* %include "read_table-lib"
*
*******************************************************************************/

#ifndef READ_TABLE_LIB_H
#define READ_TABLE_LIB_H "$Revision$"

#define READ_TABLE_STEPTOL  0.04 /* tolerancy for constant step approx */

#ifndef MC_PATHSEP_C
#ifdef WIN32
#define MC_PATHSEP_C '\\'
#define MC_PATHSEP_S "\\"
#else  /* !WIN32 */
#ifdef MAC
#define MC_PATHSEP_C ':'
#define MC_PATHSEP_S ":"
#else  /* !MAC */
#define MC_PATHSEP_C '/'
#define MC_PATHSEP_S "/"
#endif /* !MAC */
#endif /* !WIN32 */
#endif /* !MC_PATHSEP_C */

#ifndef MCSTAS
#ifdef WIN32
#define MCSTAS "C:\\mcstas\\lib"
#else  /* !WIN32 */
#ifdef MAC
#define MCSTAS ":mcstas:lib" /* ToDo: What to put here? */
#else  /* !MAC */
#define MCSTAS "/usr/local/lib/mcstas"
#endif /* !MAC */
#endif /* !WIN32 */
#endif /* !MCSTAS */

#include <sys/stat.h>
#include <stdio.h>
#include <stdlib.h>

#ifndef _MSC_EXTENSIONS
#include <strings.h>
#else
#  include <string.h>
#  define strcasecmp _stricmp
#  define strncasecmp _strnicmp
#endif

  typedef struct struct_table
  {
    char    filename[1024];
    long    filesize;
    char   *header;  /* text header, e.g. comments */
    double *data;    /* vector { x[0], y[0], ... x[n-1], y[n-1]... } */
    double  min_x;   /* min value of first column */
    double  max_x;   /* max value of first column */
    double  step_x;  /* minimal step value of first column */
    long    rows;    /* number of rows in matrix block */
    long    columns; /* number of columns in matrix block */

    long    begin;   /* start fseek index of block */
    long    end;     /* stop  fseek index of block */
    long    block_number;  /* block index. 0 is catenation of all */
    long    array_length;  /* number of elements in the t_Table array */
    char    monotonic;     /* true when 1st column/vector data is monotonic */
    char    constantstep;  /* true when 1st column/vector data has constant step */
    char    method[32];    /* interpolation method: nearest, linear */
    char    quiet;   /*output level for messages to the console 0: print all messages, 1:only print some/including errors, 2: never print anything.*/
  } t_Table;

/*maximum number of rows to rebin a table = 1M*/
enum { mcread_table_rebin_maxsize = 1000000 };

typedef struct t_Read_table_file_item {
    int ref_count;
    t_Table *table_ref;
} t_Read_table_file_item;

typedef enum enum_Read_table_file_actions {STORE,FIND,GC}  t_Read_table_file_actions;

/* read_table-lib function prototypes */
/* ========================================================================= */

/* 'public' functions */
long     Table_Read              (t_Table *Table, char *File, long block_number);
long     Table_Read_Offset       (t_Table *Table, char *File, long block_number,
                                  long *offset, long max_lines);
long     Table_Read_Offset_Binary(t_Table *Table, char *File, char *Type,
                                  long *Offset, long Rows, long Columns);
long     Table_Rebin(t_Table *Table); /* rebin table with regular 1st column and interpolate all columns 2:end */
long     Table_Info (t_Table Table);
#pragma acc routine
double   Table_Index(t_Table Table,   long i, long j); /* get indexed value */
#pragma acc routine
double   Table_Value(t_Table Table, double X, long j); /* search X in 1st column and return interpolated value in j-column */
t_Table *Table_Read_Array(char *File, long *blocks);
void     Table_Free_Array(t_Table *Table);
long     Table_Info_Array(t_Table *Table);
int      Table_SetElement(t_Table *Table, long i, long j, double value);
long     Table_Init(t_Table *Table, long rows, long columns); /* create a Table */
#pragma acc routine
double   Table_Value2d(t_Table Table, double X, double Y);    /* same as Table_Index with non-integer indices and 2d interpolation */
MCDETECTOR Table_Write(t_Table Table, char*file, char*xl, char*yl, 
           double x1, double x2, double y1, double y2); /* write Table to disk */
void * Table_File_List_Handler(t_Read_table_file_actions action, void *item, void *item_modifier);
t_Table *Table_File_List_find(char *name, int block, int offset);
int Table_File_List_gc(t_Table *tab);
void *Table_File_List_store(t_Table *tab);

#define Table_ParseHeader(header, ...) \
  Table_ParseHeader_backend(header,__VA_ARGS__,NULL);

char **Table_ParseHeader_backend(char *header, ...);
FILE *Open_File(char *name, const char *Mode, char *path);


/* private functions */
void Table_Free(t_Table *Table);
long Table_Read_Handle(t_Table *Table, FILE *fid, long block_number, long max_lines, char *name);
static void Table_Stat(t_Table *Table);
#pragma acc routine
double Table_Interp1d(double x, double x1, double y1, double x2, double y2);
#pragma acc routine
double Table_Interp1d_nearest(double x, double x1, double y1, double x2, double y2);
#pragma acc routine
double Table_Interp2d(double x, double y, double x1, double y1, double x2, double y2,
double z11, double z12, double z21, double z22);


#endif

/* end of read_table-lib.h */
/*******************************************************************************
*
* McStas, neutron ray-tracing package
*         Copyright (C) 1997-2009, All rights reserved
*         Risoe National Laboratory, Roskilde, Denmark
*         Institut Laue Langevin, Grenoble, France
*
* Library: share/read_table-lib.c
*
* %Identification
* Written by: EF
* Date: Aug 28, 2002
* Origin: ILL
* Release: McStas CVS_090504
* Version: $Revision$
*
* This file is to be imported by components that may read data from table files
* It handles some shared functions. Embedded within instrument in runtime mode.
*
* Usage: within SHARE
* %include "read_table-lib"
*
*******************************************************************************/

#ifndef READ_TABLE_LIB_H
#include "read_table-lib.h"
#endif

#ifndef READ_TABLE_LIB_C
#define READ_TABLE_LIB_C "$Revision$"


/*******************************************************************************
 * void *Table_File_List_Handler(action, item, item_modifier)
 *   ACTION: handle file entries in the read_table-lib file list. If a file is read - it is supposed to be
 *   stored in a list such that we can avoid reading the same file many times.
 *   input  action: FIND, STORE, GC. check if file exists in the list, store an item in the list, or check if it can be garbage collected.
 *   input item: depends on the action.
 *    FIND)  item is a filename, and item_modifier is the block number
 *    STORE) item is the Table to store - item_modifier is ignored
 *    GC)    item is the Table to check. If it has a ref_count >1 then this is simply decremented.
 *   return  depends on the action
 *    FIND)  return a reference to a table+ref_count item if found - NULL otherwise. I.e. NULL means the file has not been read before and must be read again.
 *    STORE) return NULL always
 *    GC)    return NULL if no garbage collection is needed, return an adress to the t_Table which should be garbage collected. 0x1 is returned if
 *           the item is not found in the list
*******************************************************************************/
void * Table_File_List_Handler(t_Read_table_file_actions action, void *item, void *item_modifier){

    /* logic here is Read_Table should include a call to FIND. If found the return value should just be used as
     * if the table had been read from disk. If not found then read the table and STORE.
     * Table_Free should include a call to GC. If this returns non-NULL then we should proceed with freeing the memory
     * associated with the table item - otherwise only decrement the reference counter since there are more references
     * that may need it.*/

    static t_Read_table_file_item read_table_file_list[1024];  
    static int read_table_file_count=0;

    t_Read_table_file_item *tr;
    switch(action){
        case FIND:
            /*interpret data item as a filename, if it is found return a pointer to the table and increment refcount.
             * if not found return the item itself*/
            tr=read_table_file_list;
            while ( tr->table_ref!=NULL ){
                int i=*((int*) item_modifier);
                int j=*( ((int*) item_modifier)+1);
                if ( !strcmp(tr->table_ref->filename,(char *) item) &&
                        tr->table_ref->block_number==i && tr->table_ref->begin==j ){
                    tr->ref_count++;
                    return (void *) tr;
                }
                tr++;
            }
            return NULL;
        case STORE:
            /*find an available slot and store references to table there*/
            tr=&(read_table_file_list[read_table_file_count++]);
            tr->table_ref = ((t_Table *) item);
            tr->ref_count++;
            return NULL;
        case GC:
            /* Should this item be garbage collected (freed) - if so scratch the entry and return the address of the item - 
             * else decrement ref_count and return NULL.
             * A non-NULL return expects the item to actually be freed afterwards.*/
            tr=read_table_file_list;
            while ( tr->table_ref!=NULL ){
                if ( tr->table_ref->data ==((t_Table *)item)->data && 
                        tr->table_ref->block_number == ((t_Table *)item)->block_number){
                    /*matching item found*/
                    if (tr->ref_count>1){
                        /*the item is found and no garbage collection needed*/
                        tr->ref_count--;
                        return NULL;
                    }else{
                        /* The item is found and the reference counter is 1.
                         * This means we should garbage collect. Move remaining list items up one slot,
                         * and return the table for garbage collection by caller*/
                        while (tr->table_ref!=NULL){
                            *tr=*(tr+1);
                            tr++;
                        }
                        read_table_file_count--;
                        return (t_Table *) item;
                    }
                }
                tr++;
            }
            /* item not found, and so should be garbage collected. This could be the case if freeing a
             * Table that has been constructed from code - not read from file. Return 0x1 to flag it for
             * collection.*/
            return (void *) 0x1 ;
    }
    /* If we arrive here, nothing worked, return NULL */
    return NULL;
}

/* Access functions to the handler*/

/********************************************
 * t_Table *Table_File_List_find(char *name, int block, int offset)
 * input name: filename to search for in the file list
 * input block: data block in the file as each file may contain more than 1 data block.
 * return a ref. to a table if it is found (you may use this pointer and skip reading the file), NULL otherwise (i.e. go ahead and read the file)
*********************************************/
t_Table *Table_File_List_find(char *name, int block, int offset){
    int vars[2]={block,offset};
    t_Read_table_file_item *item = Table_File_List_Handler(FIND,name, vars);
    if (item == NULL){
        return NULL;
    }else{
        return item->table_ref;
    }
}
/********************************************
 * int Table_File_List_gc(t_Table *tab)
 * input tab: the table to check for references.
 * return 0: no garbage collection needed
 *        1: Table's data and header (at least) should be freed.
*********************************************/
int Table_File_List_gc(t_Table *tab){
    void *rval=Table_File_List_Handler(GC,tab,0);
    if (rval==NULL) return 0;
    else return 1;
}


/*****************************************************************************
 * void *Table_File_List_store(t_Table *tab)
 * input tab: pointer to table to store.
 * return None. 
*******************************************************************************/
void *Table_File_List_store(t_Table *tab){
    return Table_File_List_Handler(STORE,tab,0);
}


/*******************************************************************************
* FILE *Open_File(char *name, char *Mode, char *path)
*   ACTION: search for a file and open it. Optionally return the opened path.
*   input   name:  file name from which table should be extracted
*           mode: "r", "w", "a" or any valid fopen mode
*           path:  NULL or a pointer to at least 1024 allocated chars
*   return  initialized file handle or NULL in case of error
*******************************************************************************/

  FILE *Open_File(char *File, const char *Mode, char *Path)
  {
    char path[1024];
    FILE *hfile = NULL;
    
    if (!File || File[0]=='\0')                     return(NULL);
    if (!strcmp(File,"NULL") || !strcmp(File,"0"))  return(NULL);
    
    /* search in current or full path */
    strncpy(path, File, 1024);
    hfile = fopen(path, Mode);
    if(!hfile)
    {
      char dir[1024];

      if (!hfile && instrument_source[0] != '\0' && strlen(instrument_source)) /* search in instrument source location */
      {
        char *path_pos   = NULL;
        /* extract path: searches for last file separator */
        path_pos    = strrchr(instrument_source, MC_PATHSEP_C);  /* last PATHSEP */
        if (path_pos) {
          long path_length = path_pos +1 - instrument_source;  /* from start to path+sep */
          if (path_length) {
            strncpy(dir, instrument_source, path_length);
            dir[path_length] = '\0';
            snprintf(path, 1024, "%s%c%s", dir, MC_PATHSEP_C, File);
            hfile = fopen(path, Mode);
          }
        }
      }
      if (!hfile && instrument_exe[0] != '\0' && strlen(instrument_exe)) /* search in PWD instrument executable location */
      {
        char *path_pos   = NULL;
        /* extract path: searches for last file separator */
        path_pos    = strrchr(instrument_exe, MC_PATHSEP_C);  /* last PATHSEP */
        if (path_pos) {
          long path_length = path_pos +1 - instrument_exe;  /* from start to path+sep */
          if (path_length) {
            strncpy(dir, instrument_exe, path_length);
            dir[path_length] = '\0';
            snprintf(path, 1024, "%s%c%s", dir, MC_PATHSEP_C, File);
            hfile = fopen(path, Mode);
          }
        }
      }
      if (!hfile) /* search in HOME or . */
      {
        strcpy(dir, getenv("HOME") ? getenv("HOME") : ".");
        snprintf(path, 1024, "%s%c%s", dir, MC_PATHSEP_C, File);
        hfile = fopen(path, Mode);
      }
      if (!hfile) /* search in MCSTAS/data */
      {
        strcpy(dir, getenv(FLAVOR_UPPER) ? getenv(FLAVOR_UPPER) : MCSTAS);
        snprintf(path, 1024, "%s%c%s%c%s", dir, MC_PATHSEP_C, "data", MC_PATHSEP_C, File);
        hfile = fopen(path, Mode);
      }
      if (!hfile) /* search in MVCSTAS/contrib */
      {
        strcpy(dir, getenv(FLAVOR_UPPER) ? getenv(FLAVOR_UPPER) : MCSTAS);
        snprintf(path, 1024, "%s%c%s%c%s", dir, MC_PATHSEP_C, "contrib", MC_PATHSEP_C, File);
        hfile = fopen(path, Mode);
      }
      if(!hfile)
      {
        // fprintf(stderr, "Warning: Could not open input file '%s' (Open_File)\n", File);
        return (NULL);
      }
    }
    if (Path) strncpy(Path, path, 1024);
    return(hfile);
  } /* end Open_File */

/*******************************************************************************
* long Read_Table(t_Table *Table, char *name, int block_number)
*   ACTION: read a single Table from a text file
*   input   Table: pointer to a t_Table structure
*           name:  file name from which table should be extracted
*           block_number: if the file does contain more than one
*                 data block, then indicates which one to get (from index 1)
*                 a 0 value means append/catenate all
*   return  initialized single Table t_Table structure containing data, header, ...
*           number of read elements (-1: error, 0:header only)
* The routine stores any line starting with '#', '%' and ';' into the header
* File is opened, read and closed
* Other lines are interpreted as numerical data, and stored.
* Data block should be a rectangular matrix or vector.
* Data block may be rebinned with Table_Rebin (also sort in ascending order)
*******************************************************************************/
  long Table_Read(t_Table *Table, char *File, long block_number)
  { /* reads all or a single data block from 'file' and returns a Table structure  */
    return(Table_Read_Offset(Table, File, block_number, NULL, 0));
  } /* end Table_Read */

/*******************************************************************************
* long Table_Read_Offset(t_Table *Table, char *name, int block_number, long *offset
*                        long max_rows)
*   ACTION: read a single Table from a text file, starting at offset
*     Same as Table_Read(..) except:
*   input   offset:    pointer to an offset (*offset should be 0 at start)
*           max_rows: max number of data rows to read from file (0 means all)
*   return  initialized single Table t_Table structure containing data, header, ...
*           number of read elements (-1: error, 0:header only)
*           updated *offset position (where end of reading occured)
*******************************************************************************/
  long Table_Read_Offset(t_Table *Table, char *File,
                         long block_number, long *offset,
                         long max_rows)
  { /* reads all/a data block in 'file' and returns a Table structure  */
    FILE *hfile;
    long  nelements=0;
    long  begin=0;
    long  filesize=0;
    char  name[1024];
    char  path[1024];
    struct stat stfile;

    /*Need to be able to store the pointer*/
    if (!Table) return(-1);

    /*TK: Valgrind flags it as usage of uninitialised variable: */
    Table->quiet = 0;

    //if (offset && *offset) snprintf(name, 1024, "%s@%li", File, *offset);
    //else                   
    strncpy(name, File, 1024);
    if(offset && *offset){
        begin=*offset;
    }
    /* Check if the table has already been read from file.
     * If so just reuse the table, if not (this is flagged by returning NULL
     * set up a new table and read the data into it */
    t_Table *tab_p= Table_File_List_find(name,block_number,begin);
    if ( tab_p!=NULL ){
        /*table was found in the Table_File_List*/
        *Table=*tab_p;
        MPI_MASTER(
            if(Table->quiet<1)
              printf("Reusing input file '%s' (Table_Read_Offset)\n", name);
            );
        return Table->rows*Table->columns;
    }

    /* open the file */
    hfile = Open_File(File, "r", path);
    if (!hfile) return(-1);
    else {
      MPI_MASTER(
          if(Table->quiet<1)
            printf("Opening input file '%s' (Table_Read_Offset)\n", path);
          );
    }
    
    /* read file state */
    stat(path,&stfile); filesize = stfile.st_size;
    if (offset && *offset) fseek(hfile, *offset, SEEK_SET);
    begin     = ftell(hfile);
    
    Table_Init(Table, 0, 0);

    /* read file content and set the Table */
    nelements = Table_Read_Handle(Table, hfile, block_number, max_rows, name);
    Table->begin = begin;
    Table->end   = ftell(hfile);
    Table->filesize = (filesize>0 ? filesize : 0);
    Table_Stat(Table);
    
    Table_File_List_store(Table);

    if (offset) *offset=Table->end;
    fclose(hfile);
    return(nelements);

  } /* end Table_Read_Offset */

/*******************************************************************************
* long Table_Read_Offset_Binary(t_Table *Table, char *File, char *type,
*                               long *offset, long rows, long columns)
*   ACTION: read a single Table from a binary file, starting at offset
*     Same as Table_Read_Offset(..) except that it handles binary files.
*   input   type: may be "float"/NULL or "double"
*           offset: pointer to an offset (*offset should be 0 at start)
*           rows   : number of rows (0 means read all)
*           columns: number of columns
*   return  initialized single Table t_Table structure containing data, header, ...
*           number of read elements (-1: error, 0:header only)
*           updated *offset position (where end of reading occured)
*******************************************************************************/
  long Table_Read_Offset_Binary(t_Table *Table, char *File, char *type,
                                long *offset, long rows, long columns)
  { /* reads all/a data block in binary 'file' and returns a Table structure  */
    long    nelements, sizeofelement;
    long    filesize;
    FILE   *hfile;
    char    path[1024];
    struct stat stfile;
    double *data    = NULL;
    double *datatmp = NULL;
    long    i;
    long    begin;

    if (!Table) return(-1);

    Table_Init(Table, 0, 0);
    
    /* open the file */
    hfile = Open_File(File, "r", path);
    if (!hfile) return(-1);
    else {
      MPI_MASTER(
          if(Table->quiet<1)
            printf("Opening input file '%s' (Table_Read, Binary)\n", path);
      );
    }
    
    /* read file state */
    stat(File,&stfile);
    filesize = stfile.st_size;
    Table->filesize=filesize;
    
    /* read file content */
    if (type && !strcmp(type,"double")) sizeofelement = sizeof(double);
    else  sizeofelement = sizeof(float);
    if (offset && *offset) fseek(hfile, *offset, SEEK_SET);
    begin     = ftell(hfile);
    if (rows && filesize > sizeofelement*columns*rows)
      nelements = columns*rows;
    else nelements = (long)(filesize/sizeofelement);
    if (!nelements || filesize <= *offset) return(0);
    data    = (double*)malloc(nelements*sizeofelement);
    if (!data) {
      if(!(Table->quiet>1))
        fprintf(stderr,"Error: allocating %ld elements for %s file '%s'. Too big (Table_Read_Offset_Binary).\n", nelements, type, File);
      exit(-1);
    }
    nelements = fread(data, sizeofelement, nelements, hfile);

    if (!data || !nelements)
    {
      if(!(Table->quiet>1))
        fprintf(stderr,"Error: reading %ld elements from %s file '%s' (Table_Read_Offset_Binary)\n", nelements, type, File);
      exit(-1);
    }
    Table->begin   = begin;
    Table->end     = ftell(hfile);
    if (offset) *offset=Table->end;
    fclose(hfile);

    datatmp = (double*)realloc(data, (double)nelements*sizeofelement);
    if (!datatmp) {
      free(data);
      fprintf(stderr,"Error: reallocating %ld elements for %s file '%s'. Too big (Table_Read_Offset_Binary).\n", nelements, type, File);
      exit(-1);
    } else {
      data = datatmp;
    }
    /* copy file data into Table */
    if (type && !strcmp(type,"double")) Table->data = data;
    else {
      float  *s;
      double *dataf;
      s     = (float*)data;
      dataf = (double*)malloc(sizeof(double)*nelements);
      if (!dataf) {
	fprintf(stderr, "Could not allocate data block of size %ld\n", nelements);
	exit(-1);
      }
      for (i=0; i<nelements; i++)
        dataf[i]=s[i];
      free(data);
      Table->data = dataf;
    }
    strncpy(Table->filename, File, 1024);
    Table->rows    = nelements/columns;
    Table->columns = columns;
    Table->array_length = 1;
    Table->block_number = 1;

    Table_Stat(Table);

    return(nelements);
  } /* end Table_Read_Offset_Binary */

/*******************************************************************************
* long Table_Read_Handle(t_Table *Table, FILE *fid, int block_number, long max_rows, char *name)
*   ACTION: read a single Table from a text file handle (private)
*   input   Table:pointer to a t_Table structure
*           fid:  pointer to FILE handle
*           block_number: if the file does contain more than one
*                 data block, then indicates which one to get (from index 1)
*                 a 0 value means append/catenate all
*           max_rows: if non 0, only reads that number of lines
*   return  initialized single Table t_Table structure containing data, header, ...
*           modified Table t_Table structure containing data, header, ...
*           number of read elements (-1: error, 0:header only)
* The routine stores any line starting with '#', '%' and ';' into the header
* Other lines are interpreted as numerical data, and stored.
* Data block should be a rectangular matrix or vector.
* Data block may be rebined with Table_Rebin (also sort in ascending order)
*******************************************************************************/
  long Table_Read_Handle(t_Table *Table, FILE *hfile,
                         long block_number, long max_rows, char *name)
  { /* reads all/a data block from 'file' handle and returns a Table structure  */
    double *Data              = NULL;
    double *Datatmp           = NULL;
    char *Header              = NULL;
    char *Headertmp           = NULL;
    long  malloc_size         = CHAR_BUF_LENGTH;
    long  malloc_size_h       = 4096;
    long  Rows = 0,   Columns = 0;
    long  count_in_array      = 0;
    long  count_in_header     = 0;
    long  count_invalid       = 0;
    long  block_Current_index = 0;
    char  flag_End_row_loop   = 0;

    if (!Table) return(-1);
    Table_Init(Table, 0, 0);
    if (name && name[0]!='\0') strncpy(Table->filename, name, 1024);

    if(!hfile) {
       fprintf(stderr, "Error: File handle is NULL (Table_Read_Handle).\n");
       return (-1);
    }
    Header = (char*)  calloc(malloc_size_h, sizeof(char));
    Data   = (double*)calloc(malloc_size,   sizeof(double));
    if ((Header == NULL) || (Data == NULL)) {
       fprintf(stderr, "Error: Could not allocate Table and Header (Table_Read_Handle).\n");
       return (-1);
    }

    int flag_In_array = 0;
    do { /* while (!flag_End_row_loop) */
      char  *line=malloc(1024*CHAR_BUF_LENGTH*sizeof(char));
      long  back_pos=0;   /* ftell start of line */

      if (!line) {
	fprintf(stderr,"Could not allocate line buffer\n");
	exit(-1);
      }
      back_pos = ftell(hfile);
      if (fgets(line, 1024*CHAR_BUF_LENGTH, hfile) != NULL) { /* analyse line */
        /* first skip blank and tabulation characters */
        int i = strspn(line, " \t");

        /* handle comments: stored in header */
        if (NULL != strchr("#%;/", line[i]))
        { /* line is a comment */
          count_in_header += strlen(line);
          if (count_in_header >= malloc_size_h) {
            /* if succeed and in array : add (and realloc if necessary) */
            malloc_size_h = count_in_header+4096;
            char *Headertmp = (char*)realloc(Header, malloc_size_h*sizeof(char));
	    if(!Headertmp) {
	      free(Header);
	             fprintf(stderr, "Error: Could not reallocate Header (Table_Read_Handle).\n");
		     free(Header);
		     return (-1);
	    } else {
	      Header = Headertmp;
	    }
          }
          strncat(Header, line, 4096);
          flag_In_array=0;
          /* exit line and file if passed desired block */
          if (block_number > 0 && block_number == block_Current_index) {
            flag_End_row_loop = 1;
          }

          /* Continue with next line */
          continue;
        }
        if (strstr(line, "***"))
        {
          count_invalid++;
          /* Continue with next line */
          continue;
        }

        /* get the number of columns splitting line with strtok */
        char  *lexeme;
        char  flag_End_Line = 0;
        long  block_Num_Columns = 0;
        const char seps[] = " ,;\t\n\r";

        lexeme = strtok(line, seps);
        while (!flag_End_Line) {
          if ((lexeme != NULL) && (lexeme[0] != '\0')) {
            /* reading line: the token is not empty */
            double X;
            int    count=1;
            /* test if we have 'NaN','Inf' */
            if (!strncasecmp(lexeme,"NaN",3))
              X = 0;
            else if (!strncasecmp(lexeme,"Inf",3) || !strncasecmp(lexeme,"+Inf",4))
              X = FLT_MAX;
            else if (!strncasecmp(lexeme,"-Inf",4))
              X = -FLT_MAX;
            else
              count = sscanf(lexeme,"%lg",&X);
            if (count == 1) {
              /* reading line: the token is a number in the line */
              if (!flag_In_array) {
                /* reading num: not already in a block: starts a new data block */
                block_Current_index++;
                flag_In_array    = 1;
                block_Num_Columns= 0;
                if (block_number > 0) {
                  /* initialise a new data block */
                  Rows = 0;
                  count_in_array = 0;
                } /* else append */
              }
              /* reading num: all blocks or selected block */
              if (flag_In_array && (block_number == 0 ||
                  block_number == block_Current_index)) {
                /* starting block: already the desired number of rows ? */
                if (block_Num_Columns == 0 &&
                    max_rows > 0 && Rows >= max_rows) {
                  flag_End_Line      = 1;
                  flag_End_row_loop  = 1;
                  flag_In_array      = 0;
                  /* reposition to begining of line (ignore line) */
                  fseek(hfile, back_pos, SEEK_SET);
                } else { /* store into data array */
                  if (count_in_array >= malloc_size) {
                    /* realloc data buffer if necessary */
                    malloc_size = count_in_array*1.5;
                    Datatmp = (double*) realloc(Data, malloc_size*sizeof(double));
                    if (Datatmp == NULL) {
                      fprintf(stderr, "Error: Can not re-allocate memory %zi (Table_Read_Handle).\n",
                              malloc_size*sizeof(double));
		      free(Data);
                      return (-1);
                    } else {
                      Data=Datatmp;
                    }
                  }
                  if (0 == block_Num_Columns) Rows++;
                  Data[count_in_array] = X;
                  count_in_array++;
                  block_Num_Columns++;
                }
              } /* reading num: end if flag_In_array */
            } /* end reading num: end if sscanf lexeme -> numerical */
            else {
              /* reading line: the token is not numerical in that line. end block */
              if (block_Current_index == block_number) {
                flag_End_Line = 1;
                flag_End_row_loop = 1;
              } else {
                flag_In_array = 0;
                flag_End_Line = 1;
              }
            }
          }
          else {
            /* no more tokens in line */
            flag_End_Line = 1;
            if (block_Num_Columns > 0) Columns = block_Num_Columns;
          }

          // parse next token
          lexeme = strtok(NULL, seps);

        } /* while (!flag_End_Line) */
      } /* end: if fgets */
      else flag_End_row_loop = 1; /* else fgets : end of file */
      free(line);
    } while (!flag_End_row_loop); /* end while flag_End_row_loop */

    Table->block_number = block_number;
    Table->array_length = 1;

    // shrink header to actual size (plus terminating 0-byte)
    if (count_in_header) {
      Headertmp = (char*)realloc(Header, count_in_header*sizeof(char) + 1);
      if(!Headertmp) {
	fprintf(stderr, "Error: Could not shrink Header (Table_Read_Handle).\n");
	free(Header);
	return (-1);
      } else {
        Header = Headertmp;
      }
    }
    Table->header = Header;

    if (count_in_array*Rows*Columns == 0)
    {
      Table->rows         = 0;
      Table->columns      = 0;
      free(Data);
      return (0);
    }
    if (Rows * Columns != count_in_array)
    {
      fprintf(stderr, "Warning: Read_Table :%s %s Data has %li values that should be %li x %li\n",
        (Table->filename[0] != '\0' ? Table->filename : ""),
        (!block_number ? " catenated" : ""),
        count_in_array, Rows, Columns);
      Columns = count_in_array; Rows = 1;
    }
    if (count_invalid)
    {
      fprintf(stderr,"Warning: Read_Table :%s %s Data has %li invalid lines (*****). Ignored.\n",
      (Table->filename[0] != '\0' ? Table->filename : ""),
        (!block_number ? " catenated" : ""),
        count_invalid);
    }
    Datatmp     = (double*)realloc(Data, count_in_array*sizeof(double));
    if(!Datatmp) {
      fprintf(stderr, "Error: Could reallocate Data block to %li doubles (Table_Read_Handle).\n", count_in_array);
      free(Data);
      return (-1);
    } else {
      Data = Datatmp;
    }
    Table->data         = Data;
    Table->rows         = Rows;
    Table->columns      = Columns;

    return (count_in_array);

  } /* end Table_Read_Handle */

/*******************************************************************************
* long Table_Rebin(t_Table *Table)
*   ACTION: rebin a single Table, sorting 1st column in ascending order
*   input   Table: single table containing data.
*                  The data block is reallocated in this process
*   return  updated Table with increasing, evenly spaced first column (index 0)
*           number of data elements (-1: error, 0:empty data)
*******************************************************************************/
  long Table_Rebin(t_Table *Table)
  {
    double new_step=0;
    long   i;
    /* performs linear interpolation on X axis (0-th column) */

    if (!Table) return(-1);
    if (!Table->data 
    || Table->rows*Table->columns == 0 || !Table->step_x)
      return(0);
    Table_Stat(Table); /* recompute statitstics and minimal step */
    new_step = Table->step_x; /* minimal step in 1st column */

    if (!(Table->constantstep)) /* not already evenly spaced */
    {
      long Length_Table;
      double *New_Table;

      Length_Table = ceil(fabs(Table->max_x - Table->min_x)/new_step)+1;
      /*return early if the rebinned table will become too large*/
      if (Length_Table > mcread_table_rebin_maxsize){
        fprintf(stderr,"WARNING: (Table_Rebin): Rebinning table from %s would exceed 1M rows. Skipping.\n", Table->filename); 
        return(Table->rows*Table->columns);
      }
      New_Table    = (double*)malloc(Length_Table*Table->columns*sizeof(double));
      if (!New_Table) {
	fprintf(stderr,"Could not allocate New_Table of size %ld x %ld\n", Length_Table, Table->columns);
	exit(-1);
      }
      for (i=0; i < Length_Table; i++)
      {
        long   j;
        double X;
        X = Table->min_x + i*new_step;
        New_Table[i*Table->columns] = X;
        for (j=1; j < Table->columns; j++)
          New_Table[i*Table->columns+j]
                = Table_Value(*Table, X, j);
      } /* end for i */

      Table->rows = Length_Table;
      Table->step_x = new_step;
      Table->max_x = Table->min_x + (Length_Table-1)*new_step; 
      /*max might not be the same anymore
       * Use Length_Table -1 since the first and laset rows are the limits of the defined interval.*/
      free(Table->data);
      Table->data = New_Table;
      Table->constantstep=1;
    } /* end else (!constantstep) */
    return (Table->rows*Table->columns);
  } /* end Table_Rebin */

/*******************************************************************************
* double Table_Index(t_Table Table, long i, long j)
*   ACTION: read an element [i,j] of a single Table
*   input   Table: table containing data
*           i : index of row      (0:Rows-1)
*           j : index of column   (0:Columns-1)
*   return  Value = data[i][j]
* Returns Value from the i-th row, j-th column of Table
* Tests are performed on indexes i,j to avoid errors
*******************************************************************************/

#ifndef MIN
#define MIN(a, b)  (((a) < (b)) ? (a) : (b))
#endif
#ifndef MAX
#define MAX(a, b)  (((a) > (b)) ? (a) : (b))
#endif

double Table_Index(t_Table Table, long i, long j)
{
  long AbsIndex;

  if (Table.rows == 1 || Table.columns == 1) {
    /* vector */
    j = MIN(MAX(0, i+j), Table.columns*Table.rows - 1);
    i = 0;
  } else {
    /* matrix */
    i = MIN(MAX(0, i), Table.rows - 1);
    j = MIN(MAX(0, j), Table.columns - 1);
  }

  /* handle vectors specifically */
  AbsIndex = i*(Table.columns)+j;

  if (Table.data != NULL)
    return (Table.data[AbsIndex]);
  else
    return 0;
} /* end Table_Index */

/*******************************************************************************
* void Table_SetElement(t_Table *Table, long i, long j, double value)
*   ACTION: set an element [i,j] of a single Table
*   input   Table: table containing data
*           i : index of row      (0:Rows-1)
*           j : index of column   (0:Columns-1)
*           value = data[i][j]
* Returns 0 in case of error
* Tests are performed on indexes i,j to avoid errors
*******************************************************************************/
int Table_SetElement(t_Table *Table, long i, long j,
                     double value)
{
  long AbsIndex;

  if (Table->rows == 1 || Table->columns == 1) {
    /* vector */
    j = MIN(MAX(0, i+j), Table->columns*Table->rows - 1); i=0;
  } else {
    /* matrix */
    i = MIN(MAX(0, i), Table->rows - 1);
    j = MIN(MAX(0, j), Table->columns - 1);
  }

  AbsIndex = i*(Table->columns)+j;
  if (Table->data != NULL) {
    Table->data[AbsIndex] = value;
    return 1;
  }

  return 0;
} /* end Table_SetElement */

/*******************************************************************************
* double Table_Value(t_Table Table, double X, long j)
*   ACTION: read column [j] of a single Table at row which 1st column is X
*   input   Table: table containing data.
*           X : data value in the first column (index 0)
*           j : index of column from which is extracted the Value (0:Columns-1)
*   return  Value = data[index for X][j] with linear interpolation
* Returns Value from the j-th column of Table corresponding to the
* X value for the 1st column (index 0)
* Tests are performed (within Table_Index) on indexes i,j to avoid errors
* NOTE: data should rather be monotonic, and evenly sampled.
*******************************************************************************/
double Table_Value(t_Table Table, double X, long j)
{
  long   Index = -1;
  double X1=0, Y1=0, X2=0, Y2=0;
  double ret=0;

  if (X > Table.max_x) return Table_Index(Table,Table.rows-1  ,j);
  if (X < Table.min_x) return Table_Index(Table,0  ,j);

  // Use constant-time lookup when possible
  if(Table.constantstep) {
    Index = (long)floor(
              (X - Table.min_x) / (Table.max_x - Table.min_x) * (Table.rows-1));
    X1 = Table_Index(Table,Index-1,0);
    X2 = Table_Index(Table,Index  ,0);
  }
  // Use binary search on large, monotonic tables
  else if(Table.monotonic && Table.rows > 100) {
    long left = Table.min_x;
    long right = Table.max_x;

    while (!((X1 <= X) && (X < X2)) && (right - left > 1)) {
      Index = (left + right) / 2;

      X1 = Table_Index(Table, Index-1, 0);
      X2 = Table_Index(Table, Index,   0);

      if (X < X1) {
        right = Index;
      } else {
        left  = Index;
      }
    }
  }

  // Fall back to linear search, if no-one else has set X1, X2 correctly
  if (!((X1 <= X) && (X < X2))) {
    /* look for index surrounding X in the table -> Index */
    for (Index=1; Index <= Table.rows-1; Index++) {
        X1 = Table_Index(Table, Index-1,0);
        X2 = Table_Index(Table, Index  ,0);
        if ((X1 <= X) && (X < X2)) break;
      } /* end for Index */
  }

  Y1 = Table_Index(Table,Index-1, j);
  Y2 = Table_Index(Table,Index  , j);

#ifdef OPENACC
#define strcmp(a,b) str_comp(a,b)
#endif

  if (!strcmp(Table.method,"linear")) {
    ret = Table_Interp1d(X, X1,Y1, X2,Y2);
  }
  else if (!strcmp(Table.method,"nearest")) {
    ret = Table_Interp1d_nearest(X, X1,Y1, X2,Y2);
  }

#ifdef OPENACC
#ifdef strcmp
#undef strcmp
#endif
#endif

  return ret;
} /* end Table_Value */

/*******************************************************************************
* double Table_Value2d(t_Table Table, double X, double Y)
*   ACTION: read element [X,Y] of a matrix Table
*   input   Table: table containing data.
*           X : row index, may be non integer
*           Y : column index, may be non integer
*   return  Value = data[index X][index Y] with bi-linear interpolation
* Returns Value for the indices [X,Y]
* Tests are performed (within Table_Index) on indexes i,j to avoid errors
* NOTE: data should rather be monotonic, and evenly sampled.
*******************************************************************************/
double Table_Value2d(t_Table Table, double X, double Y)
  {
    long   x1,x2,y1,y2;
    double z11,z12,z21,z22;
    double ret=0;

    x1 = (long)floor(X);
    y1 = (long)floor(Y);

    if (x1 > Table.rows-1 || x1 < 0) {
      x2 = x1;
    } else {
      x2 = x1 + 1;
    }

    if (y1 > Table.columns-1 || y1 < 0) {
      y2 = y1;
    } else {
      y2 = y1 + 1;
    }

    z11 = Table_Index(Table, x1, y1);

    if (y2 != y1) z12=Table_Index(Table, x1, y2); else z12 = z11;
    if (x2 != x1) z21=Table_Index(Table, x2, y1); else z21 = z11;
    if (y2 != y1) z22=Table_Index(Table, x2, y2); else z22 = z21;

#ifdef OPENACC
#define strcmp(a,b) str_comp(a,b)
#endif

    if (!strcmp(Table.method,"linear"))
      ret = Table_Interp2d(X,Y, x1,y1,x2,y2, z11,z12,z21,z22);
#ifdef OPENACC
#ifdef strcmp
#undef strcmp
#endif
#endif
    else {
      if (fabs(X-x1) < fabs(X-x2)) {
        if (fabs(Y-y1) < fabs(Y-y2)) ret = z11; else ret = z12;
      } else {
        if (fabs(Y-y1) < fabs(Y-y2)) ret = z21; else ret = z22;
      }
    }
    return ret;
  } /* end Table_Value2d */


/*******************************************************************************
* void Table_Free(t_Table *Table)
*   ACTION: free a single Table. First Call Table_File_list_gc. If this returns
*   non-zero it means there are more refernces to the table, and so the table
*   should not bee freed.
*   return: empty Table
*******************************************************************************/
  void Table_Free(t_Table *Table)
  {
    if( !Table_File_List_gc(Table) ){
       return;
    } 
    if (!Table) return;
    if (Table->data   != NULL) free(Table->data);
    if (Table->header != NULL) free(Table->header);
    Table->data   = NULL;
    Table->header = NULL;
  } /* end Table_Free */

/******************************************************************************
* void Table_Info(t_Table Table)
*    ACTION: print informations about a single Table
*******************************************************************************/
  long Table_Info(t_Table Table)
  {
    char buffer[256];
    long ret=0;

    if (!Table.block_number) strcpy(buffer, "catenated");
    else sprintf(buffer, "block %li", Table.block_number);
    printf("Table from file '%s' (%s)",
        Table.filename[0] != '\0' ? Table.filename : "", buffer);
    if ((Table.data != NULL) && (Table.rows*Table.columns))
    {
      printf(" is %li x %li ", Table.rows, Table.columns);
      if (Table.rows*Table.columns > 1)
        printf("(x=%g:%g)", Table.min_x, Table.max_x);
      else printf("(x=%g) ", Table.min_x);
      ret = Table.rows*Table.columns;
      if (Table.monotonic)    printf(", monotonic");
      if (Table.constantstep) printf(", constant step");
      printf(". interpolation: %s\n", Table.method);
    }
    else printf(" is empty.\n");

    if (Table.header && strlen(Table.header)) {
      char *header;
      int  i;
      header = malloc(80);
      if (!header) return(ret);
      for (i=0; i<80; header[i++]=0);
      strncpy(header, Table.header, 75);
      if (strlen(Table.header) > 75) {
        strcat( header, " ...");
      }
      for (i=0; i<strlen(header); i++)
        if (header[i] == '\n' || header[i] == '\r') header[i] = ';';
      printf("  '%s'\n", header);
      free(header);
    }

    return(ret);
  } /* end Table_Info */

/******************************************************************************
* long Table_Init(t_Table *Table, m, n)
*   ACTION: initialise a Table to empty m by n table
*   return: empty Table
******************************************************************************/
long Table_Init(t_Table *Table, long rows, long columns)
{
  double *data=NULL;
  long   i;

  if (!Table) return(0);

  Table->header  = NULL;
  Table->filename[0]= '\0';
  Table->filesize= 0;
  Table->min_x   = 0;
  Table->max_x   = 0;
  Table->step_x  = 0;
  Table->block_number = 0;
  Table->array_length = 0;
  Table->monotonic    = 0;
  Table->constantstep = 0;
  Table->begin   = 0;
  Table->end     = 0;
  strcpy(Table->method,"linear");

  if (rows*columns >= 1) {
    data    = (double*)malloc(rows*columns*sizeof(double));
    if (data) for (i=0; i < rows*columns; data[i++]=0);
    else {
      if(Table->quiet<2)
        fprintf(stderr,"Error: allocating %ld double elements."
            "Too big (Table_Init).\n", rows*columns);
      rows = columns = 0;
    }
  }
  Table->rows    = (rows >= 1 ? rows : 0);
  Table->columns = (columns >= 1 ? columns : 0);
  Table->data    = data;
  return(Table->rows*Table->columns);
} /* end Table_Init */

/******************************************************************************
* long Table_Write(t_Table Table, char *file, x1,x2, y1,y2)
*   ACTION: write a Table to disk (ascii).
*     when x1=x2=0 or y1=y2=0, the table default limits are used.
*   return: 0=all is fine, non-0: error
*******************************************************************************/
MCDETECTOR Table_Write(t_Table Table, char *file, char *xl, char *yl, 
  double x1, double x2, double y1, double y2)
{
  MCDETECTOR detector;

  if ((Table.data == NULL) && (Table.rows*Table.columns)) {
    detector.m = 0;
    detector.xmin = 0;
    detector.xmax = 0;
    detector.ymin = 0;
    detector.ymax = 0;
    detector.zmin = 0;
    detector.zmax = 0; 
    detector.intensity = 0;
    detector.error = 0;
    detector.events = 0;
    detector.min = 0;
    detector.max = 0;
    detector.mean = 0;
    detector.centerX = 0;
    detector.halfwidthX = 0;
    detector.centerY = 0;
    detector.halfwidthY = 0;
    detector.rank = 0;
    detector.istransposed = 0;
    detector.n = 0;
    detector.p = 0;
    detector.date_l = 0;
    detector.p0 = NULL;
    detector.p1 = NULL;
    detector.p2 = NULL;
    return(detector); /* Table is empty - nothing to do */
  }
  if (!x1 && !x2) {
    x1 = Table.min_x;
    x2 = Table.max_x;
  }
  if (!y1 && !y2) {
    y1 = 1;
    y2 = Table.columns;
  }

  /* transfer content of the Table into a 2D detector */
  Coords coords = { 0, 0, 0};
  Rotation rot;
  rot_set_rotation(rot, 0, 0, 0);
  
  if (Table.rows == 1 || Table.columns == 1) {
    detector = mcdetector_out_1D(Table.filename,
                      xl ? xl : "", yl ? yl : "",
                      "x", x1, x2,
                      Table.rows * Table.columns,
                      NULL, Table.data, NULL,
		      file, file, coords, rot,9999);
  } else {
    detector = mcdetector_out_2D(Table.filename,
                      xl ? xl : "", yl ? yl : "",
                      x1, x2, y1, y2,
                      Table.rows, Table.columns,
                      NULL, Table.data, NULL,
		      file, file, coords, rot,9999);
  }
  return(detector);
}

/******************************************************************************
* void Table_Stat(t_Table *Table)
*   ACTION: computes min/max/mean step of 1st column for a single table (private)
*   return: updated Table
*******************************************************************************/
  static void Table_Stat(t_Table *Table)
  {
    long   i;
    double max_x, min_x;
    double row=1;
    char   monotonic=1;
    char   constantstep=1;
    double step=0;
    long n;

    if (!Table) return;
    if (!Table->rows || !Table->columns) return;
    if (Table->rows == 1) row=0; // single row
    max_x = -FLT_MAX;
    min_x =  FLT_MAX;
    n     = (row ? Table->rows : Table->columns);
    /* get min and max of first column/vector */
    for (i=0; i < n; i++)
    {
      double X;
      X = (row ? Table_Index(*Table,i  ,0)
                               : Table_Index(*Table,0, i));
      if (X < min_x) min_x = X;
      if (X > max_x) max_x = X;
    } /* for */
    
    /* test for monotonicity and constant step if the table is an XY or single vector */
    if (n > 1) {
      /* mean step */
      step = (max_x - min_x)/(n-1);
      /* now test if table is monotonic on first column, and get minimal step size */
      for (i=0; i < n-1; i++) {
        double X, diff;;
        X    = (row ? Table_Index(*Table,i  ,0)
                    : Table_Index(*Table,0,  i));
        diff = (row ? Table_Index(*Table,i+1,0)
                    : Table_Index(*Table,0,  i+1)) - X;
        if (diff && fabs(diff) < fabs(step)) step = diff;
        /* change sign ? */
        if ((max_x - min_x)*diff < 0 && monotonic)
          monotonic = 0;
      } /* end for */
      
      /* now test if steps are constant within READ_TABLE_STEPTOL */
      if(!step){
        /*means there's a disconitnuity -> not constantstep*/
        constantstep=0;
      }else if (monotonic) {
        for (i=0; i < n-1; i++) {
          double X, diff;
          X    = (row ? Table_Index(*Table,i  ,0)
              : Table_Index(*Table,0,  i));
          diff = (row ? Table_Index(*Table,i+1,0)
              : Table_Index(*Table,0,  i+1)) - X;
          if ( fabs(step)*(1+READ_TABLE_STEPTOL) < fabs(diff) ||
                fabs(diff) < fabs(step)*(1-READ_TABLE_STEPTOL) )
          { constantstep = 0; break; }
        }
      }

    }
    Table->step_x= step;
    Table->max_x = max_x;
    Table->min_x = min_x;
    Table->monotonic = monotonic;
    Table->constantstep = constantstep;
  } /* end Table_Stat */

/******************************************************************************
* t_Table *Table_Read_Array(char *File, long *blocks)
*   ACTION: read as many data blocks as available, iteratively from file
*   return: initialized t_Table array, last element is an empty Table.
*           the number of extracted blocks in non NULL pointer *blocks
*******************************************************************************/
  t_Table *Table_Read_Array(char *File, long *blocks)
  {
    t_Table *Table_Array    = NULL;
    t_Table *Table_Arraytmp = NULL;
    long offset=0;
    long block_number=0;
    long allocated=256;
    long nelements=1;

    /* first allocate an initial empty t_Table array */
    Table_Array = (t_Table *)malloc(allocated*sizeof(t_Table));
    if (!Table_Array) {
      fprintf(stderr, "Error: Can not allocate memory %zi (Table_Read_Array).\n",
         allocated*sizeof(t_Table));
      *blocks = 0;
      return (NULL);
    }

    while (nelements > 0)
    {
      t_Table Table;

      /* if ok, set t_Table block number else exit loop */
      block_number++;
      Table.block_number = block_number;
      
      /* access file at offset and get following block. Block number is from the set offset
       * hence the hardcoded 1 - i.e. the next block counted from offset.*/
      nelements = Table_Read_Offset(&Table, File, 1, &offset,0);
      /*if the block is empty - don't store it*/
      if (nelements>0){
          /* if t_Table array is not long enough, expand and realocate */
          if (block_number >= allocated-1) {
              allocated += 256;
              Table_Arraytmp = (t_Table *)realloc(Table_Array,
                      allocated*sizeof(t_Table));
              if (!Table_Arraytmp) {
                  fprintf(stderr, "Error: Can not re-allocate memory %zi (Table_Read_Array).\n",
                          allocated*sizeof(t_Table));
                  free(Table_Array);
                  *blocks = 0;
                  return (NULL);
              } else {
                Table_Array = Table_Arraytmp;
              }
          }
          /* store it into t_Table array */
          //snprintf(Table.filename, 1024, "%s#%li", File, block_number-1);
          Table_Array[block_number-1] = Table;
      }
      /* continues until we find an empty block */
    }
    /* send back number of extracted blocks */
    if (blocks) *blocks = block_number-1;

    /* now store total number of elements in Table array */
    for (offset=0; offset < block_number;
      Table_Array[offset++].array_length = block_number-1);

    return(Table_Array);
  } /* end Table_Read_Array */
/*******************************************************************************
* void Table_Free_Array(t_Table *Table)
*   ACTION: free a Table array
*******************************************************************************/
  void Table_Free_Array(t_Table *Table)
  {
    long index;
    if (!Table) return;
    for (index=0;index < Table[0].array_length; index++){
            Table_Free(&Table[index]);
    }
    free(Table);
  } /* end Table_Free_Array */

/******************************************************************************
* long Table_Info_Array(t_Table *Table)
*    ACTION: print informations about a Table array
*    return: number of elements in the Table array
*******************************************************************************/
  long Table_Info_Array(t_Table *Table)
  {
    long index=0;

    if (!Table) return(-1);
    while (index < Table[index].array_length
       && (Table[index].data || Table[index].header)
       && (Table[index].rows*Table[index].columns) ) {
      Table_Info(Table[index]);
      index++;
    }
    printf("This Table array contains %li elements\n", index);
    return(index);
  } /* end Table_Info_Array */

/******************************************************************************
* char **Table_ParseHeader(char *header, symbol1, symbol2, ..., NULL)
*    ACTION: search for char* symbols in header and return their value or NULL
*            the search is not case sensitive.
*            Last argument MUST be NULL
*    return: array of char* with line following each symbol, or NULL if not found
*******************************************************************************/
#ifndef MyNL_ARGMAX
#define MyNL_ARGMAX 50
#endif

char **Table_ParseHeader_backend(char *header, ...){
  va_list ap;
  char exit_flag=0;
  int counter   =0;
  char **ret    =NULL;
  if (!header || header[0]=='\0') return(NULL);

  ret = (char**)calloc(MyNL_ARGMAX, sizeof(char*));
  if (!ret) {
    printf("Table_ParseHeader: Cannot allocate %i values array for Parser (Table_ParseHeader).\n",
      MyNL_ARGMAX);
    return(NULL);
  }
  for (counter=0; counter < MyNL_ARGMAX; ret[counter++] = NULL);
  counter=0;

  va_start(ap, header);
  while(!exit_flag && counter < MyNL_ARGMAX-1)
  {
    char *arg_char=NULL;
    char *pos     =NULL;
    /* get variable argument value as a char */
    arg_char = va_arg(ap, char *);
    if (!arg_char || arg_char[0]=='\0'){
      exit_flag = 1; break;
    }
    /* search for the symbol in the header */
    pos = (char*)strcasestr(header, arg_char);
    if (pos) {
      char *eol_pos;
      eol_pos = strchr(pos+strlen(arg_char), '\n');
      if (!eol_pos)
        eol_pos = strchr(pos+strlen(arg_char), '\r');
      if (!eol_pos)
        eol_pos = pos+strlen(pos)-1;
      ret[counter] = (char*)malloc(eol_pos - pos);
      if (!ret[counter]) {
        printf("Table_ParseHeader: Cannot allocate value[%i] array for Parser searching for %s (Table_ParseHeader).\n",
          counter, arg_char);
        exit_flag = 1; break;
      }
      strncpy(ret[counter], pos+strlen(arg_char), eol_pos - pos - strlen(arg_char));
      ret[counter][eol_pos - pos - strlen(arg_char)]='\0';
    }
    counter++;
  }
  va_end(ap);
  return(ret);
} /* Table_ParseHeader */

/******************************************************************************
* double Table_Interp1d(x, x1, y1, x2, y2)
*    ACTION: interpolates linearly at x between y1=f(x1) and y2=f(x2)
*    return: y=f(x) value
*******************************************************************************/
double Table_Interp1d(double x,
  double x1, double y1,
  double x2, double y2)
{
  double slope;
  if (x2 == x1) return (y1+y2)/2;
  if (y1 == y2) return  y1;
  slope = (y2 - y1)/(x2 - x1);
  return y1+slope*(x - x1);
} /* Table_Interp1d */

/******************************************************************************
* double Table_Interp1d_nearest(x, x1, y1, x2, y2)
*    ACTION: table lookup with nearest method at x between y1=f(x1) and y2=f(x2)
*    return: y=f(x) value
*******************************************************************************/
double Table_Interp1d_nearest(double x,
  double x1, double y1,
  double x2, double y2)
{
  if (fabs(x-x1) < fabs(x-x2)) return (y1);
  else return(y2);
} /* Table_Interp1d_nearest */

/******************************************************************************
* double Table_Interp2d(x,y, x1,y1, x2,y2, z11,z12,z21,z22)
*    ACTION: interpolates bi-linearly at (x,y) between z1=f(x1,y1) and z2=f(x2,y2)
*    return: z=f(x,y) value
*    x,y |   x1   x2
*    ----------------
*     y1 |   z11  z21
*     y2 |   z12  z22
*******************************************************************************/
double Table_Interp2d(double x, double y,
  double x1, double y1,
  double x2, double y2,
  double z11, double z12, double z21, double z22)
{
  double ratio_x, ratio_y;
  if (x2 == x1) return Table_Interp1d(y, y1,z11, y2,z12);
  if (y1 == y2) return Table_Interp1d(x, x1,z11, x2,z21);

  ratio_y = (y - y1)/(y2 - y1);
  ratio_x = (x - x1)/(x2 - x1);
  return (1-ratio_x)*(1-ratio_y)*z11 + ratio_x*(1-ratio_y)*z21
    + ratio_x*ratio_y*z22         + (1-ratio_x)*ratio_y*z12;
} /* Table_Interp2d */

/* end of read_table-lib.c */
#endif // READ_TABLE_LIB_C

/*******************************************************************************
*
* McStas, neutron ray-tracing package
*         Copyright (C) 1997-2008, All rights reserved
*         Risoe National Laboratory, Roskilde, Denmark
*         Institut Laue Langevin, Grenoble, France
*
* Runtime: share/interoff.h
*
* %Identification
* Written by: Reynald Arnerin
* Date:    Jun 12, 2008
* Release:
* Version:
*
* Object File Format intersection header for McStas. Requires the qsort function.
*
* Such files may be obtained with e.g.
*   qhull < points.xyz Qx Qv Tv o > points.off
* where points.xyz has format:
*   3
*   <nb_points>
*   <x> <y> <z>
*   ...
* The resulting file should have its first line being changed from '3' into 'OFF'.
* It can then be displayed with geomview.
* A similar, but somewhat older solution is to use 'powercrust' with e.g.
*   powercrust -i points.xyz
* which will generate a 'pc.off' file to be renamed as suited.
*
*******************************************************************************/

#ifndef INTEROFF_LIB_H
#define INTEROFF_LIB_H "$Revision$"

#ifndef OFF_EPSILON
#define OFF_EPSILON 1e-13
#endif

#ifndef OFF_INTERSECT_MAX
#ifdef OPENACC
#define OFF_INTERSECT_MAX 100
#else
#define OFF_INTERSECT_MAX 1024
#endif
#endif

//#include <float.h>

#define N_VERTEX_DISPLAYED    200000

typedef struct intersection {
	MCNUM time;  	  //time of the intersection
	Coords v;	      //intersection point
	Coords normal;  //normal vector of the surface intersected
	short in_out;	  //1 if the ray enters the volume, -1 otherwise
	short edge;	    //1 if the intersection is on the boundary of the polygon, and error is possible
	unsigned long index; // index of the face
} intersection;

typedef struct polygon {
  MCNUM* p;       //vertices of the polygon in adjacent order, this way : x1 | y1 | z1 | x2 | y2 | z2 ...
  int npol;       //number of vertices
  #pragma acc shape(p[0:npol]) init_needed(npol)
  Coords normal;
  double D;
} polygon;

typedef struct off_struct {
    long vtxSize;
    long polySize;
    long faceSize;
    Coords* vtxArray;
    #pragma acc shape(vtxArray[0:vtxSize]) init_needed(vtxSize)
    Coords* normalArray;
    #pragma acc shape(vtxArray[0:faceSize]) init_needed(faceSize)
    unsigned long* faceArray;
    #pragma acc shape(vtxArray[0:faceSize][0:polySize]) init_needed(faceSize,polySize)
    double* DArray;
    #pragma acc shape(vtxArray[0:polySize]) init_needed(polySize)
    char *filename;
    int mantidflag;
    long mantidoffset;
    intersection intersects[OFF_INTERSECT_MAX]; // After a call to off_intersect_all contains the list of intersections.
    int nextintersect;                 // 'Next' intersection (first t>0) solution after call to off_intersect_all
    int numintersect;               // Number of intersections after call to off_intersect_all
} off_struct;

/*******************************************************************************
* long off_init(  char *offfile, double xwidth, double yheight, double zdepth, off_struct* data)
* ACTION: read an OFF file, optionally center object and rescale, initialize OFF data structure
* INPUT: 'offfile' OFF file to read
*        'xwidth,yheight,zdepth' if given as non-zero, apply bounding box.
*           Specifying only one of these will also use the same ratio on all axes
*        'notcenter' center the object to the (0,0,0) position in local frame when set to zero
* RETURN: number of polyhedra and 'data' OFF structure
*******************************************************************************/
long off_init(  char *offfile, double xwidth, double yheight, double zdepth,
                int notcenter, off_struct* data);

/*******************************************************************************
* int off_intersect_all(double* t0, double* t3,
     Coords *n0, Coords *n3,
     double x, double y, double z,
     double vx, double vy, double vz,
     double ax, double ay, double az,
     off_struct *data )
* ACTION: computes intersection of neutron trajectory with an object.
* INPUT:  x,y,z and vx,vy,vz are the position and velocity of the neutron
*         ax, ay, az are the local acceleration vector
*         data points to the OFF data structure
* RETURN: the number of polyhedral which trajectory intersects
*         t0 and t3 are the smallest incoming and outgoing intersection times
*         n0 and n3 are the corresponding normal vectors to the surface
*         data is the full OFF structure, including a list intersection type
*******************************************************************************/
#pragma acc routine
int off_intersect_all(double* t0, double* t3,
     Coords *n0, Coords *n3,
     double x, double y, double z,
     double vx, double vy, double vz,
     double ax, double ay, double az,
     off_struct *data );

/*******************************************************************************
* int off_intersect(double* t0, double* t3,
     Coords *n0, Coords *n3,
     double x, double y, double z,
     double vx, double vy, double vz,
     double ax, double ay, double az,
     off_struct data )
* ACTION: computes intersection of neutron trajectory with an object.
* INPUT:  x,y,z and vx,vy,vz are the position and velocity of the neutron
*         ax, ay, az are the local acceleration vector
*         data points to the OFF data structure
* RETURN: the number of polyhedral which trajectory intersects
*         t0 and t3 are the smallest incoming and outgoing intersection times
*         n0 and n3 are the corresponding normal vectors to the surface
*******************************************************************************/
#pragma acc routine
int off_intersect(double* t0, double* t3,
     Coords *n0, Coords *n3,
     double x, double y, double z,
     double vx, double vy, double vz,
     double ax, double ay, double az,
     off_struct data );

/*****************************************************************************
* int off_intersectx(double* l0, double* l3,
     Coords *n0, Coords *n3,
     double x, double y, double z,
     double kx, double ky, double kz,
     off_struct data )
* ACTION: computes intersection of an xray trajectory with an object.
* INPUT:  x,y,z and kx,ky,kz, are spatial coordinates and wavevector of the x-ray
*         respectively. data points to the OFF data structure.
* RETURN: the number of polyhedral the trajectory intersects
*         l0 and l3 are the smallest incoming and outgoing intersection lengths
*         n0 and n3 are the corresponding normal vectors to the surface
*******************************************************************************/
#pragma acc routine
int off_x_intersect(double *l0,double *l3,
     Coords *n0, Coords *n3,
     double x,  double y,  double z,
     double kx, double ky, double kz,
     off_struct data );

/*******************************************************************************
* void off_display(off_struct data)
* ACTION: display up to N_VERTEX_DISPLAYED points from the object
*******************************************************************************/
void off_display(off_struct);

/*******************************************************************************
void p_to_quadratic(double eq[], Coords acc,
                    Coords pos, Coords vel,
                    double* teq)
* ACTION: define the quadratic for the intersection of a parabola with a plane
* INPUT: 'eq' plane equation
*        'acc' acceleration vector
*        'vel' velocity of the particle
*        'pos' position of the particle
*         equation of plane A * x + B * y + C * z - D = 0
*         eq[0] = (C*az)/2+(B*ay)/2+(A*ax)/2
*         eq[1] = C*vz+B*vy+A*vx
*         eq[2] = C*z0+B*y0+A*x0-D
* RETURN: equation of parabola: teq(0) * t^2 + teq(1) * t + teq(2)
*******************************************************************************/
void p_to_quadratic(Coords norm, MCNUM d, Coords acc, Coords pos, Coords vel,
		    double* teq);

/*******************************************************************************
int quadraticSolve(double eq[], double* x1, double* x2);
* ACTION: solves the quadratic for the roots x1 and x2 
*         eq[0] * t^2 + eq[1] * t + eq[2] = 0
* INPUT: 'eq' the coefficients of the parabola
* RETURN: roots x1 and x2 and the number of solutions
*******************************************************************************/
int quadraticSolve(double* eq, double* x1, double* x2);

#endif

/* end of interoff-lib.h */
/*******************************************************************************
*
* McStas, neutron ray-tracing package
*         Copyright (C) 1997-2008, All rights reserved
*         Risoe National Laboratory, Roskilde, Denmark
*         Institut Laue Langevin, Grenoble, France
*
* Runtime: share/interoff-lib.c
*
* %Identification
* Written by: Reynald Arnerin
* Date:    Jun 12, 2008
* Origin: ILL
* Release: $Revision$
* Version: McStas X.Y
*
* Object File Format intersection library for McStas. Requires the qsort function.
*
* Such files may be obtained with e.g.
*   qhull < points.xyz Qx Qv Tv o > points.off
* where points.xyz has format (it supports comments):
*   3
*   <nb_points>
*   <x> <y> <z>
*   ...
* The resulting file should have its first line being changed from '3' into 'OFF'.
* It can then be displayed with geomview.
* A similar, but somewhat older solution is to use 'powercrust' with e.g.
*   powercrust -i points.xyz
* which will generate a 'pc.off' file to be renamed as suited.
*
*******************************************************************************/

#ifndef INTEROFF_LIB_H
#include "interoff-lib.h"
#endif

#ifndef INTEROFF_LIB_C
#define INTEROFF_LIB_C "$Revision$"

#ifdef OPENACC // If on GPU map fprintf to printf
#define fprintf(stderr,...) printf(__VA_ARGS__)
#endif

#pragma acc routine
double off_F(double x, double y,double z,double A,double B,double C,double D) {
  return ( A*x + B*y + C*z + D );
}

#pragma acc routine
char off_sign(double a) {
  if (a<0)       return(-1);
  else if (a==0) return(0);
  else           return(1);
}

// off_normal ******************************************************************
//gives the normal vector of p
#pragma acc routine
void off_normal(Coords* n, polygon p)
{
  //using Newell method
  int i=0,j=0;
  n->x=0;n->y=0;n->z=0;
  for (i = 0, j = p.npol-1; i < p.npol; j = i++)
  {
    MCNUM x1=p.p[3*i],
          y1=p.p[3*i+1],
          z1=p.p[3*i+2];
    MCNUM x2=p.p[3*j],
          y2=p.p[3*j+1],
          z2=p.p[3*j+2];
    // n is the cross product of v1*v2
    n->x += (y1 - y2) * (z1 + z2);
    n->y += (z1 - z2) * (x1 + x2);
    n->z += (x1 - x2) * (y1 + y2);
  }
} /* off_normal */

// off_pnpoly ******************************************************************
//based on http://www.ecse.rpi.edu/Homepages/wrf/Research/Short_Notes/pnpoly.html
//return 0 if the vertex is out
//    1 if it is in
//   -1 if on the boundary
#pragma acc routine
int off_pnpoly(polygon p, Coords v)
{
  int i=0, c = 0;
  MCNUM minx=FLT_MAX,maxx=-FLT_MAX,miny=FLT_MAX,maxy=-FLT_MAX,minz=FLT_MAX,maxz=-FLT_MAX;
  MCNUM areax=0,areay=0,areaz=0;

  int pol2dx=0,pol2dy=1;          //2d restriction of the poly
  MCNUM x=v.x,y=v.y;

  /*areax: projected area with x-scratched = |v1_yz x v2_yz|, where v1=(x1-x0,0,z1-z0) & v2=(x2-x0,0,z2-z0).*/
  /* In principle, if polygon is triangle area should be scaled by 1/2, but this is irrelevant for finding the maximum area.*/
  /* Similarly for y and z scratched.*/
  areax=coords_len(coords_xp(
        coords_set(0,p.p[3*1+1]-p.p[0+1],p.p[3*1+2]-p.p[0+2]),
        coords_set(0,p.p[3*2+1]-p.p[0+1],p.p[3*2+2]-p.p[0+2])));
  areay=coords_len(coords_xp(
        coords_set(p.p[3*1+0]-p.p[0+0],0,p.p[3*1+2]-p.p[0+2]),
        coords_set(p.p[3*2+0]-p.p[0+0],0,p.p[3*2+2]-p.p[0+2])));
  areaz=coords_len(coords_xp(
        coords_set(p.p[3*1+0]-p.p[0+0],p.p[3*1+1]-p.p[0+1],0),
        coords_set(p.p[3*2+0]-p.p[0+0],p.p[3*2+1]-p.p[0+1],0)));

  if(areaz<areax){
    if(areax<areay){
      /*pick areay - i.e. scratch y*/
      pol2dy=2;
      y=v.z;
    }else{
      /*scratch x*/
      pol2dx=2;
      x=v.z;
    }
  }else if (areaz<areay){
    pol2dy=2;
    y=v.z;
  }

  //trace rays and test number of intersection
  int j;
  for (i = 0, j = p.npol-1; i < p.npol; j = i++) {
    if (((((p.p[3*i+pol2dy])<=y) && (y<(p.p[3*j+pol2dy]))) ||
         (((p.p[3*j+pol2dy])<=y) && (y<(p.p[3*i+pol2dy])))) &&
        (x < ( (p.p[3*j+pol2dx] - p.p[3*i+pol2dx]) * (y - p.p[3*i+pol2dy])
             / (p.p[3*j+pol2dy] - p.p[3*i+pol2dy]) + p.p[3*i+pol2dx]) ))
      c = !c;

    if (((fabs(p.p[3*i+pol2dy]-y)<=OFF_EPSILON) || ((fabs(p.p[3*j+pol2dy]-y)<=OFF_EPSILON))) &&
        fabs(x -((p.p[3*j+pol2dx] - p.p[3*i+pol2dx]) * (y - p.p[3*i+pol2dy])
          / (p.p[3*j+pol2dy] - p.p[3*i+pol2dy]) + p.p[3*i+pol2dx])) < OFF_EPSILON)
    {
      //the point lies on the edge
      c=-1;
      break;
    }
  }

  return c;
} /* off_pnpoly */

// off_intersectPoly ***********************************************************
//gives the intersection vertex between ray [a,b) and polygon p and its parametric value on (a b)
//based on http://geometryalgorithms.com/Archive/algorithm_0105/algorithm_0105.htm
#pragma acc routine
int off_intersectPoly(intersection *inter, Coords a, Coords b, polygon p)
{
  //direction vector of [a,b]
  Coords dir = {b.x-a.x, b.y-a.y, b.z-a.z};

  //the normal vector to the polygon
  Coords normale=p.normal;
  //off_normal(&normale, p); done at the init stage

  //direction vector from a to a vertex of the polygon
  Coords w0 = {a.x-p.p[0], a.y-p.p[1], a.z-p.p[2]};

  //scalar product
  MCNUM nw0  =-scalar_prod(normale.x,normale.y,normale.z,w0.x,w0.y,w0.z);
  MCNUM ndir = scalar_prod(normale.x,normale.y,normale.z,dir.x,dir.y,dir.z);
  inter->time = inter->edge = inter->in_out=0;
  inter->v = inter->normal = coords_set(0,0,1);

  if (fabs(ndir) < OFF_EPSILON)    // ray is parallel to polygon plane
  {
    if (nw0 == 0)              // ray lies in polygon plane (infinite number of solution)
      return 0;
    else return 0;             // ray disjoint from plane (no solution)
  }

  // get intersect point of ray with polygon plane
  inter->time = nw0 / ndir;            //parametric value the point on line (a,b)

  inter->v = coords_set(a.x + inter->time * dir.x,// intersect point of ray and plane
    a.y + inter->time * dir.y,
    a.z + inter->time * dir.z);

  int res=off_pnpoly(p,inter->v);

  inter->edge=(res==-1);
  if (ndir<0)
    inter->in_out=1;  //the negative dot product means we enter the surface
  else
    inter->in_out=-1;

  inter->normal=p.normal;

  return res;         //true if the intersection point lies inside the poly
} /* off_intersectPoly */


// off_getBlocksIndex **********************************************************
/*reads the indexes at the beginning of the off file as this :
line 1  OFF
line 2  nbVertex nbFaces nbEdges
*/
FILE *off_getBlocksIndex(char* filename, long* vtxSize, long* polySize )
{
  FILE* f = Open_File(filename,"r", NULL); /* from read_table-lib: FILE *Open_File(char *name, char *Mode, char *path) */
  if (!f) return (f);

  char line[CHAR_BUF_LENGTH];
  char *ret=0;
  *vtxSize = *polySize = 0;

  /* **************** start to read the file header */
  /* OFF file:
     'OFF' or '3'
   */

  ret=fgets(line,CHAR_BUF_LENGTH , f);// line 1 = "OFF"
  if (ret == NULL)
  {
    fprintf(stderr, "Error: Can not read 1st line in file %s (interoff/off_getBlocksIndex)\n", filename);
    exit(1);
  }
  if (strlen(line)>5)
  {
      fprintf(stderr,"Error: First line in %s is too long (=%lu). Possibly the line is not terminated by '\\n'.\n"
              "       The first line is required to be exactly 'OFF', '3' or 'ply'.\n",
              filename,(long unsigned)strlen(line));
      fclose(f);
      return(NULL);
  }

  if (strncmp(line,"OFF",3) && strncmp(line,"3",1) && strncmp(line,"ply",1))
  {
    fprintf(stderr, "Error: %s is probably not an OFF, NOFF or PLY file (interoff/off_getBlocksIndex).\n"
                    "       Requires first line to be 'OFF', '3' or 'ply'.\n",filename);
    fclose(f);
    return(NULL);
  }

  if (!strncmp(line,"OFF",3) || !strncmp(line,"3",1)) {
    do  /* OFF file: skip # comments which may be there */
    {
      ret=fgets(line,CHAR_BUF_LENGTH , f);
      if (ret == NULL)
      {
        fprintf(stderr, "Error: Can not read line in file %s (interoff/off_getBlocksIndex)\n", filename);
        exit(1);
      }
    } while (line[0]=='#');
    //line = nblines of vertex,faces and edges arrays
    sscanf(line,"%lu %lu",vtxSize,polySize);
  } else {
    do  /* PLY file: read all lines until find 'end_header'
           and locate 'element faces' and 'element vertex' */
    {
      ret=fgets(line,CHAR_BUF_LENGTH , f);
      if (ret == NULL)
      {
        fprintf(stderr, "Error: Can not read line in file %s (interoff/off_getBlocksIndex)\n", filename);
        exit(1);
      }
      if (!strncmp(line,"element face",12))
        sscanf(line,"element face %lu",polySize);
      else if (!strncmp(line,"element vertex",14))
        sscanf(line,"element vertex %lu",vtxSize);
      else if (!strncmp(line,"format binary",13))
        exit(fprintf(stderr,
          "Error: Can not read binary PLY file %s, only 'format ascii' (interoff/off_getBlocksIndex)\n%s\n",
          filename, line));
    } while (strncmp(line,"end_header",10));
  }

  /* The FILE is left opened ready to read 'vtxSize' vertices (vtxSize *3 numbers)
     and then polySize polygons (rows) */

  return(f);
} /* off_getBlocksIndex */

// off_init_planes *************************************************************
//gives the equations of 2 perpandicular planes of [ab]
#pragma acc routine
void off_init_planes(Coords a, Coords b,
  MCNUM* A1, MCNUM* C1, MCNUM* D1, MCNUM *A2, MCNUM* B2, MCNUM* C2, MCNUM* D2)
{
  //direction vector of [a b]
  Coords dir={b.x-a.x, b.y-a.y, b.z-a.z};

  //the plane parallel to the 'y' is computed with the normal vector of the projection of [ab] on plane 'xz'
  *A1= dir.z;
  *C1=-dir.x;
  if(*A1!=0 || *C1!=0)
    *D1=-(a.x)*(*A1)-(a.z)*(*C1);
  else
  {
    //the plane does not support the vector, take the one parallel to 'z''
    *A1=1;
    //B1=dir.x=0
    *D1=-(a.x);
  }
  //the plane parallel to the 'x' is computed with the normal vector of the projection of [ab] on plane 'yz'
  *B2= dir.z;
  *C2=-dir.y;
  *A2= 0;
  if (*B2==0 && *C2==0)
  {
    //the plane does not support the vector, take the one parallel to 'z'
    *B2=1;
    //B1=dir.x=0
    *D2=-(a.y);
  }
  else {
    if (dir.z==0)
    {
      //the planes are the same, take the one parallel to 'z'
      *A2= dir.y;
      *B2=-dir.x;
      *D2=-(a.x)*(*A2)-(a.y)*(*B2);
    }
    else
      *D2=-(a.y)**B2-(a.z)**C2;
  }
} /* off_init_planes */

// off_clip_3D_mod *************************************************************
#pragma acc routine
int off_clip_3D_mod(intersection* t, Coords a, Coords b,
  Coords* vtxArray, unsigned long vtxSize, unsigned long* faceArray,
  unsigned long faceSize, Coords* normalArray)
{
  MCNUM A1=0, C1=0, D1=0, A2=0, B2=0, C2=0, D2=0;      //perpendicular plane equations to [a,b]
  off_init_planes(a, b, &A1, &C1, &D1, &A2, &B2, &C2, &D2);

  int t_size=0;
  MCNUM popol[3*4]; /*3 dimensions and max 4 vertices to form a polygon*/
  unsigned long i=0,indPoly=0;

  //exploring the polygons :
  i=indPoly=0;
  while (i<faceSize)
  {
    polygon pol;
    pol.npol  = faceArray[i];                //nb vertex of polygon
    pol.p     = popol;
    pol.normal= coords_set(0,0,1);
    pol.D     = 1;
    unsigned long indVertP1=faceArray[++i];  //polygon's first vertex index in vtxTable
    int j=1;
    /*check whether vertex is left or right of plane*/
    char sg0=off_sign(off_F(vtxArray[indVertP1].x,vtxArray[indVertP1].y,vtxArray[indVertP1].z,A1,0,C1,D1));
    while (j<pol.npol)
    {
      //polygon's j-th vertex index in vtxTable
      unsigned long indVertP2=faceArray[i+j];
      /*check whether vertex is left or right of plane*/
      char sg1=off_sign(off_F(vtxArray[indVertP2].x,vtxArray[indVertP2].y,vtxArray[indVertP2].z,A1,0,C1,D1));
      if (sg0!=sg1) //if the plane intersect the polygon
        break;

      ++j;
    }

    if (j<pol.npol)          //ok, let's test with the second plane
    {
      char sg1=off_sign(off_F(vtxArray[indVertP1].x,vtxArray[indVertP1].y,vtxArray[indVertP1].z,A2,B2,C2,D2));//tells if vertex is left or right of the plane

      j=1;
      while (j<pol.npol)
      {
        //unsigned long indVertPi=faceArray[i+j];  //polyg's j-th vertex index in vtxTable
        Coords vertPi=vtxArray[faceArray[i+j]];
        if (sg1!=off_sign(off_F(vertPi.x,vertPi.y,vertPi.z,A2,B2,C2,D2)))//if the plane intersect the polygon
          break;
        ++j;
      }
      if (j<pol.npol)
      {
#ifdef OFF_LEGACY
        if (t_size>OFF_INTERSECT_MAX)
        {
          fprintf(stderr, "Warning: number of intersection exceeded (%d) (interoff-lib/off_clip_3D_mod)\n", OFF_INTERSECT_MAX);
            return (t_size);
        }
#endif
        //both planes intersect the polygon, let's find the intersection point
        //our polygon :
        int k;
        for (k=0; k<pol.npol; ++k)
        {
          Coords vertPk=vtxArray[faceArray[i+k]];
          pol.p[3*k]  =vertPk.x;
          pol.p[3*k+1]=vertPk.y;
          pol.p[3*k+2]=vertPk.z;
        }
        pol.normal=normalArray[indPoly];
        intersection x;
        if (off_intersectPoly(&x, a, b, pol))
        {
          x.index = indPoly;
#ifdef OFF_LEGACY
          t[t_size++]=x;
#else
	  /* Check against our 4 existing times, starting from [-FLT_MAX, FLT_MAX, FLT_MAX, FLT_MAX] */
	  /* Case 1, negative time? */
	  if (t_size < 4) t_size++;	  
	  if (x.time < 0) {
	    if (x.time > t[0].time) {
	      t[0]=x;
	    }
	  } else {
	    /* Case 2, positive time */
	    intersection xtmp;
	    if (x.time < t[3].time) {
	      t[3]=x;
	      if (t[3].time < t[2].time) {
		xtmp = t[2];
		t[2] = t[3];
		t[3] = xtmp;
	      }
	      if (t[2].time < t[1].time) {
		xtmp = t[1];
		t[1] = t[2];
		t[2] = xtmp;
	      }
	    } 
	  }
#endif
	}
      } /* if (j<pol.npol) */
    } /* if (j<pol.npol) */
    i += pol.npol;
    indPoly++;
  } /* while i<faceSize */
  return t_size;
} /* off_clip_3D_mod */

// off_clip_3D_mod_grav *************************************************************
/*******************************************************************************
version of off_clip_3D_mod_grav
*******************************************************************************/
#pragma acc routine seq
int off_clip_3D_mod_grav(intersection* t, Coords pos, Coords vel, Coords acc,
  Coords* vtxArray, unsigned long vtxSize, unsigned long* faceArray,
  unsigned long faceSize, Coords* normalArray, double* DArray)
{
  int t_size=0;
  MCNUM popol[3*CHAR_BUF_LENGTH];
  double plane_Eq [4];
  double quadratic [3];
  unsigned long i=0,indPoly=0;
  //exploring the polygons :
  i=indPoly=0;
  while (i<faceSize)
  {
    polygon pol;
    pol.npol  = faceArray[i];                //nb vertex of polygon
    pol.p     = popol;
    pol.normal= coords_set(0,0,1);
    unsigned long indVertP1=faceArray[++i];  //polygon's first vertex index in vtxTable
    
    if (t_size>CHAR_BUF_LENGTH)
      {
	fprintf(stderr, "Warning: number of intersection exceeded (%d) (interoff-lib/off_clip_3D_mod)\n", CHAR_BUF_LENGTH);
	return (t_size);
      }
    //both planes intersect the polygon, let's find the intersection point
    //our polygon :
    int k;
    for (k=0; k<pol.npol; ++k)
      {
	Coords vertPk=vtxArray[faceArray[i+k]];
	pol.p[3*k]  =vertPk.x;
	pol.p[3*k+1]=vertPk.y;
	pol.p[3*k+2]=vertPk.z;
      }
    pol.normal=normalArray[indPoly];
    pol.D=DArray[indPoly];
    p_to_quadratic(pol.normal, pol.D, acc, pos, vel, quadratic);
    double x1, x2;
    int nsol = quadraticSolve(quadratic, &x1, &x2);

    if (nsol >= 1) {
      double time = 1.0e36;
      if (x1 < time && x1 > 0.0) {
	time = x1;
      }
      if (nsol == 2 && x2 < time && x2 > 0.0) {
	time = x2;
      }
      if (time != 1.0e36) {
	intersection inters;
	double t2 = time * time * 0.5;
	double tx = pos.x + time * vel.x;
	if (acc.x != 0.0) {
	  tx = tx + t2 * acc.x;
	}
	double ty = pos.y + time * vel.y;
	if (acc.y != 0.0) {
	  ty = ty + t2 * acc.y;
	}
	double tz = pos.z + time * vel.z;
	if (acc.z != 0.0) {
	  tz = tz + t2 * acc.z;
	}
	inters.v = coords_set(tx, ty, tz);
	Coords tvel = coords_set(vel.x + time * acc.x,
				 vel.y + time * acc.y,
				 vel.z + time * acc.z);
	inters.time = time;
	inters.normal = pol.normal;
	inters.index = indPoly;
	int res=off_pnpoly(pol,inters.v);
	if (res != 0) {
	  inters.edge=(res==-1);
	  MCNUM ndir = scalar_prod(pol.normal.x,pol.normal.y,pol.normal.z,tvel.x,tvel.y,tvel.z);
	  if (ndir<0) {
	    inters.in_out=1;  //the negative dot product means we enter the surface
	  } else {
	    inters.in_out=-1;
	  }
#ifdef OFF_LEGACY
          t[t_size++]=inters;
#else
    /* Check against our 4 existing times, starting from [-FLT_MAX, FLT_MAX, FLT_MAX, FLT_MAX] */
    /* Case 1, negative time? */
    if (t_size < 4) t_size++;
    if (inters.time < 0) {
      if (inters.time > t[0].time) {
        t[0]=inters;
      }
    } else {
      /* Case 2, positive time */
      intersection xtmp;
      if (inters.time < t[3].time) {
      t[3]=inters;
        if (t[3].time < t[2].time) {
    xtmp = t[2];
    t[2] = t[3];
    t[3] = xtmp;
        }
        if (t[2].time < t[1].time) {
    xtmp = t[1];
    t[1] = t[2];
    t[2] = xtmp;
        }
      }
    }
#endif
	}
      }
    }
    i += pol.npol;
    indPoly++;
  } /* while i<faceSize */
  return t_size;
} /* off_clip_3D_mod_grav */

// off_compare *****************************************************************
#pragma acc routine
int off_compare (void const *a, void const *b)
{
   intersection const *pa = a;
   intersection const *pb = b;

   return off_sign(pa->time - pb->time);
} /* off_compare */

// off_cleanDouble *************************************************************
//given an array of intersections throw those which appear several times
//returns 1 if there is a possibility of error
#pragma acc routine
int off_cleanDouble(intersection* t, int* t_size)
{
  int i=1;
  intersection prev=t[0];
  while (i<*t_size)
  {
    int j=i;
    //for each intersection with the same time
    while (j<*t_size && fabs(prev.time-t[j].time)<OFF_EPSILON)
    {
      //if the intersection is the exact same erase it
      if (prev.in_out==t[j].in_out)
      {
        int k;
        for (k=j+1; k<*t_size; ++k)
        {
          t[k-1]=t[k];
        }
        *t_size-=1;
      }
      else
        ++j;
    }
    prev=t[i];
    ++i;

  }
  return 1;
} /* off_cleanDouble */

// off_cleanInOut **************************************************************
//given an array of intesections throw those which enter and exit in the same time
//Meaning the ray passes very close to the volume
//returns 1 if there is a possibility of error
#pragma acc routine
int off_cleanInOut(intersection* t, int* t_size)
{
  int i=1;
  intersection prev=t[0];
  while (i<*t_size)
  {
    //if two intersection have the same time but one enters and the other exits erase both
    //(such intersections must be adjacent in the array : run off_cleanDouble before)
    if (fabs(prev.time-t[i].time)<OFF_EPSILON && prev.in_out!=t[i].in_out)
    {
      int j=0;
      for (j=i+1; j<*t_size; ++j)
      {
        t[j-2]=t[j];
      }
      *t_size-=2;
      prev=t[i-1];
    }
    else
    {
      prev=t[i];
      ++i;
    }
  }
  return (*t_size);
} /* off_cleanInOut */

/* PUBLIC functions ******************************************************** */

/*******************************************************************************
* long off_init(  char *offfile, double xwidth, double yheight, double zdepth, off_struct* data)
* ACTION: read an OFF file, optionally center object and rescale, initialize OFF data structure
* INPUT: 'offfile' OFF file to read
*        'xwidth,yheight,zdepth' if given as non-zero, apply bounding box.
*           Specifying only one of these will also use the same ratio on all axes
*        'notcenter' center the object to the (0,0,0) position in local frame when set to zero
* RETURN: number of polyhedra and 'data' OFF structure
*******************************************************************************/
long off_init(  char *offfile, double xwidth, double yheight, double zdepth,
                int notcenter, off_struct* data)
{
  // data to be initialized
  long    vtxSize =0, polySize=0, i=0, ret=0, faceSize=0;
  Coords* vtxArray        =NULL;
  Coords* normalArray     =NULL;
  double* DArray          =NULL;
  unsigned long* faceArray=NULL;
  FILE*   f               =NULL; /* the FILE with vertices and polygons */
  double minx=FLT_MAX,maxx=-FLT_MAX,miny=FLT_MAX,maxy=-FLT_MAX,minz=FLT_MAX,maxz=-FLT_MAX;

  // get the indexes
  if (!data) return(0);

  MPI_MASTER(
  printf("Loading geometry file (OFF/PLY): %s\n", offfile);
  );

  f=off_getBlocksIndex(offfile,&vtxSize,&polySize);
  if (!f) return(0);

  // read vertex table = [x y z | x y z | ...] =================================
  // now we read the vertices as 'vtxSize*3' numbers and store it in vtxArray
  MPI_MASTER(
  printf("  Number of vertices: %ld\n", vtxSize);
  );
  vtxArray   = malloc(vtxSize*sizeof(Coords));
  if (!vtxArray) return(0);
  i=0;
  while (i<vtxSize && ~feof(f))
  {
    double x,y,z;
    ret=fscanf(f, "%lg%lg%lg", &x,&y,&z);
    if (!ret) {
      // invalid line: we skip it (probably a comment)
      char line[CHAR_BUF_LENGTH];
      char *s=fgets(line, CHAR_BUF_LENGTH, f);
      continue;
    }
    if (ret != 3) {
      fprintf(stderr, "Error: can not read [xyz] coordinates for vertex %li in file %s (interoff/off_init). Read %li values.\n",
        i, offfile, ret);
      exit(2);
    }
    vtxArray[i].x=x;
    vtxArray[i].y=y;
    vtxArray[i].z=z;

    //bounding box
    if (vtxArray[i].x<minx) minx=vtxArray[i].x;
    if (vtxArray[i].x>maxx) maxx=vtxArray[i].x;
    if (vtxArray[i].y<miny) miny=vtxArray[i].y;
    if (vtxArray[i].y>maxy) maxy=vtxArray[i].y;
    if (vtxArray[i].z<minz) minz=vtxArray[i].z;
    if (vtxArray[i].z>maxz) maxz=vtxArray[i].z;
    i++; // inquire next vertex
  }

  // resizing and repositioning params
  double centerx=0, centery=0, centerz=0;
  if (!notcenter) {
    centerx=(minx+maxx)*0.5;
    centery=(miny+maxy)*0.5;
    centerz=(minz+maxz)*0.5;
  }

  double rangex=-minx+maxx,
         rangey=-miny+maxy,
         rangez=-minz+maxz;

  double ratiox=1,ratioy=1,ratioz=1;

  if (xwidth && rangex)
  {
    ratiox=xwidth/rangex;
    ratioy=ratiox;
    ratioz=ratiox;
  }

  if (yheight && rangey)
  {
    ratioy=yheight/rangey;
    if(!xwidth)  ratiox=ratioy;
    ratioz=ratioy;
  }

  if (zdepth && rangez)
  {
    ratioz=zdepth/rangez;
    if(!xwidth)  ratiox=ratioz;
    if(!yheight) ratioy=ratioz;
  }

  rangex *= ratiox;
  rangey *= ratioy;
  rangez *= ratioz;

  //center and resize the object
  for (i=0; i<vtxSize; ++i)
  {
    vtxArray[i].x=(vtxArray[i].x-centerx)*ratiox+(!notcenter ? 0 : centerx);
    vtxArray[i].y=(vtxArray[i].y-centery)*ratioy+(!notcenter ? 0 : centery);
    vtxArray[i].z=(vtxArray[i].z-centerz)*ratioz+(!notcenter ? 0 : centerz);
  }

  // read face table = [nbvertex v1 v2 vn | nbvertex v1 v2 vn ...] =============
  MPI_MASTER(
  printf("  Number of polygons: %ld\n", polySize);
  );
  normalArray= malloc(polySize*sizeof(Coords));
  faceArray  = malloc(polySize*10*sizeof(unsigned long)); // we assume polygons have less than 9 vertices
  DArray     = malloc(polySize*sizeof(double));
  if (!normalArray || !faceArray || !DArray) return(0);

  // fill faces
  faceSize=0;
  i=0;
  while (i<polySize && ~feof(f)) {
    int  nbVertex=0, j=0;
    // read the length of this polygon
    ret=fscanf(f, "%d", &nbVertex);
    if (!ret) {
      // invalid line: we skip it (probably a comment)
      char line[CHAR_BUF_LENGTH];
      char *s=fgets(line, CHAR_BUF_LENGTH, f);
      continue;
    }
    if (ret != 1) {
      fprintf(stderr, "Error: can not read polygon %li length in file %s (interoff/off_init)\n",
        i, offfile);
      exit(3);
    }
    if (faceSize > polySize*10) {
      fprintf(stderr, "Error: %li exceeded allocated polygon array[%li] in file %s (interoff/off_init)\n",
        faceSize, polySize*10, offfile);
    }
    faceArray[faceSize++] = nbVertex; // length of the polygon/face
    // then read the vertex ID's
    for (j=0; j<nbVertex; j++) {
      double vtx=0;
      ret=fscanf(f, "%lg", &vtx);
      faceArray[faceSize++] = vtx;   // add vertices index after length of polygon
    }
    i++;
  }

  // precomputes normals
  long indNormal=0;//index in polyArray
  i=0;    //index in faceArray
  while (i<faceSize)
  {
    int    nbVertex=faceArray[i];//nb of vertices of this polygon
    double *vertices=malloc(3*nbVertex*sizeof(double));
    if (!vertices) {
      fprintf(stderr,"Error allocating vertex array sized %i\n",nbVertex);
      exit(-1);
    }
    int j;

    for (j=0; j<nbVertex; ++j)
    {
      unsigned long indVertPj=faceArray[i+j+1];
      vertices[3*j]  =vtxArray[indVertPj].x;
      vertices[3*j+1]=vtxArray[indVertPj].y;
      vertices[3*j+2]=vtxArray[indVertPj].z;
    }

    polygon p;
    p.p   =vertices;
    p.npol=nbVertex;
    p.D=1;
    off_normal(&(p.normal),p);

    normalArray[indNormal]=p.normal;
    p.D = scalar_prod(p.normal.x,p.normal.y,p.normal.z,
		      vertices[0],vertices[1],vertices[2]);
    DArray[indNormal]=p.D;

    i += nbVertex+1;
    indNormal++;
    free(vertices);
  }

  MPI_MASTER(
  if (ratiox!=ratioy || ratiox!=ratioz || ratioy!=ratioz)
    printf("Warning: Aspect ratio of the geometry %s was modified.\n"
           "         If you want to keep the original proportions, specifiy only one of the dimensions.\n",
           offfile);
  if ( xwidth==0 && yheight==0 && zdepth==0 ) {
    printf("Warning: Neither xwidth, yheight or zdepth are defined.\n"
	   "           The file-defined (non-scaled) geometry the OFF geometry %s will be applied!\n",
           offfile);
  }
  printf("  Bounding box dimensions for geometry %s:\n", offfile);
  printf("    Length=%f (%.3f%%)\n", rangex, ratiox*100);
  printf("    Width= %f (%.3f%%)\n", rangey, ratioy*100);
  printf("    Depth= %f (%.3f%%)\n", rangez, ratioz*100);
  );

  data->vtxArray   = vtxArray;
  data->normalArray= normalArray;
  data->DArray     = DArray;
  data->faceArray  = faceArray;
  data->vtxSize    = vtxSize;
  data->polySize   = polySize;
  data->faceSize   = faceSize;
  data->filename   = offfile;
  #ifdef OPENACC
  acc_attach((void *)&vtxArray);
  acc_attach((void *)&normalArray);
  acc_attach((void *)&faceArray);
  #endif

  return(polySize);
} /* off_init */

#pragma acc routine
int Min_int(int x, int y) {
  return (x<y)? x :y;
}

 
#pragma acc routine
void merge(intersection *arr, int l, int m, int r)
{
int i, j, k;
int n1 = m - l + 1;
int n2 =  r - m;

/* create temp arrays */
intersection *L, *R;
 L = (intersection *)malloc(sizeof(intersection) * n1);
 R = (intersection *)malloc(sizeof(intersection) * n2);
 if (!L||!R) {
   fprintf(stderr,"Error allocating intersection arrays\n");
   exit(-1);
 }
/* Copy data to temp arrays L[] and R[] */
 #pragma acc loop independent
for (i = 0; i < n1; i++)
    L[i] = arr[l + i];
 #pragma acc loop independent
for (j = 0; j < n2; j++)
    R[j] = arr[m + 1+ j];

/* Merge the temp arrays back into arr[l..r]*/
i = 0;
j = 0;
k = l;

while (i < n1 && j < n2)
{
    if (L[i].time <= R[j].time)
    {
        arr[k] = L[i];
        i++;
    }
    else
    {
        arr[k] = R[j];
        j++;
    }
    k++;
}

/* Copy the remaining elements of L[], if there are any */

while (i < n1)
{
    arr[k] = L[i];
    i++;
    k++;
}

/* Copy the remaining elements of R[], if there are any */
while (j < n2)
{
    arr[k] = R[j];
    j++;
    k++;
}
free(L);
free(R);
}


#ifdef USE_OFF
#pragma acc routine
void gpusort(intersection *arr, int size)
{
  int curr_size;  // For current size of subarrays to be merged
  // curr_size varies from 1 to n/2
  int left_start; // For picking starting index of left subarray
  // to be merged
  // pcopying (R[0:n2])
  {
    for (curr_size=1; curr_size<=size-1; curr_size = 2*curr_size)
      {
	// Pick starting point of different subarrays of current size
	for (left_start=0; left_start<size-1; left_start += 2*curr_size)
	  {
	    // Find ending point of left subarray. mid+1 is starting
	    // point of right
	    int mid = left_start + curr_size - 1;

	    int right_end = Min_int(left_start + 2*curr_size - 1, size-1);

	    // Merge Subarrays arr[left_start...mid] & arr[mid+1...right_end]
	    if (mid < right_end) merge(arr, left_start, mid, right_end);
	  }
      }
  }
}
#endif

/*******************************************************************************
void p_to_quadratic(double eq[], Coords acc,
                    Coords pos, Coords vel,
                    double* teq)
* ACTION: define the quadratic for the intersection of a parabola with a plane
* INPUT: 'eq' plane equation
*        'acc' acceleration vector
*        'vel' velocity of the particle
*        'pos' position of the particle
*         equation of plane A * x + B * y + C * z - D = 0
*         eq[0] = (C*az)/2+(B*ay)/2+(A*ax)/2
*         eq[1] = C*vz+B*vy+A*vx
*         eq[2] = C*z0+B*y0+A*x0-D
* RETURN: equation of parabola: teq(0) * t^2 + teq(1) * t + teq(2)
*******************************************************************************/
void p_to_quadratic(Coords norm, MCNUM d, Coords acc, Coords pos, Coords vel,
		    double* teq)
{
  teq[0] = scalar_prod(norm.x, norm.y, norm.z, acc.x, acc.y, acc.z) * 0.5;
  teq[1] = scalar_prod(norm.x, norm.y, norm.z, vel.x, vel.y, vel.z);
  teq[2] = scalar_prod(norm.x, norm.y, norm.z, pos.x, pos.y, pos.z) - d;
  return;
}

/*******************************************************************************
int quadraticSolve(double eq[], double* x1, double* x2);
* ACTION: solves the quadratic for the roots x1 and x2 
*         eq[0] * t^2 + eq[1] * t + eq[2] = 0
* INPUT: 'eq' the coefficients of the parabola
* RETURN: roots x1 and x2 and the number of solutions
*******************************************************************************/
int quadraticSolve(double* eq, double* x1, double* x2)
{
  if (eq[0] == 0.0) { // This is a linear equation
    if (eq[1] != 0.0) { // one solution
      *x1 = -eq[2]/eq[1];
      *x2 = 1.0e36;
      return 1;
    }else { // no solutions, 1.0e36 will be ignored.
      *x1 = 1.0e36;
      *x2 = 1.0e36;
      return 0;
    }
  }
  double delta = eq[1]*eq[1]-4.0*eq[0]*eq[2];
  if (delta < 0.0) { // no solutions, both are imaginary
    *x1 = 1.0e36;
    *x2 = 1.0e36;
    return 0;
  }
  double s = 1.0;
  if (eq[1] < 0) {
    s = -1.0;
  }
  *x1 = (-eq[1] - s * sqrt(delta))/(2.0*eq[0]);
  if (eq[0] != 0.0) { //two solutions
    *x2 = eq[2]/(eq[0]*(*x1));
    return 2;
  } else { //one solution
    *x2 = 1.0e36;
    return 1;
  }
}

/*******************************************************************************
* int off_intersect_all(double* t0, double* t3,
     Coords *n0, Coords *n3,
     double x, double y, double z,
     double vx, double vy, double vz,
     double ax, double ay, double az,
     off_struct *data )
* ACTION: computes intersection of neutron trajectory with an object.
* INPUT:  x,y,z and vx,vy,vz are the position and velocity of the neutron
*         ax, ay, az are the local acceleration vector
*         data points to the OFF data structure
* RETURN: the number of polyhedral which trajectory intersects
*         t0 and t3 are the smallest incoming and outgoing intersection times
*         n0 and n3 are the corresponding normal vectors to the surface
*         data is the full OFF structure, including a list intersection type
*******************************************************************************/
int off_intersect_all(double* t0, double* t3,
     Coords *n0, Coords *n3,
     double x,  double y,  double z,
     double vx, double vy, double vz,
     double ax, double ay, double az,
     off_struct *data )
{

    int t_size = 0;
#ifdef OFF_LEGACY

    if(mcgravitation) {
      Coords pos={ x,  y,  z};
      Coords vel={vx, vy, vz};
      Coords acc={ax, ay, az};
      t_size=off_clip_3D_mod_grav(data->intersects, pos, vel, acc,
				  data->vtxArray, data->vtxSize, data->faceArray,
				  data->faceSize, data->normalArray, data->DArray );
    } else {
    ///////////////////////////////////
    // non-grav
      Coords A={x, y, z};
      Coords B={x+vx, y+vy, z+vz};
      t_size=off_clip_3D_mod(data->intersects, A, B,
			     data->vtxArray, data->vtxSize, data->faceArray,
			     data->faceSize, data->normalArray );
    }
    #ifndef OPENACC
    qsort(data->intersects, t_size, sizeof(intersection),  off_compare);
    #else
    #ifdef USE_OFF
    gpusort(data->intersects, t_size);
    #endif
    #endif
    off_cleanDouble(data->intersects, &t_size);
    off_cleanInOut(data->intersects,  &t_size);

    /*find intersections "closest" to 0 (favouring positive ones)*/
    if(t_size>0){
      int i=0;
      if(t_size>1) {
        for (i=1; i < t_size-1; i++){
          if (data->intersects[i-1].time > 0 && data->intersects[i].time > 0)
            break;
        }

	data->nextintersect=i-1;
	data->numintersect=t_size;

        if (t0) *t0 = data->intersects[i-1].time;
        if (n0) *n0 = data->intersects[i-1].normal;
        if (t3) *t3 = data->intersects[i].time;
        if (n3) *n3 = data->intersects[i].normal;
      } else {
        if (t0) *t0 = data->intersects[0].time;
	      if (n0) *n0 = data->intersects[0].normal;
      }
      /* should also return t[0].index and t[i].index as polygon ID */
      data->nextintersect=(data->intersects[data->nextintersect]).index;
      return t_size;
    }
#else
    intersection intersect4[4];
    intersect4[0].time=-FLT_MAX;
    intersect4[1].time=FLT_MAX;
    intersect4[2].time=FLT_MAX;
    intersect4[3].time=FLT_MAX;
    if(mcgravitation) {
      Coords pos={ x,  y,  z};
      Coords vel={vx, vy, vz};
      Coords acc={ax, ay, az};
      t_size=off_clip_3D_mod_grav(intersect4, pos, vel, acc,
				  data->vtxArray, data->vtxSize, data->faceArray,
				  data->faceSize, data->normalArray, data->DArray);
    } else {
    ///////////////////////////////////
    // non-grav
      Coords A={x, y, z};
      Coords B={x+vx, y+vy, z+vz};
      t_size=off_clip_3D_mod(intersect4, A, B,
	  data->vtxArray, data->vtxSize, data->faceArray, data->faceSize, data->normalArray );
    }
    if(t_size>0){
      int i=0;
      if (intersect4[0].time == -FLT_MAX) i=1;
      data->numintersect=t_size;
      if (t0) *t0 = intersect4[i].time;
      if (n0) *n0 = intersect4[i].normal;
      if (t3) *t3 = intersect4[i+1].time;
      if (n3) *n3 = intersect4[i+1].normal;

      if (intersect4[1].time == FLT_MAX)
      {
        if (t3) *t3 = 0.0;
      }

      /* should also return t[0].index and t[i].index as polygon ID */
      data->nextintersect=(int)intersect4[i].index;
      return t_size;
    }
#endif
    return 0;
} /* off_intersect */

/*******************************************************************************
* int off_intersect(double* t0, double* t3,
     Coords *n0, Coords *n3,
     double x, double y, double z,
     double vx, double vy, double vz,
     off_struct data )
* ACTION: computes intersection of neutron trajectory with an object.
* INPUT:  x,y,z and vx,vy,vz are the position and velocity of the neutron
*         data points to the OFF data structure
* RETURN: the number of polyhedral which trajectory intersects
*         t0 and t3 are the smallest incoming and outgoing intersection times
*         n0 and n3 are the corresponding normal vectors to the surface
*******************************************************************************/
int off_intersect(double* t0, double* t3,
     Coords *n0, Coords *n3,
     double x,  double y,  double z,
     double vx, double vy, double vz,
     double ax, double ay, double az,
     off_struct data )
{
  return off_intersect_all(t0, t3, n0, n3, x, y, z, vx, vy, vz, ax, ay, az, &data );
} /* off_intersect */

/*****************************************************************************
* int off_x_intersect(double* l0, double* l3,
     Coords *n0, Coords *n3,
     double x, double y, double z,
     double kx, double ky, double kz,
     off_struct data )
* ACTION: computes intersection of an xray trajectory with an object.
* INPUT:  x,y,z and kx,ky,kz, are spatial coordinates and wavevector of the x-ray
*         respectively. data points to the OFF data structure.
* RETURN: the number of polyhedral the trajectory intersects
*         l0 and l3 are the smallest incoming and outgoing intersection lengths
*         n0 and n3 are the corresponding normal vectors to the surface
*******************************************************************************/
int off_x_intersect(double *l0,double *l3,
     Coords *n0, Coords *n3,
     double x,  double y,  double z,
     double kx, double ky, double kz,
     off_struct data )
{
  /*This function simply reformats and calls off_intersect (as for neutrons)
   *by normalizing the wavevector - this will yield the intersection lengths
   *in m*/
  double jx,jy,jz,invk;
  int n;
  invk=1/sqrt(scalar_prod(kx,ky,kz,kx,ky,kz));
  jx=kx*invk;jy=ky*invk;jz=kz*invk;
  n=off_intersect(l0,l3,n0,n3,x,y,z,jx,jy,jz,0.0,0.0,0.0,data);
  return n;
}


/*******************************************************************************
* void off_display(off_struct data)
* ACTION: display up to N_VERTEX_DISPLAYED polygons from the object
*******************************************************************************/
void off_display(off_struct data)
{
    if(mcdotrace==2){
    // Estimate size of the JSON string
    const int VERTEX_OVERHEAD = 30;
    const int FACE_OVERHEAD_BASE = 20;
    const int FACE_INDEX_OVERHEAD = 15;
    int estimated_size = 256; // Base size
    estimated_size += data.vtxSize * VERTEX_OVERHEAD;

    for (int i = 0; i < data.faceSize;) {
        int num_indices = data.faceArray[i];
        estimated_size += FACE_OVERHEAD_BASE + num_indices * FACE_INDEX_OVERHEAD;
        i += num_indices + 1;
    }

    char *json_string = malloc(estimated_size);
    if (json_string == NULL) {
        fprintf(stderr, "Memory allocation failed.\n");
        return;
    }

    char *ptr = json_string;
    ptr += sprintf(ptr, "{ \"vertices\": [");

    for (int i = 0; i < data.vtxSize; i++) {
        ptr += sprintf(ptr, "[%g, %g, %g]", data.vtxArray[i].x, data.vtxArray[i].y, data.vtxArray[i].z);
        if (i < data.vtxSize - 1) {
            ptr += sprintf(ptr, ", ");
        }
    }

    ptr += sprintf(ptr, "], \"faces\": [");

    for (int i = 0; i < data.faceSize;) {
        int num = data.faceArray[i];
        ptr += sprintf(ptr, "{ \"face\": [");
        for (int j = 1; j <= num; j++) {
            ptr += sprintf(ptr, "%lu", data.faceArray[i + j]);
            if (j < num) {
                ptr += sprintf(ptr, ", ");
            }
        }
        ptr += sprintf(ptr, "]}");
        i += num + 1;
        if(i<data.faceSize){
          ptr += sprintf(ptr, ", ");
        }
    }

    ptr += sprintf(ptr, "]}");
    mcdis_polyhedron(json_string);

    free(json_string);
    }
    else {
      unsigned int i;
      double ratio=(double)(N_VERTEX_DISPLAYED)/(double)data.faceSize;
      unsigned int pixel=0;
      for (i=0; i<data.faceSize-1; i++) {
        int j;
        int nbVertex = data.faceArray[i];
        double x0,y0,z0;
        x0 = data.vtxArray[data.faceArray[i+1]].x;
        y0 = data.vtxArray[data.faceArray[i+1]].y;
        z0 = data.vtxArray[data.faceArray[i+1]].z;
        double x1=x0,y1=y0,z1=z0;
        double cmx=0,cmy=0,cmz=0;

        int drawthis = rand01() < ratio;
        // First pass, calculate center of mass location...
        for (j=1; j<=nbVertex; j++) {
          cmx = cmx+data.vtxArray[data.faceArray[i+j]].x;
          cmy = cmy+data.vtxArray[data.faceArray[i+j]].y;
          cmz = cmz+data.vtxArray[data.faceArray[i+j]].z;
        }
        cmx /= nbVertex;
        cmy /= nbVertex;
        cmz /= nbVertex;

        char pixelinfo[1024];
	char pixelinfotmp[1024];
        sprintf(pixelinfo, "%li,%li,%li,%i,%g,%g,%g,%g,%g,%g", data.mantidoffset+pixel, data.mantidoffset, data.mantidoffset+data.polySize-1, nbVertex, cmx, cmy, cmz, x1-cmx, y1-cmy, z1-cmz);
        for (j=2; j<=nbVertex; j++) {
          double x2,y2,z2;
          x2 = data.vtxArray[data.faceArray[i+j]].x;
          y2 = data.vtxArray[data.faceArray[i+j]].y;
          z2 = data.vtxArray[data.faceArray[i+j]].z;
          sprintf(pixelinfotmp, "%s,%g,%g,%g", pixelinfo, x2-cmx, y2-cmy, z2-cmz);
	  sprintf(pixelinfo,"%s",pixelinfotmp);
          if (ratio > 1 || drawthis) {
	    mcdis_line(x1,y1,z1,x2,y2,z2);
          }
          x1 = x2; y1 = y2; z1 = z2;
        }
        if (ratio > 1 || drawthis) {
	    mcdis_line(x1,y1,z1,x0,y0,z0);
          }
        if (data.mantidflag) {
          printf("MANTID_PIXEL: %s\n", pixelinfo);
          pixel++;
        }
        i += nbVertex;
      }
    }
} /* off_display */

/* end of interoff-lib.c */
#endif // INTEROFF_LIB_C

  /* Declare structures and functions only once in each instrument. */
  #ifndef POWDERN_DECL_UNION
  #define POWDERN_DECL_UNION
  /* format definitions in the order {j d F2 DW Dd inv2d q F strain} */
  #ifndef Crystallographica
  #define Crystallographica { 4,5,7,0,0,0,0,0,0 }
  #define Fullprof          { 4,0,8,0,0,5,0,0,0 }
  #define Lazy              {17,6,0,0,0,0,0,13,0 }
  #define Undefined         { 0,0,0,0,0,0,0,0,0 }
  #endif

  struct line_data_union {
    double F2;       /* Value of structure factor */
    double q;        /* Qvector */
    int j;           /* Multiplicity */
    double DWfactor; /* Debye-Waller factor */
    double w;        /* Intrinsic line width */
    double Epsilon;  /* Strain=delta_d_d/d shift in ppm */
  };

  struct line_info_struct_union {
    struct line_data_union* list; /* Reflection array */
    int count;                    /* Number of reflections */
    double Dd;
    double DWfactor;
    double V_0;
    double rho;
    double at_weight;
    double at_nb;
    double sigma_a; // should not be used
    double sigma_i; // should not be used
    char compname[256];
    double flag_barns;
    int shape;           /* 0 cylinder, 1 box, 2 sphere, 3 OFF file */
    int column_order[9]; /* column signification */
    int flag_warning;
    char type;      /* interaction type of event t=Transmit, i=Incoherent, c=Coherent */
    double dq;      /* wavevector transfer [Angs-1] */
    double Epsilon; /* global strain in ppm */
    double XsectionFactor;
    double my_s_v2_sum;
    double my_a_v;
    double my_inc;
    double *w_v, *q_v, *my_s_v2;
    double radius_i, xwidth_i, yheight_i, zdepth_i; // not to be used, but still here
    double v;                                       /* last velocity (cached) */
    double Nq;
    int nb_reuses, nb_refl, nb_refl_count;
    double v_min, v_max;
    double xs_Nq[CHAR_BUF_LENGTH];
    double xs_sum[CHAR_BUF_LENGTH];
    double neutron_passed;
    long xs_compute, xs_reuse, xs_calls;
  };

  off_struct offdata_union;

  // PN_list_compare *****************************************************************

  int
  PN_list_compare_union (const void* a, const void* b) {
    const struct line_data_union* pa = a;
    const struct line_data_union* pb = b;

    /* Sort by q */
    if (pa->q < pb->q)
      return -1;
    if (pa->q > pb->q)
      return 1;

    /* In case of tie, sort by F2 also */
    if (pa->F2 < pb->F2)
      return -1;
    if (pa->F2 > pb->F2)
      return 1;

    /* In case of tie, sort by j also */
    if (pa->j < pb->j)
      return -1;
    if (pa->j > pb->j)
      return 1;

    return 0;
  } /* PN_list_compare */

  int
  read_line_data_union (char* SC_file, struct line_info_struct_union* info) {
    struct line_data_union* list = NULL;
    int size = 0;
    t_Table sTable; /* sample data table structure from SC_file */
    int i = 0;
    int mult_count = 0;
    char flag = 0;
    double q_count = 0, j_count = 0, F2_count = 0;
    char** parsing;
    int list_count = 0;

    if (!SC_file || !strlen (SC_file) || !strcmp (SC_file, "NULL")) {
      MPI_MASTER (printf ("PowderN: %s: Using incoherent elastic scattering only\n", info->compname););
      info->count = 0;
      return (0);
    }
    Table_Read (&sTable, SC_file, 1); /* read 1st block data from SC_file into sTable*/

    /* parsing of header */
    parsing = Table_ParseHeader (sTable.header, "Vc", "V_0", "sigma_abs", "sigma_a ", "sigma_inc", "sigma_i ", "column_j", "column_d", "column_F2", "column_DW",
                                 "column_Dd", "column_inv2d", "column_1/2d", "column_sintheta/lambda", "column_q",                           /* 14 */
                                 "DW", "Debye_Waller", "delta_d_d/d", "column_F ", "V_rho", "density", "weight", "nb_atoms", "multiplicity", /* 23 */
                                 "column_ppm", "column_strain", NULL);

    if (parsing) {
      if (parsing[0] && !info->V_0)
        info->V_0 = atof (parsing[0]);
      if (parsing[1] && !info->V_0)
        info->V_0 = atof (parsing[1]);
      if (parsing[2] && !info->sigma_a)
        info->sigma_a = atof (parsing[2]);
      if (parsing[3] && !info->sigma_a)
        info->sigma_a = atof (parsing[3]);
      if (parsing[4] && !info->sigma_i)
        info->sigma_i = atof (parsing[4]);
      if (parsing[5] && !info->sigma_i)
        info->sigma_i = atof (parsing[5]);
      if (parsing[6])
        info->column_order[0] = atoi (parsing[6]);
      if (parsing[7])
        info->column_order[1] = atoi (parsing[7]);
      if (parsing[8])
        info->column_order[2] = atoi (parsing[8]);
      if (parsing[9])
        info->column_order[3] = atoi (parsing[9]);
      if (parsing[10])
        info->column_order[4] = atoi (parsing[10]);
      if (parsing[11])
        info->column_order[5] = atoi (parsing[11]);
      if (parsing[12])
        info->column_order[5] = atoi (parsing[12]);
      if (parsing[13])
        info->column_order[5] = atoi (parsing[13]);
      if (parsing[14])
        info->column_order[6] = atoi (parsing[14]);
      if (parsing[15] && info->DWfactor <= 0)
        info->DWfactor = atof (parsing[15]);
      if (parsing[16] && info->DWfactor <= 0)
        info->DWfactor = atof (parsing[16]);
      if (parsing[17] && info->Dd < 0)
        info->Dd = atof (parsing[17]);
      if (parsing[18])
        info->column_order[7] = atoi (parsing[18]);
      if (parsing[19] && !info->V_0)
        info->V_0 = 1 / atof (parsing[19]);
      if (parsing[20] && !info->rho)
        info->rho = atof (parsing[20]);
      if (parsing[21] && !info->at_weight)
        info->at_weight = atof (parsing[21]);
      if (parsing[22] && info->at_nb <= 1)
        info->at_nb = atof (parsing[22]);
      if (parsing[23] && info->at_nb <= 1)
        info->at_nb = atof (parsing[23]);
      if (parsing[24])
        info->column_order[8] = atoi (parsing[24]);
      if (parsing[25])
        info->column_order[8] = atoi (parsing[25]);
      for (i = 0; i <= 25; i++)
        if (parsing[i])
          free (parsing[i]);
      free (parsing);
    }

    if (!sTable.rows)
      exit (fprintf (stderr,
                     "PowderN: %s: Error: The number of rows in %s "
                     "should be at least %d\n",
                     info->compname, SC_file, 1));
    else
      size = sTable.rows;
    Table_Info (sTable);
    MPI_MASTER (printf ("PowderN: %s: Reading %d rows from %s\n", info->compname, size, SC_file); if (info->column_order[0] == 4 && info->flag_barns != 0)
                    printf ("PowderN: %s: Powder file probably of type Crystallographica/Fullprof (lau)\n"
                            "WARNING: but F2 unit is set to barns=1 (barns). Intensity might be 100 times too high.\n",
                            info->compname);
                if (info->column_order[0] == 17 && info->flag_barns == 0)
                    printf ("PowderN: %s: Powder file probably of type Lazy Pulver (laz)\n"
                            "WARNING: but F2 unit is set to barns=0 (fm^2). Intensity might be 100 times too low.\n",
                            info->compname););
    /* allocate line_data array */
    list = (struct line_data_union*)malloc (size * sizeof (struct line_data_union));

    for (i = 0; i < size; i++) {
      /*      printf("Reading in line %i\n",i);*/
      double j = 0, d = 0, w = 0, q = 0, DWfactor = 0, F2 = 0, Epsilon = 0;
      int index;

      if (info->Dd >= 0)
        w = info->Dd;
      if (info->DWfactor > 0)
        DWfactor = info->DWfactor;
      if (info->Epsilon)
        Epsilon = info->Epsilon * 1e-6;

      /* get data from table using columns {j d F2 DW Dd inv2d q F} */
      /* column indexes start at 1, thus need to substract 1 */
      if (info->column_order[0] > 0)
        j = Table_Index (sTable, i, info->column_order[0] - 1);
      if (info->column_order[1] > 0)
        d = Table_Index (sTable, i, info->column_order[1] - 1);
      if (info->column_order[2] > 0)
        F2 = Table_Index (sTable, i, info->column_order[2] - 1);
      if (info->column_order[3] > 0)
        DWfactor = Table_Index (sTable, i, info->column_order[3] - 1);
      if (info->column_order[4] > 0)
        w = Table_Index (sTable, i, info->column_order[4] - 1);
      if (info->column_order[5] > 0) {
        d = Table_Index (sTable, i, info->column_order[5] - 1);
        d = (d > 0 ? 1 / d / 2 : 0);
      }
      if (info->column_order[6] > 0) {
        q = Table_Index (sTable, i, info->column_order[6] - 1);
        d = (q > 0 ? 2 * PI / q : 0);
      }
      if (info->column_order[7] > 0 && !F2) {
        F2 = Table_Index (sTable, i, info->column_order[7] - 1);
        F2 *= F2;
      }
      if (info->column_order[8] > 0 && !Epsilon) {
        Epsilon = Table_Index (sTable, i, info->column_order[8] - 1) * 1e-6;
      }

      /* assign and check values */
      j = (j > 0 ? j : 0);
      q = (d > 0 ? 2 * PI / d : 0); /* this is q */
      if (Epsilon && fabs (Epsilon) < 1e6) {
        q -= Epsilon * q; /* dq/q = -delta_d_d/d = -Epsilon */
      }
      DWfactor = (DWfactor > 0 ? DWfactor : 1);
      w = (w > 0 ? w : 0); /* this is q and d relative spreading */
      F2 = (F2 >= 0 ? F2 : 0);
      if (j == 0 || q == 0) {
        MPI_MASTER (printf ("PowderN: %s: line %i has invalid definition\n"
                            "         (mult=0 or q=0 or d=0)\n",
                            info->compname, i););
        continue;
      }
      list[list_count].j = j;
      list[list_count].q = q;
      list[list_count].DWfactor = DWfactor;
      list[list_count].w = w;
      list[list_count].F2 = F2;
      list[list_count].Epsilon = Epsilon;

      /* adjust multiplicity if j-column + multiple d-spacing lines */
      /* if  d = previous d, increase line duplication index */
      if (!q_count)
        q_count = q;
      if (!j_count)
        j_count = j;
      if (!F2_count)
        F2_count = F2;
      if (fabs (q_count - q) < 0.0001 * fabs (q) && fabs (F2_count - F2) < 0.0001 * fabs (F2) && j_count == j) {
        mult_count++;
        flag = 0;
      } else
        flag = 1;
      if (i == size - 1)
        flag = 1;
      /* else if d != previous d : just passed equivalent lines */
      if (flag) {
        if (i == size - 1)
          list_count++;
        /*   if duplication index == previous multiplicity */
        /*      set back multiplicity of previous lines to 1 */
        if ((mult_count && list_count > 0)
            && (mult_count == list[list_count - 1].j || ((list_count < size) && (i == size - 1) && (mult_count == list[list_count].j)))) {
          MPI_MASTER (printf ("PowderN: %s: Set multiplicity to 1 for lines [%i:%i]\n"
                              "         (d-spacing %g is duplicated %i times)\n",
                              info->compname, list_count - mult_count, list_count - 1, list[list_count - 1].q, mult_count););
          for (index = list_count - mult_count; index < list_count; list[index++].j = 1)
            ;
          mult_count = 1;
          q_count = q;
          j_count = j;
          F2_count = F2;
        }
        if (i == size - 1)
          list_count--;
        flag = 0;
      }
      list_count++;
    } /* end for */

    Table_Free (&sTable);

    /* sort the list with increasing q */
    qsort (list, list_count, sizeof (struct line_data_union), PN_list_compare_union);

    MPI_MASTER (printf ("PowderN: %s: Read %i reflections from file '%s'\n", info->compname, list_count, SC_file););
    info->list = list;
    info->count = list_count;

    return (list_count);
  } /* read_line_data_union */

  /* computes the number of possible reflections (return value), and the total xsection 'sum' */
  /* this routine looks for a pre-computed value in the Nq and sum cache tables               */
  /* when found, the earch starts from the corresponding lower element in the table           */
  int
  calc_xsect_union (double v, double* qv, double* my_sv2, int count, double* sum, struct line_info_struct_union* line_info) {
    int Nq = 0, line = 0, line0 = 0;
    *sum = 0;

    // printf("Line_info when entering cross_section calculation\n");
    // printf("v = %f, qv = %f, my_sv2 = %f, count = %d, sum = %f\n",v,*qv,*my_sv2,count,*sum);
    // printf("v = %f\n",v);
    // printf("line_info->v = %f, line_info->v_min = %f, line_info->v_max = %f, line_info->neutron_passed =
    // %f\n",line_info->v,line_info->v_min,line_info->v_max,line_info->neutron_passed); printf("line_info->xs_reuses = %d, line_info->xs_compute =
    // %d\n",line_info->xs_reuse,line_info->xs_compute);

    /* check if a line_info element has been recorded already */
    if (v >= line_info->v_min && v <= line_info->v_max && line_info->neutron_passed >= CHAR_BUF_LENGTH) {
      line = (int)floor (v - line_info->v_min) * CHAR_BUF_LENGTH / (line_info->v_max - line_info->v_min);
      Nq = line_info->xs_Nq[line];
      *sum = line_info->xs_sum[line];
      if (!Nq && *sum == 0) {
        /* not yet set: we compute the sum up to the corresponding speed in the table cache */
        // printf("Nq and sum not yet set, have to do this calculation now\n");
        double line_v = line_info->v_min + line * (line_info->v_max - line_info->v_min) / CHAR_BUF_LENGTH;
        for (line0 = 0; line0 < count; line0++) {
          if (qv[line0] <= 2 * line_v) { /* q < 2*kf: restrict structural range */
            *sum += my_sv2[line0];
            if (Nq < line0 + 1)
              Nq = line0 + 1; /* determine maximum line index which can scatter */
          } else
            break;
        }
        line_info->xs_Nq[line] = Nq;
        line_info->xs_sum[line] = *sum;
        line_info->xs_compute++;
        // printf("line_info->xs_Nq[line] = %f, line_info->xs_sum[line] = %f, line_info->xs_compute =
        // %d\n",line_info->xs_Nq[line],line_info->xs_sum[line],line_info->xs_compute);
      } else
        line_info->xs_reuse++;
      line0 = Nq;
    }

    line_info->xs_calls++;

    for (line = line0; line < count; line++) {
      if (qv[line] <= 2 * v) { /* q < 2*kf: restrict structural range */
        *sum += my_sv2[line];
        if (Nq < line + 1)
          Nq = line + 1; /* determine maximum line index which can scatter */
      } else
        break;
    }

    // printf("cross_section function to return %d lines to scatter with, with cross section sum %f \n",Nq,*sum);
    return (Nq);
  } /* calc_xsect_union */

  #endif /* !POWDERN_DECL */

  struct Powder_physics_storage_struct {
    // Variables that needs to be transfered between any of the following places:
    // The initialize in this component
    // The function for calculating my
    // The function for calculating scattering

    struct line_info_struct_union* line_info_storage;
    double my_scattering;
    double vertical_angular_limit;
  };

  // Obsolete: Function for initializing test_physics. Done in component instead.
  int
  Powder_physics_initialize (union data_transfer_union data_transfer) {
    // Obsolte
    return 1;
  };

  // Function for calculating my in a test case.
  int
  Powder_physics_my (double* my, double* k_initial, union data_transfer_union data_transfer, struct focus_data_struct* focus_data, _class_particle* _particle) {
    //*my = data_transfer.pointer_to_a_Powder_physics_storage_struct->my_scattering;

    int method_switch = 1;
    // For test
    int line_v, line0, line;

    // Should not interfer with the global variables
    double vx = k_initial[0] * K2V;
    double vy = k_initial[1] * K2V;
    double vz = k_initial[2] * K2V;

    // Not sure one can do this, but I do not see why not
    struct line_info_struct_union* line_info = data_transfer.pointer_to_a_Powder_physics_storage_struct->line_info_storage;

    double v = sqrt (vx * vx + vy * vy + vz * vz);
    // printf("Velocity = %f \n",v);

    // printf("line_info->v = %f, line_info->v_min = %f, line_info->v_max = %f, line_info->neutron_passed =
    // %f\n",line_info->v,line_info->v_min,line_info->v_max,line_info->neutron_passed);
    //  Here the maximum and minimum v is recorded, should this be for scattering events or cross section calculations?
    if (line_info->neutron_passed < CHAR_BUF_LENGTH) {
      if (v < line_info->v_min)
        line_info->v_min = v;
      if (v > line_info->v_max)
        line_info->v_max = v;
      line_info->neutron_passed++;
    }

    if (method_switch == 1) {
      // Here the cross section is calculated and stored
      if (fabs (v - line_info->v) < 1e-6) {
        line_info->nb_reuses++;
      } else {
        // printf("calling crosssection calculation \n");
        //  int calc_xsect_union(double v, double *qv, double *my_sv2, int count, double *sum, struct line_info_struct *line_info)
        line_info->Nq = calc_xsect_union (v, line_info->q_v, line_info->my_s_v2, line_info->count, &line_info->my_s_v2_sum, line_info);
        line_info->v = v;
        line_info->nb_refl += line_info->Nq;
        line_info->nb_refl_count++;
      }
    } else {
      if (fabs (v - line_info->v) < 1e-6) {
        line_info->nb_reuses++;
      } else {
        // printf("calling crosssection calculation \n");
        if (v >= line_info->v_min && v <= line_info->v_max && line_info->neutron_passed >= CHAR_BUF_LENGTH) {
          line = (int)floor (v - line_info->v_min) * CHAR_BUF_LENGTH / (line_info->v_max - line_info->v_min);
          line_info->Nq = line_info->xs_Nq[line];
          line_info->my_s_v2_sum = line_info->xs_sum[line];
          if (!line_info->Nq && line_info->my_s_v2_sum == 0) {
            /* not yet set: we compute the sum up to the corresponding speed in the table cache */
            // printf("Nq and sum not yet set, have to do this calculation now\n");
            double line_v = line_info->v_min + line * (line_info->v_max - line_info->v_min) / CHAR_BUF_LENGTH;
            for (line0 = 0; line0 < line_info->count; line0++) {
              if (line_info->q_v[line0] <= 2 * line_v) { /* q < 2*kf: restrict structural range */
                line_info->my_s_v2_sum += line_info->my_s_v2[line0];
                if (line_info->Nq < line0 + 1)
                  line_info->Nq = line0 + 1; /* determine maximum line index which can scatter */
              } else
                break;
            }
            line_info->xs_Nq[line] = line_info->Nq;
            line_info->xs_sum[line] = line_info->my_s_v2_sum;
            line_info->xs_compute++;
            // printf("line_info->xs_Nq[line] = %f, line_info->xs_sum[line] = %f, line_info->xs_compute =
            // %d\n",line_info->xs_Nq[line],line_info->xs_sum[line],line_info->xs_compute);
          } else
            line_info->xs_reuse++;
          line0 = line_info->Nq;
        }

        line_info->xs_calls++;

        for (line = line0; line < line_info->count; line++) {
          if (line_info->q_v[line] <= 2 * v) { /* q < 2*kf: restrict structural range */
            line_info->my_s_v2_sum += line_info->my_s_v2[line];
            if (line_info->Nq < line + 1)
              line_info->Nq = line + 1; /* determine maximum line index which can scatter */
          } else
            break;
        }
        line_info->v = v;
        line_info->nb_refl += line_info->Nq;
        line_info->nb_refl_count++;
      }
    }

    *my = line_info->my_s_v2_sum / (v * v);
    // printf("Returned my scattering of %f \n",*my);
    // printf("compute = %d and reuse = %d \n",line_info->xs_compute,line_info->xs_reuse);

    return 1;
  };

  // Function that provides a basic nonuniform elastic scattering. Unphysical for testing purposes.
  int
  Powder_physics_scattering (double* k_final, double* k_initial, double* weight, union data_transfer_union data_transfer, struct focus_data_struct* focus_data,
                             _class_particle* _particle) {

    // This component need to write to its storage transfer for each event, is that possible with this structure?
    struct line_info_struct_union* line_info = data_transfer.pointer_to_a_Powder_physics_storage_struct->line_info_storage;
    double vertical_angular_limit = data_transfer.pointer_to_a_Powder_physics_storage_struct->vertical_angular_limit;

    // Should not interfer with the global variables
    double vx = k_initial[0] * K2V;
    double vy = k_initial[1] * K2V;
    double vz = k_initial[2] * K2V;

    double v = sqrt (vx * vx + vy * vy + vz * vz);

    int line;
    double arg;
    double theta;
    double alpha, alpha0;

    double vout_x, vout_y, vout_z;
    double tmp_vx, tmp_vy, tmp_vz;
    double nx, ny, nz;
    double my_s_n;

    // copy from PowderN component
    if (line_info->count > 0) {
      /* choose line */
      if (line_info->Nq > 1)
        line = floor (line_info->Nq * rand01 ()); /* Select between Nq powder lines */
      else
        line = 0;
      if (line_info->w_v[line])
        arg = line_info->q_v[line] * (1 + line_info->w_v[line] * randnorm ()) / (2.0 * v);
      else
        arg = line_info->q_v[line] / (2.0 * v);
      my_s_n = line_info->my_s_v2[line] / (v * v);
      if (fabs (arg) > 1) {
        // printf("Powder scattering function returned 0, should not happen\n");
        return 0; /* No bragg scattering possible (was absorb)*/
      }
      theta = asin (arg); /* Bragg scattering law */

      /* Choose point on Debye-Scherrer cone */
      if (vertical_angular_limit) { /* relate height of detector to the height on DS cone */
        arg = sin (vertical_angular_limit * DEG2RAD / 2) / sin (2 * theta);
        /* If full Debye-Scherrer cone is within d_phi, don't focus */
        if (arg < -1 || arg > 1)
          vertical_angular_limit = 0;
        /* Otherwise, determine alpha to rotate from scattering plane
           into vertical_angular_limit focusing area*/
        else
          alpha = 2 * asin (arg);
      }
      if (vertical_angular_limit) {
        /* Focusing */
        alpha = fabs (alpha);
        /* Trick to get scattering for pos/neg theta's */
        alpha0 = 2 * rand01 () * alpha;
        if (alpha0 > alpha) {
          alpha0 = PI + (alpha0 - 1.5 * alpha);
        } else {
          alpha0 = alpha0 - 0.5 * alpha;
        }
      } else
        alpha0 = PI * randpm1 ();

      /* now find a nearly vertical rotation axis:
       * Either
       *  (v along Z) x (X axis) -> nearly Y axis
       * Or
       *  (v along X) x (Z axis) -> nearly Y axis
       */
      if (fabs (scalar_prod (1, 0, 0, vx / v, vy / v, vz / v)) < fabs (scalar_prod (0, 0, 1, vx / v, vy / v, vz / v))) {
        nx = 1;
        ny = 0;
        nz = 0;
      } else {
        nx = 0;
        ny = 0;
        nz = 1;
      }
      vec_prod (tmp_vx, tmp_vy, tmp_vz, vx, vy, vz, nx, ny, nz);

      /* v_out = rotate 'v' by 2*theta around tmp_v: Bragg angle */
      rotate (vout_x, vout_y, vout_z, vx, vy, vz, 2 * theta, tmp_vx, tmp_vy, tmp_vz);

      /* tmp_v = rotate v_out by alpha0 around 'v' (Debye-Scherrer cone) */
      rotate (tmp_vx, tmp_vy, tmp_vz, vout_x, vout_y, vout_z, alpha0, vx, vy, vz);
      vx = tmp_vx;
      vy = tmp_vy;
      vz = tmp_vz;

      k_final[0] = V2K * vx;
      k_final[1] = V2K * vy;
      k_final[2] = V2K * vz;

      //*weight *= line_info->Nq*my_s_n; I believe my_s_n is part of the correction for sampling posistion, not to be done here
      *weight *= line_info->Nq * my_s_n / line_info->my_s_v2_sum * v * v;

      // printf("my_s_n = %f \n",my_s_n);

      // What to do with my_s_n ?
      /*
      pmul  = line_info->Nq*l_full*my_s_n*exp(-(line_info->my_a_v/v+my_s)*(l+l_1))
                              /(1-(p_inc+p_transmit));
      */
      // Correction in case of vertical_angular_limit focusing - BUT only when d_phi != 0
      if (vertical_angular_limit)
        *weight *= alpha / PI;

      line_info->type = 'c';
      line_info->dq = line_info->q_v[line] * V2K;

    } else {
      /* else transmit <-- No powder lines in file */
      printf ("Error, need lines in the PowderN input file\n");
    }

    // printf("Powder scattering function returned 1\n");
    return 1;
  };
  #ifndef PROCESS_DETECTOR
  #define PROCESS_DETECTOR dummy
  #endif

  #ifndef PROCESS_POWDER_DETECTOR
  #define PROCESS_POWDER_DETECTOR dummy
  #endif

/* Shared user declarations for all components types 'Union_cylinder'. */
  #ifndef Union
  #error "The Union_init component must be included before this Union_cylinder component"
  #endif

  void
  mcdisplay_cylinder_function (struct lines_to_draw* lines_to_draw_output, int index, struct geometry_struct** Geometries, int number_of_volumes) {
    // Function to call in mcdisplay section of the sample component for this volume
    // One can assume that Geometries[index] refers to a geometry as described in this file
    // The 4 lines describin the cylinders side are aligned to the local frame of the cylinder,
    //   it would be nicer to have them alligned with the global frame so that they show up nicely in
    //   pgplotters on mcdisplay.
    // One could get the current global rotation and use this to counteract this effect.

    double height = Geometries[index]->geometry_parameters.p_cylinder_storage->height;
    double radius = Geometries[index]->geometry_parameters.p_cylinder_storage->cyl_radius;
    Coords direction = Geometries[index]->geometry_parameters.p_cylinder_storage->direction_vector;
    Coords center = Geometries[index]->center;

    Coords bottom_point = coords_add (center, coords_scalar_mult (direction, 0.5 * height));
    Coords top_point = coords_add (center, coords_scalar_mult (direction, -0.5 * height));

    struct lines_to_draw lines_to_draw_temp;
    lines_to_draw_temp.number_of_lines = 0;

    lines_to_draw_temp = draw_circle_with_highest_priority (top_point, direction, radius, index, Geometries, number_of_volumes, 2);
    merge_lines_to_draw (lines_to_draw_output, &lines_to_draw_temp);

    lines_to_draw_temp = draw_circle_with_highest_priority (bottom_point, direction, radius, index, Geometries, number_of_volumes, 2);
    merge_lines_to_draw (lines_to_draw_output, &lines_to_draw_temp);

    Coords point1, point2;
    int iterate, number_of_points = 4;

    for (iterate = 0; iterate < number_of_points; iterate++) {
      point1 = point_on_circle (top_point, direction, radius, iterate, number_of_points);
      point2 = point_on_circle (bottom_point, direction, radius, iterate, number_of_points);
      lines_to_draw_temp = draw_line_with_highest_priority (point1, point2, index, Geometries, number_of_volumes, 2);
      merge_lines_to_draw (lines_to_draw_output, &lines_to_draw_temp);
    }
  };

  void
  initialize_cylinder_geometry_from_main_component (struct geometry_struct* cylinder) {
    // Function to be called in initialize of the main component
    // This is done as the rotation matrix needs to be relative to the main component instead of global
    // Everything done in initialize in this component file has the rotation matrix relative to global

    Coords simple_vector;
    Coords cyl_vector;

    // Start with vector that points along the cylinder in the local frame
    simple_vector = coords_set (0, 1, 0);

    // Rotate the direction vector of the cylinder to the master component frame of reference
    cyl_vector = rot_apply (cylinder->rotation_matrix, simple_vector);
    NORM (cyl_vector.x, cyl_vector.y, cyl_vector.z);
    cylinder->geometry_parameters.p_cylinder_storage->direction_vector.x = cyl_vector.x;
    cylinder->geometry_parameters.p_cylinder_storage->direction_vector.y = cyl_vector.y;
    cylinder->geometry_parameters.p_cylinder_storage->direction_vector.z = cyl_vector.z;
    // if (verbal == 1) printf("Cords vector1 = (%f,%f,%f)\n",cyl_vector.x,cyl_vector.y,
  }

  struct pointer_to_1d_coords_list
  cylinder_shell_points (struct geometry_struct* geometry, int max_number_of_points) {
    // Function that returns a number (less than max) of points on the geometry surface
    // If used, remember to free the space allocated.
    int points_per_circle = floor (max_number_of_points / 2.0);

    struct pointer_to_1d_coords_list cylinder_shell_array;
    cylinder_shell_array.elements = malloc (2 * points_per_circle * sizeof (Coords));
    cylinder_shell_array.num_elements = 2 * points_per_circle;

    Coords cyl_direction = geometry->geometry_parameters.p_cylinder_storage->direction_vector;
    Coords center = geometry->center;
    double radius = geometry->geometry_parameters.p_cylinder_storage->cyl_radius;
    double height = geometry->geometry_parameters.p_cylinder_storage->height;

    Coords cyl_top_point = coords_add (center, coords_scalar_mult (cyl_direction, 0.5 * height));
    Coords cyl_bottom_point = coords_add (center, coords_scalar_mult (cyl_direction, -0.5 * height));

    points_on_circle (cylinder_shell_array.elements, cyl_top_point, cyl_direction, radius, points_per_circle);
    // Need to verify this pointer arithimatic works as intended
    points_on_circle (cylinder_shell_array.elements + points_per_circle, cyl_bottom_point, cyl_direction, radius, points_per_circle);

    return cylinder_shell_array;
  }

  #ifndef ANY_GEOMETRY_DETECTOR_DECLARE
  #define ANY_GEOMETRY_DETECTOR_DECLARE dummy
  // struct pointer_to_global_geometry_list global_geometry_list = {0,NULL};
  #endif

/* Shared user declarations for all components types 'Union_sphere'. */
  #ifndef Union
  #error "The Union_init component must be included before this Union_sphere component"
  #endif

  void
  mcdisplay_sphere_function (struct lines_to_draw* lines_to_draw_output, int index, struct geometry_struct** Geometries, int number_of_volumes) {
    // Function to call in mcdisplay section of the sample component for this volume
    // One can assume that Geometries[index] refers to a geometry as described in this file
    // The 4 lines describing the sphere are aligned to the local frame of the sphere,
    //   it would be nicer to have them alligned with the global frame so that they show up nicely in
    //   pgplotters on mcdisplay.
    // One could get the current global rotation and use this to counteract this effect.

    double radius = Geometries[index]->geometry_parameters.p_sphere_storage->sph_radius;
    Coords center = Geometries[index]->center;

    Coords direction1 = coords_set (0, 0, 1.0);
    Coords direction2 = coords_set (0, 1.0, 0);
    Coords direction3 = coords_set (1.0, 0, 0);

    struct lines_to_draw lines_to_draw_temp;
    lines_to_draw_temp.number_of_lines = 0;

    lines_to_draw_temp = draw_circle_with_highest_priority (center, direction1, radius, index, Geometries, number_of_volumes, 2);
    merge_lines_to_draw (lines_to_draw_output, &lines_to_draw_temp);

    lines_to_draw_temp = draw_circle_with_highest_priority (center, direction2, radius, index, Geometries, number_of_volumes, 2);
    merge_lines_to_draw (lines_to_draw_output, &lines_to_draw_temp);

    lines_to_draw_temp = draw_circle_with_highest_priority (center, direction3, radius, index, Geometries, number_of_volumes, 2);
    merge_lines_to_draw (lines_to_draw_output, &lines_to_draw_temp);
  };

  void
  initialize_sphere_geometry_from_main_component (struct geometry_struct* sphere) {
    // Function to be called in initialize of the main component
    // This is done as the rotation matrix needs to be relative to the main component instead of global
    // Everything done in initialize in this component file has the rotation matrix relative to global

    // Nothing needs to be done for the sphere
    // If an empty function provides difficulties for some compilers, a dummy operation can be added
    int dummy;
  };

  struct pointer_to_1d_coords_list
  sphere_shell_points (struct geometry_struct* geometry, int max_number_of_points) {
    // Function that returns a number (less than max) of points on the geometry surface
    // If used, remember to free the space allocated.

    int points_per_circle = floor (sqrt (max_number_of_points));
    int number_of_circles = points_per_circle;

    struct pointer_to_1d_coords_list sphere_shell_array;
    sphere_shell_array.elements = malloc (points_per_circle * number_of_circles * sizeof (Coords));
    sphere_shell_array.num_elements = points_per_circle * number_of_circles;

    Coords center = geometry->center;
    double radius = geometry->geometry_parameters.p_sphere_storage->sph_radius;

    Coords direction = coords_set (0, 0, 1.0);

    Rotation rot_matrix;
    rot_set_rotation (rot_matrix, (double)PI / number_of_circles, 0, 0);

    // print_rotation(rot_matrix,"rot matrix");

    // Do the first iteration before the loop to avoid one unecessary matrix operation
    points_on_circle (sphere_shell_array.elements, center, direction, radius, points_per_circle);

    int iterate;
    for (iterate = 1; iterate < number_of_circles; iterate++) {
      direction = rot_apply (rot_matrix, direction);
      points_on_circle (sphere_shell_array.elements + points_per_circle * iterate, center, direction, radius, points_per_circle);
    }
    // Other parts of the program change behavior when I use the above code, may be writing to unwanted parts of memory!

    /*
    // Debug
    for (iterate=0;iterate<sphere_shell_array.num_elements;iterate++) {
      //print_position(sphere_shell_array.elements[iterate],"Sphere shell points");
      //printf("\n%f,%f,%f",sphere_shell_array.elements[iterate].x,sphere_shell_array.elements[iterate].y,sphere_shell_array.elements[iterate].z);
    }
    */

    return sphere_shell_array;
  }

  #ifndef ANY_GEOMETRY_DETECTOR_DECLARE
  #define ANY_GEOMETRY_DETECTOR_DECLARE dummy
  // struct pointer_to_global_geometry_list global_geometry_list = {0,NULL};
  #endif

/* Shared user declarations for all components types 'Union_box'. */

  #ifndef Union
  #error "The Union_init component must be included before this Union_box component"
  #endif

  void
  mcdisplay_box_function (struct lines_to_draw* lines_to_draw_output, int index, struct geometry_struct** Geometries, int number_of_volumes) {
    // Function to call in mcdisplay section of the sample component for this volume
    // One can assume that Volumes[index] refers to a volume with the geometry described in this file

    double depth = Geometries[index]->geometry_parameters.p_box_storage->z_depth;
    double width1 = Geometries[index]->geometry_parameters.p_box_storage->x_width1;
    double width2 = Geometries[index]->geometry_parameters.p_box_storage->x_width2;
    double height1 = Geometries[index]->geometry_parameters.p_box_storage->y_height1;
    double height2 = Geometries[index]->geometry_parameters.p_box_storage->y_height2;

    Coords x_vector = Geometries[index]->geometry_parameters.p_box_storage->x_vector;
    Coords y_vector = Geometries[index]->geometry_parameters.p_box_storage->y_vector;
    Coords z_vector = Geometries[index]->geometry_parameters.p_box_storage->z_vector;

    Coords center = Geometries[index]->center;

    Coords square1[4], square2[4];

    square1[0] = coords_add (coords_add (coords_add (center, coords_scalar_mult (z_vector, -0.5 * depth)), coords_scalar_mult (x_vector, -0.5 * width1)),
                             coords_scalar_mult (y_vector, -0.5 * height1));

    square1[1] = coords_add (square1[0], coords_scalar_mult (x_vector, width1));
    square1[2] = coords_add (square1[1], coords_scalar_mult (y_vector, height1));
    square1[3] = coords_add (square1[0], coords_scalar_mult (y_vector, height1));

    square2[0] = coords_add (coords_add (coords_add (center, coords_scalar_mult (z_vector, 0.5 * depth)), coords_scalar_mult (x_vector, -0.5 * width2)),
                             coords_scalar_mult (y_vector, -0.5 * height2));

    square2[1] = coords_add (square2[0], coords_scalar_mult (x_vector, width2));
    square2[2] = coords_add (square2[1], coords_scalar_mult (y_vector, height2));
    square2[3] = coords_add (square2[0], coords_scalar_mult (y_vector, height2));

    struct lines_to_draw lines_to_draw_temp;
    lines_to_draw_temp.number_of_lines = 0;

    int iterate;
    for (iterate = 0; iterate < 3; iterate++) {
      lines_to_draw_temp = draw_line_with_highest_priority (square1[iterate], square1[iterate + 1], index, Geometries, number_of_volumes, 2);
      merge_lines_to_draw (lines_to_draw_output, &lines_to_draw_temp);
    }
    lines_to_draw_temp = draw_line_with_highest_priority (square1[3], square1[0], index, Geometries, number_of_volumes, 2);
    merge_lines_to_draw (lines_to_draw_output, &lines_to_draw_temp);

    for (iterate = 0; iterate < 3; iterate++) {
      lines_to_draw_temp = draw_line_with_highest_priority (square2[iterate], square2[iterate + 1], index, Geometries, number_of_volumes, 2);
      merge_lines_to_draw (lines_to_draw_output, &lines_to_draw_temp);
    }
    lines_to_draw_temp = draw_line_with_highest_priority (square2[3], square2[0], index, Geometries, number_of_volumes, 2);
    merge_lines_to_draw (lines_to_draw_output, &lines_to_draw_temp);

    for (iterate = 0; iterate < 4; iterate++) {
      lines_to_draw_temp = draw_line_with_highest_priority (square1[iterate], square2[iterate], index, Geometries, number_of_volumes, 2);
      merge_lines_to_draw (lines_to_draw_output, &lines_to_draw_temp);
    }
  };

  void
  initialize_box_geometry_from_main_component (struct geometry_struct* box) {
    // Function to be called in initialize of the main component
    // This is done as the rotation matrix needs to be relative to the main component instead of global
    // Everything done in initialize in this component file has the rotation matrix relative to global
    Coords simple_vector = coords_set (1, 0, 0);
    Coords rotated_vector;

    rotated_vector = rot_apply (box->rotation_matrix, simple_vector);
    NORM (rotated_vector.x, rotated_vector.y, rotated_vector.z);
    box->geometry_parameters.p_box_storage->x_vector = rotated_vector;

    simple_vector = coords_set (0, 1, 0);
    rotated_vector = rot_apply (box->rotation_matrix, simple_vector);
    NORM (rotated_vector.x, rotated_vector.y, rotated_vector.z);
    box->geometry_parameters.p_box_storage->y_vector = rotated_vector;

    simple_vector = coords_set (0, 0, 1);
    rotated_vector = rot_apply (box->rotation_matrix, simple_vector);
    NORM (rotated_vector.x, rotated_vector.y, rotated_vector.z);
    box->geometry_parameters.p_box_storage->z_vector = rotated_vector;
  };

  struct pointer_to_1d_coords_list
  box_shell_points (struct geometry_struct* geometry, int max_number_of_points) {
    // This function returns an array of corner positions for the box in the main coordinate system.
    // Normally one would limit it to a maximum number of points, but as there are only 8 for the box,
    //  it is hardcoded to 8. Other geometries can be approximated with a variable number of points.

    struct pointer_to_1d_coords_list corner_points;
    corner_points.elements = malloc (8 * sizeof (Coords));
    corner_points.num_elements = 8;

    double depth = geometry->geometry_parameters.p_box_storage->z_depth;
    double width1 = geometry->geometry_parameters.p_box_storage->x_width1;
    double width2 = geometry->geometry_parameters.p_box_storage->x_width2;
    double height1 = geometry->geometry_parameters.p_box_storage->y_height1;
    double height2 = geometry->geometry_parameters.p_box_storage->y_height2;

    Coords x_vector = geometry->geometry_parameters.p_box_storage->x_vector;
    Coords y_vector = geometry->geometry_parameters.p_box_storage->y_vector;
    Coords z_vector = geometry->geometry_parameters.p_box_storage->z_vector;

    Coords center = geometry->center;

    corner_points.elements[0]
        = coords_add (coords_add (coords_add (center, coords_scalar_mult (z_vector, -0.5 * depth)), coords_scalar_mult (x_vector, -0.5 * width1)),
                      coords_scalar_mult (y_vector, -0.5 * height1));

    corner_points.elements[1] = coords_add (corner_points.elements[0], coords_scalar_mult (x_vector, width1));
    corner_points.elements[2] = coords_add (corner_points.elements[1], coords_scalar_mult (y_vector, height1));
    corner_points.elements[3] = coords_add (corner_points.elements[0], coords_scalar_mult (y_vector, height1));

    corner_points.elements[4]
        = coords_add (coords_add (coords_add (center, coords_scalar_mult (z_vector, 0.5 * depth)), coords_scalar_mult (x_vector, -0.5 * width2)),
                      coords_scalar_mult (y_vector, -0.5 * height2));

    corner_points.elements[5] = coords_add (corner_points.elements[4], coords_scalar_mult (x_vector, width2));
    corner_points.elements[6] = coords_add (corner_points.elements[5], coords_scalar_mult (y_vector, height2));
    corner_points.elements[7] = coords_add (corner_points.elements[4], coords_scalar_mult (y_vector, height2));

    return corner_points;
  }

  #ifndef ANY_GEOMETRY_DETECTOR_DECLARE
  #define ANY_GEOMETRY_DETECTOR_DECLARE dummy
  #endif

/* Shared user declarations for all components types 'Union_cone'. */
  #ifndef Union
  #error "The Union_init component must be included before this Union_cone component"
  #endif

  void
  mcdisplay_cone_function (struct lines_to_draw* lines_to_draw_output, int index, struct geometry_struct** Geometries, int number_of_volumes) {
    // Function to call in mcdisplay section of the sample component for this volume
    // One can assume that Geometries[index] refers to a geometry as described in this file
    // The 4 lines describin the cylinders side are aligned to the local frame of the cylinder,
    //   it would be nicer to have them alligned with the global frame so that they show up nicely in
    //   pgplotters on mcdisplay.
    // One could get the current global rotation and use this to counteract this effect.

    double height = Geometries[index]->geometry_parameters.p_cone_storage->height;
    double radius_top = Geometries[index]->geometry_parameters.p_cone_storage->cone_radius_top;
    double radius_bottom = Geometries[index]->geometry_parameters.p_cone_storage->cone_radius_bottom;
    // int is_cylinder = Geometries[index]->geometry_parameters.p_cone_storage->is_cylinder;
    // Coords cone_tip = Geometries[index]->geometry_parameters.p_cone_storage->cone_tip;
    Coords direction = Geometries[index]->geometry_parameters.p_cone_storage->direction_vector;
    Coords center = Geometries[index]->center;

    Coords bottom_point = coords_add (center, coords_scalar_mult (direction, -0.5 * height));
    Coords top_point = coords_add (center, coords_scalar_mult (direction, 0.5 * height));

    struct lines_to_draw lines_to_draw_temp;
    lines_to_draw_temp.number_of_lines = 0;

    lines_to_draw_temp = draw_circle_with_highest_priority (top_point, direction, radius_top, index, Geometries, number_of_volumes, 2);
    merge_lines_to_draw (lines_to_draw_output, &lines_to_draw_temp);

    lines_to_draw_temp = draw_circle_with_highest_priority (bottom_point, direction, radius_bottom, index, Geometries, number_of_volumes, 2);
    merge_lines_to_draw (lines_to_draw_output, &lines_to_draw_temp);

    Coords point1, point2;
    int iterate, number_of_points = 4;

    for (iterate = 0; iterate < number_of_points; iterate++) {
      point1 = point_on_circle (top_point, direction, radius_top, iterate, number_of_points);
      point2 = point_on_circle (bottom_point, direction, radius_bottom, iterate, number_of_points);
      lines_to_draw_temp = draw_line_with_highest_priority (point1, point2, index, Geometries, number_of_volumes, 2);
      merge_lines_to_draw (lines_to_draw_output, &lines_to_draw_temp);
    }
  };

  void
  initialize_cone_geometry_from_main_component (struct geometry_struct* cone) {
    // Function to be called in initialize of the main component
    // This is done as the rotation matrix needs to be relative to the main component instead of global
    // Everything done in initialize in this component file has the rotation matrix relative to global

    Coords simple_vector;
    Coords cone_vector;

    // Start with vector that points along the cone in the local frame
    simple_vector = coords_set (0, 1, 0);

    // Rotate the direction vector of the cone to the master component frame of reference
    cone_vector = rot_apply (cone->rotation_matrix, simple_vector);
    NORM (cone_vector.x, cone_vector.y, cone_vector.z);
    cone->geometry_parameters.p_cone_storage->direction_vector.x = cone_vector.x;
    cone->geometry_parameters.p_cone_storage->direction_vector.y = cone_vector.y;
    cone->geometry_parameters.p_cone_storage->direction_vector.z = cone_vector.z;
    // if (verbal == 1) printf("Cords vector1 = (%f,%f,%f)\n",cone_vector.x,cone_vector.y,
  }

  struct pointer_to_1d_coords_list
  shell_points (struct geometry_struct* geometry, int max_number_of_points) {
    // Function that returns a number (less than max) of points on the geometry surface
    // If used, remember to free the space allocated.
    int points_per_circle = floor (max_number_of_points / 2);

    struct pointer_to_1d_coords_list cone_shell_array;
    cone_shell_array.elements = malloc (2 * points_per_circle * sizeof (Coords));
    cone_shell_array.num_elements = 2 * points_per_circle;

    Coords cone_direction = geometry->geometry_parameters.p_cone_storage->direction_vector;
    Coords center = geometry->center;
    double radius_top = geometry->geometry_parameters.p_cone_storage->cone_radius_top;
    double radius_bottom = geometry->geometry_parameters.p_cone_storage->cone_radius_bottom;
    double height = geometry->geometry_parameters.p_cone_storage->height;
    int number_of_points_in_array = 0;
    // int is_cylinder = geometry->geometry_parameters.p_cone_storage->is_cylinder;
    // Coords cone_tip = geometry->geometry_parameters.p_cone_storage->cone_tip;

    Coords cone_top_point = coords_add (center, coords_scalar_mult (cone_direction, 0.5 * height));
    Coords cone_bottom_point = coords_add (center, coords_scalar_mult (cone_direction, -0.5 * height));

    if (radius_top != 0) {
      points_on_circle (cone_shell_array.elements, cone_top_point, cone_direction, radius_top, points_per_circle);
      // printf("\nAdded point on top cone as index %i - %i\n",number_of_points_in_array,number_of_points_in_array + points_per_circle);
      number_of_points_in_array += points_per_circle;
    } else {
      // If this radius is 0 then add a single point instead of a circle
      cone_shell_array.elements[0] = cone_top_point;
      // printf("\nAdded point on top cone as index %i\n",0);
      number_of_points_in_array += 1;
    }
    // Need to verify this pointer arithimatic works as intended
    // points_on_circle(cone_shell_array.elements+points_per_circle,cone_bottom_point,cone_direction,radius_bottom,points_per_circle);

    if (radius_bottom != 0) {
      points_on_circle (cone_shell_array.elements + number_of_points_in_array, cone_bottom_point, cone_direction, radius_bottom, points_per_circle);
      // printf("\nAdded point on bottom cone as index %i - %i\n",number_of_points_in_array,number_of_points_in_array + points_per_circle);
      number_of_points_in_array += points_per_circle;
    } else {
      // If this radius is 0 then add a single point instead of a circle
      cone_shell_array.elements[number_of_points_in_array] = cone_bottom_point;
      // printf("\nAdded point on bottom cone as index %i\n",number_of_points_in_array);
      number_of_points_in_array += 1;
    }
    cone_shell_array.num_elements = number_of_points_in_array;
    return cone_shell_array;
  }

  #ifndef ANY_GEOMETRY_DETECTOR_DECLARE
  #define ANY_GEOMETRY_DETECTOR_DECLARE dummy
  // struct pointer_to_global_geometry_list global_geometry_list = {0,NULL};
  #endif

/* Shared user declarations for all components types 'Union_logger_1D'. */
  #ifndef Union
  #error "The Union_init component must be included before this Union_logger_1D component"
  #endif

  struct temp_1D_data_element_struct {
    int index;
    double weight;
  };

  struct temp_1D_data_struct {
    int num_elements;
    int allocated_elements;
    struct temp_1D_data_element_struct* elements;
  };

  struct a_1D_storage_struct {
    struct Detector_1D_struct Detector_1D;
    struct temp_1D_data_struct temp_1D_data;
    // some type
    int variable_identifier;
    int order;
    int order_in_this_volume;
    int order_process_in_this_volume;
  };

  // record_to_temp
  // Would be nice if x y z, k_new and k_old were all coords
  void
  record_to_temp_1D (Coords* position, double* k_new, double* k_old, double p, double p_old, double time, int scattered_in_this_volume,
                     int scattered_in_this_volume_by_this_process, int total_number_of_scattering_events, struct logger_struct* logger,
                     struct logger_with_data_struct* logger_with_data_array) {

    struct a_1D_storage_struct* storage;
    storage = logger->data_union.p_1D_storage;

    int add_point = 1;

    if (storage->order != 0) {
      if (storage->order - 1 == total_number_of_scattering_events)
        add_point = 1;
      else
        add_point = 0;
    }

    if (storage->order_in_this_volume != 0) {
      if (storage->order_in_this_volume - 1 == scattered_in_this_volume)
        add_point = 1;
      else
        add_point = 0;
    }

    if (storage->order_process_in_this_volume != 0) {
      if (storage->order_process_in_this_volume - 1 == scattered_in_this_volume_by_this_process)
        add_point = 1;
      else
        add_point = 0;
    }

    if (add_point == 1) {

      int i;
      double value;

      if (storage->variable_identifier == 1) {
        value = time;
      } else if (storage->variable_identifier == 2) {
        value = sqrt ((k_new[0] - k_old[0]) * (k_new[0] - k_old[0]) + (k_new[1] - k_old[1]) * (k_new[1] - k_old[1])
                      + (k_new[2] - k_old[2]) * (k_new[2] - k_old[2]));
      }

      // Find bin in histogram
      if (value > storage->Detector_1D.min && value < storage->Detector_1D.max) {
        i = floor ((value - storage->Detector_1D.min) * storage->Detector_1D.bins / (storage->Detector_1D.max - storage->Detector_1D.min));

        // Save bin in histogram to temp (may need to allocate more memory)
        int index;
        // printf("number of data points used: %d space allocated for %d data points.
        // \n",storage->temp_1D_data.num_elements,storage->temp_1D_data.allocated_elements);

        if (storage->temp_1D_data.num_elements < storage->temp_1D_data.allocated_elements) {
          storage->temp_1D_data.elements[storage->temp_1D_data.num_elements].index = i;
          storage->temp_1D_data.elements[storage->temp_1D_data.num_elements++].weight = p;
        } else {
          // No more space, need to allocate a larger buffer for this logger. Wish I had generics.

          // copy current data to temp
          struct temp_1D_data_struct temporary_storage;
          temporary_storage.num_elements = storage->temp_1D_data.num_elements;
          temporary_storage.elements = malloc (temporary_storage.num_elements * sizeof (struct temp_1D_data_element_struct));

          for (index = 0; index < storage->temp_1D_data.num_elements; index++) {
            temporary_storage.elements[index].index = storage->temp_1D_data.elements[index].index;
            temporary_storage.elements[index].weight = storage->temp_1D_data.elements[index].weight;
          }

          // free current data
          free (storage->temp_1D_data.elements);

          // allocate larger array (10 larger)
          storage->temp_1D_data.allocated_elements = 10 + storage->temp_1D_data.num_elements;
          storage->temp_1D_data.elements = malloc (storage->temp_1D_data.allocated_elements * sizeof (struct temp_1D_data_element_struct));

          // copy back from temp
          for (index = 0; index < storage->temp_1D_data.num_elements; index++) {
            storage->temp_1D_data.elements[index].index = temporary_storage.elements[index].index;
            storage->temp_1D_data.elements[index].weight = temporary_storage.elements[index].weight;
          }

          // free temporary data
          free (temporary_storage.elements);

          // add new data point
          storage->temp_1D_data.elements[storage->temp_1D_data.num_elements].index = i;
          storage->temp_1D_data.elements[storage->temp_1D_data.num_elements++].weight = p;
        }

        // If this is the first time this ray is being recorded in this logger, add it to the list of loggers that write to temp and may get it moved to perm
        if (storage->temp_1D_data.num_elements == 1)
          add_to_logger_with_data (logger_with_data_array, logger);
      }
    }
  }

  // clear_temp
  void
  clear_temp_1D (union logger_data_union* data_union) {
    data_union->p_1D_storage->temp_1D_data.num_elements = 0;
  }

  // record_to_perm
  void
  record_to_perm_1D (Coords* position, double* k_new, double* k_old, double p, double p_old, double time, int scattered_in_this_volume,
                     int scattered_in_this_volume_by_this_process, int total_number_of_scattering_events, struct logger_struct* logger,
                     struct logger_with_data_struct* logger_with_data_array) {

    // printf("In record to permanent \n");
    struct a_1D_storage_struct* storage;
    storage = logger->data_union.p_1D_storage;

    int add_point = 1;

    if (storage->order != 0) {
      if (storage->order - 1 == total_number_of_scattering_events)
        add_point = 1;
      else
        add_point = 0;
    }

    if (storage->order_in_this_volume != 0) {
      if (storage->order_in_this_volume - 1 == scattered_in_this_volume)
        add_point = 1;
      else
        add_point = 0;
    }

    if (storage->order_process_in_this_volume != 0) {
      if (storage->order_process_in_this_volume - 1 == scattered_in_this_volume_by_this_process)
        add_point = 1;
      else
        add_point = 0;
    }

    if (add_point == 1) {
      // printf("storage was set \n");

      int i;
      double value;

      if (storage->variable_identifier == 1) {
        value = time;
      } else if (storage->variable_identifier == 2) {
        value = sqrt ((k_new[0] - k_old[0]) * (k_new[0] - k_old[0]) + (k_new[1] - k_old[1]) * (k_new[1] - k_old[1])
                      + (k_new[2] - k_old[2]) * (k_new[2] - k_old[2]));
      }

      // Find bin in histogram
      if (value > storage->Detector_1D.min && value < storage->Detector_1D.max) {

        i = floor ((value - storage->Detector_1D.min) * (double)storage->Detector_1D.bins / (storage->Detector_1D.max - storage->Detector_1D.min));

        // printf("Added to statistics for monitor [%d] [%d] \n",i,j);
        // printf("indicies found\n");

        storage->Detector_1D.Array_N[i]++;
        storage->Detector_1D.Array_p[i] += p;
        storage->Detector_1D.Array_p2[i] += p * p;
      }
    }
  }

  // write_temp_to_perm
  void
  write_temp_to_perm_1D (union logger_data_union* data_union) {

    struct a_1D_storage_struct* storage;
    storage = data_union->p_1D_storage;

    int index;
    // Add all data points to the historgram, they are saved as index / weight combinations
    for (index = 0; index < storage->temp_1D_data.num_elements; index++) {
      storage->Detector_1D.Array_N[storage->temp_1D_data.elements[index].index]++;

      storage->Detector_1D.Array_p[storage->temp_1D_data.elements[index].index] += storage->temp_1D_data.elements[index].weight;

      storage->Detector_1D.Array_p2[storage->temp_1D_data.elements[index].index]
          += storage->temp_1D_data.elements[index].weight * storage->temp_1D_data.elements[index].weight;
    }
    clear_temp_1D (data_union);
  }

  void
  write_temp_to_perm_final_p_1D (union logger_data_union* data_union, double final_weight) {

    struct a_1D_storage_struct* storage;
    storage = data_union->p_1D_storage;

    int index;
    // Add all data points to the historgram, they are saved as index / weight combinations
    for (index = 0; index < storage->temp_1D_data.num_elements; index++) {
      storage->Detector_1D.Array_N[storage->temp_1D_data.elements[index].index]++;

      storage->Detector_1D.Array_p[storage->temp_1D_data.elements[index].index] += final_weight;

      storage->Detector_1D.Array_p2[storage->temp_1D_data.elements[index].index] += final_weight * final_weight;
    }
    clear_temp_1D (data_union);
  }

  // Only need to define linking function for loggers once.
  #ifndef UNION_LOGGER
  #define UNION_LOGGER Dummy
  // Linking function for loggers, finds the indicies of the specified geometries on the global_geometry_list
  void
  manual_linking_function_logger_volumes (char* input_string, struct pointer_to_global_geometry_list* global_geometry_list,
                                          struct pointer_to_1d_int_list* accepted_volumes, char* component_name) {
    // Need to check a input_string of text for an occurance of name. If it is in the inputstring, yes return 1, otherwise 0.
    char* token;
    int loop_index;
    char local_string[512];

    strcpy (local_string, input_string);
    // get the first token
    token = strtok (local_string, ",");

    // walk through other tokens
    while (token != NULL) {
      // printf( " %s\n", token );
      for (loop_index = 0; loop_index < global_geometry_list->num_elements; loop_index++) {
        if (strcmp (token, global_geometry_list->elements[loop_index].name) == 0) {
          add_element_to_int_list (accepted_volumes, loop_index);
          break;
        }

        if (loop_index == global_geometry_list->num_elements - 1) {
          // All possible geometry names have been looked through, and the break was not executed.
          // Alert the user to this problem by showing the geometry name that was not found and the currently available geometires
          printf ("\n");
          printf ("ERROR: The target_geometry string \"%s\" in Union logger component \"%s\" had an entry that did not match a specified geometry. \n",
                  input_string, component_name);
          printf ("       The unrecoignized geometry name was: \"%s\" \n", token);
          printf ("       The geometries available at this point (need to be defined before the logger): \n");
          for (loop_index = 0; loop_index < global_geometry_list->num_elements; loop_index++)
            printf ("         %s\n", global_geometry_list->elements[loop_index].name);
          exit (1);
        }
      }

      // Updates the token
      token = strtok (NULL, ",");
    }
  }

  void
  manual_linking_function_logger_processes (char* input_string, struct physics_struct* p_physics, struct pointer_to_1d_int_list* accepted_processes,
                                            char* component_name, char* Volume_name) {
    // Need to check a input_string of text for an occurance of name. If it is in the inputstring, yes return 1, otherwise 0.
    char* token;
    int loop_index;
    char local_string[256];

    strcpy (local_string, input_string);
    // get the first token
    token = strtok (local_string, ",");

    // walk through other tokens
    while (token != NULL) {
      // printf( " %s\n", token );
      for (loop_index = 0; loop_index < p_physics->number_of_processes; loop_index++) {
        if (strcmp (token, p_physics->p_scattering_array[loop_index].name) == 0) {
          add_element_to_int_list (accepted_processes, loop_index);
          break;
        }

        if (loop_index == p_physics->number_of_processes - 1) {
          // All possible process names have been looked through, and the break was not executed.
          // Alert the user to this problem by showing the process name that was not found and the currently available processes
          printf ("\n");
          printf ("ERROR: The target process string \"%s\" in Union logger \"%s\" had an entry that did not match a specified process in assosiated volume "
                  "\"%s\". \n",
                  input_string, component_name, Volume_name);
          printf ("       The unrecoignized process name was: \"%s\" \n", token);
          printf ("       The processes defined in material \"%s\" of which  Volume \"%s\" is made: \n", p_physics->name, Volume_name);
          for (loop_index = 0; loop_index < p_physics->number_of_processes; loop_index++)
            printf ("         %s\n", p_physics->p_scattering_array[loop_index].name);
          exit (1);
        }
      }

      // Updates the token
      token = strtok (NULL, ",");
    }
  }
  #endif

/* Shared user declarations for all components types 'Union_logger_2D_space'. */
  #ifndef Union
  #error "The Union_init component must be included before this Union_logger_2D_space component"
  #endif

  struct temp_2DS_data_element_struct {
    int index_1;
    int index_2;
    double weight;
  };

  struct temp_2DS_data_struct {
    int num_elements;
    int allocated_elements;
    struct temp_2DS_data_element_struct* elements;
  };

  struct a_2DS_storage_struct {
    struct Detector_2D_struct Detector_2D;
    struct temp_2DS_data_struct temp_2DS_data;
    int dim_1_choice;
    int dim_2_choice;
    int order;
    int order_in_this_volume;
    int order_process_in_this_volume;

    Coords position;
    Rotation rotation;
    Rotation t_rotation;
  };

  // record_to_temp
  // Would be nice if x y z, k_new and k_old were all coords
  void
  record_to_temp_2DS (Coords* position, double* k_new, double* k_old, double p, double p_old, double time, int scattered_in_this_volume,
                      int scattered_in_this_volume_by_this_process, int total_number_of_scattering_events, struct logger_struct* logger,
                      struct logger_with_data_struct* logger_with_data_array) {

    struct a_2DS_storage_struct* storage;
    storage = logger->data_union.p_2DS_storage;

    int add_point = 1;

    if (storage->order != 0) {
      if (storage->order - 1 == total_number_of_scattering_events)
        add_point = 1;
      else
        add_point = 0;
    }

    if (storage->order_in_this_volume != 0) {
      if (storage->order_in_this_volume - 1 == scattered_in_this_volume)
        add_point = 1;
      else
        add_point = 0;
    }

    if (storage->order_process_in_this_volume != 0) {
      if (storage->order_process_in_this_volume - 1 == scattered_in_this_volume_by_this_process)
        add_point = 1;
      else
        add_point = 0;
    }

    if (add_point == 1) {

      double p1, p2;
      p1 = p2 = 0;

      // dim_1_choice = 0 for x, 1 for y, 2 for z. Done in initialize from input. "x" "y" "z".
      if (storage->dim_1_choice == 0)
        p1 = position->x - storage->position.x;
      else if (storage->dim_1_choice == 1)
        p1 = position->y - storage->position.y;
      else if (storage->dim_1_choice == 2)
        p1 = position->z - storage->position.z;

      if (storage->dim_2_choice == 0)
        p2 = position->x - storage->position.x;
      else if (storage->dim_2_choice == 1)
        p2 = position->y - storage->position.y;
      else if (storage->dim_2_choice == 2)
        p2 = position->z - storage->position.z;

      int i, j;

      // Find bin in histogram
      if (p1 > storage->Detector_2D.D1min && p1 < storage->Detector_2D.D1max && p2 > storage->Detector_2D.D2min && p2 < storage->Detector_2D.D2max) {
        i = floor ((p1 - storage->Detector_2D.D1min) * storage->Detector_2D.bins_1 / (storage->Detector_2D.D1max - storage->Detector_2D.D1min));
        j = floor ((p2 - storage->Detector_2D.D2min) * storage->Detector_2D.bins_2 / (storage->Detector_2D.D2max - storage->Detector_2D.D2min));

        // Save bin in histogram to temp (may need to allocate more memory)
        int index;
        // printf("number of data points used: %d space allocated for %d data points.
        // \n",storage->temp_2DS_data.num_elements,storage->temp_2DS_data.allocated_elements);

        if (storage->temp_2DS_data.num_elements < storage->temp_2DS_data.allocated_elements) {
          storage->temp_2DS_data.elements[storage->temp_2DS_data.num_elements].index_1 = i;
          storage->temp_2DS_data.elements[storage->temp_2DS_data.num_elements].index_2 = j;
          storage->temp_2DS_data.elements[storage->temp_2DS_data.num_elements++].weight = p;
        } else {
          // No more space, need to allocate a larger buffer for this logger. Wish I had generics.

          // copy current data to temp
          struct temp_2DS_data_struct temporary_storage;
          temporary_storage.num_elements = storage->temp_2DS_data.num_elements;
          temporary_storage.elements = malloc (temporary_storage.num_elements * sizeof (struct temp_2DS_data_element_struct));

          for (index = 0; index < storage->temp_2DS_data.num_elements; index++) {
            temporary_storage.elements[index].index_1 = storage->temp_2DS_data.elements[index].index_1;
            temporary_storage.elements[index].index_2 = storage->temp_2DS_data.elements[index].index_2;
            temporary_storage.elements[index].weight = storage->temp_2DS_data.elements[index].weight;
          }

          // free current data
          free (storage->temp_2DS_data.elements);

          // allocate larger array (10 larger)
          storage->temp_2DS_data.allocated_elements = 10 + storage->temp_2DS_data.num_elements;
          storage->temp_2DS_data.elements = malloc (storage->temp_2DS_data.allocated_elements * sizeof (struct temp_2DS_data_element_struct));

          // copy back from temp
          for (index = 0; index < storage->temp_2DS_data.num_elements; index++) {
            storage->temp_2DS_data.elements[index].index_1 = temporary_storage.elements[index].index_1;
            storage->temp_2DS_data.elements[index].index_2 = temporary_storage.elements[index].index_2;
            storage->temp_2DS_data.elements[index].weight = temporary_storage.elements[index].weight;
          }

          // free temporary data
          free (temporary_storage.elements);

          // add new data point
          storage->temp_2DS_data.elements[storage->temp_2DS_data.num_elements].index_1 = i;
          storage->temp_2DS_data.elements[storage->temp_2DS_data.num_elements].index_2 = j;
          storage->temp_2DS_data.elements[storage->temp_2DS_data.num_elements++].weight = p;
        }

        // If this is the first time this ray is being recorded in this logger, add it to the list of loggers that write to temp and may get it moved to perm
        if (storage->temp_2DS_data.num_elements == 1)
          add_to_logger_with_data (logger_with_data_array, logger);
      }
    }
  }

  // clear_temp
  void
  clear_temp_2DS (union logger_data_union* data_union) {
    data_union->p_2DS_storage->temp_2DS_data.num_elements = 0;
  }

  // record_to_perm
  void
  record_to_perm_2DS (Coords* position, double* k_new, double* k_old, double p, double p_old, double time, int scattered_in_this_volume,
                      int scattered_in_this_volume_by_this_process, int total_number_of_scattering_events, struct logger_struct* logger,
                      struct logger_with_data_struct* logger_with_data_array) {

    // printf("In record to permanent \n");
    struct a_2DS_storage_struct* storage;
    storage = logger->data_union.p_2DS_storage;

    int add_point = 1;

    if (storage->order != 0) {
      if (storage->order - 1 == total_number_of_scattering_events)
        add_point = 1;
      else
        add_point = 0;
    }

    if (storage->order_in_this_volume != 0) {
      if (storage->order_in_this_volume - 1 == scattered_in_this_volume)
        add_point = 1;
      else
        add_point = 0;
    }

    if (storage->order_process_in_this_volume != 0) {
      if (storage->order_process_in_this_volume - 1 == scattered_in_this_volume_by_this_process)
        add_point = 1;
      else
        add_point = 0;
    }

    if (add_point == 1) {
      // printf("storage was set \n");
      double p1, p2;
      p1 = p2 = 0;

      // dim_1_choice = 0 for x, 1 for y, 2 for z. Done in initialize from input "x" "y" "z".
      if (storage->dim_1_choice == 0)
        p1 = position->x - storage->position.x;
      // p1 = position->x;
      else if (storage->dim_1_choice == 1)
        p1 = position->y - storage->position.y;
      // p1 = position->y;
      else if (storage->dim_1_choice == 2)
        p1 = position->z - storage->position.z;
      // p1 = position->z;

      if (storage->dim_2_choice == 0)
        p2 = position->x - storage->position.x;
      // p2 = position->x;
      else if (storage->dim_2_choice == 1)
        p2 = position->y - storage->position.y;
      // p2 = position->y;
      else if (storage->dim_2_choice == 2)
        p2 = position->z - storage->position.z;
      // p2 = position->z;

      int i, j;

      // Find bin in histogram
      if (p1 > storage->Detector_2D.D1min && p1 < storage->Detector_2D.D1max && p2 > storage->Detector_2D.D2min && p2 < storage->Detector_2D.D2max) {

        i = floor ((p1 - storage->Detector_2D.D1min) * (double)storage->Detector_2D.bins_1 / (storage->Detector_2D.D1max - storage->Detector_2D.D1min));
        j = floor ((p2 - storage->Detector_2D.D2min) * (double)storage->Detector_2D.bins_2 / (storage->Detector_2D.D2max - storage->Detector_2D.D2min));

        // printf("Added to statistics for monitor [%d] [%d] \n",i,j);
        // printf("indicies found\n");

        storage->Detector_2D.Array_N[i][j]++;
        storage->Detector_2D.Array_p[i][j] += p;
        storage->Detector_2D.Array_p2[i][j] += p * p;
      }
    }
  }

  // write_temp_to_perm
  void
  write_temp_to_perm_2DS (union logger_data_union* data_union) {
    struct a_2DS_storage_struct* storage;
    storage = data_union->p_2DS_storage;

    int index;
    // Add all data points to the historgram, they are saved as index / weight combinations
    for (index = 0; index < storage->temp_2DS_data.num_elements; index++) {

      storage->Detector_2D.Array_N[storage->temp_2DS_data.elements[index].index_1][storage->temp_2DS_data.elements[index].index_2]++;

      storage->Detector_2D.Array_p[storage->temp_2DS_data.elements[index].index_1][storage->temp_2DS_data.elements[index].index_2]
          += storage->temp_2DS_data.elements[index].weight;

      storage->Detector_2D.Array_p2[storage->temp_2DS_data.elements[index].index_1][storage->temp_2DS_data.elements[index].index_2]
          += storage->temp_2DS_data.elements[index].weight * storage->temp_2DS_data.elements[index].weight;
    }

    clear_temp_2DS (data_union);
  }

  // write_temp_to_perm_final_p
  void
  write_temp_to_perm_final_p_2DS (union logger_data_union* data_union, double final_weight) {
    struct a_2DS_storage_struct* storage;
    storage = data_union->p_2DS_storage;

    int index;
    // Add all data points to the historgram, they are saved as index / weight combinations
    for (index = 0; index < storage->temp_2DS_data.num_elements; index++) {

      storage->Detector_2D.Array_N[storage->temp_2DS_data.elements[index].index_1][storage->temp_2DS_data.elements[index].index_2]++;

      storage->Detector_2D.Array_p[storage->temp_2DS_data.elements[index].index_1][storage->temp_2DS_data.elements[index].index_2] += final_weight;

      storage->Detector_2D.Array_p2[storage->temp_2DS_data.elements[index].index_1][storage->temp_2DS_data.elements[index].index_2]
          += final_weight * final_weight;
    }

    clear_temp_2DS (data_union);
  }

  // Only need to define linking function for loggers once.
  #ifndef UNION_LOGGER
  #define UNION_LOGGER Dummy
  // Linking function for loggers, finds the indicies of the specified geometries on the global_geometry_list
  void
  manual_linking_function_logger_volumes (char* input_string, struct pointer_to_global_geometry_list* global_geometry_list,
                                          struct pointer_to_1d_int_list* accepted_volumes, char* component_name) {
    // Need to check a input_string of text for an occurance of name. If it is in the inputstring, yes return 1, otherwise 0.
    char* token;
    int loop_index;
    char local_string[512];

    strcpy (local_string, input_string);
    // get the first token
    token = strtok (local_string, ",");

    // walk through other tokens
    while (token != NULL) {
      // printf( " %s\n", token );
      for (loop_index = 0; loop_index < global_geometry_list->num_elements; loop_index++) {
        if (strcmp (token, global_geometry_list->elements[loop_index].name) == 0) {
          add_element_to_int_list (accepted_volumes, loop_index);
          break;
        }

        if (loop_index == global_geometry_list->num_elements - 1) {
          // All possible geometry names have been looked through, and the break was not executed.
          // Alert the user to this problem by showing the geometry name that was not found and the currently available geometires
          printf ("\n");
          printf ("ERROR: The target_geometry string \"%s\" in Union logger component \"%s\" had an entry that did not match a specified geometry. \n",
                  input_string, component_name);
          printf ("       The unrecoignized geometry name was: \"%s\" \n", token);
          printf ("       The geometries available at this point (need to be defined before the logger): \n");
          for (loop_index = 0; loop_index < global_geometry_list->num_elements; loop_index++)
            printf ("         %s\n", global_geometry_list->elements[loop_index].name);
          exit (1);
        }
      }

      // Updates the token
      token = strtok (NULL, ",");
    }
  }

  void
  manual_linking_function_logger_processes (char* input_string, struct physics_struct* p_physics, struct pointer_to_1d_int_list* accepted_processes,
                                            char* component_name, char* Volume_name) {
    // Need to check a input_string of text for an occurance of name. If it is in the inputstring, yes return 1, otherwise 0.
    char* token;
    int loop_index;
    char local_string[256];

    strcpy (local_string, input_string);
    // get the first token
    token = strtok (local_string, ",");

    // walk through other tokens
    while (token != NULL) {
      // printf( " %s\n", token );
      for (loop_index = 0; loop_index < p_physics->number_of_processes; loop_index++) {
        if (strcmp (token, p_physics->p_scattering_array[loop_index].name) == 0) {
          add_element_to_int_list (accepted_processes, loop_index);
          break;
        }

        if (loop_index == p_physics->number_of_processes - 1) {
          // All possible process names have been looked through, and the break was not executed.
          // Alert the user to this problem by showing the process name that was not found and the currently available processes
          printf ("\n");
          printf ("ERROR: The target process string \"%s\" in Union logger \"%s\" had an entry that did not match a specified process in assosiated volume "
                  "\"%s\". \n",
                  input_string, component_name, Volume_name);
          printf ("       The unrecoignized process name was: \"%s\" \n", token);
          printf ("       The processes defined in material \"%s\" of which  Volume \"%s\" is made: \n", p_physics->name, Volume_name);
          for (loop_index = 0; loop_index < p_physics->number_of_processes; loop_index++)
            printf ("         %s\n", p_physics->p_scattering_array[loop_index].name);
          exit (1);
        }
      }

      // Updates the token
      token = strtok (NULL, ",");
    }
  }
  #endif

  double**
  allocate2Ddouble_2DS (int count_x, int count_y) {
    // This function is needed to dynamically declare an array
    //  that has continous data as a static array would have,
    //  as that is the format expected by DETECTOR_OUT_2D.
    int i;

    // allocate space for actual data
    double* data = malloc (sizeof (double) * count_x * count_y);

    // create array or pointers to first elem in each 2D row
    double** ptr_array = malloc (sizeof (double*) * count_x);
    if (data == NULL || ptr_array == NULL) {
      free (data);
      free (ptr_array);
      printf ("\nERROR: ptr array or data not allocated in Union_logger_2D_space\n");
      exit (1);
    }
    for (i = 0; i < count_x; i++) {
      ptr_array[i] = data + (i * count_y);
    }
    return ptr_array;
  }

  void
  free2Ddouble_2DS (double** ptr_array) {
    if (!ptr_array)
      return;
    if (ptr_array[0])
      free (ptr_array[0]);
    free (ptr_array);
  }

/* Shared user declarations for all components types 'Union_conditional_standard'. */
  #ifndef Union
  #error "The Union_init component must be included before this Union_conditional_standard component"
  #endif

  int
  conditional_function_standard (union conditional_data_union* data_union, Coords* position, Coords* velocity, double* weight, double* time, int* current_volume,
                                 int* total_number_of_scattering, int* number_of_scattering_per_volume, int** number_of_scattering_per_volume_process) {

    // printf(" Inside conditional function \n");
    if (data_union->p_standard->T_limit == 1) {
      if (*time < data_union->p_standard->Tmin || *time > data_union->p_standard->Tmax)
        return 0;
    }

    if (data_union->p_standard->E_limit == 1) {
      // VS2E = 5.22704E-6 m_n / 2e
      double E = 5.22704E-6 * (velocity->x * velocity->x + velocity->y * velocity->y + velocity->z * velocity->z);
      if (E < data_union->p_standard->Emin || E > data_union->p_standard->Emax)
        return 0;
    }

    if (data_union->p_standard->Total_scat_limit == 1) {
      if (*total_number_of_scattering < data_union->p_standard->Total_scat_min || *total_number_of_scattering > data_union->p_standard->Total_scat_max)
        return 0;
    }

    if (data_union->p_standard->volume_index != -1) {
      if (*current_volume != data_union->p_standard->volume_index)
        return 0;
    }

    // All conditions fulfilled
    return 1;
  }

  // Only need to define linking function for conditionals once.
  // Does not need to redefine functions from: make_material / loggers, as one of each needs to be defined before.
  #ifndef UNION_CONDITIONAL
  #define UNION_CONDITIONAL Dummy
  // Linking function for loggers, finds the indicies of the specified loggers on global_logger_lists
  int
  manual_linking_logger_function_conditional (char* input_string, struct pointer_to_global_logger_list* global_logger_list,
                                              struct pointer_to_1d_int_list* accepted_loggers) {
    // Need to check a input_string of text for an occurance of name. If it is in the inputstring, yes return 1, otherwise 0.
    char* token;
    int loop_index;
    char local_string[512];

    strcpy (local_string, input_string);
    // get the first token
    token = strtok (local_string, ",");

    // walk through other tokens
    while (token != NULL) {
      // printf( " %s\n", token );
      for (loop_index = 0; loop_index < global_logger_list->num_elements; loop_index++) {
        if (strcmp (token, global_logger_list->elements[loop_index].name) == 0) {
          add_element_to_int_list (accepted_loggers, loop_index);
          break;
        }
      }

      // Updates the token
      token = strtok (NULL, ",");
    }

    return accepted_loggers->num_elements;
  }

  int
  manual_linking_abs_logger_function_conditional (char* input_string, struct pointer_to_global_abs_logger_list* global_abs_logger_list,
                                                  struct pointer_to_1d_int_list* accepted_abs_loggers) {
    // Need to check a input_string of text for an occurance of name. If it is in the inputstring, yes return 1, otherwise 0.
    char* token;
    int loop_index;
    char local_string[512];

    strcpy (local_string, input_string);
    // get the first token
    token = strtok (local_string, ",");

    // walk through other tokens
    while (token != NULL) {
      // printf( " %s\n", token );
      for (loop_index = 0; loop_index < global_abs_logger_list->num_elements; loop_index++) {
        if (strcmp (token, global_abs_logger_list->elements[loop_index].name) == 0) {
          add_element_to_int_list (accepted_abs_loggers, loop_index);
          break;
        }
      }

      // Updates the token
      token = strtok (NULL, ",");
    }

    return accepted_abs_loggers->num_elements;
  }

  /*
  // Linking function for masters, finds the indicies of the specified masters on the global_master_list
  int manual_linking_tagging_function_conditional(char *input_string, struct global_tagging_conditional_list_struct *global_tagging_list, struct
  pointer_to_1d_int_list *accepted_tagging) {
      // Need to check a input_string of text for an occurance of name. If it is in the inputstring, yes return 1, otherwise 0.
     char *token;
     int loop_index,found;
     char local_string[512];

     strcpy(local_string,input_string);
     // get the first token
     token = strtok(local_string,",");

     // walk through other tokens
     while( token != NULL )
     {
        found = 0;
        for (loop_index=0;loop_index<global_tagging_list->num_elements;loop_index++) {
          if (strcmp(token,global_tagging_list->elements[loop_index].name) == 0) {
            add_element_to_int_list(accepted_tagging,loop_index);
            found = 1;
            break;
          }
        }

        if (found == 0) {
          add_element_to_int_list(accepted_tagging,loop_index);
          add_element_to_tagging_conditional_list(

          add_function_to_conditional_list(global_tagging_list.elements[accepted_tagging.elements[loop_index]].conditional_list,&conditional_function_standard,&this_data_union);
        }

        // Updates the token
        token = strtok(NULL,",");
     }

     return accepted_tagging->num_elements;
  }
  */

  int
  count_commas (char* string) {
    int return_value = 0;

    int index;
    for (index = 0; index < strlen (string); index++) {
      // printf("%c \n",string[index]);
      if (string[index] == ',')
        return_value++;
    }

    // printf("number_of_commas = %d \n",return_value);
    return return_value;
  }
  #endif

/* Shared user declarations for all components types 'Union_conditional_PSD'. */
  #ifndef Union
  #error "The Union_init component must be included before this Union_conditional_PSD component"
  #endif

  int
  conditional_function_PSD (union conditional_data_union* data_union, Coords* position, Coords* velocity, double* weight, double* time, int* current_volume,
                            int* total_number_of_scattering, int* number_of_scattering_per_volume, int** number_of_scattering_per_volume_process) {

    // Transform to this coordinate system
    Coords local_position = transform_position (*position, data_union->p_PSD->PSD_position, data_union->p_PSD->PSD_rotation);
    Coords local_velocity = rot_apply (data_union->p_PSD->PSD_rotation, *velocity);

    // Prop_Z0
    if (local_velocity.z == 0)
      return 0;
    double dt = -local_position.z / local_velocity.z;
    if (dt < 0)
      return 0;
    local_position.x = local_position.x + dt * local_velocity.x;
    local_position.y = local_position.y + dt * local_velocity.y;

    double detect_time = *time + dt;

    if (data_union->p_PSD->T_limit == 1) {
      if (detect_time < data_union->p_PSD->Tmin || detect_time > data_union->p_PSD->Tmax)
        return 0;
    }

    if (local_position.x > data_union->p_PSD->PSD_half_xwidth)
      return 0;
    if (local_position.x < -data_union->p_PSD->PSD_half_xwidth)
      return 0;
    if (local_position.y > data_union->p_PSD->PSD_half_yheight)
      return 0;
    if (local_position.y < -data_union->p_PSD->PSD_half_yheight)
      return 0;

    // All conditions fulfilled
    return 1;
  }

  // Only need to define linking function for conditionals once.
  // Does not need to redefine functions from: make_material / loggers, as one of each needs to be defined before.
  #ifndef UNION_CONDITIONAL
  #define UNION_CONDITIONAL Dummy
  // Linking function for loggers, finds the indicies of the specified loggers on global_logger_lists
  int
  manual_linking_logger_function_conditional (char* input_string, struct pointer_to_global_logger_list* global_logger_list,
                                              struct pointer_to_1d_int_list* accepted_loggers) {
    // Need to check a input_string of text for an occurance of name. If it is in the inputstring, yes return 1, otherwise 0.
    char* token;
    int loop_index;
    char local_string[1024];

    strcpy (local_string, input_string);
    // get the first token
    token = strtok (local_string, ",");

    // walk through other tokens
    while (token != NULL) {
      // printf( " %s\n", token );
      for (loop_index = 0; loop_index < global_logger_list->num_elements; loop_index++) {
        if (strcmp (token, global_logger_list->elements[loop_index].name) == 0) {
          add_element_to_int_list (accepted_loggers, loop_index);
          break;
        }
      }

      // Updates the token
      token = strtok (NULL, ",");
    }

    return accepted_loggers->num_elements;
  }

  int
  count_commas (char* string) {
    int return_value = 0;

    int index;
    for (index = 0; index < strlen (string); index++) {
      // printf("%c \n",string[index]);
      if (string[index] == ',')
        return_value++;
    }

    // printf("number_of_commas = %d \n",return_value);
    return return_value;
  }

  int
  manual_linking_abs_logger_function_conditional (char* input_string, struct pointer_to_global_abs_logger_list* global_abs_logger_list,
                                                  struct pointer_to_1d_int_list* accepted_abs_loggers) {
    // Need to check a input_string of text for an occurance of name. If it is in the inputstring, yes return 1, otherwise 0.
    char* token;
    int loop_index;
    char local_string[512];

    strcpy (local_string, input_string);
    // get the first token
    token = strtok (local_string, ",");

    // walk through other tokens
    while (token != NULL) {
      // printf( " %s\n", token );
      for (loop_index = 0; loop_index < global_abs_logger_list->num_elements; loop_index++) {
        if (strcmp (token, global_abs_logger_list->elements[loop_index].name) == 0) {
          add_element_to_int_list (accepted_abs_loggers, loop_index);
          break;
        }
      }

      // Updates the token
      token = strtok (NULL, ",");
    }

    return accepted_abs_loggers->num_elements;
  }
  #endif

/* Shared user declarations for all components types 'Union_master'. */

  #ifndef Union
  #error "The Union_init component must be included before this Union_master component"
  #endif

  struct logger_with_data_struct loggers_with_data_array;
  struct abs_logger_with_data_struct abs_loggers_with_data_array;

  #ifndef MASTER_DETECTOR
  #define MASTER_DETECTOR dummy
  #endif

/* Shared user declarations for all components types 'Union_stop'. */
  #ifndef Union
  #error "The Union_init component must be included before this Union_stop component"
  #endif

/*
TODO

update Union master to use the flexible functions

deal with loggers and conditionals
*/

int physics_my(enum process choice, double *my,double *k_initial, union data_transfer_union data_transfer, struct focus_data_struct *focus_data, _class_particle *_particle) {

    int output = 0; // Error return value
    #ifdef PROCESS_DETECTOR
    switch(choice) {
        #ifdef PROCESS_INCOHERENT_DETECTOR
        case Incoherent:
            output = Incoherent_physics_my(my, k_initial, data_transfer, focus_data, _particle);
            break;
        #endif
        #ifdef PROCESS_POWDER_DETECTOR
        case Powder:
            output = Powder_physics_my(my, k_initial, data_transfer, focus_data, _particle);
            break;
        #endif
        #ifdef PROCESS_SINGLE_CRYSTAL_DETECTOR
        case Single_crystal:
            output = Single_crystal_physics_my(my, k_initial, data_transfer, focus_data, _particle);
            break;
        #endif
        #ifdef PROCESS_AF_HB_1D_DETECTOR
        case AF_HB_1D:
            output = AF_HB_1D_physics_my(my, k_initial, data_transfer, focus_data, _particle);
            break;
        #endif
        #ifdef PROCESS_PHONONSIMPLE_DETECTOR
        case PhononSimple:
            output = PhononSimple_physics_my(my, k_initial, data_transfer, focus_data, _particle);
            break;
        #endif
        #ifdef PROCESS_TEXTURE_DETECTOR
        case Texture:
            output = Texture_physics_my(my, k_initial, data_transfer, focus_data, _particle);
            break;
        #endif
        #ifdef PROCESS_INCOHERENTPHONON_DETECTOR
        case IncoherentPhonon:
            output = IncoherentPhonon_physics_my(my, k_initial, data_transfer, focus_data, _particle);
            break;
        #endif
        #ifdef PROCESS_NCRYSTAL_DETECTOR
        case NCrystal:
            output = NCrystal_physics_my(my, k_initial, data_transfer, focus_data, _particle);
            break;
        #endif	
        #ifdef PROCESS_NON_DETECTOR
        case Non:
            output = Non_physics_my(my, k_initial, data_transfer, focus_data, _particle);
            break;
        #endif
        #ifdef PROCESS_TEMPLATE_DETECTOR
        case Template:
            output = Template_physics_my(my, k_initial, data_transfer, focus_data, _particle);
            break;
        #endif
        default:
            printf("physics_my: No scattering process matches input!\n");
            break;
    }
    #endif
    return output;
}
  

int physics_scattering(enum process choice, double *k_final, double *k_initial, double *weight, union data_transfer_union data_transfer, struct focus_data_struct *focus_data, _class_particle *_particle) {

    int output = 0; // Error return value
    #ifdef PROCESS_DETECTOR
    switch(choice) {
        #ifdef PROCESS_INCOHERENT_DETECTOR
        case Incoherent:
            output = Incoherent_physics_scattering(k_final, k_initial, weight, data_transfer, focus_data, _particle);
            break;
        #endif
        #ifdef PROCESS_POWDER_DETECTOR
        case Powder:
            output = Powder_physics_scattering(k_final, k_initial, weight, data_transfer, focus_data, _particle);
            break;
        #endif
        #ifdef PROCESS_SINGLE_CRYSTAL_DETECTOR
        case Single_crystal:
            output = Single_crystal_physics_scattering(k_final, k_initial, weight, data_transfer, focus_data, _particle);
            break;
        #endif
        #ifdef PROCESS_AF_HB_1D_DETECTOR
        case AF_HB_1D:
            output = AF_HB_1D_physics_scattering(k_final, k_initial, weight, data_transfer, focus_data, _particle);
            break;
        #endif
        #ifdef PROCESS_PHONONSIMPLE_DETECTOR
        case PhononSimple:
            output = PhononSimple_physics_scattering(k_final, k_initial, weight, data_transfer, focus_data, _particle);
            break;
        #endif
        #ifdef PROCESS_TEXTURE_DETECTOR
        case Texture:
            output = Texture_physics_scattering(k_final, k_initial, weight, data_transfer, focus_data, _particle);
            break;
        #endif
        #ifdef PROCESS_INCOHERENTPHONON_DETECTOR
        case IncoherentPhonon:
            output = IncoherentPhonon_physics_scattering(k_final, k_initial, weight, data_transfer, focus_data, _particle);
            break;
        #endif
        #ifdef PROCESS_NCRYSTAL_DETECTOR
        case NCrystal:
            output = NCrystal_physics_scattering(k_final, k_initial, weight, data_transfer, focus_data, _particle);
            break;
        #endif
        #ifdef PROCESS_NON_DETECTOR
        case Non:
            output = Non_physics_scattering(k_final, k_initial, weight, data_transfer, focus_data, _particle);
            break;
        #endif			
        #ifdef PROCESS_TEMPLATE_DETECTOR
        case Template:
            output = Template_physics_scattering(k_final, k_initial, weight, data_transfer, focus_data, _particle);
            break;
        #endif
        default: printf("physics_scattering: No scattering process matches input!\n");
            break;
    }
    #endif
    return output;
}

int physics_surface(struct surface_process_struct *surface,                 // surface struct, has enum for choice and pointer to data
                    double *weight, double *wavevector,                     // information to surface_process, but also things it should update
					int *continues,                                         // output, whether the ray continues to next layer or not
					double *normal_vector, enum in_or_out in_out,           // information that should not be changed
					_class_particle *_particle) {                           // particle struct

    enum surface choice = surface->eSurface;

    int output = 0; // Error return value
    #ifdef SURFACE_DETECTOR
    switch(choice) {
        #ifdef SURFACE_PROCESS_MIRROR_DETECTOR
        case Mirror:
            output = Mirror_surface_function(surface->data_transfer, weight, wavevector, continues, normal_vector, in_out, _particle);
            break;
        #endif
        #ifdef SURFACE_PROCESS_TEMPLATE_DETECTOR
        case SurfaceTemplate:
            output = Template_surface_function(surface->data_transfer, weight, wavevector, continues, normal_vector, in_out, _particle);
            break;
        #endif
        default: printf("physics_surface: No surface process matches input!\n");
            break;
    }
    #endif
    return output;
}




/* ************************************************************************** */
/*             End of SHARE user declarations for all components              */
/* ************************************************************************** */


/* ********************** component definition declarations. **************** */

/* component init=Union_init() [1] DECLARE */
/* Parameter definition for component type 'Union_init' */
struct _struct_Union_init_parameters {
  /* Component type 'Union_init' private parameters */
  struct global_positions_to_transform_list_struct  global_positions_to_transform_list;
  struct global_rotations_to_transform_list_struct  global_rotations_to_transform_list;
  struct pointer_to_global_process_list  global_process_list;
  struct pointer_to_global_material_list  global_material_list;
  struct pointer_to_global_surface_list  global_surface_list;
  struct pointer_to_global_geometry_list  global_geometry_list;
  struct pointer_to_global_logger_list  global_all_volume_logger_list;
  struct pointer_to_global_logger_list  global_specific_volumes_logger_list;
  struct pointer_to_global_abs_logger_list  global_all_volume_abs_logger_list;
  struct pointer_to_global_abs_logger_list  global_specific_volumes_abs_logger_list;
  struct global_tagging_conditional_list_struct  global_tagging_conditional_list;
  struct pointer_to_global_master_list  global_master_list;
  int  global_mantid_min_pixel_id;
}; /* _struct_Union_init_parameters */
typedef struct _struct_Union_init_parameters _class_Union_init_parameters;

/* Parameters for component type 'Union_init' */
struct _struct_Union_init {
  char     _name[256]; /* e.g. init */
  char     _type[256]; /* Union_init */
  long     _index; /* e.g. 2 index in TRACE list */
  Coords   _position_absolute;
  Coords   _position_relative; /* wrt PREVIOUS */
  Rotation _rotation_absolute;
  Rotation _rotation_relative; /* wrt PREVIOUS */
  int      _rotation_is_identity;
  int      _position_relative_is_zero;
  _class_Union_init_parameters _parameters;
};
typedef struct _struct_Union_init _class_Union_init;
_class_Union_init _init_var;
#pragma acc declare create ( _init_var )

/* component Vanadium_incoherent=Incoherent_process() [2] DECLARE */
/* Parameter definition for component type 'Incoherent_process' */
struct _struct_Incoherent_process_parameters {
  /* Component type 'Incoherent_process' setting parameters */
  MCNUM sigma;
  MCNUM f_QE;
  MCNUM gamma;
  MCNUM packing_factor;
  MCNUM unit_cell_volume;
  MCNUM interact_fraction;
  char init[16384];
  /* Component type 'Incoherent_process' private parameters */
  struct global_process_element_struct  global_process_element;
  struct scattering_process_struct  This_process;
  struct Incoherent_physics_storage_struct  Incoherent_storage;
  double  effective_my_scattering;
}; /* _struct_Incoherent_process_parameters */
typedef struct _struct_Incoherent_process_parameters _class_Incoherent_process_parameters;

/* Parameters for component type 'Incoherent_process' */
struct _struct_Incoherent_process {
  char     _name[256]; /* e.g. Vanadium_incoherent */
  char     _type[256]; /* Incoherent_process */
  long     _index; /* e.g. 2 index in TRACE list */
  Coords   _position_absolute;
  Coords   _position_relative; /* wrt PREVIOUS */
  Rotation _rotation_absolute;
  Rotation _rotation_relative; /* wrt PREVIOUS */
  int      _rotation_is_identity;
  int      _position_relative_is_zero;
  _class_Incoherent_process_parameters _parameters;
};
typedef struct _struct_Incoherent_process _class_Incoherent_process;
_class_Incoherent_process _Vanadium_incoherent_var;
#pragma acc declare create ( _Vanadium_incoherent_var )

/* component Vanadium=Union_make_material() [3] DECLARE */
/* Parameter definition for component type 'Union_make_material' */
struct _struct_Union_make_material_parameters {
  /* Component type 'Union_make_material' setting parameters */
  char process_string[16384];
  MCNUM my_absorption;
  MCNUM absorber;
  MCNUM refraction_density;
  MCNUM refraction_sigma_coh;
  MCNUM refraction_weight;
  MCNUM refraction_SLD;
  char init[16384];
  /* Component type 'Union_make_material' private parameters */
  struct global_material_element_struct  global_material_element;
  struct physics_struct  this_material;
  int  loop_index;
  int  found_process;
  int  specified_processes;
  char  local_string[256];
  struct pointer_to_1d_int_list  accepted_processes;
}; /* _struct_Union_make_material_parameters */
typedef struct _struct_Union_make_material_parameters _class_Union_make_material_parameters;

/* Parameters for component type 'Union_make_material' */
struct _struct_Union_make_material {
  char     _name[256]; /* e.g. Vanadium */
  char     _type[256]; /* Union_make_material */
  long     _index; /* e.g. 2 index in TRACE list */
  Coords   _position_absolute;
  Coords   _position_relative; /* wrt PREVIOUS */
  Rotation _rotation_absolute;
  Rotation _rotation_relative; /* wrt PREVIOUS */
  int      _rotation_is_identity;
  int      _position_relative_is_zero;
  _class_Union_make_material_parameters _parameters;
};
typedef struct _struct_Union_make_material _class_Union_make_material;
_class_Union_make_material _Vanadium_var;
#pragma acc declare create ( _Vanadium_var )

_class_Incoherent_process _Al_incoherent_var;
#pragma acc declare create ( _Al_incoherent_var )

/* component Al_Powder=Powder_process() [5] DECLARE */
/* Parameter definition for component type 'Powder_process' */
struct _struct_Powder_process_parameters {
  /* Component type 'Powder_process' setting parameters */
  char reflections[16384];
  MCNUM packing_factor;
  MCNUM Vc;
  MCNUM delta_d_d;
  MCNUM DW;
  MCNUM nb_atoms;
  MCNUM d_phi;
  MCNUM density;
  MCNUM weight;
  MCNUM barns;
  MCNUM Strain;
  MCNUM interact_fraction;
  MCNUM format[9];
  char init[16384];
  /* Component type 'Powder_process' private parameters */
  struct global_process_element_struct  global_process_element;
  struct scattering_process_struct  This_process;
  struct Powder_physics_storage_struct  Powder_storage;
  struct line_info_struct_union  line_info;
  double  effective_my_scattering;
  double*  columns;
}; /* _struct_Powder_process_parameters */
typedef struct _struct_Powder_process_parameters _class_Powder_process_parameters;

/* Parameters for component type 'Powder_process' */
struct _struct_Powder_process {
  char     _name[256]; /* e.g. Al_Powder */
  char     _type[256]; /* Powder_process */
  long     _index; /* e.g. 2 index in TRACE list */
  Coords   _position_absolute;
  Coords   _position_relative; /* wrt PREVIOUS */
  Rotation _rotation_absolute;
  Rotation _rotation_relative; /* wrt PREVIOUS */
  int      _rotation_is_identity;
  int      _position_relative_is_zero;
  _class_Powder_process_parameters _parameters;
};
typedef struct _struct_Powder_process _class_Powder_process;
_class_Powder_process _Al_Powder_var;
#pragma acc declare create ( _Al_Powder_var )

_class_Union_make_material _Al_var;
#pragma acc declare create ( _Al_var )

_class_Incoherent_process _Cu_incoherent_var;
#pragma acc declare create ( _Cu_incoherent_var )

_class_Powder_process _Cu_Powder_var;
#pragma acc declare create ( _Cu_Powder_var )

_class_Union_make_material _Cu_var;
#pragma acc declare create ( _Cu_var )

/* component a1=Progress_bar() [10] DECLARE */
/* Parameter definition for component type 'Progress_bar' */
struct _struct_Progress_bar_parameters {
  /* Component type 'Progress_bar' setting parameters */
  char profile[16384];
  MCNUM percent;
  MCNUM flag_save;
  MCNUM minutes;
  /* Component type 'Progress_bar' private parameters */
  double  IntermediateCnts;
  time_t  StartTime;
  time_t  EndTime;
  time_t  CurrentTime;
  char  infostring[64];
}; /* _struct_Progress_bar_parameters */
typedef struct _struct_Progress_bar_parameters _class_Progress_bar_parameters;

/* Parameters for component type 'Progress_bar' */
struct _struct_Progress_bar {
  char     _name[256]; /* e.g. a1 */
  char     _type[256]; /* Progress_bar */
  long     _index; /* e.g. 2 index in TRACE list */
  Coords   _position_absolute;
  Coords   _position_relative; /* wrt PREVIOUS */
  Rotation _rotation_absolute;
  Rotation _rotation_relative; /* wrt PREVIOUS */
  int      _rotation_is_identity;
  int      _position_relative_is_zero;
  _class_Progress_bar_parameters _parameters;
};
typedef struct _struct_Progress_bar _class_Progress_bar;
_class_Progress_bar _a1_var;
#pragma acc declare create ( _a1_var )

/* component source=Source_div() [11] DECLARE */
/* Parameter definition for component type 'Source_div' */
struct _struct_Source_div_parameters {
  /* Component type 'Source_div' setting parameters */
  MCNUM xwidth;
  MCNUM yheight;
  MCNUM focus_aw;
  MCNUM focus_ah;
  MCNUM E0;
  MCNUM dE;
  MCNUM lambda0;
  MCNUM dlambda;
  MCNUM gauss;
  MCNUM flux;
  /* Component type 'Source_div' private parameters */
  double  sigmah;
  double  sigmav;
  double  p_init;
  double  dist;
  double  focus_xw;
  double  focus_yh;
}; /* _struct_Source_div_parameters */
typedef struct _struct_Source_div_parameters _class_Source_div_parameters;

/* Parameters for component type 'Source_div' */
struct _struct_Source_div {
  char     _name[256]; /* e.g. source */
  char     _type[256]; /* Source_div */
  long     _index; /* e.g. 2 index in TRACE list */
  Coords   _position_absolute;
  Coords   _position_relative; /* wrt PREVIOUS */
  Rotation _rotation_absolute;
  Rotation _rotation_relative; /* wrt PREVIOUS */
  int      _rotation_is_identity;
  int      _position_relative_is_zero;
  _class_Source_div_parameters _parameters;
};
typedef struct _struct_Source_div _class_Source_div;
_class_Source_div _source_var;
#pragma acc declare create ( _source_var )

/* component sample_pos=Arm() [12] DECLARE */
/* Parameter definition for component type 'Arm' */
struct _struct_Arm_parameters {
  char Arm_has_no_parameters;
}; /* _struct_Arm_parameters */
typedef struct _struct_Arm_parameters _class_Arm_parameters;

/* Parameters for component type 'Arm' */
struct _struct_Arm {
  char     _name[256]; /* e.g. sample_pos */
  char     _type[256]; /* Arm */
  long     _index; /* e.g. 2 index in TRACE list */
  Coords   _position_absolute;
  Coords   _position_relative; /* wrt PREVIOUS */
  Rotation _rotation_absolute;
  Rotation _rotation_relative; /* wrt PREVIOUS */
  int      _rotation_is_identity;
  int      _position_relative_is_zero;
  _class_Arm_parameters _parameters;
};
typedef struct _struct_Arm _class_Arm;
_class_Arm _sample_pos_var;
#pragma acc declare create ( _sample_pos_var )

/* component cryostat_wall=Union_cylinder() [13] DECLARE */
/* Parameter definition for component type 'Union_cylinder' */
struct _struct_Union_cylinder_parameters {
  /* Component type 'Union_cylinder' setting parameters */
  char material_string[16384];
  MCNUM priority;
  MCNUM radius;
  MCNUM yheight;
  MCNUM visualize;
  int target_index;
  MCNUM target_x;
  MCNUM target_y;
  MCNUM target_z;
  MCNUM focus_aw;
  MCNUM focus_ah;
  MCNUM focus_xw;
  MCNUM focus_xh;
  MCNUM focus_r;
  MCNUM p_interact;
  char mask_string[16384];
  char mask_setting[16384];
  MCNUM number_of_activations;
  char curved_surface[16384];
  char top_surface[16384];
  char bottom_surface[16384];
  char all_face_surface[16384];
  char cut_surface[16384];
  char init[16384];
  /* Component type 'Union_cylinder' private parameters */
  struct global_geometry_element_struct  global_geometry_element;
  int  loop_index;
  int  loop_2_index;
  int  material_index;
  struct Volume_struct  this_cylinder_volume;
  struct cylinder_storage  this_cylinder_storage;
  struct surface_stack_struct  curved_surface_stack;
  struct surface_stack_struct  top_surface_stack;
  struct surface_stack_struct  bottom_surface_stack;
  struct surface_stack_struct  cut_surface_stack;
}; /* _struct_Union_cylinder_parameters */
typedef struct _struct_Union_cylinder_parameters _class_Union_cylinder_parameters;

/* Parameters for component type 'Union_cylinder' */
struct _struct_Union_cylinder {
  char     _name[256]; /* e.g. cryostat_wall */
  char     _type[256]; /* Union_cylinder */
  long     _index; /* e.g. 2 index in TRACE list */
  Coords   _position_absolute;
  Coords   _position_relative; /* wrt PREVIOUS */
  Rotation _rotation_absolute;
  Rotation _rotation_relative; /* wrt PREVIOUS */
  int      _rotation_is_identity;
  int      _position_relative_is_zero;
  _class_Union_cylinder_parameters _parameters;
};
typedef struct _struct_Union_cylinder _class_Union_cylinder;
_class_Union_cylinder _cryostat_wall_var;
#pragma acc declare create ( _cryostat_wall_var )

_class_Union_cylinder _cryostat_wall_vacuum_var;
#pragma acc declare create ( _cryostat_wall_vacuum_var )

/* component sample_sphere=Union_sphere() [15] DECLARE */
/* Parameter definition for component type 'Union_sphere' */
struct _struct_Union_sphere_parameters {
  /* Component type 'Union_sphere' setting parameters */
  char material_string[16384];
  MCNUM priority;
  MCNUM radius;
  MCNUM visualize;
  int target_index;
  MCNUM target_x;
  MCNUM target_y;
  MCNUM target_z;
  MCNUM focus_aw;
  MCNUM focus_ah;
  MCNUM focus_xw;
  MCNUM focus_xh;
  MCNUM focus_r;
  MCNUM p_interact;
  char mask_string[16384];
  char mask_setting[16384];
  MCNUM number_of_activations;
  char surface[16384];
  char cut_surface[16384];
  char init[16384];
  /* Component type 'Union_sphere' private parameters */
  struct global_geometry_element_struct  global_geometry_element;
  int  loop_index;
  int  loop_2_index;
  int  material_index;
  struct surface_stack_struct  surface_stack;
  struct surface_stack_struct  cut_surface_stack;
  struct Volume_struct  this_sphere_volume;
  struct sphere_storage  this_sphere_storage;
}; /* _struct_Union_sphere_parameters */
typedef struct _struct_Union_sphere_parameters _class_Union_sphere_parameters;

/* Parameters for component type 'Union_sphere' */
struct _struct_Union_sphere {
  char     _name[256]; /* e.g. sample_sphere */
  char     _type[256]; /* Union_sphere */
  long     _index; /* e.g. 2 index in TRACE list */
  Coords   _position_absolute;
  Coords   _position_relative; /* wrt PREVIOUS */
  Rotation _rotation_absolute;
  Rotation _rotation_relative; /* wrt PREVIOUS */
  int      _rotation_is_identity;
  int      _position_relative_is_zero;
  _class_Union_sphere_parameters _parameters;
};
typedef struct _struct_Union_sphere _class_Union_sphere;
_class_Union_sphere _sample_sphere_var;
#pragma acc declare create ( _sample_sphere_var )

/* component sample_box=Union_box() [16] DECLARE */
/* Parameter definition for component type 'Union_box' */
struct _struct_Union_box_parameters {
  /* Component type 'Union_box' setting parameters */
  char material_string[16384];
  MCNUM priority;
  MCNUM xwidth;
  MCNUM yheight;
  MCNUM zdepth;
  MCNUM xwidth2;
  MCNUM yheight2;
  MCNUM visualize;
  int target_index;
  MCNUM target_x;
  MCNUM target_y;
  MCNUM target_z;
  MCNUM focus_aw;
  MCNUM focus_ah;
  MCNUM focus_xw;
  MCNUM focus_xh;
  MCNUM focus_r;
  char plus_z_surface[16384];
  char minus_z_surface[16384];
  char plus_x_surface[16384];
  char minus_x_surface[16384];
  char plus_y_surface[16384];
  char minus_y_surface[16384];
  char all_face_surface[16384];
  char cut_surface[16384];
  MCNUM p_interact;
  char mask_string[16384];
  char mask_setting[16384];
  MCNUM number_of_activations;
  char init[16384];
  /* Component type 'Union_box' private parameters */
  struct global_geometry_element_struct  global_geometry_element;
  int  loop_index;
  double  x_component;
  double  y_component;
  double  z_component;
  struct Volume_struct  this_box_volume;
  struct box_storage  this_box_storage;
  struct surface_stack_struct  plus_x_surface_stack;
  struct surface_stack_struct  minus_x_surface_stack;
  struct surface_stack_struct  plus_y_surface_stack;
  struct surface_stack_struct  minus_y_surface_stack;
  struct surface_stack_struct  plus_z_surface_stack;
  struct surface_stack_struct  minus_z_surface_stack;
  struct surface_stack_struct  cut_surface_stack;
}; /* _struct_Union_box_parameters */
typedef struct _struct_Union_box_parameters _class_Union_box_parameters;

/* Parameters for component type 'Union_box' */
struct _struct_Union_box {
  char     _name[256]; /* e.g. sample_box */
  char     _type[256]; /* Union_box */
  long     _index; /* e.g. 2 index in TRACE list */
  Coords   _position_absolute;
  Coords   _position_relative; /* wrt PREVIOUS */
  Rotation _rotation_absolute;
  Rotation _rotation_relative; /* wrt PREVIOUS */
  int      _rotation_is_identity;
  int      _position_relative_is_zero;
  _class_Union_box_parameters _parameters;
};
typedef struct _struct_Union_box _class_Union_box;
_class_Union_box _sample_box_var;
#pragma acc declare create ( _sample_box_var )

/* component sample_cone=Union_cone() [17] DECLARE */
/* Parameter definition for component type 'Union_cone' */
struct _struct_Union_cone_parameters {
  /* Component type 'Union_cone' setting parameters */
  char material_string[16384];
  MCNUM priority;
  MCNUM radius;
  MCNUM radius_top;
  MCNUM radius_bottom;
  MCNUM yheight;
  MCNUM visualize;
  int target_index;
  MCNUM target_x;
  MCNUM target_y;
  MCNUM target_z;
  MCNUM focus_aw;
  MCNUM focus_ah;
  MCNUM focus_xw;
  MCNUM focus_xh;
  MCNUM focus_r;
  MCNUM p_interact;
  char mask_string[16384];
  char mask_setting[16384];
  MCNUM number_of_activations;
  char curved_surface[16384];
  char top_surface[16384];
  char bottom_surface[16384];
  char all_face_surface[16384];
  char cut_surface[16384];
  char init[16384];
  /* Component type 'Union_cone' private parameters */
  struct global_geometry_element_struct  global_geometry_element;
  int  loop_index;
  int  loop_2_index;
  int  material_index;
  struct Volume_struct  this_cone_volume;
  struct cone_storage  this_cone_storage;
  struct surface_stack_struct  curved_surface_stack;
  struct surface_stack_struct  top_surface_stack;
  struct surface_stack_struct  bottom_surface_stack;
  struct surface_stack_struct  cut_surface_stack;
}; /* _struct_Union_cone_parameters */
typedef struct _struct_Union_cone_parameters _class_Union_cone_parameters;

/* Parameters for component type 'Union_cone' */
struct _struct_Union_cone {
  char     _name[256]; /* e.g. sample_cone */
  char     _type[256]; /* Union_cone */
  long     _index; /* e.g. 2 index in TRACE list */
  Coords   _position_absolute;
  Coords   _position_relative; /* wrt PREVIOUS */
  Rotation _rotation_absolute;
  Rotation _rotation_relative; /* wrt PREVIOUS */
  int      _rotation_is_identity;
  int      _position_relative_is_zero;
  _class_Union_cone_parameters _parameters;
};
typedef struct _struct_Union_cone _class_Union_cone;
_class_Union_cone _sample_cone_var;
#pragma acc declare create ( _sample_cone_var )

/* component test_logger_1D=Union_logger_1D() [18] DECLARE */
/* Parameter definition for component type 'Union_logger_1D' */
struct _struct_Union_logger_1D_parameters {
  /* Component type 'Union_logger_1D' setting parameters */
  char target_geometry[16384];
  char target_process[16384];
  MCNUM min_value;
  MCNUM max_value;
  MCNUM n1;
  char variable[16384];
  char filename[16384];
  MCNUM order_total;
  MCNUM order_volume;
  MCNUM order_volume_process;
  MCNUM logger_conditional_extend_index;
  char init[16384];
  /* Component type 'Union_logger_1D' private parameters */
  int  loop_index;
  int  found_process;
  int  specified_processes;
  char  local_string[256];
  struct pointer_to_1d_int_list  accepted_processes;
  struct global_logger_element_struct  logger_list_element;
  struct pointer_to_1d_int_list  accepted_volumes;
  struct logger_struct  this_logger;
  struct a_1D_storage_struct  this_storage;
  struct loggers_struct*  loggers_on_target_volume;
  struct Volume_struct*  target_volume;
  char  temp_string[2];
}; /* _struct_Union_logger_1D_parameters */
typedef struct _struct_Union_logger_1D_parameters _class_Union_logger_1D_parameters;

/* Parameters for component type 'Union_logger_1D' */
struct _struct_Union_logger_1D {
  char     _name[256]; /* e.g. test_logger_1D */
  char     _type[256]; /* Union_logger_1D */
  long     _index; /* e.g. 2 index in TRACE list */
  Coords   _position_absolute;
  Coords   _position_relative; /* wrt PREVIOUS */
  Rotation _rotation_absolute;
  Rotation _rotation_relative; /* wrt PREVIOUS */
  int      _rotation_is_identity;
  int      _position_relative_is_zero;
  _class_Union_logger_1D_parameters _parameters;
};
typedef struct _struct_Union_logger_1D _class_Union_logger_1D;
_class_Union_logger_1D _test_logger_1D_var;
#pragma acc declare create ( _test_logger_1D_var )

/* component test_logger_2D_space_zx=Union_logger_2D_space() [19] DECLARE */
/* Parameter definition for component type 'Union_logger_2D_space' */
struct _struct_Union_logger_2D_space_parameters {
  /* Component type 'Union_logger_2D_space' setting parameters */
  char target_geometry[16384];
  char target_process[16384];
  char D_direction_1[16384];
  MCNUM D1_min;
  MCNUM D1_max;
  MCNUM n1;
  char D_direction_2[16384];
  MCNUM D2_min;
  MCNUM D2_max;
  MCNUM n2;
  char filename[16384];
  MCNUM order_total;
  MCNUM order_volume;
  MCNUM order_volume_process;
  MCNUM logger_conditional_extend_index;
  char init[16384];
  /* Component type 'Union_logger_2D_space' private parameters */
  int  loop_index;
  int  found_process;
  int  specified_processes;
  char  local_string[256];
  struct pointer_to_1d_int_list  accepted_processes;
  struct global_logger_element_struct  logger_list_element;
  struct pointer_to_1d_int_list  accepted_volumes;
  struct logger_struct  this_logger;
  struct a_2DS_storage_struct  this_storage;
  struct loggers_struct*  loggers_on_target_volume;
  struct Volume_struct*  target_volume;
}; /* _struct_Union_logger_2D_space_parameters */
typedef struct _struct_Union_logger_2D_space_parameters _class_Union_logger_2D_space_parameters;

/* Parameters for component type 'Union_logger_2D_space' */
struct _struct_Union_logger_2D_space {
  char     _name[256]; /* e.g. test_logger_2D_space_zx */
  char     _type[256]; /* Union_logger_2D_space */
  long     _index; /* e.g. 2 index in TRACE list */
  Coords   _position_absolute;
  Coords   _position_relative; /* wrt PREVIOUS */
  Rotation _rotation_absolute;
  Rotation _rotation_relative; /* wrt PREVIOUS */
  int      _rotation_is_identity;
  int      _position_relative_is_zero;
  _class_Union_logger_2D_space_parameters _parameters;
};
typedef struct _struct_Union_logger_2D_space _class_Union_logger_2D_space;
_class_Union_logger_2D_space _test_logger_2D_space_zx_var;
#pragma acc declare create ( _test_logger_2D_space_zx_var )

_class_Union_logger_2D_space _test_logger_2D_space_zy_var;
#pragma acc declare create ( _test_logger_2D_space_zy_var )

_class_Arm _scat_direction_var;
#pragma acc declare create ( _scat_direction_var )

_class_Union_logger_2D_space _test_logger_2D_space_zx_con_time_var;
#pragma acc declare create ( _test_logger_2D_space_zx_con_time_var )

/* component test_conditional_standard=Union_conditional_standard() [23] DECLARE */
/* Parameter definition for component type 'Union_conditional_standard' */
struct _struct_Union_conditional_standard_parameters {
  /* Component type 'Union_conditional_standard' setting parameters */
  char target_loggers[16384];
  int master_tagging;
  int tagging_extend_index;
  MCNUM time_min;
  MCNUM time_max;
  MCNUM E_min;
  MCNUM E_max;
  MCNUM total_order_min;
  MCNUM total_order_max;
  MCNUM last_volume_index;
  MCNUM overwrite_logger_weight;
  char init[16384];
  /* Component type 'Union_conditional_standard' private parameters */
  int  loop_index;
  int  return_value;
  struct pointer_to_1d_int_list  accepted_loggers_all;
  struct pointer_to_1d_int_list  accepted_loggers_specific;
  struct pointer_to_1d_int_list  accepted_abs_loggers_all;
  struct pointer_to_1d_int_list  accepted_abs_loggers_specific;
  struct logger_struct*  found_logger;
  struct abs_logger_struct*  found_abs_logger;
  struct conditional_standard_struct  this_conditional_data;
  union conditional_data_union  this_data_union;
  struct global_tagging_conditional_element_struct*  new_tagging_element;
}; /* _struct_Union_conditional_standard_parameters */
typedef struct _struct_Union_conditional_standard_parameters _class_Union_conditional_standard_parameters;

/* Parameters for component type 'Union_conditional_standard' */
struct _struct_Union_conditional_standard {
  char     _name[256]; /* e.g. test_conditional_standard */
  char     _type[256]; /* Union_conditional_standard */
  long     _index; /* e.g. 2 index in TRACE list */
  Coords   _position_absolute;
  Coords   _position_relative; /* wrt PREVIOUS */
  Rotation _rotation_absolute;
  Rotation _rotation_relative; /* wrt PREVIOUS */
  int      _rotation_is_identity;
  int      _position_relative_is_zero;
  _class_Union_conditional_standard_parameters _parameters;
};
typedef struct _struct_Union_conditional_standard _class_Union_conditional_standard;
_class_Union_conditional_standard _test_conditional_standard_var;
#pragma acc declare create ( _test_conditional_standard_var )

_class_Union_logger_2D_space _test_logger_2D_space_zx_con_PSD_var;
#pragma acc declare create ( _test_logger_2D_space_zx_con_PSD_var )

/* component test_conditional_PSD=Union_conditional_PSD() [25] DECLARE */
/* Parameter definition for component type 'Union_conditional_PSD' */
struct _struct_Union_conditional_PSD_parameters {
  /* Component type 'Union_conditional_PSD' setting parameters */
  char target_loggers[16384];
  int master_tagging;
  int tagging_extend_index;
  MCNUM xwidth;
  MCNUM yheight;
  MCNUM time_min;
  MCNUM time_max;
  MCNUM overwrite_logger_weight;
  char init[16384];
  /* Component type 'Union_conditional_PSD' private parameters */
  int  loop_index;
  int  return_value;
  struct pointer_to_1d_int_list  accepted_loggers_all;
  struct pointer_to_1d_int_list  accepted_loggers_specific;
  struct pointer_to_1d_int_list  accepted_abs_loggers_all;
  struct pointer_to_1d_int_list  accepted_abs_loggers_specific;
  struct logger_struct*  found_logger;
  struct abs_logger_struct*  found_abs_logger;
  struct conditional_PSD_struct  this_conditional_data;
  union conditional_data_union  this_data_union;
  struct global_tagging_conditional_element_struct*  new_tagging_element;
}; /* _struct_Union_conditional_PSD_parameters */
typedef struct _struct_Union_conditional_PSD_parameters _class_Union_conditional_PSD_parameters;

/* Parameters for component type 'Union_conditional_PSD' */
struct _struct_Union_conditional_PSD {
  char     _name[256]; /* e.g. test_conditional_PSD */
  char     _type[256]; /* Union_conditional_PSD */
  long     _index; /* e.g. 2 index in TRACE list */
  Coords   _position_absolute;
  Coords   _position_relative; /* wrt PREVIOUS */
  Rotation _rotation_absolute;
  Rotation _rotation_relative; /* wrt PREVIOUS */
  int      _rotation_is_identity;
  int      _position_relative_is_zero;
  _class_Union_conditional_PSD_parameters _parameters;
};
typedef struct _struct_Union_conditional_PSD _class_Union_conditional_PSD;
_class_Union_conditional_PSD _test_conditional_PSD_var;
#pragma acc declare create ( _test_conditional_PSD_var )

/* component sample=Union_master() [26] DECLARE */
/* Parameter definition for component type 'Union_master' */
struct _struct_Union_master_parameters {
  /* Component type 'Union_master' setting parameters */
  int enable_refraction;
  int enable_reflection;
  MCNUM verbal;
  MCNUM list_verbal;
  MCNUM finally_verbal;
  MCNUM allow_inside_start;
  MCNUM enable_tagging;
  MCNUM history_limit;
  MCNUM enable_conditionals;
  MCNUM inherit_number_of_scattering_events;
  MCNUM weight_ratio_limit;
  char init[16384];
  /* Component type 'Union_master' private parameters */
  struct global_positions_to_transform_list_struct*  global_positions_to_transform_list_master;
  struct global_rotations_to_transform_list_struct*  global_rotations_to_transform_list_master;
  struct pointer_to_global_process_list*  global_process_list_master;
  struct pointer_to_global_material_list*  global_material_list_master;
  struct pointer_to_global_surface_list*  global_surface_list_master;
  struct pointer_to_global_geometry_list*  global_geometry_list_master;
  struct pointer_to_global_logger_list*  global_all_volume_logger_list_master;
  struct pointer_to_global_logger_list*  global_specific_volumes_logger_list_master;
  struct pointer_to_global_abs_logger_list*  global_all_volume_abs_logger_list_master;
  struct pointer_to_global_abs_logger_list*  global_specific_volumes_abs_logger_list_master;
  struct global_tagging_conditional_list_struct*  global_tagging_conditional_list_master;
  struct pointer_to_global_master_list*  global_master_list_master;
  int  starting_volume_warning;
  struct global_master_element_struct  global_master_element;
  int  this_global_master_index;
  int  previous_master_index;
  int  geometry_list_index;
  struct intersection_time_table_struct  intersection_time_table;
  struct Volume_struct**  Volumes;
  struct geometry_struct**  Geometries;
  struct Volume_struct**  Volume_copies;
  struct starting_lists_struct  starting_lists;
  struct pointer_to_1d_int_list  Volume_copies_allocated;
  double  r[3];
  double  r_start[3];
  double  v[3];
  int  error_msg;
  int  component_error_msg;
  char  string_output[128];
  int  number_of_volumes;
  int  volume_index;
  int  process_index;
  int  iterator;
  int  solutions;
  int  max_number_of_processes;
  int  limit;
  int  solution;
  int  min_solution;
  int  ignore_closest;
  int  ignore_surface_index;
  int  min_volume;
  int  time_found;
  double  intersection_time;
  double  min_intersection_time;
  struct scattering_process_struct*  process;
  struct scattering_process_struct*  process_start;
  double*  my_trace;
  double*  p_my_trace;
  double*  my_trace_fraction_control;
  double  k[3];
  double  k_new[3];
  double  k_old[3];
  double  k_rotated[3];
  double  v_length;
  double  my_sum;
  double  my_sum_plus_abs;
  double  culmative_probability;
  double  mc_prop;
  double  time_to_scattering;
  double  length_to_scattering;
  double  length_to_boundary;
  double  time_to_boundery;
  int  selected_process;
  int  scattering_event;
  double  time_propagated_without_scattering;
  int  a_next_volume_found;
  int  next_volume;
  double  next_volume_priority;
  int  done;
  int  current_volume;
  int  previous_volume;
  int  ray_sucseeded;
  int*  number_of_solutions;
  int  number_of_solutions_static;
  int*  check;
  int*  start;
  int  intersection_with_children;
  int  geometry_output;
  int  tree_next_volume;
  int*  pre_allocated1;
  int*  pre_allocated2;
  int*  pre_allocated3;
  Coords  ray_position;
  Coords  ray_velocity;
  Coords  ray_velocity_rotated;
  Coords  ray_velocity_final;
  Coords  wavevector;
  Coords  wavevector_rotated;
  int  volume_0_found;
  int*  scattered_flag;
  int**  scattered_flag_VP;
  Rotation  master_transposed_rotation_matrix;
  Rotation  temp_rotation_matrix;
  Rotation  temp_transpose_rotation_matrix;
  Coords  non_rotated_position;
  Coords  rotated_position;
  int  non_isotropic_found;
  struct list_of_tagging_tree_node_pointers  master_tagging_node_list;
  struct tagging_tree_node_struct*  current_tagging_node;
  int  tagging_leaf_counter;
  int  stop_tagging_ray;
  int  stop_creating_nodes;
  int  number_of_scattering_events;
  double  real_transmission_probability;
  double  mc_transmission_probability;
  int  number_of_process_interacts_set;
  int  index_of_lacking_process;
  double  total_process_interact;
  struct pointer_to_1d_int_list  geometry_component_index_list;
  struct pointer_to_1d_int_list  mask_volume_index_list;
  int  number_of_masks;
  int  number_of_masked_volumes;
  struct pointer_to_1d_int_list  mask_status_list;
  struct pointer_to_1d_int_list  current_mask_intersect_list_status;
  int  mask_index_main;
  int  mask_iterator;
  int*  mask_start;
  int*  mask_check;
  int  need_to_run_within_which_volume;
  int*  number_of_processes_array;
  double  p_old;
  int  log_index;
  int  conditional_status;
  struct logger_struct*  this_logger;
  struct abs_logger_struct*  this_abs_logger;
  struct conditional_list_struct*  tagging_conditional_list;
  int*  logger_conditional_extend_array;
  int*  abs_logger_conditional_extend_array;
  int  max_conditional_extend_index;
  int  tagging_conditional_extend;
  int  free_tagging_conditioanl_list;
  double  safety_distance;
  double  safety_distance2;
  struct focus_data_struct  temporary_focus_data;
  struct focus_data_struct*  this_focus_data;
  int  focus_data_index;
  double  r_old[3];
  double  initial_weight;
  double  abs_weight_factor;
  double  time_old;
  int  absorption_index;
  int  abs_weight_factor_set;
  double  my_abs;
  struct abs_event  absorption_event_data[1000];
  Coords  abs_position;
  Coords  transformed_abs_position;
  double  t_abs_propagation;
  double  abs_distance;
  double  abs_max_length;
  int  longest_surface_stack;
  struct surface_stack_struct  interface_stack;
}; /* _struct_Union_master_parameters */
typedef struct _struct_Union_master_parameters _class_Union_master_parameters;

/* Parameters for component type 'Union_master' */
struct _struct_Union_master {
  char     _name[256]; /* e.g. sample */
  char     _type[256]; /* Union_master */
  long     _index; /* e.g. 2 index in TRACE list */
  Coords   _position_absolute;
  Coords   _position_relative; /* wrt PREVIOUS */
  Rotation _rotation_absolute;
  Rotation _rotation_relative; /* wrt PREVIOUS */
  int      _rotation_is_identity;
  int      _position_relative_is_zero;
  _class_Union_master_parameters _parameters;
};
typedef struct _struct_Union_master _class_Union_master;
_class_Union_master _sample_var;
#pragma acc declare create ( _sample_var )

/* component detector=PSD_monitor() [27] DECLARE */
/* Parameter definition for component type 'PSD_monitor' */
struct _struct_PSD_monitor_parameters {
  /* Component type 'PSD_monitor' setting parameters */
  int nx;
  int ny;
  char filename[16384];
  MCNUM xmin;
  MCNUM xmax;
  MCNUM ymin;
  MCNUM ymax;
  MCNUM xwidth;
  MCNUM yheight;
  int restore_neutron;
  int nowritefile;
  /* Component type 'PSD_monitor' private parameters */
  DArray2d  PSD_N;
  DArray2d  PSD_p;
  DArray2d  PSD_p2;
}; /* _struct_PSD_monitor_parameters */
typedef struct _struct_PSD_monitor_parameters _class_PSD_monitor_parameters;

/* Parameters for component type 'PSD_monitor' */
struct _struct_PSD_monitor {
  char     _name[256]; /* e.g. detector */
  char     _type[256]; /* PSD_monitor */
  long     _index; /* e.g. 2 index in TRACE list */
  Coords   _position_absolute;
  Coords   _position_relative; /* wrt PREVIOUS */
  Rotation _rotation_absolute;
  Rotation _rotation_relative; /* wrt PREVIOUS */
  int      _rotation_is_identity;
  int      _position_relative_is_zero;
  _class_PSD_monitor_parameters _parameters;
};
typedef struct _struct_PSD_monitor _class_PSD_monitor;
_class_PSD_monitor _detector_var;
#pragma acc declare create ( _detector_var )

_class_PSD_monitor _detector_scat_var;
#pragma acc declare create ( _detector_scat_var )

/* component stop=Union_stop() [29] DECLARE */
/* Parameter definition for component type 'Union_stop' */
struct _struct_Union_stop_parameters {
  char Union_stop_has_no_parameters;
}; /* _struct_Union_stop_parameters */
typedef struct _struct_Union_stop_parameters _class_Union_stop_parameters;

/* Parameters for component type 'Union_stop' */
struct _struct_Union_stop {
  char     _name[256]; /* e.g. stop */
  char     _type[256]; /* Union_stop */
  long     _index; /* e.g. 2 index in TRACE list */
  Coords   _position_absolute;
  Coords   _position_relative; /* wrt PREVIOUS */
  Rotation _rotation_absolute;
  Rotation _rotation_relative; /* wrt PREVIOUS */
  int      _rotation_is_identity;
  int      _position_relative_is_zero;
  _class_Union_stop_parameters _parameters;
};
typedef struct _struct_Union_stop _class_Union_stop;
_class_Union_stop _stop_var;
#pragma acc declare create ( _stop_var )

int mcNUMCOMP = 29;

/* User declarations from instrument definition. Can define functions. */

#undef compcurname
#undef compcurtype
#undef compcurindex
/* end of instrument 'Conditional_test' and components DECLARE */

/* *****************************************************************************
* instrument 'Conditional_test' and components INITIALISE
***************************************************************************** */

double index_getdistance(int first_index, int second_index)
/* Calculate the distance two components from their indexes*/
{
  return coords_len(coords_sub(POS_A_COMP_INDEX(first_index), POS_A_COMP_INDEX(second_index)));
}

double getdistance(char* first_component, char* second_component)
/* Calculate the distance between two named components */
{
  int first_index = _getcomp_index(first_component);
  int second_index = _getcomp_index(second_component);
  return index_getdistance(first_index, second_index);
}

double checked_setpos_getdistance(int current_index, char* first_component, char* second_component)
/* Calculate the distance between two named components at *_setpos() time, with component index checking */
{
  int first_index = _getcomp_index(first_component);
  int second_index = _getcomp_index(second_component);
  if (first_index >= current_index || second_index >= current_index) {
    printf("setpos_getdistance can only be used with the names of components before the current one!\n");
    return 0;
  }
  return index_getdistance(first_index, second_index);
}
#define setpos_getdistance(first, second) checked_setpos_getdistance(current_setpos_index, first, second)

/* component init=Union_init() SETTING, POSITION/ROTATION */
int _init_setpos(void)
{ /* sets initial component parameters, position and rotation */
  SIG_MESSAGE("[_init_setpos] component init=Union_init() SETTING [Union_init:0]");
  stracpy(_init_var._name, "init", 16384);
  stracpy(_init_var._type, "Union_init", 16384);
  _init_var._index=1;
  int current_setpos_index = 1;

  /* component init=Union_init() AT ROTATED */
  {
    Coords tc1, tc2;
    tc1 = coords_set(0,0,0);
    tc2 = coords_set(0,0,0);
    Rotation tr1;
    rot_set_rotation(tr1,0,0,0);
    rot_set_rotation(_init_var._rotation_absolute,
      (0.0)*DEG2RAD, (0.0)*DEG2RAD, (0.0)*DEG2RAD);
    rot_copy(_init_var._rotation_relative, _init_var._rotation_absolute);
    _init_var._rotation_is_identity =  rot_test_identity(_init_var._rotation_relative);
    _init_var._position_absolute = coords_set(
      0, 0, 0);
    tc1 = coords_neg(_init_var._position_absolute);
    _init_var._position_relative = rot_apply(_init_var._rotation_absolute, tc1);
  } /* init=Union_init() AT ROTATED */
  DEBUG_COMPONENT("init", _init_var._position_absolute, _init_var._rotation_absolute);
  instrument->_position_absolute[1] = _init_var._position_absolute;
  instrument->_position_relative[1] = _init_var._position_relative;
    _init_var._position_relative_is_zero =  coords_test_zero(_init_var._position_relative);
  instrument->counter_N[1]  = instrument->counter_P[1] = instrument->counter_P2[1] = 0;
  instrument->counter_AbsorbProp[1]= 0;
  #ifdef USE_NEXUS
  if(nxhandle) {
    if ((!mcdotrace) && mcformat && strcasestr(mcformat, "NeXus")) {
    MPI_MASTER(
        mccomp_placement_type_nexus(nxhandle,"0000_init", _init_var._position_absolute, _init_var._rotation_absolute, "Union_init");
      );
    }
  } else {
    // fprintf(stderr,"NO NEXUS FILE");
  }
  #endif
  return(0);
} /* _init_setpos */

/* component Vanadium_incoherent=Incoherent_process() SETTING, POSITION/ROTATION */
int _Vanadium_incoherent_setpos(void)
{ /* sets initial component parameters, position and rotation */
  SIG_MESSAGE("[_Vanadium_incoherent_setpos] component Vanadium_incoherent=Incoherent_process() SETTING [Incoherent_process:0]");
  stracpy(_Vanadium_incoherent_var._name, "Vanadium_incoherent", 16384);
  stracpy(_Vanadium_incoherent_var._type, "Incoherent_process", 16384);
  _Vanadium_incoherent_var._index=2;
  int current_setpos_index = 2;
  _Vanadium_incoherent_var._parameters.sigma = 2 * 5.08;
  _Vanadium_incoherent_var._parameters.f_QE = 0;
  _Vanadium_incoherent_var._parameters.gamma = 0;
  _Vanadium_incoherent_var._parameters.packing_factor = 1;
  _Vanadium_incoherent_var._parameters.unit_cell_volume = 27.66;
  _Vanadium_incoherent_var._parameters.interact_fraction = -1;
  if("init" && strlen("init"))
    stracpy(_Vanadium_incoherent_var._parameters.init, "init" ? "init" : "", 16384);
  else 
  _Vanadium_incoherent_var._parameters.init[0]='\0';


  /* component Vanadium_incoherent=Incoherent_process() AT ROTATED */
  {
    Coords tc1, tc2;
    tc1 = coords_set(0,0,0);
    tc2 = coords_set(0,0,0);
    Rotation tr1;
    rot_set_rotation(tr1,0,0,0);
    rot_set_rotation(_Vanadium_incoherent_var._rotation_absolute,
      (0.0)*DEG2RAD, (0.0)*DEG2RAD, (0.0)*DEG2RAD);
    rot_copy(_Vanadium_incoherent_var._rotation_relative, _Vanadium_incoherent_var._rotation_absolute);
    _Vanadium_incoherent_var._rotation_is_identity =  rot_test_identity(_Vanadium_incoherent_var._rotation_relative);
    _Vanadium_incoherent_var._position_absolute = coords_set(
      0, 0, 0);
    tc1 = coords_neg(_Vanadium_incoherent_var._position_absolute);
    _Vanadium_incoherent_var._position_relative = rot_apply(_Vanadium_incoherent_var._rotation_absolute, tc1);
  } /* Vanadium_incoherent=Incoherent_process() AT ROTATED */
  DEBUG_COMPONENT("Vanadium_incoherent", _Vanadium_incoherent_var._position_absolute, _Vanadium_incoherent_var._rotation_absolute);
  instrument->_position_absolute[2] = _Vanadium_incoherent_var._position_absolute;
  instrument->_position_relative[2] = _Vanadium_incoherent_var._position_relative;
    _Vanadium_incoherent_var._position_relative_is_zero =  coords_test_zero(_Vanadium_incoherent_var._position_relative);
  instrument->counter_N[2]  = instrument->counter_P[2] = instrument->counter_P2[2] = 0;
  instrument->counter_AbsorbProp[2]= 0;
  #ifdef USE_NEXUS
  if(nxhandle) {
    if ((!mcdotrace) && mcformat && strcasestr(mcformat, "NeXus")) {
    MPI_MASTER(
        mccomp_placement_type_nexus(nxhandle,"0001_Vanadium_incoherent", _Vanadium_incoherent_var._position_absolute, _Vanadium_incoherent_var._rotation_absolute, "Incoherent_process");
        mccomp_param_nexus(nxhandle,"0001_Vanadium_incoherent", "sigma", "5.08", "2 * 5.08","MCNUM");
        mccomp_param_nexus(nxhandle,"0001_Vanadium_incoherent", "f_QE", "0", "0","MCNUM");
        mccomp_param_nexus(nxhandle,"0001_Vanadium_incoherent", "gamma", "0", "0","MCNUM");
        mccomp_param_nexus(nxhandle,"0001_Vanadium_incoherent", "packing_factor", "1", "1","MCNUM");
        mccomp_param_nexus(nxhandle,"0001_Vanadium_incoherent", "unit_cell_volume", "13.8", "27.66","MCNUM");
        mccomp_param_nexus(nxhandle,"0001_Vanadium_incoherent", "interact_fraction", "-1", "-1","MCNUM");
        mccomp_param_nexus(nxhandle,"0001_Vanadium_incoherent", "init", "init", "init", "char*");
      );
    }
  } else {
    // fprintf(stderr,"NO NEXUS FILE");
  }
  #endif
  return(0);
} /* _Vanadium_incoherent_setpos */

/* component Vanadium=Union_make_material() SETTING, POSITION/ROTATION */
int _Vanadium_setpos(void)
{ /* sets initial component parameters, position and rotation */
  SIG_MESSAGE("[_Vanadium_setpos] component Vanadium=Union_make_material() SETTING [Union_make_material:0]");
  stracpy(_Vanadium_var._name, "Vanadium", 16384);
  stracpy(_Vanadium_var._type, "Union_make_material", 16384);
  _Vanadium_var._index=3;
  int current_setpos_index = 3;
  if("Vanadium_incoherent" && strlen("Vanadium_incoherent"))
    stracpy(_Vanadium_var._parameters.process_string, "Vanadium_incoherent" ? "Vanadium_incoherent" : "", 16384);
  else 
  _Vanadium_var._parameters.process_string[0]='\0';
  _Vanadium_var._parameters.my_absorption = 2 * 5.08 * 100 / 27.66;
  _Vanadium_var._parameters.absorber = 0;
  _Vanadium_var._parameters.refraction_density = 0;
  _Vanadium_var._parameters.refraction_sigma_coh = 0;
  _Vanadium_var._parameters.refraction_weight = 0;
  _Vanadium_var._parameters.refraction_SLD = -1500;
  if("init" && strlen("init"))
    stracpy(_Vanadium_var._parameters.init, "init" ? "init" : "", 16384);
  else 
  _Vanadium_var._parameters.init[0]='\0';


  /* component Vanadium=Union_make_material() AT ROTATED */
  {
    Coords tc1, tc2;
    tc1 = coords_set(0,0,0);
    tc2 = coords_set(0,0,0);
    Rotation tr1;
    rot_set_rotation(tr1,0,0,0);
    rot_set_rotation(_Vanadium_var._rotation_absolute,
      (0.0)*DEG2RAD, (0.0)*DEG2RAD, (0.0)*DEG2RAD);
    rot_copy(_Vanadium_var._rotation_relative, _Vanadium_var._rotation_absolute);
    _Vanadium_var._rotation_is_identity =  rot_test_identity(_Vanadium_var._rotation_relative);
    _Vanadium_var._position_absolute = coords_set(
      0, 0, 0);
    tc1 = coords_neg(_Vanadium_var._position_absolute);
    _Vanadium_var._position_relative = rot_apply(_Vanadium_var._rotation_absolute, tc1);
  } /* Vanadium=Union_make_material() AT ROTATED */
  DEBUG_COMPONENT("Vanadium", _Vanadium_var._position_absolute, _Vanadium_var._rotation_absolute);
  instrument->_position_absolute[3] = _Vanadium_var._position_absolute;
  instrument->_position_relative[3] = _Vanadium_var._position_relative;
    _Vanadium_var._position_relative_is_zero =  coords_test_zero(_Vanadium_var._position_relative);
  instrument->counter_N[3]  = instrument->counter_P[3] = instrument->counter_P2[3] = 0;
  instrument->counter_AbsorbProp[3]= 0;
  #ifdef USE_NEXUS
  if(nxhandle) {
    if ((!mcdotrace) && mcformat && strcasestr(mcformat, "NeXus")) {
    MPI_MASTER(
        mccomp_placement_type_nexus(nxhandle,"0002_Vanadium", _Vanadium_var._position_absolute, _Vanadium_var._rotation_absolute, "Union_make_material");
        mccomp_param_nexus(nxhandle,"0002_Vanadium", "process_string", "NULL", "Vanadium_incoherent", "char*");
        mccomp_param_nexus(nxhandle,"0002_Vanadium", "my_absorption", "NONE", "2 * 5.08 * 100 / 27.66","MCNUM");
        mccomp_param_nexus(nxhandle,"0002_Vanadium", "absorber", "0", "0","MCNUM");
        mccomp_param_nexus(nxhandle,"0002_Vanadium", "refraction_density", "0", "0","MCNUM");
        mccomp_param_nexus(nxhandle,"0002_Vanadium", "refraction_sigma_coh", "0", "0","MCNUM");
        mccomp_param_nexus(nxhandle,"0002_Vanadium", "refraction_weight", "0", "0","MCNUM");
        mccomp_param_nexus(nxhandle,"0002_Vanadium", "refraction_SLD", "-1500", "-1500","MCNUM");
        mccomp_param_nexus(nxhandle,"0002_Vanadium", "init", "init", "init", "char*");
      );
    }
  } else {
    // fprintf(stderr,"NO NEXUS FILE");
  }
  #endif
  return(0);
} /* _Vanadium_setpos */

/* component Al_incoherent=Incoherent_process() SETTING, POSITION/ROTATION */
int _Al_incoherent_setpos(void)
{ /* sets initial component parameters, position and rotation */
  SIG_MESSAGE("[_Al_incoherent_setpos] component Al_incoherent=Incoherent_process() SETTING [Incoherent_process:0]");
  stracpy(_Al_incoherent_var._name, "Al_incoherent", 16384);
  stracpy(_Al_incoherent_var._type, "Incoherent_process", 16384);
  _Al_incoherent_var._index=4;
  int current_setpos_index = 4;
  _Al_incoherent_var._parameters.sigma = 4 * 0.0082;
  _Al_incoherent_var._parameters.f_QE = 0;
  _Al_incoherent_var._parameters.gamma = 0;
  _Al_incoherent_var._parameters.packing_factor = 1;
  _Al_incoherent_var._parameters.unit_cell_volume = 66.4;
  _Al_incoherent_var._parameters.interact_fraction = -1;
  if("init" && strlen("init"))
    stracpy(_Al_incoherent_var._parameters.init, "init" ? "init" : "", 16384);
  else 
  _Al_incoherent_var._parameters.init[0]='\0';


  /* component Al_incoherent=Incoherent_process() AT ROTATED */
  {
    Coords tc1, tc2;
    tc1 = coords_set(0,0,0);
    tc2 = coords_set(0,0,0);
    Rotation tr1;
    rot_set_rotation(tr1,0,0,0);
    rot_set_rotation(_Al_incoherent_var._rotation_absolute,
      (0.0)*DEG2RAD, (0.0)*DEG2RAD, (0.0)*DEG2RAD);
    rot_copy(_Al_incoherent_var._rotation_relative, _Al_incoherent_var._rotation_absolute);
    _Al_incoherent_var._rotation_is_identity =  rot_test_identity(_Al_incoherent_var._rotation_relative);
    _Al_incoherent_var._position_absolute = coords_set(
      0, 0, 0);
    tc1 = coords_neg(_Al_incoherent_var._position_absolute);
    _Al_incoherent_var._position_relative = rot_apply(_Al_incoherent_var._rotation_absolute, tc1);
  } /* Al_incoherent=Incoherent_process() AT ROTATED */
  DEBUG_COMPONENT("Al_incoherent", _Al_incoherent_var._position_absolute, _Al_incoherent_var._rotation_absolute);
  instrument->_position_absolute[4] = _Al_incoherent_var._position_absolute;
  instrument->_position_relative[4] = _Al_incoherent_var._position_relative;
    _Al_incoherent_var._position_relative_is_zero =  coords_test_zero(_Al_incoherent_var._position_relative);
  instrument->counter_N[4]  = instrument->counter_P[4] = instrument->counter_P2[4] = 0;
  instrument->counter_AbsorbProp[4]= 0;
  #ifdef USE_NEXUS
  if(nxhandle) {
    if ((!mcdotrace) && mcformat && strcasestr(mcformat, "NeXus")) {
    MPI_MASTER(
        mccomp_placement_type_nexus(nxhandle,"0003_Al_incoherent", _Al_incoherent_var._position_absolute, _Al_incoherent_var._rotation_absolute, "Incoherent_process");
        mccomp_param_nexus(nxhandle,"0003_Al_incoherent", "sigma", "5.08", "4 * 0.0082","MCNUM");
        mccomp_param_nexus(nxhandle,"0003_Al_incoherent", "f_QE", "0", "0","MCNUM");
        mccomp_param_nexus(nxhandle,"0003_Al_incoherent", "gamma", "0", "0","MCNUM");
        mccomp_param_nexus(nxhandle,"0003_Al_incoherent", "packing_factor", "1", "1","MCNUM");
        mccomp_param_nexus(nxhandle,"0003_Al_incoherent", "unit_cell_volume", "13.8", "66.4","MCNUM");
        mccomp_param_nexus(nxhandle,"0003_Al_incoherent", "interact_fraction", "-1", "-1","MCNUM");
        mccomp_param_nexus(nxhandle,"0003_Al_incoherent", "init", "init", "init", "char*");
      );
    }
  } else {
    // fprintf(stderr,"NO NEXUS FILE");
  }
  #endif
  return(0);
} /* _Al_incoherent_setpos */

/* component Al_Powder=Powder_process() SETTING, POSITION/ROTATION */
int _Al_Powder_setpos(void)
{ /* sets initial component parameters, position and rotation */
  SIG_MESSAGE("[_Al_Powder_setpos] component Al_Powder=Powder_process() SETTING [Powder_process:0]");
  stracpy(_Al_Powder_var._name, "Al_Powder", 16384);
  stracpy(_Al_Powder_var._type, "Powder_process", 16384);
  _Al_Powder_var._index=5;
  int current_setpos_index = 5;
  if("Al.laz" && strlen("Al.laz"))
    stracpy(_Al_Powder_var._parameters.reflections, "Al.laz" ? "Al.laz" : "", 16384);
  else 
  _Al_Powder_var._parameters.reflections[0]='\0';
  _Al_Powder_var._parameters.packing_factor = 1;
  _Al_Powder_var._parameters.Vc = 0;
  _Al_Powder_var._parameters.delta_d_d = 0;
  _Al_Powder_var._parameters.DW = 0;
  _Al_Powder_var._parameters.nb_atoms = 1;
  _Al_Powder_var._parameters.d_phi = 0;
  _Al_Powder_var._parameters.density = 0;
  _Al_Powder_var._parameters.weight = 0;
  _Al_Powder_var._parameters.barns = 1;
  _Al_Powder_var._parameters.Strain = 0;
  _Al_Powder_var._parameters.interact_fraction = -1;
  _Al_Powder_var._parameters.format[0] = 0;
  _Al_Powder_var._parameters.format[1] = 0;
  _Al_Powder_var._parameters.format[2] = 0;
  _Al_Powder_var._parameters.format[3] = 0;
  _Al_Powder_var._parameters.format[4] = 0;
  _Al_Powder_var._parameters.format[5] = 0;
  _Al_Powder_var._parameters.format[6] = 0;
  _Al_Powder_var._parameters.format[7] = 0;
  _Al_Powder_var._parameters.format[8] = 0;
  if("init" && strlen("init"))
    stracpy(_Al_Powder_var._parameters.init, "init" ? "init" : "", 16384);
  else 
  _Al_Powder_var._parameters.init[0]='\0';


  /* component Al_Powder=Powder_process() AT ROTATED */
  {
    Coords tc1, tc2;
    tc1 = coords_set(0,0,0);
    tc2 = coords_set(0,0,0);
    Rotation tr1;
    rot_set_rotation(tr1,0,0,0);
    rot_set_rotation(_Al_Powder_var._rotation_absolute,
      (0.0)*DEG2RAD, (0.0)*DEG2RAD, (0.0)*DEG2RAD);
    rot_copy(_Al_Powder_var._rotation_relative, _Al_Powder_var._rotation_absolute);
    _Al_Powder_var._rotation_is_identity =  rot_test_identity(_Al_Powder_var._rotation_relative);
    _Al_Powder_var._position_absolute = coords_set(
      0, 0, 0);
    tc1 = coords_neg(_Al_Powder_var._position_absolute);
    _Al_Powder_var._position_relative = rot_apply(_Al_Powder_var._rotation_absolute, tc1);
  } /* Al_Powder=Powder_process() AT ROTATED */
  DEBUG_COMPONENT("Al_Powder", _Al_Powder_var._position_absolute, _Al_Powder_var._rotation_absolute);
  instrument->_position_absolute[5] = _Al_Powder_var._position_absolute;
  instrument->_position_relative[5] = _Al_Powder_var._position_relative;
    _Al_Powder_var._position_relative_is_zero =  coords_test_zero(_Al_Powder_var._position_relative);
  instrument->counter_N[5]  = instrument->counter_P[5] = instrument->counter_P2[5] = 0;
  instrument->counter_AbsorbProp[5]= 0;
  #ifdef USE_NEXUS
  if(nxhandle) {
    if ((!mcdotrace) && mcformat && strcasestr(mcformat, "NeXus")) {
    MPI_MASTER(
        mccomp_placement_type_nexus(nxhandle,"0004_Al_Powder", _Al_Powder_var._position_absolute, _Al_Powder_var._rotation_absolute, "Powder_process");
        mccomp_param_nexus(nxhandle,"0004_Al_Powder", "reflections", "NULL", "Al.laz", "char*");
        mccomp_param_nexus(nxhandle,"0004_Al_Powder", "packing_factor", "1", "1","MCNUM");
        mccomp_param_nexus(nxhandle,"0004_Al_Powder", "Vc", "0", "0","MCNUM");
        mccomp_param_nexus(nxhandle,"0004_Al_Powder", "delta_d_d", "0", "0","MCNUM");
        mccomp_param_nexus(nxhandle,"0004_Al_Powder", "DW", "0", "0","MCNUM");
        mccomp_param_nexus(nxhandle,"0004_Al_Powder", "nb_atoms", "1", "1","MCNUM");
        mccomp_param_nexus(nxhandle,"0004_Al_Powder", "d_phi", "0", "0","MCNUM");
        mccomp_param_nexus(nxhandle,"0004_Al_Powder", "density", "0", "0","MCNUM");
        mccomp_param_nexus(nxhandle,"0004_Al_Powder", "weight", "0", "0","MCNUM");
        mccomp_param_nexus(nxhandle,"0004_Al_Powder", "barns", "1", "1","MCNUM");
        mccomp_param_nexus(nxhandle,"0004_Al_Powder", "Strain", "0", "0","MCNUM");
        mccomp_param_nexus(nxhandle,"0004_Al_Powder", "interact_fraction", "-1", "-1","MCNUM");
        mccomp_param_nexus(nxhandle,"0004_Al_Powder", "format", "{ 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 }", "{ 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 }","MCNUM");
        mccomp_param_nexus(nxhandle,"0004_Al_Powder", "init", "init", "init", "char*");
      );
    }
  } else {
    // fprintf(stderr,"NO NEXUS FILE");
  }
  #endif
  return(0);
} /* _Al_Powder_setpos */

/* component Al=Union_make_material() SETTING, POSITION/ROTATION */
int _Al_setpos(void)
{ /* sets initial component parameters, position and rotation */
  SIG_MESSAGE("[_Al_setpos] component Al=Union_make_material() SETTING [Union_make_material:0]");
  stracpy(_Al_var._name, "Al", 16384);
  stracpy(_Al_var._type, "Union_make_material", 16384);
  _Al_var._index=6;
  int current_setpos_index = 6;
  if("NULL" && strlen("NULL"))
    stracpy(_Al_var._parameters.process_string, "NULL" ? "NULL" : "", 16384);
  else 
  _Al_var._parameters.process_string[0]='\0';
  _Al_var._parameters.my_absorption = 100 * 4 * 0.231 / 66.4;
  _Al_var._parameters.absorber = 0;
  _Al_var._parameters.refraction_density = 0;
  _Al_var._parameters.refraction_sigma_coh = 0;
  _Al_var._parameters.refraction_weight = 0;
  _Al_var._parameters.refraction_SLD = -1500;
  if("init" && strlen("init"))
    stracpy(_Al_var._parameters.init, "init" ? "init" : "", 16384);
  else 
  _Al_var._parameters.init[0]='\0';


  /* component Al=Union_make_material() AT ROTATED */
  {
    Coords tc1, tc2;
    tc1 = coords_set(0,0,0);
    tc2 = coords_set(0,0,0);
    Rotation tr1;
    rot_set_rotation(tr1,0,0,0);
    rot_set_rotation(_Al_var._rotation_absolute,
      (0.0)*DEG2RAD, (0.0)*DEG2RAD, (0.0)*DEG2RAD);
    rot_copy(_Al_var._rotation_relative, _Al_var._rotation_absolute);
    _Al_var._rotation_is_identity =  rot_test_identity(_Al_var._rotation_relative);
    _Al_var._position_absolute = coords_set(
      0, 0, 0);
    tc1 = coords_neg(_Al_var._position_absolute);
    _Al_var._position_relative = rot_apply(_Al_var._rotation_absolute, tc1);
  } /* Al=Union_make_material() AT ROTATED */
  DEBUG_COMPONENT("Al", _Al_var._position_absolute, _Al_var._rotation_absolute);
  instrument->_position_absolute[6] = _Al_var._position_absolute;
  instrument->_position_relative[6] = _Al_var._position_relative;
    _Al_var._position_relative_is_zero =  coords_test_zero(_Al_var._position_relative);
  instrument->counter_N[6]  = instrument->counter_P[6] = instrument->counter_P2[6] = 0;
  instrument->counter_AbsorbProp[6]= 0;
  #ifdef USE_NEXUS
  if(nxhandle) {
    if ((!mcdotrace) && mcformat && strcasestr(mcformat, "NeXus")) {
    MPI_MASTER(
        mccomp_placement_type_nexus(nxhandle,"0005_Al", _Al_var._position_absolute, _Al_var._rotation_absolute, "Union_make_material");
        mccomp_param_nexus(nxhandle,"0005_Al", "process_string", "NULL", "NULL", "char*");
        mccomp_param_nexus(nxhandle,"0005_Al", "my_absorption", "NONE", "100 * 4 * 0.231 / 66.4","MCNUM");
        mccomp_param_nexus(nxhandle,"0005_Al", "absorber", "0", "0","MCNUM");
        mccomp_param_nexus(nxhandle,"0005_Al", "refraction_density", "0", "0","MCNUM");
        mccomp_param_nexus(nxhandle,"0005_Al", "refraction_sigma_coh", "0", "0","MCNUM");
        mccomp_param_nexus(nxhandle,"0005_Al", "refraction_weight", "0", "0","MCNUM");
        mccomp_param_nexus(nxhandle,"0005_Al", "refraction_SLD", "-1500", "-1500","MCNUM");
        mccomp_param_nexus(nxhandle,"0005_Al", "init", "init", "init", "char*");
      );
    }
  } else {
    // fprintf(stderr,"NO NEXUS FILE");
  }
  #endif
  return(0);
} /* _Al_setpos */

/* component Cu_incoherent=Incoherent_process() SETTING, POSITION/ROTATION */
int _Cu_incoherent_setpos(void)
{ /* sets initial component parameters, position and rotation */
  SIG_MESSAGE("[_Cu_incoherent_setpos] component Cu_incoherent=Incoherent_process() SETTING [Incoherent_process:0]");
  stracpy(_Cu_incoherent_var._name, "Cu_incoherent", 16384);
  stracpy(_Cu_incoherent_var._type, "Incoherent_process", 16384);
  _Cu_incoherent_var._index=7;
  int current_setpos_index = 7;
  _Cu_incoherent_var._parameters.sigma = 4 * 0.55;
  _Cu_incoherent_var._parameters.f_QE = 0;
  _Cu_incoherent_var._parameters.gamma = 0;
  _Cu_incoherent_var._parameters.packing_factor = 1;
  _Cu_incoherent_var._parameters.unit_cell_volume = 47.22;
  _Cu_incoherent_var._parameters.interact_fraction = -1;
  if("init" && strlen("init"))
    stracpy(_Cu_incoherent_var._parameters.init, "init" ? "init" : "", 16384);
  else 
  _Cu_incoherent_var._parameters.init[0]='\0';


  /* component Cu_incoherent=Incoherent_process() AT ROTATED */
  {
    Coords tc1, tc2;
    tc1 = coords_set(0,0,0);
    tc2 = coords_set(0,0,0);
    Rotation tr1;
    rot_set_rotation(tr1,0,0,0);
    rot_set_rotation(_Cu_incoherent_var._rotation_absolute,
      (0.0)*DEG2RAD, (0.0)*DEG2RAD, (0.0)*DEG2RAD);
    rot_copy(_Cu_incoherent_var._rotation_relative, _Cu_incoherent_var._rotation_absolute);
    _Cu_incoherent_var._rotation_is_identity =  rot_test_identity(_Cu_incoherent_var._rotation_relative);
    _Cu_incoherent_var._position_absolute = coords_set(
      0, 0, 0);
    tc1 = coords_neg(_Cu_incoherent_var._position_absolute);
    _Cu_incoherent_var._position_relative = rot_apply(_Cu_incoherent_var._rotation_absolute, tc1);
  } /* Cu_incoherent=Incoherent_process() AT ROTATED */
  DEBUG_COMPONENT("Cu_incoherent", _Cu_incoherent_var._position_absolute, _Cu_incoherent_var._rotation_absolute);
  instrument->_position_absolute[7] = _Cu_incoherent_var._position_absolute;
  instrument->_position_relative[7] = _Cu_incoherent_var._position_relative;
    _Cu_incoherent_var._position_relative_is_zero =  coords_test_zero(_Cu_incoherent_var._position_relative);
  instrument->counter_N[7]  = instrument->counter_P[7] = instrument->counter_P2[7] = 0;
  instrument->counter_AbsorbProp[7]= 0;
  #ifdef USE_NEXUS
  if(nxhandle) {
    if ((!mcdotrace) && mcformat && strcasestr(mcformat, "NeXus")) {
    MPI_MASTER(
        mccomp_placement_type_nexus(nxhandle,"0006_Cu_incoherent", _Cu_incoherent_var._position_absolute, _Cu_incoherent_var._rotation_absolute, "Incoherent_process");
        mccomp_param_nexus(nxhandle,"0006_Cu_incoherent", "sigma", "5.08", "4 * 0.55","MCNUM");
        mccomp_param_nexus(nxhandle,"0006_Cu_incoherent", "f_QE", "0", "0","MCNUM");
        mccomp_param_nexus(nxhandle,"0006_Cu_incoherent", "gamma", "0", "0","MCNUM");
        mccomp_param_nexus(nxhandle,"0006_Cu_incoherent", "packing_factor", "1", "1","MCNUM");
        mccomp_param_nexus(nxhandle,"0006_Cu_incoherent", "unit_cell_volume", "13.8", "47.22","MCNUM");
        mccomp_param_nexus(nxhandle,"0006_Cu_incoherent", "interact_fraction", "-1", "-1","MCNUM");
        mccomp_param_nexus(nxhandle,"0006_Cu_incoherent", "init", "init", "init", "char*");
      );
    }
  } else {
    // fprintf(stderr,"NO NEXUS FILE");
  }
  #endif
  return(0);
} /* _Cu_incoherent_setpos */

/* component Cu_Powder=Powder_process() SETTING, POSITION/ROTATION */
int _Cu_Powder_setpos(void)
{ /* sets initial component parameters, position and rotation */
  SIG_MESSAGE("[_Cu_Powder_setpos] component Cu_Powder=Powder_process() SETTING [Powder_process:0]");
  stracpy(_Cu_Powder_var._name, "Cu_Powder", 16384);
  stracpy(_Cu_Powder_var._type, "Powder_process", 16384);
  _Cu_Powder_var._index=8;
  int current_setpos_index = 8;
  if("Cu.laz" && strlen("Cu.laz"))
    stracpy(_Cu_Powder_var._parameters.reflections, "Cu.laz" ? "Cu.laz" : "", 16384);
  else 
  _Cu_Powder_var._parameters.reflections[0]='\0';
  _Cu_Powder_var._parameters.packing_factor = 1;
  _Cu_Powder_var._parameters.Vc = 0;
  _Cu_Powder_var._parameters.delta_d_d = 0;
  _Cu_Powder_var._parameters.DW = 0;
  _Cu_Powder_var._parameters.nb_atoms = 1;
  _Cu_Powder_var._parameters.d_phi = 0;
  _Cu_Powder_var._parameters.density = 0;
  _Cu_Powder_var._parameters.weight = 0;
  _Cu_Powder_var._parameters.barns = 1;
  _Cu_Powder_var._parameters.Strain = 0;
  _Cu_Powder_var._parameters.interact_fraction = -1;
  _Cu_Powder_var._parameters.format[0] = 0;
  _Cu_Powder_var._parameters.format[1] = 0;
  _Cu_Powder_var._parameters.format[2] = 0;
  _Cu_Powder_var._parameters.format[3] = 0;
  _Cu_Powder_var._parameters.format[4] = 0;
  _Cu_Powder_var._parameters.format[5] = 0;
  _Cu_Powder_var._parameters.format[6] = 0;
  _Cu_Powder_var._parameters.format[7] = 0;
  _Cu_Powder_var._parameters.format[8] = 0;
  if("init" && strlen("init"))
    stracpy(_Cu_Powder_var._parameters.init, "init" ? "init" : "", 16384);
  else 
  _Cu_Powder_var._parameters.init[0]='\0';


  /* component Cu_Powder=Powder_process() AT ROTATED */
  {
    Coords tc1, tc2;
    tc1 = coords_set(0,0,0);
    tc2 = coords_set(0,0,0);
    Rotation tr1;
    rot_set_rotation(tr1,0,0,0);
    rot_set_rotation(_Cu_Powder_var._rotation_absolute,
      (0.0)*DEG2RAD, (0.0)*DEG2RAD, (0.0)*DEG2RAD);
    rot_copy(_Cu_Powder_var._rotation_relative, _Cu_Powder_var._rotation_absolute);
    _Cu_Powder_var._rotation_is_identity =  rot_test_identity(_Cu_Powder_var._rotation_relative);
    _Cu_Powder_var._position_absolute = coords_set(
      0, 0, 0);
    tc1 = coords_neg(_Cu_Powder_var._position_absolute);
    _Cu_Powder_var._position_relative = rot_apply(_Cu_Powder_var._rotation_absolute, tc1);
  } /* Cu_Powder=Powder_process() AT ROTATED */
  DEBUG_COMPONENT("Cu_Powder", _Cu_Powder_var._position_absolute, _Cu_Powder_var._rotation_absolute);
  instrument->_position_absolute[8] = _Cu_Powder_var._position_absolute;
  instrument->_position_relative[8] = _Cu_Powder_var._position_relative;
    _Cu_Powder_var._position_relative_is_zero =  coords_test_zero(_Cu_Powder_var._position_relative);
  instrument->counter_N[8]  = instrument->counter_P[8] = instrument->counter_P2[8] = 0;
  instrument->counter_AbsorbProp[8]= 0;
  #ifdef USE_NEXUS
  if(nxhandle) {
    if ((!mcdotrace) && mcformat && strcasestr(mcformat, "NeXus")) {
    MPI_MASTER(
        mccomp_placement_type_nexus(nxhandle,"0007_Cu_Powder", _Cu_Powder_var._position_absolute, _Cu_Powder_var._rotation_absolute, "Powder_process");
        mccomp_param_nexus(nxhandle,"0007_Cu_Powder", "reflections", "NULL", "Cu.laz", "char*");
        mccomp_param_nexus(nxhandle,"0007_Cu_Powder", "packing_factor", "1", "1","MCNUM");
        mccomp_param_nexus(nxhandle,"0007_Cu_Powder", "Vc", "0", "0","MCNUM");
        mccomp_param_nexus(nxhandle,"0007_Cu_Powder", "delta_d_d", "0", "0","MCNUM");
        mccomp_param_nexus(nxhandle,"0007_Cu_Powder", "DW", "0", "0","MCNUM");
        mccomp_param_nexus(nxhandle,"0007_Cu_Powder", "nb_atoms", "1", "1","MCNUM");
        mccomp_param_nexus(nxhandle,"0007_Cu_Powder", "d_phi", "0", "0","MCNUM");
        mccomp_param_nexus(nxhandle,"0007_Cu_Powder", "density", "0", "0","MCNUM");
        mccomp_param_nexus(nxhandle,"0007_Cu_Powder", "weight", "0", "0","MCNUM");
        mccomp_param_nexus(nxhandle,"0007_Cu_Powder", "barns", "1", "1","MCNUM");
        mccomp_param_nexus(nxhandle,"0007_Cu_Powder", "Strain", "0", "0","MCNUM");
        mccomp_param_nexus(nxhandle,"0007_Cu_Powder", "interact_fraction", "-1", "-1","MCNUM");
        mccomp_param_nexus(nxhandle,"0007_Cu_Powder", "format", "{ 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 }", "{ 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 }","MCNUM");
        mccomp_param_nexus(nxhandle,"0007_Cu_Powder", "init", "init", "init", "char*");
      );
    }
  } else {
    // fprintf(stderr,"NO NEXUS FILE");
  }
  #endif
  return(0);
} /* _Cu_Powder_setpos */

/* component Cu=Union_make_material() SETTING, POSITION/ROTATION */
int _Cu_setpos(void)
{ /* sets initial component parameters, position and rotation */
  SIG_MESSAGE("[_Cu_setpos] component Cu=Union_make_material() SETTING [Union_make_material:0]");
  stracpy(_Cu_var._name, "Cu", 16384);
  stracpy(_Cu_var._type, "Union_make_material", 16384);
  _Cu_var._index=9;
  int current_setpos_index = 9;
  if("NULL" && strlen("NULL"))
    stracpy(_Cu_var._parameters.process_string, "NULL" ? "NULL" : "", 16384);
  else 
  _Cu_var._parameters.process_string[0]='\0';
  _Cu_var._parameters.my_absorption = 5 * 100 * 4 * 3.78 / 47.22;
  _Cu_var._parameters.absorber = 0;
  _Cu_var._parameters.refraction_density = 0;
  _Cu_var._parameters.refraction_sigma_coh = 0;
  _Cu_var._parameters.refraction_weight = 0;
  _Cu_var._parameters.refraction_SLD = -1500;
  if("init" && strlen("init"))
    stracpy(_Cu_var._parameters.init, "init" ? "init" : "", 16384);
  else 
  _Cu_var._parameters.init[0]='\0';


  /* component Cu=Union_make_material() AT ROTATED */
  {
    Coords tc1, tc2;
    tc1 = coords_set(0,0,0);
    tc2 = coords_set(0,0,0);
    Rotation tr1;
    rot_set_rotation(tr1,0,0,0);
    rot_set_rotation(_Cu_var._rotation_absolute,
      (0.0)*DEG2RAD, (0.0)*DEG2RAD, (0.0)*DEG2RAD);
    rot_copy(_Cu_var._rotation_relative, _Cu_var._rotation_absolute);
    _Cu_var._rotation_is_identity =  rot_test_identity(_Cu_var._rotation_relative);
    _Cu_var._position_absolute = coords_set(
      0, 0, 0);
    tc1 = coords_neg(_Cu_var._position_absolute);
    _Cu_var._position_relative = rot_apply(_Cu_var._rotation_absolute, tc1);
  } /* Cu=Union_make_material() AT ROTATED */
  DEBUG_COMPONENT("Cu", _Cu_var._position_absolute, _Cu_var._rotation_absolute);
  instrument->_position_absolute[9] = _Cu_var._position_absolute;
  instrument->_position_relative[9] = _Cu_var._position_relative;
    _Cu_var._position_relative_is_zero =  coords_test_zero(_Cu_var._position_relative);
  instrument->counter_N[9]  = instrument->counter_P[9] = instrument->counter_P2[9] = 0;
  instrument->counter_AbsorbProp[9]= 0;
  #ifdef USE_NEXUS
  if(nxhandle) {
    if ((!mcdotrace) && mcformat && strcasestr(mcformat, "NeXus")) {
    MPI_MASTER(
        mccomp_placement_type_nexus(nxhandle,"0008_Cu", _Cu_var._position_absolute, _Cu_var._rotation_absolute, "Union_make_material");
        mccomp_param_nexus(nxhandle,"0008_Cu", "process_string", "NULL", "NULL", "char*");
        mccomp_param_nexus(nxhandle,"0008_Cu", "my_absorption", "NONE", "5 * 100 * 4 * 3.78 / 47.22","MCNUM");
        mccomp_param_nexus(nxhandle,"0008_Cu", "absorber", "0", "0","MCNUM");
        mccomp_param_nexus(nxhandle,"0008_Cu", "refraction_density", "0", "0","MCNUM");
        mccomp_param_nexus(nxhandle,"0008_Cu", "refraction_sigma_coh", "0", "0","MCNUM");
        mccomp_param_nexus(nxhandle,"0008_Cu", "refraction_weight", "0", "0","MCNUM");
        mccomp_param_nexus(nxhandle,"0008_Cu", "refraction_SLD", "-1500", "-1500","MCNUM");
        mccomp_param_nexus(nxhandle,"0008_Cu", "init", "init", "init", "char*");
      );
    }
  } else {
    // fprintf(stderr,"NO NEXUS FILE");
  }
  #endif
  return(0);
} /* _Cu_setpos */

/* component a1=Progress_bar() SETTING, POSITION/ROTATION */
int _a1_setpos(void)
{ /* sets initial component parameters, position and rotation */
  SIG_MESSAGE("[_a1_setpos] component a1=Progress_bar() SETTING [Progress_bar:0]");
  stracpy(_a1_var._name, "a1", 16384);
  stracpy(_a1_var._type, "Progress_bar", 16384);
  _a1_var._index=10;
  int current_setpos_index = 10;
  if("NULL" && strlen("NULL"))
    stracpy(_a1_var._parameters.profile, "NULL" ? "NULL" : "", 16384);
  else 
  _a1_var._parameters.profile[0]='\0';
  _a1_var._parameters.percent = 10;
  _a1_var._parameters.flag_save = 0;
  _a1_var._parameters.minutes = 0;


  /* component a1=Progress_bar() AT ROTATED */
  {
    Coords tc1, tc2;
    tc1 = coords_set(0,0,0);
    tc2 = coords_set(0,0,0);
    Rotation tr1;
    rot_set_rotation(tr1,0,0,0);
    rot_set_rotation(_a1_var._rotation_absolute,
      (0.0)*DEG2RAD, (0.0)*DEG2RAD, (0.0)*DEG2RAD);
    rot_copy(_a1_var._rotation_relative, _a1_var._rotation_absolute);
    _a1_var._rotation_is_identity =  rot_test_identity(_a1_var._rotation_relative);
    _a1_var._position_absolute = coords_set(
      0, 0, 0);
    tc1 = coords_neg(_a1_var._position_absolute);
    _a1_var._position_relative = rot_apply(_a1_var._rotation_absolute, tc1);
  } /* a1=Progress_bar() AT ROTATED */
  DEBUG_COMPONENT("a1", _a1_var._position_absolute, _a1_var._rotation_absolute);
  instrument->_position_absolute[10] = _a1_var._position_absolute;
  instrument->_position_relative[10] = _a1_var._position_relative;
    _a1_var._position_relative_is_zero =  coords_test_zero(_a1_var._position_relative);
  instrument->counter_N[10]  = instrument->counter_P[10] = instrument->counter_P2[10] = 0;
  instrument->counter_AbsorbProp[10]= 0;
  #ifdef USE_NEXUS
  if(nxhandle) {
    if ((!mcdotrace) && mcformat && strcasestr(mcformat, "NeXus")) {
    MPI_MASTER(
        mccomp_placement_type_nexus(nxhandle,"0009_a1", _a1_var._position_absolute, _a1_var._rotation_absolute, "Progress_bar");
        mccomp_param_nexus(nxhandle,"0009_a1", "profile", "NULL", "NULL", "char*");
        mccomp_param_nexus(nxhandle,"0009_a1", "percent", "10", "10","MCNUM");
        mccomp_param_nexus(nxhandle,"0009_a1", "flag_save", "0", "0","MCNUM");
        mccomp_param_nexus(nxhandle,"0009_a1", "minutes", "0", "0","MCNUM");
      );
    }
  } else {
    // fprintf(stderr,"NO NEXUS FILE");
  }
  #endif
  return(0);
} /* _a1_setpos */

/* component source=Source_div() SETTING, POSITION/ROTATION */
int _source_setpos(void)
{ /* sets initial component parameters, position and rotation */
  SIG_MESSAGE("[_source_setpos] component source=Source_div() SETTING [Source_div:0]");
  stracpy(_source_var._name, "source", 16384);
  stracpy(_source_var._type, "Source_div", 16384);
  _source_var._index=11;
  int current_setpos_index = 11;
  _source_var._parameters.xwidth = 0.09;
  _source_var._parameters.yheight = 0.15;
  _source_var._parameters.focus_aw = 0.01;
  _source_var._parameters.focus_ah = 0.01;
  _source_var._parameters.E0 = 5;
  _source_var._parameters.dE = 0;
  _source_var._parameters.lambda0 = 0.0;
  _source_var._parameters.dlambda = 0.0;
  _source_var._parameters.gauss = 0;
  _source_var._parameters.flux = 1E12;


  /* component source=Source_div() AT ROTATED */
  {
    Coords tc1, tc2;
    tc1 = coords_set(0,0,0);
    tc2 = coords_set(0,0,0);
    Rotation tr1;
    rot_set_rotation(tr1,0,0,0);
    rot_set_rotation(tr1,
      (0)*DEG2RAD, (0)*DEG2RAD, (0)*DEG2RAD);
    rot_mul(tr1, _a1_var._rotation_absolute, _source_var._rotation_absolute);
    rot_transpose(_a1_var._rotation_absolute, tr1);
    rot_mul(_source_var._rotation_absolute, tr1, _source_var._rotation_relative);
    _source_var._rotation_is_identity =  rot_test_identity(_source_var._rotation_relative);
    tc1 = coords_set(
      0, 0, 0);
    rot_transpose(_a1_var._rotation_absolute, tr1);
    tc2 = rot_apply(tr1, tc1);
    _source_var._position_absolute = coords_add(_a1_var._position_absolute, tc2);
    tc1 = coords_sub(_a1_var._position_absolute, _source_var._position_absolute);
    _source_var._position_relative = rot_apply(_source_var._rotation_absolute, tc1);
  } /* source=Source_div() AT ROTATED */
  DEBUG_COMPONENT("source", _source_var._position_absolute, _source_var._rotation_absolute);
  instrument->_position_absolute[11] = _source_var._position_absolute;
  instrument->_position_relative[11] = _source_var._position_relative;
    _source_var._position_relative_is_zero =  coords_test_zero(_source_var._position_relative);
  instrument->counter_N[11]  = instrument->counter_P[11] = instrument->counter_P2[11] = 0;
  instrument->counter_AbsorbProp[11]= 0;
  #ifdef USE_NEXUS
  if(nxhandle) {
    if ((!mcdotrace) && mcformat && strcasestr(mcformat, "NeXus")) {
    MPI_MASTER(
        mccomp_placement_type_nexus(nxhandle,"0010_source", _source_var._position_absolute, _source_var._rotation_absolute, "Source_div");
        mccomp_param_nexus(nxhandle,"0010_source", "xwidth", "NONE", "0.09","MCNUM");
        mccomp_param_nexus(nxhandle,"0010_source", "yheight", "NONE", "0.15","MCNUM");
        mccomp_param_nexus(nxhandle,"0010_source", "focus_aw", "NONE", "0.01","MCNUM");
        mccomp_param_nexus(nxhandle,"0010_source", "focus_ah", "NONE", "0.01","MCNUM");
        mccomp_param_nexus(nxhandle,"0010_source", "E0", "0.0", "5","MCNUM");
        mccomp_param_nexus(nxhandle,"0010_source", "dE", "0.0", "0","MCNUM");
        mccomp_param_nexus(nxhandle,"0010_source", "lambda0", "0.0", "0.0","MCNUM");
        mccomp_param_nexus(nxhandle,"0010_source", "dlambda", "0.0", "0.0","MCNUM");
        mccomp_param_nexus(nxhandle,"0010_source", "gauss", "0", "0","MCNUM");
        mccomp_param_nexus(nxhandle,"0010_source", "flux", "1", "1E12","MCNUM");
      );
    }
  } else {
    // fprintf(stderr,"NO NEXUS FILE");
  }
  #endif
  return(0);
} /* _source_setpos */

/* component sample_pos=Arm() SETTING, POSITION/ROTATION */
int _sample_pos_setpos(void)
{ /* sets initial component parameters, position and rotation */
  SIG_MESSAGE("[_sample_pos_setpos] component sample_pos=Arm() SETTING [Arm:0]");
  stracpy(_sample_pos_var._name, "sample_pos", 16384);
  stracpy(_sample_pos_var._type, "Arm", 16384);
  _sample_pos_var._index=12;
  int current_setpos_index = 12;
  /* component sample_pos=Arm() AT ROTATED */
  {
    Coords tc1, tc2;
    tc1 = coords_set(0,0,0);
    tc2 = coords_set(0,0,0);
    Rotation tr1;
    rot_set_rotation(tr1,0,0,0);
    rot_set_rotation(tr1,
      (0.0)*DEG2RAD, (0.0)*DEG2RAD, (0.0)*DEG2RAD);
    rot_mul(tr1, _source_var._rotation_absolute, _sample_pos_var._rotation_absolute);
    rot_transpose(_source_var._rotation_absolute, tr1);
    rot_mul(_sample_pos_var._rotation_absolute, tr1, _sample_pos_var._rotation_relative);
    _sample_pos_var._rotation_is_identity =  rot_test_identity(_sample_pos_var._rotation_relative);
    tc1 = coords_set(
      0, 0, 0.3);
    rot_transpose(_source_var._rotation_absolute, tr1);
    tc2 = rot_apply(tr1, tc1);
    _sample_pos_var._position_absolute = coords_add(_source_var._position_absolute, tc2);
    tc1 = coords_sub(_source_var._position_absolute, _sample_pos_var._position_absolute);
    _sample_pos_var._position_relative = rot_apply(_sample_pos_var._rotation_absolute, tc1);
  } /* sample_pos=Arm() AT ROTATED */
  DEBUG_COMPONENT("sample_pos", _sample_pos_var._position_absolute, _sample_pos_var._rotation_absolute);
  instrument->_position_absolute[12] = _sample_pos_var._position_absolute;
  instrument->_position_relative[12] = _sample_pos_var._position_relative;
    _sample_pos_var._position_relative_is_zero =  coords_test_zero(_sample_pos_var._position_relative);
  instrument->counter_N[12]  = instrument->counter_P[12] = instrument->counter_P2[12] = 0;
  instrument->counter_AbsorbProp[12]= 0;
  #ifdef USE_NEXUS
  if(nxhandle) {
    if ((!mcdotrace) && mcformat && strcasestr(mcformat, "NeXus")) {
    MPI_MASTER(
        mccomp_placement_type_nexus(nxhandle,"0011_sample_pos", _sample_pos_var._position_absolute, _sample_pos_var._rotation_absolute, "Arm");
      );
    }
  } else {
    // fprintf(stderr,"NO NEXUS FILE");
  }
  #endif
  return(0);
} /* _sample_pos_setpos */

/* component cryostat_wall=Union_cylinder() SETTING, POSITION/ROTATION */
int _cryostat_wall_setpos(void)
{ /* sets initial component parameters, position and rotation */
  SIG_MESSAGE("[_cryostat_wall_setpos] component cryostat_wall=Union_cylinder() SETTING [Union_cylinder:0]");
  stracpy(_cryostat_wall_var._name, "cryostat_wall", 16384);
  stracpy(_cryostat_wall_var._type, "Union_cylinder", 16384);
  _cryostat_wall_var._index=13;
  int current_setpos_index = 13;
  if("Al" && strlen("Al"))
    stracpy(_cryostat_wall_var._parameters.material_string, "Al" ? "Al" : "", 16384);
  else 
  _cryostat_wall_var._parameters.material_string[0]='\0';
  _cryostat_wall_var._parameters.priority = 10;
  _cryostat_wall_var._parameters.radius = 0.07;
  _cryostat_wall_var._parameters.yheight = 0.32;
  _cryostat_wall_var._parameters.visualize = 1;
  _cryostat_wall_var._parameters.target_index = 0;
  _cryostat_wall_var._parameters.target_x = 0;
  _cryostat_wall_var._parameters.target_y = 0;
  _cryostat_wall_var._parameters.target_z = 0;
  _cryostat_wall_var._parameters.focus_aw = 0;
  _cryostat_wall_var._parameters.focus_ah = 0;
  _cryostat_wall_var._parameters.focus_xw = 0;
  _cryostat_wall_var._parameters.focus_xh = 0;
  _cryostat_wall_var._parameters.focus_r = 0;
  _cryostat_wall_var._parameters.p_interact = 0.2;
  _cryostat_wall_var._parameters.mask_string[0]='\0';
  _cryostat_wall_var._parameters.mask_setting[0]='\0';
  _cryostat_wall_var._parameters.number_of_activations = 1;
  _cryostat_wall_var._parameters.curved_surface[0]='\0';
  _cryostat_wall_var._parameters.top_surface[0]='\0';
  _cryostat_wall_var._parameters.bottom_surface[0]='\0';
  _cryostat_wall_var._parameters.all_face_surface[0]='\0';
  _cryostat_wall_var._parameters.cut_surface[0]='\0';
  if("init" && strlen("init"))
    stracpy(_cryostat_wall_var._parameters.init, "init" ? "init" : "", 16384);
  else 
  _cryostat_wall_var._parameters.init[0]='\0';


  /* component cryostat_wall=Union_cylinder() AT ROTATED */
  {
    Coords tc1, tc2;
    tc1 = coords_set(0,0,0);
    tc2 = coords_set(0,0,0);
    Rotation tr1;
    rot_set_rotation(tr1,0,0,0);
    rot_set_rotation(tr1,
      (0)*DEG2RAD, (0)*DEG2RAD, (0)*DEG2RAD);
    rot_mul(tr1, _sample_pos_var._rotation_absolute, _cryostat_wall_var._rotation_absolute);
    rot_transpose(_source_var._rotation_absolute, tr1);
    rot_mul(_cryostat_wall_var._rotation_absolute, tr1, _cryostat_wall_var._rotation_relative);
    _cryostat_wall_var._rotation_is_identity =  rot_test_identity(_cryostat_wall_var._rotation_relative);
    tc1 = coords_set(
      0, 0, 0);
    rot_transpose(_sample_pos_var._rotation_absolute, tr1);
    tc2 = rot_apply(tr1, tc1);
    _cryostat_wall_var._position_absolute = coords_add(_sample_pos_var._position_absolute, tc2);
    tc1 = coords_sub(_source_var._position_absolute, _cryostat_wall_var._position_absolute);
    _cryostat_wall_var._position_relative = rot_apply(_cryostat_wall_var._rotation_absolute, tc1);
  } /* cryostat_wall=Union_cylinder() AT ROTATED */
  DEBUG_COMPONENT("cryostat_wall", _cryostat_wall_var._position_absolute, _cryostat_wall_var._rotation_absolute);
  instrument->_position_absolute[13] = _cryostat_wall_var._position_absolute;
  instrument->_position_relative[13] = _cryostat_wall_var._position_relative;
    _cryostat_wall_var._position_relative_is_zero =  coords_test_zero(_cryostat_wall_var._position_relative);
  instrument->counter_N[13]  = instrument->counter_P[13] = instrument->counter_P2[13] = 0;
  instrument->counter_AbsorbProp[13]= 0;
  #ifdef USE_NEXUS
  if(nxhandle) {
    if ((!mcdotrace) && mcformat && strcasestr(mcformat, "NeXus")) {
    MPI_MASTER(
        mccomp_placement_type_nexus(nxhandle,"0012_cryostat_wall", _cryostat_wall_var._position_absolute, _cryostat_wall_var._rotation_absolute, "Union_cylinder");
        mccomp_param_nexus(nxhandle,"0012_cryostat_wall", "material_string", 0, "Al", "char*");
        mccomp_param_nexus(nxhandle,"0012_cryostat_wall", "priority", "NONE", "10","MCNUM");
        mccomp_param_nexus(nxhandle,"0012_cryostat_wall", "radius", "NONE", "0.07","MCNUM");
        mccomp_param_nexus(nxhandle,"0012_cryostat_wall", "yheight", "NONE", "0.32","MCNUM");
        mccomp_param_nexus(nxhandle,"0012_cryostat_wall", "visualize", "1", "1","MCNUM");
        mccomp_param_nexus(nxhandle,"0012_cryostat_wall", "target_index", "0", "0","int");
        mccomp_param_nexus(nxhandle,"0012_cryostat_wall", "target_x", "0", "0","MCNUM");
        mccomp_param_nexus(nxhandle,"0012_cryostat_wall", "target_y", "0", "0","MCNUM");
        mccomp_param_nexus(nxhandle,"0012_cryostat_wall", "target_z", "0", "0","MCNUM");
        mccomp_param_nexus(nxhandle,"0012_cryostat_wall", "focus_aw", "0", "0","MCNUM");
        mccomp_param_nexus(nxhandle,"0012_cryostat_wall", "focus_ah", "0", "0","MCNUM");
        mccomp_param_nexus(nxhandle,"0012_cryostat_wall", "focus_xw", "0", "0","MCNUM");
        mccomp_param_nexus(nxhandle,"0012_cryostat_wall", "focus_xh", "0", "0","MCNUM");
        mccomp_param_nexus(nxhandle,"0012_cryostat_wall", "focus_r", "0", "0","MCNUM");
        mccomp_param_nexus(nxhandle,"0012_cryostat_wall", "p_interact", "0", "0.2","MCNUM");
        mccomp_param_nexus(nxhandle,"0012_cryostat_wall", "mask_string", 0, 0, "char*");
        mccomp_param_nexus(nxhandle,"0012_cryostat_wall", "mask_setting", 0, 0, "char*");
        mccomp_param_nexus(nxhandle,"0012_cryostat_wall", "number_of_activations", "1", "1","MCNUM");
        mccomp_param_nexus(nxhandle,"0012_cryostat_wall", "curved_surface", 0, 0, "char*");
        mccomp_param_nexus(nxhandle,"0012_cryostat_wall", "top_surface", 0, 0, "char*");
        mccomp_param_nexus(nxhandle,"0012_cryostat_wall", "bottom_surface", 0, 0, "char*");
        mccomp_param_nexus(nxhandle,"0012_cryostat_wall", "all_face_surface", 0, 0, "char*");
        mccomp_param_nexus(nxhandle,"0012_cryostat_wall", "cut_surface", 0, 0, "char*");
        mccomp_param_nexus(nxhandle,"0012_cryostat_wall", "init", "init", "init", "char*");
      );
    }
  } else {
    // fprintf(stderr,"NO NEXUS FILE");
  }
  #endif
  return(0);
} /* _cryostat_wall_setpos */

/* component cryostat_wall_vacuum=Union_cylinder() SETTING, POSITION/ROTATION */
int _cryostat_wall_vacuum_setpos(void)
{ /* sets initial component parameters, position and rotation */
  SIG_MESSAGE("[_cryostat_wall_vacuum_setpos] component cryostat_wall_vacuum=Union_cylinder() SETTING [Union_cylinder:0]");
  stracpy(_cryostat_wall_vacuum_var._name, "cryostat_wall_vacuum", 16384);
  stracpy(_cryostat_wall_vacuum_var._type, "Union_cylinder", 16384);
  _cryostat_wall_vacuum_var._index=14;
  int current_setpos_index = 14;
  if("Vacuum" && strlen("Vacuum"))
    stracpy(_cryostat_wall_vacuum_var._parameters.material_string, "Vacuum" ? "Vacuum" : "", 16384);
  else 
  _cryostat_wall_vacuum_var._parameters.material_string[0]='\0';
  _cryostat_wall_vacuum_var._parameters.priority = 11;
  _cryostat_wall_vacuum_var._parameters.radius = 0.068;
  _cryostat_wall_vacuum_var._parameters.yheight = 0.318;
  _cryostat_wall_vacuum_var._parameters.visualize = 1;
  _cryostat_wall_vacuum_var._parameters.target_index = 0;
  _cryostat_wall_vacuum_var._parameters.target_x = 0;
  _cryostat_wall_vacuum_var._parameters.target_y = 0;
  _cryostat_wall_vacuum_var._parameters.target_z = 0;
  _cryostat_wall_vacuum_var._parameters.focus_aw = 0;
  _cryostat_wall_vacuum_var._parameters.focus_ah = 0;
  _cryostat_wall_vacuum_var._parameters.focus_xw = 0;
  _cryostat_wall_vacuum_var._parameters.focus_xh = 0;
  _cryostat_wall_vacuum_var._parameters.focus_r = 0;
  _cryostat_wall_vacuum_var._parameters.p_interact = 0;
  _cryostat_wall_vacuum_var._parameters.mask_string[0]='\0';
  _cryostat_wall_vacuum_var._parameters.mask_setting[0]='\0';
  _cryostat_wall_vacuum_var._parameters.number_of_activations = 1;
  _cryostat_wall_vacuum_var._parameters.curved_surface[0]='\0';
  _cryostat_wall_vacuum_var._parameters.top_surface[0]='\0';
  _cryostat_wall_vacuum_var._parameters.bottom_surface[0]='\0';
  _cryostat_wall_vacuum_var._parameters.all_face_surface[0]='\0';
  _cryostat_wall_vacuum_var._parameters.cut_surface[0]='\0';
  if("init" && strlen("init"))
    stracpy(_cryostat_wall_vacuum_var._parameters.init, "init" ? "init" : "", 16384);
  else 
  _cryostat_wall_vacuum_var._parameters.init[0]='\0';


  /* component cryostat_wall_vacuum=Union_cylinder() AT ROTATED */
  {
    Coords tc1, tc2;
    tc1 = coords_set(0,0,0);
    tc2 = coords_set(0,0,0);
    Rotation tr1;
    rot_set_rotation(tr1,0,0,0);
    rot_set_rotation(tr1,
      (0)*DEG2RAD, (0)*DEG2RAD, (0)*DEG2RAD);
    rot_mul(tr1, _sample_pos_var._rotation_absolute, _cryostat_wall_vacuum_var._rotation_absolute);
    rot_transpose(_source_var._rotation_absolute, tr1);
    rot_mul(_cryostat_wall_vacuum_var._rotation_absolute, tr1, _cryostat_wall_vacuum_var._rotation_relative);
    _cryostat_wall_vacuum_var._rotation_is_identity =  rot_test_identity(_cryostat_wall_vacuum_var._rotation_relative);
    tc1 = coords_set(
      0, 0, 0);
    rot_transpose(_sample_pos_var._rotation_absolute, tr1);
    tc2 = rot_apply(tr1, tc1);
    _cryostat_wall_vacuum_var._position_absolute = coords_add(_sample_pos_var._position_absolute, tc2);
    tc1 = coords_sub(_source_var._position_absolute, _cryostat_wall_vacuum_var._position_absolute);
    _cryostat_wall_vacuum_var._position_relative = rot_apply(_cryostat_wall_vacuum_var._rotation_absolute, tc1);
  } /* cryostat_wall_vacuum=Union_cylinder() AT ROTATED */
  DEBUG_COMPONENT("cryostat_wall_vacuum", _cryostat_wall_vacuum_var._position_absolute, _cryostat_wall_vacuum_var._rotation_absolute);
  instrument->_position_absolute[14] = _cryostat_wall_vacuum_var._position_absolute;
  instrument->_position_relative[14] = _cryostat_wall_vacuum_var._position_relative;
    _cryostat_wall_vacuum_var._position_relative_is_zero =  coords_test_zero(_cryostat_wall_vacuum_var._position_relative);
  instrument->counter_N[14]  = instrument->counter_P[14] = instrument->counter_P2[14] = 0;
  instrument->counter_AbsorbProp[14]= 0;
  #ifdef USE_NEXUS
  if(nxhandle) {
    if ((!mcdotrace) && mcformat && strcasestr(mcformat, "NeXus")) {
    MPI_MASTER(
        mccomp_placement_type_nexus(nxhandle,"0013_cryostat_wall_vacuum", _cryostat_wall_vacuum_var._position_absolute, _cryostat_wall_vacuum_var._rotation_absolute, "Union_cylinder");
        mccomp_param_nexus(nxhandle,"0013_cryostat_wall_vacuum", "material_string", 0, "Vacuum", "char*");
        mccomp_param_nexus(nxhandle,"0013_cryostat_wall_vacuum", "priority", "NONE", "11","MCNUM");
        mccomp_param_nexus(nxhandle,"0013_cryostat_wall_vacuum", "radius", "NONE", "0.068","MCNUM");
        mccomp_param_nexus(nxhandle,"0013_cryostat_wall_vacuum", "yheight", "NONE", "0.318","MCNUM");
        mccomp_param_nexus(nxhandle,"0013_cryostat_wall_vacuum", "visualize", "1", "1","MCNUM");
        mccomp_param_nexus(nxhandle,"0013_cryostat_wall_vacuum", "target_index", "0", "0","int");
        mccomp_param_nexus(nxhandle,"0013_cryostat_wall_vacuum", "target_x", "0", "0","MCNUM");
        mccomp_param_nexus(nxhandle,"0013_cryostat_wall_vacuum", "target_y", "0", "0","MCNUM");
        mccomp_param_nexus(nxhandle,"0013_cryostat_wall_vacuum", "target_z", "0", "0","MCNUM");
        mccomp_param_nexus(nxhandle,"0013_cryostat_wall_vacuum", "focus_aw", "0", "0","MCNUM");
        mccomp_param_nexus(nxhandle,"0013_cryostat_wall_vacuum", "focus_ah", "0", "0","MCNUM");
        mccomp_param_nexus(nxhandle,"0013_cryostat_wall_vacuum", "focus_xw", "0", "0","MCNUM");
        mccomp_param_nexus(nxhandle,"0013_cryostat_wall_vacuum", "focus_xh", "0", "0","MCNUM");
        mccomp_param_nexus(nxhandle,"0013_cryostat_wall_vacuum", "focus_r", "0", "0","MCNUM");
        mccomp_param_nexus(nxhandle,"0013_cryostat_wall_vacuum", "p_interact", "0", "0","MCNUM");
        mccomp_param_nexus(nxhandle,"0013_cryostat_wall_vacuum", "mask_string", 0, 0, "char*");
        mccomp_param_nexus(nxhandle,"0013_cryostat_wall_vacuum", "mask_setting", 0, 0, "char*");
        mccomp_param_nexus(nxhandle,"0013_cryostat_wall_vacuum", "number_of_activations", "1", "1","MCNUM");
        mccomp_param_nexus(nxhandle,"0013_cryostat_wall_vacuum", "curved_surface", 0, 0, "char*");
        mccomp_param_nexus(nxhandle,"0013_cryostat_wall_vacuum", "top_surface", 0, 0, "char*");
        mccomp_param_nexus(nxhandle,"0013_cryostat_wall_vacuum", "bottom_surface", 0, 0, "char*");
        mccomp_param_nexus(nxhandle,"0013_cryostat_wall_vacuum", "all_face_surface", 0, 0, "char*");
        mccomp_param_nexus(nxhandle,"0013_cryostat_wall_vacuum", "cut_surface", 0, 0, "char*");
        mccomp_param_nexus(nxhandle,"0013_cryostat_wall_vacuum", "init", "init", "init", "char*");
      );
    }
  } else {
    // fprintf(stderr,"NO NEXUS FILE");
  }
  #endif
  return(0);
} /* _cryostat_wall_vacuum_setpos */

/* component sample_sphere=Union_sphere() SETTING, POSITION/ROTATION */
int _sample_sphere_setpos(void)
{ /* sets initial component parameters, position and rotation */
  SIG_MESSAGE("[_sample_sphere_setpos] component sample_sphere=Union_sphere() SETTING [Union_sphere:0]");
  stracpy(_sample_sphere_var._name, "sample_sphere", 16384);
  stracpy(_sample_sphere_var._type, "Union_sphere", 16384);
  _sample_sphere_var._index=15;
  int current_setpos_index = 15;
  if("Cu" && strlen("Cu"))
    stracpy(_sample_sphere_var._parameters.material_string, "Cu" ? "Cu" : "", 16384);
  else 
  _sample_sphere_var._parameters.material_string[0]='\0';
  _sample_sphere_var._parameters.priority = 20;
  _sample_sphere_var._parameters.radius = 0.031;
  _sample_sphere_var._parameters.visualize = 1;
  _sample_sphere_var._parameters.target_index = 0;
  _sample_sphere_var._parameters.target_x = 0;
  _sample_sphere_var._parameters.target_y = 0;
  _sample_sphere_var._parameters.target_z = 0;
  _sample_sphere_var._parameters.focus_aw = 0;
  _sample_sphere_var._parameters.focus_ah = 0;
  _sample_sphere_var._parameters.focus_xw = 0;
  _sample_sphere_var._parameters.focus_xh = 0;
  _sample_sphere_var._parameters.focus_r = 0;
  _sample_sphere_var._parameters.p_interact = 0;
  _sample_sphere_var._parameters.mask_string[0]='\0';
  _sample_sphere_var._parameters.mask_setting[0]='\0';
  _sample_sphere_var._parameters.number_of_activations = 1;
  _sample_sphere_var._parameters.surface[0]='\0';
  _sample_sphere_var._parameters.cut_surface[0]='\0';
  if("init" && strlen("init"))
    stracpy(_sample_sphere_var._parameters.init, "init" ? "init" : "", 16384);
  else 
  _sample_sphere_var._parameters.init[0]='\0';


  /* component sample_sphere=Union_sphere() AT ROTATED */
  {
    Coords tc1, tc2;
    tc1 = coords_set(0,0,0);
    tc2 = coords_set(0,0,0);
    Rotation tr1;
    rot_set_rotation(tr1,0,0,0);
    rot_set_rotation(tr1,
      (0)*DEG2RAD, (0)*DEG2RAD, (0)*DEG2RAD);
    rot_mul(tr1, _sample_pos_var._rotation_absolute, _sample_sphere_var._rotation_absolute);
    rot_transpose(_source_var._rotation_absolute, tr1);
    rot_mul(_sample_sphere_var._rotation_absolute, tr1, _sample_sphere_var._rotation_relative);
    _sample_sphere_var._rotation_is_identity =  rot_test_identity(_sample_sphere_var._rotation_relative);
    tc1 = coords_set(
      0.01, 0, 0);
    rot_transpose(_sample_pos_var._rotation_absolute, tr1);
    tc2 = rot_apply(tr1, tc1);
    _sample_sphere_var._position_absolute = coords_add(_sample_pos_var._position_absolute, tc2);
    tc1 = coords_sub(_source_var._position_absolute, _sample_sphere_var._position_absolute);
    _sample_sphere_var._position_relative = rot_apply(_sample_sphere_var._rotation_absolute, tc1);
  } /* sample_sphere=Union_sphere() AT ROTATED */
  DEBUG_COMPONENT("sample_sphere", _sample_sphere_var._position_absolute, _sample_sphere_var._rotation_absolute);
  instrument->_position_absolute[15] = _sample_sphere_var._position_absolute;
  instrument->_position_relative[15] = _sample_sphere_var._position_relative;
    _sample_sphere_var._position_relative_is_zero =  coords_test_zero(_sample_sphere_var._position_relative);
  instrument->counter_N[15]  = instrument->counter_P[15] = instrument->counter_P2[15] = 0;
  instrument->counter_AbsorbProp[15]= 0;
  #ifdef USE_NEXUS
  if(nxhandle) {
    if ((!mcdotrace) && mcformat && strcasestr(mcformat, "NeXus")) {
    MPI_MASTER(
        mccomp_placement_type_nexus(nxhandle,"0014_sample_sphere", _sample_sphere_var._position_absolute, _sample_sphere_var._rotation_absolute, "Union_sphere");
        mccomp_param_nexus(nxhandle,"0014_sample_sphere", "material_string", 0, "Cu", "char*");
        mccomp_param_nexus(nxhandle,"0014_sample_sphere", "priority", "NONE", "20","MCNUM");
        mccomp_param_nexus(nxhandle,"0014_sample_sphere", "radius", "NONE", "0.031","MCNUM");
        mccomp_param_nexus(nxhandle,"0014_sample_sphere", "visualize", "1", "1","MCNUM");
        mccomp_param_nexus(nxhandle,"0014_sample_sphere", "target_index", "0", "0","int");
        mccomp_param_nexus(nxhandle,"0014_sample_sphere", "target_x", "0", "0","MCNUM");
        mccomp_param_nexus(nxhandle,"0014_sample_sphere", "target_y", "0", "0","MCNUM");
        mccomp_param_nexus(nxhandle,"0014_sample_sphere", "target_z", "0", "0","MCNUM");
        mccomp_param_nexus(nxhandle,"0014_sample_sphere", "focus_aw", "0", "0","MCNUM");
        mccomp_param_nexus(nxhandle,"0014_sample_sphere", "focus_ah", "0", "0","MCNUM");
        mccomp_param_nexus(nxhandle,"0014_sample_sphere", "focus_xw", "0", "0","MCNUM");
        mccomp_param_nexus(nxhandle,"0014_sample_sphere", "focus_xh", "0", "0","MCNUM");
        mccomp_param_nexus(nxhandle,"0014_sample_sphere", "focus_r", "0", "0","MCNUM");
        mccomp_param_nexus(nxhandle,"0014_sample_sphere", "p_interact", "0", "0","MCNUM");
        mccomp_param_nexus(nxhandle,"0014_sample_sphere", "mask_string", 0, 0, "char*");
        mccomp_param_nexus(nxhandle,"0014_sample_sphere", "mask_setting", 0, 0, "char*");
        mccomp_param_nexus(nxhandle,"0014_sample_sphere", "number_of_activations", "1", "1","MCNUM");
        mccomp_param_nexus(nxhandle,"0014_sample_sphere", "surface", 0, 0, "char*");
        mccomp_param_nexus(nxhandle,"0014_sample_sphere", "cut_surface", 0, 0, "char*");
        mccomp_param_nexus(nxhandle,"0014_sample_sphere", "init", "init", "init", "char*");
      );
    }
  } else {
    // fprintf(stderr,"NO NEXUS FILE");
  }
  #endif
  return(0);
} /* _sample_sphere_setpos */

/* component sample_box=Union_box() SETTING, POSITION/ROTATION */
int _sample_box_setpos(void)
{ /* sets initial component parameters, position and rotation */
  SIG_MESSAGE("[_sample_box_setpos] component sample_box=Union_box() SETTING [Union_box:0]");
  stracpy(_sample_box_var._name, "sample_box", 16384);
  stracpy(_sample_box_var._type, "Union_box", 16384);
  _sample_box_var._index=16;
  int current_setpos_index = 16;
  if("Vanadium" && strlen("Vanadium"))
    stracpy(_sample_box_var._parameters.material_string, "Vanadium" ? "Vanadium" : "", 16384);
  else 
  _sample_box_var._parameters.material_string[0]='\0';
  _sample_box_var._parameters.priority = 21;
  _sample_box_var._parameters.xwidth = 0.021;
  _sample_box_var._parameters.yheight = 0.018;
  _sample_box_var._parameters.zdepth = 0.1;
  _sample_box_var._parameters.xwidth2 = -1;
  _sample_box_var._parameters.yheight2 = -1;
  _sample_box_var._parameters.visualize = 1;
  _sample_box_var._parameters.target_index = 0;
  _sample_box_var._parameters.target_x = 0;
  _sample_box_var._parameters.target_y = 0;
  _sample_box_var._parameters.target_z = 0;
  _sample_box_var._parameters.focus_aw = 0;
  _sample_box_var._parameters.focus_ah = 0;
  _sample_box_var._parameters.focus_xw = 0;
  _sample_box_var._parameters.focus_xh = 0;
  _sample_box_var._parameters.focus_r = 0;
  _sample_box_var._parameters.plus_z_surface[0]='\0';
  _sample_box_var._parameters.minus_z_surface[0]='\0';
  _sample_box_var._parameters.plus_x_surface[0]='\0';
  _sample_box_var._parameters.minus_x_surface[0]='\0';
  _sample_box_var._parameters.plus_y_surface[0]='\0';
  _sample_box_var._parameters.minus_y_surface[0]='\0';
  _sample_box_var._parameters.all_face_surface[0]='\0';
  _sample_box_var._parameters.cut_surface[0]='\0';
  _sample_box_var._parameters.p_interact = 0;
  _sample_box_var._parameters.mask_string[0]='\0';
  _sample_box_var._parameters.mask_setting[0]='\0';
  _sample_box_var._parameters.number_of_activations = 1;
  if("init" && strlen("init"))
    stracpy(_sample_box_var._parameters.init, "init" ? "init" : "", 16384);
  else 
  _sample_box_var._parameters.init[0]='\0';


  /* component sample_box=Union_box() AT ROTATED */
  {
    Coords tc1, tc2;
    tc1 = coords_set(0,0,0);
    tc2 = coords_set(0,0,0);
    Rotation tr1;
    rot_set_rotation(tr1,0,0,0);
    rot_set_rotation(tr1,
      (34)*DEG2RAD, (17)*DEG2RAD, (0)*DEG2RAD);
    rot_mul(tr1, _sample_pos_var._rotation_absolute, _sample_box_var._rotation_absolute);
    rot_transpose(_source_var._rotation_absolute, tr1);
    rot_mul(_sample_box_var._rotation_absolute, tr1, _sample_box_var._rotation_relative);
    _sample_box_var._rotation_is_identity =  rot_test_identity(_sample_box_var._rotation_relative);
    tc1 = coords_set(
      -0.01, 0, 0);
    rot_transpose(_sample_pos_var._rotation_absolute, tr1);
    tc2 = rot_apply(tr1, tc1);
    _sample_box_var._position_absolute = coords_add(_sample_pos_var._position_absolute, tc2);
    tc1 = coords_sub(_source_var._position_absolute, _sample_box_var._position_absolute);
    _sample_box_var._position_relative = rot_apply(_sample_box_var._rotation_absolute, tc1);
  } /* sample_box=Union_box() AT ROTATED */
  DEBUG_COMPONENT("sample_box", _sample_box_var._position_absolute, _sample_box_var._rotation_absolute);
  instrument->_position_absolute[16] = _sample_box_var._position_absolute;
  instrument->_position_relative[16] = _sample_box_var._position_relative;
    _sample_box_var._position_relative_is_zero =  coords_test_zero(_sample_box_var._position_relative);
  instrument->counter_N[16]  = instrument->counter_P[16] = instrument->counter_P2[16] = 0;
  instrument->counter_AbsorbProp[16]= 0;
  #ifdef USE_NEXUS
  if(nxhandle) {
    if ((!mcdotrace) && mcformat && strcasestr(mcformat, "NeXus")) {
    MPI_MASTER(
        mccomp_placement_type_nexus(nxhandle,"0015_sample_box", _sample_box_var._position_absolute, _sample_box_var._rotation_absolute, "Union_box");
        mccomp_param_nexus(nxhandle,"0015_sample_box", "material_string", 0, "Vanadium", "char*");
        mccomp_param_nexus(nxhandle,"0015_sample_box", "priority", "NONE", "21","MCNUM");
        mccomp_param_nexus(nxhandle,"0015_sample_box", "xwidth", "NONE", "0.021","MCNUM");
        mccomp_param_nexus(nxhandle,"0015_sample_box", "yheight", "NONE", "0.018","MCNUM");
        mccomp_param_nexus(nxhandle,"0015_sample_box", "zdepth", "NONE", "0.1","MCNUM");
        mccomp_param_nexus(nxhandle,"0015_sample_box", "xwidth2", "-1", "-1","MCNUM");
        mccomp_param_nexus(nxhandle,"0015_sample_box", "yheight2", "-1", "-1","MCNUM");
        mccomp_param_nexus(nxhandle,"0015_sample_box", "visualize", "1", "1","MCNUM");
        mccomp_param_nexus(nxhandle,"0015_sample_box", "target_index", "0", "0","int");
        mccomp_param_nexus(nxhandle,"0015_sample_box", "target_x", "0", "0","MCNUM");
        mccomp_param_nexus(nxhandle,"0015_sample_box", "target_y", "0", "0","MCNUM");
        mccomp_param_nexus(nxhandle,"0015_sample_box", "target_z", "0", "0","MCNUM");
        mccomp_param_nexus(nxhandle,"0015_sample_box", "focus_aw", "0", "0","MCNUM");
        mccomp_param_nexus(nxhandle,"0015_sample_box", "focus_ah", "0", "0","MCNUM");
        mccomp_param_nexus(nxhandle,"0015_sample_box", "focus_xw", "0", "0","MCNUM");
        mccomp_param_nexus(nxhandle,"0015_sample_box", "focus_xh", "0", "0","MCNUM");
        mccomp_param_nexus(nxhandle,"0015_sample_box", "focus_r", "0", "0","MCNUM");
        mccomp_param_nexus(nxhandle,"0015_sample_box", "plus_z_surface", 0, 0, "char*");
        mccomp_param_nexus(nxhandle,"0015_sample_box", "minus_z_surface", 0, 0, "char*");
        mccomp_param_nexus(nxhandle,"0015_sample_box", "plus_x_surface", 0, 0, "char*");
        mccomp_param_nexus(nxhandle,"0015_sample_box", "minus_x_surface", 0, 0, "char*");
        mccomp_param_nexus(nxhandle,"0015_sample_box", "plus_y_surface", 0, 0, "char*");
        mccomp_param_nexus(nxhandle,"0015_sample_box", "minus_y_surface", 0, 0, "char*");
        mccomp_param_nexus(nxhandle,"0015_sample_box", "all_face_surface", 0, 0, "char*");
        mccomp_param_nexus(nxhandle,"0015_sample_box", "cut_surface", 0, 0, "char*");
        mccomp_param_nexus(nxhandle,"0015_sample_box", "p_interact", "0", "0","MCNUM");
        mccomp_param_nexus(nxhandle,"0015_sample_box", "mask_string", 0, 0, "char*");
        mccomp_param_nexus(nxhandle,"0015_sample_box", "mask_setting", 0, 0, "char*");
        mccomp_param_nexus(nxhandle,"0015_sample_box", "number_of_activations", "1", "1","MCNUM");
        mccomp_param_nexus(nxhandle,"0015_sample_box", "init", "init", "init", "char*");
      );
    }
  } else {
    // fprintf(stderr,"NO NEXUS FILE");
  }
  #endif
  return(0);
} /* _sample_box_setpos */

/* component sample_cone=Union_cone() SETTING, POSITION/ROTATION */
int _sample_cone_setpos(void)
{ /* sets initial component parameters, position and rotation */
  SIG_MESSAGE("[_sample_cone_setpos] component sample_cone=Union_cone() SETTING [Union_cone:0]");
  stracpy(_sample_cone_var._name, "sample_cone", 16384);
  stracpy(_sample_cone_var._type, "Union_cone", 16384);
  _sample_cone_var._index=17;
  int current_setpos_index = 17;
  if("Cu" && strlen("Cu"))
    stracpy(_sample_cone_var._parameters.material_string, "Cu" ? "Cu" : "", 16384);
  else 
  _sample_cone_var._parameters.material_string[0]='\0';
  _sample_cone_var._parameters.priority = 22;
  _sample_cone_var._parameters.radius = 0;
  _sample_cone_var._parameters.radius_top = 0.031;
  _sample_cone_var._parameters.radius_bottom = 0.005;
  _sample_cone_var._parameters.yheight = 0.04;
  _sample_cone_var._parameters.visualize = 1;
  _sample_cone_var._parameters.target_index = 0;
  _sample_cone_var._parameters.target_x = 0;
  _sample_cone_var._parameters.target_y = 0;
  _sample_cone_var._parameters.target_z = 0;
  _sample_cone_var._parameters.focus_aw = 0;
  _sample_cone_var._parameters.focus_ah = 0;
  _sample_cone_var._parameters.focus_xw = 0;
  _sample_cone_var._parameters.focus_xh = 0;
  _sample_cone_var._parameters.focus_r = 0;
  _sample_cone_var._parameters.p_interact = 0;
  _sample_cone_var._parameters.mask_string[0]='\0';
  _sample_cone_var._parameters.mask_setting[0]='\0';
  _sample_cone_var._parameters.number_of_activations = 1;
  _sample_cone_var._parameters.curved_surface[0]='\0';
  _sample_cone_var._parameters.top_surface[0]='\0';
  _sample_cone_var._parameters.bottom_surface[0]='\0';
  _sample_cone_var._parameters.all_face_surface[0]='\0';
  _sample_cone_var._parameters.cut_surface[0]='\0';
  if("init" && strlen("init"))
    stracpy(_sample_cone_var._parameters.init, "init" ? "init" : "", 16384);
  else 
  _sample_cone_var._parameters.init[0]='\0';


  /* component sample_cone=Union_cone() AT ROTATED */
  {
    Coords tc1, tc2;
    tc1 = coords_set(0,0,0);
    tc2 = coords_set(0,0,0);
    Rotation tr1;
    rot_set_rotation(tr1,0,0,0);
    rot_set_rotation(tr1,
      (0)*DEG2RAD, (0)*DEG2RAD, (20)*DEG2RAD);
    rot_mul(tr1, _sample_pos_var._rotation_absolute, _sample_cone_var._rotation_absolute);
    rot_transpose(_source_var._rotation_absolute, tr1);
    rot_mul(_sample_cone_var._rotation_absolute, tr1, _sample_cone_var._rotation_relative);
    _sample_cone_var._rotation_is_identity =  rot_test_identity(_sample_cone_var._rotation_relative);
    tc1 = coords_set(
      0.02, -0.03, 0);
    rot_transpose(_sample_pos_var._rotation_absolute, tr1);
    tc2 = rot_apply(tr1, tc1);
    _sample_cone_var._position_absolute = coords_add(_sample_pos_var._position_absolute, tc2);
    tc1 = coords_sub(_source_var._position_absolute, _sample_cone_var._position_absolute);
    _sample_cone_var._position_relative = rot_apply(_sample_cone_var._rotation_absolute, tc1);
  } /* sample_cone=Union_cone() AT ROTATED */
  DEBUG_COMPONENT("sample_cone", _sample_cone_var._position_absolute, _sample_cone_var._rotation_absolute);
  instrument->_position_absolute[17] = _sample_cone_var._position_absolute;
  instrument->_position_relative[17] = _sample_cone_var._position_relative;
    _sample_cone_var._position_relative_is_zero =  coords_test_zero(_sample_cone_var._position_relative);
  instrument->counter_N[17]  = instrument->counter_P[17] = instrument->counter_P2[17] = 0;
  instrument->counter_AbsorbProp[17]= 0;
  #ifdef USE_NEXUS
  if(nxhandle) {
    if ((!mcdotrace) && mcformat && strcasestr(mcformat, "NeXus")) {
    MPI_MASTER(
        mccomp_placement_type_nexus(nxhandle,"0016_sample_cone", _sample_cone_var._position_absolute, _sample_cone_var._rotation_absolute, "Union_cone");
        mccomp_param_nexus(nxhandle,"0016_sample_cone", "material_string", 0, "Cu", "char*");
        mccomp_param_nexus(nxhandle,"0016_sample_cone", "priority", "NONE", "22","MCNUM");
        mccomp_param_nexus(nxhandle,"0016_sample_cone", "radius", "0", "0","MCNUM");
        mccomp_param_nexus(nxhandle,"0016_sample_cone", "radius_top", "0", "0.031","MCNUM");
        mccomp_param_nexus(nxhandle,"0016_sample_cone", "radius_bottom", "0", "0.005","MCNUM");
        mccomp_param_nexus(nxhandle,"0016_sample_cone", "yheight", "NONE", "0.04","MCNUM");
        mccomp_param_nexus(nxhandle,"0016_sample_cone", "visualize", "1", "1","MCNUM");
        mccomp_param_nexus(nxhandle,"0016_sample_cone", "target_index", "0", "0","int");
        mccomp_param_nexus(nxhandle,"0016_sample_cone", "target_x", "0", "0","MCNUM");
        mccomp_param_nexus(nxhandle,"0016_sample_cone", "target_y", "0", "0","MCNUM");
        mccomp_param_nexus(nxhandle,"0016_sample_cone", "target_z", "0", "0","MCNUM");
        mccomp_param_nexus(nxhandle,"0016_sample_cone", "focus_aw", "0", "0","MCNUM");
        mccomp_param_nexus(nxhandle,"0016_sample_cone", "focus_ah", "0", "0","MCNUM");
        mccomp_param_nexus(nxhandle,"0016_sample_cone", "focus_xw", "0", "0","MCNUM");
        mccomp_param_nexus(nxhandle,"0016_sample_cone", "focus_xh", "0", "0","MCNUM");
        mccomp_param_nexus(nxhandle,"0016_sample_cone", "focus_r", "0", "0","MCNUM");
        mccomp_param_nexus(nxhandle,"0016_sample_cone", "p_interact", "0", "0","MCNUM");
        mccomp_param_nexus(nxhandle,"0016_sample_cone", "mask_string", 0, 0, "char*");
        mccomp_param_nexus(nxhandle,"0016_sample_cone", "mask_setting", 0, 0, "char*");
        mccomp_param_nexus(nxhandle,"0016_sample_cone", "number_of_activations", "1", "1","MCNUM");
        mccomp_param_nexus(nxhandle,"0016_sample_cone", "curved_surface", 0, 0, "char*");
        mccomp_param_nexus(nxhandle,"0016_sample_cone", "top_surface", 0, 0, "char*");
        mccomp_param_nexus(nxhandle,"0016_sample_cone", "bottom_surface", 0, 0, "char*");
        mccomp_param_nexus(nxhandle,"0016_sample_cone", "all_face_surface", 0, 0, "char*");
        mccomp_param_nexus(nxhandle,"0016_sample_cone", "cut_surface", 0, 0, "char*");
        mccomp_param_nexus(nxhandle,"0016_sample_cone", "init", "init", "init", "char*");
      );
    }
  } else {
    // fprintf(stderr,"NO NEXUS FILE");
  }
  #endif
  return(0);
} /* _sample_cone_setpos */

/* component test_logger_1D=Union_logger_1D() SETTING, POSITION/ROTATION */
int _test_logger_1D_setpos(void)
{ /* sets initial component parameters, position and rotation */
  SIG_MESSAGE("[_test_logger_1D_setpos] component test_logger_1D=Union_logger_1D() SETTING [Union_logger_1D:0]");
  stracpy(_test_logger_1D_var._name, "test_logger_1D", 16384);
  stracpy(_test_logger_1D_var._type, "Union_logger_1D", 16384);
  _test_logger_1D_var._index=18;
  int current_setpos_index = 18;
  if("NULL" && strlen("NULL"))
    stracpy(_test_logger_1D_var._parameters.target_geometry, "NULL" ? "NULL" : "", 16384);
  else 
  _test_logger_1D_var._parameters.target_geometry[0]='\0';
  if("NULL" && strlen("NULL"))
    stracpy(_test_logger_1D_var._parameters.target_process, "NULL" ? "NULL" : "", 16384);
  else 
  _test_logger_1D_var._parameters.target_process[0]='\0';
  _test_logger_1D_var._parameters.min_value = 1E-4;
  _test_logger_1D_var._parameters.max_value = 8E-4;
  _test_logger_1D_var._parameters.n1 = 200;
  if("time" && strlen("time"))
    stracpy(_test_logger_1D_var._parameters.variable, "time" ? "time" : "", 16384);
  else 
  _test_logger_1D_var._parameters.variable[0]='\0';
  if("logger_1D.dat" && strlen("logger_1D.dat"))
    stracpy(_test_logger_1D_var._parameters.filename, "logger_1D.dat" ? "logger_1D.dat" : "", 16384);
  else 
  _test_logger_1D_var._parameters.filename[0]='\0';
  _test_logger_1D_var._parameters.order_total = 0;
  _test_logger_1D_var._parameters.order_volume = 0;
  _test_logger_1D_var._parameters.order_volume_process = 0;
  _test_logger_1D_var._parameters.logger_conditional_extend_index = -1;
  if("init" && strlen("init"))
    stracpy(_test_logger_1D_var._parameters.init, "init" ? "init" : "", 16384);
  else 
  _test_logger_1D_var._parameters.init[0]='\0';


  /* component test_logger_1D=Union_logger_1D() AT ROTATED */
  {
    Coords tc1, tc2;
    tc1 = coords_set(0,0,0);
    tc2 = coords_set(0,0,0);
    Rotation tr1;
    rot_set_rotation(tr1,0,0,0);
    rot_set_rotation(tr1,
      (0)*DEG2RAD, (0)*DEG2RAD, (0)*DEG2RAD);
    rot_mul(tr1, _sample_pos_var._rotation_absolute, _test_logger_1D_var._rotation_absolute);
    rot_transpose(_source_var._rotation_absolute, tr1);
    rot_mul(_test_logger_1D_var._rotation_absolute, tr1, _test_logger_1D_var._rotation_relative);
    _test_logger_1D_var._rotation_is_identity =  rot_test_identity(_test_logger_1D_var._rotation_relative);
    tc1 = coords_set(
      0, 0, 0);
    rot_transpose(_sample_pos_var._rotation_absolute, tr1);
    tc2 = rot_apply(tr1, tc1);
    _test_logger_1D_var._position_absolute = coords_add(_sample_pos_var._position_absolute, tc2);
    tc1 = coords_sub(_source_var._position_absolute, _test_logger_1D_var._position_absolute);
    _test_logger_1D_var._position_relative = rot_apply(_test_logger_1D_var._rotation_absolute, tc1);
  } /* test_logger_1D=Union_logger_1D() AT ROTATED */
  DEBUG_COMPONENT("test_logger_1D", _test_logger_1D_var._position_absolute, _test_logger_1D_var._rotation_absolute);
  instrument->_position_absolute[18] = _test_logger_1D_var._position_absolute;
  instrument->_position_relative[18] = _test_logger_1D_var._position_relative;
    _test_logger_1D_var._position_relative_is_zero =  coords_test_zero(_test_logger_1D_var._position_relative);
  instrument->counter_N[18]  = instrument->counter_P[18] = instrument->counter_P2[18] = 0;
  instrument->counter_AbsorbProp[18]= 0;
  #ifdef USE_NEXUS
  if(nxhandle) {
    if ((!mcdotrace) && mcformat && strcasestr(mcformat, "NeXus")) {
    MPI_MASTER(
        mccomp_placement_type_nexus(nxhandle,"0017_test_logger_1D", _test_logger_1D_var._position_absolute, _test_logger_1D_var._rotation_absolute, "Union_logger_1D");
        mccomp_param_nexus(nxhandle,"0017_test_logger_1D", "target_geometry", "NULL", "NULL", "char*");
        mccomp_param_nexus(nxhandle,"0017_test_logger_1D", "target_process", "NULL", "NULL", "char*");
        mccomp_param_nexus(nxhandle,"0017_test_logger_1D", "min_value", "NONE", "1E-4","MCNUM");
        mccomp_param_nexus(nxhandle,"0017_test_logger_1D", "max_value", "NONE", "8E-4","MCNUM");
        mccomp_param_nexus(nxhandle,"0017_test_logger_1D", "n1", "90", "200","MCNUM");
        mccomp_param_nexus(nxhandle,"0017_test_logger_1D", "variable", "time", "time", "char*");
        mccomp_param_nexus(nxhandle,"0017_test_logger_1D", "filename", "NULL", "logger_1D.dat", "char*");
        mccomp_param_nexus(nxhandle,"0017_test_logger_1D", "order_total", "0", "0","MCNUM");
        mccomp_param_nexus(nxhandle,"0017_test_logger_1D", "order_volume", "0", "0","MCNUM");
        mccomp_param_nexus(nxhandle,"0017_test_logger_1D", "order_volume_process", "0", "0","MCNUM");
        mccomp_param_nexus(nxhandle,"0017_test_logger_1D", "logger_conditional_extend_index", "-1", "-1","MCNUM");
        mccomp_param_nexus(nxhandle,"0017_test_logger_1D", "init", "init", "init", "char*");
      );
    }
  } else {
    // fprintf(stderr,"NO NEXUS FILE");
  }
  #endif
  return(0);
} /* _test_logger_1D_setpos */

/* component test_logger_2D_space_zx=Union_logger_2D_space() SETTING, POSITION/ROTATION */
int _test_logger_2D_space_zx_setpos(void)
{ /* sets initial component parameters, position and rotation */
  SIG_MESSAGE("[_test_logger_2D_space_zx_setpos] component test_logger_2D_space_zx=Union_logger_2D_space() SETTING [Union_logger_2D_space:0]");
  stracpy(_test_logger_2D_space_zx_var._name, "test_logger_2D_space_zx", 16384);
  stracpy(_test_logger_2D_space_zx_var._type, "Union_logger_2D_space", 16384);
  _test_logger_2D_space_zx_var._index=19;
  int current_setpos_index = 19;
  if("NULL" && strlen("NULL"))
    stracpy(_test_logger_2D_space_zx_var._parameters.target_geometry, "NULL" ? "NULL" : "", 16384);
  else 
  _test_logger_2D_space_zx_var._parameters.target_geometry[0]='\0';
  if("NULL" && strlen("NULL"))
    stracpy(_test_logger_2D_space_zx_var._parameters.target_process, "NULL" ? "NULL" : "", 16384);
  else 
  _test_logger_2D_space_zx_var._parameters.target_process[0]='\0';
  if("z" && strlen("z"))
    stracpy(_test_logger_2D_space_zx_var._parameters.D_direction_1, "z" ? "z" : "", 16384);
  else 
  _test_logger_2D_space_zx_var._parameters.D_direction_1[0]='\0';
  _test_logger_2D_space_zx_var._parameters.D1_min = -0.1;
  _test_logger_2D_space_zx_var._parameters.D1_max = 0.1;
  _test_logger_2D_space_zx_var._parameters.n1 = 200;
  if("x" && strlen("x"))
    stracpy(_test_logger_2D_space_zx_var._parameters.D_direction_2, "x" ? "x" : "", 16384);
  else 
  _test_logger_2D_space_zx_var._parameters.D_direction_2[0]='\0';
  _test_logger_2D_space_zx_var._parameters.D2_min = -0.1;
  _test_logger_2D_space_zx_var._parameters.D2_max = 0.1;
  _test_logger_2D_space_zx_var._parameters.n2 = 200;
  if("logger_2D_space_zx.dat" && strlen("logger_2D_space_zx.dat"))
    stracpy(_test_logger_2D_space_zx_var._parameters.filename, "logger_2D_space_zx.dat" ? "logger_2D_space_zx.dat" : "", 16384);
  else 
  _test_logger_2D_space_zx_var._parameters.filename[0]='\0';
  _test_logger_2D_space_zx_var._parameters.order_total = 0;
  _test_logger_2D_space_zx_var._parameters.order_volume = 0;
  _test_logger_2D_space_zx_var._parameters.order_volume_process = 0;
  _test_logger_2D_space_zx_var._parameters.logger_conditional_extend_index = -1;
  if("init" && strlen("init"))
    stracpy(_test_logger_2D_space_zx_var._parameters.init, "init" ? "init" : "", 16384);
  else 
  _test_logger_2D_space_zx_var._parameters.init[0]='\0';


  /* component test_logger_2D_space_zx=Union_logger_2D_space() AT ROTATED */
  {
    Coords tc1, tc2;
    tc1 = coords_set(0,0,0);
    tc2 = coords_set(0,0,0);
    Rotation tr1;
    rot_set_rotation(tr1,0,0,0);
    rot_set_rotation(tr1,
      (0)*DEG2RAD, (0)*DEG2RAD, (0)*DEG2RAD);
    rot_mul(tr1, _sample_pos_var._rotation_absolute, _test_logger_2D_space_zx_var._rotation_absolute);
    rot_transpose(_source_var._rotation_absolute, tr1);
    rot_mul(_test_logger_2D_space_zx_var._rotation_absolute, tr1, _test_logger_2D_space_zx_var._rotation_relative);
    _test_logger_2D_space_zx_var._rotation_is_identity =  rot_test_identity(_test_logger_2D_space_zx_var._rotation_relative);
    tc1 = coords_set(
      0, 0, 0);
    rot_transpose(_sample_pos_var._rotation_absolute, tr1);
    tc2 = rot_apply(tr1, tc1);
    _test_logger_2D_space_zx_var._position_absolute = coords_add(_sample_pos_var._position_absolute, tc2);
    tc1 = coords_sub(_source_var._position_absolute, _test_logger_2D_space_zx_var._position_absolute);
    _test_logger_2D_space_zx_var._position_relative = rot_apply(_test_logger_2D_space_zx_var._rotation_absolute, tc1);
  } /* test_logger_2D_space_zx=Union_logger_2D_space() AT ROTATED */
  DEBUG_COMPONENT("test_logger_2D_space_zx", _test_logger_2D_space_zx_var._position_absolute, _test_logger_2D_space_zx_var._rotation_absolute);
  instrument->_position_absolute[19] = _test_logger_2D_space_zx_var._position_absolute;
  instrument->_position_relative[19] = _test_logger_2D_space_zx_var._position_relative;
    _test_logger_2D_space_zx_var._position_relative_is_zero =  coords_test_zero(_test_logger_2D_space_zx_var._position_relative);
  instrument->counter_N[19]  = instrument->counter_P[19] = instrument->counter_P2[19] = 0;
  instrument->counter_AbsorbProp[19]= 0;
  #ifdef USE_NEXUS
  if(nxhandle) {
    if ((!mcdotrace) && mcformat && strcasestr(mcformat, "NeXus")) {
    MPI_MASTER(
        mccomp_placement_type_nexus(nxhandle,"0018_test_logger_2D_space_zx", _test_logger_2D_space_zx_var._position_absolute, _test_logger_2D_space_zx_var._rotation_absolute, "Union_logger_2D_space");
        mccomp_param_nexus(nxhandle,"0018_test_logger_2D_space_zx", "target_geometry", "NULL", "NULL", "char*");
        mccomp_param_nexus(nxhandle,"0018_test_logger_2D_space_zx", "target_process", "NULL", "NULL", "char*");
        mccomp_param_nexus(nxhandle,"0018_test_logger_2D_space_zx", "D_direction_1", "x", "z", "char*");
        mccomp_param_nexus(nxhandle,"0018_test_logger_2D_space_zx", "D1_min", "-5", "-0.1","MCNUM");
        mccomp_param_nexus(nxhandle,"0018_test_logger_2D_space_zx", "D1_max", "5", "0.1","MCNUM");
        mccomp_param_nexus(nxhandle,"0018_test_logger_2D_space_zx", "n1", "90", "200","MCNUM");
        mccomp_param_nexus(nxhandle,"0018_test_logger_2D_space_zx", "D_direction_2", "z", "x", "char*");
        mccomp_param_nexus(nxhandle,"0018_test_logger_2D_space_zx", "D2_min", "-5", "-0.1","MCNUM");
        mccomp_param_nexus(nxhandle,"0018_test_logger_2D_space_zx", "D2_max", "5", "0.1","MCNUM");
        mccomp_param_nexus(nxhandle,"0018_test_logger_2D_space_zx", "n2", "90", "200","MCNUM");
        mccomp_param_nexus(nxhandle,"0018_test_logger_2D_space_zx", "filename", "NULL", "logger_2D_space_zx.dat", "char*");
        mccomp_param_nexus(nxhandle,"0018_test_logger_2D_space_zx", "order_total", "0", "0","MCNUM");
        mccomp_param_nexus(nxhandle,"0018_test_logger_2D_space_zx", "order_volume", "0", "0","MCNUM");
        mccomp_param_nexus(nxhandle,"0018_test_logger_2D_space_zx", "order_volume_process", "0", "0","MCNUM");
        mccomp_param_nexus(nxhandle,"0018_test_logger_2D_space_zx", "logger_conditional_extend_index", "-1", "-1","MCNUM");
        mccomp_param_nexus(nxhandle,"0018_test_logger_2D_space_zx", "init", "init", "init", "char*");
      );
    }
  } else {
    // fprintf(stderr,"NO NEXUS FILE");
  }
  #endif
  return(0);
} /* _test_logger_2D_space_zx_setpos */

/* component test_logger_2D_space_zy=Union_logger_2D_space() SETTING, POSITION/ROTATION */
int _test_logger_2D_space_zy_setpos(void)
{ /* sets initial component parameters, position and rotation */
  SIG_MESSAGE("[_test_logger_2D_space_zy_setpos] component test_logger_2D_space_zy=Union_logger_2D_space() SETTING [Union_logger_2D_space:0]");
  stracpy(_test_logger_2D_space_zy_var._name, "test_logger_2D_space_zy", 16384);
  stracpy(_test_logger_2D_space_zy_var._type, "Union_logger_2D_space", 16384);
  _test_logger_2D_space_zy_var._index=20;
  int current_setpos_index = 20;
  if("NULL" && strlen("NULL"))
    stracpy(_test_logger_2D_space_zy_var._parameters.target_geometry, "NULL" ? "NULL" : "", 16384);
  else 
  _test_logger_2D_space_zy_var._parameters.target_geometry[0]='\0';
  if("NULL" && strlen("NULL"))
    stracpy(_test_logger_2D_space_zy_var._parameters.target_process, "NULL" ? "NULL" : "", 16384);
  else 
  _test_logger_2D_space_zy_var._parameters.target_process[0]='\0';
  if("z" && strlen("z"))
    stracpy(_test_logger_2D_space_zy_var._parameters.D_direction_1, "z" ? "z" : "", 16384);
  else 
  _test_logger_2D_space_zy_var._parameters.D_direction_1[0]='\0';
  _test_logger_2D_space_zy_var._parameters.D1_min = -0.1;
  _test_logger_2D_space_zy_var._parameters.D1_max = 0.1;
  _test_logger_2D_space_zy_var._parameters.n1 = 200;
  if("y" && strlen("y"))
    stracpy(_test_logger_2D_space_zy_var._parameters.D_direction_2, "y" ? "y" : "", 16384);
  else 
  _test_logger_2D_space_zy_var._parameters.D_direction_2[0]='\0';
  _test_logger_2D_space_zy_var._parameters.D2_min = -0.1;
  _test_logger_2D_space_zy_var._parameters.D2_max = 0.1;
  _test_logger_2D_space_zy_var._parameters.n2 = 200;
  if("logger_2D_space_zy.dat" && strlen("logger_2D_space_zy.dat"))
    stracpy(_test_logger_2D_space_zy_var._parameters.filename, "logger_2D_space_zy.dat" ? "logger_2D_space_zy.dat" : "", 16384);
  else 
  _test_logger_2D_space_zy_var._parameters.filename[0]='\0';
  _test_logger_2D_space_zy_var._parameters.order_total = 0;
  _test_logger_2D_space_zy_var._parameters.order_volume = 0;
  _test_logger_2D_space_zy_var._parameters.order_volume_process = 0;
  _test_logger_2D_space_zy_var._parameters.logger_conditional_extend_index = -1;
  if("init" && strlen("init"))
    stracpy(_test_logger_2D_space_zy_var._parameters.init, "init" ? "init" : "", 16384);
  else 
  _test_logger_2D_space_zy_var._parameters.init[0]='\0';


  /* component test_logger_2D_space_zy=Union_logger_2D_space() AT ROTATED */
  {
    Coords tc1, tc2;
    tc1 = coords_set(0,0,0);
    tc2 = coords_set(0,0,0);
    Rotation tr1;
    rot_set_rotation(tr1,0,0,0);
    rot_set_rotation(tr1,
      (0)*DEG2RAD, (0)*DEG2RAD, (0)*DEG2RAD);
    rot_mul(tr1, _sample_pos_var._rotation_absolute, _test_logger_2D_space_zy_var._rotation_absolute);
    rot_transpose(_source_var._rotation_absolute, tr1);
    rot_mul(_test_logger_2D_space_zy_var._rotation_absolute, tr1, _test_logger_2D_space_zy_var._rotation_relative);
    _test_logger_2D_space_zy_var._rotation_is_identity =  rot_test_identity(_test_logger_2D_space_zy_var._rotation_relative);
    tc1 = coords_set(
      0, 0, 0);
    rot_transpose(_sample_pos_var._rotation_absolute, tr1);
    tc2 = rot_apply(tr1, tc1);
    _test_logger_2D_space_zy_var._position_absolute = coords_add(_sample_pos_var._position_absolute, tc2);
    tc1 = coords_sub(_source_var._position_absolute, _test_logger_2D_space_zy_var._position_absolute);
    _test_logger_2D_space_zy_var._position_relative = rot_apply(_test_logger_2D_space_zy_var._rotation_absolute, tc1);
  } /* test_logger_2D_space_zy=Union_logger_2D_space() AT ROTATED */
  DEBUG_COMPONENT("test_logger_2D_space_zy", _test_logger_2D_space_zy_var._position_absolute, _test_logger_2D_space_zy_var._rotation_absolute);
  instrument->_position_absolute[20] = _test_logger_2D_space_zy_var._position_absolute;
  instrument->_position_relative[20] = _test_logger_2D_space_zy_var._position_relative;
    _test_logger_2D_space_zy_var._position_relative_is_zero =  coords_test_zero(_test_logger_2D_space_zy_var._position_relative);
  instrument->counter_N[20]  = instrument->counter_P[20] = instrument->counter_P2[20] = 0;
  instrument->counter_AbsorbProp[20]= 0;
  #ifdef USE_NEXUS
  if(nxhandle) {
    if ((!mcdotrace) && mcformat && strcasestr(mcformat, "NeXus")) {
    MPI_MASTER(
        mccomp_placement_type_nexus(nxhandle,"0019_test_logger_2D_space_zy", _test_logger_2D_space_zy_var._position_absolute, _test_logger_2D_space_zy_var._rotation_absolute, "Union_logger_2D_space");
        mccomp_param_nexus(nxhandle,"0019_test_logger_2D_space_zy", "target_geometry", "NULL", "NULL", "char*");
        mccomp_param_nexus(nxhandle,"0019_test_logger_2D_space_zy", "target_process", "NULL", "NULL", "char*");
        mccomp_param_nexus(nxhandle,"0019_test_logger_2D_space_zy", "D_direction_1", "x", "z", "char*");
        mccomp_param_nexus(nxhandle,"0019_test_logger_2D_space_zy", "D1_min", "-5", "-0.1","MCNUM");
        mccomp_param_nexus(nxhandle,"0019_test_logger_2D_space_zy", "D1_max", "5", "0.1","MCNUM");
        mccomp_param_nexus(nxhandle,"0019_test_logger_2D_space_zy", "n1", "90", "200","MCNUM");
        mccomp_param_nexus(nxhandle,"0019_test_logger_2D_space_zy", "D_direction_2", "z", "y", "char*");
        mccomp_param_nexus(nxhandle,"0019_test_logger_2D_space_zy", "D2_min", "-5", "-0.1","MCNUM");
        mccomp_param_nexus(nxhandle,"0019_test_logger_2D_space_zy", "D2_max", "5", "0.1","MCNUM");
        mccomp_param_nexus(nxhandle,"0019_test_logger_2D_space_zy", "n2", "90", "200","MCNUM");
        mccomp_param_nexus(nxhandle,"0019_test_logger_2D_space_zy", "filename", "NULL", "logger_2D_space_zy.dat", "char*");
        mccomp_param_nexus(nxhandle,"0019_test_logger_2D_space_zy", "order_total", "0", "0","MCNUM");
        mccomp_param_nexus(nxhandle,"0019_test_logger_2D_space_zy", "order_volume", "0", "0","MCNUM");
        mccomp_param_nexus(nxhandle,"0019_test_logger_2D_space_zy", "order_volume_process", "0", "0","MCNUM");
        mccomp_param_nexus(nxhandle,"0019_test_logger_2D_space_zy", "logger_conditional_extend_index", "-1", "-1","MCNUM");
        mccomp_param_nexus(nxhandle,"0019_test_logger_2D_space_zy", "init", "init", "init", "char*");
      );
    }
  } else {
    // fprintf(stderr,"NO NEXUS FILE");
  }
  #endif
  return(0);
} /* _test_logger_2D_space_zy_setpos */

/* component scat_direction=Arm() SETTING, POSITION/ROTATION */
int _scat_direction_setpos(void)
{ /* sets initial component parameters, position and rotation */
  SIG_MESSAGE("[_scat_direction_setpos] component scat_direction=Arm() SETTING [Arm:0]");
  stracpy(_scat_direction_var._name, "scat_direction", 16384);
  stracpy(_scat_direction_var._type, "Arm", 16384);
  _scat_direction_var._index=21;
  int current_setpos_index = 21;
  /* component scat_direction=Arm() AT ROTATED */
  {
    Coords tc1, tc2;
    tc1 = coords_set(0,0,0);
    tc2 = coords_set(0,0,0);
    Rotation tr1;
    rot_set_rotation(tr1,0,0,0);
    rot_set_rotation(tr1,
      (0)*DEG2RAD, (34)*DEG2RAD, (0)*DEG2RAD);
    rot_mul(tr1, _sample_pos_var._rotation_absolute, _scat_direction_var._rotation_absolute);
    rot_transpose(_source_var._rotation_absolute, tr1);
    rot_mul(_scat_direction_var._rotation_absolute, tr1, _scat_direction_var._rotation_relative);
    _scat_direction_var._rotation_is_identity =  rot_test_identity(_scat_direction_var._rotation_relative);
    tc1 = coords_set(
      0, 0, 0);
    rot_transpose(_sample_pos_var._rotation_absolute, tr1);
    tc2 = rot_apply(tr1, tc1);
    _scat_direction_var._position_absolute = coords_add(_sample_pos_var._position_absolute, tc2);
    tc1 = coords_sub(_source_var._position_absolute, _scat_direction_var._position_absolute);
    _scat_direction_var._position_relative = rot_apply(_scat_direction_var._rotation_absolute, tc1);
  } /* scat_direction=Arm() AT ROTATED */
  DEBUG_COMPONENT("scat_direction", _scat_direction_var._position_absolute, _scat_direction_var._rotation_absolute);
  instrument->_position_absolute[21] = _scat_direction_var._position_absolute;
  instrument->_position_relative[21] = _scat_direction_var._position_relative;
    _scat_direction_var._position_relative_is_zero =  coords_test_zero(_scat_direction_var._position_relative);
  instrument->counter_N[21]  = instrument->counter_P[21] = instrument->counter_P2[21] = 0;
  instrument->counter_AbsorbProp[21]= 0;
  #ifdef USE_NEXUS
  if(nxhandle) {
    if ((!mcdotrace) && mcformat && strcasestr(mcformat, "NeXus")) {
    MPI_MASTER(
        mccomp_placement_type_nexus(nxhandle,"0020_scat_direction", _scat_direction_var._position_absolute, _scat_direction_var._rotation_absolute, "Arm");
      );
    }
  } else {
    // fprintf(stderr,"NO NEXUS FILE");
  }
  #endif
  return(0);
} /* _scat_direction_setpos */

/* component test_logger_2D_space_zx_con_time=Union_logger_2D_space() SETTING, POSITION/ROTATION */
int _test_logger_2D_space_zx_con_time_setpos(void)
{ /* sets initial component parameters, position and rotation */
  SIG_MESSAGE("[_test_logger_2D_space_zx_con_time_setpos] component test_logger_2D_space_zx_con_time=Union_logger_2D_space() SETTING [Union_logger_2D_space:0]");
  stracpy(_test_logger_2D_space_zx_con_time_var._name, "test_logger_2D_space_zx_con_time", 16384);
  stracpy(_test_logger_2D_space_zx_con_time_var._type, "Union_logger_2D_space", 16384);
  _test_logger_2D_space_zx_con_time_var._index=22;
  int current_setpos_index = 22;
  if("NULL" && strlen("NULL"))
    stracpy(_test_logger_2D_space_zx_con_time_var._parameters.target_geometry, "NULL" ? "NULL" : "", 16384);
  else 
  _test_logger_2D_space_zx_con_time_var._parameters.target_geometry[0]='\0';
  if("NULL" && strlen("NULL"))
    stracpy(_test_logger_2D_space_zx_con_time_var._parameters.target_process, "NULL" ? "NULL" : "", 16384);
  else 
  _test_logger_2D_space_zx_con_time_var._parameters.target_process[0]='\0';
  if("z" && strlen("z"))
    stracpy(_test_logger_2D_space_zx_con_time_var._parameters.D_direction_1, "z" ? "z" : "", 16384);
  else 
  _test_logger_2D_space_zx_con_time_var._parameters.D_direction_1[0]='\0';
  _test_logger_2D_space_zx_con_time_var._parameters.D1_min = -0.1;
  _test_logger_2D_space_zx_con_time_var._parameters.D1_max = 0.1;
  _test_logger_2D_space_zx_con_time_var._parameters.n1 = 200;
  if("x" && strlen("x"))
    stracpy(_test_logger_2D_space_zx_con_time_var._parameters.D_direction_2, "x" ? "x" : "", 16384);
  else 
  _test_logger_2D_space_zx_con_time_var._parameters.D_direction_2[0]='\0';
  _test_logger_2D_space_zx_con_time_var._parameters.D2_min = -0.1;
  _test_logger_2D_space_zx_con_time_var._parameters.D2_max = 0.1;
  _test_logger_2D_space_zx_con_time_var._parameters.n2 = 200;
  if("logger_2D_space_zx_con_time.dat" && strlen("logger_2D_space_zx_con_time.dat"))
    stracpy(_test_logger_2D_space_zx_con_time_var._parameters.filename, "logger_2D_space_zx_con_time.dat" ? "logger_2D_space_zx_con_time.dat" : "", 16384);
  else 
  _test_logger_2D_space_zx_con_time_var._parameters.filename[0]='\0';
  _test_logger_2D_space_zx_con_time_var._parameters.order_total = 0;
  _test_logger_2D_space_zx_con_time_var._parameters.order_volume = 0;
  _test_logger_2D_space_zx_con_time_var._parameters.order_volume_process = 0;
  _test_logger_2D_space_zx_con_time_var._parameters.logger_conditional_extend_index = -1;
  if("init" && strlen("init"))
    stracpy(_test_logger_2D_space_zx_con_time_var._parameters.init, "init" ? "init" : "", 16384);
  else 
  _test_logger_2D_space_zx_con_time_var._parameters.init[0]='\0';


  /* component test_logger_2D_space_zx_con_time=Union_logger_2D_space() AT ROTATED */
  {
    Coords tc1, tc2;
    tc1 = coords_set(0,0,0);
    tc2 = coords_set(0,0,0);
    Rotation tr1;
    rot_set_rotation(tr1,0,0,0);
    rot_set_rotation(tr1,
      (0)*DEG2RAD, (0)*DEG2RAD, (0)*DEG2RAD);
    rot_mul(tr1, _sample_pos_var._rotation_absolute, _test_logger_2D_space_zx_con_time_var._rotation_absolute);
    rot_transpose(_source_var._rotation_absolute, tr1);
    rot_mul(_test_logger_2D_space_zx_con_time_var._rotation_absolute, tr1, _test_logger_2D_space_zx_con_time_var._rotation_relative);
    _test_logger_2D_space_zx_con_time_var._rotation_is_identity =  rot_test_identity(_test_logger_2D_space_zx_con_time_var._rotation_relative);
    tc1 = coords_set(
      0, 0, 0);
    rot_transpose(_sample_pos_var._rotation_absolute, tr1);
    tc2 = rot_apply(tr1, tc1);
    _test_logger_2D_space_zx_con_time_var._position_absolute = coords_add(_sample_pos_var._position_absolute, tc2);
    tc1 = coords_sub(_source_var._position_absolute, _test_logger_2D_space_zx_con_time_var._position_absolute);
    _test_logger_2D_space_zx_con_time_var._position_relative = rot_apply(_test_logger_2D_space_zx_con_time_var._rotation_absolute, tc1);
  } /* test_logger_2D_space_zx_con_time=Union_logger_2D_space() AT ROTATED */
  DEBUG_COMPONENT("test_logger_2D_space_zx_con_time", _test_logger_2D_space_zx_con_time_var._position_absolute, _test_logger_2D_space_zx_con_time_var._rotation_absolute);
  instrument->_position_absolute[22] = _test_logger_2D_space_zx_con_time_var._position_absolute;
  instrument->_position_relative[22] = _test_logger_2D_space_zx_con_time_var._position_relative;
    _test_logger_2D_space_zx_con_time_var._position_relative_is_zero =  coords_test_zero(_test_logger_2D_space_zx_con_time_var._position_relative);
  instrument->counter_N[22]  = instrument->counter_P[22] = instrument->counter_P2[22] = 0;
  instrument->counter_AbsorbProp[22]= 0;
  #ifdef USE_NEXUS
  if(nxhandle) {
    if ((!mcdotrace) && mcformat && strcasestr(mcformat, "NeXus")) {
    MPI_MASTER(
        mccomp_placement_type_nexus(nxhandle,"0021_test_logger_2D_space_zx_con_time", _test_logger_2D_space_zx_con_time_var._position_absolute, _test_logger_2D_space_zx_con_time_var._rotation_absolute, "Union_logger_2D_space");
        mccomp_param_nexus(nxhandle,"0021_test_logger_2D_space_zx_con_time", "target_geometry", "NULL", "NULL", "char*");
        mccomp_param_nexus(nxhandle,"0021_test_logger_2D_space_zx_con_time", "target_process", "NULL", "NULL", "char*");
        mccomp_param_nexus(nxhandle,"0021_test_logger_2D_space_zx_con_time", "D_direction_1", "x", "z", "char*");
        mccomp_param_nexus(nxhandle,"0021_test_logger_2D_space_zx_con_time", "D1_min", "-5", "-0.1","MCNUM");
        mccomp_param_nexus(nxhandle,"0021_test_logger_2D_space_zx_con_time", "D1_max", "5", "0.1","MCNUM");
        mccomp_param_nexus(nxhandle,"0021_test_logger_2D_space_zx_con_time", "n1", "90", "200","MCNUM");
        mccomp_param_nexus(nxhandle,"0021_test_logger_2D_space_zx_con_time", "D_direction_2", "z", "x", "char*");
        mccomp_param_nexus(nxhandle,"0021_test_logger_2D_space_zx_con_time", "D2_min", "-5", "-0.1","MCNUM");
        mccomp_param_nexus(nxhandle,"0021_test_logger_2D_space_zx_con_time", "D2_max", "5", "0.1","MCNUM");
        mccomp_param_nexus(nxhandle,"0021_test_logger_2D_space_zx_con_time", "n2", "90", "200","MCNUM");
        mccomp_param_nexus(nxhandle,"0021_test_logger_2D_space_zx_con_time", "filename", "NULL", "logger_2D_space_zx_con_time.dat", "char*");
        mccomp_param_nexus(nxhandle,"0021_test_logger_2D_space_zx_con_time", "order_total", "0", "0","MCNUM");
        mccomp_param_nexus(nxhandle,"0021_test_logger_2D_space_zx_con_time", "order_volume", "0", "0","MCNUM");
        mccomp_param_nexus(nxhandle,"0021_test_logger_2D_space_zx_con_time", "order_volume_process", "0", "0","MCNUM");
        mccomp_param_nexus(nxhandle,"0021_test_logger_2D_space_zx_con_time", "logger_conditional_extend_index", "-1", "-1","MCNUM");
        mccomp_param_nexus(nxhandle,"0021_test_logger_2D_space_zx_con_time", "init", "init", "init", "char*");
      );
    }
  } else {
    // fprintf(stderr,"NO NEXUS FILE");
  }
  #endif
  return(0);
} /* _test_logger_2D_space_zx_con_time_setpos */

/* component test_conditional_standard=Union_conditional_standard() SETTING, POSITION/ROTATION */
int _test_conditional_standard_setpos(void)
{ /* sets initial component parameters, position and rotation */
  SIG_MESSAGE("[_test_conditional_standard_setpos] component test_conditional_standard=Union_conditional_standard() SETTING [Union_conditional_standard:0]");
  stracpy(_test_conditional_standard_var._name, "test_conditional_standard", 16384);
  stracpy(_test_conditional_standard_var._type, "Union_conditional_standard", 16384);
  _test_conditional_standard_var._index=23;
  int current_setpos_index = 23;
  if("test_logger_2D_space_zx_con_time" && strlen("test_logger_2D_space_zx_con_time"))
    stracpy(_test_conditional_standard_var._parameters.target_loggers, "test_logger_2D_space_zx_con_time" ? "test_logger_2D_space_zx_con_time" : "", 16384);
  else 
  _test_conditional_standard_var._parameters.target_loggers[0]='\0';
  _test_conditional_standard_var._parameters.master_tagging = 0;
  _test_conditional_standard_var._parameters.tagging_extend_index = -1;
  _test_conditional_standard_var._parameters.time_min = 4E-4;
  _test_conditional_standard_var._parameters.time_max = 5E-4;
  _test_conditional_standard_var._parameters.E_min = 0;
  _test_conditional_standard_var._parameters.E_max = 0;
  _test_conditional_standard_var._parameters.total_order_min = 0;
  _test_conditional_standard_var._parameters.total_order_max = 0;
  _test_conditional_standard_var._parameters.last_volume_index = -1;
  _test_conditional_standard_var._parameters.overwrite_logger_weight = 0;
  if("init" && strlen("init"))
    stracpy(_test_conditional_standard_var._parameters.init, "init" ? "init" : "", 16384);
  else 
  _test_conditional_standard_var._parameters.init[0]='\0';


  /* component test_conditional_standard=Union_conditional_standard() AT ROTATED */
  {
    Coords tc1, tc2;
    tc1 = coords_set(0,0,0);
    tc2 = coords_set(0,0,0);
    Rotation tr1;
    rot_set_rotation(tr1,0,0,0);
    rot_set_rotation(tr1,
      (0.0)*DEG2RAD, (0.0)*DEG2RAD, (0.0)*DEG2RAD);
    rot_mul(tr1, _sample_pos_var._rotation_absolute, _test_conditional_standard_var._rotation_absolute);
    rot_transpose(_source_var._rotation_absolute, tr1);
    rot_mul(_test_conditional_standard_var._rotation_absolute, tr1, _test_conditional_standard_var._rotation_relative);
    _test_conditional_standard_var._rotation_is_identity =  rot_test_identity(_test_conditional_standard_var._rotation_relative);
    tc1 = coords_set(
      0, 0, 0);
    rot_transpose(_sample_pos_var._rotation_absolute, tr1);
    tc2 = rot_apply(tr1, tc1);
    _test_conditional_standard_var._position_absolute = coords_add(_sample_pos_var._position_absolute, tc2);
    tc1 = coords_sub(_source_var._position_absolute, _test_conditional_standard_var._position_absolute);
    _test_conditional_standard_var._position_relative = rot_apply(_test_conditional_standard_var._rotation_absolute, tc1);
  } /* test_conditional_standard=Union_conditional_standard() AT ROTATED */
  DEBUG_COMPONENT("test_conditional_standard", _test_conditional_standard_var._position_absolute, _test_conditional_standard_var._rotation_absolute);
  instrument->_position_absolute[23] = _test_conditional_standard_var._position_absolute;
  instrument->_position_relative[23] = _test_conditional_standard_var._position_relative;
    _test_conditional_standard_var._position_relative_is_zero =  coords_test_zero(_test_conditional_standard_var._position_relative);
  instrument->counter_N[23]  = instrument->counter_P[23] = instrument->counter_P2[23] = 0;
  instrument->counter_AbsorbProp[23]= 0;
  #ifdef USE_NEXUS
  if(nxhandle) {
    if ((!mcdotrace) && mcformat && strcasestr(mcformat, "NeXus")) {
    MPI_MASTER(
        mccomp_placement_type_nexus(nxhandle,"0022_test_conditional_standard", _test_conditional_standard_var._position_absolute, _test_conditional_standard_var._rotation_absolute, "Union_conditional_standard");
        mccomp_param_nexus(nxhandle,"0022_test_conditional_standard", "target_loggers", "NULL", "test_logger_2D_space_zx_con_time", "char*");
        mccomp_param_nexus(nxhandle,"0022_test_conditional_standard", "master_tagging", "0", "0","int");
        mccomp_param_nexus(nxhandle,"0022_test_conditional_standard", "tagging_extend_index", "-1", "-1","int");
        mccomp_param_nexus(nxhandle,"0022_test_conditional_standard", "time_min", "0", "4E-4","MCNUM");
        mccomp_param_nexus(nxhandle,"0022_test_conditional_standard", "time_max", "0", "5E-4","MCNUM");
        mccomp_param_nexus(nxhandle,"0022_test_conditional_standard", "E_min", "0", "0","MCNUM");
        mccomp_param_nexus(nxhandle,"0022_test_conditional_standard", "E_max", "0", "0","MCNUM");
        mccomp_param_nexus(nxhandle,"0022_test_conditional_standard", "total_order_min", "0", "0","MCNUM");
        mccomp_param_nexus(nxhandle,"0022_test_conditional_standard", "total_order_max", "0", "0","MCNUM");
        mccomp_param_nexus(nxhandle,"0022_test_conditional_standard", "last_volume_index", "-1", "-1","MCNUM");
        mccomp_param_nexus(nxhandle,"0022_test_conditional_standard", "overwrite_logger_weight", "0", "0","MCNUM");
        mccomp_param_nexus(nxhandle,"0022_test_conditional_standard", "init", "init", "init", "char*");
      );
    }
  } else {
    // fprintf(stderr,"NO NEXUS FILE");
  }
  #endif
  return(0);
} /* _test_conditional_standard_setpos */

/* component test_logger_2D_space_zx_con_PSD=Union_logger_2D_space() SETTING, POSITION/ROTATION */
int _test_logger_2D_space_zx_con_PSD_setpos(void)
{ /* sets initial component parameters, position and rotation */
  SIG_MESSAGE("[_test_logger_2D_space_zx_con_PSD_setpos] component test_logger_2D_space_zx_con_PSD=Union_logger_2D_space() SETTING [Union_logger_2D_space:0]");
  stracpy(_test_logger_2D_space_zx_con_PSD_var._name, "test_logger_2D_space_zx_con_PSD", 16384);
  stracpy(_test_logger_2D_space_zx_con_PSD_var._type, "Union_logger_2D_space", 16384);
  _test_logger_2D_space_zx_con_PSD_var._index=24;
  int current_setpos_index = 24;
  if("NULL" && strlen("NULL"))
    stracpy(_test_logger_2D_space_zx_con_PSD_var._parameters.target_geometry, "NULL" ? "NULL" : "", 16384);
  else 
  _test_logger_2D_space_zx_con_PSD_var._parameters.target_geometry[0]='\0';
  if("NULL" && strlen("NULL"))
    stracpy(_test_logger_2D_space_zx_con_PSD_var._parameters.target_process, "NULL" ? "NULL" : "", 16384);
  else 
  _test_logger_2D_space_zx_con_PSD_var._parameters.target_process[0]='\0';
  if("z" && strlen("z"))
    stracpy(_test_logger_2D_space_zx_con_PSD_var._parameters.D_direction_1, "z" ? "z" : "", 16384);
  else 
  _test_logger_2D_space_zx_con_PSD_var._parameters.D_direction_1[0]='\0';
  _test_logger_2D_space_zx_con_PSD_var._parameters.D1_min = -0.1;
  _test_logger_2D_space_zx_con_PSD_var._parameters.D1_max = 0.1;
  _test_logger_2D_space_zx_con_PSD_var._parameters.n1 = 200;
  if("x" && strlen("x"))
    stracpy(_test_logger_2D_space_zx_con_PSD_var._parameters.D_direction_2, "x" ? "x" : "", 16384);
  else 
  _test_logger_2D_space_zx_con_PSD_var._parameters.D_direction_2[0]='\0';
  _test_logger_2D_space_zx_con_PSD_var._parameters.D2_min = -0.1;
  _test_logger_2D_space_zx_con_PSD_var._parameters.D2_max = 0.1;
  _test_logger_2D_space_zx_con_PSD_var._parameters.n2 = 200;
  if("logger_2D_space_zx_con_PSD.dat" && strlen("logger_2D_space_zx_con_PSD.dat"))
    stracpy(_test_logger_2D_space_zx_con_PSD_var._parameters.filename, "logger_2D_space_zx_con_PSD.dat" ? "logger_2D_space_zx_con_PSD.dat" : "", 16384);
  else 
  _test_logger_2D_space_zx_con_PSD_var._parameters.filename[0]='\0';
  _test_logger_2D_space_zx_con_PSD_var._parameters.order_total = 0;
  _test_logger_2D_space_zx_con_PSD_var._parameters.order_volume = 0;
  _test_logger_2D_space_zx_con_PSD_var._parameters.order_volume_process = 0;
  _test_logger_2D_space_zx_con_PSD_var._parameters.logger_conditional_extend_index = -1;
  if("init" && strlen("init"))
    stracpy(_test_logger_2D_space_zx_con_PSD_var._parameters.init, "init" ? "init" : "", 16384);
  else 
  _test_logger_2D_space_zx_con_PSD_var._parameters.init[0]='\0';


  /* component test_logger_2D_space_zx_con_PSD=Union_logger_2D_space() AT ROTATED */
  {
    Coords tc1, tc2;
    tc1 = coords_set(0,0,0);
    tc2 = coords_set(0,0,0);
    Rotation tr1;
    rot_set_rotation(tr1,0,0,0);
    rot_set_rotation(tr1,
      (0)*DEG2RAD, (0)*DEG2RAD, (0)*DEG2RAD);
    rot_mul(tr1, _sample_pos_var._rotation_absolute, _test_logger_2D_space_zx_con_PSD_var._rotation_absolute);
    rot_transpose(_source_var._rotation_absolute, tr1);
    rot_mul(_test_logger_2D_space_zx_con_PSD_var._rotation_absolute, tr1, _test_logger_2D_space_zx_con_PSD_var._rotation_relative);
    _test_logger_2D_space_zx_con_PSD_var._rotation_is_identity =  rot_test_identity(_test_logger_2D_space_zx_con_PSD_var._rotation_relative);
    tc1 = coords_set(
      0, 0, 0);
    rot_transpose(_sample_pos_var._rotation_absolute, tr1);
    tc2 = rot_apply(tr1, tc1);
    _test_logger_2D_space_zx_con_PSD_var._position_absolute = coords_add(_sample_pos_var._position_absolute, tc2);
    tc1 = coords_sub(_source_var._position_absolute, _test_logger_2D_space_zx_con_PSD_var._position_absolute);
    _test_logger_2D_space_zx_con_PSD_var._position_relative = rot_apply(_test_logger_2D_space_zx_con_PSD_var._rotation_absolute, tc1);
  } /* test_logger_2D_space_zx_con_PSD=Union_logger_2D_space() AT ROTATED */
  DEBUG_COMPONENT("test_logger_2D_space_zx_con_PSD", _test_logger_2D_space_zx_con_PSD_var._position_absolute, _test_logger_2D_space_zx_con_PSD_var._rotation_absolute);
  instrument->_position_absolute[24] = _test_logger_2D_space_zx_con_PSD_var._position_absolute;
  instrument->_position_relative[24] = _test_logger_2D_space_zx_con_PSD_var._position_relative;
    _test_logger_2D_space_zx_con_PSD_var._position_relative_is_zero =  coords_test_zero(_test_logger_2D_space_zx_con_PSD_var._position_relative);
  instrument->counter_N[24]  = instrument->counter_P[24] = instrument->counter_P2[24] = 0;
  instrument->counter_AbsorbProp[24]= 0;
  #ifdef USE_NEXUS
  if(nxhandle) {
    if ((!mcdotrace) && mcformat && strcasestr(mcformat, "NeXus")) {
    MPI_MASTER(
        mccomp_placement_type_nexus(nxhandle,"0023_test_logger_2D_space_zx_con_PSD", _test_logger_2D_space_zx_con_PSD_var._position_absolute, _test_logger_2D_space_zx_con_PSD_var._rotation_absolute, "Union_logger_2D_space");
        mccomp_param_nexus(nxhandle,"0023_test_logger_2D_space_zx_con_PSD", "target_geometry", "NULL", "NULL", "char*");
        mccomp_param_nexus(nxhandle,"0023_test_logger_2D_space_zx_con_PSD", "target_process", "NULL", "NULL", "char*");
        mccomp_param_nexus(nxhandle,"0023_test_logger_2D_space_zx_con_PSD", "D_direction_1", "x", "z", "char*");
        mccomp_param_nexus(nxhandle,"0023_test_logger_2D_space_zx_con_PSD", "D1_min", "-5", "-0.1","MCNUM");
        mccomp_param_nexus(nxhandle,"0023_test_logger_2D_space_zx_con_PSD", "D1_max", "5", "0.1","MCNUM");
        mccomp_param_nexus(nxhandle,"0023_test_logger_2D_space_zx_con_PSD", "n1", "90", "200","MCNUM");
        mccomp_param_nexus(nxhandle,"0023_test_logger_2D_space_zx_con_PSD", "D_direction_2", "z", "x", "char*");
        mccomp_param_nexus(nxhandle,"0023_test_logger_2D_space_zx_con_PSD", "D2_min", "-5", "-0.1","MCNUM");
        mccomp_param_nexus(nxhandle,"0023_test_logger_2D_space_zx_con_PSD", "D2_max", "5", "0.1","MCNUM");
        mccomp_param_nexus(nxhandle,"0023_test_logger_2D_space_zx_con_PSD", "n2", "90", "200","MCNUM");
        mccomp_param_nexus(nxhandle,"0023_test_logger_2D_space_zx_con_PSD", "filename", "NULL", "logger_2D_space_zx_con_PSD.dat", "char*");
        mccomp_param_nexus(nxhandle,"0023_test_logger_2D_space_zx_con_PSD", "order_total", "0", "0","MCNUM");
        mccomp_param_nexus(nxhandle,"0023_test_logger_2D_space_zx_con_PSD", "order_volume", "0", "0","MCNUM");
        mccomp_param_nexus(nxhandle,"0023_test_logger_2D_space_zx_con_PSD", "order_volume_process", "0", "0","MCNUM");
        mccomp_param_nexus(nxhandle,"0023_test_logger_2D_space_zx_con_PSD", "logger_conditional_extend_index", "-1", "-1","MCNUM");
        mccomp_param_nexus(nxhandle,"0023_test_logger_2D_space_zx_con_PSD", "init", "init", "init", "char*");
      );
    }
  } else {
    // fprintf(stderr,"NO NEXUS FILE");
  }
  #endif
  return(0);
} /* _test_logger_2D_space_zx_con_PSD_setpos */

/* component test_conditional_PSD=Union_conditional_PSD() SETTING, POSITION/ROTATION */
int _test_conditional_PSD_setpos(void)
{ /* sets initial component parameters, position and rotation */
  SIG_MESSAGE("[_test_conditional_PSD_setpos] component test_conditional_PSD=Union_conditional_PSD() SETTING [Union_conditional_PSD:0]");
  stracpy(_test_conditional_PSD_var._name, "test_conditional_PSD", 16384);
  stracpy(_test_conditional_PSD_var._type, "Union_conditional_PSD", 16384);
  _test_conditional_PSD_var._index=25;
  int current_setpos_index = 25;
  if("test_logger_2D_space_zx_con_PSD" && strlen("test_logger_2D_space_zx_con_PSD"))
    stracpy(_test_conditional_PSD_var._parameters.target_loggers, "test_logger_2D_space_zx_con_PSD" ? "test_logger_2D_space_zx_con_PSD" : "", 16384);
  else 
  _test_conditional_PSD_var._parameters.target_loggers[0]='\0';
  _test_conditional_PSD_var._parameters.master_tagging = 0;
  _test_conditional_PSD_var._parameters.tagging_extend_index = -1;
  _test_conditional_PSD_var._parameters.xwidth = 0.085;
  _test_conditional_PSD_var._parameters.yheight = 0.15;
  _test_conditional_PSD_var._parameters.time_min = 0;
  _test_conditional_PSD_var._parameters.time_max = 0;
  _test_conditional_PSD_var._parameters.overwrite_logger_weight = 0;
  if("init" && strlen("init"))
    stracpy(_test_conditional_PSD_var._parameters.init, "init" ? "init" : "", 16384);
  else 
  _test_conditional_PSD_var._parameters.init[0]='\0';


  /* component test_conditional_PSD=Union_conditional_PSD() AT ROTATED */
  {
    Coords tc1, tc2;
    tc1 = coords_set(0,0,0);
    tc2 = coords_set(0,0,0);
    Rotation tr1;
    rot_set_rotation(tr1,0,0,0);
    rot_set_rotation(tr1,
      (0.0)*DEG2RAD, (0.0)*DEG2RAD, (0.0)*DEG2RAD);
    rot_mul(tr1, _scat_direction_var._rotation_absolute, _test_conditional_PSD_var._rotation_absolute);
    rot_transpose(_source_var._rotation_absolute, tr1);
    rot_mul(_test_conditional_PSD_var._rotation_absolute, tr1, _test_conditional_PSD_var._rotation_relative);
    _test_conditional_PSD_var._rotation_is_identity =  rot_test_identity(_test_conditional_PSD_var._rotation_relative);
    tc1 = coords_set(
      0, 0, 0.5);
    rot_transpose(_scat_direction_var._rotation_absolute, tr1);
    tc2 = rot_apply(tr1, tc1);
    _test_conditional_PSD_var._position_absolute = coords_add(_scat_direction_var._position_absolute, tc2);
    tc1 = coords_sub(_source_var._position_absolute, _test_conditional_PSD_var._position_absolute);
    _test_conditional_PSD_var._position_relative = rot_apply(_test_conditional_PSD_var._rotation_absolute, tc1);
  } /* test_conditional_PSD=Union_conditional_PSD() AT ROTATED */
  DEBUG_COMPONENT("test_conditional_PSD", _test_conditional_PSD_var._position_absolute, _test_conditional_PSD_var._rotation_absolute);
  instrument->_position_absolute[25] = _test_conditional_PSD_var._position_absolute;
  instrument->_position_relative[25] = _test_conditional_PSD_var._position_relative;
    _test_conditional_PSD_var._position_relative_is_zero =  coords_test_zero(_test_conditional_PSD_var._position_relative);
  instrument->counter_N[25]  = instrument->counter_P[25] = instrument->counter_P2[25] = 0;
  instrument->counter_AbsorbProp[25]= 0;
  #ifdef USE_NEXUS
  if(nxhandle) {
    if ((!mcdotrace) && mcformat && strcasestr(mcformat, "NeXus")) {
    MPI_MASTER(
        mccomp_placement_type_nexus(nxhandle,"0024_test_conditional_PSD", _test_conditional_PSD_var._position_absolute, _test_conditional_PSD_var._rotation_absolute, "Union_conditional_PSD");
        mccomp_param_nexus(nxhandle,"0024_test_conditional_PSD", "target_loggers", "NULL", "test_logger_2D_space_zx_con_PSD", "char*");
        mccomp_param_nexus(nxhandle,"0024_test_conditional_PSD", "master_tagging", "0", "0","int");
        mccomp_param_nexus(nxhandle,"0024_test_conditional_PSD", "tagging_extend_index", "-1", "-1","int");
        mccomp_param_nexus(nxhandle,"0024_test_conditional_PSD", "xwidth", "NONE", "0.085","MCNUM");
        mccomp_param_nexus(nxhandle,"0024_test_conditional_PSD", "yheight", "NONE", "0.15","MCNUM");
        mccomp_param_nexus(nxhandle,"0024_test_conditional_PSD", "time_min", "0", "0","MCNUM");
        mccomp_param_nexus(nxhandle,"0024_test_conditional_PSD", "time_max", "0", "0","MCNUM");
        mccomp_param_nexus(nxhandle,"0024_test_conditional_PSD", "overwrite_logger_weight", "0", "0","MCNUM");
        mccomp_param_nexus(nxhandle,"0024_test_conditional_PSD", "init", "init", "init", "char*");
      );
    }
  } else {
    // fprintf(stderr,"NO NEXUS FILE");
  }
  #endif
  return(0);
} /* _test_conditional_PSD_setpos */

/* component sample=Union_master() SETTING, POSITION/ROTATION */
int _sample_setpos(void)
{ /* sets initial component parameters, position and rotation */
  SIG_MESSAGE("[_sample_setpos] component sample=Union_master() SETTING [Union_master:0]");
  stracpy(_sample_var._name, "sample", 16384);
  stracpy(_sample_var._type, "Union_master", 16384);
  _sample_var._index=26;
  int current_setpos_index = 26;
  _sample_var._parameters.enable_refraction = 1;
  _sample_var._parameters.enable_reflection = 1;
  _sample_var._parameters.verbal = 0;
  _sample_var._parameters.list_verbal = 0;
  _sample_var._parameters.finally_verbal = 0;
  _sample_var._parameters.allow_inside_start = 0;
  _sample_var._parameters.enable_tagging = 0;
  _sample_var._parameters.history_limit = 300000;
  _sample_var._parameters.enable_conditionals = 1;
  _sample_var._parameters.inherit_number_of_scattering_events = 0;
  _sample_var._parameters.weight_ratio_limit = 1e-90;
  if("init" && strlen("init"))
    stracpy(_sample_var._parameters.init, "init" ? "init" : "", 16384);
  else 
  _sample_var._parameters.init[0]='\0';


  /* component sample=Union_master() AT ROTATED */
  {
    Coords tc1, tc2;
    tc1 = coords_set(0,0,0);
    tc2 = coords_set(0,0,0);
    Rotation tr1;
    rot_set_rotation(tr1,0,0,0);
    rot_set_rotation(tr1,
      (0)*DEG2RAD, (0)*DEG2RAD, (0)*DEG2RAD);
    rot_mul(tr1, _sample_pos_var._rotation_absolute, _sample_var._rotation_absolute);
    rot_transpose(_source_var._rotation_absolute, tr1);
    rot_mul(_sample_var._rotation_absolute, tr1, _sample_var._rotation_relative);
    _sample_var._rotation_is_identity =  rot_test_identity(_sample_var._rotation_relative);
    tc1 = coords_set(
      0, 0, 0);
    rot_transpose(_sample_pos_var._rotation_absolute, tr1);
    tc2 = rot_apply(tr1, tc1);
    _sample_var._position_absolute = coords_add(_sample_pos_var._position_absolute, tc2);
    tc1 = coords_sub(_source_var._position_absolute, _sample_var._position_absolute);
    _sample_var._position_relative = rot_apply(_sample_var._rotation_absolute, tc1);
  } /* sample=Union_master() AT ROTATED */
  DEBUG_COMPONENT("sample", _sample_var._position_absolute, _sample_var._rotation_absolute);
  instrument->_position_absolute[26] = _sample_var._position_absolute;
  instrument->_position_relative[26] = _sample_var._position_relative;
    _sample_var._position_relative_is_zero =  coords_test_zero(_sample_var._position_relative);
  instrument->counter_N[26]  = instrument->counter_P[26] = instrument->counter_P2[26] = 0;
  instrument->counter_AbsorbProp[26]= 0;
  #ifdef USE_NEXUS
  if(nxhandle) {
    if ((!mcdotrace) && mcformat && strcasestr(mcformat, "NeXus")) {
    MPI_MASTER(
        mccomp_placement_type_nexus(nxhandle,"0025_sample", _sample_var._position_absolute, _sample_var._rotation_absolute, "Union_master");
        mccomp_param_nexus(nxhandle,"0025_sample", "enable_refraction", "1", "1","int");
        mccomp_param_nexus(nxhandle,"0025_sample", "enable_reflection", "1", "1","int");
        mccomp_param_nexus(nxhandle,"0025_sample", "verbal", "0", "0","MCNUM");
        mccomp_param_nexus(nxhandle,"0025_sample", "list_verbal", "0", "0","MCNUM");
        mccomp_param_nexus(nxhandle,"0025_sample", "finally_verbal", "0", "0","MCNUM");
        mccomp_param_nexus(nxhandle,"0025_sample", "allow_inside_start", "0", "0","MCNUM");
        mccomp_param_nexus(nxhandle,"0025_sample", "enable_tagging", "0", "0","MCNUM");
        mccomp_param_nexus(nxhandle,"0025_sample", "history_limit", "300000", "300000","MCNUM");
        mccomp_param_nexus(nxhandle,"0025_sample", "enable_conditionals", "1", "1","MCNUM");
        mccomp_param_nexus(nxhandle,"0025_sample", "inherit_number_of_scattering_events", "0", "0","MCNUM");
        mccomp_param_nexus(nxhandle,"0025_sample", "weight_ratio_limit", "1e-90", "1e-90","MCNUM");
        mccomp_param_nexus(nxhandle,"0025_sample", "init", "init", "init", "char*");
      );
    }
  } else {
    // fprintf(stderr,"NO NEXUS FILE");
  }
  #endif
  return(0);
} /* _sample_setpos */

/* component detector=PSD_monitor() SETTING, POSITION/ROTATION */
int _detector_setpos(void)
{ /* sets initial component parameters, position and rotation */
  SIG_MESSAGE("[_detector_setpos] component detector=PSD_monitor() SETTING [PSD_monitor:0]");
  stracpy(_detector_var._name, "detector", 16384);
  stracpy(_detector_var._type, "PSD_monitor", 16384);
  _detector_var._index=27;
  int current_setpos_index = 27;
  _detector_var._parameters.nx = 200;
  _detector_var._parameters.ny = 200;
  if("PSD.dat" && strlen("PSD.dat"))
    stracpy(_detector_var._parameters.filename, "PSD.dat" ? "PSD.dat" : "", 16384);
  else 
  _detector_var._parameters.filename[0]='\0';
  _detector_var._parameters.xmin = -0.05;
  _detector_var._parameters.xmax = 0.05;
  _detector_var._parameters.ymin = -0.05;
  _detector_var._parameters.ymax = 0.05;
  _detector_var._parameters.xwidth = 0.085;
  _detector_var._parameters.yheight = 0.15;
  _detector_var._parameters.restore_neutron = 1;
  _detector_var._parameters.nowritefile = 0;


  /* component detector=PSD_monitor() AT ROTATED */
  {
    Coords tc1, tc2;
    tc1 = coords_set(0,0,0);
    tc2 = coords_set(0,0,0);
    Rotation tr1;
    rot_set_rotation(tr1,0,0,0);
    rot_set_rotation(tr1,
      (0)*DEG2RAD, (0)*DEG2RAD, (0)*DEG2RAD);
    rot_mul(tr1, _sample_pos_var._rotation_absolute, _detector_var._rotation_absolute);
    rot_transpose(_sample_var._rotation_absolute, tr1);
    rot_mul(_detector_var._rotation_absolute, tr1, _detector_var._rotation_relative);
    _detector_var._rotation_is_identity =  rot_test_identity(_detector_var._rotation_relative);
    tc1 = coords_set(
      0, 0, 0.3);
    rot_transpose(_sample_pos_var._rotation_absolute, tr1);
    tc2 = rot_apply(tr1, tc1);
    _detector_var._position_absolute = coords_add(_sample_pos_var._position_absolute, tc2);
    tc1 = coords_sub(_sample_var._position_absolute, _detector_var._position_absolute);
    _detector_var._position_relative = rot_apply(_detector_var._rotation_absolute, tc1);
  } /* detector=PSD_monitor() AT ROTATED */
  DEBUG_COMPONENT("detector", _detector_var._position_absolute, _detector_var._rotation_absolute);
  instrument->_position_absolute[27] = _detector_var._position_absolute;
  instrument->_position_relative[27] = _detector_var._position_relative;
    _detector_var._position_relative_is_zero =  coords_test_zero(_detector_var._position_relative);
  instrument->counter_N[27]  = instrument->counter_P[27] = instrument->counter_P2[27] = 0;
  instrument->counter_AbsorbProp[27]= 0;
  #ifdef USE_NEXUS
  if(nxhandle) {
    if ((!mcdotrace) && mcformat && strcasestr(mcformat, "NeXus")) {
    MPI_MASTER(
        mccomp_placement_type_nexus(nxhandle,"0026_detector", _detector_var._position_absolute, _detector_var._rotation_absolute, "PSD_monitor");
        mccomp_param_nexus(nxhandle,"0026_detector", "nx", "90", "200","int");
        mccomp_param_nexus(nxhandle,"0026_detector", "ny", "90", "200","int");
        mccomp_param_nexus(nxhandle,"0026_detector", "filename", 0, "PSD.dat", "char*");
        mccomp_param_nexus(nxhandle,"0026_detector", "xmin", "-0.05", "-0.05","MCNUM");
        mccomp_param_nexus(nxhandle,"0026_detector", "xmax", "0.05", "0.05","MCNUM");
        mccomp_param_nexus(nxhandle,"0026_detector", "ymin", "-0.05", "-0.05","MCNUM");
        mccomp_param_nexus(nxhandle,"0026_detector", "ymax", "0.05", "0.05","MCNUM");
        mccomp_param_nexus(nxhandle,"0026_detector", "xwidth", "0", "0.085","MCNUM");
        mccomp_param_nexus(nxhandle,"0026_detector", "yheight", "0", "0.15","MCNUM");
        mccomp_param_nexus(nxhandle,"0026_detector", "restore_neutron", "0", "1","int");
        mccomp_param_nexus(nxhandle,"0026_detector", "nowritefile", "0", "0","int");
      );
    }
  } else {
    // fprintf(stderr,"NO NEXUS FILE");
  }
  #endif
  return(0);
} /* _detector_setpos */

/* component detector_scat=PSD_monitor() SETTING, POSITION/ROTATION */
int _detector_scat_setpos(void)
{ /* sets initial component parameters, position and rotation */
  SIG_MESSAGE("[_detector_scat_setpos] component detector_scat=PSD_monitor() SETTING [PSD_monitor:0]");
  stracpy(_detector_scat_var._name, "detector_scat", 16384);
  stracpy(_detector_scat_var._type, "PSD_monitor", 16384);
  _detector_scat_var._index=28;
  int current_setpos_index = 28;
  _detector_scat_var._parameters.nx = 200;
  _detector_scat_var._parameters.ny = 200;
  if("PSD_scat.dat" && strlen("PSD_scat.dat"))
    stracpy(_detector_scat_var._parameters.filename, "PSD_scat.dat" ? "PSD_scat.dat" : "", 16384);
  else 
  _detector_scat_var._parameters.filename[0]='\0';
  _detector_scat_var._parameters.xmin = -0.05;
  _detector_scat_var._parameters.xmax = 0.05;
  _detector_scat_var._parameters.ymin = -0.05;
  _detector_scat_var._parameters.ymax = 0.05;
  _detector_scat_var._parameters.xwidth = 0.085;
  _detector_scat_var._parameters.yheight = 0.15;
  _detector_scat_var._parameters.restore_neutron = 1;
  _detector_scat_var._parameters.nowritefile = 0;


  /* component detector_scat=PSD_monitor() AT ROTATED */
  {
    Coords tc1, tc2;
    tc1 = coords_set(0,0,0);
    tc2 = coords_set(0,0,0);
    Rotation tr1;
    rot_set_rotation(tr1,0,0,0);
    rot_set_rotation(tr1,
      (0)*DEG2RAD, (0)*DEG2RAD, (0)*DEG2RAD);
    rot_mul(tr1, _scat_direction_var._rotation_absolute, _detector_scat_var._rotation_absolute);
    rot_transpose(_detector_var._rotation_absolute, tr1);
    rot_mul(_detector_scat_var._rotation_absolute, tr1, _detector_scat_var._rotation_relative);
    _detector_scat_var._rotation_is_identity =  rot_test_identity(_detector_scat_var._rotation_relative);
    tc1 = coords_set(
      0, 0, 0.5);
    rot_transpose(_scat_direction_var._rotation_absolute, tr1);
    tc2 = rot_apply(tr1, tc1);
    _detector_scat_var._position_absolute = coords_add(_scat_direction_var._position_absolute, tc2);
    tc1 = coords_sub(_detector_var._position_absolute, _detector_scat_var._position_absolute);
    _detector_scat_var._position_relative = rot_apply(_detector_scat_var._rotation_absolute, tc1);
  } /* detector_scat=PSD_monitor() AT ROTATED */
  DEBUG_COMPONENT("detector_scat", _detector_scat_var._position_absolute, _detector_scat_var._rotation_absolute);
  instrument->_position_absolute[28] = _detector_scat_var._position_absolute;
  instrument->_position_relative[28] = _detector_scat_var._position_relative;
    _detector_scat_var._position_relative_is_zero =  coords_test_zero(_detector_scat_var._position_relative);
  instrument->counter_N[28]  = instrument->counter_P[28] = instrument->counter_P2[28] = 0;
  instrument->counter_AbsorbProp[28]= 0;
  #ifdef USE_NEXUS
  if(nxhandle) {
    if ((!mcdotrace) && mcformat && strcasestr(mcformat, "NeXus")) {
    MPI_MASTER(
        mccomp_placement_type_nexus(nxhandle,"0027_detector_scat", _detector_scat_var._position_absolute, _detector_scat_var._rotation_absolute, "PSD_monitor");
        mccomp_param_nexus(nxhandle,"0027_detector_scat", "nx", "90", "200","int");
        mccomp_param_nexus(nxhandle,"0027_detector_scat", "ny", "90", "200","int");
        mccomp_param_nexus(nxhandle,"0027_detector_scat", "filename", 0, "PSD_scat.dat", "char*");
        mccomp_param_nexus(nxhandle,"0027_detector_scat", "xmin", "-0.05", "-0.05","MCNUM");
        mccomp_param_nexus(nxhandle,"0027_detector_scat", "xmax", "0.05", "0.05","MCNUM");
        mccomp_param_nexus(nxhandle,"0027_detector_scat", "ymin", "-0.05", "-0.05","MCNUM");
        mccomp_param_nexus(nxhandle,"0027_detector_scat", "ymax", "0.05", "0.05","MCNUM");
        mccomp_param_nexus(nxhandle,"0027_detector_scat", "xwidth", "0", "0.085","MCNUM");
        mccomp_param_nexus(nxhandle,"0027_detector_scat", "yheight", "0", "0.15","MCNUM");
        mccomp_param_nexus(nxhandle,"0027_detector_scat", "restore_neutron", "0", "1","int");
        mccomp_param_nexus(nxhandle,"0027_detector_scat", "nowritefile", "0", "0","int");
      );
    }
  } else {
    // fprintf(stderr,"NO NEXUS FILE");
  }
  #endif
  return(0);
} /* _detector_scat_setpos */

/* component stop=Union_stop() SETTING, POSITION/ROTATION */
int _stop_setpos(void)
{ /* sets initial component parameters, position and rotation */
  SIG_MESSAGE("[_stop_setpos] component stop=Union_stop() SETTING [Union_stop:0]");
  stracpy(_stop_var._name, "stop", 16384);
  stracpy(_stop_var._type, "Union_stop", 16384);
  _stop_var._index=29;
  int current_setpos_index = 29;
  /* component stop=Union_stop() AT ROTATED */
  {
    Coords tc1, tc2;
    tc1 = coords_set(0,0,0);
    tc2 = coords_set(0,0,0);
    Rotation tr1;
    rot_set_rotation(tr1,0,0,0);
    rot_set_rotation(_stop_var._rotation_absolute,
      (0.0)*DEG2RAD, (0.0)*DEG2RAD, (0.0)*DEG2RAD);
    rot_transpose(_detector_scat_var._rotation_absolute, tr1);
    rot_mul(_stop_var._rotation_absolute, tr1, _stop_var._rotation_relative);
    _stop_var._rotation_is_identity =  rot_test_identity(_stop_var._rotation_relative);
    _stop_var._position_absolute = coords_set(
      0, 0, 0);
    tc1 = coords_sub(_detector_scat_var._position_absolute, _stop_var._position_absolute);
    _stop_var._position_relative = rot_apply(_stop_var._rotation_absolute, tc1);
  } /* stop=Union_stop() AT ROTATED */
  DEBUG_COMPONENT("stop", _stop_var._position_absolute, _stop_var._rotation_absolute);
  instrument->_position_absolute[29] = _stop_var._position_absolute;
  instrument->_position_relative[29] = _stop_var._position_relative;
    _stop_var._position_relative_is_zero =  coords_test_zero(_stop_var._position_relative);
  instrument->counter_N[29]  = instrument->counter_P[29] = instrument->counter_P2[29] = 0;
  instrument->counter_AbsorbProp[29]= 0;
  #ifdef USE_NEXUS
  if(nxhandle) {
    if ((!mcdotrace) && mcformat && strcasestr(mcformat, "NeXus")) {
    MPI_MASTER(
        mccomp_placement_type_nexus(nxhandle,"0028_stop", _stop_var._position_absolute, _stop_var._rotation_absolute, "Union_stop");
      );
    }
  } else {
    // fprintf(stderr,"NO NEXUS FILE");
  }
  #endif
  return(0);
} /* _stop_setpos */

_class_Union_init *class_Union_init_init(_class_Union_init *_comp
) {
  #define global_positions_to_transform_list (_comp->_parameters.global_positions_to_transform_list)
  #define global_rotations_to_transform_list (_comp->_parameters.global_rotations_to_transform_list)
  #define global_process_list (_comp->_parameters.global_process_list)
  #define global_material_list (_comp->_parameters.global_material_list)
  #define global_surface_list (_comp->_parameters.global_surface_list)
  #define global_geometry_list (_comp->_parameters.global_geometry_list)
  #define global_all_volume_logger_list (_comp->_parameters.global_all_volume_logger_list)
  #define global_specific_volumes_logger_list (_comp->_parameters.global_specific_volumes_logger_list)
  #define global_all_volume_abs_logger_list (_comp->_parameters.global_all_volume_abs_logger_list)
  #define global_specific_volumes_abs_logger_list (_comp->_parameters.global_specific_volumes_abs_logger_list)
  #define global_tagging_conditional_list (_comp->_parameters.global_tagging_conditional_list)
  #define global_master_list (_comp->_parameters.global_master_list)
  #define global_mantid_min_pixel_id (_comp->_parameters.global_mantid_min_pixel_id)
  SIG_MESSAGE("[_init_init] component init=Union_init() INITIALISE [Union_init:0]");

  global_positions_to_transform_list.num_elements = 0;
  global_positions_to_transform_list.positions = NULL;

  global_rotations_to_transform_list.num_elements = 0;
  global_rotations_to_transform_list.rotations = NULL;

  global_process_list.num_elements = 0;
  global_process_list.elements = NULL;

  global_material_list.num_elements = 0;
  global_material_list.elements = NULL;

  global_surface_list.num_elements = 0;
  global_surface_list.elements = NULL;

  global_geometry_list.num_elements = 0;
  global_geometry_list.elements = NULL;

  global_all_volume_logger_list.num_elements = 0;
  global_all_volume_logger_list.elements = NULL;

  global_specific_volumes_logger_list.num_elements = 0;
  global_specific_volumes_logger_list.elements = NULL;

  global_all_volume_abs_logger_list.num_elements = 0;
  global_all_volume_abs_logger_list.elements = NULL;

  global_specific_volumes_abs_logger_list.num_elements = 0;
  global_specific_volumes_abs_logger_list.elements = NULL;

  global_tagging_conditional_list.num_elements = 0;
  global_tagging_conditional_list.elements = NULL;

  global_master_list.num_elements = 0;
  global_master_list.elements = NULL;

  global_mantid_min_pixel_id = 0;
  #undef global_positions_to_transform_list
  #undef global_rotations_to_transform_list
  #undef global_process_list
  #undef global_material_list
  #undef global_surface_list
  #undef global_geometry_list
  #undef global_all_volume_logger_list
  #undef global_specific_volumes_logger_list
  #undef global_all_volume_abs_logger_list
  #undef global_specific_volumes_abs_logger_list
  #undef global_tagging_conditional_list
  #undef global_master_list
  #undef global_mantid_min_pixel_id
  return(_comp);
} /* class_Union_init_init */

_class_Incoherent_process *class_Incoherent_process_init(_class_Incoherent_process *_comp
) {
  #define sigma (_comp->_parameters.sigma)
  #define f_QE (_comp->_parameters.f_QE)
  #define gamma (_comp->_parameters.gamma)
  #define packing_factor (_comp->_parameters.packing_factor)
  #define unit_cell_volume (_comp->_parameters.unit_cell_volume)
  #define interact_fraction (_comp->_parameters.interact_fraction)
  #define init (_comp->_parameters.init)
  #define global_process_element (_comp->_parameters.global_process_element)
  #define This_process (_comp->_parameters.This_process)
  #define Incoherent_storage (_comp->_parameters.Incoherent_storage)
  #define effective_my_scattering (_comp->_parameters.effective_my_scattering)
  SIG_MESSAGE("[_Vanadium_incoherent_init] component Vanadium_incoherent=Incoherent_process() INITIALISE [Incoherent_process:0]");

  // Initialize done in the component
  effective_my_scattering = ((packing_factor / unit_cell_volume) * 100 * sigma);
  Incoherent_storage.my_scattering = effective_my_scattering;

  Incoherent_storage.QE_sampling_frequency = f_QE;
  Incoherent_storage.lorentzian_width = gamma;

  // First initialise This_process with default values:
  scattering_process_struct_init (&This_process);

  // Need to specify if this process is isotropic
  This_process.non_isotropic_rot_index = -1; // Yes (powder)
  // This_process.non_isotropic_rot_index =  1;  // No (single crystal)

  // Need to specify if this process need to use focusing in calculation of inverse penetration depth (physics_my)
  // This_process.needs_cross_section_focus = 1; // Yes
  This_process.needs_cross_section_focus = -1; // No

  // The type of the process must be saved in the global enum process
  This_process.eProcess = Incoherent;

  // Packing the data into a structure that is transported to the main component
  sprintf (This_process.name, "%s", NAME_CURRENT_COMP);
  This_process.process_p_interact = interact_fraction;
  This_process.data_transfer.pointer_to_a_Incoherent_physics_storage_struct = &Incoherent_storage;
  // This_process.data_transfer.pointer_to_a_Incoherent_physics_storage_struct->my_scattering = effective_my_scattering;
  This_process.probability_for_scattering_function = &Incoherent_physics_my;
  This_process.scattering_function = &Incoherent_physics_scattering;

  // This will be the same for all process's, and can thus be moved to an include.
  sprintf (global_process_element.name, "%s", NAME_CURRENT_COMP);
  global_process_element.component_index = INDEX_CURRENT_COMP;
  global_process_element.p_scattering_process = &This_process;

  if (_getcomp_index (init) < 0) {
    fprintf (stderr, "Incoherent_process:%s: Error identifying Union_init component, %s is not a known component name.\n", NAME_CURRENT_COMP, init);
    exit (-1);
  }

  struct pointer_to_global_process_list* global_process_list = COMP_GETPAR3 (Union_init, init, global_process_list);
  add_element_to_process_list (global_process_list, global_process_element);
  #undef sigma
  #undef f_QE
  #undef gamma
  #undef packing_factor
  #undef unit_cell_volume
  #undef interact_fraction
  #undef init
  #undef global_process_element
  #undef This_process
  #undef Incoherent_storage
  #undef effective_my_scattering
  return(_comp);
} /* class_Incoherent_process_init */

_class_Union_make_material *class_Union_make_material_init(_class_Union_make_material *_comp
) {
  #define process_string (_comp->_parameters.process_string)
  #define my_absorption (_comp->_parameters.my_absorption)
  #define absorber (_comp->_parameters.absorber)
  #define refraction_density (_comp->_parameters.refraction_density)
  #define refraction_sigma_coh (_comp->_parameters.refraction_sigma_coh)
  #define refraction_weight (_comp->_parameters.refraction_weight)
  #define refraction_SLD (_comp->_parameters.refraction_SLD)
  #define init (_comp->_parameters.init)
  #define global_material_element (_comp->_parameters.global_material_element)
  #define this_material (_comp->_parameters.this_material)
  #define loop_index (_comp->_parameters.loop_index)
  #define found_process (_comp->_parameters.found_process)
  #define specified_processes (_comp->_parameters.specified_processes)
  #define local_string (_comp->_parameters.local_string)
  #define accepted_processes (_comp->_parameters.accepted_processes)
  SIG_MESSAGE("[_Vanadium_init] component Vanadium=Union_make_material() INITIALISE [Union_make_material:0]");


  accepted_processes.num_elements = 0;
  accepted_processes.elements = NULL;

  if (0 == strcmp (NAME_CURRENT_COMP, "vacuum") || 0 == strcmp (NAME_CURRENT_COMP, "Vacuum")) {
    printf ("ERROR, a Union material may not be called Vacuum. A vacuum volume may be created by material=\"Vacuum\" in a geometry component.\n");
    exit (1);
  }
  if (0 == strcmp (NAME_CURRENT_COMP, "exit") || 0 == strcmp (NAME_CURRENT_COMP, "Exit")) {
    printf ("ERROR, a Union material may not be called Exit. A exit volume may be created by material=\"Exit\" in a geometry component.\n");
    exit (1);
  }
  if (my_absorption < 0) {
    printf ("ERROR, Union make material named %s have a negative absorption cross section!.\n", NAME_CURRENT_COMP);
    exit (1);
  }

  if (_getcomp_index (init) < 0) {
    fprintf (stderr, "Union_make_material:%s: Error identifying Union_init component, %s is not a known component name.\n", NAME_CURRENT_COMP, init);
    exit (-1);
  }

  struct pointer_to_global_process_list* global_process_list = COMP_GETPAR3 (Union_init, init, global_process_list);
  struct pointer_to_global_material_list* global_material_list = COMP_GETPAR3 (Union_init, init, global_material_list);

  if (absorber == 0) {
    if (process_string && strlen (process_string) && strcmp (process_string, "NULL") && strcmp (process_string, "0")) {
      manual_linking_function_material (process_string, global_process_list, &accepted_processes, NAME_CURRENT_COMP);
    } else {
      for (loop_index = 0; loop_index < global_process_list->num_elements; loop_index++) {
        // printf("Automatic linking chosen [loop index = %d] with process_string = %s  \n",loop_index,process_string);
        // automatic linking
        // accept a process if index is between current and former index of make_material components
        if (1 == automatic_linking_materials_function (global_process_list->elements[loop_index], *global_material_list, INDEX_CURRENT_COMP))
          add_element_to_int_list (&accepted_processes, loop_index);
      }
    }
  }

  this_material.number_of_processes = accepted_processes.num_elements; // Add number of processes
  this_material.is_vacuum = 0;                                         // This material is not vacuum

  if (this_material.number_of_processes == 0 && my_absorption == 0) {
    printf ("ERROR, the material named %s has no processes assigned and no absorption cross section, making it eqvialent to vacuum. Vacuums are assigned by "
            "setting material=\"Vacuum\" in a geometry component.\n",
            NAME_CURRENT_COMP);
    exit (1);
  }

  this_material.any_process_needs_cross_section_focus = -1; // Assume no process need focusing in cross section calculation
  // add process element to this_material, building an array of processes called p_scattering_array
  if (this_material.number_of_processes > 0)
    this_material.p_scattering_array = malloc (this_material.number_of_processes * sizeof (struct scattering_process_struct));
  for (loop_index = 0; loop_index < accepted_processes.num_elements; loop_index++) {
    this_material.p_scattering_array[loop_index] = *global_process_list->elements[accepted_processes.elements[loop_index]].p_scattering_process;

    // Check if each process needs focusing capability in the calculation of cross section / inverse penetration depth
    if (this_material.p_scattering_array[loop_index].needs_cross_section_focus == 1) {
      this_material.any_process_needs_cross_section_focus = 1;
    }
  }

  this_material.my_a = my_absorption; // add the absorption to this material
  sprintf (this_material.name, "%s", NAME_CURRENT_COMP);

  // Section on refracation information
  this_material.has_refraction_info = 0;
  if (refraction_density != 0 || refraction_weight != 0 || refraction_sigma_coh != 0) {

    double refraction_rho, refraction_bc;
    if (refraction_density == 0 || refraction_weight <= 0)
      exit (printf ("Union_make_material: %s: FATAL: invalid material density or molar weight: density=%g weight=%g\n", NAME_CURRENT_COMP, refraction_density,
                    refraction_weight));

    refraction_rho = fabs (refraction_density) * 6.02214179 * 1e23 * 1e-24 / refraction_weight; // per at/Angs^3

    if (refraction_sigma_coh == 0)
      exit (printf ("Refractor: %s: FATAL: invalid material coherent cross section: sigma_coh=%g\n", NAME_CURRENT_COMP, refraction_sigma_coh));

    refraction_bc = sqrt (fabs (refraction_sigma_coh) * 100 / 4 / PI) * 1e-5; // bound coherent scattering length
    if (refraction_sigma_coh < 0)
      refraction_bc *= -1.0;

    this_material.refraction_scattering_length_density = refraction_rho * refraction_bc;
    this_material.refraction_Qc = 4 * sqrt (PI * refraction_rho * fabs (refraction_bc));
    this_material.has_refraction_info = 1;
  }

  if (refraction_SLD > -1499.0) { // refraction_SLD can be negative, zero or positive, it can however not be this negative, so this is used as input check
    this_material.refraction_scattering_length_density = refraction_SLD;
    this_material.refraction_Qc = 4.0 * sqrt (PI * fabs (refraction_SLD));
    this_material.has_refraction_info = 1;
  }

  // packing the information into the global_material_element, which is then included in the global_material_list.
  sprintf (global_material_element.name, "%s", NAME_CURRENT_COMP);
  global_material_element.component_index = INDEX_CURRENT_COMP;
  global_material_element.physics = &this_material;
  add_element_to_material_list (global_material_list, global_material_element);
  #undef process_string
  #undef my_absorption
  #undef absorber
  #undef refraction_density
  #undef refraction_sigma_coh
  #undef refraction_weight
  #undef refraction_SLD
  #undef init
  #undef global_material_element
  #undef this_material
  #undef loop_index
  #undef found_process
  #undef specified_processes
  #undef local_string
  #undef accepted_processes
  return(_comp);
} /* class_Union_make_material_init */

_class_Powder_process *class_Powder_process_init(_class_Powder_process *_comp
) {
  #define reflections (_comp->_parameters.reflections)
  #define packing_factor (_comp->_parameters.packing_factor)
  #define Vc (_comp->_parameters.Vc)
  #define delta_d_d (_comp->_parameters.delta_d_d)
  #define DW (_comp->_parameters.DW)
  #define nb_atoms (_comp->_parameters.nb_atoms)
  #define d_phi (_comp->_parameters.d_phi)
  #define density (_comp->_parameters.density)
  #define weight (_comp->_parameters.weight)
  #define barns (_comp->_parameters.barns)
  #define Strain (_comp->_parameters.Strain)
  #define interact_fraction (_comp->_parameters.interact_fraction)
  #define format (_comp->_parameters.format)
  #define init (_comp->_parameters.init)
  #define global_process_element (_comp->_parameters.global_process_element)
  #define This_process (_comp->_parameters.This_process)
  #define Powder_storage (_comp->_parameters.Powder_storage)
  #define line_info (_comp->_parameters.line_info)
  #define effective_my_scattering (_comp->_parameters.effective_my_scattering)
  #define columns (_comp->_parameters.columns)
  SIG_MESSAGE("[_Al_Powder_init] component Al_Powder=Powder_process() INITIALISE [Powder_process:0]");


  // Initialize done in the component
  columns = format;

  // Copy from PowderN component
  int i = 0;
  struct line_data_union* L;
  line_info.Dd = delta_d_d;
  line_info.DWfactor = DW;
  line_info.V_0 = Vc;
  line_info.rho = density;
  line_info.at_weight = weight;
  line_info.at_nb = nb_atoms;
  line_info.sigma_a = 0; // This inputs are not needed, as absorption is handled elsewhere
  line_info.sigma_i = 0; // This input is not needed, as incoherent scattering is handled elsewhere
  line_info.flag_barns = barns;
  // line_info.shape    = 0;
  line_info.flag_warning = 0;
  line_info.Epsilon = Strain;
  line_info.radius_i = line_info.xwidth_i = line_info.yheight_i = line_info.zdepth_i = 0;
  line_info.v = 0;
  line_info.Nq = 0;
  // line_info.v_min = FLT_MAX; line_info.v_max = 0;
  line_info.v_min = 10000000000;
  line_info.v_max = 0;
  line_info.neutron_passed = 0;
  line_info.nb_reuses = line_info.nb_refl = line_info.nb_refl_count = 0;
  line_info.xs_compute = line_info.xs_reuse = line_info.xs_calls = 0;
  for (i = 0; i < 9; i++)
    line_info.column_order[i] = columns[i];
  strncpy (line_info.compname, NAME_CURRENT_COMP, 256);

  // p_interact handled elsewhere
  // if (p_interact) {
  //  if (p_interact < p_inc) { double tmp=p_interact; p_interact=p_inc; p_inc=tmp; }
  //  p_transmit = 1-p_interact-p_inc;
  //}

  if (reflections && strlen (reflections) && strcmp (reflections, "NULL") && strcmp (reflections, "0")) {
    i = read_line_data_union (reflections, &line_info);
    if (i == 0)
      exit (fprintf (stderr,
                     "PowderN: %s: reflection file %s is not valid.\n"
                     "ERROR    Please check file format (laz or lau).\n",
                     NAME_CURRENT_COMP, reflections));
  }

  /* compute the scattering unit density from material weight and density */
  /* the weight of the scattering element is the chemical formula molecular weight
   * times the nb of chemical formulae in the scattering element (nb_atoms) */
  if (!line_info.V_0 && line_info.at_nb > 0 && line_info.at_weight > 0 && line_info.rho > 0) {
    /* molar volume [cm^3/mol] = weight [g/mol] / density [g/cm^3] */
    /* atom density per Angs^3 = [mol/cm^3] * N_Avogadro *(1e-8)^3 */
    line_info.V_0 = line_info.at_nb / (line_info.rho / line_info.at_weight / 1e24 * 6.02214199e23);
  }

  /* the scattering unit cross sections are the chemical formula onces
   * times the nb of chemical formulae in the scattering element */
  if (line_info.at_nb > 0) {
    line_info.sigma_a *= line_info.at_nb;
    line_info.sigma_i *= line_info.at_nb;
  }

  if (line_info.V_0 <= 0)
    fprintf (stderr, "PowderN: %s: density/unit cell volume is NULL (Vc). Unactivating component.\n", NAME_CURRENT_COMP);

  if (line_info.flag_barns) { /* Factor 100 to convert from barns to fm^2 */
    line_info.XsectionFactor = 100;
  } else {
    line_info.XsectionFactor = 1;
  }

  if (line_info.V_0 && i) {
    L = line_info.list;

    line_info.q_v = malloc (line_info.count * sizeof (double));
    line_info.w_v = malloc (line_info.count * sizeof (double));
    line_info.my_s_v2 = malloc (line_info.count * sizeof (double));
    if (!line_info.q_v || !line_info.w_v || !line_info.my_s_v2)
      exit (fprintf (stderr, "PowderN: %s: ERROR allocating memory (init)\n", NAME_CURRENT_COMP));
    for (i = 0; i < line_info.count; i++) {
      line_info.my_s_v2[i] = 4 * PI * PI * PI * packing_factor * (L[i].DWfactor ? L[i].DWfactor : 1) / (line_info.V_0 * line_info.V_0 * V2K * V2K)
                             * (L[i].j * L[i].F2 / L[i].q) * line_info.XsectionFactor;
      /* Is not yet divided by v^2 */
      /* Squires [3.103] */
      line_info.q_v[i] = L[i].q * K2V;
      line_info.w_v[i] = L[i].w;
    }
  }
  if (line_info.V_0) {
    /* Is not yet divided by v */
    line_info.my_a_v = packing_factor * line_info.sigma_a / line_info.V_0 * 2200 * 100; // Factor 100 to convert from barns to fm^2
    line_info.my_inc = packing_factor * line_info.sigma_i / line_info.V_0 * 100;        // Factor 100 to convert from barns to fm^2

    MPI_MASTER (printf ("PowderN: %s: Vc=%g [Angs] sigma_abs=%g [barn] sigma_inc=%g [barn] reflections=%s\n", NAME_CURRENT_COMP, line_info.V_0, line_info.sigma_a,
                        line_info.sigma_i, reflections && strlen (reflections) ? reflections : "NULL"););
  }

  // printf("INTIALIZE line_info.v = %f, line_info.v_min = %f, line_info.v_max = %f, line_info.neutron_passed =
  // %f\n",line_info.v,line_info.v_min,line_info.v_max,line_info.neutron_passed);

  Powder_storage.line_info_storage = &line_info;
  Powder_storage.vertical_angular_limit = d_phi;

  // First initialise This_process with default values:
  scattering_process_struct_init (&This_process);

  // Need to specify if this process is isotropic
  This_process.non_isotropic_rot_index = -1; // Yes (powder)
  // This_process.non_isotropic_rot_index =  1;  // No (single crystal)

  // Need to specify if this process need to use focusing in calculation of inverse penetration depth (physics_my)
  // This_process.needs_cross_section_focus = 1; // Yes
  This_process.needs_cross_section_focus = -1; // No

  // The type of the process must be saved in the global enum process
  This_process.eProcess = Powder;

  // Packing the data into a structure that is transported to the main component
  This_process.data_transfer.pointer_to_a_Powder_physics_storage_struct = &Powder_storage;
  This_process.data_transfer.pointer_to_a_Powder_physics_storage_struct->my_scattering = effective_my_scattering;
  This_process.probability_for_scattering_function = &Powder_physics_my;
  This_process.scattering_function = &Powder_physics_scattering;

  // This will be the same for all process's, and can thus be moved to an include.
  This_process.process_p_interact = interact_fraction;
  sprintf (This_process.name, "%s", NAME_CURRENT_COMP);
  rot_copy (This_process.rotation_matrix, ROT_A_CURRENT_COMP);
  sprintf (global_process_element.name, "%s", NAME_CURRENT_COMP);
  global_process_element.component_index = INDEX_CURRENT_COMP;
  global_process_element.p_scattering_process = &This_process;

  if (_getcomp_index (init) < 0) {
    fprintf (stderr, "Powder_process:%s: Error identifying Union_init component, %s is not a known component name.\n", NAME_CURRENT_COMP, init);
    exit (-1);
  }

  struct pointer_to_global_process_list* global_process_list = COMP_GETPAR3 (Union_init, init, global_process_list);
  add_element_to_process_list (global_process_list, global_process_element);
  #undef reflections
  #undef packing_factor
  #undef Vc
  #undef delta_d_d
  #undef DW
  #undef nb_atoms
  #undef d_phi
  #undef density
  #undef weight
  #undef barns
  #undef Strain
  #undef interact_fraction
  #undef format
  #undef init
  #undef global_process_element
  #undef This_process
  #undef Powder_storage
  #undef line_info
  #undef effective_my_scattering
  #undef columns
  return(_comp);
} /* class_Powder_process_init */

_class_Progress_bar *class_Progress_bar_init(_class_Progress_bar *_comp
) {
  #define profile (_comp->_parameters.profile)
  #define percent (_comp->_parameters.percent)
  #define flag_save (_comp->_parameters.flag_save)
  #define minutes (_comp->_parameters.minutes)
  #define IntermediateCnts (_comp->_parameters.IntermediateCnts)
  #define StartTime (_comp->_parameters.StartTime)
  #define EndTime (_comp->_parameters.EndTime)
  #define CurrentTime (_comp->_parameters.CurrentTime)
  #define infostring (_comp->_parameters.infostring)
  SIG_MESSAGE("[_a1_init] component a1=Progress_bar() INITIALISE [Progress_bar:0]");

  IntermediateCnts = 0;
  StartTime = 0;
  EndTime = 0;
  CurrentTime = 0;

  fprintf (stdout, "[%s] Initialize\n", instrument_name);
  if (percent * mcget_ncount () / 100 < 1e5) {
    percent = 1e5 * 100.0 / mcget_ncount ();
  }
  #ifdef OPENACC
  time (&StartTime);
  #endif

  #ifdef USE_MPI
  sprintf (infostring, "(%i MPI processes) ", mpi_node_count);
  #else
  sprintf (infostring, "(single process) ");
  #endif
  #undef profile
  #undef percent
  #undef flag_save
  #undef minutes
  #undef IntermediateCnts
  #undef StartTime
  #undef EndTime
  #undef CurrentTime
  #undef infostring
  return(_comp);
} /* class_Progress_bar_init */

_class_Source_div *class_Source_div_init(_class_Source_div *_comp
) {
  #define xwidth (_comp->_parameters.xwidth)
  #define yheight (_comp->_parameters.yheight)
  #define focus_aw (_comp->_parameters.focus_aw)
  #define focus_ah (_comp->_parameters.focus_ah)
  #define E0 (_comp->_parameters.E0)
  #define dE (_comp->_parameters.dE)
  #define lambda0 (_comp->_parameters.lambda0)
  #define dlambda (_comp->_parameters.dlambda)
  #define gauss (_comp->_parameters.gauss)
  #define flux (_comp->_parameters.flux)
  #define sigmah (_comp->_parameters.sigmah)
  #define sigmav (_comp->_parameters.sigmav)
  #define p_init (_comp->_parameters.p_init)
  #define dist (_comp->_parameters.dist)
  #define focus_xw (_comp->_parameters.focus_xw)
  #define focus_yh (_comp->_parameters.focus_yh)
  SIG_MESSAGE("[_source_init] component source=Source_div() INITIALISE [Source_div:0]");

sigmah = DEG2RAD*focus_aw/(sqrt(8.0*log(2.0)));
  sigmav = DEG2RAD*focus_ah/(sqrt(8.0*log(2.0)));

  if (xwidth < 0 || yheight < 0 || focus_aw < 0 || focus_ah < 0) {
      printf("Source_div: %s: Error in input parameter values!\n"
             "ERROR       Exiting\n",
           NAME_CURRENT_COMP);
      exit(0);
  }
  if ((!lambda0 && !E0 && !dE && !dlambda)) {
    printf("Source_div: %s: You must specify either a wavelength or energy range!\n ERROR - Exiting\n",
           NAME_CURRENT_COMP);
    exit(0);
  }
  if ((!lambda0 && !dlambda && (E0 <= 0 || dE < 0 || E0-dE <= 0))
    || (!E0 && !dE && (lambda0 <= 0 || dlambda < 0 || lambda0-dlambda <= 0))) {
    printf("Source_div: %s: Unmeaningful definition of wavelength or energy range!\n ERROR - Exiting\n",
           NAME_CURRENT_COMP);
      exit(0);
  }
  /* compute distance to next component */
  Coords ToTarget;
  double tx,ty,tz;
  ToTarget = coords_sub(POS_A_COMP_INDEX(INDEX_CURRENT_COMP+1),POS_A_CURRENT_COMP);
  ToTarget = rot_apply(ROT_A_CURRENT_COMP, ToTarget);
  coords_get(ToTarget, &tx, &ty, &tz);
  dist=sqrt(tx*tx+ty*ty+tz*tz);
  /* compute target area */
  if (dist) {
    focus_xw=dist*tan(focus_aw*DEG2RAD);
    focus_yh=dist*tan(focus_ah*DEG2RAD);
  }

  p_init  = flux*1e4*xwidth*yheight/mcget_ncount();
  if (!focus_aw || !focus_ah)
    exit(printf("Source_div: %s: Zero divergence defined. \n"
                "ERROR       Use non zero values for focus_aw and focus_ah.\n",
           NAME_CURRENT_COMP));
  p_init *= 2*fabs(DEG2RAD*focus_aw*sin(DEG2RAD*focus_ah/2));  /* solid angle */
  if (dlambda)
    p_init *= 2*dlambda;
  else if (dE)
    p_init *= 2*dE;
  #undef xwidth
  #undef yheight
  #undef focus_aw
  #undef focus_ah
  #undef E0
  #undef dE
  #undef lambda0
  #undef dlambda
  #undef gauss
  #undef flux
  #undef sigmah
  #undef sigmav
  #undef p_init
  #undef dist
  #undef focus_xw
  #undef focus_yh
  return(_comp);
} /* class_Source_div_init */

_class_Union_cylinder *class_Union_cylinder_init(_class_Union_cylinder *_comp
) {
  #define material_string (_comp->_parameters.material_string)
  #define priority (_comp->_parameters.priority)
  #define radius (_comp->_parameters.radius)
  #define yheight (_comp->_parameters.yheight)
  #define visualize (_comp->_parameters.visualize)
  #define target_index (_comp->_parameters.target_index)
  #define target_x (_comp->_parameters.target_x)
  #define target_y (_comp->_parameters.target_y)
  #define target_z (_comp->_parameters.target_z)
  #define focus_aw (_comp->_parameters.focus_aw)
  #define focus_ah (_comp->_parameters.focus_ah)
  #define focus_xw (_comp->_parameters.focus_xw)
  #define focus_xh (_comp->_parameters.focus_xh)
  #define focus_r (_comp->_parameters.focus_r)
  #define p_interact (_comp->_parameters.p_interact)
  #define mask_string (_comp->_parameters.mask_string)
  #define mask_setting (_comp->_parameters.mask_setting)
  #define number_of_activations (_comp->_parameters.number_of_activations)
  #define curved_surface (_comp->_parameters.curved_surface)
  #define top_surface (_comp->_parameters.top_surface)
  #define bottom_surface (_comp->_parameters.bottom_surface)
  #define all_face_surface (_comp->_parameters.all_face_surface)
  #define cut_surface (_comp->_parameters.cut_surface)
  #define init (_comp->_parameters.init)
  #define global_geometry_element (_comp->_parameters.global_geometry_element)
  #define loop_index (_comp->_parameters.loop_index)
  #define loop_2_index (_comp->_parameters.loop_2_index)
  #define material_index (_comp->_parameters.material_index)
  #define this_cylinder_volume (_comp->_parameters.this_cylinder_volume)
  #define this_cylinder_storage (_comp->_parameters.this_cylinder_storage)
  #define curved_surface_stack (_comp->_parameters.curved_surface_stack)
  #define top_surface_stack (_comp->_parameters.top_surface_stack)
  #define bottom_surface_stack (_comp->_parameters.bottom_surface_stack)
  #define cut_surface_stack (_comp->_parameters.cut_surface_stack)
  SIG_MESSAGE("[_cryostat_wall_init] component cryostat_wall=Union_cylinder() INITIALISE [Union_cylinder:0]");

  geometry_struct_init (&(this_cylinder_volume.geometry));
  // Initializes the focusing system for this volume including input sanitation.
  focus_initialize (&this_cylinder_volume.geometry, POS_A_COMP_INDEX (INDEX_CURRENT_COMP + target_index), POS_A_CURRENT_COMP, ROT_A_CURRENT_COMP, target_index,
                    target_x, target_y, target_z, focus_aw, focus_ah, focus_xw, focus_xh, focus_r, NAME_CURRENT_COMP);

  // Input sanitation for this geometry
  if (radius <= 0) {
    printf ("\nERROR in Union_cylinder named %s, the radius is <= 0. \n", NAME_CURRENT_COMP);
    exit (1);
  }

  if (yheight <= 0) {
    printf ("\nERROR in Union_cylinder named %s, yheight is <= 0. \n", NAME_CURRENT_COMP);
    exit (1);
  }

  if (_getcomp_index (init) < 0) {
    fprintf (stderr, "Union_cylinder:%s: Error identifying Union_init component, %s is not a known component name.\n", NAME_CURRENT_COMP, init);
    exit (-1);
  }

  struct pointer_to_global_material_list* global_material_list = COMP_GETPAR3 (Union_init, init, global_material_list);
  // Use sanitation
  #ifdef MATERIAL_DETECTOR
  if (global_material_list->num_elements == 0) {
    // Here if the user have defined a material, but only after this material
    printf ("\nERROR: Need to define a material using Union_make_material before using a Union geometry component. \n");
    printf ("       %s was defined before first use of Union_make_material.\n", NAME_CURRENT_COMP);
    exit (1);
  }
  #endif
  #ifndef MATERIAL_DETECTOR
  printf ("\nERROR: Need to define a material using Union_make_material before using a Union geometry component. \n");
  exit (1);
  #endif

  this_cylinder_volume.geometry.is_masked_volume = 0;
  this_cylinder_volume.geometry.is_exit_volume = 0;
  this_cylinder_volume.geometry.is_mask_volume = 0;
  struct pointer_to_global_geometry_list* global_geometry_list = COMP_GETPAR3 (Union_init, init, global_geometry_list);

  // Read the material input, or if it lacks, use automatic linking.
  if (mask_string && strlen (mask_string) && strcmp (mask_string, "NULL") && strcmp (mask_string, "0")) {
    // A mask volume is used to limit the extend of other volumes, called the masked volumes. These are specified in the mask_string.
    // In order for a ray to enter a masked volume, it needs to be both in the region covered by that volume AND the mask volume.
    // When more than
    this_cylinder_volume.geometry.mask_mode = 1; // Default is mask mode is ALL
    if (mask_setting && strlen (mask_setting) && strcmp (mask_setting, "NULL") && strcmp (mask_setting, "0")) {
      if (strcmp (mask_setting, "ALL") == 0 || strcmp (mask_setting, "All") == 0)
        this_cylinder_volume.geometry.mask_mode = 1;
      else if (strcmp (mask_setting, "ANY") == 0 || strcmp (mask_setting, "Any") == 0)
        this_cylinder_volume.geometry.mask_mode = 2;
      else {
        printf ("The mask_mode of component %s is set to %s, but must be either ALL or ANY.\n", NAME_CURRENT_COMP, mask_setting);
        exit (1);
      }
    }

    int found_geometries = 0;
    for (loop_index = 0; loop_index < global_geometry_list->num_elements; loop_index++) {
      // Add mask list
      if (1 == manual_linking_function (global_geometry_list->elements[loop_index].name, mask_string)) {
        add_element_to_int_list (&this_cylinder_volume.geometry.mask_list, global_geometry_list->elements[loop_index].component_index);
        add_element_to_int_list (&global_geometry_list->elements[loop_index].Volume->geometry.masked_by_list, INDEX_CURRENT_COMP);
        global_geometry_list->elements[loop_index].Volume->geometry.is_masked_volume = 1;
        if (this_cylinder_volume.geometry.mask_mode == 2)
          global_geometry_list->elements[loop_index].Volume->geometry.mask_mode = 2;
        if (this_cylinder_volume.geometry.mask_mode == 1) {
          if (global_geometry_list->elements[loop_index].Volume->geometry.is_masked_volume == 1
              && global_geometry_list->elements[loop_index].Volume->geometry.mask_mode != 2)
            // If more than one mask is added to one volume, the ANY mode overwrites the (default) ALL mode.
            global_geometry_list->elements[loop_index].Volume->geometry.mask_mode = 1;
        }

        found_geometries = 1;
      }
    }
    if (found_geometries == 0) {
      printf ("The mask_string in geometry: %s did not find any of the specified volumes in the mask_string %s \n", NAME_CURRENT_COMP, mask_string);
      exit (1);
    }
    this_cylinder_volume.p_physics = malloc (sizeof (struct physics_struct));
    this_cylinder_volume.p_physics->is_vacuum = 0;                // Makes this volume a vacuum
    this_cylinder_volume.p_physics->number_of_processes = (int)0; // Should not be used.
    this_cylinder_volume.p_physics->my_a = 0;                     // Should not be used.
    sprintf (this_cylinder_volume.p_physics->name, "Mask");
    this_cylinder_volume.geometry.is_mask_volume = 1;

    // Read the material input, or if it lacks, use automatic linking.
  } else if (material_string && strlen (material_string) && strcmp (material_string, "NULL") && strcmp (material_string, "0")) {
    // A geometry string was given, use it to determine which material
    if (0 == strcmp (material_string, "vacuum") || 0 == strcmp (material_string, "Vacuum")) {
      // One could have a global physics struct for vacuum instead of creating one for each
      this_cylinder_volume.p_physics = malloc (sizeof (struct physics_struct));
      this_cylinder_volume.p_physics->is_vacuum = 1; // Makes this volume a vacuum
      this_cylinder_volume.p_physics->number_of_processes = (int)0;
      this_cylinder_volume.p_physics->my_a = 0; // Should not be used.
      sprintf (this_cylinder_volume.p_physics->name, "Vacuum");
    } else if (0 == strcmp (material_string, "exit") || 0 == strcmp (material_string, "Exit")) {
      // One could have a global physics struct for exit instead of creating one for each
      this_cylinder_volume.p_physics = malloc (sizeof (struct physics_struct));
      this_cylinder_volume.p_physics->is_vacuum = 1; // Makes this volume a vacuum
      this_cylinder_volume.p_physics->number_of_processes = (int)0;
      this_cylinder_volume.p_physics->my_a = 0; // Should not be used.
      this_cylinder_volume.geometry.is_exit_volume = 1;
      sprintf (this_cylinder_volume.p_physics->name, "Exit");
    } else {
      for (loop_index = 0; loop_index < global_material_list->num_elements; loop_index++) {
        if (0 == strcmp (material_string, global_material_list->elements[loop_index].name)) {
          this_cylinder_volume.p_physics = global_material_list->elements[loop_index].physics;
          break;
        }
        if (loop_index == global_material_list->num_elements - 1) {
          printf ("\n");
          printf ("ERROR: The material string \"%s\" in Union geometry \"%s\" did not match a specified material. \n", material_string, NAME_CURRENT_COMP);
          printf ("       The materials available at this point (need to be defined before the geometry): \n");
          for (loop_index = 0; loop_index < global_material_list->num_elements; loop_index++)
            printf ("         %s\n", global_material_list->elements[loop_index].name);
          printf ("\n");
          printf ("       It is also possible to use one of the defualt materials avaiable: \n");
          printf ("           Vacuum (for a Volume without scattering or absorption)\n");
          printf ("           Exit (for a Volume where the ray exits the component if it enters)\n");
          printf ("           Mask (for a Volume that masks existing volumes specified in the mask_string\n");
          exit (1);
        }
      }
    }
  } else {
    // Automatic linking, simply using the last defined material.
    #ifndef MATERIAL_DETECTOR
    printf ("Need to define a material before the geometry to use automatic linking %s.\n", NAME_CURRENT_COMP);
    exit (1);
    #endif
    this_cylinder_volume.p_physics = global_material_list->elements[global_material_list->num_elements - 1].physics;
  }

  // Handle surface input
  if (all_face_surface && strlen (all_face_surface) && strcmp (all_face_surface, "NULL") && strcmp (all_face_surface, "0")) {
    // Overwrite all other surfaces that have no input (if one wants a surface left without anything, None can be used)
    overwrite_if_empty (curved_surface, all_face_surface);
    overwrite_if_empty (top_surface, all_face_surface);
    overwrite_if_empty (bottom_surface, all_face_surface);
  }

  this_cylinder_volume.geometry.number_of_faces = 3;
  this_cylinder_volume.geometry.surface_stack_for_each_face = malloc (this_cylinder_volume.geometry.number_of_faces * sizeof (struct surface_stack_struct*));

  // This could be inserted into the fill_surface_stack function
  this_cylinder_volume.geometry.surface_stack_for_each_face[0] = &curved_surface_stack;
  this_cylinder_volume.geometry.surface_stack_for_each_face[1] = &top_surface_stack;
  this_cylinder_volume.geometry.surface_stack_for_each_face[2] = &bottom_surface_stack;
  this_cylinder_volume.geometry.internal_cut_surface_stack
      = &cut_surface_stack; // This surface is used if the volume is cut by overlapping of higher priority volume

  struct pointer_to_global_surface_list* global_surface_list = COMP_GETPAR3 (Union_init, init, global_surface_list);
  fill_surface_stack (curved_surface, global_surface_list, NAME_CURRENT_COMP, &curved_surface_stack);
  fill_surface_stack (top_surface, global_surface_list, NAME_CURRENT_COMP, &top_surface_stack);
  fill_surface_stack (bottom_surface, global_surface_list, NAME_CURRENT_COMP, &bottom_surface_stack);
  fill_surface_stack (cut_surface, global_surface_list, NAME_CURRENT_COMP, &cut_surface_stack);

  sprintf (this_cylinder_volume.name, "%s", NAME_CURRENT_COMP);
  sprintf (this_cylinder_volume.geometry.shape, "cylinder");
  this_cylinder_volume.geometry.eShape = cylinder;
  this_cylinder_volume.geometry.priority_value = priority;
  // Currently the coordinates will be in absolute space.
  this_cylinder_volume.geometry.center = POS_A_CURRENT_COMP;

  this_cylinder_volume.geometry.geometry_p_interact = p_interact;
  this_cylinder_storage.cyl_radius = radius;
  this_cylinder_storage.height = yheight;
  this_cylinder_volume.geometry.visualization_on = visualize;
  this_cylinder_volume.geometry.geometry_parameters.p_cylinder_storage = &this_cylinder_storage;
  this_cylinder_volume.geometry.within_function = &r_within_cylinder;
  this_cylinder_volume.geometry.intersect_function = &sample_cylinder_intersect;
  this_cylinder_volume.geometry.mcdisplay_function = &mcdisplay_cylinder_function;
  this_cylinder_volume.geometry.shell_points = &cylinder_shell_points;
  this_cylinder_volume.geometry.initialize_from_main_function = &initialize_cylinder_geometry_from_main_component;
  this_cylinder_volume.geometry.process_rot_allocated = 0;
  this_cylinder_volume.geometry.copy_geometry_parameters = &allocate_cylinder_storage_copy;

  rot_copy (this_cylinder_volume.geometry.rotation_matrix, ROT_A_CURRENT_COMP);
  rot_transpose (ROT_A_CURRENT_COMP, this_cylinder_volume.geometry.transpose_rotation_matrix);

  // Initialize loggers
  this_cylinder_volume.loggers.num_elements = 0;
  this_cylinder_volume.abs_loggers.num_elements = 0;

  // packing the information into the global_geometry_element, which is then included in the global_geometry_list.
  sprintf (global_geometry_element.name, "%s", NAME_CURRENT_COMP);
  global_geometry_element.activation_counter = number_of_activations;
  global_geometry_element.component_index = INDEX_CURRENT_COMP;
  global_geometry_element.Volume = &this_cylinder_volume; // Would be nicer if this m was a pointer, now we have the (small) data two places
  add_element_to_geometry_list (global_geometry_list, global_geometry_element);
  #undef material_string
  #undef priority
  #undef radius
  #undef yheight
  #undef visualize
  #undef target_index
  #undef target_x
  #undef target_y
  #undef target_z
  #undef focus_aw
  #undef focus_ah
  #undef focus_xw
  #undef focus_xh
  #undef focus_r
  #undef p_interact
  #undef mask_string
  #undef mask_setting
  #undef number_of_activations
  #undef curved_surface
  #undef top_surface
  #undef bottom_surface
  #undef all_face_surface
  #undef cut_surface
  #undef init
  #undef global_geometry_element
  #undef loop_index
  #undef loop_2_index
  #undef material_index
  #undef this_cylinder_volume
  #undef this_cylinder_storage
  #undef curved_surface_stack
  #undef top_surface_stack
  #undef bottom_surface_stack
  #undef cut_surface_stack
  return(_comp);
} /* class_Union_cylinder_init */

_class_Union_sphere *class_Union_sphere_init(_class_Union_sphere *_comp
) {
  #define material_string (_comp->_parameters.material_string)
  #define priority (_comp->_parameters.priority)
  #define radius (_comp->_parameters.radius)
  #define visualize (_comp->_parameters.visualize)
  #define target_index (_comp->_parameters.target_index)
  #define target_x (_comp->_parameters.target_x)
  #define target_y (_comp->_parameters.target_y)
  #define target_z (_comp->_parameters.target_z)
  #define focus_aw (_comp->_parameters.focus_aw)
  #define focus_ah (_comp->_parameters.focus_ah)
  #define focus_xw (_comp->_parameters.focus_xw)
  #define focus_xh (_comp->_parameters.focus_xh)
  #define focus_r (_comp->_parameters.focus_r)
  #define p_interact (_comp->_parameters.p_interact)
  #define mask_string (_comp->_parameters.mask_string)
  #define mask_setting (_comp->_parameters.mask_setting)
  #define number_of_activations (_comp->_parameters.number_of_activations)
  #define surface (_comp->_parameters.surface)
  #define cut_surface (_comp->_parameters.cut_surface)
  #define init (_comp->_parameters.init)
  #define global_geometry_element (_comp->_parameters.global_geometry_element)
  #define loop_index (_comp->_parameters.loop_index)
  #define loop_2_index (_comp->_parameters.loop_2_index)
  #define material_index (_comp->_parameters.material_index)
  #define surface_stack (_comp->_parameters.surface_stack)
  #define cut_surface_stack (_comp->_parameters.cut_surface_stack)
  #define this_sphere_volume (_comp->_parameters.this_sphere_volume)
  #define this_sphere_storage (_comp->_parameters.this_sphere_storage)
  SIG_MESSAGE("[_sample_sphere_init] component sample_sphere=Union_sphere() INITIALISE [Union_sphere:0]");


  geometry_struct_init (&(this_sphere_volume.geometry));
  // Initializes the focusing system for this volume including input sanitation.
  focus_initialize (&this_sphere_volume.geometry, POS_A_COMP_INDEX (INDEX_CURRENT_COMP + target_index), POS_A_CURRENT_COMP, ROT_A_CURRENT_COMP, target_index,
                    target_x, target_y, target_z, focus_aw, focus_ah, focus_xw, focus_xh, focus_r, NAME_CURRENT_COMP);

  // Input sanitation for this geometry
  if (radius <= 0) {
    printf ("\nERROR in Union_sphere named %s, the radius is <= 0. \n", NAME_CURRENT_COMP);
    exit (1);
  }

  if (_getcomp_index (init) < 0) {
    fprintf (stderr, "Union_sphere:%s: Error identifying Union_init component, %s is not a known component name.\n", NAME_CURRENT_COMP, init);
    exit (-1);
  }
  // Get global variable from init component
  struct pointer_to_global_material_list* global_material_list = COMP_GETPAR3 (Union_init, init, global_material_list);
  // Use sanitation
  #ifdef MATERIAL_DETECTOR
  if (global_material_list->num_elements == 0) {
    // Here if the user have defined a material, but only after this material
    printf ("\nERROR: Need to define a material using Union_make_material before using a Union geometry component. \n");
    printf ("       %s was defined before first use of Union_make_material.\n", NAME_CURRENT_COMP);
    exit (1);
  }
  #endif
  #ifndef MATERIAL_DETECTOR
  printf ("\nERROR: Need to define a material using Union_make_material before using a Union geometry component. \n");
  exit (1);
  #endif

  this_sphere_volume.geometry.is_masked_volume = 0;
  this_sphere_volume.geometry.is_exit_volume = 0;
  this_sphere_volume.geometry.is_mask_volume = 0;

  struct pointer_to_global_geometry_list* global_geometry_list = COMP_GETPAR3 (Union_init, init, global_geometry_list);
  // Read the material input, or if it lacks, use automatic linking.
  if (mask_string && strlen (mask_string) && strcmp (mask_string, "NULL") && strcmp (mask_string, "0")) {
    // A mask volume is used to limit the extend of other volumes, called the masked volumes. These are specified in the mask_string.
    // In order for a ray to enter a masked volume, it needs to be both in the region covered by that volume AND the mask volume.
    // When more than
    this_sphere_volume.geometry.mask_mode = 1; // Default is mask mode is ALL
    if (mask_setting && strlen (mask_setting) && strcmp (mask_setting, "NULL") && strcmp (mask_setting, "0")) {
      if (strcmp (mask_setting, "ALL") == 0 || strcmp (mask_setting, "All") == 0)
        this_sphere_volume.geometry.mask_mode = 1;
      else if (strcmp (mask_setting, "ANY") == 0 || strcmp (mask_setting, "Any") == 0)
        this_sphere_volume.geometry.mask_mode = 2;
      else {
        printf ("The mask_mode of component %s is set to %s, but must be either ALL or ANY.\n", NAME_CURRENT_COMP, mask_setting);
        exit (1);
      }
    }

    int found_geometries = 0;
    for (loop_index = 0; loop_index < global_geometry_list->num_elements; loop_index++) {
      // Add mask list
      if (1 == manual_linking_function (global_geometry_list->elements[loop_index].name, mask_string)) {
        add_element_to_int_list (&this_sphere_volume.geometry.mask_list, global_geometry_list->elements[loop_index].component_index);
        add_element_to_int_list (&global_geometry_list->elements[loop_index].Volume->geometry.masked_by_list, INDEX_CURRENT_COMP);
        global_geometry_list->elements[loop_index].Volume->geometry.is_masked_volume = 1;
        if (this_sphere_volume.geometry.mask_mode == 2)
          global_geometry_list->elements[loop_index].Volume->geometry.mask_mode = 2;
        if (this_sphere_volume.geometry.mask_mode == 1) {
          if (global_geometry_list->elements[loop_index].Volume->geometry.is_masked_volume == 1
              && global_geometry_list->elements[loop_index].Volume->geometry.mask_mode != 2)
            // If more than one mask is added to one volume, the ANY mode overwrites the (default) ALL mode.
            global_geometry_list->elements[loop_index].Volume->geometry.mask_mode = 1;
        }

        found_geometries = 1;
      }
    }
    if (found_geometries == 0) {
      printf ("The mask_string in geometry: %s did not find any of the specified volumes in the mask_string %s \n", NAME_CURRENT_COMP, mask_string);
      exit (1);
    }
    this_sphere_volume.p_physics = malloc (sizeof (struct physics_struct));
    this_sphere_volume.p_physics->is_vacuum = 0;                // Makes this volume a vacuum
    this_sphere_volume.p_physics->number_of_processes = (int)0; // Should not be used.
    this_sphere_volume.p_physics->my_a = 0;                     // Should not be used.
    sprintf (this_sphere_volume.p_physics->name, "Mask");
    this_sphere_volume.geometry.is_mask_volume = 1;

    // Read the material input, or if it lacks, use automatic linking.
  } else if (material_string && strlen (material_string) && strcmp (material_string, "NULL") && strcmp (material_string, "0")) {
    // A geometry string was given, use it to determine which material
    if (0 == strcmp (material_string, "vacuum") || 0 == strcmp (material_string, "Vacuum")) {
      // One could have a global physics struct for vacuum instead of creating one for each
      this_sphere_volume.p_physics = malloc (sizeof (struct physics_struct));
      this_sphere_volume.p_physics->is_vacuum = 1; // Makes this volume a vacuum
      this_sphere_volume.p_physics->number_of_processes = (int)0;
      this_sphere_volume.p_physics->my_a = 0; // Should not be used.
      sprintf (this_sphere_volume.p_physics->name, "Vacuum");
    } else if (0 == strcmp (material_string, "exit") || 0 == strcmp (material_string, "Exit")) {
      // One could have a global physics struct for exit instead of creating one for each
      this_sphere_volume.p_physics = malloc (sizeof (struct physics_struct));
      this_sphere_volume.p_physics->is_vacuum = 1; // Makes this volume a vacuum
      this_sphere_volume.p_physics->number_of_processes = (int)0;
      this_sphere_volume.p_physics->my_a = 0; // Should not be used.
      this_sphere_volume.geometry.is_exit_volume = 1;
      sprintf (this_sphere_volume.p_physics->name, "Exit");
    } else {
      for (loop_index = 0; loop_index < global_material_list->num_elements; loop_index++) {
        if (0 == strcmp (material_string, global_material_list->elements[loop_index].name)) {
          this_sphere_volume.p_physics = global_material_list->elements[loop_index].physics;
          break;
        }
        if (loop_index == global_material_list->num_elements - 1) {
          printf ("\n");
          printf ("ERROR: The material string \"%s\" in Union geometry \"%s\" did not match a specified material. \n", material_string, NAME_CURRENT_COMP);
          printf ("       The materials available at this point (need to be defined before the geometry): \n");
          for (loop_index = 0; loop_index < global_material_list->num_elements; loop_index++)
            printf ("         %s\n", global_material_list->elements[loop_index].name);
          printf ("\n");
          printf ("       It is also possible to use one of the defualt materials avaiable: \n");
          printf ("           Vacuum (for a Volume without scattering or absorption)\n");
          printf ("           Exit (for a Volume where the ray exits the component if it enters)\n");
          printf ("           Mask (for a Volume that masks existing volumes specified in the mask_string\n");
          exit (1);
        }
      }
    }
  } else {
    // Automatic linking, simply using the last defined material.
    #ifndef MATERIAL_DETECTOR
    printf ("Need to define a material before the geometry to use automatic linking %s.\n", NAME_CURRENT_COMP);
    exit (1);
    #endif
    this_sphere_volume.p_physics = global_material_list->elements[global_material_list->num_elements - 1].physics;
  }

  this_sphere_volume.geometry.number_of_faces = 1;
  this_sphere_volume.geometry.surface_stack_for_each_face = malloc (this_sphere_volume.geometry.number_of_faces * sizeof (struct surface_stack_struct*));

  // This could be inserted into the fill_surface_stack function
  this_sphere_volume.geometry.surface_stack_for_each_face[0] = &surface_stack;
  this_sphere_volume.geometry.internal_cut_surface_stack
      = &cut_surface_stack; // This surface is used if the volume is cut by overlapping of higher priority volume

  struct pointer_to_global_surface_list* global_surface_list = COMP_GETPAR3 (Union_init, init, global_surface_list);
  fill_surface_stack (surface, global_surface_list, NAME_CURRENT_COMP, &surface_stack);
  fill_surface_stack (cut_surface, global_surface_list, NAME_CURRENT_COMP, &cut_surface_stack);

  sprintf (this_sphere_volume.name, "%s", NAME_CURRENT_COMP);
  sprintf (this_sphere_volume.geometry.shape, "sphere");
  this_sphere_volume.geometry.eShape = sphere;
  this_sphere_volume.geometry.priority_value = priority;
  // Currently the coordinates will be in absolute space.
  this_sphere_volume.geometry.center = POS_A_CURRENT_COMP;

  this_sphere_volume.geometry.geometry_p_interact = p_interact;
  this_sphere_storage.sph_radius = radius;
  this_sphere_volume.geometry.visualization_on = visualize;
  this_sphere_volume.geometry.geometry_parameters.p_sphere_storage = &this_sphere_storage;
  this_sphere_volume.geometry.within_function = &r_within_sphere;
  this_sphere_volume.geometry.intersect_function = &sample_sphere_intersect;
  this_sphere_volume.geometry.mcdisplay_function = &mcdisplay_sphere_function;
  this_sphere_volume.geometry.initialize_from_main_function = &initialize_sphere_geometry_from_main_component;
  this_sphere_volume.geometry.shell_points = &sphere_shell_points;
  this_sphere_volume.geometry.process_rot_allocated = 0;
  this_sphere_volume.geometry.copy_geometry_parameters = &allocate_sphere_storage_copy;

  rot_copy (this_sphere_volume.geometry.rotation_matrix, ROT_A_CURRENT_COMP);
  rot_transpose (ROT_A_CURRENT_COMP, this_sphere_volume.geometry.transpose_rotation_matrix);

  // Initialize loggers
  this_sphere_volume.loggers.num_elements = 0;
  this_sphere_volume.abs_loggers.num_elements = 0;

  // packing the information into the global_geometry_element, which is then included in the global_geometry_list.
  sprintf (global_geometry_element.name, "%s", NAME_CURRENT_COMP);
  global_geometry_element.activation_counter = number_of_activations;
  global_geometry_element.component_index = INDEX_CURRENT_COMP;
  global_geometry_element.Volume = &this_sphere_volume;
  add_element_to_geometry_list (global_geometry_list, global_geometry_element);

  // TEST Union sphere has been initialized, test shell point function by writing to file.
  /*
  struct pointer_to_1d_coords_list shell_points;
  shell_points = this_sphere_volume.geometry.shell_points(&this_sphere_volume.geometry,400); // using 500 rings with 500 points


  FILE *fp;

  char filename[124] = "";

  strcat(filename,NAME_CURRENT_COMP);
  strcat(filename,"_debug.dat");

  fp = fopen(filename,"w");

  for (loop_index=0;loop_index<shell_points.num_elements;loop_index++)
     fprintf(fp,"%lf %lf %lf\n",shell_points.elements[loop_index].x,shell_points.elements[loop_index].y,shell_points.elements[loop_index].z);

  free(shell_points.elements);

  fclose(fp);
  */
  #undef material_string
  #undef priority
  #undef radius
  #undef visualize
  #undef target_index
  #undef target_x
  #undef target_y
  #undef target_z
  #undef focus_aw
  #undef focus_ah
  #undef focus_xw
  #undef focus_xh
  #undef focus_r
  #undef p_interact
  #undef mask_string
  #undef mask_setting
  #undef number_of_activations
  #undef surface
  #undef cut_surface
  #undef init
  #undef global_geometry_element
  #undef loop_index
  #undef loop_2_index
  #undef material_index
  #undef surface_stack
  #undef cut_surface_stack
  #undef this_sphere_volume
  #undef this_sphere_storage
  return(_comp);
} /* class_Union_sphere_init */

_class_Union_box *class_Union_box_init(_class_Union_box *_comp
) {
  #define material_string (_comp->_parameters.material_string)
  #define priority (_comp->_parameters.priority)
  #define xwidth (_comp->_parameters.xwidth)
  #define yheight (_comp->_parameters.yheight)
  #define zdepth (_comp->_parameters.zdepth)
  #define xwidth2 (_comp->_parameters.xwidth2)
  #define yheight2 (_comp->_parameters.yheight2)
  #define visualize (_comp->_parameters.visualize)
  #define target_index (_comp->_parameters.target_index)
  #define target_x (_comp->_parameters.target_x)
  #define target_y (_comp->_parameters.target_y)
  #define target_z (_comp->_parameters.target_z)
  #define focus_aw (_comp->_parameters.focus_aw)
  #define focus_ah (_comp->_parameters.focus_ah)
  #define focus_xw (_comp->_parameters.focus_xw)
  #define focus_xh (_comp->_parameters.focus_xh)
  #define focus_r (_comp->_parameters.focus_r)
  #define plus_z_surface (_comp->_parameters.plus_z_surface)
  #define minus_z_surface (_comp->_parameters.minus_z_surface)
  #define plus_x_surface (_comp->_parameters.plus_x_surface)
  #define minus_x_surface (_comp->_parameters.minus_x_surface)
  #define plus_y_surface (_comp->_parameters.plus_y_surface)
  #define minus_y_surface (_comp->_parameters.minus_y_surface)
  #define all_face_surface (_comp->_parameters.all_face_surface)
  #define cut_surface (_comp->_parameters.cut_surface)
  #define p_interact (_comp->_parameters.p_interact)
  #define mask_string (_comp->_parameters.mask_string)
  #define mask_setting (_comp->_parameters.mask_setting)
  #define number_of_activations (_comp->_parameters.number_of_activations)
  #define init (_comp->_parameters.init)
  #define global_geometry_element (_comp->_parameters.global_geometry_element)
  #define loop_index (_comp->_parameters.loop_index)
  #define x_component (_comp->_parameters.x_component)
  #define y_component (_comp->_parameters.y_component)
  #define z_component (_comp->_parameters.z_component)
  #define this_box_volume (_comp->_parameters.this_box_volume)
  #define this_box_storage (_comp->_parameters.this_box_storage)
  #define plus_x_surface_stack (_comp->_parameters.plus_x_surface_stack)
  #define minus_x_surface_stack (_comp->_parameters.minus_x_surface_stack)
  #define plus_y_surface_stack (_comp->_parameters.plus_y_surface_stack)
  #define minus_y_surface_stack (_comp->_parameters.minus_y_surface_stack)
  #define plus_z_surface_stack (_comp->_parameters.plus_z_surface_stack)
  #define minus_z_surface_stack (_comp->_parameters.minus_z_surface_stack)
  #define cut_surface_stack (_comp->_parameters.cut_surface_stack)
  SIG_MESSAGE("[_sample_box_init] component sample_box=Union_box() INITIALISE [Union_box:0]");

  // Initialize this Volume
  geometry_struct_init (&(this_box_volume.geometry));
  // Initializes the focusing system for this volume including input sanitation.
  focus_initialize (&this_box_volume.geometry, POS_A_COMP_INDEX (INDEX_CURRENT_COMP + target_index), POS_A_CURRENT_COMP, ROT_A_CURRENT_COMP, target_index,
                    target_x, target_y, target_z, focus_aw, focus_ah, focus_xw, focus_xh, focus_r, NAME_CURRENT_COMP);

  // Input sanitation for this geometry
  if (xwidth <= 0) {
    printf ("\nERROR in Union_box named %s, the xwidth is <= 0. \n", NAME_CURRENT_COMP);
    exit (1);
  }
  if (yheight <= 0) {
    printf ("\nERROR in Union_box named %s, yheight is <= 0. \n", NAME_CURRENT_COMP);
    exit (1);
  }
  if (zdepth <= 0) {
    printf ("\nERROR in Union_box named %s, zdepth is <= 0. \n", NAME_CURRENT_COMP);
    exit (1);
  }
  if (xwidth2 <= 0 && xwidth2 != -1) {
    printf ("\nERROR in Union_box named %s, the xwidth2 is <= 0. \n", NAME_CURRENT_COMP);
    exit (1);
  }
  if (yheight2 <= 0 && yheight2 != -1) {
    printf ("\nERROR in Union_box named %s, yheight2 is <= 0. \n", NAME_CURRENT_COMP);
    exit (1);
  }

  if (_getcomp_index (init) < 0) {
    fprintf (stderr, "Union_box:%s: Error identifying Union_init component, %s is not a known component name.\n", NAME_CURRENT_COMP, init);
    exit (-1);
  }

  struct pointer_to_global_material_list* global_material_list = COMP_GETPAR3 (Union_init, init, global_material_list);
  // Use sanitation
  if (global_material_list->num_elements == 0) {
    printf ("\nERROR: Need to define a material using Union_make_material before using a Union geometry component. \n");
    printf ("       %s was defined before first use of Union_make_material.\n", NAME_CURRENT_COMP);
    exit (1);
  }

  this_box_volume.geometry.is_masked_volume = 0;
  this_box_volume.geometry.is_exit_volume = 0;
  this_box_volume.geometry.is_mask_volume = 0;
  struct pointer_to_global_geometry_list* global_geometry_list = COMP_GETPAR3 (Union_init, init, global_geometry_list);
  // check if the volume is a mask, if it is the material string is irelevant.
  if (mask_string && strlen (mask_string) && strcmp (mask_string, "NULL") && strcmp (mask_string, "0")) {
    // A mask volume is used to limit the extend of other volumes, called the masked volumes. These are specified in the mask_string.
    // In order for a ray to enter a masked volume, it needs to be both in the region covered by that volume AND the mask volume.
    // When more than
    this_box_volume.geometry.mask_mode = 1; // Default is mask mode is ALL
    if (mask_setting && strlen (mask_setting) && strcmp (mask_setting, "NULL") && strcmp (mask_setting, "0")) {
      if (strcmp (mask_setting, "ALL") == 0 || strcmp (mask_setting, "All") == 0)
        this_box_volume.geometry.mask_mode = 1;
      else if (strcmp (mask_setting, "ANY") == 0 || strcmp (mask_setting, "Any") == 0)
        this_box_volume.geometry.mask_mode = 2;
      else {
        printf ("The mask_mode of component %s is set to %s, but must be either ALL or ANY.\n", NAME_CURRENT_COMP, mask_setting);
        exit (1);
      }
    }

    int found_geometries = 0;
    for (loop_index = 0; loop_index < global_geometry_list->num_elements; loop_index++) {
      // Add mask list
      if (1 == manual_linking_function (global_geometry_list->elements[loop_index].name, mask_string)) {
        add_element_to_int_list (&this_box_volume.geometry.mask_list, global_geometry_list->elements[loop_index].component_index);
        add_element_to_int_list (&global_geometry_list->elements[loop_index].Volume->geometry.masked_by_list, INDEX_CURRENT_COMP);
        global_geometry_list->elements[loop_index].Volume->geometry.is_masked_volume = 1;
        if (this_box_volume.geometry.mask_mode == 2)
          global_geometry_list->elements[loop_index].Volume->geometry.mask_mode = 2;
        if (this_box_volume.geometry.mask_mode == 1) {
          if (global_geometry_list->elements[loop_index].Volume->geometry.is_masked_volume == 1
              && global_geometry_list->elements[loop_index].Volume->geometry.mask_mode != 2)
            // If more than one mask is added to one volume, the ANY mode overwrites the (default) ALL mode.
            global_geometry_list->elements[loop_index].Volume->geometry.mask_mode = 1;
        }

        found_geometries = 1;
      }
    }
    if (found_geometries == 0) {
      printf ("The mask_string in geometry: %s did not find any of the specified volumes in the mask_string %s \n", NAME_CURRENT_COMP, mask_string);
      exit (1);
    }
    this_box_volume.p_physics = malloc (sizeof (struct physics_struct));
    this_box_volume.p_physics->is_vacuum = 0;                // Makes this volume a vacuum
    this_box_volume.p_physics->number_of_processes = (int)0; // Should not be used.
    this_box_volume.p_physics->my_a = 0;                     // Should not be used.
    sprintf (this_box_volume.p_physics->name, "Mask");
    this_box_volume.geometry.is_mask_volume = 1;

    // Read the material input, or if it lacks, use automatic linking.
  } else if (material_string && strlen (material_string) && strcmp (material_string, "NULL") && strcmp (material_string, "0")) {
    // A geometry string was given, use it to determine which material
    if (0 == strcmp (material_string, "vacuum") || 0 == strcmp (material_string, "Vacuum")) {
      // One could have a global physics struct for vacuum instead of creating one for each
      this_box_volume.p_physics = malloc (sizeof (struct physics_struct));
      this_box_volume.p_physics->is_vacuum = 1;                // Makes this volume a vacuum
      this_box_volume.p_physics->number_of_processes = (int)0; // Should not be used.
      this_box_volume.p_physics->my_a = 0;                     // Should not be used.
      sprintf (this_box_volume.p_physics->name, "Vacuum");
    } else if (0 == strcmp (material_string, "exit") || 0 == strcmp (material_string, "Exit")) {
      // One could have a global physics struct for vacuum instead of creating one for each
      this_box_volume.p_physics = malloc (sizeof (struct physics_struct));
      this_box_volume.p_physics->is_vacuum = 1;                // Makes this volume a vacuum
      this_box_volume.p_physics->number_of_processes = (int)0; // Should not be used.
      this_box_volume.p_physics->my_a = 0;                     // Should not be used.
      this_box_volume.geometry.is_exit_volume = 1;
      sprintf (this_box_volume.p_physics->name, "Exit");
    } else {
      #ifndef MATERIAL_DETECTOR
      printf ("Need to define a material before refering to it in a geometry %s.\n", NAME_CURRENT_COMP);
      exit (1);
      #endif
      for (loop_index = 0; loop_index < global_material_list->num_elements; loop_index++) {
        if (0 == strcmp (material_string, global_material_list->elements[loop_index].name)) {
          this_box_volume.p_physics = global_material_list->elements[loop_index].physics;
          break;
        }
        if (loop_index == global_material_list->num_elements - 1) {
          printf ("\n");
          printf ("ERROR: The material string \"%s\" in Union geometry \"%s\" did not match a specified material. \n", material_string, NAME_CURRENT_COMP);
          printf ("       The materials available at this point (need to be defined before the geometry): \n");
          for (loop_index = 0; loop_index < global_material_list->num_elements; loop_index++)
            printf ("         %s\n", global_material_list->elements[loop_index].name);
          printf ("\n");
          printf ("       It is also possible to use one of the defualt materials avaiable: \n");
          printf ("           Vacuum (for a Volume without scattering or absorption)\n");
          printf ("           Exit (for a Volume where the ray exits the component if it enters)\n");
          printf ("           Mask (for a Volume that masks existing volumes specified in the mask_string\n");
          exit (1);
        }
      }
    }
  } else {
    // Automatic linking, simply using the last defined material.
    #ifndef MATERIAL_DETECTOR
    printf ("Need to define a material before the geometry to use automatic linking %s.\n", NAME_CURRENT_COMP);
    exit (1);
    #endif
    this_box_volume.p_physics = global_material_list->elements[global_material_list->num_elements - 1].physics;
  }

  sprintf (this_box_volume.name, "%s", NAME_CURRENT_COMP);
  sprintf (this_box_volume.geometry.shape, "box");
  this_box_volume.geometry.eShape = box;
  this_box_volume.geometry.priority_value = priority;
  this_box_volume.geometry.geometry_p_interact = p_interact;
  // Currently the coordinates will be in absolute space.
  this_box_volume.geometry.center = POS_A_CURRENT_COMP;

  this_box_storage.z_depth = zdepth;
  this_box_storage.x_width1 = xwidth;
  this_box_storage.y_height1 = yheight;

  this_box_storage.is_rectangle = 0;
  if (xwidth2 < 0 && yheight2 < 0)
    this_box_storage.is_rectangle = 1;
  if (xwidth == xwidth2 && yheight == yheight2)
    this_box_storage.is_rectangle = 1;

  if (xwidth2 < 0) {
    this_box_storage.x_width2 = xwidth;
    xwidth2 = xwidth;
  } else
    this_box_storage.x_width2 = xwidth2;

  if (yheight2 < 0) {
    this_box_storage.y_height2 = yheight;
    yheight2 = yheight;
  } else
    this_box_storage.y_height2 = yheight2;

  this_box_storage.normal_vectors[0] = coords_set (0, 0, 1);
  this_box_storage.normal_vectors[1] = coords_set (0, 0, 1);

  // for sides with y component = 0
  x_component = 2.0 * zdepth / sqrt ((xwidth - xwidth2) * (xwidth - xwidth2) + 4.0 * zdepth * zdepth);
  z_component = (xwidth - xwidth2) / sqrt (4.0 * zdepth * zdepth + (xwidth - xwidth2) * (xwidth - xwidth2));

  this_box_storage.normal_vectors[2] = coords_set (x_component, 0.0, z_component);
  this_box_storage.normal_vectors[3] = coords_set (-x_component, 0.0, z_component);

  // for sides with x component = 0
  y_component = 2.0 * zdepth / sqrt ((yheight - yheight2) * (yheight - yheight2) + 4.0 * zdepth * zdepth);
  z_component = (yheight - yheight2) / sqrt (4.0 * zdepth * zdepth + (yheight - yheight2) * (yheight - yheight2));

  this_box_storage.normal_vectors[4] = coords_set (0, y_component, z_component);
  this_box_storage.normal_vectors[5] = coords_set (0, -y_component, z_component);

  this_box_volume.geometry.visualization_on = visualize;

  this_box_volume.geometry.geometry_parameters.p_box_storage = &this_box_storage;

  // Assign pointers to functions for intersection with the shape, checking if a point is inside the shape
  if (this_box_storage.is_rectangle == 1) {
    this_box_volume.geometry.intersect_function = &sample_box_intersect_simple;
    this_box_volume.geometry.within_function = &r_within_box_simple;
  } else {
    this_box_volume.geometry.intersect_function = &sample_box_intersect_advanced;
    this_box_volume.geometry.within_function = &r_within_box_advanced;
  }

  this_box_volume.geometry.shell_points = &box_shell_points;
  this_box_volume.geometry.mcdisplay_function = &mcdisplay_box_function;
  this_box_volume.geometry.initialize_from_main_function = &initialize_box_geometry_from_main_component;
  this_box_volume.geometry.process_rot_allocated = 0;

  this_box_volume.geometry.copy_geometry_parameters = &allocate_box_storage_copy;

  rot_copy (this_box_volume.geometry.rotation_matrix, ROT_A_CURRENT_COMP); // check how ROT_R_CURRENT_COMP would work
  rot_transpose (ROT_A_CURRENT_COMP, this_box_volume.geometry.transpose_rotation_matrix);

  // Handle surface input
  if (all_face_surface && strlen (all_face_surface) && strcmp (all_face_surface, "NULL") && strcmp (all_face_surface, "0")) {
    // Overwrite all other surfaces that have no input (if one wants a surface left without anything, None can be used)
    overwrite_if_empty (plus_x_surface, all_face_surface);
    overwrite_if_empty (minus_x_surface, all_face_surface);
    overwrite_if_empty (plus_y_surface, all_face_surface);
    overwrite_if_empty (minus_y_surface, all_face_surface);
    overwrite_if_empty (plus_z_surface, all_face_surface);
    overwrite_if_empty (minus_z_surface, all_face_surface);
  }

  this_box_volume.geometry.number_of_faces = 6;
  this_box_volume.geometry.surface_stack_for_each_face = malloc (this_box_volume.geometry.number_of_faces * sizeof (struct surface_stack_struct*));

  // This could be inserted into the fill_surface_stack function
  this_box_volume.geometry.surface_stack_for_each_face[0] = &minus_z_surface_stack;
  this_box_volume.geometry.surface_stack_for_each_face[1] = &plus_z_surface_stack;
  this_box_volume.geometry.surface_stack_for_each_face[2] = &plus_x_surface_stack;
  this_box_volume.geometry.surface_stack_for_each_face[3] = &minus_x_surface_stack;
  this_box_volume.geometry.surface_stack_for_each_face[4] = &plus_y_surface_stack;
  this_box_volume.geometry.surface_stack_for_each_face[5] = &minus_y_surface_stack;
  this_box_volume.geometry.internal_cut_surface_stack = &cut_surface_stack; // This surface is used if the volume is cut by overlapping of higher priority volume

  struct pointer_to_global_surface_list* global_surface_list = COMP_GETPAR3 (Union_init, init, global_surface_list);
  fill_surface_stack (plus_x_surface, global_surface_list, NAME_CURRENT_COMP, &plus_x_surface_stack);
  fill_surface_stack (minus_x_surface, global_surface_list, NAME_CURRENT_COMP, &minus_x_surface_stack);
  fill_surface_stack (plus_y_surface, global_surface_list, NAME_CURRENT_COMP, &plus_y_surface_stack);
  fill_surface_stack (minus_y_surface, global_surface_list, NAME_CURRENT_COMP, &minus_y_surface_stack);
  fill_surface_stack (plus_z_surface, global_surface_list, NAME_CURRENT_COMP, &plus_z_surface_stack);
  fill_surface_stack (minus_z_surface, global_surface_list, NAME_CURRENT_COMP, &minus_z_surface_stack);
  fill_surface_stack (cut_surface, global_surface_list, NAME_CURRENT_COMP, &cut_surface_stack);

  // Initialize loggers
  this_box_volume.loggers.num_elements = 0;
  this_box_volume.abs_loggers.num_elements = 0;

  // packing the information into the global_geometry_element, which is then included in the global_geometry_list.
  sprintf (global_geometry_element.name, "%s", NAME_CURRENT_COMP);
  global_geometry_element.activation_counter = number_of_activations;
  global_geometry_element.component_index = INDEX_CURRENT_COMP;
  global_geometry_element.Volume = &this_box_volume; // Would be nicer if this m was a pointer, now we have the (small) data two places
  // char *test_component;
  // char *sigma_name;
  // COMP_GETPAR(test_component, sigma_name);
  add_element_to_geometry_list (global_geometry_list, global_geometry_element);
  #undef material_string
  #undef priority
  #undef xwidth
  #undef yheight
  #undef zdepth
  #undef xwidth2
  #undef yheight2
  #undef visualize
  #undef target_index
  #undef target_x
  #undef target_y
  #undef target_z
  #undef focus_aw
  #undef focus_ah
  #undef focus_xw
  #undef focus_xh
  #undef focus_r
  #undef plus_z_surface
  #undef minus_z_surface
  #undef plus_x_surface
  #undef minus_x_surface
  #undef plus_y_surface
  #undef minus_y_surface
  #undef all_face_surface
  #undef cut_surface
  #undef p_interact
  #undef mask_string
  #undef mask_setting
  #undef number_of_activations
  #undef init
  #undef global_geometry_element
  #undef loop_index
  #undef x_component
  #undef y_component
  #undef z_component
  #undef this_box_volume
  #undef this_box_storage
  #undef plus_x_surface_stack
  #undef minus_x_surface_stack
  #undef plus_y_surface_stack
  #undef minus_y_surface_stack
  #undef plus_z_surface_stack
  #undef minus_z_surface_stack
  #undef cut_surface_stack
  return(_comp);
} /* class_Union_box_init */

_class_Union_cone *class_Union_cone_init(_class_Union_cone *_comp
) {
  #define material_string (_comp->_parameters.material_string)
  #define priority (_comp->_parameters.priority)
  #define radius (_comp->_parameters.radius)
  #define radius_top (_comp->_parameters.radius_top)
  #define radius_bottom (_comp->_parameters.radius_bottom)
  #define yheight (_comp->_parameters.yheight)
  #define visualize (_comp->_parameters.visualize)
  #define target_index (_comp->_parameters.target_index)
  #define target_x (_comp->_parameters.target_x)
  #define target_y (_comp->_parameters.target_y)
  #define target_z (_comp->_parameters.target_z)
  #define focus_aw (_comp->_parameters.focus_aw)
  #define focus_ah (_comp->_parameters.focus_ah)
  #define focus_xw (_comp->_parameters.focus_xw)
  #define focus_xh (_comp->_parameters.focus_xh)
  #define focus_r (_comp->_parameters.focus_r)
  #define p_interact (_comp->_parameters.p_interact)
  #define mask_string (_comp->_parameters.mask_string)
  #define mask_setting (_comp->_parameters.mask_setting)
  #define number_of_activations (_comp->_parameters.number_of_activations)
  #define curved_surface (_comp->_parameters.curved_surface)
  #define top_surface (_comp->_parameters.top_surface)
  #define bottom_surface (_comp->_parameters.bottom_surface)
  #define all_face_surface (_comp->_parameters.all_face_surface)
  #define cut_surface (_comp->_parameters.cut_surface)
  #define init (_comp->_parameters.init)
  #define global_geometry_element (_comp->_parameters.global_geometry_element)
  #define loop_index (_comp->_parameters.loop_index)
  #define loop_2_index (_comp->_parameters.loop_2_index)
  #define material_index (_comp->_parameters.material_index)
  #define this_cone_volume (_comp->_parameters.this_cone_volume)
  #define this_cone_storage (_comp->_parameters.this_cone_storage)
  #define curved_surface_stack (_comp->_parameters.curved_surface_stack)
  #define top_surface_stack (_comp->_parameters.top_surface_stack)
  #define bottom_surface_stack (_comp->_parameters.bottom_surface_stack)
  #define cut_surface_stack (_comp->_parameters.cut_surface_stack)
  SIG_MESSAGE("[_sample_cone_init] component sample_cone=Union_cone() INITIALISE [Union_cone:0]");

  geometry_struct_init (&(this_cone_volume.geometry));
  // Initializes the focusing system for this volume including input sanitation.
  focus_initialize (&this_cone_volume.geometry, POS_A_COMP_INDEX (INDEX_CURRENT_COMP + target_index), POS_A_CURRENT_COMP, ROT_A_CURRENT_COMP, target_index,
                    target_x, target_y, target_z, focus_aw, focus_ah, focus_xw, focus_xh, focus_r, NAME_CURRENT_COMP);

  // Input sanitation for this geometry
  /* Disabled because radius might be 0 on cone
  if (radius <= 0) {
    printf("\nERROR in Union_cylinder named %s, the radius is <= 0. \n",NAME_CURRENT_COMP);
    exit(1);
  }
  */

  if (radius > 0) {
    radius_top = radius;
    radius_bottom = radius;
  }

  if (yheight <= 0) {
    printf ("\nERROR in Union_cone named %s, yheight is <= 0. \n", NAME_CURRENT_COMP);
    exit (1);
  }

  if (_getcomp_index (init) < 0) {
    fprintf (stderr, "Union_cone:%s: Error identifying Union_init component, %s is not a known component name.\n", NAME_CURRENT_COMP, init);
    exit (-1);
  }

  struct pointer_to_global_material_list* global_material_list = COMP_GETPAR3 (Union_init, init, global_material_list);
  // Use sanitation
  #ifdef MATERIAL_DETECTOR
  if (global_material_list->num_elements == 0) {
    // Here if the user have defined a material, but only after this material
    printf ("\nERROR: Need to define a material using Union_make_material before using a Union geometry component. \n");
    printf ("       %s was defined before first use of Union_make_material.\n", NAME_CURRENT_COMP);
    exit (1);
  }
  #endif
  #ifndef MATERIAL_DETECTOR
  printf ("\nERROR: Need to define a material using Union_make_material before using a Union geometry component. \n");
  exit (1);
  #endif

  this_cone_volume.geometry.is_masked_volume = 0;
  this_cone_volume.geometry.is_exit_volume = 0;
  this_cone_volume.geometry.is_mask_volume = 0;
  struct pointer_to_global_geometry_list* global_geometry_list = COMP_GETPAR3 (Union_init, init, global_geometry_list);

  // Read the material input, or if it lacks, use automatic linking.
  if (mask_string && strlen (mask_string) && strcmp (mask_string, "NULL") && strcmp (mask_string, "0")) {
    // A mask volume is used to limit the extend of other volumes, called the masked volumes. These are specified in the mask_string.
    // In order for a ray to enter a masked volume, it needs to be both in the region covered by that volume AND the mask volume.
    // When more than
    this_cone_volume.geometry.mask_mode = 1; // Default mask mode is ALL
    if (mask_setting && strlen (mask_setting) && strcmp (mask_setting, "NULL") && strcmp (mask_setting, "0")) {
      if (strcmp (mask_setting, "ALL") == 0 || strcmp (mask_setting, "All") == 0)
        this_cone_volume.geometry.mask_mode = 1;
      else if (strcmp (mask_setting, "ANY") == 0 || strcmp (mask_setting, "Any") == 0)
        this_cone_volume.geometry.mask_mode = 2;
      else {
        printf ("The mask_mode of component %s is set to %s, but must be either ALL or ANY.\n", NAME_CURRENT_COMP, mask_setting);
        exit (1);
      }
    }

    int found_geometries = 0;
    for (loop_index = 0; loop_index < global_geometry_list->num_elements; loop_index++) {
      // Add mask list
      if (1 == manual_linking_function (global_geometry_list->elements[loop_index].name, mask_string)) {
        add_element_to_int_list (&this_cone_volume.geometry.mask_list, global_geometry_list->elements[loop_index].component_index);
        add_element_to_int_list (&global_geometry_list->elements[loop_index].Volume->geometry.masked_by_list, INDEX_CURRENT_COMP);
        global_geometry_list->elements[loop_index].Volume->geometry.is_masked_volume = 1;
        if (this_cone_volume.geometry.mask_mode == 2)
          global_geometry_list->elements[loop_index].Volume->geometry.mask_mode = 2;
        if (this_cone_volume.geometry.mask_mode == 1) {
          if (global_geometry_list->elements[loop_index].Volume->geometry.is_masked_volume == 1
              && global_geometry_list->elements[loop_index].Volume->geometry.mask_mode != 2)
            // If more than one mask is added to one volume, the ANY mode overwrites the (default) ALL mode.
            global_geometry_list->elements[loop_index].Volume->geometry.mask_mode = 1;
        }

        found_geometries = 1;
      }
    }
    if (found_geometries == 0) {
      printf ("The mask_string in geometry: %s did not find any of the specified volumes in the mask_string %s \n", NAME_CURRENT_COMP, mask_string);
      exit (1);
    }
    this_cone_volume.p_physics = malloc (sizeof (struct physics_struct));
    this_cone_volume.p_physics->is_vacuum = 0;                // Makes this volume a vacuum
    this_cone_volume.p_physics->number_of_processes = (int)0; // Should not be used.
    this_cone_volume.p_physics->my_a = 0;                     // Should not be used.
    sprintf (this_cone_volume.p_physics->name, "Mask");
    this_cone_volume.geometry.is_mask_volume = 1;

    // Read the material input, or if it lacks, use automatic linking.
  } else if (material_string && strlen (material_string) && strcmp (material_string, "NULL") && strcmp (material_string, "0")) {
    // A geometry string was given, use it to determine which material
    if (0 == strcmp (material_string, "vacuum") || 0 == strcmp (material_string, "Vacuum")) {
      // One could have a global physics struct for vacuum instead of creating one for each
      this_cone_volume.p_physics = malloc (sizeof (struct physics_struct));
      this_cone_volume.p_physics->is_vacuum = 1; // Makes this volume a vacuum
      this_cone_volume.p_physics->number_of_processes = (int)0;
      this_cone_volume.p_physics->my_a = 0; // Should not be used.
      sprintf (this_cone_volume.p_physics->name, "Vacuum");
    } else if (0 == strcmp (material_string, "exit") || 0 == strcmp (material_string, "Exit")) {
      // One could have a global physics struct for exit instead of creating one for each
      this_cone_volume.p_physics = malloc (sizeof (struct physics_struct));
      this_cone_volume.p_physics->is_vacuum = 1; // Makes this volume a vacuum
      this_cone_volume.p_physics->number_of_processes = (int)0;
      this_cone_volume.p_physics->my_a = 0; // Should not be used.
      this_cone_volume.geometry.is_exit_volume = 1;
      sprintf (this_cone_volume.p_physics->name, "Exit");
    } else {
      for (loop_index = 0; loop_index < global_material_list->num_elements; loop_index++) {
        if (0 == strcmp (material_string, global_material_list->elements[loop_index].name)) {
          this_cone_volume.p_physics = global_material_list->elements[loop_index].physics;
          break;
        }
        if (loop_index == global_material_list->num_elements - 1) {
          printf ("\n");
          printf ("ERROR: The material string \"%s\" in Union geometry \"%s\" did not match a specified material. \n", material_string, NAME_CURRENT_COMP);
          printf ("       The materials available at this point (need to be defined before the geometry): \n");
          for (loop_index = 0; loop_index < global_material_list->num_elements; loop_index++)
            printf ("         %s\n", global_material_list->elements[loop_index].name);
          printf ("\n");
          printf ("       It is also possible to use one of the defualt materials avaiable: \n");
          printf ("           Vacuum (for a Volume without scattering or absorption)\n");
          printf ("           Exit (for a Volume where the ray exits the component if it enters)\n");
          printf ("           Mask (for a Volume that masks existing volumes specified in the mask_string\n");
          exit (1);
        }
      }
    }
  } else {
    // Automatic linking, simply using the last defined material.
    #ifndef MATERIAL_DETECTOR
    printf ("Need to define a material before the geometry to use automatic linking %s.\n", NAME_CURRENT_COMP);
    exit (1);
    #endif
    this_cone_volume.p_physics = global_material_list->elements[global_material_list->num_elements - 1].physics;
  }

  // Handle surface input
  if (all_face_surface && strlen (all_face_surface) && strcmp (all_face_surface, "NULL") && strcmp (all_face_surface, "0")) {
    // Overwrite all other surfaces that have no input (if one wants a surface left without anything, None can be used)
    overwrite_if_empty (curved_surface, all_face_surface);
    overwrite_if_empty (top_surface, all_face_surface);
    overwrite_if_empty (bottom_surface, all_face_surface);
  }

  this_cone_volume.geometry.number_of_faces = 3;
  this_cone_volume.geometry.surface_stack_for_each_face = malloc (this_cone_volume.geometry.number_of_faces * sizeof (struct surface_stack_struct*));

  // This could be inserted into the fill_surface_stack function
  this_cone_volume.geometry.surface_stack_for_each_face[0] = &curved_surface_stack;
  this_cone_volume.geometry.surface_stack_for_each_face[1] = &top_surface_stack;
  this_cone_volume.geometry.surface_stack_for_each_face[2] = &bottom_surface_stack;
  this_cone_volume.geometry.internal_cut_surface_stack = &cut_surface_stack; // This surface is used if the volume is cut by overlapping of higher priority volume

  struct pointer_to_global_surface_list* global_surface_list = COMP_GETPAR3 (Union_init, init, global_surface_list);
  fill_surface_stack (curved_surface, global_surface_list, NAME_CURRENT_COMP, &curved_surface_stack);
  fill_surface_stack (top_surface, global_surface_list, NAME_CURRENT_COMP, &top_surface_stack);
  fill_surface_stack (bottom_surface, global_surface_list, NAME_CURRENT_COMP, &bottom_surface_stack);
  fill_surface_stack (cut_surface, global_surface_list, NAME_CURRENT_COMP, &cut_surface_stack);

  // check if this is a cylinder and add coordinates of tip if it is a cone

  /*
  Coords center = POS_A_CURRENT_COMP;
  Coords cone_direction = ROT_A_CURRENT_COMP;
  Coords cone_top_point = coords_add(center,coords_scalar_mult(cone_direction,0.5*yheight));
  Coords cone_bottom_point = coords_add(center,coords_scalar_mult(cone_direction,-0.5*yheight));
  */
  // double cone_gradient = (radius_bottom-radius_top)/yheight;

  int is_cylinder = 0;
  if (radius_top == radius_bottom) {
    is_cylinder = 1;
  }
  /*
  double cone_tip_yheight = 0;
  double theta = 0;
  if (is_cylinder == 0){
      cone_tip_yheight = 0.5*yheight+1/cone_gradient*radius_top;
      theta = asin(cone_gradient)*(180/3.14159265359)/2;
      printf("\nTheta = %2.2f \n",theta);
  }
  */

  this_cone_storage.cone_radius_top = radius_top;
  this_cone_storage.cone_radius_bottom = radius_bottom;
  // this_cone_storage.is_cylinder = is_cylinder;
  // this_cone_storage.cone_tip_yheight = cone_tip_yheight;
  this_cone_storage.height = yheight;
  // this_cone_storage.theta = theta;

  sprintf (this_cone_volume.name, "%s", NAME_CURRENT_COMP);
  sprintf (this_cone_volume.geometry.shape, "cone");
  this_cone_volume.geometry.eShape = cone;
  this_cone_volume.geometry.priority_value = priority;
  // Currently the coordinates will be in absolute space.
  this_cone_volume.geometry.center = POS_A_CURRENT_COMP;

  this_cone_volume.geometry.geometry_p_interact = p_interact;

  this_cone_volume.geometry.visualization_on = visualize;
  this_cone_volume.geometry.geometry_parameters.p_cone_storage = &this_cone_storage;
  this_cone_volume.geometry.within_function = &r_within_cone;
  this_cone_volume.geometry.intersect_function = &sample_cone_intersect;
  this_cone_volume.geometry.mcdisplay_function = &mcdisplay_cone_function;
  this_cone_volume.geometry.shell_points = &shell_points;
  this_cone_volume.geometry.initialize_from_main_function = &initialize_cone_geometry_from_main_component;
  this_cone_volume.geometry.process_rot_allocated = 0;
  this_cone_volume.geometry.copy_geometry_parameters = &allocate_cone_storage_copy;
  rot_copy (this_cone_volume.geometry.rotation_matrix, ROT_A_CURRENT_COMP);
  rot_transpose (ROT_A_CURRENT_COMP, this_cone_volume.geometry.transpose_rotation_matrix);

  // Initialize loggers
  this_cone_volume.loggers.num_elements = 0;
  this_cone_volume.abs_loggers.num_elements = 0;

  // packing the information into the global_geometry_element, which is then included in the global_geometry_list.
  sprintf (global_geometry_element.name, "%s", NAME_CURRENT_COMP);
  global_geometry_element.activation_counter = number_of_activations;
  global_geometry_element.component_index = INDEX_CURRENT_COMP;
  global_geometry_element.Volume = &this_cone_volume; // Would be nicer if this m was a pointer, now we have the (small) data two places
  add_element_to_geometry_list (global_geometry_list, global_geometry_element);
  #undef material_string
  #undef priority
  #undef radius
  #undef radius_top
  #undef radius_bottom
  #undef yheight
  #undef visualize
  #undef target_index
  #undef target_x
  #undef target_y
  #undef target_z
  #undef focus_aw
  #undef focus_ah
  #undef focus_xw
  #undef focus_xh
  #undef focus_r
  #undef p_interact
  #undef mask_string
  #undef mask_setting
  #undef number_of_activations
  #undef curved_surface
  #undef top_surface
  #undef bottom_surface
  #undef all_face_surface
  #undef cut_surface
  #undef init
  #undef global_geometry_element
  #undef loop_index
  #undef loop_2_index
  #undef material_index
  #undef this_cone_volume
  #undef this_cone_storage
  #undef curved_surface_stack
  #undef top_surface_stack
  #undef bottom_surface_stack
  #undef cut_surface_stack
  return(_comp);
} /* class_Union_cone_init */

_class_Union_logger_1D *class_Union_logger_1D_init(_class_Union_logger_1D *_comp
) {
  #define target_geometry (_comp->_parameters.target_geometry)
  #define target_process (_comp->_parameters.target_process)
  #define min_value (_comp->_parameters.min_value)
  #define max_value (_comp->_parameters.max_value)
  #define n1 (_comp->_parameters.n1)
  #define variable (_comp->_parameters.variable)
  #define filename (_comp->_parameters.filename)
  #define order_total (_comp->_parameters.order_total)
  #define order_volume (_comp->_parameters.order_volume)
  #define order_volume_process (_comp->_parameters.order_volume_process)
  #define logger_conditional_extend_index (_comp->_parameters.logger_conditional_extend_index)
  #define init (_comp->_parameters.init)
  #define loop_index (_comp->_parameters.loop_index)
  #define found_process (_comp->_parameters.found_process)
  #define specified_processes (_comp->_parameters.specified_processes)
  #define local_string (_comp->_parameters.local_string)
  #define accepted_processes (_comp->_parameters.accepted_processes)
  #define logger_list_element (_comp->_parameters.logger_list_element)
  #define accepted_volumes (_comp->_parameters.accepted_volumes)
  #define this_logger (_comp->_parameters.this_logger)
  #define this_storage (_comp->_parameters.this_storage)
  #define loggers_on_target_volume (_comp->_parameters.loggers_on_target_volume)
  #define target_volume (_comp->_parameters.target_volume)
  #define temp_string (_comp->_parameters.temp_string)
  SIG_MESSAGE("[_test_logger_1D_init] component test_logger_1D=Union_logger_1D() INITIALISE [Union_logger_1D:0]");

  // Initialize 1d_int_lists
  accepted_processes.elements = NULL;
  accepted_processes.num_elements = 0;

  accepted_volumes.elements = NULL;
  accepted_volumes.num_elements = 0;

  // Initialize storage from input
  if (min_value >= max_value) {
    printf ("ERROR, Union logger \"%s\" had min_value >= max_value.\n", NAME_CURRENT_COMP);
    exit (1);
  }
  this_storage.Detector_1D.min = min_value;
  this_storage.Detector_1D.max = max_value;

  if (n1 <= 0) {
    printf ("ERROR, Union logger \"%s\" had n1 <= 0.\n", NAME_CURRENT_COMP);
    exit (1);
  }
  this_storage.Detector_1D.bins = n1;

  // Remember to take special care when deallocating this array
  this_storage.Detector_1D.Array_N = malloc (n1 * sizeof (double));
  this_storage.Detector_1D.Array_p = malloc (n1 * sizeof (double));
  this_storage.Detector_1D.Array_p2 = malloc (n1 * sizeof (double));

  int l1, l2;
  for (l1 = 0; l1 < n1; l1++) { // n1 is technically a double, but this works fine
    this_storage.Detector_1D.Array_N[l1] = 0;
    this_storage.Detector_1D.Array_p[l1] = 0;
    this_storage.Detector_1D.Array_p2[l1] = 0;
  }

  // printf("past 1D pointer assignment \n");

  // Input sanitation for filename apparently done in 2D_detector_out
  sprintf (this_storage.Detector_1D.title_string, "1D Union logger");
  sprintf (this_storage.Detector_1D.string_axis_value, "Intensity");
  sprintf (this_storage.Detector_1D.Filename, "%s", filename);

  // detect variable type and set axis accordingly
  int variable_identifier = 0; // 1 time, 2 |q|, ... set to 0 as placeholder to make linter play nice
  if (strcmp (variable, "time") == 0) {
    variable_identifier = 1;
    sprintf (this_storage.Detector_1D.string_axis, "time [s]");
    sprintf (this_storage.Detector_1D.string_axis_short, "t");
  } else if (strcmp (variable, "q") == 0) {
    variable_identifier = 2;
    sprintf (this_storage.Detector_1D.string_axis, "q [1/AA]");
    sprintf (this_storage.Detector_1D.string_axis_short, "q");
  } else {
    printf ("ERROR, Union logger \"%s\" had unrecoignized variable name \"%s\", must be either time or q.\n", NAME_CURRENT_COMP, variable);
    exit (1);
  }

  this_storage.variable_identifier = variable_identifier;
  this_storage.order = order_total;
  this_storage.order_in_this_volume = order_volume;
  this_storage.order_process_in_this_volume = order_volume_process;
  this_storage.temp_1D_data.num_elements = 0;
  this_storage.temp_1D_data.allocated_elements = 10;
  this_storage.temp_1D_data.elements = malloc (this_storage.temp_1D_data.allocated_elements * sizeof (struct temp_1D_data_element_struct));

  // printf("past direction choices sanitation \n");

  // Book keeping
  this_logger.logger_extend_index = logger_conditional_extend_index;
  this_logger.function_pointers.active_record_function = &record_to_perm_1D;   // Assume no conditional
  this_logger.function_pointers.inactive_record_function = &record_to_temp_1D; // If an assume is present, these two pointers are switched

  // Temp to perm functions, and standard identifier
  this_logger.function_pointers.select_t_to_p = 1; // 1: temp_to_perm, 2: temp_to_perm_final_p
  this_logger.function_pointers.temp_to_perm = &write_temp_to_perm_1D;
  this_logger.function_pointers.temp_to_perm_final_p = &write_temp_to_perm_final_p_1D;
  this_logger.function_pointers.clear_temp = &clear_temp_1D;
  // Initializing for conditional
  this_logger.conditional_list.num_elements = 0;

  // this_logger.function_pointers.perm_to_disk = &write_perm_to_disk_2DQ; //Obsolete

  // printf("past this_logger function assignment \n");

  this_logger.data_union.p_1D_storage = &this_storage;

  sprintf (this_logger.name, "%s", NAME_CURRENT_COMP);

  // printf("past this_logger assignment \n");

  sprintf (logger_list_element.name, "%s", NAME_CURRENT_COMP);
  logger_list_element.component_index = INDEX_CURRENT_COMP;
  logger_list_element.logger = &this_logger;

  // printf("past logger_list_element assignment \n");

  // In order to run the logger at the right times, pointers to this logger is stored in each volume it logs,
  //  and additionally for each avaiable process. If a process is not logged, the pointer is simply not stored.

  if (_getcomp_index (init) < 0) {
    fprintf (stderr, "Union_logger_1D:%s: Error identifying Union_init component, %s is not a known component name.\n", NAME_CURRENT_COMP, init);
    exit (-1);
  }

  struct pointer_to_global_geometry_list* global_geometry_list = COMP_GETPAR3 (Union_init, init, global_geometry_list);
  struct pointer_to_global_logger_list* global_specific_volumes_logger_list = COMP_GETPAR3 (Union_init, init, global_specific_volumes_logger_list);

  // Need to find the volumes for which the processes should have a reference to this logger
  if (target_geometry && strlen (target_geometry) && strcmp (target_geometry, "NULL") && strcmp (target_geometry, "0")) {
    // Certain volumes were selected, find the indicies in the global_geometry_list
    manual_linking_function_logger_volumes (target_geometry, global_geometry_list, &accepted_volumes, NAME_CURRENT_COMP);
    // Add this logger to the global_specific_volumes_logger_list (so that conditionals can affect it)
    add_element_to_logger_list (global_specific_volumes_logger_list, logger_list_element);

    for (loop_index = 0; loop_index < accepted_volumes.num_elements; loop_index++) {
      target_volume = global_geometry_list->elements[accepted_volumes.elements[loop_index]].Volume;
      // Add an element to its logger list
      add_initialized_logger_in_volume (&target_volume->loggers, target_volume->p_physics->number_of_processes);

      int process_index;
      if (target_process && strlen (target_process) && strcmp (target_process, "NULL") && strcmp (target_process, "0")) {
        // Unused process pointers should point to NULL
        for (process_index = 0; process_index < target_volume->p_physics->number_of_processes; process_index++) {
          target_volume->loggers.p_logger_volume[target_volume->loggers.num_elements - 1].p_logger_process[process_index] = NULL;
        }
        // A target_process was set, find it within the volume structure (can be many processes)
        manual_linking_function_logger_processes (target_process, target_volume->p_physics, &accepted_processes, NAME_CURRENT_COMP, target_volume->name);
        for (process_index = 0; process_index < accepted_processes.num_elements; process_index++) {
          // Add pointer to this logger for all the accepted processes in this newly added loggers element
          target_volume->loggers.p_logger_volume[target_volume->loggers.num_elements - 1].p_logger_process[accepted_processes.elements[process_index]]
              = &this_logger;
        }
      } else {
        // No target_process was set, attatch the logger to all processes
        for (process_index = 0; process_index < target_volume->p_physics->number_of_processes; process_index++) {
          target_volume->loggers.p_logger_volume[target_volume->loggers.num_elements - 1].p_logger_process[process_index] = &this_logger;
        }
      }
    }
  } else {
    // Send to global_all_volumes_logger_list
    // Here there is no system for selecting processes as well
    struct pointer_to_global_logger_list* global_all_volume_logger_list = COMP_GETPAR3 (Union_init, init, global_all_volume_logger_list);
    add_element_to_logger_list (global_all_volume_logger_list, logger_list_element);
  }
  #undef target_geometry
  #undef target_process
  #undef min_value
  #undef max_value
  #undef n1
  #undef variable
  #undef filename
  #undef order_total
  #undef order_volume
  #undef order_volume_process
  #undef logger_conditional_extend_index
  #undef init
  #undef loop_index
  #undef found_process
  #undef specified_processes
  #undef local_string
  #undef accepted_processes
  #undef logger_list_element
  #undef accepted_volumes
  #undef this_logger
  #undef this_storage
  #undef loggers_on_target_volume
  #undef target_volume
  #undef temp_string
  return(_comp);
} /* class_Union_logger_1D_init */

_class_Union_logger_2D_space *class_Union_logger_2D_space_init(_class_Union_logger_2D_space *_comp
) {
  #define target_geometry (_comp->_parameters.target_geometry)
  #define target_process (_comp->_parameters.target_process)
  #define D_direction_1 (_comp->_parameters.D_direction_1)
  #define D1_min (_comp->_parameters.D1_min)
  #define D1_max (_comp->_parameters.D1_max)
  #define n1 (_comp->_parameters.n1)
  #define D_direction_2 (_comp->_parameters.D_direction_2)
  #define D2_min (_comp->_parameters.D2_min)
  #define D2_max (_comp->_parameters.D2_max)
  #define n2 (_comp->_parameters.n2)
  #define filename (_comp->_parameters.filename)
  #define order_total (_comp->_parameters.order_total)
  #define order_volume (_comp->_parameters.order_volume)
  #define order_volume_process (_comp->_parameters.order_volume_process)
  #define logger_conditional_extend_index (_comp->_parameters.logger_conditional_extend_index)
  #define init (_comp->_parameters.init)
  #define loop_index (_comp->_parameters.loop_index)
  #define found_process (_comp->_parameters.found_process)
  #define specified_processes (_comp->_parameters.specified_processes)
  #define local_string (_comp->_parameters.local_string)
  #define accepted_processes (_comp->_parameters.accepted_processes)
  #define logger_list_element (_comp->_parameters.logger_list_element)
  #define accepted_volumes (_comp->_parameters.accepted_volumes)
  #define this_logger (_comp->_parameters.this_logger)
  #define this_storage (_comp->_parameters.this_storage)
  #define loggers_on_target_volume (_comp->_parameters.loggers_on_target_volume)
  #define target_volume (_comp->_parameters.target_volume)
  SIG_MESSAGE("[_test_logger_2D_space_zx_init] component test_logger_2D_space_zx=Union_logger_2D_space() INITIALISE [Union_logger_2D_space:0]");


  accepted_processes.elements = NULL;
  accepted_processes.num_elements = 0;

  accepted_volumes.elements = NULL;
  accepted_volumes.num_elements = 0;

  // Initialize storage from input
  if (D1_min >= D1_max) {
    printf ("ERROR, Union logger \"%s\" had D1_min >= D1_max.\n", NAME_CURRENT_COMP);
    exit (1);
  }
  this_storage.Detector_2D.D1min = D1_min;
  this_storage.Detector_2D.D1max = D1_max;

  if (D2_min >= D2_max) {
    printf ("ERROR, Union logger \"%s\" had D2_min >= D2_max.\n", NAME_CURRENT_COMP);
    exit (1);
  }
  this_storage.Detector_2D.D2min = D2_min;
  this_storage.Detector_2D.D2max = D2_max;

  if (n1 <= 0) {
    printf ("ERROR, Union logger \"%s\" had n1 <= 0.\n", NAME_CURRENT_COMP);
    exit (1);
  }
  this_storage.Detector_2D.bins_1 = n1;

  if (n2 <= 0) {
    printf ("ERROR, Union logger \"%s\" had n2 <= 0.\n", NAME_CURRENT_COMP);
    exit (1);
  }
  this_storage.Detector_2D.bins_2 = n2;

  // printf("past input sanitation \n");

  // Remember to take special care when deallocating this array, use free2Ddouble
  this_storage.Detector_2D.Array_N = allocate2Ddouble_2DS (n1, n2); // Here the n1 double is cast to an int
  this_storage.Detector_2D.Array_p = allocate2Ddouble_2DS (n1, n2);
  this_storage.Detector_2D.Array_p2 = allocate2Ddouble_2DS (n1, n2);

  int l1, l2;
  for (l1 = 0; l1 < n1; l1++) { // n1 is technically a double, but this works fine
    for (l2 = 0; l2 < n2; l2++) {
      this_storage.Detector_2D.Array_N[l1][l2] = 0;
      this_storage.Detector_2D.Array_p[l1][l2] = 0;
      this_storage.Detector_2D.Array_p2[l1][l2] = 0;
    }
  }

  // printf("past 2D pointer assignment \n");

  // Input sanitation for filename apparently done in 2D_detector_out

  if (strcmp (D_direction_1, "x") == 0 || strcmp (D_direction_1, "X") == 0) {
    this_storage.dim_1_choice = 0;
    sprintf (this_storage.Detector_2D.string_axis_1, "x [m]");
    sprintf (this_storage.Detector_2D.title_string, "2D position Union logger in plane x");
  } else if (strcmp (D_direction_1, "y") == 0 || strcmp (D_direction_1, "Y") == 0) {
    this_storage.dim_1_choice = 1;
    sprintf (this_storage.Detector_2D.string_axis_1, "y [m]");
    sprintf (this_storage.Detector_2D.title_string, "2D position Union logger in plane y");
  } else if (strcmp (D_direction_1, "z") == 0 || strcmp (D_direction_1, "Z") == 0) {
    this_storage.dim_1_choice = 2;
    sprintf (this_storage.Detector_2D.string_axis_1, "z [m]");
    sprintf (this_storage.Detector_2D.title_string, "2D position Union logger in plane z");
  } else {
    printf ("ERROR, Union logger 2DS named \"%s\" has an invalid D_direction_1 string, it should be \"x\",\"y\" or \"z\".\n", NAME_CURRENT_COMP);
    exit (1);
  }

  char temp_string[2] = "UN"; // UN stands for unset. Courtesy of linter play
  if (strcmp (D_direction_2, "x") == 0 || strcmp (D_direction_2, "X") == 0) {
    this_storage.dim_2_choice = 0;
    sprintf (this_storage.Detector_2D.string_axis_2, "x [m]");
    sprintf (temp_string, "x");
  } else if (strcmp (D_direction_2, "y") == 0 || strcmp (D_direction_2, "Y") == 0) {
    this_storage.dim_2_choice = 1;
    sprintf (this_storage.Detector_2D.string_axis_2, "y [m]");
    sprintf (temp_string, "y");
  } else if (strcmp (D_direction_2, "z") == 0 || strcmp (D_direction_2, "Z") == 0) {
    this_storage.dim_2_choice = 2;
    sprintf (this_storage.Detector_2D.string_axis_2, "z [m]");
    sprintf (temp_string, "z");
  } else {
    printf ("ERROR, Union logger 2DS named \"%s\" has an invalid D_direction_2 string, it should be \"x\",\"y\" or \"z\".\n", NAME_CURRENT_COMP);
    exit (1);
  }

  strcat (this_storage.Detector_2D.title_string, temp_string); // Connects the title string started in D_direction_1 part with the ending in D_direction_2 part

  sprintf (this_storage.Detector_2D.Filename, "%s", filename);

  this_storage.order = order_total;
  this_storage.order_in_this_volume = order_volume;
  this_storage.order_process_in_this_volume = order_volume_process;
  this_storage.temp_2DS_data.num_elements = 0;
  // Added 17/11/2016, allocating some elements in initialize makes code during trace simpler
  this_storage.temp_2DS_data.allocated_elements = 10;
  this_storage.temp_2DS_data.elements = malloc (this_storage.temp_2DS_data.allocated_elements * sizeof (struct temp_2DS_data_element_struct));

  if (_getcomp_index (init) < 0) {
    fprintf (stderr, "Union_logger_2D_space:%s: Error identifying Union_init component, %s is not a known component name.\n", NAME_CURRENT_COMP, init);
    exit (-1);
  }

  struct global_positions_to_transform_list_struct* global_positions_to_transform_list = COMP_GETPAR3 (Union_init, init, global_positions_to_transform_list);
  struct global_rotations_to_transform_list_struct* global_rotations_to_transform_list = COMP_GETPAR3 (Union_init, init, global_rotations_to_transform_list);
  // Test position and rotation stored in a data storage, and pointers assigned to transform lists
  this_storage.position = POS_A_CURRENT_COMP;
  add_position_pointer_to_list (global_positions_to_transform_list, &this_storage.position);

  rot_copy (this_storage.rotation, ROT_A_CURRENT_COMP);
  add_rotation_pointer_to_list (global_rotations_to_transform_list, &this_storage.rotation);

  rot_transpose (ROT_A_CURRENT_COMP, this_storage.t_rotation);
  add_rotation_pointer_to_list (global_rotations_to_transform_list, &this_storage.t_rotation);

  // printf("past direction choices sanitation \n");

  // Book keeping
  this_logger.logger_extend_index = logger_conditional_extend_index;
  this_logger.function_pointers.active_record_function = &record_to_perm_2DS;   // Assume no conditional
  this_logger.function_pointers.inactive_record_function = &record_to_temp_2DS; // If an assume is present, these two pointers are switched
  // Temp to perm functions, and standard identifier
  this_logger.function_pointers.select_t_to_p = 1; // 1: temp_to_perm, 2: temp_to_perm_final_p
  this_logger.function_pointers.temp_to_perm = &write_temp_to_perm_2DS;
  this_logger.function_pointers.temp_to_perm_final_p = &write_temp_to_perm_final_p_2DS;
  this_logger.function_pointers.clear_temp = &clear_temp_2DS;
  // Initializing for conditional
  this_logger.conditional_list.num_elements = 0;

  // this_logger.function_pointers.perm_to_disk = &write_perm_to_disk_2DS; //Obsolete

  // printf("past this_logger function assignment \n");

  this_logger.data_union.p_2DS_storage = &this_storage;

  sprintf (this_logger.name, "%s", NAME_CURRENT_COMP);

  // printf("past this_logger assignment \n");

  sprintf (logger_list_element.name, "%s", NAME_CURRENT_COMP);
  logger_list_element.component_index = INDEX_CURRENT_COMP;
  logger_list_element.logger = &this_logger;

  // printf("past logger_list_element assignment \n");

  struct pointer_to_global_logger_list* global_specific_volumes_logger_list = COMP_GETPAR3 (Union_init, init, global_specific_volumes_logger_list);
  struct pointer_to_global_geometry_list* global_geometry_list = COMP_GETPAR3 (Union_init, init, global_geometry_list);

  // In order to run the logger at the right times, pointers to this logger is stored in each volume it logs,
  //  and additionally for each avaiable process. If a process is not logged, the pointer is simply not stored.
  int process_index;
  // Need to find the volumes for which the processes should have a reference to this logger
  if (target_geometry && strlen (target_geometry) && strcmp (target_geometry, "NULL") && strcmp (target_geometry, "0")) {
    // Certain volumes were selected, find the indicies in the global_geometry_list
    manual_linking_function_logger_volumes (target_geometry, global_geometry_list, &accepted_volumes, NAME_CURRENT_COMP);
    // Add this logger to the global_specific_volumes_logger_list (so that conditionals can affect it)
    add_element_to_logger_list (global_specific_volumes_logger_list, logger_list_element);

    for (loop_index = 0; loop_index < accepted_volumes.num_elements; loop_index++) {
      target_volume = global_geometry_list->elements[accepted_volumes.elements[loop_index]].Volume;
      // Add an element to its logger list
      add_initialized_logger_in_volume (&target_volume->loggers, target_volume->p_physics->number_of_processes);

      if (target_process && strlen (target_process) && strcmp (target_process, "NULL") && strcmp (target_process, "0")) {
        // Unused process pointers should point to NULL
        for (process_index = 0; process_index < target_volume->p_physics->number_of_processes; process_index++) {
          target_volume->loggers.p_logger_volume[target_volume->loggers.num_elements - 1].p_logger_process[process_index] = NULL;
        }
        // A target_process was set, find it within the volume structure (can be many processes)
        manual_linking_function_logger_processes (target_process, target_volume->p_physics, &accepted_processes, NAME_CURRENT_COMP, target_volume->name);
        for (process_index = 0; process_index < accepted_processes.num_elements; process_index++) {
          // Add pointer to this logger for all the accepted processes in this newly added loggers element
          target_volume->loggers.p_logger_volume[target_volume->loggers.num_elements - 1].p_logger_process[accepted_processes.elements[process_index]]
              = &this_logger;
        }
      } else {
        // No target_process was set, attatch the logger to all processes
        for (process_index = 0; process_index < target_volume->p_physics->number_of_processes; process_index++) {
          target_volume->loggers.p_logger_volume[target_volume->loggers.num_elements - 1].p_logger_process[process_index] = &this_logger;
        }
      }
    }
  } else {
    // Send to global_all_volumes_logger_list
    // Here there is no system for selecting processes as well
    struct pointer_to_global_logger_list* global_all_volume_logger_list = COMP_GETPAR3 (Union_init, init, global_all_volume_logger_list);
    add_element_to_logger_list (global_all_volume_logger_list, logger_list_element);
  }
  #undef target_geometry
  #undef target_process
  #undef D_direction_1
  #undef D1_min
  #undef D1_max
  #undef n1
  #undef D_direction_2
  #undef D2_min
  #undef D2_max
  #undef n2
  #undef filename
  #undef order_total
  #undef order_volume
  #undef order_volume_process
  #undef logger_conditional_extend_index
  #undef init
  #undef loop_index
  #undef found_process
  #undef specified_processes
  #undef local_string
  #undef accepted_processes
  #undef logger_list_element
  #undef accepted_volumes
  #undef this_logger
  #undef this_storage
  #undef loggers_on_target_volume
  #undef target_volume
  return(_comp);
} /* class_Union_logger_2D_space_init */

_class_Union_conditional_standard *class_Union_conditional_standard_init(_class_Union_conditional_standard *_comp
) {
  #define target_loggers (_comp->_parameters.target_loggers)
  #define master_tagging (_comp->_parameters.master_tagging)
  #define tagging_extend_index (_comp->_parameters.tagging_extend_index)
  #define time_min (_comp->_parameters.time_min)
  #define time_max (_comp->_parameters.time_max)
  #define E_min (_comp->_parameters.E_min)
  #define E_max (_comp->_parameters.E_max)
  #define total_order_min (_comp->_parameters.total_order_min)
  #define total_order_max (_comp->_parameters.total_order_max)
  #define last_volume_index (_comp->_parameters.last_volume_index)
  #define overwrite_logger_weight (_comp->_parameters.overwrite_logger_weight)
  #define init (_comp->_parameters.init)
  #define loop_index (_comp->_parameters.loop_index)
  #define return_value (_comp->_parameters.return_value)
  #define accepted_loggers_all (_comp->_parameters.accepted_loggers_all)
  #define accepted_loggers_specific (_comp->_parameters.accepted_loggers_specific)
  #define accepted_abs_loggers_all (_comp->_parameters.accepted_abs_loggers_all)
  #define accepted_abs_loggers_specific (_comp->_parameters.accepted_abs_loggers_specific)
  #define found_logger (_comp->_parameters.found_logger)
  #define found_abs_logger (_comp->_parameters.found_abs_logger)
  #define this_conditional_data (_comp->_parameters.this_conditional_data)
  #define this_data_union (_comp->_parameters.this_data_union)
  #define new_tagging_element (_comp->_parameters.new_tagging_element)
  SIG_MESSAGE("[_test_conditional_standard_init] component test_conditional_standard=Union_conditional_standard() INITIALISE [Union_conditional_standard:0]");

  accepted_loggers_all.elements = NULL;
  accepted_loggers_all.num_elements = 0;
  accepted_loggers_specific.elements = NULL;
  accepted_loggers_specific.num_elements = 0;
  accepted_abs_loggers_all.elements = NULL;
  accepted_abs_loggers_all.num_elements = 0;
  accepted_abs_loggers_specific.elements = NULL;
  accepted_abs_loggers_specific.num_elements = 0;

  // printf("starded conditional initialize \n");
  //  Check if an E range has been set
  if (E_min == E_max)
    // If not, do not check the final energy
    this_conditional_data.E_limit = 0;
  else
    // If it has, check the final energy
    this_conditional_data.E_limit = 1;

  // Likewise for time
  if (time_min == time_max)
    this_conditional_data.T_limit = 0;
  else
    this_conditional_data.T_limit = 1;

  this_conditional_data.Tmin = time_min;
  this_conditional_data.Tmax = time_max;

  if (last_volume_index != -1)
    this_conditional_data.volume_index = last_volume_index;

  // Initialize storage from input
  if (E_min < 0) {
    printf ("ERROR, Union logger \"%s\" had E_min < 0.\n", NAME_CURRENT_COMP);
    exit (1);
  }
  this_conditional_data.Emin = E_min; // The names need to be different to avoid defines ruining the day

  // Initialize storage from input
  if (E_max < 0) {
    printf ("ERROR, Union logger \"%s\" had E_max < 0.\n", NAME_CURRENT_COMP);
    exit (1);
  }
  this_conditional_data.Emax = E_max; // The names need to be different to avoid defines ruining the day

  if (total_order_max != 0)
    this_conditional_data.Total_scat_limit = 1;
  else
    this_conditional_data.Total_scat_limit = 0;

  if (total_order_max < total_order_min) {
    printf ("ERROR, Union logger \"%s\" had total_order_max < total_oder_min.\n", NAME_CURRENT_COMP);
    exit (1);
  }
  this_conditional_data.Total_scat_max = total_order_max;
  this_conditional_data.Total_scat_min = total_order_min;

  if (_getcomp_index (init) < 0) {
    fprintf (stderr, "Union_conditional_standard:%s: Error identifying Union_init component, %s is not a known component name.\n", NAME_CURRENT_COMP, init);
    exit (-1);
  }

  struct global_positions_to_transform_list_struct* global_positions_to_transform_list = COMP_GETPAR3 (Union_init, init, global_positions_to_transform_list);
  struct global_rotations_to_transform_list_struct* global_rotations_to_transform_list = COMP_GETPAR3 (Union_init, init, global_rotations_to_transform_list);
  // Test position and rotation stored in a data storage, and pointers assigned to transform lists
  this_conditional_data.test_position = POS_A_CURRENT_COMP;
  add_position_pointer_to_list (global_positions_to_transform_list, &this_conditional_data.test_position);

  rot_copy (this_conditional_data.test_rotation, ROT_A_CURRENT_COMP);
  add_rotation_pointer_to_list (global_rotations_to_transform_list, &this_conditional_data.test_rotation);

  rot_transpose (ROT_A_CURRENT_COMP, this_conditional_data.test_t_rotation);
  add_rotation_pointer_to_list (global_rotations_to_transform_list, &this_conditional_data.test_t_rotation);

  // Set the data union to be for this specific type of conditional, and supply it with a pointer to its static data
  this_data_union.p_standard = &this_conditional_data;

  struct global_tagging_conditional_list_struct* global_tagging_conditional_list = COMP_GETPAR3 (Union_init, init, global_tagging_conditional_list);

  if (master_tagging == 1) {
    // Apply this conditional to the next master components tagging system
    // When a master is encountered, the current_index is increased, meaning it is always the next master that gets the additional conditional
    if (global_tagging_conditional_list->num_elements < global_tagging_conditional_list->current_index + 1) {
      // printf("allocating new element for global_tagging_conditional_list \n");
      //  New element needs to be allocated first
      new_tagging_element = malloc (sizeof (struct global_tagging_conditional_element_struct));
      new_tagging_element->extend_index = tagging_extend_index;
      new_tagging_element->conditional_list.num_elements = 0;
      strcpy (new_tagging_element->name, NAME_CURRENT_COMP);

      // printf("adding new element to global_tagging_conditional_list \n");
      add_element_to_tagging_conditional_list (global_tagging_conditional_list, *new_tagging_element);
      // printf("added new element to global_tagging_conditional_list \n");

    } else {
      // Making the name reflect all the conditionals used
      strcat (global_tagging_conditional_list->elements[global_tagging_conditional_list->current_index].name, "-");
      strcat (global_tagging_conditional_list->elements[global_tagging_conditional_list->current_index].name, NAME_CURRENT_COMP);
    }
    // Add the conditional element to the current list
    add_function_to_conditional_list (&global_tagging_conditional_list->elements[global_tagging_conditional_list->current_index].conditional_list,
                                      &conditional_function_standard, &this_data_union);
  }

  struct pointer_to_global_logger_list* global_specific_volumes_logger_list = COMP_GETPAR3 (Union_init, init, global_specific_volumes_logger_list);
  struct pointer_to_global_logger_list* global_all_volume_logger_list = COMP_GETPAR3 (Union_init, init, global_all_volume_logger_list);
  struct pointer_to_global_abs_logger_list* global_specific_volumes_abs_logger_list = COMP_GETPAR3 (Union_init, init, global_specific_volumes_abs_logger_list);
  struct pointer_to_global_abs_logger_list* global_all_volume_abs_logger_list = COMP_GETPAR3 (Union_init, init, global_all_volume_abs_logger_list);

  // Need to find the loggers which need to have this conditional applied
  if (target_loggers && strlen (target_loggers) && strcmp (target_loggers, "NULL") && strcmp (target_loggers, "0")) {
    // Certain loggers were selected, find the indicies in the global_specific_volumes_logger_list / global_all_volume_list
    // Create accepted_loggers_specific / accepted_loggers_all

    return_value = manual_linking_logger_function_conditional (target_loggers, global_all_volume_logger_list, &accepted_loggers_all);
    return_value += manual_linking_logger_function_conditional (target_loggers, global_specific_volumes_logger_list, &accepted_loggers_specific);
    return_value += manual_linking_abs_logger_function_conditional (target_loggers, global_all_volume_abs_logger_list, &accepted_abs_loggers_all);
    return_value += manual_linking_abs_logger_function_conditional (target_loggers, global_specific_volumes_abs_logger_list, &accepted_abs_loggers_specific);

    // return_value += manual_linking_tagging_function_conditional(target_loggers, &global_tagging_conditional_list, &accepted_tagging);

    // Need to find a list over tokens that were not taken by either all_volume or specific_volumes

    if (return_value < count_commas (target_loggers) + 1) {
      // Fewer links than tokens were made
      printf ("Union conditional component named \"%s\" did not find all loggers in it's target_logger string \"%s\". \n", NAME_CURRENT_COMP, target_loggers);
      printf ("  A conditional component needs to be linked to a logger in order to function. \n");
      printf ("  The Union logger component must be defined before the conditional that tries to link to it. \n");
      printf ("  A comma separated list of Union logger component names can be given if the conditional should affect many loggers. \n");

      exit (1);
    }

    void (*temp_function_pointer) (Coords*, double*, double*, double, double, double, int, int, int, struct logger_struct*, struct logger_with_data_struct*);
    void (*temp_abs_function_pointer) (Coords*, double*, double, double, int, int, struct abs_logger_struct*, struct abs_logger_with_data_struct*);
    for (loop_index = 0; loop_index < accepted_loggers_all.num_elements; loop_index++) {
      // printf("all loop \n");

      // For each accepted logger do these tasks:
      //  if not conditional function pointer is assigned yet, switch the active and inactive record function pointers in the logger
      //  Assing a function pointer to the conditional function pointer (maybe a list of these if more conditions are to be used)

      found_logger = global_all_volume_logger_list->elements[accepted_loggers_all.elements[loop_index]].logger;

      if (found_logger->conditional_list.num_elements == 0) {
        // Switching function pointers, only necessary for the first application of a conditional
        // This makes the logger record to a temporary storage that is transfered to the permanent in case the conditional is true istead of directly to the
        // permanent storage.
        temp_function_pointer = found_logger->function_pointers.active_record_function;
        found_logger->function_pointers.active_record_function = found_logger->function_pointers.inactive_record_function;
        found_logger->function_pointers.inactive_record_function = temp_function_pointer;
      }

      if (overwrite_logger_weight == 1)
        found_logger->function_pointers.select_t_to_p = 2;

      // Add a pointer to this conditional function to the list of conditionals for this logger
      add_function_to_conditional_list (&found_logger->conditional_list, &conditional_function_standard, &this_data_union);
    }
    for (loop_index = 0; loop_index < accepted_loggers_specific.num_elements; loop_index++) {
      // printf("specific loop \n");
      //  For each accepted logger do these tasks:
      //   if not conditional function pointer is assigned yet, switch the active and inactive record function pointers in the logger
      //   Assing a function pointer to the conditional function pointer (maybe a list of these if more conditions are to be used)

      found_logger = global_specific_volumes_logger_list->elements[accepted_loggers_specific.elements[loop_index]].logger;

      if (found_logger->conditional_list.num_elements == 0) {
        // Switching function pointers, only necessary for the first application of a conditional
        // This makes the logger record to a temporary storage that is transfered to the permanent in case the conditional is true istead of directly to the
        // permanent storage.
        temp_function_pointer = found_logger->function_pointers.active_record_function;
        found_logger->function_pointers.active_record_function = found_logger->function_pointers.inactive_record_function;
        found_logger->function_pointers.inactive_record_function = temp_function_pointer;
      }

      if (overwrite_logger_weight == 1)
        found_logger->function_pointers.select_t_to_p = 2;

      // Add a pointer to this conditional function to the list of conditionals for this logger
      add_function_to_conditional_list (&found_logger->conditional_list, &conditional_function_standard, &this_data_union);
    }
    for (loop_index = 0; loop_index < accepted_abs_loggers_all.num_elements; loop_index++) {
      // printf("all loop \n");

      // For each accepted logger do these tasks:
      //  if not conditional function pointer is assigned yet, switch the active and inactive record function pointers in the logger
      //  Assing a function pointer to the conditional function pointer (maybe a list of these if more conditions are to be used)

      found_abs_logger = global_all_volume_abs_logger_list->elements[accepted_abs_loggers_all.elements[loop_index]].abs_logger;

      if (found_abs_logger->conditional_list.num_elements == 0) {
        // Switching function pointers, only necessary for the first application of a conditional
        // This makes the logger record to a temporary storage that is transfered to the permanent in case the conditional is true istead of directly to the
        // permanent storage.
        temp_abs_function_pointer = found_abs_logger->function_pointers.active_record_function;
        found_abs_logger->function_pointers.active_record_function = found_abs_logger->function_pointers.inactive_record_function;
        found_abs_logger->function_pointers.inactive_record_function = temp_abs_function_pointer;
      }

      // Add a pointer to this conditional function to the list of conditionals for this logger
      add_function_to_conditional_list (&found_abs_logger->conditional_list, &conditional_function_standard, &this_data_union);
    }
    for (loop_index = 0; loop_index < accepted_abs_loggers_specific.num_elements; loop_index++) {
      // printf("specific loop \n");
      //  For each accepted logger do these tasks:
      //   if not conditional function pointer is assigned yet, switch the active and inactive record function pointers in the logger
      //   Assing a function pointer to the conditional function pointer (maybe a list of these if more conditions are to be used)

      found_abs_logger = global_specific_volumes_abs_logger_list->elements[accepted_abs_loggers_specific.elements[loop_index]].abs_logger;

      if (found_abs_logger->conditional_list.num_elements == 0) {
        // Switching function pointers, only necessary for the first application of a conditional
        // This makes the logger record to a temporary storage that is transfered to the permanent in case the conditional is true istead of directly to the
        // permanent storage.
        temp_abs_function_pointer = found_abs_logger->function_pointers.active_record_function;
        found_abs_logger->function_pointers.active_record_function = found_abs_logger->function_pointers.inactive_record_function;
        found_abs_logger->function_pointers.inactive_record_function = temp_abs_function_pointer;
      }

      // Add a pointer to this conditional function to the list of conditionals for this logger
      add_function_to_conditional_list (&found_logger->conditional_list, &conditional_function_standard, &this_data_union);
    }
    /*
    for (loop_index=0;loop_index<accepted_tagging.num_elements;loop_index++) {
      //printf("master loop \n");
      // Adding a conditional to the tagging part of a master does not need any change in the record functions, as it doesn't work as a logger
      add_function_to_conditional_list(&global_tagging_conditional_list->elements[accepted_tagging.elements[loop_index]].conditional_list,&conditional_function_standard,&this_data_union);
    }
    */

  } else {
    // What to do if no target_logger string given? Apply to all loggers? Apply to overall tagging system? Apply to former logger?
    // Give error.
    if (master_tagging == 0) {
      printf ("ERROR: Union conditional component named \"%s\" need the target_logger string, or to be applied to the next master using master_tagging=1. \n",
              NAME_CURRENT_COMP);
      exit (1);
    }
  }

  // printf("completed conditional initialize \n");
  #undef target_loggers
  #undef master_tagging
  #undef tagging_extend_index
  #undef time_min
  #undef time_max
  #undef E_min
  #undef E_max
  #undef total_order_min
  #undef total_order_max
  #undef last_volume_index
  #undef overwrite_logger_weight
  #undef init
  #undef loop_index
  #undef return_value
  #undef accepted_loggers_all
  #undef accepted_loggers_specific
  #undef accepted_abs_loggers_all
  #undef accepted_abs_loggers_specific
  #undef found_logger
  #undef found_abs_logger
  #undef this_conditional_data
  #undef this_data_union
  #undef new_tagging_element
  return(_comp);
} /* class_Union_conditional_standard_init */

_class_Union_conditional_PSD *class_Union_conditional_PSD_init(_class_Union_conditional_PSD *_comp
) {
  #define target_loggers (_comp->_parameters.target_loggers)
  #define master_tagging (_comp->_parameters.master_tagging)
  #define tagging_extend_index (_comp->_parameters.tagging_extend_index)
  #define xwidth (_comp->_parameters.xwidth)
  #define yheight (_comp->_parameters.yheight)
  #define time_min (_comp->_parameters.time_min)
  #define time_max (_comp->_parameters.time_max)
  #define overwrite_logger_weight (_comp->_parameters.overwrite_logger_weight)
  #define init (_comp->_parameters.init)
  #define loop_index (_comp->_parameters.loop_index)
  #define return_value (_comp->_parameters.return_value)
  #define accepted_loggers_all (_comp->_parameters.accepted_loggers_all)
  #define accepted_loggers_specific (_comp->_parameters.accepted_loggers_specific)
  #define accepted_abs_loggers_all (_comp->_parameters.accepted_abs_loggers_all)
  #define accepted_abs_loggers_specific (_comp->_parameters.accepted_abs_loggers_specific)
  #define found_logger (_comp->_parameters.found_logger)
  #define found_abs_logger (_comp->_parameters.found_abs_logger)
  #define this_conditional_data (_comp->_parameters.this_conditional_data)
  #define this_data_union (_comp->_parameters.this_data_union)
  #define new_tagging_element (_comp->_parameters.new_tagging_element)
  SIG_MESSAGE("[_test_conditional_PSD_init] component test_conditional_PSD=Union_conditional_PSD() INITIALISE [Union_conditional_PSD:0]");

  accepted_loggers_all.elements = NULL;
  accepted_loggers_all.num_elements = 0;
  accepted_loggers_specific.elements = NULL;
  accepted_loggers_specific.num_elements = 0;
  accepted_abs_loggers_all.elements = NULL;
  accepted_abs_loggers_all.num_elements = 0;
  accepted_abs_loggers_specific.elements = NULL;
  accepted_abs_loggers_specific.num_elements = 0;

  printf ("starded conditional initialize \n");

  // If time range is set, use it
  if (time_min == time_max)
    this_conditional_data.T_limit = 0;
  else
    this_conditional_data.T_limit = 1;

  this_conditional_data.Tmin = time_min;
  this_conditional_data.Tmax = time_max;

  if (xwidth < 0) {
    printf ("ERROR: In Union conditional %s xwidth is less than 0!\n", NAME_CURRENT_COMP);
  }
  this_conditional_data.PSD_half_xwidth = 0.5 * xwidth;

  if (yheight < 0) {
    printf ("ERROR: In Union conditional %s yheight is less than 0!\n", NAME_CURRENT_COMP);
  }
  this_conditional_data.PSD_half_yheight = 0.5 * yheight;

  if (_getcomp_index (init) < 0) {
    fprintf (stderr, "Union_conditional_PSD:%s: Error identifying Union_init component, %s is not a known component name.\n", NAME_CURRENT_COMP, init);
    exit (-1);
  }

  struct global_positions_to_transform_list_struct* global_positions_to_transform_list = COMP_GETPAR3 (Union_init, init, global_positions_to_transform_list);
  struct global_rotations_to_transform_list_struct* global_rotations_to_transform_list = COMP_GETPAR3 (Union_init, init, global_rotations_to_transform_list);
  // Test position and rotation stored in a data storage, and pointers assigned to transform lists
  this_conditional_data.PSD_position = POS_A_CURRENT_COMP;
  add_position_pointer_to_list (global_positions_to_transform_list, &this_conditional_data.PSD_position);

  rot_copy (this_conditional_data.PSD_rotation, ROT_A_CURRENT_COMP);
  add_rotation_pointer_to_list (global_rotations_to_transform_list, &this_conditional_data.PSD_rotation);

  rot_transpose (ROT_A_CURRENT_COMP, this_conditional_data.PSD_t_rotation);
  add_rotation_pointer_to_list (global_rotations_to_transform_list, &this_conditional_data.PSD_t_rotation);

  // Set the data union to be for this specific type of conditional, and supply it with a pointer to its static data
  this_data_union.p_PSD = &this_conditional_data;

  struct global_tagging_conditional_list_struct* global_tagging_conditional_list = COMP_GETPAR3 (Union_init, init, global_tagging_conditional_list);

  if (master_tagging == 1) {
    // Apply this conditional to the next master components tagging system
    // When a master is encountered, the current_index is increased, meaning it is always the next master that gets the additional conditional
    if (global_tagging_conditional_list->num_elements < global_tagging_conditional_list->current_index + 1) {
      // printf("allocating new element for global_tagging_conditional_list \n");
      //  New element needs to be allocated first
      new_tagging_element = malloc (sizeof (struct global_tagging_conditional_element_struct));
      new_tagging_element->extend_index = tagging_extend_index;
      new_tagging_element->conditional_list.num_elements = 0;
      strcpy (new_tagging_element->name, NAME_CURRENT_COMP);

      // printf("adding new element to global_tagging_conditional_list \n");
      add_element_to_tagging_conditional_list (global_tagging_conditional_list, *new_tagging_element);
      // printf("added new element to global_tagging_conditional_list \n");

    } else {
      // Making the name reflect all the conditionals used
      strcat (global_tagging_conditional_list->elements[global_tagging_conditional_list->current_index].name, "-");
      strcat (global_tagging_conditional_list->elements[global_tagging_conditional_list->current_index].name, NAME_CURRENT_COMP);
    }
    // Add the conditional element to the current list
    add_function_to_conditional_list (&global_tagging_conditional_list->elements[global_tagging_conditional_list->current_index].conditional_list,
                                      &conditional_function_PSD, &this_data_union);
  }

  struct pointer_to_global_logger_list* global_specific_volumes_logger_list = COMP_GETPAR3 (Union_init, init, global_specific_volumes_logger_list);
  struct pointer_to_global_logger_list* global_all_volume_logger_list = COMP_GETPAR3 (Union_init, init, global_all_volume_logger_list);
  struct pointer_to_global_abs_logger_list* global_specific_volumes_abs_logger_list = COMP_GETPAR3 (Union_init, init, global_specific_volumes_abs_logger_list);
  struct pointer_to_global_abs_logger_list* global_all_volume_abs_logger_list = COMP_GETPAR3 (Union_init, init, global_all_volume_abs_logger_list);

  // Need to find the loggers which need to have this conditional applied
  if (target_loggers && strlen (target_loggers) && strcmp (target_loggers, "NULL") && strcmp (target_loggers, "0")) {
    // Certain loggers were selected, find the indicies in the global_specific_volumes_logger_list / global_all_volume_list
    // Create accepted_loggers_specific / accepted_loggers_all

    // printf("Reached target logger section \n");

    return_value = manual_linking_logger_function_conditional (target_loggers, global_all_volume_logger_list, &accepted_loggers_all);
    return_value += manual_linking_logger_function_conditional (target_loggers, global_specific_volumes_logger_list, &accepted_loggers_specific);
    return_value += manual_linking_abs_logger_function_conditional (target_loggers, global_all_volume_abs_logger_list, &accepted_abs_loggers_all);
    return_value += manual_linking_abs_logger_function_conditional (target_loggers, global_specific_volumes_abs_logger_list, &accepted_abs_loggers_specific);

    // return_value += manual_linking_tagging_function_conditional(target_loggers, &global_tagging_conditional_list, &accepted_tagging);

    // Need to find a list over tokens that were not taken by either all_volume or specific_volumes

    if (return_value < count_commas (target_loggers) + 1) {
      // Fewer links than tokens were made
      printf ("Union conditional component named \"%s\" did not find all loggers in it's target_logger string \"%s\". \n", NAME_CURRENT_COMP, target_loggers);
      printf ("  A conditional component needs to be linked to a logger in order to function. \n");
      printf ("  The Union logger component must be defined before the conditional that tries to link to it. \n");
      printf ("  A comma separated list of Union logger component names can be given if the conditional should affect many loggers. \n");

      exit (1);
    }

    // This function pointer is used to switch the logger function pointers for active and inactive logger functions.
    // Need to have the same input (defined in logger_pointer_set_struct in union-lib.c)
    void (*temp_function_pointer) (Coords*, double*, double*, double, double, double, int, int, int, struct logger_struct*, struct logger_with_data_struct*);
    void (*temp_abs_function_pointer) (Coords*, double*, double, double, int, int, struct abs_logger_struct*, struct abs_logger_with_data_struct*);
    for (loop_index = 0; loop_index < accepted_loggers_all.num_elements; loop_index++) {
      // printf("all loop \n");

      // For each accepted logger do these tasks:
      //  if not conditional function pointer is assigned yet, switch the active and inactive record function pointers in the logger
      //  Assing a function pointer to the conditional function pointer (maybe a list of these if more conditions are to be used)

      found_logger = global_all_volume_logger_list->elements[accepted_loggers_all.elements[loop_index]].logger;

      if (found_logger->conditional_list.num_elements == 0) {
        // Switching function pointers, only necessary for the first application of a conditional
        // This makes the logger record to a temporary storage that is transfered to the permanent in case the conditional is true istead of directly to the
        // permanent storage.
        temp_function_pointer = found_logger->function_pointers.active_record_function;
        found_logger->function_pointers.active_record_function = found_logger->function_pointers.inactive_record_function;
        found_logger->function_pointers.inactive_record_function = temp_function_pointer;
      }

      if (overwrite_logger_weight == 1)
        found_logger->function_pointers.select_t_to_p = 2;

      // Add a pointer to this conditional function to the list of conditionals for this logger
      add_function_to_conditional_list (&found_logger->conditional_list, &conditional_function_PSD, &this_data_union);
    }
    for (loop_index = 0; loop_index < accepted_loggers_specific.num_elements; loop_index++) {
      // printf("specific loop \n");
      //  For each accepted logger do these tasks:
      //   if not conditional function pointer is assigned yet, switch the active and inactive record function pointers in the logger
      //   Assing a function pointer to the conditional function pointer (maybe a list of these if more conditions are to be used)

      found_logger = global_specific_volumes_logger_list->elements[accepted_loggers_specific.elements[loop_index]].logger;

      if (found_logger->conditional_list.num_elements == 0) {
        // Switching function pointers, only necessary for the first application of a conditional
        // This makes the logger record to a temporary storage that is transfered to the permanent in case the conditional is true istead of directly to the
        // permanent storage.
        temp_function_pointer = found_logger->function_pointers.active_record_function;
        found_logger->function_pointers.active_record_function = found_logger->function_pointers.inactive_record_function;
        found_logger->function_pointers.inactive_record_function = temp_function_pointer;
      }

      if (overwrite_logger_weight == 1)
        found_logger->function_pointers.select_t_to_p = 2;

      // Add a pointer to this conditional function to the list of conditionals for this logger
      add_function_to_conditional_list (&found_logger->conditional_list, &conditional_function_PSD, &this_data_union);
    }
    for (loop_index = 0; loop_index < accepted_abs_loggers_all.num_elements; loop_index++) {
      // printf("all loop \n");

      // For each accepted logger do these tasks:
      //  if not conditional function pointer is assigned yet, switch the active and inactive record function pointers in the logger
      //  Assing a function pointer to the conditional function pointer (maybe a list of these if more conditions are to be used)

      found_abs_logger = global_all_volume_abs_logger_list->elements[accepted_abs_loggers_all.elements[loop_index]].abs_logger;

      if (found_abs_logger->conditional_list.num_elements == 0) {
        // Switching function pointers, only necessary for the first application of a conditional
        // This makes the logger record to a temporary storage that is transfered to the permanent in case the conditional is true istead of directly to the
        // permanent storage.
        temp_abs_function_pointer = found_abs_logger->function_pointers.active_record_function;
        found_abs_logger->function_pointers.active_record_function = found_abs_logger->function_pointers.inactive_record_function;
        found_abs_logger->function_pointers.inactive_record_function = temp_abs_function_pointer;
      }

      // Add a pointer to this conditional function to the list of conditionals for this logger
      add_function_to_conditional_list (&found_abs_logger->conditional_list, &conditional_function_PSD, &this_data_union);
    }
    for (loop_index = 0; loop_index < accepted_abs_loggers_specific.num_elements; loop_index++) {
      // printf("specific loop \n");
      //  For each accepted logger do these tasks:
      //   if not conditional function pointer is assigned yet, switch the active and inactive record function pointers in the logger
      //   Assing a function pointer to the conditional function pointer (maybe a list of these if more conditions are to be used)

      found_abs_logger = global_specific_volumes_abs_logger_list->elements[accepted_abs_loggers_specific.elements[loop_index]].abs_logger;

      if (found_abs_logger->conditional_list.num_elements == 0) {
        // Switching function pointers, only necessary for the first application of a conditional
        // This makes the logger record to a temporary storage that is transfered to the permanent in case the conditional is true istead of directly to the
        // permanent storage.
        temp_abs_function_pointer = found_abs_logger->function_pointers.active_record_function;
        found_abs_logger->function_pointers.active_record_function = found_abs_logger->function_pointers.inactive_record_function;
        found_abs_logger->function_pointers.inactive_record_function = temp_abs_function_pointer;
      }

      // Add a pointer to this conditional function to the list of conditionals for this logger
      add_function_to_conditional_list (&found_logger->conditional_list, &conditional_function_PSD, &this_data_union);
    }

  } else {
    // What to do if no target_logger string given? Apply to all loggers? Apply to overall tagging system? Apply to former logger?
    // Give error.
    if (master_tagging == 0) {
      printf ("ERROR: Union conditional component named \"%s\" need the target_logger string, or to be applied to the next master using master_tagging=1. \n",
              NAME_CURRENT_COMP);
      exit (1);
    }
  }

  // printf("completed conditional initialize \n");
  #undef target_loggers
  #undef master_tagging
  #undef tagging_extend_index
  #undef xwidth
  #undef yheight
  #undef time_min
  #undef time_max
  #undef overwrite_logger_weight
  #undef init
  #undef loop_index
  #undef return_value
  #undef accepted_loggers_all
  #undef accepted_loggers_specific
  #undef accepted_abs_loggers_all
  #undef accepted_abs_loggers_specific
  #undef found_logger
  #undef found_abs_logger
  #undef this_conditional_data
  #undef this_data_union
  #undef new_tagging_element
  return(_comp);
} /* class_Union_conditional_PSD_init */

_class_Union_master *class_Union_master_init(_class_Union_master *_comp
) {
  #define enable_refraction (_comp->_parameters.enable_refraction)
  #define enable_reflection (_comp->_parameters.enable_reflection)
  #define verbal (_comp->_parameters.verbal)
  #define list_verbal (_comp->_parameters.list_verbal)
  #define finally_verbal (_comp->_parameters.finally_verbal)
  #define allow_inside_start (_comp->_parameters.allow_inside_start)
  #define enable_tagging (_comp->_parameters.enable_tagging)
  #define history_limit (_comp->_parameters.history_limit)
  #define enable_conditionals (_comp->_parameters.enable_conditionals)
  #define inherit_number_of_scattering_events (_comp->_parameters.inherit_number_of_scattering_events)
  #define weight_ratio_limit (_comp->_parameters.weight_ratio_limit)
  #define init (_comp->_parameters.init)
  #define global_positions_to_transform_list_master (_comp->_parameters.global_positions_to_transform_list_master)
  #define global_rotations_to_transform_list_master (_comp->_parameters.global_rotations_to_transform_list_master)
  #define global_process_list_master (_comp->_parameters.global_process_list_master)
  #define global_material_list_master (_comp->_parameters.global_material_list_master)
  #define global_surface_list_master (_comp->_parameters.global_surface_list_master)
  #define global_geometry_list_master (_comp->_parameters.global_geometry_list_master)
  #define global_all_volume_logger_list_master (_comp->_parameters.global_all_volume_logger_list_master)
  #define global_specific_volumes_logger_list_master (_comp->_parameters.global_specific_volumes_logger_list_master)
  #define global_all_volume_abs_logger_list_master (_comp->_parameters.global_all_volume_abs_logger_list_master)
  #define global_specific_volumes_abs_logger_list_master (_comp->_parameters.global_specific_volumes_abs_logger_list_master)
  #define global_tagging_conditional_list_master (_comp->_parameters.global_tagging_conditional_list_master)
  #define global_master_list_master (_comp->_parameters.global_master_list_master)
  #define starting_volume_warning (_comp->_parameters.starting_volume_warning)
  #define global_master_element (_comp->_parameters.global_master_element)
  #define this_global_master_index (_comp->_parameters.this_global_master_index)
  #define previous_master_index (_comp->_parameters.previous_master_index)
  #define geometry_list_index (_comp->_parameters.geometry_list_index)
  #define intersection_time_table (_comp->_parameters.intersection_time_table)
  #define Volumes (_comp->_parameters.Volumes)
  #define Geometries (_comp->_parameters.Geometries)
  #define Volume_copies (_comp->_parameters.Volume_copies)
  #define starting_lists (_comp->_parameters.starting_lists)
  #define Volume_copies_allocated (_comp->_parameters.Volume_copies_allocated)
  #define r (_comp->_parameters.r)
  #define r_start (_comp->_parameters.r_start)
  #define v (_comp->_parameters.v)
  #define error_msg (_comp->_parameters.error_msg)
  #define component_error_msg (_comp->_parameters.component_error_msg)
  #define string_output (_comp->_parameters.string_output)
  #define number_of_volumes (_comp->_parameters.number_of_volumes)
  #define volume_index (_comp->_parameters.volume_index)
  #define process_index (_comp->_parameters.process_index)
  #define iterator (_comp->_parameters.iterator)
  #define solutions (_comp->_parameters.solutions)
  #define max_number_of_processes (_comp->_parameters.max_number_of_processes)
  #define limit (_comp->_parameters.limit)
  #define solution (_comp->_parameters.solution)
  #define min_solution (_comp->_parameters.min_solution)
  #define ignore_closest (_comp->_parameters.ignore_closest)
  #define ignore_surface_index (_comp->_parameters.ignore_surface_index)
  #define min_volume (_comp->_parameters.min_volume)
  #define time_found (_comp->_parameters.time_found)
  #define intersection_time (_comp->_parameters.intersection_time)
  #define min_intersection_time (_comp->_parameters.min_intersection_time)
  #define process (_comp->_parameters.process)
  #define process_start (_comp->_parameters.process_start)
  #define my_trace (_comp->_parameters.my_trace)
  #define p_my_trace (_comp->_parameters.p_my_trace)
  #define my_trace_fraction_control (_comp->_parameters.my_trace_fraction_control)
  #define k (_comp->_parameters.k)
  #define k_new (_comp->_parameters.k_new)
  #define k_old (_comp->_parameters.k_old)
  #define k_rotated (_comp->_parameters.k_rotated)
  #define v_length (_comp->_parameters.v_length)
  #define my_sum (_comp->_parameters.my_sum)
  #define my_sum_plus_abs (_comp->_parameters.my_sum_plus_abs)
  #define culmative_probability (_comp->_parameters.culmative_probability)
  #define mc_prop (_comp->_parameters.mc_prop)
  #define time_to_scattering (_comp->_parameters.time_to_scattering)
  #define length_to_scattering (_comp->_parameters.length_to_scattering)
  #define length_to_boundary (_comp->_parameters.length_to_boundary)
  #define time_to_boundery (_comp->_parameters.time_to_boundery)
  #define selected_process (_comp->_parameters.selected_process)
  #define scattering_event (_comp->_parameters.scattering_event)
  #define time_propagated_without_scattering (_comp->_parameters.time_propagated_without_scattering)
  #define a_next_volume_found (_comp->_parameters.a_next_volume_found)
  #define next_volume (_comp->_parameters.next_volume)
  #define next_volume_priority (_comp->_parameters.next_volume_priority)
  #define done (_comp->_parameters.done)
  #define current_volume (_comp->_parameters.current_volume)
  #define previous_volume (_comp->_parameters.previous_volume)
  #define ray_sucseeded (_comp->_parameters.ray_sucseeded)
  #define number_of_solutions (_comp->_parameters.number_of_solutions)
  #define number_of_solutions_static (_comp->_parameters.number_of_solutions_static)
  #define check (_comp->_parameters.check)
  #define start (_comp->_parameters.start)
  #define intersection_with_children (_comp->_parameters.intersection_with_children)
  #define geometry_output (_comp->_parameters.geometry_output)
  #define tree_next_volume (_comp->_parameters.tree_next_volume)
  #define pre_allocated1 (_comp->_parameters.pre_allocated1)
  #define pre_allocated2 (_comp->_parameters.pre_allocated2)
  #define pre_allocated3 (_comp->_parameters.pre_allocated3)
  #define ray_position (_comp->_parameters.ray_position)
  #define ray_velocity (_comp->_parameters.ray_velocity)
  #define ray_velocity_rotated (_comp->_parameters.ray_velocity_rotated)
  #define ray_velocity_final (_comp->_parameters.ray_velocity_final)
  #define wavevector (_comp->_parameters.wavevector)
  #define wavevector_rotated (_comp->_parameters.wavevector_rotated)
  #define volume_0_found (_comp->_parameters.volume_0_found)
  #define scattered_flag (_comp->_parameters.scattered_flag)
  #define scattered_flag_VP (_comp->_parameters.scattered_flag_VP)
  #define master_transposed_rotation_matrix (_comp->_parameters.master_transposed_rotation_matrix)
  #define temp_rotation_matrix (_comp->_parameters.temp_rotation_matrix)
  #define temp_transpose_rotation_matrix (_comp->_parameters.temp_transpose_rotation_matrix)
  #define non_rotated_position (_comp->_parameters.non_rotated_position)
  #define rotated_position (_comp->_parameters.rotated_position)
  #define non_isotropic_found (_comp->_parameters.non_isotropic_found)
  #define master_tagging_node_list (_comp->_parameters.master_tagging_node_list)
  #define current_tagging_node (_comp->_parameters.current_tagging_node)
  #define tagging_leaf_counter (_comp->_parameters.tagging_leaf_counter)
  #define stop_tagging_ray (_comp->_parameters.stop_tagging_ray)
  #define stop_creating_nodes (_comp->_parameters.stop_creating_nodes)
  #define number_of_scattering_events (_comp->_parameters.number_of_scattering_events)
  #define real_transmission_probability (_comp->_parameters.real_transmission_probability)
  #define mc_transmission_probability (_comp->_parameters.mc_transmission_probability)
  #define number_of_process_interacts_set (_comp->_parameters.number_of_process_interacts_set)
  #define index_of_lacking_process (_comp->_parameters.index_of_lacking_process)
  #define total_process_interact (_comp->_parameters.total_process_interact)
  #define geometry_component_index_list (_comp->_parameters.geometry_component_index_list)
  #define mask_volume_index_list (_comp->_parameters.mask_volume_index_list)
  #define number_of_masks (_comp->_parameters.number_of_masks)
  #define number_of_masked_volumes (_comp->_parameters.number_of_masked_volumes)
  #define mask_status_list (_comp->_parameters.mask_status_list)
  #define current_mask_intersect_list_status (_comp->_parameters.current_mask_intersect_list_status)
  #define mask_index_main (_comp->_parameters.mask_index_main)
  #define mask_iterator (_comp->_parameters.mask_iterator)
  #define mask_start (_comp->_parameters.mask_start)
  #define mask_check (_comp->_parameters.mask_check)
  #define need_to_run_within_which_volume (_comp->_parameters.need_to_run_within_which_volume)
  #define number_of_processes_array (_comp->_parameters.number_of_processes_array)
  #define p_old (_comp->_parameters.p_old)
  #define log_index (_comp->_parameters.log_index)
  #define conditional_status (_comp->_parameters.conditional_status)
  #define this_logger (_comp->_parameters.this_logger)
  #define this_abs_logger (_comp->_parameters.this_abs_logger)
  #define tagging_conditional_list (_comp->_parameters.tagging_conditional_list)
  #define logger_conditional_extend_array (_comp->_parameters.logger_conditional_extend_array)
  #define abs_logger_conditional_extend_array (_comp->_parameters.abs_logger_conditional_extend_array)
  #define max_conditional_extend_index (_comp->_parameters.max_conditional_extend_index)
  #define tagging_conditional_extend (_comp->_parameters.tagging_conditional_extend)
  #define free_tagging_conditioanl_list (_comp->_parameters.free_tagging_conditioanl_list)
  #define safety_distance (_comp->_parameters.safety_distance)
  #define safety_distance2 (_comp->_parameters.safety_distance2)
  #define temporary_focus_data (_comp->_parameters.temporary_focus_data)
  #define this_focus_data (_comp->_parameters.this_focus_data)
  #define focus_data_index (_comp->_parameters.focus_data_index)
  #define r_old (_comp->_parameters.r_old)
  #define initial_weight (_comp->_parameters.initial_weight)
  #define abs_weight_factor (_comp->_parameters.abs_weight_factor)
  #define time_old (_comp->_parameters.time_old)
  #define absorption_index (_comp->_parameters.absorption_index)
  #define abs_weight_factor_set (_comp->_parameters.abs_weight_factor_set)
  #define my_abs (_comp->_parameters.my_abs)
  #define absorption_event_data (_comp->_parameters.absorption_event_data)
  #define abs_position (_comp->_parameters.abs_position)
  #define transformed_abs_position (_comp->_parameters.transformed_abs_position)
  #define t_abs_propagation (_comp->_parameters.t_abs_propagation)
  #define abs_distance (_comp->_parameters.abs_distance)
  #define abs_max_length (_comp->_parameters.abs_max_length)
  #define longest_surface_stack (_comp->_parameters.longest_surface_stack)
  #define interface_stack (_comp->_parameters.interface_stack)
  SIG_MESSAGE("[_sample_init] component sample=Union_master() INITIALISE [Union_master:0]");

  if (_getcomp_index (init) < 0) {
    fprintf (stderr, "Union_master:%s: Error identifying Union_init component, %s is not a known component name.\n", NAME_CURRENT_COMP, init);
    exit (-1);
  }
  // Unpack global lists
  global_positions_to_transform_list_master = COMP_GETPAR3 (Union_init, init, global_positions_to_transform_list);
  global_rotations_to_transform_list_master = COMP_GETPAR3 (Union_init, init, global_rotations_to_transform_list);
  global_process_list_master = COMP_GETPAR3 (Union_init, init, global_process_list);
  global_material_list_master = COMP_GETPAR3 (Union_init, init, global_material_list);
  global_surface_list_master = COMP_GETPAR3 (Union_init, init, global_surface_list);
  global_geometry_list_master = COMP_GETPAR3 (Union_init, init, global_geometry_list);
  global_all_volume_logger_list_master = COMP_GETPAR3 (Union_init, init, global_all_volume_logger_list);
  global_specific_volumes_logger_list_master = COMP_GETPAR3 (Union_init, init, global_specific_volumes_logger_list);
  global_all_volume_abs_logger_list_master = COMP_GETPAR3 (Union_init, init, global_all_volume_abs_logger_list);
  global_specific_volumes_abs_logger_list_master = COMP_GETPAR3 (Union_init, init, global_specific_volumes_abs_logger_list);
  global_tagging_conditional_list_master = COMP_GETPAR3 (Union_init, init, global_tagging_conditional_list);
  global_master_list_master = COMP_GETPAR3 (Union_init, init, global_master_list);

  // It is possible to surpress warnings on starting volume by setting this to 1
  starting_volume_warning = 0;

  // Start at 0 error messages, quit after 100.
  component_error_msg = 0;

  // For within_which_volume
  volume_0_found = 0;

  // For tagging
  tagging_leaf_counter = 0;

  // For masks
  number_of_masks = 0;
  number_of_masked_volumes = 0;

  // For surfaces
  longest_surface_stack = 0;

  // Use sanitation
  #ifndef ANY_GEOMETRY_DETECTOR_DECLARE
  printf ("\nERROR: Need to define at least one Volume using Union_cylinder or Union_box before using the Union_master component. \n");
  exit (1);
  #endif
  #ifdef ANY_GEOMETRY_DETECTOR_DECLARE
  if (global_geometry_list_master->num_elements == 0) {
    printf ("\nERROR: Need to define at least one Volume using Union_cylinder or Union_box before using the Union_master component. \n");
    printf ("       Union_master component named \"%s\" is before any Volumes in the instrument file. At least one Volume need to be defined before\n",
            NAME_CURRENT_COMP);

    exit (1);
  }
  #endif

  // Parameters describing the safety distances close to surfaces, as scattering should not occur closer to a surface than the
  //  accuracy of the intersection calculation.
  safety_distance = 1E-11;
  safety_distance2 = safety_distance * 2.0;

  // Write information to the global_master_list_master about the current Union_master
  sprintf (global_master_element.name, "%s", NAME_CURRENT_COMP);
  global_master_element.component_index = INDEX_CURRENT_COMP;
  add_element_to_master_list (global_master_list_master, global_master_element);
  if (inherit_number_of_scattering_events == 1 && global_master_list_master->num_elements == 1) {
    printf ("ERROR in Union_master with name %s. Inherit_number_of_scattering_events set to 1 for first Union_master component, but there is no preceeding "
            "Union_master component. Aborting.\n",
            NAME_CURRENT_COMP);
    exit (1);
  }
  this_global_master_index = global_master_list_master->num_elements - 1; // Save the index for this master in global master list

  // Set the component index of the previous Union_master component if one exists
  if (global_master_list_master->num_elements == 1)
    previous_master_index = 0; // no previous index
  else
    previous_master_index = global_master_list_master->elements[global_master_list_master->num_elements - 2]
                                .component_index; // -2 because of zero indexing and needing the previous index.
  // printf("Assigned previous_master_index = %d \n",previous_master_index);

  // All volumes in the global_geometry_list_master is being check for activity using the number_of_activations input made for each geometry (default is 1)
  // In addition it is counted how many volumes, mask volumes and masked volumes are active in this Union_master.
  number_of_volumes = 1;        // Starting with 1 as the surrounding vacuum is considered a volume
  number_of_masks = 0;          // Starting with 0 mask volumes
  number_of_masked_volumes = 0; // Starting with 0 masked volumes
  for (iterator = 0; iterator < global_geometry_list_master->num_elements; iterator++) {
    if (global_geometry_list_master->elements[iterator].component_index < INDEX_CURRENT_COMP
        && global_geometry_list_master->elements[iterator].activation_counter > 0) {
      global_geometry_list_master->elements[iterator].active = 1;
      global_geometry_list_master->elements[iterator].activation_counter--;
      number_of_volumes++;
      if (global_geometry_list_master->elements[iterator].Volume->geometry.is_mask_volume == 1)
        number_of_masks++;
      if (global_geometry_list_master->elements[iterator].Volume->geometry.is_masked_volume == 1)
        number_of_masked_volumes++;
    } else
      global_geometry_list_master->elements[iterator].active = 0;
  }

  // Allocation of global lists
  geometry_component_index_list.num_elements = number_of_volumes;
  geometry_component_index_list.elements = malloc (geometry_component_index_list.num_elements * sizeof (int));
  mask_volume_index_list.num_elements = number_of_masks;
  if (number_of_masks > 0)
    mask_volume_index_list.elements = malloc (number_of_masks * sizeof (int));
  mask_status_list.num_elements = number_of_masks;
  if (number_of_masks > 0)
    mask_status_list.elements = malloc (number_of_masks * sizeof (int));
  current_mask_intersect_list_status.num_elements = number_of_masked_volumes;
  if (number_of_masked_volumes > 0)
    current_mask_intersect_list_status.elements = malloc (number_of_masked_volumes * sizeof (int));

  // Make a list of component index from each volume index
  volume_index = 0;
  for (iterator = 0; iterator < global_geometry_list_master->num_elements; iterator++) {
    if (global_geometry_list_master->elements[iterator].active == 1)
      geometry_component_index_list.elements[++volume_index] = global_geometry_list_master->elements[iterator].component_index;
  }
  geometry_component_index_list.elements[0] = 0; // Volume 0 is never set in the above code, but should never be used.

  // The input for this component is done through a series of input components
  // All information needed is stored in global lists, some of which is printed here for an overview to the user.
  MPI_MASTER ( // MPI_MASTER ensures just one thread output this information to the user
      if (verbal == 1) {
        printf ("---------------------------------------------------------------------\n");
        printf ("global_process_list_master->num_elements: %d\n", global_process_list_master->num_elements);
        for (iterator = 0; iterator < global_process_list_master->num_elements; iterator++) {
          printf ("name of process [%d]: %s \n", iterator, global_process_list_master->elements[iterator].name);
          printf ("component index [%d]: %d \n", iterator, global_process_list_master->elements[iterator].component_index);
        }

        printf ("---------------------------------------------------------------------\n");
        printf ("global_material_list_master->num_elements: %d\n", global_material_list_master->num_elements);
        for (iterator = 0; iterator < global_material_list_master->num_elements; iterator++) {
          printf ("name of material    [%d]: %s \n", iterator, global_material_list_master->elements[iterator].name);
          printf ("component index     [%d]: %d \n", iterator, global_material_list_master->elements[iterator].component_index);
          printf ("my_absoprtion       [%d]: %f \n", iterator, global_material_list_master->elements[iterator].physics->my_a);
          printf ("number of processes [%d]: %d \n", iterator, global_material_list_master->elements[iterator].physics->number_of_processes);
        }

        printf ("---------------------------------------------------------------------\n");
        printf ("global_geometry_list_master->num_elements: %d\n", global_material_list_master->num_elements);
        for (iterator = 0; iterator < global_geometry_list_master->num_elements; iterator++) {
          if (global_geometry_list_master->elements[iterator].active == 1) {
            printf ("\n");
            printf ("name of geometry    [%d]: %s \n", iterator, global_geometry_list_master->elements[iterator].name);
            printf ("component index     [%d]: %d \n", iterator, global_geometry_list_master->elements[iterator].component_index);
            printf ("Volume.name         [%d]: %s \n", iterator, global_geometry_list_master->elements[iterator].Volume->name);
            if (global_geometry_list_master->elements[iterator].Volume->geometry.is_mask_volume == 0) {
              printf ("Volume.p_physics.is_vacuum           [%d]: %d \n", iterator, global_geometry_list_master->elements[iterator].Volume->p_physics->is_vacuum);
              printf ("Volume.p_physics.my_absorption       [%d]: %f \n", iterator, global_geometry_list_master->elements[iterator].Volume->p_physics->my_a);
              printf ("Volume.p_physics.number of processes [%d]: %d \n", iterator,
                      global_geometry_list_master->elements[iterator].Volume->p_physics->number_of_processes);
            }
            printf ("Volume.geometry.shape                [%d]: %s \n", iterator, global_geometry_list_master->elements[iterator].Volume->geometry.shape);
            printf ("Volume.geometry.center.x             [%d]: %f \n", iterator, global_geometry_list_master->elements[iterator].Volume->geometry.center.x);
            printf ("Volume.geometry.center.y             [%d]: %f \n", iterator, global_geometry_list_master->elements[iterator].Volume->geometry.center.y);
            printf ("Volume.geometry.center.z             [%d]: %f \n", iterator, global_geometry_list_master->elements[iterator].Volume->geometry.center.z);
            printf ("Volume.geometry.rotation_matrix[0]           [%d]: [%f %f %f] \n", iterator,
                    global_geometry_list_master->elements[iterator].Volume->geometry.rotation_matrix[0][0],
                    global_geometry_list_master->elements[iterator].Volume->geometry.rotation_matrix[0][1],
                    global_geometry_list_master->elements[iterator].Volume->geometry.rotation_matrix[0][2]);
            printf ("Volume.geometry.rotation_matrix[1]           [%d]: [%f %f %f] \n", iterator,
                    global_geometry_list_master->elements[iterator].Volume->geometry.rotation_matrix[1][0],
                    global_geometry_list_master->elements[iterator].Volume->geometry.rotation_matrix[1][1],
                    global_geometry_list_master->elements[iterator].Volume->geometry.rotation_matrix[1][2]);
            printf ("Volume.geometry.rotation_matrix[2]           [%d]: [%f %f %f] \n", iterator,
                    global_geometry_list_master->elements[iterator].Volume->geometry.rotation_matrix[2][0],
                    global_geometry_list_master->elements[iterator].Volume->geometry.rotation_matrix[2][1],
                    global_geometry_list_master->elements[iterator].Volume->geometry.rotation_matrix[2][2]);
            if (strcmp (global_geometry_list_master->elements[iterator].Volume->geometry.shape, "cylinder") == 0) {
              printf ("Volume.geometry.geometry_parameters.cyl_radius [%d]: %f \n", iterator,
                      global_geometry_list_master->elements[iterator].Volume->geometry.geometry_parameters.p_cylinder_storage->cyl_radius);
              printf ("Volume.geometry.geometry_parameters.height [%d]: %f \n", iterator,
                      global_geometry_list_master->elements[iterator].Volume->geometry.geometry_parameters.p_cylinder_storage->height);
            }
            printf ("Volume.geometry.focus_data_array.elements[0].Aim             [%d]: [%f %f %f] \n", iterator,
                    global_geometry_list_master->elements[iterator].Volume->geometry.focus_data_array.elements[0].Aim.x,
                    global_geometry_list_master->elements[iterator].Volume->geometry.focus_data_array.elements[0].Aim.y,
                    global_geometry_list_master->elements[iterator].Volume->geometry.focus_data_array.elements[0].Aim.z);
          }
        }
        printf ("---------------------------------------------------------------------\n");
        printf ("number_of_volumes = %d\n", number_of_volumes);
        printf ("number_of_masks = %d\n", number_of_masks);
        printf ("number_of_masked_volumes = %d\n", number_of_masked_volumes);
      }

  ); // End MPI_MASTER

  // --- Initialization tasks independent of volume stucture -----------------------

  // Store a pointer to the conditional list and update the current index in that structure
  // If no tagging_conditionals were defined between this and the previous master, a dummy is allocated instead
  if (global_tagging_conditional_list_master->num_elements == global_tagging_conditional_list_master->current_index + 1) {
    tagging_conditional_list = &global_tagging_conditional_list_master->elements[global_tagging_conditional_list_master->current_index++].conditional_list;
    free_tagging_conditioanl_list = 0;
  } else {
    tagging_conditional_list = malloc (sizeof (struct conditional_list_struct));
    tagging_conditional_list->num_elements = 0;
    free_tagging_conditioanl_list = 1;
  }

  // Find the maximum logger extend index so that the correct memory allocation can be performed later
  // Here the loggers applied to all volumes are searched, later this result is compared to volume specific loggers and updated
  max_conditional_extend_index = -1;
  for (iterator = 0; iterator < global_all_volume_logger_list_master->num_elements; iterator++) {
    if (global_all_volume_logger_list_master->elements[iterator].logger->logger_extend_index > max_conditional_extend_index) {
      max_conditional_extend_index = global_all_volume_logger_list_master->elements[iterator].logger->logger_extend_index;
    }
  }

  // The absolute rotation of this component is saved for use in initialization
  rot_transpose (ROT_A_CURRENT_COMP, master_transposed_rotation_matrix);

  // Preceeding componnets can add coordinates and rotations to global_positions_to_transform and global_rotations_to_transform
  //  in order to have these transformed into the coordinate system of the next master compoent in the instrument file.
  // Here these transformations are performed, and the lists are cleared so no transformed information is further altered by
  //  next master components.

  // Position transformation
  for (iterator = 0; iterator < global_positions_to_transform_list_master->num_elements; iterator++) {
    non_rotated_position = coords_sub (*(global_positions_to_transform_list_master->positions[iterator]), POS_A_CURRENT_COMP);
    *(global_positions_to_transform_list_master->positions[iterator]) = rot_apply (ROT_A_CURRENT_COMP, non_rotated_position);
  }
  if (global_positions_to_transform_list_master->num_elements > 0) {
    global_positions_to_transform_list_master->num_elements = 0;
    free (global_positions_to_transform_list_master->positions);
  }
  // Rotation transformation
  for (iterator = 0; iterator < global_rotations_to_transform_list_master->num_elements; iterator++) {
    // print_rotation(*(global_rotations_to_transform_list_master->rotations[iterator]),"rotation matrix to be updated");
    rot_mul (master_transposed_rotation_matrix, *(global_rotations_to_transform_list_master->rotations[iterator]), temp_rotation_matrix);
    rot_copy (*(global_rotations_to_transform_list_master->rotations[iterator]), temp_rotation_matrix);
  }
  if (global_rotations_to_transform_list_master->num_elements > 0) {
    global_rotations_to_transform_list_master->num_elements = 0;
    free (global_rotations_to_transform_list_master->rotations);
  }

  // --- Definition of volumes and loading of appropriate data  -----------------------

  // The information stored in global lists is to be stored in one array of structures that is allocated here
  Volumes = malloc (number_of_volumes * sizeof (struct Volume_struct*));
  scattered_flag = malloc (number_of_volumes * sizeof (int));
  scattered_flag_VP = (int**)malloc (number_of_volumes * sizeof (int*));
  number_of_processes_array = malloc (number_of_volumes * sizeof (int));

  // The mcdisplay functions need access to the other geomtries, but can not use the Volumes struct because of order of definition.
  // A separate list of pointers to the geometry structures is thus allocated
  Geometries = malloc (number_of_volumes * sizeof (struct geometry_struct*));

  // When activation counter is used to have several copies of one volume, it can become necessary to have soft copies of volumes
  // Not all of these will necessarily be allocated or used.
  Volume_copies = malloc (number_of_volumes * sizeof (struct Volume_struct*));
  Volume_copies_allocated.num_elements = 0;

  // The central structure is called a "Volume", it describes a region in space with certain scattering processes and absorption cross section

  // ---  Volume 0 ------------------------------------------------------------------------------------------------
  // Volume 0 is the vacuum surrounding the experiment (infinite, everywhere) and its properties are hardcoded here
  Volumes[0] = malloc (sizeof (struct Volume_struct));
  strcpy (Volumes[0]->name, "Surrounding vacuum");
  // Assign geometry

  // This information is meaningless for volume 0, and is never be acsessed in the logic.
  Volumes[0]->geometry.priority_value = 0.0;
  Volumes[0]->geometry.center.x = 0;
  Volumes[0]->geometry.center.y = 0;
  Volumes[0]->geometry.center.z = 0;
  strcpy (Volumes[0]->geometry.shape, "vacuum");
  Volumes[0]->geometry.eShape = surroundings;
  Volumes[0]->geometry.within_function = &r_within_surroundings; // Always returns 1
  // No physics struct allocated
  Volumes[0]->p_physics = NULL;
  number_of_processes_array[volume_index] = 0;

  // These are never used for volume 0, but by setting the length to 0 it is automatically skipped in many forloops without the need for an if statement
  Volumes[0]->geometry.children.num_elements = 0;
  Volumes[0]->geometry.direct_children.num_elements = 0;
  Volumes[0]->geometry.destinations_list.num_elements = 0;
  Volumes[0]->geometry.reduced_destinations_list.num_elements = 0;

  Volumes[0]->geometry.is_exit_volume = 0;
  Volumes[0]->geometry.masked_by_list.num_elements = 0;
  Volumes[0]->geometry.mask_list.num_elements = 0;
  Volumes[0]->geometry.masked_by_mask_index_list.num_elements = 0;
  Volumes[0]->geometry.mask_mode = 0;
  Volumes[0]->geometry.is_mask_volume = 0;
  Volumes[0]->geometry.is_masked_volume = 0;

  // A pointer to the geometry structure
  Geometries[0] = &Volumes[0]->geometry;

  // Logging initialization
  Volumes[0]->loggers.num_elements = 0;
  Volumes[0]->abs_loggers.num_elements = 0;

  // --- Loop over user defined volumes ------------------------------------------------------------------------
  // Here the user defined volumes are loaded into the volume structure that is used in the ray-tracing
  //  algorithm. Not all user defined volumes are used, some could be used by a previous master, some
  //  could be used by the previous master, this one, and perhaps more. This is controlled by the
  //  activation counter input for geometries, and is here condensed to the active variable.
  // Volumes that were used before

  max_number_of_processes = 0; // The maximum number of processes in a volume is assumed 0 and updated during the following loop

  volume_index = 0;
  mask_index_main = 0;
  for (geometry_list_index = 0; geometry_list_index < global_geometry_list_master->num_elements; geometry_list_index++) {
    if (global_geometry_list_master->elements[geometry_list_index].active == 1) { // Only include the volume if it is active
      volume_index++;
      // Connect a volume for each of the geometry.comp instances in the McStas instrument files
      if (global_geometry_list_master->elements[geometry_list_index].activation_counter == 0) {
        // This is the last time this volume is used, use the hard copy from the geometry component
        Volumes[volume_index] = global_geometry_list_master->elements[geometry_list_index].Volume;
      } else {
        // Since this volume is still needed more than this once, we need to make a shallow copy and use instead

        Volume_copies[volume_index] = malloc (sizeof (struct Volume_struct));
        *(Volume_copies[volume_index]) = *global_geometry_list_master->elements[geometry_list_index].Volume; // Makes shallow copy
        Volumes[volume_index] = Volume_copies[volume_index];
        add_element_to_int_list (&Volume_copies_allocated, volume_index); // Keep track of dynamically allocated volumes in order to free them in FINALLY.

        // The geometry storage needs a shallow copy as well (hard copy not necessary for any current geometries), may need changes in future
        // A simple copy_geometry_parameters function is added to the geometry in each geometry component
        Volumes[volume_index]->geometry.geometry_parameters = Volumes[volume_index]->geometry.copy_geometry_parameters (
            &global_geometry_list_master->elements[geometry_list_index].Volume->geometry.geometry_parameters);

        // Copy focusing data too, it will be modified based on this master components rotation, so should not be reused
        copy_focus_data_array (&global_geometry_list_master->elements[geometry_list_index].Volume->geometry.focus_data_array,
                               &Volumes[volume_index]->geometry.focus_data_array);
      }

      // This section identifies the different non isotropic processes in the current volume and give them appropriate transformation matrices
      // Identify the number of non isotropic processes in a material (this code can be safely executed for the same material many times)
      // A setting of -1 means no transformation necessary, other settings are assigned a unique identifier instead
      non_isotropic_found = 0;
      for (iterator = 0; iterator < Volumes[volume_index]->p_physics->number_of_processes; iterator++) {
        if (Volumes[volume_index]->p_physics->p_scattering_array[iterator].non_isotropic_rot_index != -1) {
          Volumes[volume_index]->p_physics->p_scattering_array[iterator].non_isotropic_rot_index = non_isotropic_found;
          non_isotropic_found++;
        }
      }

      // Update focusing absolute_rotation before running through processes, then this will be the baseline for nonisotropic processes
      // rot_copy(Volumes[volume_index]->geometry.focus_data_array.elements[0].absolute_rotation, ROT_A_CURRENT_COMP);

      Volumes[volume_index]->geometry.focus_array_indices.num_elements = 0;
      // For the non_isotropic volumes found, rotation matrices need to be allocated and calculated
      if (non_isotropic_found > 0) {
        // Allocation of rotation and transpose rotation matrices
        if (Volumes[volume_index]->geometry.process_rot_allocated == 0) {
          Volumes[volume_index]->geometry.process_rot_matrix_array = malloc (non_isotropic_found * sizeof (Rotation));
          Volumes[volume_index]->geometry.transpose_process_rot_matrix_array = malloc (non_isotropic_found * sizeof (Rotation));
          Volumes[volume_index]->geometry.process_rot_allocated = 1;
        }

        // Calculation of the appropriate rotation matrices for transformation between Union_master and the process in a given volume.
        non_isotropic_found = 0;
        for (iterator = 0; iterator < Volumes[volume_index]->p_physics->number_of_processes; iterator++) {
          if (Volumes[volume_index]->p_physics->p_scattering_array[iterator].non_isotropic_rot_index != -1) {
            // Transformation for each process / geometry combination

            // The focus vector is given in relation to the geometry and needs to be transformed to the process
            // Work on temporary_focus_data_element which is added to the focus_data_array_at the end
            temporary_focus_data = Volumes[volume_index]->geometry.focus_data_array.elements[0];

            // Correct for process rotation
            // Aim
            temporary_focus_data.Aim = rot_apply (Volumes[volume_index]->p_physics->p_scattering_array[iterator].rotation_matrix, temporary_focus_data.Aim);

            // Absolute rotation of focus_data needs to updated using the rotation matrix from this process (before it is combined with the rotation matrix of the
            // geometry)
            rot_mul (Volumes[volume_index]->p_physics->p_scattering_array[iterator].rotation_matrix, temporary_focus_data.absolute_rotation,
                     temp_rotation_matrix);
            rot_copy (temporary_focus_data.absolute_rotation, temp_rotation_matrix);

            // Add element to focus_array_indices
            // focus_array_indices refers to the correct element in focus_data_array for this volume/process combination
            // focus_data_array[0] is the isotropic version in all cases, so the first non_isotropic goes to focus_data_array[1]
            //  and so forth. When a process is isotropic, this array is appended with a zero.
            // The focus_array_indices maps process numbers to the correct focus_data_array index.
            add_element_to_int_list (&Volumes[volume_index]->geometry.focus_array_indices, non_isotropic_found + 1);

            // Add the new focus_data element to this volumes focus_data_array.
            add_element_to_focus_data_array (&Volumes[volume_index]->geometry.focus_data_array, temporary_focus_data);

            // Quick error check to see the length is correct which indirectly confirms the indices are correct
            if (Volumes[volume_index]->geometry.focus_data_array.num_elements != non_isotropic_found + 2) {
              printf ("ERROR, focus_data_array length for volume %s inconsistent with number of non isotropic processes found!\n", Volumes[volume_index]->name);
              exit (1);
            }

            // Create rotation matrix for this specific volume / process combination to transform from master coordinate system to the non-isotropics process
            // coordinate system This is done by multipling the transpose master component roration matrix, the volume rotation, and then the process rotation
            // matrix onto the velocity / wavevector
            rot_mul (Volumes[volume_index]->geometry.rotation_matrix, master_transposed_rotation_matrix, temp_rotation_matrix);
            rot_mul (Volumes[volume_index]->p_physics->p_scattering_array[iterator].rotation_matrix, temp_rotation_matrix,
                     Volumes[volume_index]->geometry.process_rot_matrix_array[non_isotropic_found]);

            // Need to transpose as well to transform back to the master coordinate system
            rot_transpose (Volumes[volume_index]->geometry.process_rot_matrix_array[non_isotropic_found],
                           Volumes[volume_index]->geometry.transpose_process_rot_matrix_array[non_isotropic_found]);

            // Debug print
            // print_rotation(Volumes[volume_index]->geometry.process_rot_matrix_array[non_isotropic_found],"Process rotation matrix");
            // print_rotation(Volumes[volume_index]->geometry.transpose_process_rot_matrix_array[non_isotropic_found],"Transpose process rotation matrix");

            non_isotropic_found++;
          } else {
            // This process can use the standard isotropic focus_data_array which is indexed zero.
            add_element_to_int_list (&Volumes[volume_index]->geometry.focus_array_indices, 0);
          }
        }
      } else {
        // No non isotropic volumes found, focus_array_indices should just be a list of 0's of same length as the number of processes.
        // In this way all processes use the isotropic focus_data structure
        Volumes[volume_index]->geometry.focus_array_indices.elements = malloc (Volumes[volume_index]->p_physics->number_of_processes * sizeof (int));
        for (iterator = 0; iterator < Volumes[volume_index]->p_physics->number_of_processes; iterator++)
          Volumes[volume_index]->geometry.focus_array_indices.elements[iterator] = 0;
      }

      rot_copy (Volumes[volume_index]->geometry.focus_data_array.elements[0].absolute_rotation, ROT_A_CURRENT_COMP);

      // This component works in its local coordinate system, and thus all information from the input components should be transformed to its coordinate system.
      // All the input components saved their absolute rotation/position into their Volume structure, and the absolute rotation of the current component is known.
      // The next section finds the relative rotation and translation of all the volumes and the master component.

      // Transform the rotation matrices for each volume
      rot_mul (ROT_A_CURRENT_COMP, Volumes[volume_index]->geometry.transpose_rotation_matrix, temp_rotation_matrix);
      // Copy the result back to the volumes structure
      rot_copy (Volumes[volume_index]->geometry.rotation_matrix, temp_rotation_matrix);
      // Now update the transpose as well
      rot_transpose (Volumes[volume_index]->geometry.rotation_matrix, temp_rotation_matrix);
      rot_copy (Volumes[volume_index]->geometry.transpose_rotation_matrix, temp_rotation_matrix);

      // Transform the position for each volume
      non_rotated_position.x = Volumes[volume_index]->geometry.center.x - POS_A_CURRENT_COMP.x;
      non_rotated_position.y = Volumes[volume_index]->geometry.center.y - POS_A_CURRENT_COMP.y;
      non_rotated_position.z = Volumes[volume_index]->geometry.center.z - POS_A_CURRENT_COMP.z;

      rot_transpose (ROT_A_CURRENT_COMP, temp_rotation_matrix); // REVIEW LINE
      rotated_position = rot_apply (ROT_A_CURRENT_COMP, non_rotated_position);

      Volumes[volume_index]->geometry.center.x = rotated_position.x;
      Volumes[volume_index]->geometry.center.y = rotated_position.y;
      Volumes[volume_index]->geometry.center.z = rotated_position.z;

      // Use same rotation on the aim vector of the isotropic focus_data element
      Volumes[volume_index]->geometry.focus_data_array.elements[0].Aim
          = rot_apply (Volumes[volume_index]->geometry.rotation_matrix, Volumes[volume_index]->geometry.focus_data_array.elements[0].Aim);

      // To allocate enough memory to hold information on all processes, the maximum of these is updated if this volume has more
      if (Volumes[volume_index]->p_physics->number_of_processes > max_number_of_processes)
        max_number_of_processes = Volumes[volume_index]->p_physics->number_of_processes;

      // Allocate memory to scattered_flag_VP (holds statistics for scatterings in each process of the volume)
      scattered_flag_VP[volume_index] = malloc (Volumes[volume_index]->p_physics->number_of_processes * sizeof (int));
      number_of_processes_array[volume_index] = Volumes[volume_index]->p_physics->number_of_processes;

      // Normalizing and error checking process interact fraction
      number_of_process_interacts_set = 0;
      total_process_interact = 0;
      for (process_index = 0; process_index < Volumes[volume_index]->p_physics->number_of_processes; process_index++) {
        if (Volumes[volume_index]->p_physics->p_scattering_array[process_index].process_p_interact != -1) {
          number_of_process_interacts_set++;
          total_process_interact += Volumes[volume_index]->p_physics->p_scattering_array[process_index].process_p_interact;
        } else {
          index_of_lacking_process = process_index;
        }
      }

      if (number_of_process_interacts_set == 0)
        Volumes[volume_index]->p_physics->interact_control = 0;
      else
        Volumes[volume_index]->p_physics->interact_control = 1;

      // If all are set, check if they need renormalization so that the sum is one.
      if (number_of_process_interacts_set == Volumes[volume_index]->p_physics->number_of_processes) {
        if (total_process_interact > 1.001 || total_process_interact < 0.999) {
          for (process_index = 0; process_index < Volumes[volume_index]->p_physics->number_of_processes; process_index++) {
            Volumes[volume_index]->p_physics->p_scattering_array[process_index].process_p_interact
                = Volumes[volume_index]->p_physics->p_scattering_array[process_index].process_p_interact / total_process_interact;
          }
        }
      } else if (number_of_process_interacts_set != 0) {
        if (number_of_process_interacts_set == Volumes[volume_index]->p_physics->number_of_processes - 1) { // If all but one is set, it is an easy fix
          Volumes[volume_index]->p_physics->p_scattering_array[index_of_lacking_process].process_p_interact = 1 - total_process_interact;
          if (total_process_interact >= 1) {
            printf ("ERROR, material %s has a total interact_fraction above 1 and a process without an interact_fraction. Either set all so they can be "
                    "renormalized, or have a sum below 1, so that the last can have 1 - sum.\n",
                    Volumes[volume_index]->p_physics->name);
            exit (1);
          }
        } else {
          printf ("ERROR, material %s needs to have all, all minus one or none of its processes with an interact_fraction \n",
                  Volumes[volume_index]->p_physics->name);
          exit (1);
        }
      }

      // Some initialization can only happen after the rotation matrix relative to the master is known
      // Such initialization is placed in the geometry component, and executed here through a function pointer
      Volumes[volume_index]->geometry.initialize_from_main_function (&Volumes[volume_index]->geometry);

      // Add pointer to geometry to Geometries
      Geometries[volume_index] = &Volumes[volume_index]->geometry;

      // Initialize mask intersect list
      Volumes[volume_index]->geometry.mask_intersect_list.num_elements = 0;

      // Here the mask_list and masked_by_list for the volume is updated from component index values to volume indexes
      for (iterator = 0; iterator < Volumes[volume_index]->geometry.mask_list.num_elements; iterator++)
        Volumes[volume_index]->geometry.mask_list.elements[iterator]
            = find_on_int_list (geometry_component_index_list, Volumes[volume_index]->geometry.mask_list.elements[iterator]);

      for (iterator = 0; iterator < Volumes[volume_index]->geometry.masked_by_list.num_elements; iterator++)
        Volumes[volume_index]->geometry.masked_by_list.elements[iterator]
            = find_on_int_list (geometry_component_index_list, Volumes[volume_index]->geometry.masked_by_list.elements[iterator]);

      // If the volume is a mask, its volume number is added to the mask_volume_index list so volume index can be converted to mask_index.
      if (Volumes[volume_index]->geometry.is_mask_volume == 1)
        Volumes[volume_index]->geometry.mask_index = mask_index_main;
      if (Volumes[volume_index]->geometry.is_mask_volume == 1)
        mask_volume_index_list.elements[mask_index_main++] = volume_index;

      // Check all loggers assosiated with this volume and update the max_conditional_extend_index if necessary
      for (iterator = 0; iterator < Volumes[volume_index]->loggers.num_elements; iterator++) {
        for (process_index = 0; process_index < Volumes[volume_index]->loggers.p_logger_volume[iterator].num_elements; process_index++) {
          if (Volumes[volume_index]->loggers.p_logger_volume[iterator].p_logger_process[process_index] != NULL) {
            if (Volumes[volume_index]->loggers.p_logger_volume[iterator].p_logger_process[process_index]->logger_extend_index > max_conditional_extend_index)
              max_conditional_extend_index = Volumes[volume_index]->loggers.p_logger_volume[iterator].p_logger_process[process_index]->logger_extend_index;
          }
        }
      }

      // Find longest surface length
      int surfaces_in_this_stack;
      for (iterator = 0; iterator < Volumes[volume_index]->geometry.number_of_faces; iterator++) {
        surfaces_in_this_stack = Volumes[volume_index]->geometry.surface_stack_for_each_face[iterator]->number_of_surfaces;
        if (surfaces_in_this_stack > longest_surface_stack) {
          longest_surface_stack = surfaces_in_this_stack;
        }
      }
    }
  } // Initialization for each volume done

  // ------- Initialization of ray-tracing algorithm ------------------------------------

  my_trace = malloc (max_number_of_processes * sizeof (double));
  my_trace_fraction_control = malloc (max_number_of_processes * sizeof (double));

  // All geometries can have 2 intersections currently, when this changes the maximum number of solutions need to be reported to the Union_master.
  number_of_solutions = &number_of_solutions_static;
  component_error_msg = 0;

  // Pre allocated memory for destination list search
  pre_allocated1 = malloc (number_of_volumes * sizeof (int));
  pre_allocated2 = malloc (number_of_volumes * sizeof (int));
  pre_allocated3 = malloc (number_of_volumes * sizeof (int));

  // Pre allocated memory to fit all surfaces in an interface
  interface_stack.number_of_surfaces = 2 * longest_surface_stack;
  interface_stack.p_surface_array = malloc (interface_stack.number_of_surfaces * sizeof (struct surface_process_struct*));

  // Allocate memory for logger_conditional_extend_array used in the extend section of the master component, if it is needed.
  if (max_conditional_extend_index > -1) {
    logger_conditional_extend_array = malloc ((max_conditional_extend_index + 1) * sizeof (int));
  }

  // In this function different lists of volume indecies are generated. They are the key to the speed of the component and central for the logic.
  // They use simple set algebra to generate these lists for each volume:
  // Children list for volume n: Indicies of volumes that are entirely within the set described by volume n
  // Overlap list for volume n: Indicies of volume that contains some of the set described by volume n (excluding volume n)
  // Intersect check list for volume n: Indicies of volumes to check for intersection if a ray originates from volume n (is generated from the children and
  // overlap lists) Parents list for volume n: Indicies of volumes that contain the entire set of volume n Grandparents lists for volume n: Indicies of volumes
  // that contain the entire set of at least one parent of volume n Destination list for volume n: Indicies of volumes that could be the destination volume when a
  // ray leaves volume n The overlap, parents and grandparents lists are local variables in the function, and not in the main scope.

  generate_lists (Volumes, &starting_lists, number_of_volumes, list_verbal);

  // Generate "safe starting list", which contains all volumes that the ray may enter from other components
  // These are all volumes without scattering or absorption

  // Updating mask lists from volume index to global_mask_indices
  // Filling out the masked_by list that uses mask indices
  for (volume_index = 0; volume_index < number_of_volumes; volume_index++) {
    Volumes[volume_index]->geometry.masked_by_mask_index_list.num_elements = Volumes[volume_index]->geometry.masked_by_list.num_elements;
    Volumes[volume_index]->geometry.masked_by_mask_index_list.elements
        = malloc (Volumes[volume_index]->geometry.masked_by_mask_index_list.num_elements * sizeof (int));
    for (iterator = 0; iterator < Volumes[volume_index]->geometry.masked_by_list.num_elements; iterator++)
      Volumes[volume_index]->geometry.masked_by_mask_index_list.elements[iterator]
          = find_on_int_list (mask_volume_index_list, Volumes[volume_index]->geometry.masked_by_list.elements[iterator]);
  }

  int volume_index_main;

  // Checking for equal priorities in order to alert the user to a potential input error
  for (volume_index_main = 0; volume_index_main < number_of_volumes; volume_index_main++) {
    for (volume_index = 0; volume_index < number_of_volumes; volume_index++)
      if (Volumes[volume_index_main]->geometry.priority_value == Volumes[volume_index]->geometry.priority_value && volume_index_main != volume_index) {
        if (Volumes[volume_index_main]->geometry.is_mask_volume == 0 && Volumes[volume_index]->geometry.is_mask_volume == 0) {
          // Priority of masks do not matter
          printf ("ERROR in Union_master with name %s. The volumes named %s and %s have the same priority. Change the priorities so the one present in case of "
                  "overlap has highest priority.\n",
                  NAME_CURRENT_COMP, Volumes[volume_index_main]->name, Volumes[volume_index]->name);
          exit (1);
        }
      }
  }

  // Printing the generated lists for all volumes.
  MPI_MASTER (if (verbal) printf ("\n ---- Overview of the lists generated for each volume ---- \n");

              if (verbal) printf ("List overview for surrounding vacuum\n");
              for (volume_index_main = 0; volume_index_main < number_of_volumes; volume_index_main++) {
                if (verbal) {
                  if (volume_index_main != 0) {
                    if (Volumes[volume_index_main]->geometry.is_mask_volume == 0 || Volumes[volume_index_main]->geometry.is_masked_volume == 0
                        || Volumes[volume_index_main]->geometry.is_exit_volume == 0) {
                      printf ("List overview for %s with %s shape made of %s\n", Volumes[volume_index_main]->name, Volumes[volume_index_main]->geometry.shape,
                              Volumes[volume_index_main]->p_physics->name);
                    } else {
                      printf ("List overview for %s with shape %s\n", Volumes[volume_index_main]->name, Volumes[volume_index_main]->geometry.shape);
                    }
                  }
                }

                if (verbal)
                  sprintf (string_output, "Children for Volume                  %d", volume_index_main);
                if (verbal)
                  print_1d_int_list (Volumes[volume_index_main]->geometry.children, string_output);

                if (verbal)
                  sprintf (string_output, "Direct_children for Volume           %d", volume_index_main);
                if (verbal)
                  print_1d_int_list (Volumes[volume_index_main]->geometry.direct_children, string_output);

                if (verbal)
                  sprintf (string_output, "Intersect_check_list for Volume      %d", volume_index_main);
                if (verbal)
                  print_1d_int_list (Volumes[volume_index_main]->geometry.intersect_check_list, string_output);

                if (verbal)
                  sprintf (string_output, "Mask_intersect_list for Volume       %d", volume_index_main);
                if (verbal)
                  print_1d_int_list (Volumes[volume_index_main]->geometry.mask_intersect_list, string_output);

                if (verbal)
                  sprintf (string_output, "Destinations_list for Volume         %d", volume_index_main);
                if (verbal)
                  print_1d_int_list (Volumes[volume_index_main]->geometry.destinations_list, string_output);

                // if (verbal) sprintf(string_output,"Destinations_logic_list for Volume   %d",volume_index_main);
                // if (verbal) print_1d_int_list(Volumes[volume_index_main]->geometry.destinations_logic_list,string_output);

                if (verbal)
                  sprintf (string_output, "Reduced_destinations_list for Volume %d", volume_index_main);
                if (verbal)
                  print_1d_int_list (Volumes[volume_index_main]->geometry.reduced_destinations_list, string_output);

                if (verbal)
                  sprintf (string_output, "Next_volume_list for Volume          %d", volume_index_main);
                if (verbal)
                  print_1d_int_list (Volumes[volume_index_main]->geometry.next_volume_list, string_output);

                if (verbal) {
                  if (volume_index_main != 0)
                    printf ("      Is_vacuum for Volume                 %d = %d\n", volume_index_main, Volumes[volume_index_main]->p_physics->is_vacuum);
                }
                if (verbal) {
                  if (volume_index_main != 0)
                    printf ("      is_mask_volume for Volume            %d = %d\n", volume_index_main, Volumes[volume_index_main]->geometry.is_mask_volume);
                }
                if (verbal) {
                  if (volume_index_main != 0)
                    printf ("      is_masked_volume for Volume          %d = %d\n", volume_index_main, Volumes[volume_index_main]->geometry.is_masked_volume);
                }
                if (verbal) {
                  if (volume_index_main != 0)
                    printf ("      is_exit_volume for Volume            %d = %d\n", volume_index_main, Volumes[volume_index_main]->geometry.is_exit_volume);
                }

                if (verbal)
                  sprintf (string_output, "mask_list for Volume                 %d", volume_index_main);
                if (verbal)
                  print_1d_int_list (Volumes[volume_index_main]->geometry.mask_list, string_output);

                if (verbal)
                  sprintf (string_output, "masked_by_list for Volume            %d", volume_index_main);
                if (verbal)
                  print_1d_int_list (Volumes[volume_index_main]->geometry.masked_by_list, string_output);

                if (verbal)
                  sprintf (string_output, "masked_by_mask_index_list for Volume %d", volume_index_main);
                if (verbal)
                  print_1d_int_list (Volumes[volume_index_main]->geometry.masked_by_mask_index_list, string_output);

                if (verbal)
                  printf ("      mask_mode for Volume                 %d = %d\n", volume_index_main, Volumes[volume_index_main]->geometry.mask_mode);
                if (verbal)
                  printf ("\n");
              }) // End of MPI_MASTER

  // Initializing intersection_time_table
  // The intersection time table contains all information on intersection times for the current position/direction, and is cleared everytime a ray changes
  // direction. Not all entries needs to be calculated, so there is a variable that keeps track of which intersection times have been calculated in order to avoid
  // redoing that. When the intersections times are calculated for a volume, all future intersections are kept in the time table. Thus the memory allocation have
  // to take into account how many intersections there can be with each volume, but it is currently set to 2, but can easily be changed. This may need to be
  // reported by individual geometry components in the future.

  intersection_time_table.num_volumes = number_of_volumes;

  intersection_time_table.n_elements = (int*)malloc (intersection_time_table.num_volumes * sizeof (int));
  intersection_time_table.calculated = (int*)malloc (intersection_time_table.num_volumes * sizeof (int));
  intersection_time_table.intersection_times = (double**)malloc (intersection_time_table.num_volumes * sizeof (double*));
  intersection_time_table.normal_vector_x = (double**)malloc (intersection_time_table.num_volumes * sizeof (double*));
  intersection_time_table.normal_vector_y = (double**)malloc (intersection_time_table.num_volumes * sizeof (double*));
  intersection_time_table.normal_vector_z = (double**)malloc (intersection_time_table.num_volumes * sizeof (double*));
  intersection_time_table.surface_index = (int**)malloc (intersection_time_table.num_volumes * sizeof (int*));

  for (iterator = 0; iterator < intersection_time_table.num_volumes; iterator++) {
    if (strcmp (Volumes[iterator]->geometry.shape, "mesh") == 0) {
      intersection_time_table.n_elements[iterator] = (int)100; // Meshes can have any number of intersections, here we allocate room for 100
    } else {
      intersection_time_table.n_elements[iterator] = (int)2; // number of intersection for all other geometries
    }
    if (iterator == 0)
      intersection_time_table.n_elements[iterator] = (int)0; // number of intersection solutions
    intersection_time_table.calculated[iterator] = (int)0;   // Initializing calculated logic

    if (iterator == 0) {
      intersection_time_table.intersection_times[0] = NULL;
    } else {
      // printf("allocating memory for volume %d \n", iterator);
      intersection_time_table.intersection_times[iterator] = (double*)malloc (intersection_time_table.n_elements[iterator] * sizeof (double));
      intersection_time_table.normal_vector_x[iterator] = (double*)malloc (intersection_time_table.n_elements[iterator] * sizeof (double));
      intersection_time_table.normal_vector_y[iterator] = (double*)malloc (intersection_time_table.n_elements[iterator] * sizeof (double));
      intersection_time_table.normal_vector_z[iterator] = (double*)malloc (intersection_time_table.n_elements[iterator] * sizeof (double));
      intersection_time_table.surface_index[iterator] = (int*)malloc (intersection_time_table.n_elements[iterator] * sizeof (int));

      for (solutions = 0; solutions < intersection_time_table.n_elements[iterator]; solutions++) {
        intersection_time_table.intersection_times[iterator][solutions] = -1.0;
        intersection_time_table.normal_vector_x[iterator][solutions] = -1.0;
        intersection_time_table.normal_vector_y[iterator][solutions] = -1.0;
        intersection_time_table.normal_vector_z[iterator][solutions] = -1.0;
        intersection_time_table.surface_index[iterator][solutions] = -1;
      }
    }
  }

  // If enabled, the tagging system tracks all different histories sampled by the program.

  // Initialize the tagging tree
  // Allocate a list of host nodes with the same length as the number of volumes

  stop_creating_nodes = 0;
  stop_tagging_ray = 0;
  tagging_leaf_counter = 0;
  if (enable_tagging) {
    master_tagging_node_list.num_elements = number_of_volumes;
    master_tagging_node_list.elements = malloc (master_tagging_node_list.num_elements * sizeof (struct tagging_tree_node_struct*));

    // Initialize
    for (volume_index = 0; volume_index < number_of_volumes; volume_index++) {
      // if (verbal) printf("Allocating master tagging node for volume number %d \n",volume_index);
      master_tagging_node_list.elements[volume_index]
          = initialize_tagging_tree_node (master_tagging_node_list.elements[volume_index], NULL, Volumes[volume_index]);
      // if (verbal) printf("Allocated master tagging node for volume number %d \n",volume_index);
    }
  }

  // Initialize loggers
  loggers_with_data_array.allocated_elements = 0;
  loggers_with_data_array.used_elements = 0;

  abs_loggers_with_data_array.allocated_elements = 0;
  abs_loggers_with_data_array.used_elements = 0;

  // Initialize data structure needed for surfaces

  // Signal initialization complete
  MPI_MASTER (printf ("Union_master component %s initialized sucessfully\n", NAME_CURRENT_COMP);)
  #undef enable_refraction
  #undef enable_reflection
  #undef verbal
  #undef list_verbal
  #undef finally_verbal
  #undef allow_inside_start
  #undef enable_tagging
  #undef history_limit
  #undef enable_conditionals
  #undef inherit_number_of_scattering_events
  #undef weight_ratio_limit
  #undef init
  #undef global_positions_to_transform_list_master
  #undef global_rotations_to_transform_list_master
  #undef global_process_list_master
  #undef global_material_list_master
  #undef global_surface_list_master
  #undef global_geometry_list_master
  #undef global_all_volume_logger_list_master
  #undef global_specific_volumes_logger_list_master
  #undef global_all_volume_abs_logger_list_master
  #undef global_specific_volumes_abs_logger_list_master
  #undef global_tagging_conditional_list_master
  #undef global_master_list_master
  #undef starting_volume_warning
  #undef global_master_element
  #undef this_global_master_index
  #undef previous_master_index
  #undef geometry_list_index
  #undef intersection_time_table
  #undef Volumes
  #undef Geometries
  #undef Volume_copies
  #undef starting_lists
  #undef Volume_copies_allocated
  #undef r
  #undef r_start
  #undef v
  #undef error_msg
  #undef component_error_msg
  #undef string_output
  #undef number_of_volumes
  #undef volume_index
  #undef process_index
  #undef iterator
  #undef solutions
  #undef max_number_of_processes
  #undef limit
  #undef solution
  #undef min_solution
  #undef ignore_closest
  #undef ignore_surface_index
  #undef min_volume
  #undef time_found
  #undef intersection_time
  #undef min_intersection_time
  #undef process
  #undef process_start
  #undef my_trace
  #undef p_my_trace
  #undef my_trace_fraction_control
  #undef k
  #undef k_new
  #undef k_old
  #undef k_rotated
  #undef v_length
  #undef my_sum
  #undef my_sum_plus_abs
  #undef culmative_probability
  #undef mc_prop
  #undef time_to_scattering
  #undef length_to_scattering
  #undef length_to_boundary
  #undef time_to_boundery
  #undef selected_process
  #undef scattering_event
  #undef time_propagated_without_scattering
  #undef a_next_volume_found
  #undef next_volume
  #undef next_volume_priority
  #undef done
  #undef current_volume
  #undef previous_volume
  #undef ray_sucseeded
  #undef number_of_solutions
  #undef number_of_solutions_static
  #undef check
  #undef start
  #undef intersection_with_children
  #undef geometry_output
  #undef tree_next_volume
  #undef pre_allocated1
  #undef pre_allocated2
  #undef pre_allocated3
  #undef ray_position
  #undef ray_velocity
  #undef ray_velocity_rotated
  #undef ray_velocity_final
  #undef wavevector
  #undef wavevector_rotated
  #undef volume_0_found
  #undef scattered_flag
  #undef scattered_flag_VP
  #undef master_transposed_rotation_matrix
  #undef temp_rotation_matrix
  #undef temp_transpose_rotation_matrix
  #undef non_rotated_position
  #undef rotated_position
  #undef non_isotropic_found
  #undef master_tagging_node_list
  #undef current_tagging_node
  #undef tagging_leaf_counter
  #undef stop_tagging_ray
  #undef stop_creating_nodes
  #undef number_of_scattering_events
  #undef real_transmission_probability
  #undef mc_transmission_probability
  #undef number_of_process_interacts_set
  #undef index_of_lacking_process
  #undef total_process_interact
  #undef geometry_component_index_list
  #undef mask_volume_index_list
  #undef number_of_masks
  #undef number_of_masked_volumes
  #undef mask_status_list
  #undef current_mask_intersect_list_status
  #undef mask_index_main
  #undef mask_iterator
  #undef mask_start
  #undef mask_check
  #undef need_to_run_within_which_volume
  #undef number_of_processes_array
  #undef p_old
  #undef log_index
  #undef conditional_status
  #undef this_logger
  #undef this_abs_logger
  #undef tagging_conditional_list
  #undef logger_conditional_extend_array
  #undef abs_logger_conditional_extend_array
  #undef max_conditional_extend_index
  #undef tagging_conditional_extend
  #undef free_tagging_conditioanl_list
  #undef safety_distance
  #undef safety_distance2
  #undef temporary_focus_data
  #undef this_focus_data
  #undef focus_data_index
  #undef r_old
  #undef initial_weight
  #undef abs_weight_factor
  #undef time_old
  #undef absorption_index
  #undef abs_weight_factor_set
  #undef my_abs
  #undef absorption_event_data
  #undef abs_position
  #undef transformed_abs_position
  #undef t_abs_propagation
  #undef abs_distance
  #undef abs_max_length
  #undef longest_surface_stack
  #undef interface_stack
  return(_comp);
} /* class_Union_master_init */

_class_PSD_monitor *class_PSD_monitor_init(_class_PSD_monitor *_comp
) {
  #define nx (_comp->_parameters.nx)
  #define ny (_comp->_parameters.ny)
  #define filename (_comp->_parameters.filename)
  #define xmin (_comp->_parameters.xmin)
  #define xmax (_comp->_parameters.xmax)
  #define ymin (_comp->_parameters.ymin)
  #define ymax (_comp->_parameters.ymax)
  #define xwidth (_comp->_parameters.xwidth)
  #define yheight (_comp->_parameters.yheight)
  #define restore_neutron (_comp->_parameters.restore_neutron)
  #define nowritefile (_comp->_parameters.nowritefile)
  #define PSD_N (_comp->_parameters.PSD_N)
  #define PSD_p (_comp->_parameters.PSD_p)
  #define PSD_p2 (_comp->_parameters.PSD_p2)
  SIG_MESSAGE("[_detector_init] component detector=PSD_monitor() INITIALISE [PSD_monitor:0]");

  if (xwidth > 0) {
    xmax = xwidth / 2;
    xmin = -xmax;
  }
  if (yheight > 0) {
    ymax = yheight / 2;
    ymin = -ymax;
  }

  if ((xmin >= xmax) || (ymin >= ymax)) {
    printf ("PSD_monitor: %s: Null detection area !\n"
            "ERROR        (xwidth,yheight,xmin,xmax,ymin,ymax). Exiting",
            NAME_CURRENT_COMP);
    exit (0);
  }

  PSD_N = create_darr2d (nx, ny);
  PSD_p = create_darr2d (nx, ny);
  PSD_p2 = create_darr2d (nx, ny);

  // Use instance name for monitor output if no input was given
  if (!strcmp (filename, "\0"))
    sprintf (filename, "%s", NAME_CURRENT_COMP);
  #undef nx
  #undef ny
  #undef filename
  #undef xmin
  #undef xmax
  #undef ymin
  #undef ymax
  #undef xwidth
  #undef yheight
  #undef restore_neutron
  #undef nowritefile
  #undef PSD_N
  #undef PSD_p
  #undef PSD_p2
  return(_comp);
} /* class_PSD_monitor_init */



int init(void) { /* called by mccode_main for Conditional_test:INITIALISE */
  DEBUG_INSTR();
  // Initialise rng
  srandom(_hash(mcseed-1));

  /* code_main/parseoptions/readparams sets instrument parameters value */
  stracpy(instrument->_name, "Conditional_test", 256);

  _init_setpos(); /* type Union_init */
  _Vanadium_incoherent_setpos(); /* type Incoherent_process */
  _Vanadium_setpos(); /* type Union_make_material */
  _Al_incoherent_setpos(); /* type Incoherent_process */
  _Al_Powder_setpos(); /* type Powder_process */
  _Al_setpos(); /* type Union_make_material */
  _Cu_incoherent_setpos(); /* type Incoherent_process */
  _Cu_Powder_setpos(); /* type Powder_process */
  _Cu_setpos(); /* type Union_make_material */
  _a1_setpos(); /* type Progress_bar */
  _source_setpos(); /* type Source_div */
  _sample_pos_setpos(); /* type Arm */
  _cryostat_wall_setpos(); /* type Union_cylinder */
  _cryostat_wall_vacuum_setpos(); /* type Union_cylinder */
  _sample_sphere_setpos(); /* type Union_sphere */
  _sample_box_setpos(); /* type Union_box */
  _sample_cone_setpos(); /* type Union_cone */
  _test_logger_1D_setpos(); /* type Union_logger_1D */
  _test_logger_2D_space_zx_setpos(); /* type Union_logger_2D_space */
  _test_logger_2D_space_zy_setpos(); /* type Union_logger_2D_space */
  _scat_direction_setpos(); /* type Arm */
  _test_logger_2D_space_zx_con_time_setpos(); /* type Union_logger_2D_space */
  _test_conditional_standard_setpos(); /* type Union_conditional_standard */
  _test_logger_2D_space_zx_con_PSD_setpos(); /* type Union_logger_2D_space */
  _test_conditional_PSD_setpos(); /* type Union_conditional_PSD */
  _sample_setpos(); /* type Union_master */
  _detector_setpos(); /* type PSD_monitor */
  _detector_scat_setpos(); /* type PSD_monitor */
  _stop_setpos(); /* type Union_stop */

  /* call iteratively all components INITIALISE */
  class_Union_init_init(&_init_var);

  class_Incoherent_process_init(&_Vanadium_incoherent_var);

  class_Union_make_material_init(&_Vanadium_var);

  class_Incoherent_process_init(&_Al_incoherent_var);

  class_Powder_process_init(&_Al_Powder_var);

  class_Union_make_material_init(&_Al_var);

  class_Incoherent_process_init(&_Cu_incoherent_var);

  class_Powder_process_init(&_Cu_Powder_var);

  class_Union_make_material_init(&_Cu_var);

  class_Progress_bar_init(&_a1_var);

  class_Source_div_init(&_source_var);


  class_Union_cylinder_init(&_cryostat_wall_var);

  class_Union_cylinder_init(&_cryostat_wall_vacuum_var);

  class_Union_sphere_init(&_sample_sphere_var);

  class_Union_box_init(&_sample_box_var);

  class_Union_cone_init(&_sample_cone_var);

  class_Union_logger_1D_init(&_test_logger_1D_var);

  class_Union_logger_2D_space_init(&_test_logger_2D_space_zx_var);

  class_Union_logger_2D_space_init(&_test_logger_2D_space_zy_var);


  class_Union_logger_2D_space_init(&_test_logger_2D_space_zx_con_time_var);

  class_Union_conditional_standard_init(&_test_conditional_standard_var);

  class_Union_logger_2D_space_init(&_test_logger_2D_space_zx_con_PSD_var);

  class_Union_conditional_PSD_init(&_test_conditional_PSD_var);

  class_Union_master_init(&_sample_var);

  class_PSD_monitor_init(&_detector_var);

  class_PSD_monitor_init(&_detector_scat_var);


  if (mcdotrace) display();
  DEBUG_INSTR_END();

#ifdef OPENACC
#include <openacc.h>
#pragma acc update device(_init_var)
#pragma acc update device(_Vanadium_incoherent_var)
#pragma acc update device(_Vanadium_var)
#pragma acc update device(_Al_incoherent_var)
#pragma acc update device(_Al_Powder_var)
#pragma acc update device(_Al_var)
#pragma acc update device(_Cu_incoherent_var)
#pragma acc update device(_Cu_Powder_var)
#pragma acc update device(_Cu_var)
#pragma acc update device(_a1_var)
#pragma acc update device(_source_var)
#pragma acc update device(_sample_pos_var)
#pragma acc update device(_cryostat_wall_var)
#pragma acc update device(_cryostat_wall_vacuum_var)
#pragma acc update device(_sample_sphere_var)
#pragma acc update device(_sample_box_var)
#pragma acc update device(_sample_cone_var)
#pragma acc update device(_test_logger_1D_var)
#pragma acc update device(_test_logger_2D_space_zx_var)
#pragma acc update device(_test_logger_2D_space_zy_var)
#pragma acc update device(_scat_direction_var)
#pragma acc update device(_test_logger_2D_space_zx_con_time_var)
#pragma acc update device(_test_conditional_standard_var)
#pragma acc update device(_test_logger_2D_space_zx_con_PSD_var)
#pragma acc update device(_test_conditional_PSD_var)
#pragma acc update device(_sample_var)
#pragma acc update device(_detector_var)
#pragma acc update device(_detector_scat_var)
#pragma acc update device(_stop_var)
#pragma acc update device(_instrument_var)
#endif

  return(0);
} /* init */

/*******************************************************************************
* components TRACE
*******************************************************************************/

#define x (_particle->x)
#define y (_particle->y)
#define z (_particle->z)
#define vx (_particle->vx)
#define vy (_particle->vy)
#define vz (_particle->vz)
#define t (_particle->t)
#define sx (_particle->sx)
#define sy (_particle->sy)
#define sz (_particle->sz)
#define p (_particle->p)
#define mcgravitation (_particle->mcgravitation)
#define mcMagnet (_particle->mcMagnet)
#define allow_backprop (_particle->allow_backprop)
#define _mctmp_a (_particle->_mctmp_a)
#define _mctmp_b (_particle->_mctmp_b)
#define _mctmp_c (_particle->_mctmp_c)
/* if on GPU, globally nullify sprintf,fprintf,printfs   */
/* (Similar defines are available in each comp trace but */
/*  those are not enough to handle external libs etc. )  */
#ifdef OPENACC
#define fprintf(stderr,...) printf(__VA_ARGS__)
#define sprintf(string,...) printf(__VA_ARGS__)
#define exit(...) noprintf()
#define strcmp(a,b) str_comp(a,b)
#define strlen(a) str_len(a)
#endif
#define SCATTERED (_particle->_scattered)
#define RESTORE (_particle->_restore)
#define RESTORE_NEUTRON(_index, ...) _particle->_restore = _index;
#define ABSORB0 do { DEBUG_STATE(); DEBUG_ABSORB(); MAGNET_OFF; ABSORBED++; return; } while(0)
#define ABSORBED (_particle->_absorbed)
#define mcget_run_num() _particle->_uid
#define ABSORB ABSORB0
#pragma acc routine
void class_Progress_bar_trace(_class_Progress_bar *_comp
  , _class_particle *_particle) {
  ABSORBED=SCATTERED=RESTORE=0;
  #define profile (_comp->_parameters.profile)
  #define percent (_comp->_parameters.percent)
  #define flag_save (_comp->_parameters.flag_save)
  #define minutes (_comp->_parameters.minutes)
  #define IntermediateCnts (_comp->_parameters.IntermediateCnts)
  #define StartTime (_comp->_parameters.StartTime)
  #define EndTime (_comp->_parameters.EndTime)
  #define CurrentTime (_comp->_parameters.CurrentTime)
  #define infostring (_comp->_parameters.infostring)
  SIG_MESSAGE("[_a1_trace] component a1=Progress_bar() TRACE [Progress_bar:0]");

  #ifndef OPENACC
  double ncount;
  ncount = mcget_run_num ();
  if (!StartTime) {
    time (&StartTime); /* compute starting time */
    IntermediateCnts = 1e3;
  }
  time_t NowTime;
  time (&NowTime);
  /* compute initial estimate of computation duration */
  if (!EndTime && ncount >= IntermediateCnts) {
    CurrentTime = NowTime;
    if (difftime (NowTime, StartTime) > 10 && ncount) { /* wait 10 sec before writing ETA */
      EndTime = StartTime + (time_t)(difftime (NowTime, StartTime) * (double)mcget_ncount () / ncount);
      IntermediateCnts = 0;
      MPI_MASTER (fprintf (stdout, "\nTrace ETA "); fprintf (stdout, "%s", infostring);
                  if (difftime (EndTime, StartTime) < 60.0) fprintf (stdout, "%g [s] ", difftime (EndTime, StartTime));
                  else if (difftime (EndTime, StartTime) > 3600.0) fprintf (stdout, "%g [h] ", difftime (EndTime, StartTime) / 3600.0);
                  else fprintf (stdout, "%g [min] ", difftime (EndTime, StartTime) / 60.0); fprintf (stdout, "\n"););
    } else
      IntermediateCnts += 1e3;
    fflush (stdout);
  }

  /* display percentage when percent or minutes have reached step */
  if (EndTime && mcget_ncount () && ((minutes && difftime (NowTime, CurrentTime) > minutes * 60) || (percent && !minutes && ncount >= IntermediateCnts))) {
    MPI_MASTER (fprintf (stdout, "%llu %%\n", (unsigned long long)(ncount * 100.0 / mcget_ncount ())); fflush (stdout););
    CurrentTime = NowTime;

    IntermediateCnts = ncount + percent * mcget_ncount () / 100;
    /* check that next intermediate ncount check is a multiple of the desired percentage */
    IntermediateCnts = floor (IntermediateCnts * 100 / percent / mcget_ncount ()) * percent * mcget_ncount () / 100;
    /* raise flag to indicate that we did something */
    SCATTER;
    if (flag_save)
      save (NULL);
  }
  #endif
#ifndef NOABSORB_INF_NAN
  /* Check for nan or inf particle parms */ 
  if(isnan(p + t + vx + vy + vz + x + y + z)) ABSORB;
  if(isinf(fabs(p) + fabs(t) + fabs(vx) + fabs(vy) + fabs(vz) + fabs(x) + fabs(y) + fabs(z))) ABSORB;
#else
  if(isnan(p)  ||  isinf(p)) printf("NAN or INF found in p,  %s (particle %lld)\n",_comp->_name,_particle->_uid);
  if(isnan(t)  ||  isinf(t)) printf("NAN or INF found in t,  %s (particle %lld)\n",_comp->_name,_particle->_uid);
  if(isnan(vx) || isinf(vx)) printf("NAN or INF found in vx, %s (particle %lld)\n",_comp->_name,_particle->_uid);
  if(isnan(vy) || isinf(vy)) printf("NAN or INF found in vy, %s (particle %lld)\n",_comp->_name,_particle->_uid);
  if(isnan(vz) || isinf(vz)) printf("NAN or INF found in vz, %s (particle %lld)\n",_comp->_name,_particle->_uid);
  if(isnan(x)  ||  isinf(x)) printf("NAN or INF found in x,  %s (particle %lld)\n",_comp->_name,_particle->_uid);
  if(isnan(y)  ||  isinf(y)) printf("NAN or INF found in y,  %s (particle %lld)\n",_comp->_name,_particle->_uid);
  if(isnan(z)  ||  isinf(z)) printf("NAN or INF found in z,  %s (particle %lld)\n",_comp->_name,_particle->_uid);
#endif
  #undef profile
  #undef percent
  #undef flag_save
  #undef minutes
  #undef IntermediateCnts
  #undef StartTime
  #undef EndTime
  #undef CurrentTime
  #undef infostring
  return;
} /* class_Progress_bar_trace */

#pragma acc routine
void class_Source_div_trace(_class_Source_div *_comp
  , _class_particle *_particle) {
  ABSORBED=SCATTERED=RESTORE=0;
  #define xwidth (_comp->_parameters.xwidth)
  #define yheight (_comp->_parameters.yheight)
  #define focus_aw (_comp->_parameters.focus_aw)
  #define focus_ah (_comp->_parameters.focus_ah)
  #define E0 (_comp->_parameters.E0)
  #define dE (_comp->_parameters.dE)
  #define lambda0 (_comp->_parameters.lambda0)
  #define dlambda (_comp->_parameters.dlambda)
  #define gauss (_comp->_parameters.gauss)
  #define flux (_comp->_parameters.flux)
  #define sigmah (_comp->_parameters.sigmah)
  #define sigmav (_comp->_parameters.sigmav)
  #define p_init (_comp->_parameters.p_init)
  #define dist (_comp->_parameters.dist)
  #define focus_xw (_comp->_parameters.focus_xw)
  #define focus_yh (_comp->_parameters.focus_yh)
  SIG_MESSAGE("[_source_trace] component source=Source_div() TRACE [Source_div:0]");

  double E,lambda,v;
  double tan_h;
  double tan_v;
  double thetah;
  double thetav;

  p=p_init;
  z=0;
  t=0;

  x=randpm1()*xwidth/2.0;
  y=randpm1()*yheight/2.0;
  if(lambda0==0) {
    if (!gauss) {
      E=E0+dE*randpm1();              /*  Choose from uniform distribution */
    } else {
      E=E0+randnorm()*dE;
    }
    v=sqrt(E)*SE2V;
  } else {
    if (!gauss) {
      lambda=lambda0+dlambda*randpm1();
    } else {
      lambda=lambda0+randnorm()*dlambda;
    }
    v = K2V*(2*PI/lambda);
  }

  if (gauss==1) {
    thetah = randnorm()*sigmah;
    thetav = randnorm()*sigmav;
  } else {
    thetah = randpm1()*focus_aw*DEG2RAD/2;
    thetav = randpm1()*focus_ah*DEG2RAD/2;
  }

  tan_h = tan(thetah);
  tan_v = tan(thetav);

  /* Perform the correct treatment - no small angle approx. here! */
  vz = v / sqrt(1 + tan_v*tan_v + tan_h*tan_h);
  vy = tan_v * vz;
  vx = tan_h * vz;
#ifndef NOABSORB_INF_NAN
  /* Check for nan or inf particle parms */ 
  if(isnan(p + t + vx + vy + vz + x + y + z)) ABSORB;
  if(isinf(fabs(p) + fabs(t) + fabs(vx) + fabs(vy) + fabs(vz) + fabs(x) + fabs(y) + fabs(z))) ABSORB;
#else
  if(isnan(p)  ||  isinf(p)) printf("NAN or INF found in p,  %s (particle %lld)\n",_comp->_name,_particle->_uid);
  if(isnan(t)  ||  isinf(t)) printf("NAN or INF found in t,  %s (particle %lld)\n",_comp->_name,_particle->_uid);
  if(isnan(vx) || isinf(vx)) printf("NAN or INF found in vx, %s (particle %lld)\n",_comp->_name,_particle->_uid);
  if(isnan(vy) || isinf(vy)) printf("NAN or INF found in vy, %s (particle %lld)\n",_comp->_name,_particle->_uid);
  if(isnan(vz) || isinf(vz)) printf("NAN or INF found in vz, %s (particle %lld)\n",_comp->_name,_particle->_uid);
  if(isnan(x)  ||  isinf(x)) printf("NAN or INF found in x,  %s (particle %lld)\n",_comp->_name,_particle->_uid);
  if(isnan(y)  ||  isinf(y)) printf("NAN or INF found in y,  %s (particle %lld)\n",_comp->_name,_particle->_uid);
  if(isnan(z)  ||  isinf(z)) printf("NAN or INF found in z,  %s (particle %lld)\n",_comp->_name,_particle->_uid);
#endif
  #undef xwidth
  #undef yheight
  #undef focus_aw
  #undef focus_ah
  #undef E0
  #undef dE
  #undef lambda0
  #undef dlambda
  #undef gauss
  #undef flux
  #undef sigmah
  #undef sigmav
  #undef p_init
  #undef dist
  #undef focus_xw
  #undef focus_yh
  return;
} /* class_Source_div_trace */

void class_Union_master_trace(_class_Union_master *_comp
  , _class_particle *_particle) {
  ABSORBED=SCATTERED=RESTORE=0;
  #define enable_refraction (_comp->_parameters.enable_refraction)
  #define enable_reflection (_comp->_parameters.enable_reflection)
  #define verbal (_comp->_parameters.verbal)
  #define list_verbal (_comp->_parameters.list_verbal)
  #define finally_verbal (_comp->_parameters.finally_verbal)
  #define allow_inside_start (_comp->_parameters.allow_inside_start)
  #define enable_tagging (_comp->_parameters.enable_tagging)
  #define history_limit (_comp->_parameters.history_limit)
  #define enable_conditionals (_comp->_parameters.enable_conditionals)
  #define inherit_number_of_scattering_events (_comp->_parameters.inherit_number_of_scattering_events)
  #define weight_ratio_limit (_comp->_parameters.weight_ratio_limit)
  #define init (_comp->_parameters.init)
  #define global_positions_to_transform_list_master (_comp->_parameters.global_positions_to_transform_list_master)
  #define global_rotations_to_transform_list_master (_comp->_parameters.global_rotations_to_transform_list_master)
  #define global_process_list_master (_comp->_parameters.global_process_list_master)
  #define global_material_list_master (_comp->_parameters.global_material_list_master)
  #define global_surface_list_master (_comp->_parameters.global_surface_list_master)
  #define global_geometry_list_master (_comp->_parameters.global_geometry_list_master)
  #define global_all_volume_logger_list_master (_comp->_parameters.global_all_volume_logger_list_master)
  #define global_specific_volumes_logger_list_master (_comp->_parameters.global_specific_volumes_logger_list_master)
  #define global_all_volume_abs_logger_list_master (_comp->_parameters.global_all_volume_abs_logger_list_master)
  #define global_specific_volumes_abs_logger_list_master (_comp->_parameters.global_specific_volumes_abs_logger_list_master)
  #define global_tagging_conditional_list_master (_comp->_parameters.global_tagging_conditional_list_master)
  #define global_master_list_master (_comp->_parameters.global_master_list_master)
  #define starting_volume_warning (_comp->_parameters.starting_volume_warning)
  #define global_master_element (_comp->_parameters.global_master_element)
  #define this_global_master_index (_comp->_parameters.this_global_master_index)
  #define previous_master_index (_comp->_parameters.previous_master_index)
  #define geometry_list_index (_comp->_parameters.geometry_list_index)
  #define intersection_time_table (_comp->_parameters.intersection_time_table)
  #define Volumes (_comp->_parameters.Volumes)
  #define Geometries (_comp->_parameters.Geometries)
  #define Volume_copies (_comp->_parameters.Volume_copies)
  #define starting_lists (_comp->_parameters.starting_lists)
  #define Volume_copies_allocated (_comp->_parameters.Volume_copies_allocated)
  #define r (_comp->_parameters.r)
  #define r_start (_comp->_parameters.r_start)
  #define v (_comp->_parameters.v)
  #define error_msg (_comp->_parameters.error_msg)
  #define component_error_msg (_comp->_parameters.component_error_msg)
  #define string_output (_comp->_parameters.string_output)
  #define number_of_volumes (_comp->_parameters.number_of_volumes)
  #define volume_index (_comp->_parameters.volume_index)
  #define process_index (_comp->_parameters.process_index)
  #define iterator (_comp->_parameters.iterator)
  #define solutions (_comp->_parameters.solutions)
  #define max_number_of_processes (_comp->_parameters.max_number_of_processes)
  #define limit (_comp->_parameters.limit)
  #define solution (_comp->_parameters.solution)
  #define min_solution (_comp->_parameters.min_solution)
  #define ignore_closest (_comp->_parameters.ignore_closest)
  #define ignore_surface_index (_comp->_parameters.ignore_surface_index)
  #define min_volume (_comp->_parameters.min_volume)
  #define time_found (_comp->_parameters.time_found)
  #define intersection_time (_comp->_parameters.intersection_time)
  #define min_intersection_time (_comp->_parameters.min_intersection_time)
  #define process (_comp->_parameters.process)
  #define process_start (_comp->_parameters.process_start)
  #define my_trace (_comp->_parameters.my_trace)
  #define p_my_trace (_comp->_parameters.p_my_trace)
  #define my_trace_fraction_control (_comp->_parameters.my_trace_fraction_control)
  #define k (_comp->_parameters.k)
  #define k_new (_comp->_parameters.k_new)
  #define k_old (_comp->_parameters.k_old)
  #define k_rotated (_comp->_parameters.k_rotated)
  #define v_length (_comp->_parameters.v_length)
  #define my_sum (_comp->_parameters.my_sum)
  #define my_sum_plus_abs (_comp->_parameters.my_sum_plus_abs)
  #define culmative_probability (_comp->_parameters.culmative_probability)
  #define mc_prop (_comp->_parameters.mc_prop)
  #define time_to_scattering (_comp->_parameters.time_to_scattering)
  #define length_to_scattering (_comp->_parameters.length_to_scattering)
  #define length_to_boundary (_comp->_parameters.length_to_boundary)
  #define time_to_boundery (_comp->_parameters.time_to_boundery)
  #define selected_process (_comp->_parameters.selected_process)
  #define scattering_event (_comp->_parameters.scattering_event)
  #define time_propagated_without_scattering (_comp->_parameters.time_propagated_without_scattering)
  #define a_next_volume_found (_comp->_parameters.a_next_volume_found)
  #define next_volume (_comp->_parameters.next_volume)
  #define next_volume_priority (_comp->_parameters.next_volume_priority)
  #define done (_comp->_parameters.done)
  #define current_volume (_comp->_parameters.current_volume)
  #define previous_volume (_comp->_parameters.previous_volume)
  #define ray_sucseeded (_comp->_parameters.ray_sucseeded)
  #define number_of_solutions (_comp->_parameters.number_of_solutions)
  #define number_of_solutions_static (_comp->_parameters.number_of_solutions_static)
  #define check (_comp->_parameters.check)
  #define start (_comp->_parameters.start)
  #define intersection_with_children (_comp->_parameters.intersection_with_children)
  #define geometry_output (_comp->_parameters.geometry_output)
  #define tree_next_volume (_comp->_parameters.tree_next_volume)
  #define pre_allocated1 (_comp->_parameters.pre_allocated1)
  #define pre_allocated2 (_comp->_parameters.pre_allocated2)
  #define pre_allocated3 (_comp->_parameters.pre_allocated3)
  #define ray_position (_comp->_parameters.ray_position)
  #define ray_velocity (_comp->_parameters.ray_velocity)
  #define ray_velocity_rotated (_comp->_parameters.ray_velocity_rotated)
  #define ray_velocity_final (_comp->_parameters.ray_velocity_final)
  #define wavevector (_comp->_parameters.wavevector)
  #define wavevector_rotated (_comp->_parameters.wavevector_rotated)
  #define volume_0_found (_comp->_parameters.volume_0_found)
  #define scattered_flag (_comp->_parameters.scattered_flag)
  #define scattered_flag_VP (_comp->_parameters.scattered_flag_VP)
  #define master_transposed_rotation_matrix (_comp->_parameters.master_transposed_rotation_matrix)
  #define temp_rotation_matrix (_comp->_parameters.temp_rotation_matrix)
  #define temp_transpose_rotation_matrix (_comp->_parameters.temp_transpose_rotation_matrix)
  #define non_rotated_position (_comp->_parameters.non_rotated_position)
  #define rotated_position (_comp->_parameters.rotated_position)
  #define non_isotropic_found (_comp->_parameters.non_isotropic_found)
  #define master_tagging_node_list (_comp->_parameters.master_tagging_node_list)
  #define current_tagging_node (_comp->_parameters.current_tagging_node)
  #define tagging_leaf_counter (_comp->_parameters.tagging_leaf_counter)
  #define stop_tagging_ray (_comp->_parameters.stop_tagging_ray)
  #define stop_creating_nodes (_comp->_parameters.stop_creating_nodes)
  #define number_of_scattering_events (_comp->_parameters.number_of_scattering_events)
  #define real_transmission_probability (_comp->_parameters.real_transmission_probability)
  #define mc_transmission_probability (_comp->_parameters.mc_transmission_probability)
  #define number_of_process_interacts_set (_comp->_parameters.number_of_process_interacts_set)
  #define index_of_lacking_process (_comp->_parameters.index_of_lacking_process)
  #define total_process_interact (_comp->_parameters.total_process_interact)
  #define geometry_component_index_list (_comp->_parameters.geometry_component_index_list)
  #define mask_volume_index_list (_comp->_parameters.mask_volume_index_list)
  #define number_of_masks (_comp->_parameters.number_of_masks)
  #define number_of_masked_volumes (_comp->_parameters.number_of_masked_volumes)
  #define mask_status_list (_comp->_parameters.mask_status_list)
  #define current_mask_intersect_list_status (_comp->_parameters.current_mask_intersect_list_status)
  #define mask_index_main (_comp->_parameters.mask_index_main)
  #define mask_iterator (_comp->_parameters.mask_iterator)
  #define mask_start (_comp->_parameters.mask_start)
  #define mask_check (_comp->_parameters.mask_check)
  #define need_to_run_within_which_volume (_comp->_parameters.need_to_run_within_which_volume)
  #define number_of_processes_array (_comp->_parameters.number_of_processes_array)
  #define p_old (_comp->_parameters.p_old)
  #define log_index (_comp->_parameters.log_index)
  #define conditional_status (_comp->_parameters.conditional_status)
  #define this_logger (_comp->_parameters.this_logger)
  #define this_abs_logger (_comp->_parameters.this_abs_logger)
  #define tagging_conditional_list (_comp->_parameters.tagging_conditional_list)
  #define logger_conditional_extend_array (_comp->_parameters.logger_conditional_extend_array)
  #define abs_logger_conditional_extend_array (_comp->_parameters.abs_logger_conditional_extend_array)
  #define max_conditional_extend_index (_comp->_parameters.max_conditional_extend_index)
  #define tagging_conditional_extend (_comp->_parameters.tagging_conditional_extend)
  #define free_tagging_conditioanl_list (_comp->_parameters.free_tagging_conditioanl_list)
  #define safety_distance (_comp->_parameters.safety_distance)
  #define safety_distance2 (_comp->_parameters.safety_distance2)
  #define temporary_focus_data (_comp->_parameters.temporary_focus_data)
  #define this_focus_data (_comp->_parameters.this_focus_data)
  #define focus_data_index (_comp->_parameters.focus_data_index)
  #define r_old (_comp->_parameters.r_old)
  #define initial_weight (_comp->_parameters.initial_weight)
  #define abs_weight_factor (_comp->_parameters.abs_weight_factor)
  #define time_old (_comp->_parameters.time_old)
  #define absorption_index (_comp->_parameters.absorption_index)
  #define abs_weight_factor_set (_comp->_parameters.abs_weight_factor_set)
  #define my_abs (_comp->_parameters.my_abs)
  #define absorption_event_data (_comp->_parameters.absorption_event_data)
  #define abs_position (_comp->_parameters.abs_position)
  #define transformed_abs_position (_comp->_parameters.transformed_abs_position)
  #define t_abs_propagation (_comp->_parameters.t_abs_propagation)
  #define abs_distance (_comp->_parameters.abs_distance)
  #define abs_max_length (_comp->_parameters.abs_max_length)
  #define longest_surface_stack (_comp->_parameters.longest_surface_stack)
  #define interface_stack (_comp->_parameters.interface_stack)
  SIG_MESSAGE("[_sample_trace] component sample=Union_master() TRACE [Union_master:0]");


  #ifdef Union_trace_verbal_setting
  printf ("\n\n\n\n\n----------- NEW RAY -------------------------------------------------\n");
  printf ("Union_master component name: %s \n \n", NAME_CURRENT_COMP);
  #endif

  double start_weight;
  start_weight = p;

  double weight_limit;
  weight_limit = p * weight_ratio_limit;

  // Initialize logic
  done = 0;
  error_msg = 0;
  clear_intersection_table (&intersection_time_table);

  time_propagated_without_scattering = 0;
  v_length = sqrt (vx * vx + vy * vy + vz * vz);

  // Initialize logger system / Statistics
  number_of_scattering_events = 0;

  if (inherit_number_of_scattering_events == 1) // Continue number of scattering from previous Union_master
    number_of_scattering_events = global_master_list_master->elements[this_global_master_index - 1].stored_number_of_scattering_events;

  // Zero scattered_flag_VP data
  for (volume_index = 1; volume_index < number_of_volumes; volume_index++) { // No reason to update volume 0, as scattering doesn't happen there
    scattered_flag[volume_index] = 0;
    for (process_index = 0; process_index < number_of_processes_array[volume_index]; process_index++)
      scattered_flag_VP[volume_index][process_index] = 0;
  }

  // If first Union_master in instrument, reset loggers_with_data_array and clean unused data.
  // Unused data happens when logging data is passed to the next Union_master, but the ray is absorbed on the way.
  // Could be improved by using the precompiler instead as ncount times the number of Union_masters could be avoided.
  if (global_master_list_master->elements[0].component_index == INDEX_CURRENT_COMP) {
    // If this is the first Union master, clean up logger data for rays that did not make it through Union components
    for (log_index = loggers_with_data_array.used_elements - 1; log_index > -1; log_index--) {
      loggers_with_data_array.logger_pointers[log_index]->function_pointers.clear_temp (&loggers_with_data_array.logger_pointers[log_index]->data_union);
    }
    loggers_with_data_array.used_elements = 0;
    for (log_index = abs_loggers_with_data_array.used_elements - 1; log_index > -1; log_index--) {
      abs_loggers_with_data_array.abs_logger_pointers[log_index]->function_pointers.clear_temp (
          &abs_loggers_with_data_array.abs_logger_pointers[log_index]->data_union);
    }
    abs_loggers_with_data_array.used_elements = 0;
  }
  tagging_conditional_extend = 0;
  for (iterator = 0; iterator < max_conditional_extend_index + 1; iterator++) {
    logger_conditional_extend_array[iterator] = 0;
  }

  // Need to clean up the double notation for position and velocity. // REVIEW_LINE
  r_start[0] = x;
  r_start[1] = y;
  r_start[2] = z;
  r[0] = x;
  r[1] = y;
  r[2] = z;
  v[0] = vx;
  v[1] = vy;
  v[2] = vz; // REVIEW_LINE r and v are bad names
  k[0] = V2K * vx;
  k[1] = V2K * vy;
  k[2] = V2K * vz;

  ray_position = coords_set (x, y, z);
  ray_velocity = coords_set (vx, vy, vz);

  // Mask update: need to check the mask status for the initial position
  // mask status for a mask is 1 if the ray position is inside, 0 if it is outside
  for (iterator = 0; iterator < number_of_masks; iterator++) {
    // CPU Only
    // if(Volumes[mask_volume_index_list.elements[iterator]]->geometry.within_function(ray_position,&Volumes[mask_volume_index_list.elements[iterator]]->geometry)
    // == 1) {
    // GPU
    if (r_within_function (ray_position, &Volumes[mask_volume_index_list.elements[iterator]]->geometry) == 1) {
      mask_status_list.elements[iterator] = 1;
    } else {
      mask_status_list.elements[iterator] = 0;
    }
  }

  #ifdef Union_trace_verbal_setting
  print_1d_int_list (mask_status_list, "Initial mask status list");
  #endif

  // Now the initial current_volume can be found, which requires the up to date mask_status_list
  current_volume = within_which_volume_GPU (ray_position, starting_lists.reduced_start_list, starting_lists.starting_destinations_list, Volumes,
                                            &mask_status_list, number_of_volumes, pre_allocated1, pre_allocated2, pre_allocated3);

  // For excluding closest intersection in search after refraction/reflection
  ignore_closest = -1;

  // Using the mask_status_list and the current volume, the current_mask_intersect_list_status can be made
  //  it contains the effective mask status of all volumes on the current volumes mask intersect list, which needs to be calculated,
  //  but only when the current volume or mask status changes, not under for example scattering inside the current volume
  update_current_mask_intersect_status (&current_mask_intersect_list_status, &mask_status_list, Volumes, &current_volume);

  #ifdef Union_trace_verbal_setting
  printf ("Starting current_volume = %d\n", current_volume);
  #endif

  // Check if the ray appeared in an allowed starting volume, unless this check is disabled by the user for advanced cases
  if (allow_inside_start == 0 && starting_lists.allowed_starting_volume_logic_list.elements[current_volume] == 0) {
    printf ("ERROR, ray ''teleported'' into Union component %s, if intentional, set allow_inside_start=1\n", NAME_CURRENT_COMP);
    // NEED ERROR FLAG: Need to set an error flag that is read in finally to warn user of problem.
    exit (1);
  }
  // Warn the user that rays have appeared inside a volume instead of outside as expected
  if (starting_volume_warning == 0 && current_volume != 0) {
    printf ("WARNING: Ray started in volume ''%s'' rather than the surrounding vacuum in component %s. This warning is only shown once.\n",
            Volumes[current_volume]->name, NAME_CURRENT_COMP);
    starting_volume_warning = 1;
  }

  // Placing the new ray at the start of the tagging tree corresponding to current volume
  // A history limit can be imposed so that no new nodes are created after this limit (may be necessary to fit in memory)
  // Rays can still follow the nodes created before even when no additional nodes are created, but if a situation that
  //  requires a new node is encountered, stop_tagging_ray is set to 1, stopping further tagging and preventing the data
  //  for that ray to be used further.
  if (enable_tagging) {
    current_tagging_node = master_tagging_node_list.elements[current_volume];
    stop_tagging_ray = 0; // Allow this ray to be tracked
    if (tagging_leaf_counter > history_limit)
      stop_creating_nodes = 1;
  }

  #ifdef Union_trace_verbal_setting
  if (enable_tagging)
    printf ("current_tagging_node->intensity = %f\n", current_tagging_node->intensity);
  if (enable_tagging)
    printf ("current_tagging_node->number_of_rays = %d \n", current_tagging_node->number_of_rays);
  #endif

  // Propagation loop including scattering
  // This while loop continues until the ray leaves the ensamble of user defined volumes either through volume 0
  //  or a dedicated exit volume. The loop is cancelled after a large number of iterations as a failsafe for errors.
  // A single run of the loop will either be a propagation to the next volume along the path of the ray, or a
  //  scattering event at some point along the path of the ray in the current volume.
  limit = 100000;
  while (done == 0) {
    limit--;

    #ifdef Union_trace_verbal_setting
    printf ("----------- START OF WHILE LOOP --------------------------------------\n");
    print_intersection_table (&intersection_time_table);
    printf ("current_volume = %d \n", current_volume);
    #endif

    if (weight_ratio_limit && p < weight_limit) {
      #ifdef Union_trace_verbal_setting
      printf ("Weight reduced more than ratio_limit p=%lf, p0=%lf, (p_limit=%lf, limit=%d)\n", p, start_weight, weight_limit, limit);
      #endif
      // printf("Weight reduced more than ratio_limit p=%30.28lf, p0=%15.13lf, (p_limit=%15.13lf, scatter=%d)\n", p, start_weight, weight_limit, 100000-limit);
      ABSORB;
    }

    // Calculating intersections with the necessary volumes. The relevant set of volumes depend on the current volume and the mask status array.
    // First the volumes on the current volumes intersect list is checked, then its mask interset list. Before checking the volume itself, it is
    //  checked if any children of the current volume is intersected, in which case the intersection calculation with the current volume can be
    //  skipped.

    // Checking intersections for all volumes in the intersect list.
    for (start = check = Volumes[current_volume]->geometry.intersect_check_list.elements;
         check - start < Volumes[current_volume]->geometry.intersect_check_list.num_elements; check++) {
      // This will leave check as a pointer to the intergers in the intersect_check_list and iccrement nicely
      #ifdef Union_trace_verbal_setting
      printf ("Intersect_list = %d being checked \n", *check);
      #endif

      if (intersection_time_table.calculated[*check] == 0) {
        #ifdef Union_trace_verbal_setting
        printf ("running intersection for intersect_list with *check = %d \n", *check);
        #endif
        // Calculate intersections using intersect function imbedded in the relevant volume structure using parameters that are also imbedded in the structure.
        #ifdef Union_trace_verbal_setting
        printf ("surface_index[*check][0] = %d, surface_index[*check][1] = %d\n", intersection_time_table.surface_index[*check][0],
                intersection_time_table.surface_index[*check][1]);
        #endif
        // GPU Flexible intersect_function call
        geometry_output = intersect_function (intersection_time_table.intersection_times[*check], intersection_time_table.normal_vector_x[*check],
                                              intersection_time_table.normal_vector_y[*check], intersection_time_table.normal_vector_z[*check],
                                              intersection_time_table.surface_index[*check], number_of_solutions, r_start, v, &Volumes[*check]->geometry);

        intersection_time_table.calculated[*check] = 1;
        #ifdef Union_trace_verbal_setting
        printf ("finished running intersection for intersect_list with *check = %d \n", *check);
        print_intersection_table (&intersection_time_table);
        #endif
      }
    }

    // Mask update: add additional loop for checking intersections with masked volumes depending on mask statuses
    for (mask_iterator = 0; mask_iterator < Volumes[current_volume]->geometry.mask_intersect_list.num_elements; mask_iterator++) {
      if (current_mask_intersect_list_status.elements[mask_iterator] == 1) { // Only check if the mask is active
                                                                             #ifdef Union_trace_verbal_setting
        printf ("Mask Intersect_list = %d being checked \n", Volumes[current_volume]->geometry.mask_intersect_list.elements[mask_iterator]);
        #endif
        if (intersection_time_table.calculated[Volumes[current_volume]->geometry.mask_intersect_list.elements[mask_iterator]] == 0) {
          #ifdef Union_trace_verbal_setting
          printf ("running intersection for mask_intersect_list element = %d \n", Volumes[current_volume]->geometry.mask_intersect_list.elements[mask_iterator]);
          //  printf("r = (%f,%f,%f) v = (%f,%f,%f) \n",r[0],r[1],r[2],v[0],v[1],v[2]);
          #endif
          // Calculate intersections using intersect function imbedded in the relevant volume structure using parameters
          //  that are also imbedded in the structure.
          // CPU Only
          // geometry_output =
          // Volumes[Volumes[current_volume]->geometry.mask_intersect_list.elements[mask_iterator]]->geometry.intersect_function(intersection_time_table.intersection_times[Volumes[current_volume]->geometry.mask_intersect_list.elements[mask_iterator]],number_of_solutions,r_start,v,&Volumes[Volumes[current_volume]->geometry.mask_intersect_list.elements[mask_iterator]]->geometry);

          // GPU allowed
          int selected_index;
          selected_index = Volumes[current_volume]->geometry.mask_intersect_list.elements[mask_iterator];
          geometry_output
              = intersect_function (intersection_time_table.intersection_times[selected_index], intersection_time_table.normal_vector_x[selected_index],
                                    intersection_time_table.normal_vector_y[selected_index], intersection_time_table.normal_vector_z[selected_index],
                                    intersection_time_table.surface_index[selected_index], number_of_solutions, r_start, v, &Volumes[selected_index]->geometry);

          intersection_time_table.calculated[Volumes[current_volume]->geometry.mask_intersect_list.elements[mask_iterator]] = 1;
          // if printf("succesfully calculated intersection times for volume *check = %d \n",*check);
        }
      }
    }

    // Checking if there are intersections with children of current volume, which means there is an intersection before current_volume, and thus can be skipped.
    // But only if they have not been overwritten. In case current_volume is 0, there is no need to do this regardless.
    if (current_volume != 0 && intersection_time_table.calculated[current_volume] == 0) {
      #ifdef Union_trace_verbal_setting
      printf ("Checking if children of current_volume = %d have intersections. \n", current_volume);
      #endif
      intersection_with_children = 0;
      // for (start = check = Volumes[current_volume]->geometry.direct_children.elements;check - start <
      // Volumes[current_volume]->geometry.children.num_elements;check++) { // REVIEW LINE. Caused bug with masks.
      if (!Volumes[current_volume]->geometry.skip_hierarchy_optimization) {
        for (start = check = Volumes[current_volume]->geometry.children.elements; check - start < Volumes[current_volume]->geometry.children.num_elements;
             check++) {
          #ifdef Union_trace_verbal_setting
          printf ("Checking if child %d of current_volume = %d have intersections. \n", *check, current_volume);
          #endif
          // Only check the first of the two results in the intersection table, as they are ordered, and the second is of no interest
          if (intersection_time_table.calculated[*check] == 1 && intersection_time_table.intersection_times[*check][0] > time_propagated_without_scattering) {
            // If this child is masked, its mask status need to be 1 in order to be taken into account
            if (Volumes[*check]->geometry.is_masked_volume == 0) {
              #ifdef Union_trace_verbal_setting
              printf ("Found an child of current_volume with an intersection. Skips calculating for current_volume \n");
              #endif
              intersection_with_children = 1;
              break; // No need to check more, if there is just one it is not necessary to calculate intersection with current_volume yet
            } else {
              #ifdef Union_trace_verbal_setting
              printf ("Found an child of current_volume with an intersection, but it is masked. Check to see if it can skip calculating for current_volume \n");
              #endif

              if (Volumes[*check]->geometry.mask_mode == 2) { // ANY mask mode
                tree_next_volume = 0;
                for (mask_start = mask_check = Volumes[*check]->geometry.masked_by_mask_index_list.elements;
                     mask_check - mask_start < Volumes[*check]->geometry.masked_by_mask_index_list.num_elements; mask_check++) {
                  if (mask_status_list.elements[*mask_check] == 1) {
                    intersection_with_children = 1;
                    break;
                  }
                }
              } else { // ALL mask mode
                intersection_with_children = 1;
                for (mask_start = mask_check = Volumes[*check]->geometry.masked_by_mask_index_list.elements;
                     mask_check - mask_start < Volumes[*check]->geometry.masked_by_mask_index_list.num_elements; mask_check++) {
                  if (mask_status_list.elements[*mask_check] == 0) {
                    intersection_with_children = 0;
                    break;
                  }
                }
              }
              #ifdef Union_trace_verbal_setting
              printf ("The mask status was 1, can actually skip intersection calculation for current volume \n");
              #endif
              if (intersection_with_children == 1)
                break;
            }
          }
        }
      }
      #ifdef Union_trace_verbal_setting
      printf ("intersection_with_children = %d \n", intersection_with_children);
      #endif
      if (intersection_with_children == 0) {
        // GPU Allowed
        geometry_output
            = intersect_function (intersection_time_table.intersection_times[current_volume], intersection_time_table.normal_vector_x[current_volume],
                                  intersection_time_table.normal_vector_y[current_volume], intersection_time_table.normal_vector_z[current_volume],
                                  intersection_time_table.surface_index[current_volume], number_of_solutions, r_start, v, &Volumes[current_volume]->geometry);

        intersection_time_table.calculated[current_volume] = 1;
      }
    }

    // At this point, intersection_time_table is updated with intersection times of all possible intersections.
    #ifdef Union_trace_verbal_setting
    print_intersection_table (&intersection_time_table);
    #endif

    // For the closest intersection to volume with index ignore_closest, the scattering with the shortest absolute time should be ignored
    if (ignore_closest > 0) {
      min_intersection_time = 1E9;
      min_solution = -1;
      for (solution = 0; solution < intersection_time_table.n_elements[ignore_closest]; solution++) {
        // For a solution to be removed, it must have the same surface index as the ignored surface interaction point
        if (intersection_time_table.surface_index[ignore_closest][solution] == ignore_surface_index
            && fabs (intersection_time_table.intersection_times[ignore_closest][solution]) < min_intersection_time) {
          //            if (fabs(intersection_time_table.intersection_times[ignore_closest][solution]) < min_intersection_time) {
          min_intersection_time = fabs (intersection_time_table.intersection_times[ignore_closest][solution]);
          min_solution = solution;
        }
      }
      if (min_solution != -1) {
        // Remove the intersection closest to current point from time table
        intersection_time_table.intersection_times[ignore_closest][min_solution] = -1;
        #ifdef Union_trace_verbal_setting
        printf ("Removed solution nr %d for volume %d with ignore closest\n", min_solution, ignore_closest);
        print_intersection_table (&intersection_time_table);
        #endif
      }
    }

    // Next task is to find the next intersection time. The next intersection must be greater than the time_propagated_without_scattering (0 at start of loop)
    // Loops are eqvialent to the 3 intersection calculation loops already completed

    // First loop for checking intersect_check_list

    #ifdef Union_trace_verbal_setting
    printf ("Incoming value of MIN_intersection_time=%g\n", min_intersection_time);
    #endif
    min_intersection_time = 0;
    time_found = 0;
    for (start = check = Volumes[current_volume]->geometry.intersect_check_list.elements;
         check - start < Volumes[current_volume]->geometry.intersect_check_list.num_elements; check++) {
      for (solution = 0; solution < intersection_time_table.n_elements[*check]; solution++) {
        if (time_found) {
          if ((intersection_time = intersection_time_table.intersection_times[*check][solution]) > time_propagated_without_scattering
              && intersection_time < min_intersection_time) {
            min_intersection_time = intersection_time;
            min_solution = solution;
            min_volume = *check;
            #ifdef Union_trace_verbal_setting
            printf ("found A at %i x %i\n", *check, solution);
            #endif
          }
        } else {
          if ((intersection_time = intersection_time_table.intersection_times[*check][solution]) > time_propagated_without_scattering) {
            min_intersection_time = intersection_time;
            min_solution = solution;
            min_volume = *check;
            time_found = 1;
            #ifdef Union_trace_verbal_setting
            printf ("found B at %i x %i\n", *check, solution);
            #endif
          }
        }
      }
    }
    #ifdef Union_trace_verbal_setting
    printf ("min_intersection_time=%g min_solution=%i\n", min_intersection_time, min_solution);
    #endif

    // Now check the masked_intersect_list, but only the ones that are currently active
    for (mask_iterator = 0; mask_iterator < Volumes[current_volume]->geometry.mask_intersect_list.num_elements; mask_iterator++) {
      if (current_mask_intersect_list_status.elements[mask_iterator] == 1) {
        for (solution = 0; solution < intersection_time_table.n_elements[Volumes[current_volume]->geometry.mask_intersect_list.elements[mask_iterator]];
             solution++) {
          if (time_found) {
            if ((intersection_time
                 = intersection_time_table.intersection_times[Volumes[current_volume]->geometry.mask_intersect_list.elements[mask_iterator]][solution])
                    > time_propagated_without_scattering
                && intersection_time < min_intersection_time) {
              min_intersection_time = intersection_time;
              min_solution = solution;
              min_volume = Volumes[current_volume]->geometry.mask_intersect_list.elements[mask_iterator];
            }
          } else {
            if ((intersection_time
                 = intersection_time_table.intersection_times[Volumes[current_volume]->geometry.mask_intersect_list.elements[mask_iterator]][solution])
                > time_propagated_without_scattering) {
              min_intersection_time = intersection_time;
              min_solution = solution;
              min_volume = Volumes[current_volume]->geometry.mask_intersect_list.elements[mask_iterator];
              time_found = 1;
            }
          }
        }
      }
    }

    // And check the current_volume
    for (solution = 0; solution < intersection_time_table.n_elements[current_volume]; solution++) {
      if (time_found) {
        if ((intersection_time = intersection_time_table.intersection_times[current_volume][solution]) > time_propagated_without_scattering
            && intersection_time < min_intersection_time) {
          min_intersection_time = intersection_time;
          min_solution = solution;
          min_volume = current_volume;
        }
      } else {
        if ((intersection_time = intersection_time_table.intersection_times[current_volume][solution]) > time_propagated_without_scattering) {
          min_intersection_time = intersection_time;
          min_solution = solution;
          min_volume = current_volume;
          time_found = 1;
        }
      }
    }

    // Reset ingore_closest after one intersection iteration
    ignore_closest = -1;

    #ifdef Union_trace_verbal_setting
    printf ("min_intersection_time = %f \n", min_intersection_time);
    printf ("min_solution          = %d \n", min_solution);
    printf ("min_volume            = %d \n", min_volume);
    printf ("time_found            = %d \n", time_found);
    #endif

    abs_weight_factor = 1.0;
    abs_weight_factor_set = 0;

    // If a time is found, propagation continues, and it will be checked if a scattering occurs before the next intersection.
    // If a time is not found, the ray must be leaving the ensamble of volumes and the loop will be concluded
    if (time_found) {
      time_to_boundery = min_intersection_time - time_propagated_without_scattering; // calculate the time remaining before the next intersection
      scattering_event = 0;                                                          // Assume a scattering event will not occur

      // Check if a scattering event should occur
      if (current_volume != 0) { // Volume 0 is always vacuum, and if this is the current volume, an event will not occur
        if (Volumes[current_volume]->p_physics->number_of_processes == 0) { // If there are no processes, the volume could be vacuum or an absorber
          if (Volumes[current_volume]->p_physics->is_vacuum == 0) {
            // This volume does not have physical processes but does have an absorption cross section, so the ray weight is reduced accordingly

            my_sum_plus_abs = Volumes[current_volume]->p_physics->my_a * (2200 / v_length);
            length_to_boundary = time_to_boundery * v_length;

            abs_weight_factor = exp (-Volumes[current_volume]->p_physics->my_a * 2200 * time_to_boundery);
            abs_weight_factor_set = 1;

            #ifdef Union_trace_verbal_setting
            printf ("name of material: %s \n", Volumes[current_volume]->name);
            printf ("length to boundery  = %f\n", length_to_boundary);
            printf ("absorption cross section  = %f\n", Volumes[current_volume]->p_physics->my_a);
            printf ("chance to get through this length of absorber: %f %%\n", 100 * exp (-Volumes[current_volume]->p_physics->my_a * length_to_boundary));
            #endif
          }
        } else {
          // Since there is a non-zero number of processes in this material, all the scattering cross section for these are calculated
          my_sum = 0;
          k[0] = V2K * vx;
          k[1] = V2K * vy;
          k[2] = V2K * vz;
          p_my_trace = my_trace;
          wavevector = coords_set (k[0], k[1], k[2]);
          length_to_boundary = time_to_boundery * v_length;

          double forced_length_to_scattering;
          Coords ray_position_geometry;

          // If any process in this material needs focusing, sample scattering position and update focus_data accordingly
          if (Volumes[current_volume]->p_physics->any_process_needs_cross_section_focus == 1) {
            // Sample length_to_scattering in linear manner
            forced_length_to_scattering = safety_distance + rand01 () * (length_to_boundary - safety_distance2);

            ray_velocity = coords_set (vx, vy, vz); // Test for root cause
            // Find location of scattering point in master coordinate system without changing main position / velocity variables
            Coords direction = coords_scalar_mult (ray_velocity, 1.0 / length_of_position_vector (ray_velocity));
            Coords scattering_displacement = coords_scalar_mult (direction, forced_length_to_scattering);
            Coords forced_ray_scattering_point = coords_add (ray_position, scattering_displacement);
            ray_position_geometry
                = coords_sub (forced_ray_scattering_point, Volumes[current_volume]->geometry.center); // ray_position relative to geometry center

            // Calculate the aim for non isotropic processes
            this_focus_data = &Volumes[current_volume]->geometry.focus_data_array.elements[0];
            this_focus_data->RayAim = coords_sub (this_focus_data->Aim, ray_position_geometry); // Aim vector for this ray

            #ifdef Union_trace_verbal_setting
            printf ("Prepared for focus in cross section calculation in volume: %s \n", Volumes[current_volume]->name);
            printf ("forced_length_to_scattering =%lf \n", forced_length_to_scattering);
            print_position (ray_position, "ray_position");
            print_position (direction, "direction");
            print_position (scattering_displacement, "scattering_displacement");
            print_position (forced_ray_scattering_point, "forced_ray_scattering_point");
            print_position (ray_position_geometry, "ray_position_geometry");
            printf ("for isotropic processes this RayAim is used \n");
            print_position (this_focus_data->RayAim, "this_focus_data->RayAim");
            #endif

            /*
            // update focus data for this ray (could limit this to only update the necessary focus_data element, but there are typically very few)
            int f_index;
            for (f_index=0; f_index < Volumes[current_volume]->geometry.focus_data_array.num_elements; f_index++) {
              this_focus_data = &Volumes[current_volume]->geometry.focus_data_array.elements[f_index];
              // Coords ray_position_geometry_rotated = rot_apply(this_focus_data.absolute_rotation, ray_position_geometry);
              this_focus_data->RayAim = coords_sub(this_focus_data->Aim, ray_position_geometry); // Aim vector for this ray
            }

            printf("calculated forced_length_to_scattering = %lf, new RayAim \n", forced_length_to_scattering);
            print_position(direction, "direction");
            print_position(scattering_displacement, "scattering_displacement");
            print_position(forced_ray_scattering_point, "forced_ray_scattering_point");
            print_position(ray_position_geometry, "ray_position_geometry");
            print_position(this_focus_data->RayAim, "this_focus_data->RayAim");
            */

          } else {
            forced_length_to_scattering = -1.0; // Signals that no forcing needed, could also if on the selected process struct
          }

          int p_index;
          for (p_index = 0; p_index < Volumes[current_volume]->p_physics->number_of_processes; p_index++) { // GPU

            // Find correct focus_data_array index for this volume/process
            focus_data_index = Volumes[current_volume]->geometry.focus_array_indices.elements[p_index];
            this_focus_data = &Volumes[current_volume]->geometry.focus_data_array.elements[focus_data_index];

            if (Volumes[current_volume]->p_physics->p_scattering_array[p_index].non_isotropic_rot_index != -1) {
              // If the process is not isotropic, the wavevector is transformed into the local coordinate system of the process
              int non_isotropic_rot_index = Volumes[current_volume]->p_physics->p_scattering_array[p_index].non_isotropic_rot_index;
              wavevector_rotated = rot_apply (Volumes[current_volume]->geometry.process_rot_matrix_array[non_isotropic_rot_index], wavevector);
              coords_get (wavevector_rotated, &k_rotated[0], &k_rotated[1], &k_rotated[2]);

              if (Volumes[current_volume]->p_physics->p_scattering_array[p_index].needs_cross_section_focus == 1) {
                // Prepare focus data using ray_position_geometry of forced scattering point which will be prepared if any process needs cross_section time
                // focusing
                Coords ray_position_geometry_rotated
                    = rot_apply (Volumes[current_volume]->geometry.process_rot_matrix_array[non_isotropic_rot_index], ray_position_geometry);
                this_focus_data->RayAim = coords_sub (this_focus_data->Aim, ray_position_geometry_rotated); // Aim vector for this ray
                                                                                                            #ifdef Union_trace_verbal_setting
                printf ("Checking process number : %d, it was not isotropic, so RayAim updated \n", p_index);
                print_position (ray_position_geometry, "ray_position_geometry");
                print_position (ray_position_geometry_rotated, "ray_position_geometry_rotated");
                print_position (this_focus_data->RayAim, "this_focus_data->RayAim");
                #endif
              }

            } else {
              k_rotated[0] = k[0];
              k_rotated[1] = k[1];
              k_rotated[2] = k[2];
              // focus_data RayAim already updated for non isotropic processes
            }

            // Call the probability for scattering function assighed to this specific procress (the process pointer is updated in the for loop)
            process = &Volumes[current_volume]->p_physics->p_scattering_array[p_index]; // GPU Allowed

            int physics_output;
            physics_output = physics_my (process->eProcess, p_my_trace, k_rotated, process->data_transfer, this_focus_data, _particle);

            my_sum += *p_my_trace;
            #ifdef Union_trace_verbal_setting
            printf ("my_trace = %f, my_sum = %f\n", *p_my_trace, my_sum);
            #endif
            // increment the pointers so that it point to the next element (max number of process in any material is allocated)
            p_my_trace++;
          }

          #ifdef Union_trace_verbal_setting
          printf ("time_propagated_without_scattering = %f.\n", time_propagated_without_scattering);
          printf ("v_length                           = %f.\n", v_length);
          printf ("exp(- length_to_boundary*my_sum) = %f. length_to_boundary = %f. my_sum = %f.\n", exp (-length_to_boundary * my_sum), length_to_boundary,
                  my_sum);
          #endif

          my_sum_plus_abs = my_sum + Volumes[current_volume]->p_physics->my_a * (2200 / v_length);

          // New flow:length_to_boundary

          // Calculate if scattering happens based on my_sub_plus_abs
          if (my_sum < 1E-18) {
            scattering_event = 0;
          } else if (length_to_boundary < safety_distance2) {
            scattering_event = 0;
          } else {
            real_transmission_probability = exp (-length_to_boundary * my_sum_plus_abs);
            if (Volumes[current_volume]->geometry.geometry_p_interact != 0) {
              mc_transmission_probability = (1.0 - Volumes[current_volume]->geometry.geometry_p_interact);
              if ((scattering_event = (rand01 () > mc_transmission_probability))) {
                // Scattering event happens, this is the correction for the weight
                p *= (1.0 - real_transmission_probability) / (1.0 - mc_transmission_probability);
              } else {
                // Scattering event does not happen, this is the appropriate correction
                p *= real_transmission_probability / mc_transmission_probability;
              }
            } else {
              // probability to scatter is the natural value
              scattering_event = rand01 () > real_transmission_probability;
            }
          }

          // If scattering happens
          if (scattering_event == 1) {
            // Select scattering process

            // Adjust weight for absorption
            abs_weight_factor *= my_sum / my_sum_plus_abs;
            abs_weight_factor_set = 1;
            // Safety feature, alert in case of nonsense my results / negative absorption
            if (my_sum / my_sum_plus_abs > 1.0)
              printf ("WARNING: Absorption weight factor above 1! Should not happen! \n");
            // Select process
            if (Volumes[current_volume]->p_physics->number_of_processes == 1) { // trivial case
              // Select the only available process, which will always have index 0
              selected_process = 0;
            } else {
              if (Volumes[current_volume]->p_physics->interact_control == 1) {
                // Interact_fraction is used to influence the choice of process in this material
                mc_prop = rand01 ();
                culmative_probability = 0;
                total_process_interact = 1.0;

                // If any of the processes have probability 0, they are excluded from the selection
                for (iterator = 0; iterator < Volumes[current_volume]->p_physics->number_of_processes; iterator++) {
                  if (my_trace[iterator] < 1E-18) {
                    // When this happens, the total force probability is corrected and the probability for this particular instance is set to 0
                    total_process_interact -= Volumes[current_volume]->p_physics->p_scattering_array[iterator].process_p_interact;
                    my_trace_fraction_control[iterator] = 0;
                    // In cases where my_trace is not zero, the forced fraction is still used.
                  } else
                    my_trace_fraction_control[iterator] = Volumes[current_volume]->p_physics->p_scattering_array[iterator].process_p_interact;
                }
                // Randomly select a process using the weights stored in my_trace_fraction_control divided by total_process_interact
                for (iterator = 0; iterator < Volumes[current_volume]->p_physics->number_of_processes; iterator++) {
                  culmative_probability += my_trace_fraction_control[iterator] / total_process_interact;
                  if (culmative_probability > mc_prop) {
                    selected_process = iterator;
                    p *= (my_trace[iterator] / my_sum) * (total_process_interact / my_trace_fraction_control[iterator]);
                    break;
                  }
                }

              } else {
                // Select a process based on their relative attenuations factors
                mc_prop = rand01 ();
                culmative_probability = 0;
                for (iterator = 0; iterator < Volumes[current_volume]->p_physics->number_of_processes; iterator++) {
                  culmative_probability += my_trace[iterator] / my_sum;
                  if (culmative_probability > mc_prop) {
                    selected_process = iterator;
                    break;
                  }
                }
              }
            }

            // Select distance to scattering position
            if (Volumes[current_volume]->p_physics->p_scattering_array[selected_process].needs_cross_section_focus == 1) {
              // Respect forced length to scattering chosen by process
              length_to_scattering = forced_length_to_scattering;
              // Drawing between 0 and L from constant s = 1/L and should have been q = A*exp(-kz).
              // Normalizing A*exp(-kz) over 0 to L: A = k/(1-exp(-k*L))
              // Weight correction is ratio between s and q, L*A*exp(-kz) = L*k*exp(-kz)/(1-exp(-Lk))
              p *= length_to_boundary * my_sum_plus_abs * exp (-length_to_scattering * my_sum_plus_abs) / (1.0 - exp (-length_to_boundary * my_sum_plus_abs));
              #ifdef Union_trace_verbal_setting
              printf ("Used forced length to scattering, %lf \n", length_to_scattering);
              #endif

            } else {
              // Decided the ray scatters, choose where on truncated exponential from safety_distance to length_to_boundary - safety_distance
              length_to_scattering
                  = safety_distance - log (1.0 - rand0max ((1.0 - exp (-my_sum_plus_abs * (length_to_boundary - safety_distance2))))) / my_sum_plus_abs;
              #ifdef Union_trace_verbal_setting
              printf ("Sampled length to scattering, %lf \n", length_to_scattering);
              #endif
            }

          } // Done handling scattering

        } // Done handling situation where there are scattering processes in the material

      } // Done checking for scttering event and in case of scattering selecting a process

      // Record initial weight, absorption weight factor and initial position

      initial_weight = p;
      r_old[0] = r[0];
      r_old[1] = r[1];
      r_old[2] = r[2];
      time_old = t;
      // Apply absorption
      p *= abs_weight_factor;

      // Create event for absorption loggers
      // Need to use start position and length travelled to sample that trajectory for absorption event. Could do several, here just one.

      // min length: 0, max length: length_to_scattering if scattering, else length to boundary

      // Avoid logging absorption when the ray is in vacuum.
      if (current_volume != 0 && abs_weight_factor_set == 1) {    // Volume 0 is always vacuum, and if this is the current volume, an event will not occur
        if (Volumes[current_volume]->p_physics->is_vacuum == 0) { // No absorption in vacuum

          if (scattering_event == 1) {
            // When scattering events occur, place the absoprtion the same place (the total cross section is used to place it)
            abs_distance = length_to_scattering;
          } else {
            // When the ray exits a volume, the absorption position should be exponentially distributed using the total cross section
            my_abs = Volumes[current_volume]->p_physics->my_a * (2200 / v_length);
            abs_distance = -log (1.0 - rand0max (1.0 - exp (-my_sum_plus_abs * length_to_boundary))) / my_sum_plus_abs;
          }

          t_abs_propagation = abs_distance / v_length;

          abs_position = coords_set (x + t_abs_propagation * vx, y + t_abs_propagation * vy, z + t_abs_propagation * vz);

          // This info needs to be loaded into the absorption loggers

          // Need to run through relevant absorption loggers here
          #ifdef Union_trace_verbal_setting
          printf ("Running abs_logger system for specific volumes \n");
          #endif

          // Logging for detector components assosiated with this volume
          for (log_index = 0; log_index < Volumes[current_volume]->abs_loggers.num_elements; log_index++) {
            // Make transformation according to the individual position of the abs_logger? This would require position / rotation for all abs_loggers
            transformed_abs_position = coords_sub (abs_position, Volumes[current_volume]->abs_loggers.p_abs_logger[log_index]->position);
            transformed_abs_position = rot_apply (Volumes[current_volume]->abs_loggers.p_abs_logger[log_index]->rotation, transformed_abs_position);

            // This function calls a logger function which in turn stores some data among the passed, and possibly performs some basic data analysis
            Volumes[current_volume]->abs_loggers.p_abs_logger[log_index]->function_pointers.active_record_function (
                &transformed_abs_position, k_new, initial_weight * (1.0 - abs_weight_factor), t + t_abs_propagation, scattered_flag[current_volume],
                number_of_scattering_events, Volumes[current_volume]->abs_loggers.p_abs_logger[log_index], &abs_loggers_with_data_array);
            // If the logging component have a conditional attatched, the collected data will be written to a temporary place
            // At the end of the rays life, it will be checked if the condition is met
            //  if it is met, the temporary data is transfered to permanent, and temp is cleared.
            //  if it is not met, the temporary data is cleared.
          }

          #ifdef Union_trace_verbal_setting
          printf ("Running abs_logger system for all volumes \n");
          #endif
          for (log_index = 0; log_index < global_all_volume_abs_logger_list_master->num_elements; log_index++) {
            // As above, but on a global scale, meaning scattering in all volumes are logged

            // Problems with VN, PV, as there is no assosiated volume or process. The functions however need to have the same input to make the logger components
            // general. Could be interesting to have a monitor that just globally measurres the second scattering event in any volume (must be two in the same).
            // Weird but not meaningless.

            // Make transformation according to the individual position of the abs_logger? This would require position / rotation for all abs_loggers
            transformed_abs_position = coords_sub (abs_position, global_all_volume_abs_logger_list_master->elements[log_index].abs_logger->position);
            transformed_abs_position = rot_apply (global_all_volume_abs_logger_list_master->elements[log_index].abs_logger->rotation, transformed_abs_position);

            // Above version includes scattered_flag_VP, but selected_process may be undefined at this point.
            global_all_volume_abs_logger_list_master->elements[log_index].abs_logger->function_pointers.active_record_function (
                &transformed_abs_position, k_new, initial_weight * (1.0 - abs_weight_factor), t + t_abs_propagation, scattered_flag[current_volume],
                number_of_scattering_events, global_all_volume_abs_logger_list_master->elements[log_index].abs_logger, &abs_loggers_with_data_array);
          }
        }
      }

      if (scattering_event == 1) {
        #ifdef Union_trace_verbal_setting
        printf ("SCATTERING EVENT \n");
        printf ("current_volume            = %d \n", current_volume);
        printf ("r = (%f,%f,%f) v = (%f,%f,%f) \n", r[0], r[1], r[2], v[0], v[1], v[2]);
        #endif

        // Calculate the time to scattering
        time_to_scattering = length_to_scattering / v_length;

        #ifdef Union_trace_verbal_setting
        printf ("time to scattering        = %2.20f \n", time_to_scattering);
        printf ("length to boundery = %f, length to scattering = %f \n", length_to_boundary, length_to_scattering);
        #endif

        // May be replace by version without gravity
        // PROP_DT(time_to_scattering);

        // Reduce the double book keeping done here
        x += time_to_scattering * vx;
        y += time_to_scattering * vy;
        z += time_to_scattering * vz;
        t += time_to_scattering;
        r_start[0] = x;
        r_start[1] = y;
        r_start[2] = z;
        r[0] = x;
        r[1] = y;
        r[2] = z;
        ray_position = coords_set (x, y, z);
        ray_velocity = coords_set (vx, vy, vz);

        // Safe check that should be unecessary. Used to fine tune how close to the edge of a volume a scattering event is allowed to take place (1E-14 m away
        // currently).
        if (r_within_function (ray_position, &Volumes[current_volume]->geometry) == 0) {
          printf ("\nERROR, propagated out of current volume instead of to a point within!\n");
          printf ("length_to_scattering_specified = %2.20f\n             length propagated = %2.20f\n            length_to_boundary = %2.20f \n   "
                  "current_position = (%lf,%lf,%lf) \n",
                  length_to_scattering,
                  sqrt (time_to_scattering * time_to_scattering * vx * vx + time_to_scattering * time_to_scattering * vy * vy
                        + time_to_scattering * time_to_scattering * vz * vz),
                  length_to_boundary, x, y, z);

          volume_index = within_which_volume_GPU (ray_position, starting_lists.reduced_start_list, starting_lists.starting_destinations_list, Volumes,
                                                  &mask_status_list, number_of_volumes, pre_allocated1, pre_allocated2, pre_allocated3);

          printf ("Debug info: Volumes[current_volume]->name = %s, but now inside volume number %d named %s.\n", Volumes[current_volume]->name, volume_index,
                  Volumes[volume_index]->name);
          printf ("Ray absorbed \n");
          ABSORB;
        }

        // Save information before scattering event needed in logging section
        p_old = p;
        k_old[0] = k[0];
        k_old[1] = k[1];
        k_old[2] = k[2];

        // Find correct focus_data_array index for this volume/process and correct for ray position
        focus_data_index = Volumes[current_volume]->geometry.focus_array_indices.elements[selected_process];
        this_focus_data = &Volumes[current_volume]->geometry.focus_data_array.elements[focus_data_index];

        Coords ray_position_geometry = coords_sub (ray_position, Volumes[current_volume]->geometry.center); // ray_position relative to geometry center

        this_focus_data->RayAim = coords_sub (this_focus_data->Aim, ray_position_geometry); // Aim vector for this ray

        // Rotation to local process coordinate system (for non isotropic processes)
        if (Volumes[current_volume]->p_physics->p_scattering_array[selected_process].non_isotropic_rot_index != -1) {
          ray_velocity_rotated = rot_apply (
              Volumes[current_volume]
                  ->geometry.process_rot_matrix_array[Volumes[current_volume]->p_physics->p_scattering_array[selected_process].non_isotropic_rot_index],
              ray_velocity);

          Coords ray_position_geometry_rotated = rot_apply (
              Volumes[current_volume]
                  ->geometry.process_rot_matrix_array[Volumes[current_volume]->p_physics->p_scattering_array[selected_process].non_isotropic_rot_index],
              ray_position_geometry);
          this_focus_data->RayAim = coords_sub (this_focus_data->Aim, ray_position_geometry_rotated); // Aim vector for this ray
        } else {
          ray_velocity_rotated = ray_velocity;
          this_focus_data->RayAim = coords_sub (this_focus_data->Aim, ray_position_geometry); // Aim vector for this ray
        }
        #ifdef Union_trace_verbal_setting
        printf ("Kin: %g %g %g, selected_process: %i %i\n", k[0], k[1], k[2], selected_process, current_volume);
        coords_print (Volumes[current_volume]->geometry.focus_data_array.elements[0].Aim);
        #endif
        // test_physics_scattering(double *k_final, double *k_initial, union data_transfer_union data_transfer) {
        coords_get (coords_scalar_mult (ray_velocity_rotated, V2K), &k[0], &k[1], &k[2]);

        // I may replace a intial and final k with one instance that serves as both input and output
        process = &Volumes[current_volume]->p_physics->p_scattering_array[selected_process]; // CPU Only
        if (0 == physics_scattering (process->eProcess, k_new, k, &p, process->data_transfer, this_focus_data, _particle)) {
          /*
          // PowderN and Single_crystal requires the option of absorbing the neutron, which is weird. If there is a scattering probability, there should be a new
          direction.
          // It can arise from need to simplify sampling process and end up in cases where weight factor is 0, and the ray should be absorbed in these cases
          printf("ERROR: Union_master: %s.Absorbed ray because scattering function returned 0 (error/absorb)\n",NAME_CURRENT_COMP);
          component_error_msg++;
          if (component_error_msg > 100) {
            printf("To many errors encountered, exiting. \n");
            exit(1);
          }
          */
          ABSORB;
        }
        #ifdef Union_trace_verbal_setting
        printf ("Kout: %g %g %g\n", k_new[0], k_new[1], k_new[2]);
        #endif
        // Update velocity using k
        ray_velocity_rotated = coords_set (K2V * k_new[0], K2V * k_new[1], K2V * k_new[2]);

        // Transformation back to main coordinate system (maybe one should only do this when multiple scattering in that volume was over, especially if there is
        // only one non isotropic frame)
        if (Volumes[current_volume]->p_physics->p_scattering_array[selected_process].non_isotropic_rot_index != -1) {
          ray_velocity_final = rot_apply (
              Volumes[current_volume]
                  ->geometry.transpose_process_rot_matrix_array[Volumes[current_volume]->p_physics->p_scattering_array[selected_process].non_isotropic_rot_index],
              ray_velocity_rotated);
        } else {
          ray_velocity_final = ray_velocity_rotated;
        }
        #ifdef Union_trace_verbal_setting
        printf ("Final velocity vector ");
        coords_print (ray_velocity_final);
        #endif
        // Write velocity to global variable (temp, only really necessary at final)
        coords_get (ray_velocity_final, &vx, &vy, &vz);

        // Write velocity in array format as it is still used by intersect functions (temp, they need to be updated to ray_position / ray_velocity)
        v[0] = vx;
        v[1] = vy;
        v[2] = vz;
        v_length = sqrt (vx * vx + vy * vy + vz * vz);
        k_new[0] = V2K * vx;
        k_new[1] = V2K * vy;
        k_new[2] = V2K * vz;
        if (verbal)
          if (v_length < 1)
            printf ("velocity set to less than 1\n");
        ray_velocity = coords_set (vx, vy, vz);

        #ifdef Union_trace_verbal_setting
        printf ("Running logger system for specific volumes \n");
        #endif
        // Logging for detector components assosiated with this volume
        for (log_index = 0; log_index < Volumes[current_volume]->loggers.num_elements; log_index++) {
          if (Volumes[current_volume]->loggers.p_logger_volume[log_index].p_logger_process[selected_process] != NULL) {
            // Technically the scattering function could edit k, the wavevector before the scattering, even though there would be little point to doing that.
            // Could save a secure copy and pass that instead to be certain that no scattering process accidently tampers with the logging.

            // This function calls a logger function which in turn stores some data among the passed, and possibly performs some basic data analysis
            Volumes[current_volume]->loggers.p_logger_volume[log_index].p_logger_process[selected_process]->function_pointers.active_record_function (
                &ray_position, k_new, k_old, p, p_old, t, scattered_flag[current_volume], scattered_flag_VP[current_volume][selected_process],
                number_of_scattering_events, Volumes[current_volume]->loggers.p_logger_volume[log_index].p_logger_process[selected_process],
                &loggers_with_data_array);
            // If the logging component have a conditional attatched, the collected data will be written to a temporary place
            // At the end of the rays life, it will be checked if the condition is met
            //  if it is met, the temporary data is transfered to permanent, and temp is cleared.
            //  if it is not met, the temporary data is cleared.
          }
        }

        #ifdef Union_trace_verbal_setting
        printf ("Running logger system for all volumes \n");
        #endif
        for (log_index = 0; log_index < global_all_volume_logger_list_master->num_elements; log_index++) {
          // As above, but on a global scale, meaning scattering in all volumes are logged

          // Problems with VN, PV, as there is no assosiated volume or process. The functions however need to have the same input to make the logger components
          // general. Could be interesting to have a monitor that just globally measurres the second scattering event in any volume (must be two in the same).
          // Weird but not meaningless.
          global_all_volume_logger_list_master->elements[log_index].logger->function_pointers.active_record_function (
              &ray_position, k_new, k_old, p, p_old, t, scattered_flag[current_volume], scattered_flag_VP[current_volume][selected_process],
              number_of_scattering_events, global_all_volume_logger_list_master->elements[log_index].logger, &loggers_with_data_array);
        }
        #ifdef Union_trace_verbal_setting
        printf ("Outgoing event: %g %g %g // %g %g %g\n", x, y, z, vx, vy, vz);
        #endif
        SCATTER;
        ++number_of_scattering_events;
        ++scattered_flag[current_volume];
        ++scattered_flag_VP[current_volume][selected_process];

        // Clear intersection time lists as the direction of the ray has changed
        clear_intersection_table (&intersection_time_table);
        time_propagated_without_scattering = 0.0;
        #ifdef Union_trace_verbal_setting
        printf ("SCATTERED SUCSSESFULLY \n");
        printf ("r = (%f,%f,%f) v = (%f,%f,%f) \n", x, y, z, vx, vy, vz);

        if (enable_tagging && stop_tagging_ray == 0)
          printf ("Before new process node: current_tagging_node->intensity = %f\n", current_tagging_node->intensity);
        if (enable_tagging && stop_tagging_ray == 0)
          printf ("Before new process node: current_tagging_node->number_of_rays = %d\n", current_tagging_node->number_of_rays);
        #endif

        if (enable_tagging && stop_tagging_ray == 0)
          current_tagging_node = goto_process_node (current_tagging_node, selected_process, Volumes[current_volume], &stop_tagging_ray, stop_creating_nodes);

        #ifdef Union_trace_verbal_setting

        if (enable_tagging && stop_tagging_ray == 0)
          printf ("After new process node: current_tagging_node->intensity = %f\n", current_tagging_node->intensity);
        if (enable_tagging && stop_tagging_ray == 0)
          printf ("After new process node: current_tagging_node->number_of_rays = %d\n", current_tagging_node->number_of_rays);
        #endif

      } else {
        previous_volume = current_volume; // Record the current volume as previous, as this will change in this branch of the code

        #ifdef Union_trace_verbal_setting
        printf ("Propagate out of volume %d\n", current_volume);
        printf ("r = (%f,%f,%f) v = (%f,%f,%f) \n", x, y, z, vx, vy, vz);
        #endif
        // Propagate neutron to found minimum time
        // PROP_DT(time_to_boundery);
        x += time_to_boundery * vx;
        y += time_to_boundery * vy;
        z += time_to_boundery * vz;
        t += time_to_boundery;
        r[0] = x;
        r[1] = y;
        r[2] = z;
        ray_position = coords_set (x, y, z);
        ray_velocity = coords_set (vx, vy, vz);

        time_propagated_without_scattering = min_intersection_time;
        SCATTER; // For debugging purposes
                 #ifdef Union_trace_verbal_setting
        printf ("r = (%f,%f,%f) v = (%f,%f,%f) \n", x, y, z, vx, vy, vz);
        #endif
        // Remove this entry from the intersection_time_table
        intersection_time_table.intersection_times[min_volume][min_solution] = -1;

        // Use destination list for corresponding intersection entry n,i) to find next volume
        #ifdef Union_trace_verbal_setting
        printf ("PROPAGATION FROM VOLUME %d \n", current_volume);
        #endif
        if (min_volume == current_volume) {
          #ifdef Union_trace_verbal_setting
          printf ("min_volume == current_volume \n");
          #endif
          // List approach to finding the next volume.
          // When the ray intersects the current volume, the next volume must be on the destination list of the current volume
          // However, the reduced_destination_list can be investigated first, and depending on the results, the
          //  direct children of the volumes on the reduced destination list are investigated.
          // In the worst case, all direct children are investigated, which is eqvivalent to the entire destination list.
          // There is however a certain overhead in the logic needed to set up this tree, avoid duplicates of direct children, and so on.
          // This method is only faster than just checking the destination list when there are direct children (nested structures),
          //  but in general the tree method scales better with complexity, and is only slightly slower in simple cases.

          if (Volumes[current_volume]->geometry.destinations_list.num_elements == 1)
            tree_next_volume = Volumes[current_volume]->geometry.destinations_list.elements[0];
          else {
            ray_position = coords_set (x, y, z);
            ray_velocity = coords_set (vx, vy, vz);
            tree_next_volume = within_which_volume_GPU (ray_position, Volumes[current_volume]->geometry.reduced_destinations_list,
                                                        Volumes[current_volume]->geometry.destinations_list, Volumes, &mask_status_list, number_of_volumes,
                                                        pre_allocated1, pre_allocated2, pre_allocated3);
          }

          #ifdef Union_trace_verbal_setting
          if (enable_tagging)
            printf ("tree method moves from %d to %d\n", current_volume, tree_next_volume);

          if (enable_tagging && stop_tagging_ray == 0)
            printf ("Before new tree volume node: current_tagging_node->intensity = %f\n", current_tagging_node->intensity);
          if (enable_tagging && stop_tagging_ray == 0)
            printf ("Before new tree volume node: current_tagging_node->number_of_rays = %d\n", current_tagging_node->number_of_rays);
          #endif

          if (enable_tagging && stop_tagging_ray == 0)
            current_tagging_node = goto_volume_node (current_tagging_node, current_volume, tree_next_volume, Volumes, &stop_tagging_ray, stop_creating_nodes);

          #ifdef Union_trace_verbal_setting
          if (enable_tagging && stop_tagging_ray == 0)
            printf ("After new tree volume node: current_tagging_node->intensity = %f\n", current_tagging_node->intensity);
          if (enable_tagging && stop_tagging_ray == 0)
            printf ("After new tree volume node: current_tagging_node->number_of_rays = %d\n", current_tagging_node->number_of_rays);
          #endif

          // Set next volume to the solution found in the tree method
          current_volume = tree_next_volume;
          update_current_mask_intersect_status (&current_mask_intersect_list_status, &mask_status_list, Volumes, &current_volume);
          #ifdef Union_trace_verbal_setting
          print_1d_int_list (current_mask_intersect_list_status, "Updated current_mask_intersect_list_status");
          #endif

        } else {
          #ifdef Union_trace_verbal_setting
          if (enable_tagging && stop_tagging_ray == 0)
            printf ("Before new intersection volume node: current_tagging_node->intensity = %f\n", current_tagging_node->intensity);
          if (enable_tagging && stop_tagging_ray == 0)
            printf ("Before new intersection volume node: current_tagging_node->number_of_rays = %d\n", current_tagging_node->number_of_rays);
          #endif

          // Mask update: If the min_volume is not a mask, things are simple, current_volume = min_volume.
          //               however, if it is a mask, the mask status will switch.
          //               if the mask status becomes one, the masked volumes inside may be the next volume (unless they are children of the mask)
          //               if the mask status becomes zero (and the current volume is masked by min_volume), the destinations list of the mask is searched
          //               if the mask status becomes zero (and the current volume is NOT masked by min volume), the current volume doesn't change

          if (Volumes[min_volume]->geometry.is_mask_volume == 0) {
            #ifdef Union_trace_verbal_setting
            printf ("Min volume is not a mask, next volume = min volume\n");
            #endif
            if (enable_tagging && stop_tagging_ray == 0) {
              current_tagging_node = goto_volume_node (current_tagging_node, current_volume, min_volume, Volumes, &stop_tagging_ray, stop_creating_nodes);
            }
            current_volume = min_volume;
          } else {
            #ifdef Union_trace_verbal_setting
            printf ("Current volume is not a mask, complex decision tree\n");
            #endif
            if (mask_status_list.elements[Volumes[min_volume]->geometry.mask_index] == 1) {
              // We are leaving the mask, change the status
              #ifdef Union_trace_verbal_setting
              printf ("mask status changed from 1 to 0 as a mask is left\n");
              #endif
              mask_status_list.elements[Volumes[min_volume]->geometry.mask_index] = 0;
              // If the current volume is masked by this mask, run within_which_volume using the masks destination list, otherwise keep the current volume
              if (on_int_list (Volumes[current_volume]->geometry.masked_by_list, min_volume) == 1) {
                #ifdef Union_trace_verbal_setting
                printf ("The current volume was masked by this mask, and my need updating\n");
                #endif
                // In case of ANY mode, need to see if another mask on the masked_by list of the current volume is active, and if so, nothing happens
                need_to_run_within_which_volume = 1;
                if (Volumes[current_volume]->geometry.mask_mode == 2) {
                  for (mask_start = mask_check = Volumes[current_volume]->geometry.masked_by_mask_index_list.elements;
                       mask_check - mask_start < Volumes[current_volume]->geometry.masked_by_mask_index_list.num_elements; mask_check++) {
                    if (mask_status_list.elements[*mask_check] == 1) {
                      // Nothing needs to be done, the effective mask status of the current volume is still 1
                      need_to_run_within_which_volume = 0;
                      break;
                    }
                  }
                }
                if (need_to_run_within_which_volume == 1) {
                  #ifdef Union_trace_verbal_setting
                  printf ("The current volume was masked by this mask, and does need updating\n");
                  #endif
                  if (Volumes[min_volume]->geometry.destinations_list.num_elements == 1) {
                    #ifdef Union_trace_verbal_setting
                    printf ("Only one element in the destination tree of the mask\n");
                    #endif
                    // If there is only one element on the destinations list (quite common) there is no reason to run within_which_volume
                    // Instead the mask status is calculated here
                    if (Volumes[Volumes[min_volume]->geometry.destinations_list.elements[0]]->geometry.is_masked_volume == 1) {
                      #ifdef Union_trace_verbal_setting
                      printf ("The one element is however masked, so the mask status need to be calculated\n");
                      #endif
                      // figure out the effective mask status of this volume
                      if (Volumes[Volumes[min_volume]->geometry.destinations_list.elements[0]]->geometry.mask_mode == 2) { // ANY mask mode
                        tree_next_volume = 0;
                        for (mask_start = mask_check
                             = Volumes[Volumes[min_volume]->geometry.destinations_list.elements[0]]->geometry.masked_by_mask_index_list.elements;
                             mask_check - mask_start
                             < Volumes[Volumes[min_volume]->geometry.destinations_list.elements[0]]->geometry.masked_by_mask_index_list.num_elements;
                             mask_check++) {
                          if (mask_status_list.elements[*mask_check] == 1) {
                            tree_next_volume = Volumes[min_volume]->geometry.destinations_list.elements[0];
                            break;
                          }
                        }
                      } else { // ALL mask mode
                        tree_next_volume = Volumes[min_volume]->geometry.destinations_list.elements[0];
                        for (mask_start = mask_check
                             = Volumes[Volumes[min_volume]->geometry.destinations_list.elements[0]]->geometry.masked_by_mask_index_list.elements;
                             mask_check - mask_start
                             < Volumes[Volumes[min_volume]->geometry.destinations_list.elements[0]]->geometry.masked_by_mask_index_list.num_elements;
                             mask_check++) {
                          if (mask_status_list.elements[*mask_check] == 0) {
                            tree_next_volume = 0;
                            break;
                          }
                        }
                      }
                    } else
                      tree_next_volume = Volumes[min_volume]->geometry.destinations_list.elements[0];
                    #ifdef Union_trace_verbal_setting
                    printf ("The method found the next tree volume to be %d\n", tree_next_volume);
                    #endif
                    if (enable_tagging && stop_tagging_ray == 0)
                      current_tagging_node
                          = goto_volume_node (current_tagging_node, current_volume, tree_next_volume, Volumes, &stop_tagging_ray, stop_creating_nodes);
                    current_volume = tree_next_volume;
                  } else {
                    #ifdef Union_trace_verbal_setting
                    printf ("Many elements in destinations list, use within_which_volume\n");
                    #endif
                    ray_position = coords_set (x, y, z);
                    ray_velocity = coords_set (vx, vy, vz);
                    tree_next_volume = within_which_volume_GPU (ray_position, Volumes[min_volume]->geometry.reduced_destinations_list,
                                                                Volumes[min_volume]->geometry.destinations_list, Volumes, &mask_status_list, number_of_volumes,
                                                                pre_allocated1, pre_allocated2, pre_allocated3);

                    if (enable_tagging && stop_tagging_ray == 0)
                      current_tagging_node
                          = goto_volume_node (current_tagging_node, current_volume, tree_next_volume, Volumes, &stop_tagging_ray, stop_creating_nodes);
                    current_volume = tree_next_volume;
                    #ifdef Union_trace_verbal_setting
                    printf ("Set new new volume to %d\n", tree_next_volume);
                    #endif
                  }
                } else {
                  #ifdef Union_trace_verbal_setting
                  printf ("Did not need updating, as another mask was covering the volume\n");
                  #endif
                }
              }

            } else {
              // Here beccause the mask status of the mask that is intersected was 0, and it is thus switched to 1
              mask_status_list.elements[Volumes[min_volume]->geometry.mask_index] = 1;
              // When entering a mask, the new highest priority volume may be one of the masked volumes, if not we keep the current volume
              ray_position = coords_set (x, y, z);
              ray_velocity = coords_set (vx, vy, vz);
              // Bug found on the 2/9 2016, the destinations_list of a mask does not contain the volumes inside it. Could make an additional list for this.
              // The temporary fix will be to use the mask list for both reduced destinations list and destinations list.
              tree_next_volume = within_which_volume_GPU (ray_position, Volumes[min_volume]->geometry.mask_list, Volumes[min_volume]->geometry.mask_list, Volumes,
                                                          &mask_status_list, number_of_volumes, pre_allocated1, pre_allocated2, pre_allocated3);
              // if within_which_volume returns 0, no result was found (volume 0 can not be masked, so it could not be on the mask list)
              if (tree_next_volume != 0) {
                if (Volumes[tree_next_volume]->geometry.priority_value > Volumes[current_volume]->geometry.priority_value) {
                  // In case the current volume has a higher priority, nothing happens, otherwise change current volume
                  if (enable_tagging && stop_tagging_ray == 0)
                    current_tagging_node
                        = goto_volume_node (current_tagging_node, current_volume, tree_next_volume, Volumes, &stop_tagging_ray, stop_creating_nodes);
                  current_volume = tree_next_volume;
                }
              }
            }
          }

          // Regardless of the outcome of the above code, either the mask status or current volume have changed, and thus a effective mask update is needed.
          update_current_mask_intersect_status (&current_mask_intersect_list_status, &mask_status_list, Volumes, &current_volume);
          #ifdef Union_trace_verbal_setting
          print_1d_int_list (mask_status_list, "Updated mask status list");
          print_1d_int_list (current_mask_intersect_list_status, "Updated current_mask_intersect_list_status");
          if (enable_tagging)
            printf ("After new intersection volume node: current_tagging_node->intensity = %f\n", current_tagging_node->intensity);
          if (enable_tagging)
            printf ("After new intersection volume node: current_tagging_node->number_of_rays = %d\n", current_tagging_node->number_of_rays);
          #endif
        }
        #ifdef Union_trace_verbal_setting
        printf (" TO VOLUME %d \n", current_volume);
        #endif

        double n1, n2;
        int perform_refraction = 1;
        double nx;
        double ny;
        double nz;
        double normal_vector[3];

        if (previous_volume
            != current_volume) { // With masks, it can happen that the ray takes an extra iteration within the same volume, can skip surface / refraction

          double surface_wavevector_before[3];
          double surface_wavevector[3];

          nx = intersection_time_table.normal_vector_x[min_volume][min_solution];
          ny = intersection_time_table.normal_vector_y[min_volume][min_solution];
          nz = intersection_time_table.normal_vector_z[min_volume][min_solution];
          NORM (nx, ny, nz);

          // loop over surface processes
          // Need face index of both the geometry the ray is leaving and entering, only one from scattering

          // List of surfaces from geometry ray is leaving, bottom to top (length n_leaving)
          int relevant_surface_index, n_leaving, n_entering;
          struct surface_stack_struct* leaving_stack;
          struct surface_stack_struct* entering_stack;

          #ifdef Union_trace_verbal_setting
          printf (" Creating leaving surface stack %d \n", previous_volume);
          #endif

          if (previous_volume == 0) {
            n_leaving = 0; // surrounding vacuum has no stack
          } else {
            if (previous_volume == min_volume) {
              // ray left previous volume on a face of that volume, use that for the list
              relevant_surface_index = intersection_time_table.surface_index[min_volume][min_solution];
              leaving_stack = Volumes[previous_volume]->geometry.surface_stack_for_each_face[relevant_surface_index];
            } else {
              // ray left previous volume through an internal cut of some kind
              leaving_stack = Volumes[previous_volume]->geometry.internal_cut_surface_stack;
            }
            n_leaving = leaving_stack->number_of_surfaces;
          }

          #ifdef Union_trace_verbal_setting
          printf (" Created leaving surface stack for volume %d with %d effects \n", previous_volume, n_leaving);
          #endif

          if (current_volume == 0) {
            n_entering = 0;
          } else {

            // List of surfaces from geometry ray is entering, top to bottom (length n_entering)
            if (current_volume == min_volume) {
              // ray entered current volume on a face of that volume, use that for the list
              relevant_surface_index = intersection_time_table.surface_index[min_volume][min_solution];
              entering_stack = Volumes[current_volume]->geometry.surface_stack_for_each_face[relevant_surface_index];
            } else {
              // ray left previous volume through an internal cut of some kind
              entering_stack = Volumes[current_volume]->geometry.internal_cut_surface_stack;
            }
            n_entering = entering_stack->number_of_surfaces;
          }

          int n_total = n_leaving + n_entering;

          #ifdef Union_trace_verbal_setting
          printf (" Created entering surface stack for volume %d with %d effects. Combining into one surface stack \n", current_volume, n_entering);
          #endif

          // Only need to run surface system if there are any surface processes
          if (n_total > 0) {

            // Make combined list, leaving bottom to top followed by entering top to bottom
            // Stacks are naturally from bottom to top
            for (iterator = 0; iterator < n_total; iterator++) {
              if (iterator < n_leaving) {
                // grab from leaving stack in natural order
                interface_stack.p_surface_array[iterator] = leaving_stack->p_surface_array[iterator];
              } else {
                // grab from entering stack in reverse order
                interface_stack.p_surface_array[iterator] = entering_stack->p_surface_array[n_entering - iterator + n_leaving - 1];
              }
            }

            // struct surface_process_struct **surface_list; could do interface_struct like this instead

            int surface_transverse_index = 0;
            int surface_direction = 1;
            int surface_iterations = 0;
            int in_direction;
            int continues;
            enum in_or_out inward_or_outward;

            struct surface_process_struct* surface_pointer;

            surface_wavevector[0] = V2K * vx;
            surface_wavevector[1] = V2K * vy;
            surface_wavevector[2] = V2K * vz;
            surface_wavevector_before[0] = surface_wavevector[0];
            surface_wavevector_before[1] = surface_wavevector[1];
            surface_wavevector_before[2] = surface_wavevector[2];

            #ifdef Union_trace_verbal_setting
            double dot_product_before = nx * vx + ny * vy + nz * vz;
            printf (" Entering surface stack loop \n");
            printf ("  - normal dot v  = %lf \n", dot_product_before);
            #endif

            while (1) {

              #ifdef Union_trace_verbal_setting
              printf (" Start of surface stack with transverse_index = %d \n", surface_transverse_index);
              #endif

              // Escape conditions on each side of stack
              if (surface_transverse_index < 0) {
                // Escaped from incoming direction
                perform_refraction = 0;
                current_volume = previous_volume;

                #ifdef Union_trace_verbal_setting
                printf (" Left stack to incoming side \n");
                #endif

                break;
              }

              if (surface_transverse_index >= n_total) {
                // Went through all layers, continue to refraction as normal
                #ifdef Union_trace_verbal_setting
                printf (" Left stack by going all the way through \n");
                #endif
                break;
              }

              surface_pointer = interface_stack.p_surface_array[surface_transverse_index];

              if (surface_transverse_index < n_leaving)
                in_direction = 1;
              else
                in_direction = -1;
              if (surface_direction == in_direction)
                inward_or_outward = inward_bound;
              else
                inward_or_outward = outward_bound;

              // fresh normal in case surface function messed with it
              normal_vector[0] = nx;
              normal_vector[1] = ny;
              normal_vector[2] = nz;

              // p, wavevector and continues updated by surface_function
              #ifdef Union_trace_verbal_setting
              printf (" Running physics_surface with transverse_index = %d \n", surface_transverse_index);
              #endif
              physics_surface (surface_pointer, &p, surface_wavevector, &continues, normal_vector, inward_or_outward, _particle);
              #ifdef Union_trace_verbal_setting
              printf (" physics_surface reported continues = %d \n", continues);
              #endif

              // insert logging

              if (!continues)
                surface_direction = -surface_direction; // Flip stack direction if the ray does not continue

              surface_transverse_index += surface_direction; // Go to next element in stack
              surface_iterations += 1;

              if (surface_iterations > 10000) {
                printf ("ERROR: Ray stuck in surface stack, ABSORBED\n");
                done = 1;
                ray_sucseeded = 0;
                break;
              }
            }

            // If velocity was updated, register that to the ray and reset intersection memory
            // Hardcoded limit of 1E-10 m/s change in any direction
            if (fabs (surface_wavevector_before[0] - surface_wavevector[0]) > 1E-10 || fabs (surface_wavevector_before[1] - surface_wavevector[1]) > 1E-10
                || fabs (surface_wavevector_before[2] - surface_wavevector[2]) > 1E-10) {

              vx = surface_wavevector[0] * K2V;
              vy = surface_wavevector[1] * K2V;
              vz = surface_wavevector[2] * K2V;
              #ifdef Union_trace_verbal_setting
              printf (" ray direction updated by surface stack \n");
              printf ("r = (%f,%f,%f) v = (%f,%f,%f) \n", x, y, z, vx, vy, vz);
              printf ("  - normal dot v  = %lf \n", nx * vx + ny * vy + nz * vz);
              if (surface_transverse_index < 0)
                printf ("Should have different sign \n");
              if (surface_transverse_index >= n_total)
                printf ("Should have same sign \n");
              if (dot_product_before * (nx * vx + ny * vy + nz * vz) < 0) {
                printf ("Sign did change \n");
              } else
                printf ("Sign did not change \n");
              #endif

              // Report new velocity back,
              // Update velocity in all ways used in master
              v[0] = vx;
              v[1] = vy;
              v[2] = vz;
              v_length = sqrt (vx * vx + vy * vy + vz * vz);
              k_new[0] = V2K * vx;
              k_new[1] = V2K * vy;
              k_new[2] = V2K * vz;
              ray_velocity = coords_set (vx, vy, vz);

              ignore_closest = min_volume;
              ignore_surface_index = intersection_time_table.surface_index[min_volume][min_solution];

              // Since velocity is updated, we need to clear the intersection time table
              clear_intersection_table (&intersection_time_table);

              // Reset origin point for ray
              r_start[0] = x;
              r_start[1] = y;
              r_start[2] = z;
              time_propagated_without_scattering = 0.0;
            }
          }

          // Need old and current volume here to compare refraction index
          // Need information on normal vector for intersection
          // Can then change direction accordingly

          #ifdef Union_trace_verbal_setting
          printf ("Entering refraction system \n");
          printf ("r = (%f,%f,%f) v = (%f,%f,%f) \n", x, y, z, vx, vy, vz);
          printf ("n = (%f,%f,%f)\n", nx, ny, nz);
          double dot_product_before = nx * vx + ny * vy + nz * vz;
          printf ("normal dot v  = %lf \n", dot_product_before);
          printf ("min_volume = %d, min_solution = %d\n", min_volume, min_solution);
          #endif

          // Check if the volume intersection was found with returns normal, allowing refraction calculation
          // todo will implement differnet solution
          // if (Volumes[min_volume]->geometry.returns_intersection_normal == 0) perform_refraction = 0;

          v_length = sqrt (vx * vx + vy * vy + vz * vz);
          double lambda = 3956.0032 / v_length;

          if (previous_volume == 0)
            n1 = 1.0;
          else {
            if (Volumes[previous_volume]->p_physics->has_refraction_info == 0 && Volumes[previous_volume]->p_physics->is_vacuum == 0) {
              perform_refraction = 0;
            } else {

              if (Volumes[previous_volume]->p_physics->is_vacuum == 1) {
                n1 = 1.0;
              } else {
                n1 = sqrt (1.0 - (lambda * lambda * Volumes[previous_volume]->p_physics->refraction_scattering_length_density / PI));
              }

              // If the intersection is found with this volume, it is leaving, and the normal needs to be flipped
              if (previous_volume == min_volume) {
                nx *= -1;
                ny *= -1;
                nz *= -1;
              }
            }
          }

          if (current_volume == 0)
            n2 = 1.0;
          else {
            if (Volumes[current_volume]->p_physics->has_refraction_info == 0 && Volumes[current_volume]->p_physics->is_vacuum == 0) {
              perform_refraction = 0;
            } else {

              if (Volumes[current_volume]->p_physics->is_vacuum == 1) {
                n2 = 1.0;
              } else {
                n2 = sqrt (1.0 - (lambda * lambda * Volumes[current_volume]->p_physics->refraction_scattering_length_density / PI));
              }
            }
          }

          // Check if the two materials are the same, no need to check for refraction / reflection.
          if (perform_refraction == 1 && previous_volume != 0 && current_volume != 0) {
            if (strcmp (Volumes[previous_volume]->p_physics->name, Volumes[current_volume]->p_physics->name) == 0) {
              perform_refraction = 0;
            }
          }

          if (previous_volume == 0 && current_volume == 0) {
            perform_refraction = 0; // Vacuum to vacuum
          } else if (previous_volume == 0) {
            if (Volumes[current_volume]->p_physics->is_vacuum == 1)
              perform_refraction = 0; // Vacuum to vacuum
          } else if (current_volume == 0) {
            if (Volumes[previous_volume]->p_physics->is_vacuum == 1)
              perform_refraction = 0; // Vacuum to vacuum
          } else {
            if (Volumes[previous_volume]->p_physics->is_vacuum == 1 && Volumes[current_volume]->p_physics->is_vacuum == 1)
              perform_refraction = 0; // Vacuum to vacuum
          }

          #ifdef Union_trace_verbal_setting
          if (perform_refraction == 1)
            printf ("ready to calculate refraction, current_volume = %d, n1=%lf, n2=%lf \n", current_volume, n1, n2);
          else
            printf ("skipping refraction system, current_volume = %d, n1=%lf, n2=%lf \n", current_volume, n1, n2);
          #endif

          if (perform_refraction == 1) {

            double reflectivity;

            // Skipping roughness
            // if (RMS>0) Surface_wavyness(&nx, &ny, &nz, atan(2*RMS/lambda), _particle);

            Coords N = coords_set (nx, ny, nz);        // normal vector to surface
            Coords V = coords_set (vx, vy, vz);        // incoming velocity
            Coords I = coords_scale (V, 1 / v_length); // normalised ray = v/|v|

            // compute reflectivity
            double qc;
            double q_normal = fabs (2 * coords_sp (V, N) * V2Q);
            double q_qc_quadratic_diff;
            int use_fresnel;

            use_fresnel = 1;

            /* Reflectivity (see component Guide). */
            // StdReflecFunc(q_normal, par, &reflectivity);

            // Reflectivity calculation
            if (n2 / n1 < 1.0) {
              // qc exists
              qc = 4.0 * PI * sin (acos (n2 / n1)) / lambda;
              if (q_normal < qc) {
                reflectivity = 1.0;
                use_fresnel = 0;
              }
              // NEUTRON REFLECTION: PRINCIPLES AND EXAMPLES OF APPLICATIONS: Robert Cubitt and Giovanna Fragneto
              // This expression only works when a qc exists
              // q_qc_quadratic_diff = sqrt(q_normal*q_normal - qc*qc)
              // reflectivity = pow((q_normal - q_qc_quadratic_diff)/(q_normal + q_qc_quadratic_diff), 2);
            }

            if (use_fresnel) {
              // Fresnel law for both n1 > n2 and n1 < n2
              double term1, term2, R_perp, R_parallel;
              term1 = n1 * sqrt (1.0 - pow (lambda * q_normal / (4.0 * PI * n1), 2));
              term2 = n2 * sqrt (1.0 - pow (lambda * q_normal / (4.0 * PI * n2), 2));
              R_perp = ((term1 - term2) / (term1 + term2)) * ((term1 - term2) / (term1 + term2));
              R_parallel = ((term2 - term1) / (term2 + term1)) * ((term2 - term1) / (term2 + term1));
              reflectivity = 0.5 * (R_perp + R_parallel); // Unpolarized neutrons
            }

            double theta1, theta2;
            // theta1: incident angle to the surface normal
            double cos_theta1 = -coords_sp (N, I); // cos(theta1) = -N.I

            if (fabs (cos_theta1) > 1) {
              printf ("cos_theta1 > 1, Refraction error! Asborbed ray.\n");
              ABSORB; // should never occur...
            }
            theta1 = acos (cos_theta1) * RAD2DEG;

            // reflected ray: probability R
            // reflected beam:     I + 2cos(theta1).N
            Coords I_reflect = coords_add (I, coords_scale (N, 2.0 * cos_theta1));
            // reflected velocity: I_reflect.v
            Coords V_reflect = coords_scale (I_reflect, v_length);

            // compute refracted angle theta2...
            double sqr_cos_theta2 = 1.0 - (n1 / n2) * (n1 / n2) * (1.0 - cos_theta1 * cos_theta1);

            // now choose which one to use, and compute outgoing velocity
            if (0 < sqr_cos_theta2 && sqr_cos_theta2 < 1.0) {
              // refraction is possible

              // theta2: refracted angle to the surface normal
              double cos_theta2 = sqrt (sqr_cos_theta2);

              // select reflection (or refraction) from Monte-Carlo choice with probability R
              // in this case we expect R to be small (q > Qc)
              if (enable_reflection && 0.0 < reflectivity && reflectivity < 1.0 && rand01 () < reflectivity) {

                // choose reflection from MC
                theta2 = theta1;
                coords_get (V_reflect, &vx, &vy, &vz);

                current_volume = previous_volume; // Reflected, stays in current volume

                // Update velocity in all ways used in master
                v[0] = vx;
                v[1] = vy;
                v[2] = vz;
                v_length = sqrt (vx * vx + vy * vy + vz * vz);
                k_new[0] = V2K * vx;
                k_new[1] = V2K * vy;
                k_new[2] = V2K * vz;
                ray_velocity = coords_set (vx, vy, vz);

                #ifdef Union_trace_verbal_setting
                printf (" Refraction system : ray reflected, (branch 1) going back to volume %d\n", previous_volume);

                printf (" normal dot v  = %lf \n", nx * vx + ny * vy + nz * vz);
                if ((nx * vx + ny * vy + nz * vz) * dot_product_before > 0)
                  printf (" SIGN SHOULD HAVE CHANGED BUT DIDN'T \n");
                #endif

                ignore_closest = min_volume;
                ignore_surface_index = intersection_time_table.surface_index[min_volume][min_solution];

                // Since velocity is updated, we need to clear the intersection time table
                clear_intersection_table (&intersection_time_table);

                // Reset origin point for ray
                r_start[0] = x;
                r_start[1] = y;
                r_start[2] = z;
                time_propagated_without_scattering = 0.0;

              } else if (enable_refraction) {
                // compute refracted ray
                theta2 = acos (cos_theta2) * RAD2DEG;

                Coords I_refract = coords_add (coords_scale (I, n1 / n2), coords_scale (N, n1 / n2 * cos_theta1 + (cos_theta1 < 0 ? cos_theta2 : -cos_theta2)));
                Coords V_refract = coords_scale (I_refract, v_length);

                coords_get (V_refract, &vx, &vy, &vz);

                // Update velocity in all ways used in master
                v[0] = vx;
                v[1] = vy;
                v[2] = vz;
                v_length = sqrt (vx * vx + vy * vy + vz * vz);
                k_new[0] = V2K * vx;
                k_new[1] = V2K * vy;
                k_new[2] = V2K * vz;
                ray_velocity = coords_set (vx, vy, vz);

                #ifdef Union_trace_verbal_setting
                printf (" Refraction system : ray refracted, continues to to volume %d\n", current_volume);

                printf (" normal dot v  = %lf theta2 = %lf \n", nx * vx + ny * vy + nz * vz, theta2);
                if ((nx * vx + ny * vy + nz * vz) * dot_product_before < 0)
                  printf (" SIGN SHOULD NOT HAVE CHANGED BUT DID \n");
                #endif

                // Reflected can ignore some intersections in next geometry iteration
                ignore_closest = min_volume; // Ignore closest intersection in next geometry iteration
                ignore_surface_index = intersection_time_table.surface_index[min_volume][min_solution];

                // Since velocity is updated, we need to clear the intersection time table
                clear_intersection_table (&intersection_time_table);

                // Reset origin point for ray
                r_start[0] = x;
                r_start[1] = y;
                r_start[2] = z;
                time_propagated_without_scattering = 0.0;
              }
            } else if (enable_reflection) {
              // only reflection: below total reflection

              theta2 = theta1;
              if (0 < reflectivity && reflectivity < 1)
                p *= reflectivity; // should be R0
              coords_get (V_reflect, &vx, &vy, &vz);

              current_volume = previous_volume; // Reflected, stays in current volume

              // Update velocity in all ways used in master
              v[0] = vx;
              v[1] = vy;
              v[2] = vz;
              v_length = sqrt (vx * vx + vy * vy + vz * vz);
              k_new[0] = V2K * vx;
              k_new[1] = V2K * vy;
              k_new[2] = V2K * vz;
              ray_velocity = coords_set (vx, vy, vz);

              #ifdef Union_trace_verbal_setting
              printf (" Refraction system : ray reflected (branch 3), going back to volume %d\n", previous_volume);
              printf (" normal dot v  = %lf \n", nx * vx + ny * vy + nz * vz);
              if ((nx * vx + ny * vy + nz * vz) * dot_product_before > 0)
                printf (" SIGN SHOULD HAVE CHANGED BUT DIDN'T \n");
              #endif

              // Reflected can ignore some intersections in next geometry iteration
              ignore_closest = min_volume;
              ignore_surface_index = intersection_time_table.surface_index[min_volume][min_solution];
              // Since velocity is updated, we need to clear the intersection time table
              clear_intersection_table (&intersection_time_table);

              // Reset origin point for ray
              r_start[0] = x;
              r_start[1] = y;
              r_start[2] = z;
              time_propagated_without_scattering = 0.0;
            }
          }

          // todo: decide how surface on an exit volume should be treated
          if (Volumes[current_volume]->geometry.is_exit_volume == 1) {
            done = 1;          // Exit volumes allow the ray to escape the component
            ray_sucseeded = 1; // Allows the ray to leave loop
          }

          #ifdef Union_trace_verbal_setting
          printf ("After refraction system \n");
          printf ("r = (%f,%f,%f) v = (%f,%f,%f) \n", x, y, z, vx, vy, vz);
          printf ("  - normal_dot_v = %lf \n", nx * vx + ny * vy + nz * vz);
          printf ("CURRENT_VOLUME = %d \n", current_volume);
          #endif

        } // End of if (previous_volume != current_volume)
      } // End of scattering or propagation if

    } else { // Here because a shortest time is not found
      if (current_volume == 0) {
        done = 1;
        ray_sucseeded = 1;

      } else { // Check for errors (debugging phase)
        if (error_msg == 0) {
          component_error_msg++;
          ray_sucseeded = 0;
          done = 1; // stop the loop
          printf ("\n----------------------------------------------------------------------------------------------------\n");
          printf ("Union_master %s: Somehow reached a situation with no intersection time found, but still inside volume %d instead of 0\n", NAME_CURRENT_COMP,
                  current_volume);
          for (volume_index = 1; volume_index < number_of_volumes; volume_index++) {
            if (r_within_function (ray_position, &Volumes[volume_index]->geometry) == 1)
              printf ("The ray is in volume       %d\n", volume_index);
          }

          print_1d_int_list (mask_status_list, "mask status list");
          for (iterator = 0; iterator < number_of_volumes; iterator++)
            printf ("%d:%d - ", iterator, scattered_flag[iterator]);
          printf ("\n");
          printf ("r = (%f,%f,%f) v = (%f,%f,%f) \n", x, y, z, vx, vy, vz);

          printf ("Trace error number (%d/100) \n", component_error_msg);
        }
        error_msg++;

        if (component_error_msg > 100) {
          printf ("To many errors encountered, exiting. \n");
          // need ERROR FLAG to be read in finally which can warn the user of problems!
          exit (1);
        }
      }
    }

    if (limit == 0) {
      done = 1;
      ray_sucseeded = 0;
      printf ("Reached limit on number of interactions, and discarded the neutron, was in volume %d\n", current_volume);
      ABSORB;
    }
    #ifdef Union_trace_verbal_setting
    printf ("----------- END OF WHILE LOOP --------------------------------------\n");
    #endif
  }
  // Could move all add_statistics and similar to this point, but need to filter for failed rays
  if (ray_sucseeded == 1) {

    // Ray sucseeded, need to check status of conditionals
    #ifdef Union_trace_verbal_setting
    printf ("----------- logger loop --------------------------------------\n");
    #endif
    // Loggers attatched to specific volumes need to be handled with care to avoid looping over all loggers for every ray
    if (enable_conditionals == 1) {
      for (log_index = loggers_with_data_array.used_elements - 1; log_index > -1; log_index--) {
        // Check all conditionals attatched to the current logger
        this_logger = loggers_with_data_array.logger_pointers[log_index];
        conditional_status = 1;
        for (iterator = 0; iterator < loggers_with_data_array.logger_pointers[log_index]->conditional_list.num_elements; iterator++) {
          // Call this particular conditional. If it fails, report the status and break
          #ifdef Union_trace_verbal_setting
          printf ("Checking conditional number %d for logger named %s \n", iterator, loggers_with_data_array.logger_pointers[log_index]->name);
          #endif
          if (0
              == this_logger->conditional_list.conditional_functions[iterator](this_logger->conditional_list.p_data_unions[iterator], &ray_position,
                                                                               &ray_velocity, &p, &t, &current_volume, &number_of_scattering_events,
                                                                               scattered_flag, scattered_flag_VP)) {
            conditional_status = 0;
            break;
          }
        }
        if (conditional_status == 1) {
          // If a logger does not have a conditional, it will write directly to perm, and not even add it to the loggers_with_data_array, thus we know the
          // temp_to_perm function needs to be called The input for the temp_to_perm function is a pointer to the logger_data_union for the appropriate logger

          if (loggers_with_data_array.logger_pointers[log_index]->function_pointers.select_t_to_p == 1) {
            loggers_with_data_array.logger_pointers[log_index]->function_pointers.temp_to_perm (&loggers_with_data_array.logger_pointers[log_index]->data_union);
          } else if (loggers_with_data_array.logger_pointers[log_index]->function_pointers.select_t_to_p == 2) {
            loggers_with_data_array.logger_pointers[log_index]->function_pointers.temp_to_perm_final_p (
                &loggers_with_data_array.logger_pointers[log_index]->data_union, p);
          }

          // The user can set a condtional_extend_index, so that the evaluation of this specific conditional can be taken easily from extend
          if (loggers_with_data_array.logger_pointers[log_index]->logger_extend_index != -1) {
            #ifdef Union_trace_verbal_setting
            printf ("Updating logger_conditional_extend_array[%d] to 1 (max length = %d)\n",
                    loggers_with_data_array.logger_pointers[log_index]->logger_extend_index, max_conditional_extend_index);
            #endif
            logger_conditional_extend_array[loggers_with_data_array.logger_pointers[log_index]->logger_extend_index] = 1; // Can be reached from EXTEND
                                                                                                                          // Are all reset to 0 for each new ray
            #ifdef Union_trace_verbal_setting
            printf ("Updated extend index sucessfully\n");
            #endif
          }

          // Need to remove the current element from logger_with_data as it has been cleared and written to disk
          // The remaining elements is passed on to the next Union_master as it may fulfill the conditional after that master
          if (global_master_list_master->elements[global_master_list_master->num_elements - 1].component_index != INDEX_CURRENT_COMP) {
            // Move current logger pointer in logger_with_data to end position
            loggers_with_data_array.logger_pointers[log_index] = loggers_with_data_array.logger_pointers[loggers_with_data_array.used_elements - 1];
            // Decrease logger_with_data.used_elements with 1
            loggers_with_data_array.used_elements--;
          }
        }
      }

      // Perform the same loop with abs_loggers and their conditionals
      for (log_index = abs_loggers_with_data_array.used_elements - 1; log_index > -1; log_index--) {
        // Check all conditionals attatched to the current logger
        this_abs_logger = abs_loggers_with_data_array.abs_logger_pointers[log_index];
        conditional_status = 1;
        for (iterator = 0; iterator < abs_loggers_with_data_array.abs_logger_pointers[log_index]->conditional_list.num_elements; iterator++) {
          // Call this particular conditional. If it fails, report the status and break
          #ifdef Union_trace_verbal_setting
          printf ("Checking conditional number %d for abs logger named %s \n", iterator, abs_loggers_with_data_array.abs_logger_pointers[log_index]->name);
          #endif
          if (0
              == this_abs_logger->conditional_list.conditional_functions[iterator](this_abs_logger->conditional_list.p_data_unions[iterator], &ray_position,
                                                                                   &ray_velocity, &p, &t, &current_volume, &number_of_scattering_events,
                                                                                   scattered_flag, scattered_flag_VP)) {
            conditional_status = 0;
            break;
          }
        }
        if (conditional_status == 1) {
          // If a logger does not have a conditional, it will write directly to perm, and not even add it to the loggers_with_data_array, thus we know the
          // temp_to_perm function needs to be called The input for the temp_to_perm function is a pointer to the logger_data_union for the appropriate logger
          abs_loggers_with_data_array.abs_logger_pointers[log_index]->function_pointers.temp_to_perm (
              &abs_loggers_with_data_array.abs_logger_pointers[log_index]->data_union);

          // The user can set a condtional_extend_index, so that the evaluation of this specific conditional can be taken easily from extend
          if (abs_loggers_with_data_array.abs_logger_pointers[log_index]->abs_logger_extend_index != -1) {
            #ifdef Union_trace_verbal_setting
            printf ("Updating logger_conditional_extend_array[%d] to 1 (max length = %d)\n",
                    abs_loggers_with_data_array.abs_logger_pointers[log_index]->abs_logger_extend_index, max_conditional_extend_index);
            #endif
            abs_logger_conditional_extend_array[abs_loggers_with_data_array.abs_logger_pointers[log_index]->abs_logger_extend_index]
                = 1; // Can be reached from EXTEND
                     // Are all reset to 0 for each new ray
                     #ifdef Union_trace_verbal_setting
            printf ("Updated extend index sucessfully\n");
            #endif
          }

          // Need to remove the current element from logger_with_data as it has been cleared and written to disk
          // The remaining elements is passed on to the next Union_master as it may fulfill the conditional after that master
          if (global_master_list_master->elements[global_master_list_master->num_elements - 1].component_index != INDEX_CURRENT_COMP) {
            // Move current logger pointer in logger_with_data to end position
            abs_loggers_with_data_array.abs_logger_pointers[log_index]
                = abs_loggers_with_data_array.abs_logger_pointers[abs_loggers_with_data_array.used_elements - 1];
            // Decrease logger_with_data.used_elements with 1
            abs_loggers_with_data_array.used_elements--;
          }
        }
      }
    }

    if (enable_tagging && stop_tagging_ray == 0) {
      conditional_status = 1;
      for (iterator = 0; iterator < tagging_conditional_list->num_elements; iterator++) {
        // Call this particular conditional. If it fails, report the status and break
        // Since a conditional can work for a logger and master_tagging at the same time, it may be evaluated twice
        #ifdef Union_trace_verbal_setting
        printf ("Checking tagging conditional number %d\n", iterator);
        #endif
        if (0
            == tagging_conditional_list->conditional_functions[iterator](tagging_conditional_list->p_data_unions[iterator], &ray_position, &ray_velocity, &p, &t,
                                                                         &current_volume, &number_of_scattering_events, scattered_flag, scattered_flag_VP)) {
          conditional_status = 0;
          break;
        }
      }
      if (conditional_status == 1) {
        tagging_conditional_extend = 1;
        #ifdef Union_trace_verbal_setting
        printf ("Before adding statistics to node: current_tagging_nodbe->intensity = %f\n", current_tagging_node->intensity);
        printf ("Before adding statistics to node: current_tagging_node->number_of_rays = %d\n", current_tagging_node->number_of_rays);
        #endif

        add_statistics_to_node (current_tagging_node, &ray_position, &ray_velocity, &p, &tagging_leaf_counter);

        #ifdef Union_trace_verbal_setting
        printf ("After adding statistics to node: current_tagging_node->intensity = %f\n", current_tagging_node->intensity);
        printf ("After adding statistics to node: current_tagging_node->number_of_rays = %d\n", current_tagging_node->number_of_rays);
        #endif
      }
    }

    // Move the rays a nano meter away from the surface it left, in case activation counter > 1, this will prevent the ray from starting on a volume boundery
    x += vx * 1E-9;
    y += vy * 1E-9;
    z += vz * 1E-9;
    t += 1E-9;

  } else {
    ABSORB; // Absorb rays that didn't exit correctly for whatever reason
    // Could error log here
  }

  // Stores nubmer of scattering events in global master list so that another master with inherit_number_of_scattering_events can continue
  global_master_list_master->elements[this_global_master_index].stored_number_of_scattering_events = number_of_scattering_events;
#ifndef NOABSORB_INF_NAN
  /* Check for nan or inf particle parms */ 
  if(isnan(p + t + vx + vy + vz + x + y + z)) ABSORB;
  if(isinf(fabs(p) + fabs(t) + fabs(vx) + fabs(vy) + fabs(vz) + fabs(x) + fabs(y) + fabs(z))) ABSORB;
#else
  if(isnan(p)  ||  isinf(p)) printf("NAN or INF found in p,  %s (particle %lld)\n",_comp->_name,_particle->_uid);
  if(isnan(t)  ||  isinf(t)) printf("NAN or INF found in t,  %s (particle %lld)\n",_comp->_name,_particle->_uid);
  if(isnan(vx) || isinf(vx)) printf("NAN or INF found in vx, %s (particle %lld)\n",_comp->_name,_particle->_uid);
  if(isnan(vy) || isinf(vy)) printf("NAN or INF found in vy, %s (particle %lld)\n",_comp->_name,_particle->_uid);
  if(isnan(vz) || isinf(vz)) printf("NAN or INF found in vz, %s (particle %lld)\n",_comp->_name,_particle->_uid);
  if(isnan(x)  ||  isinf(x)) printf("NAN or INF found in x,  %s (particle %lld)\n",_comp->_name,_particle->_uid);
  if(isnan(y)  ||  isinf(y)) printf("NAN or INF found in y,  %s (particle %lld)\n",_comp->_name,_particle->_uid);
  if(isnan(z)  ||  isinf(z)) printf("NAN or INF found in z,  %s (particle %lld)\n",_comp->_name,_particle->_uid);
#endif
  #undef enable_refraction
  #undef enable_reflection
  #undef verbal
  #undef list_verbal
  #undef finally_verbal
  #undef allow_inside_start
  #undef enable_tagging
  #undef history_limit
  #undef enable_conditionals
  #undef inherit_number_of_scattering_events
  #undef weight_ratio_limit
  #undef init
  #undef global_positions_to_transform_list_master
  #undef global_rotations_to_transform_list_master
  #undef global_process_list_master
  #undef global_material_list_master
  #undef global_surface_list_master
  #undef global_geometry_list_master
  #undef global_all_volume_logger_list_master
  #undef global_specific_volumes_logger_list_master
  #undef global_all_volume_abs_logger_list_master
  #undef global_specific_volumes_abs_logger_list_master
  #undef global_tagging_conditional_list_master
  #undef global_master_list_master
  #undef starting_volume_warning
  #undef global_master_element
  #undef this_global_master_index
  #undef previous_master_index
  #undef geometry_list_index
  #undef intersection_time_table
  #undef Volumes
  #undef Geometries
  #undef Volume_copies
  #undef starting_lists
  #undef Volume_copies_allocated
  #undef r
  #undef r_start
  #undef v
  #undef error_msg
  #undef component_error_msg
  #undef string_output
  #undef number_of_volumes
  #undef volume_index
  #undef process_index
  #undef iterator
  #undef solutions
  #undef max_number_of_processes
  #undef limit
  #undef solution
  #undef min_solution
  #undef ignore_closest
  #undef ignore_surface_index
  #undef min_volume
  #undef time_found
  #undef intersection_time
  #undef min_intersection_time
  #undef process
  #undef process_start
  #undef my_trace
  #undef p_my_trace
  #undef my_trace_fraction_control
  #undef k
  #undef k_new
  #undef k_old
  #undef k_rotated
  #undef v_length
  #undef my_sum
  #undef my_sum_plus_abs
  #undef culmative_probability
  #undef mc_prop
  #undef time_to_scattering
  #undef length_to_scattering
  #undef length_to_boundary
  #undef time_to_boundery
  #undef selected_process
  #undef scattering_event
  #undef time_propagated_without_scattering
  #undef a_next_volume_found
  #undef next_volume
  #undef next_volume_priority
  #undef done
  #undef current_volume
  #undef previous_volume
  #undef ray_sucseeded
  #undef number_of_solutions
  #undef number_of_solutions_static
  #undef check
  #undef start
  #undef intersection_with_children
  #undef geometry_output
  #undef tree_next_volume
  #undef pre_allocated1
  #undef pre_allocated2
  #undef pre_allocated3
  #undef ray_position
  #undef ray_velocity
  #undef ray_velocity_rotated
  #undef ray_velocity_final
  #undef wavevector
  #undef wavevector_rotated
  #undef volume_0_found
  #undef scattered_flag
  #undef scattered_flag_VP
  #undef master_transposed_rotation_matrix
  #undef temp_rotation_matrix
  #undef temp_transpose_rotation_matrix
  #undef non_rotated_position
  #undef rotated_position
  #undef non_isotropic_found
  #undef master_tagging_node_list
  #undef current_tagging_node
  #undef tagging_leaf_counter
  #undef stop_tagging_ray
  #undef stop_creating_nodes
  #undef number_of_scattering_events
  #undef real_transmission_probability
  #undef mc_transmission_probability
  #undef number_of_process_interacts_set
  #undef index_of_lacking_process
  #undef total_process_interact
  #undef geometry_component_index_list
  #undef mask_volume_index_list
  #undef number_of_masks
  #undef number_of_masked_volumes
  #undef mask_status_list
  #undef current_mask_intersect_list_status
  #undef mask_index_main
  #undef mask_iterator
  #undef mask_start
  #undef mask_check
  #undef need_to_run_within_which_volume
  #undef number_of_processes_array
  #undef p_old
  #undef log_index
  #undef conditional_status
  #undef this_logger
  #undef this_abs_logger
  #undef tagging_conditional_list
  #undef logger_conditional_extend_array
  #undef abs_logger_conditional_extend_array
  #undef max_conditional_extend_index
  #undef tagging_conditional_extend
  #undef free_tagging_conditioanl_list
  #undef safety_distance
  #undef safety_distance2
  #undef temporary_focus_data
  #undef this_focus_data
  #undef focus_data_index
  #undef r_old
  #undef initial_weight
  #undef abs_weight_factor
  #undef time_old
  #undef absorption_index
  #undef abs_weight_factor_set
  #undef my_abs
  #undef absorption_event_data
  #undef abs_position
  #undef transformed_abs_position
  #undef t_abs_propagation
  #undef abs_distance
  #undef abs_max_length
  #undef longest_surface_stack
  #undef interface_stack
  return;
} /* class_Union_master_trace */

#pragma acc routine
void class_PSD_monitor_trace(_class_PSD_monitor *_comp
  , _class_particle *_particle) {
  ABSORBED=SCATTERED=RESTORE=0;
  #define nx (_comp->_parameters.nx)
  #define ny (_comp->_parameters.ny)
  #define filename (_comp->_parameters.filename)
  #define xmin (_comp->_parameters.xmin)
  #define xmax (_comp->_parameters.xmax)
  #define ymin (_comp->_parameters.ymin)
  #define ymax (_comp->_parameters.ymax)
  #define xwidth (_comp->_parameters.xwidth)
  #define yheight (_comp->_parameters.yheight)
  #define restore_neutron (_comp->_parameters.restore_neutron)
  #define nowritefile (_comp->_parameters.nowritefile)
  #define PSD_N (_comp->_parameters.PSD_N)
  #define PSD_p (_comp->_parameters.PSD_p)
  #define PSD_p2 (_comp->_parameters.PSD_p2)
  SIG_MESSAGE("[_detector_trace] component detector=PSD_monitor() TRACE [PSD_monitor:0]");

  PROP_Z0;
  if (x > xmin && x < xmax && y > ymin && y < ymax) {
    int i = floor ((x - xmin) * nx / (xmax - xmin));
    int j = floor ((y - ymin) * ny / (ymax - ymin));

    double p2 = p * p;
    #pragma acc atomic
    PSD_N[i][j] = PSD_N[i][j] + 1;

    #pragma acc atomic
    PSD_p[i][j] = PSD_p[i][j] + p;

    #pragma acc atomic
    PSD_p2[i][j] = PSD_p2[i][j] + p2;

    SCATTER;
  }
  if (restore_neutron) {
    RESTORE_NEUTRON (INDEX_CURRENT_COMP, x, y, z, vx, vy, vz, t, sx, sy, sz, p);
  }
#ifndef NOABSORB_INF_NAN
  /* Check for nan or inf particle parms */ 
  if(isnan(p + t + vx + vy + vz + x + y + z)) ABSORB;
  if(isinf(fabs(p) + fabs(t) + fabs(vx) + fabs(vy) + fabs(vz) + fabs(x) + fabs(y) + fabs(z))) ABSORB;
#else
  if(isnan(p)  ||  isinf(p)) printf("NAN or INF found in p,  %s (particle %lld)\n",_comp->_name,_particle->_uid);
  if(isnan(t)  ||  isinf(t)) printf("NAN or INF found in t,  %s (particle %lld)\n",_comp->_name,_particle->_uid);
  if(isnan(vx) || isinf(vx)) printf("NAN or INF found in vx, %s (particle %lld)\n",_comp->_name,_particle->_uid);
  if(isnan(vy) || isinf(vy)) printf("NAN or INF found in vy, %s (particle %lld)\n",_comp->_name,_particle->_uid);
  if(isnan(vz) || isinf(vz)) printf("NAN or INF found in vz, %s (particle %lld)\n",_comp->_name,_particle->_uid);
  if(isnan(x)  ||  isinf(x)) printf("NAN or INF found in x,  %s (particle %lld)\n",_comp->_name,_particle->_uid);
  if(isnan(y)  ||  isinf(y)) printf("NAN or INF found in y,  %s (particle %lld)\n",_comp->_name,_particle->_uid);
  if(isnan(z)  ||  isinf(z)) printf("NAN or INF found in z,  %s (particle %lld)\n",_comp->_name,_particle->_uid);
#endif
  #undef nx
  #undef ny
  #undef filename
  #undef xmin
  #undef xmax
  #undef ymin
  #undef ymax
  #undef xwidth
  #undef yheight
  #undef restore_neutron
  #undef nowritefile
  #undef PSD_N
  #undef PSD_p
  #undef PSD_p2
  return;
} /* class_PSD_monitor_trace */

/* *****************************************************************************
* instrument 'Conditional_test' TRACE
***************************************************************************** */

#ifndef FUNNEL
#pragma acc routine
int raytrace(_class_particle* _particle) { /* single event propagation, called by mccode_main for Conditional_test:TRACE */

  /* init variables and counters for TRACE */
  #undef ABSORB0
  #undef ABSORB
  #define ABSORB0 do { DEBUG_ABSORB(); MAGNET_OFF; ABSORBED++;} while(0)
  #define ABSORB ABSORB0
  DEBUG_ENTER();
  DEBUG_STATE();
  _particle->flag_nocoordschange=0; /* Init */
  _class_particle _particle_save=*_particle;
  /* the main iteration loop for one incoming event */
  while (!ABSORBED) { /* iterate event until absorbed */
    /* send particle event to component instance, one after the other */
    /* begin component init=Union_init() [1] */
    if (!ABSORBED && _particle->_index == 1) {
      _particle->flag_nocoordschange=0; /* Reset if we came here from a JUMP */
      _particle->_index++;
    } /* end component init [1] */
    /* begin component Vanadium_incoherent=Incoherent_process() [2] */
    if (!ABSORBED && _particle->_index == 2) {
      _particle->flag_nocoordschange=0; /* Reset if we came here from a JUMP */
      _particle->_index++;
    } /* end component Vanadium_incoherent [2] */
    /* begin component Vanadium=Union_make_material() [3] */
    if (!ABSORBED && _particle->_index == 3) {
      _particle->flag_nocoordschange=0; /* Reset if we came here from a JUMP */
      _particle->_index++;
    } /* end component Vanadium [3] */
    /* begin component Al_incoherent=Incoherent_process() [4] */
    if (!ABSORBED && _particle->_index == 4) {
      _particle->flag_nocoordschange=0; /* Reset if we came here from a JUMP */
      _particle->_index++;
    } /* end component Al_incoherent [4] */
    /* begin component Al_Powder=Powder_process() [5] */
    if (!ABSORBED && _particle->_index == 5) {
      _particle->flag_nocoordschange=0; /* Reset if we came here from a JUMP */
      _particle->_index++;
    } /* end component Al_Powder [5] */
    /* begin component Al=Union_make_material() [6] */
    if (!ABSORBED && _particle->_index == 6) {
      _particle->flag_nocoordschange=0; /* Reset if we came here from a JUMP */
      _particle->_index++;
    } /* end component Al [6] */
    /* begin component Cu_incoherent=Incoherent_process() [7] */
    if (!ABSORBED && _particle->_index == 7) {
      _particle->flag_nocoordschange=0; /* Reset if we came here from a JUMP */
      _particle->_index++;
    } /* end component Cu_incoherent [7] */
    /* begin component Cu_Powder=Powder_process() [8] */
    if (!ABSORBED && _particle->_index == 8) {
      _particle->flag_nocoordschange=0; /* Reset if we came here from a JUMP */
      _particle->_index++;
    } /* end component Cu_Powder [8] */
    /* begin component Cu=Union_make_material() [9] */
    if (!ABSORBED && _particle->_index == 9) {
      _particle->flag_nocoordschange=0; /* Reset if we came here from a JUMP */
      _particle->_index++;
    } /* end component Cu [9] */
    /* begin component a1=Progress_bar() [10] */
    if (!_particle->flag_nocoordschange) { // flag activated by JUMP to pass coords change
      if (_a1_var._rotation_is_identity) {
        if(!_a1_var._position_relative_is_zero) {
          coords_get(coords_add(coords_set(x,y,z), _a1_var._position_relative),&x, &y, &z);
        }
      } else {
          mccoordschange(_a1_var._position_relative, _a1_var._rotation_relative, _particle);
      }
    }
    if (!ABSORBED && _particle->_index == 10) {
      _particle->flag_nocoordschange=0; /* Reset if we came here from a JUMP */
      _particle_save = *_particle;
      DEBUG_COMP(_a1_var._name);
      DEBUG_STATE();
      class_Progress_bar_trace(&_a1_var, _particle);
      if (_particle->_restore)
        particle_restore(_particle, &_particle_save);
      _particle->_index++;
      if (!ABSORBED) { DEBUG_STATE(); }
    } /* end component a1 [10] */
    /* begin component source=Source_div() [11] */
    if (!_particle->flag_nocoordschange) { // flag activated by JUMP to pass coords change
      if (_source_var._rotation_is_identity) {
        if(!_source_var._position_relative_is_zero) {
          coords_get(coords_add(coords_set(x,y,z), _source_var._position_relative),&x, &y, &z);
        }
      } else {
          mccoordschange(_source_var._position_relative, _source_var._rotation_relative, _particle);
      }
    }
    if (!ABSORBED && _particle->_index == 11) {
      _particle->flag_nocoordschange=0; /* Reset if we came here from a JUMP */
      _particle_save = *_particle;
      DEBUG_COMP(_source_var._name);
      DEBUG_STATE();
      class_Source_div_trace(&_source_var, _particle);
      if (_particle->_restore)
        particle_restore(_particle, &_particle_save);
      _particle->_index++;
      if (!ABSORBED) { DEBUG_STATE(); }
    } /* end component source [11] */
    /* begin component sample_pos=Arm() [12] */
    if (!ABSORBED && _particle->_index == 12) {
      _particle->flag_nocoordschange=0; /* Reset if we came here from a JUMP */
      _particle->_index++;
    } /* end component sample_pos [12] */
    /* begin component cryostat_wall=Union_cylinder() [13] */
    if (!ABSORBED && _particle->_index == 13) {
      _particle->flag_nocoordschange=0; /* Reset if we came here from a JUMP */
      _particle->_index++;
    } /* end component cryostat_wall [13] */
    /* begin component cryostat_wall_vacuum=Union_cylinder() [14] */
    if (!ABSORBED && _particle->_index == 14) {
      _particle->flag_nocoordschange=0; /* Reset if we came here from a JUMP */
      _particle->_index++;
    } /* end component cryostat_wall_vacuum [14] */
    /* begin component sample_sphere=Union_sphere() [15] */
    if (!ABSORBED && _particle->_index == 15) {
      _particle->flag_nocoordschange=0; /* Reset if we came here from a JUMP */
      _particle->_index++;
    } /* end component sample_sphere [15] */
    /* begin component sample_box=Union_box() [16] */
    if (!ABSORBED && _particle->_index == 16) {
      _particle->flag_nocoordschange=0; /* Reset if we came here from a JUMP */
      _particle->_index++;
    } /* end component sample_box [16] */
    /* begin component sample_cone=Union_cone() [17] */
    if (!ABSORBED && _particle->_index == 17) {
      _particle->flag_nocoordschange=0; /* Reset if we came here from a JUMP */
      _particle->_index++;
    } /* end component sample_cone [17] */
    /* begin component test_logger_1D=Union_logger_1D() [18] */
    if (!ABSORBED && _particle->_index == 18) {
      _particle->flag_nocoordschange=0; /* Reset if we came here from a JUMP */
      _particle->_index++;
    } /* end component test_logger_1D [18] */
    /* begin component test_logger_2D_space_zx=Union_logger_2D_space() [19] */
    if (!ABSORBED && _particle->_index == 19) {
      _particle->flag_nocoordschange=0; /* Reset if we came here from a JUMP */
      _particle->_index++;
    } /* end component test_logger_2D_space_zx [19] */
    /* begin component test_logger_2D_space_zy=Union_logger_2D_space() [20] */
    if (!ABSORBED && _particle->_index == 20) {
      _particle->flag_nocoordschange=0; /* Reset if we came here from a JUMP */
      _particle->_index++;
    } /* end component test_logger_2D_space_zy [20] */
    /* begin component scat_direction=Arm() [21] */
    if (!ABSORBED && _particle->_index == 21) {
      _particle->flag_nocoordschange=0; /* Reset if we came here from a JUMP */
      _particle->_index++;
    } /* end component scat_direction [21] */
    /* begin component test_logger_2D_space_zx_con_time=Union_logger_2D_space() [22] */
    if (!ABSORBED && _particle->_index == 22) {
      _particle->flag_nocoordschange=0; /* Reset if we came here from a JUMP */
      _particle->_index++;
    } /* end component test_logger_2D_space_zx_con_time [22] */
    /* begin component test_conditional_standard=Union_conditional_standard() [23] */
    if (!ABSORBED && _particle->_index == 23) {
      _particle->flag_nocoordschange=0; /* Reset if we came here from a JUMP */
      _particle->_index++;
    } /* end component test_conditional_standard [23] */
    /* begin component test_logger_2D_space_zx_con_PSD=Union_logger_2D_space() [24] */
    if (!ABSORBED && _particle->_index == 24) {
      _particle->flag_nocoordschange=0; /* Reset if we came here from a JUMP */
      _particle->_index++;
    } /* end component test_logger_2D_space_zx_con_PSD [24] */
    /* begin component test_conditional_PSD=Union_conditional_PSD() [25] */
    if (!ABSORBED && _particle->_index == 25) {
      _particle->flag_nocoordschange=0; /* Reset if we came here from a JUMP */
      _particle->_index++;
    } /* end component test_conditional_PSD [25] */
    /* begin component sample=Union_master() [26] */
    if (!_particle->flag_nocoordschange) { // flag activated by JUMP to pass coords change
      if (_sample_var._rotation_is_identity) {
        if(!_sample_var._position_relative_is_zero) {
          coords_get(coords_add(coords_set(x,y,z), _sample_var._position_relative),&x, &y, &z);
        }
      } else {
          mccoordschange(_sample_var._position_relative, _sample_var._rotation_relative, _particle);
      }
    }
    if (!ABSORBED && _particle->_index == 26) {
      _particle->flag_nocoordschange=0; /* Reset if we came here from a JUMP */
      _particle_save = *_particle;
      DEBUG_COMP(_sample_var._name);
      DEBUG_STATE();
      class_Union_master_trace(&_sample_var, _particle);
      if (_particle->_restore)
        particle_restore(_particle, &_particle_save);
      _particle->_index++;
      if (!ABSORBED) { DEBUG_STATE(); }
    } /* end component sample [26] */
    /* begin component detector=PSD_monitor() [27] */
    if (!_particle->flag_nocoordschange) { // flag activated by JUMP to pass coords change
      if (_detector_var._rotation_is_identity) {
        if(!_detector_var._position_relative_is_zero) {
          coords_get(coords_add(coords_set(x,y,z), _detector_var._position_relative),&x, &y, &z);
        }
      } else {
          mccoordschange(_detector_var._position_relative, _detector_var._rotation_relative, _particle);
      }
    }
    if (!ABSORBED && _particle->_index == 27) {
      _particle->flag_nocoordschange=0; /* Reset if we came here from a JUMP */
      _particle_save = *_particle;
      DEBUG_COMP(_detector_var._name);
      DEBUG_STATE();
      class_PSD_monitor_trace(&_detector_var, _particle);
      if (_particle->_restore)
        particle_restore(_particle, &_particle_save);
      _particle->_index++;
      if (!ABSORBED) { DEBUG_STATE(); }
    } /* end component detector [27] */
    /* begin component detector_scat=PSD_monitor() [28] */
    if (!_particle->flag_nocoordschange) { // flag activated by JUMP to pass coords change
      if (_detector_scat_var._rotation_is_identity) {
        if(!_detector_scat_var._position_relative_is_zero) {
          coords_get(coords_add(coords_set(x,y,z), _detector_scat_var._position_relative),&x, &y, &z);
        }
      } else {
          mccoordschange(_detector_scat_var._position_relative, _detector_scat_var._rotation_relative, _particle);
      }
    }
    if (!ABSORBED && _particle->_index == 28) {
      _particle->flag_nocoordschange=0; /* Reset if we came here from a JUMP */
      _particle_save = *_particle;
      DEBUG_COMP(_detector_scat_var._name);
      DEBUG_STATE();
      class_PSD_monitor_trace(&_detector_scat_var, _particle);
      if (_particle->_restore)
        particle_restore(_particle, &_particle_save);
      _particle->_index++;
      if (!ABSORBED) { DEBUG_STATE(); }
    } /* end component detector_scat [28] */
    /* begin component stop=Union_stop() [29] */
    if (!ABSORBED && _particle->_index == 29) {
      _particle->flag_nocoordschange=0; /* Reset if we came here from a JUMP */
      _particle->_index++;
    } /* end component stop [29] */
    if (_particle->_index > 29)
      ABSORBED++; /* absorbed when passed all components */
  } /* while !ABSORBED */

  DEBUG_LEAVE()
  particle_restore(_particle, &_particle_save);
  DEBUG_STATE()

  return(_particle->_index);
} /* raytrace */

/* loop to generate events and call raytrace() propagate them */
void raytrace_all(unsigned long long ncount, unsigned long seed) {

  /* CPU-loop */
  unsigned long long loops;
  loops = ceil((double)ncount/gpu_innerloop);
  /* if on GPU, printf has been globally nullified, re-enable here */
  #ifdef OPENACC
  #undef strlen
  #undef strcmp
  #undef exit
  #undef printf
  #undef sprintf
  #undef fprintf
  #endif

  #ifdef OPENACC
  if (ncount>gpu_innerloop) {
    printf("Defining %llu CPU loops around GPU kernel and adjusting ncount\n",loops);
    mcset_ncount(loops*gpu_innerloop);
  } else {
    #endif
    loops=1;
    gpu_innerloop = ncount;
    #ifdef OPENACC
  }
    #endif

  for (unsigned long long cloop=0; cloop<loops; cloop++) {
    #ifdef OPENACC
    if (loops>1) fprintf(stdout, "%d..", (int)cloop); fflush(stdout);
    #endif

    /* if on GPU, re-nullify printf */
     #ifdef OPENACC
     #undef strlen
     #undef strcmp
     #undef exit
     #undef printf
     #undef sprintf
     #undef fprintf
     #endif

    #pragma acc parallel loop num_gangs(numgangs) vector_length(vecsize)
    for (unsigned long pidx=0 ; pidx < gpu_innerloop ; pidx++) {
      _class_particle particleN = mcgenstate(); // initial particle
      _class_particle* _particle = &particleN;
      particleN._uid = pidx;
      #ifdef USE_MPI
      particleN._uid += mpi_node_rank * ncount; 
      #endif

      srandom(_hash((pidx+1)*(seed+1)));

      raytrace(_particle);
    } /* inner for */
    seed = seed+gpu_innerloop;
  } /* CPU for */
  /* if on GPU, printf has been globally nullified, re-enable here */
     #ifdef OPENACC
     #undef strlen
     #undef strcmp
     #undef exit
     #undef printf
     #undef sprintf
     #undef fprintf
     #endif
  MPI_MASTER(
  printf("*** TRACE end *** \n");
  );
} /* raytrace_all */

#endif //no-FUNNEL

#ifdef FUNNEL
// Alternative raytrace algorithm which iterates all particles through
// one component at the time, can remove absorbs from the next loop and
// switch between cpu/gpu.
void raytrace_all_funnel(unsigned long long ncount, unsigned long seed) {

  // set up outer (CPU) loop / particle batches
  unsigned long long loops;

  /* if on GPU, printf has been globally nullified, re-enable here */
   #ifdef OPENACC
   #undef strlen
   #undef strcmp
   #undef exit
   #undef printf
   #undef sprintf
   #undef fprintf
   #endif
  #ifdef OPENACC
  loops = ceil((double)ncount/gpu_innerloop);
  if (ncount>gpu_innerloop) {
    printf("Defining %llu CPU loops around kernel and adjusting ncount\n",loops);
    mcset_ncount(loops*gpu_innerloop);
  } else {
  #endif
    loops=1;
    gpu_innerloop = ncount;
  #ifdef OPENACC
  }
  #endif

  // create particles struct and pointer arrays (same memory used by all batches)
  _class_particle* particles = malloc(gpu_innerloop*sizeof(_class_particle));
  _class_particle* pbuffer = malloc(gpu_innerloop*sizeof(_class_particle));
  long livebatchsize = gpu_innerloop;

  #undef ABSORB0
  #undef ABSORB
  #define ABSORB0 do { DEBUG_ABSORB(); MAGNET_OFF; ABSORBED++; } while(0)
  #define ABSORB ABSORB0
  // outer loop / particle batches
  for (unsigned long long cloop=0; cloop<loops; cloop++) {
    if (loops>1) fprintf(stdout, "%d..", (int)cloop); fflush(stdout);

    // init particles
    #pragma acc parallel loop present(particles[0:livebatchsize])
    for (unsigned long pidx=0 ; pidx < livebatchsize ; pidx++) {
      // generate particle state, set loop index and seed
      particles[pidx] = mcgenstate();
      _class_particle* _particle = particles + pidx;
      _particle->_uid = pidx;
      #ifdef USE_MPI
      _particle->_uid += mpi_node_rank * ncount; 
      #endif
      srandom(_hash((pidx+1)*(seed+1))); // _particle->state usage built into srandom macro
    }

    // iterate components

    #pragma acc parallel loop present(particles[0:livebatchsize])
    for (unsigned long pidx=0 ; pidx < livebatchsize ; pidx++) {
      _class_particle* _particle = &particles[pidx];
      _class_particle _particle_save;

      // init
    if (!ABSORBED && _particle->_index == 1) {
        _particle->_index++;
      }

      // Vanadium_incoherent
    if (!ABSORBED && _particle->_index == 2) {
        _particle->_index++;
      }

      // Vanadium
    if (!ABSORBED && _particle->_index == 3) {
        _particle->_index++;
      }

      // Al_incoherent
    if (!ABSORBED && _particle->_index == 4) {
        _particle->_index++;
      }

      // Al_Powder
    if (!ABSORBED && _particle->_index == 5) {
        _particle->_index++;
      }

      // Al
    if (!ABSORBED && _particle->_index == 6) {
        _particle->_index++;
      }

      // Cu_incoherent
    if (!ABSORBED && _particle->_index == 7) {
        _particle->_index++;
      }

      // Cu_Powder
    if (!ABSORBED && _particle->_index == 8) {
        _particle->_index++;
      }

      // Cu
    if (!ABSORBED && _particle->_index == 9) {
        _particle->_index++;
      }

      // a1
    if (!ABSORBED && _particle->_index == 10) {
#ifndef MULTICORE
        if (_a1_var._rotation_is_identity)
          coords_get(coords_add(coords_set(x,y,z), _a1_var._position_relative),&x, &y, &z);
        else
#endif
          mccoordschange(_a1_var._position_relative, _a1_var._rotation_relative, _particle);
        _particle_save = *_particle;
        class_Progress_bar_trace(&_a1_var, _particle);
        if (_particle->_restore)
        particle_restore(_particle, &_particle_save);
        _particle->_index++;
      }

      // source
    if (!ABSORBED && _particle->_index == 11) {
#ifndef MULTICORE
        if (_source_var._rotation_is_identity)
          coords_get(coords_add(coords_set(x,y,z), _source_var._position_relative),&x, &y, &z);
        else
#endif
          mccoordschange(_source_var._position_relative, _source_var._rotation_relative, _particle);
        _particle_save = *_particle;
        class_Source_div_trace(&_source_var, _particle);
        if (_particle->_restore)
        particle_restore(_particle, &_particle_save);
        _particle->_index++;
      }

      // sample_pos
    if (!ABSORBED && _particle->_index == 12) {
        _particle->_index++;
      }

      // cryostat_wall
    if (!ABSORBED && _particle->_index == 13) {
        _particle->_index++;
      }

      // cryostat_wall_vacuum
    if (!ABSORBED && _particle->_index == 14) {
        _particle->_index++;
      }

      // sample_sphere
    if (!ABSORBED && _particle->_index == 15) {
        _particle->_index++;
      }

      // sample_box
    if (!ABSORBED && _particle->_index == 16) {
        _particle->_index++;
      }

      // sample_cone
    if (!ABSORBED && _particle->_index == 17) {
        _particle->_index++;
      }

      // test_logger_1D
    if (!ABSORBED && _particle->_index == 18) {
        _particle->_index++;
      }

      // test_logger_2D_space_zx
    if (!ABSORBED && _particle->_index == 19) {
        _particle->_index++;
      }

      // test_logger_2D_space_zy
    if (!ABSORBED && _particle->_index == 20) {
        _particle->_index++;
      }

      // scat_direction
    if (!ABSORBED && _particle->_index == 21) {
        _particle->_index++;
      }

      // test_logger_2D_space_zx_con_time
    if (!ABSORBED && _particle->_index == 22) {
        _particle->_index++;
      }

      // test_conditional_standard
    if (!ABSORBED && _particle->_index == 23) {
        _particle->_index++;
      }

      // test_logger_2D_space_zx_con_PSD
    if (!ABSORBED && _particle->_index == 24) {
        _particle->_index++;
      }

      // test_conditional_PSD
    if (!ABSORBED && _particle->_index == 25) {
        _particle->_index++;
      }
        #define JUMP_FUNNEL
    }

    #ifdef MULTICORE
    #pragma acc parallel loop device_type(host)
    #endif
    for (unsigned long pidx=0 ; pidx < livebatchsize ; pidx++) {
      _class_particle* _particle = &particles[pidx];
      _class_particle _particle_save;

      // sample
    if (!ABSORBED && _particle->_index == 26) {
#ifndef MULTICORE
        if (_sample_var._rotation_is_identity)
          coords_get(coords_add(coords_set(x,y,z), _sample_var._position_relative),&x, &y, &z);
        else
#endif
          mccoordschange(_sample_var._position_relative, _sample_var._rotation_relative, _particle);
        _particle_save = *_particle;
        class_Union_master_trace(&_sample_var, _particle);
        if (_particle->_restore)
        particle_restore(_particle, &_particle_save);
        _particle->_index++;
      }
    }

    #pragma acc parallel loop present(particles[0:livebatchsize])
    for (unsigned long pidx=0 ; pidx < livebatchsize ; pidx++) {
      _class_particle* _particle = &particles[pidx];
      _class_particle _particle_save;

      // detector
    if (!ABSORBED && _particle->_index == 27) {
#ifndef MULTICORE
        if (_detector_var._rotation_is_identity)
          coords_get(coords_add(coords_set(x,y,z), _detector_var._position_relative),&x, &y, &z);
        else
#endif
          mccoordschange(_detector_var._position_relative, _detector_var._rotation_relative, _particle);
        _particle_save = *_particle;
        class_PSD_monitor_trace(&_detector_var, _particle);
        if (_particle->_restore)
        particle_restore(_particle, &_particle_save);
        _particle->_index++;
      }

      // detector_scat
    if (!ABSORBED && _particle->_index == 28) {
#ifndef MULTICORE
        if (_detector_scat_var._rotation_is_identity)
          coords_get(coords_add(coords_set(x,y,z), _detector_scat_var._position_relative),&x, &y, &z);
        else
#endif
          mccoordschange(_detector_scat_var._position_relative, _detector_scat_var._rotation_relative, _particle);
        _particle_save = *_particle;
        class_PSD_monitor_trace(&_detector_scat_var, _particle);
        if (_particle->_restore)
        particle_restore(_particle, &_particle_save);
        _particle->_index++;
      }

      // stop
    if (!ABSORBED && _particle->_index == 29) {
        _particle->_index++;
      }

    }

    // jump to next viable seed
    seed = seed + gpu_innerloop;
  } // outer loop / particle batches

  free(particles);
  free(pbuffer);

  printf("\n");
} /* raytrace_all_funnel */
#endif // FUNNEL

#undef x
#undef y
#undef z
#undef vx
#undef vy
#undef vz
#undef t
#undef sx
#undef sy
#undef sz
#undef p
#undef mcgravitation
#undef mcMagnet
#undef allow_backprop
#undef _mctmp_a
#undef _mctmp_b
#undef _mctmp_c
#ifdef OPENACC
#undef strlen
#undef strcmp
#undef exit
#undef printf
#undef sprintf
#undef fprintf
#endif
#undef SCATTERED
#undef RESTORE
#undef RESTORE_NEUTRON
#undef STORE_NEUTRON
#undef ABSORBED
#undef ABSORB
#undef ABSORB0
/* *****************************************************************************
* instrument 'Conditional_test' and components SAVE
***************************************************************************** */

_class_Progress_bar *class_Progress_bar_save(_class_Progress_bar *_comp
) {
  #define profile (_comp->_parameters.profile)
  #define percent (_comp->_parameters.percent)
  #define flag_save (_comp->_parameters.flag_save)
  #define minutes (_comp->_parameters.minutes)
  #define IntermediateCnts (_comp->_parameters.IntermediateCnts)
  #define StartTime (_comp->_parameters.StartTime)
  #define EndTime (_comp->_parameters.EndTime)
  #define CurrentTime (_comp->_parameters.CurrentTime)
  #define infostring (_comp->_parameters.infostring)
  SIG_MESSAGE("[_a1_save] component a1=Progress_bar() SAVE [Progress_bar:0]");

  MPI_MASTER (fprintf (stdout, "\nSave [%s]\n", instrument_name););
  if (profile && strlen (profile) && strcmp (profile, "NULL") && strcmp (profile, "0")) {
    char filename[256];
    if (!strlen (profile) || !strcmp (profile, "NULL") || !strcmp (profile, "0"))
      strcpy (filename, instrument_name);
    else
      strcpy (filename, profile);
    DETECTOR_OUT_1D ("Intensity profiler", "Component index [1]", "Intensity", "prof", 1, mcNUMCOMP, mcNUMCOMP - 1, &(instrument->counter_N[1]),
                     &(instrument->counter_P[1]), &(instrument->counter_P2[1]), filename);
  }
  #undef profile
  #undef percent
  #undef flag_save
  #undef minutes
  #undef IntermediateCnts
  #undef StartTime
  #undef EndTime
  #undef CurrentTime
  #undef infostring
  return(_comp);
} /* class_Progress_bar_save */

_class_Union_logger_1D *class_Union_logger_1D_save(_class_Union_logger_1D *_comp
) {
  #define target_geometry (_comp->_parameters.target_geometry)
  #define target_process (_comp->_parameters.target_process)
  #define min_value (_comp->_parameters.min_value)
  #define max_value (_comp->_parameters.max_value)
  #define n1 (_comp->_parameters.n1)
  #define variable (_comp->_parameters.variable)
  #define filename (_comp->_parameters.filename)
  #define order_total (_comp->_parameters.order_total)
  #define order_volume (_comp->_parameters.order_volume)
  #define order_volume_process (_comp->_parameters.order_volume_process)
  #define logger_conditional_extend_index (_comp->_parameters.logger_conditional_extend_index)
  #define init (_comp->_parameters.init)
  #define loop_index (_comp->_parameters.loop_index)
  #define found_process (_comp->_parameters.found_process)
  #define specified_processes (_comp->_parameters.specified_processes)
  #define local_string (_comp->_parameters.local_string)
  #define accepted_processes (_comp->_parameters.accepted_processes)
  #define logger_list_element (_comp->_parameters.logger_list_element)
  #define accepted_volumes (_comp->_parameters.accepted_volumes)
  #define this_logger (_comp->_parameters.this_logger)
  #define this_storage (_comp->_parameters.this_storage)
  #define loggers_on_target_volume (_comp->_parameters.loggers_on_target_volume)
  #define target_volume (_comp->_parameters.target_volume)
  #define temp_string (_comp->_parameters.temp_string)
  SIG_MESSAGE("[_test_logger_1D_save] component test_logger_1D=Union_logger_1D() SAVE [Union_logger_1D:0]");

  // Write to disk
  DETECTOR_OUT_1D (this_storage.Detector_1D.title_string, this_storage.Detector_1D.string_axis, this_storage.Detector_1D.string_axis_value,
                   this_storage.Detector_1D.string_axis_short, this_storage.Detector_1D.min, this_storage.Detector_1D.max, this_storage.Detector_1D.bins,
                   &this_storage.Detector_1D.Array_N[0], &this_storage.Detector_1D.Array_p[0], &this_storage.Detector_1D.Array_p2[0],
                   this_storage.Detector_1D.Filename);
  #undef target_geometry
  #undef target_process
  #undef min_value
  #undef max_value
  #undef n1
  #undef variable
  #undef filename
  #undef order_total
  #undef order_volume
  #undef order_volume_process
  #undef logger_conditional_extend_index
  #undef init
  #undef loop_index
  #undef found_process
  #undef specified_processes
  #undef local_string
  #undef accepted_processes
  #undef logger_list_element
  #undef accepted_volumes
  #undef this_logger
  #undef this_storage
  #undef loggers_on_target_volume
  #undef target_volume
  #undef temp_string
  return(_comp);
} /* class_Union_logger_1D_save */

_class_Union_logger_2D_space *class_Union_logger_2D_space_save(_class_Union_logger_2D_space *_comp
) {
  #define target_geometry (_comp->_parameters.target_geometry)
  #define target_process (_comp->_parameters.target_process)
  #define D_direction_1 (_comp->_parameters.D_direction_1)
  #define D1_min (_comp->_parameters.D1_min)
  #define D1_max (_comp->_parameters.D1_max)
  #define n1 (_comp->_parameters.n1)
  #define D_direction_2 (_comp->_parameters.D_direction_2)
  #define D2_min (_comp->_parameters.D2_min)
  #define D2_max (_comp->_parameters.D2_max)
  #define n2 (_comp->_parameters.n2)
  #define filename (_comp->_parameters.filename)
  #define order_total (_comp->_parameters.order_total)
  #define order_volume (_comp->_parameters.order_volume)
  #define order_volume_process (_comp->_parameters.order_volume_process)
  #define logger_conditional_extend_index (_comp->_parameters.logger_conditional_extend_index)
  #define init (_comp->_parameters.init)
  #define loop_index (_comp->_parameters.loop_index)
  #define found_process (_comp->_parameters.found_process)
  #define specified_processes (_comp->_parameters.specified_processes)
  #define local_string (_comp->_parameters.local_string)
  #define accepted_processes (_comp->_parameters.accepted_processes)
  #define logger_list_element (_comp->_parameters.logger_list_element)
  #define accepted_volumes (_comp->_parameters.accepted_volumes)
  #define this_logger (_comp->_parameters.this_logger)
  #define this_storage (_comp->_parameters.this_storage)
  #define loggers_on_target_volume (_comp->_parameters.loggers_on_target_volume)
  #define target_volume (_comp->_parameters.target_volume)
  SIG_MESSAGE("[_test_logger_2D_space_zx_save] component test_logger_2D_space_zx=Union_logger_2D_space() SAVE [Union_logger_2D_space:0]");

  // Write to disk
  DETECTOR_OUT_2D (this_storage.Detector_2D.title_string, this_storage.Detector_2D.string_axis_1, this_storage.Detector_2D.string_axis_2,
                   this_storage.Detector_2D.D1min, this_storage.Detector_2D.D1max, this_storage.Detector_2D.D2min, this_storage.Detector_2D.D2max,
                   this_storage.Detector_2D.bins_1, this_storage.Detector_2D.bins_2, *this_storage.Detector_2D.Array_N, *this_storage.Detector_2D.Array_p,
                   *this_storage.Detector_2D.Array_p2, this_storage.Detector_2D.Filename);
  #undef target_geometry
  #undef target_process
  #undef D_direction_1
  #undef D1_min
  #undef D1_max
  #undef n1
  #undef D_direction_2
  #undef D2_min
  #undef D2_max
  #undef n2
  #undef filename
  #undef order_total
  #undef order_volume
  #undef order_volume_process
  #undef logger_conditional_extend_index
  #undef init
  #undef loop_index
  #undef found_process
  #undef specified_processes
  #undef local_string
  #undef accepted_processes
  #undef logger_list_element
  #undef accepted_volumes
  #undef this_logger
  #undef this_storage
  #undef loggers_on_target_volume
  #undef target_volume
  return(_comp);
} /* class_Union_logger_2D_space_save */

_class_PSD_monitor *class_PSD_monitor_save(_class_PSD_monitor *_comp
) {
  #define nx (_comp->_parameters.nx)
  #define ny (_comp->_parameters.ny)
  #define filename (_comp->_parameters.filename)
  #define xmin (_comp->_parameters.xmin)
  #define xmax (_comp->_parameters.xmax)
  #define ymin (_comp->_parameters.ymin)
  #define ymax (_comp->_parameters.ymax)
  #define xwidth (_comp->_parameters.xwidth)
  #define yheight (_comp->_parameters.yheight)
  #define restore_neutron (_comp->_parameters.restore_neutron)
  #define nowritefile (_comp->_parameters.nowritefile)
  #define PSD_N (_comp->_parameters.PSD_N)
  #define PSD_p (_comp->_parameters.PSD_p)
  #define PSD_p2 (_comp->_parameters.PSD_p2)
  SIG_MESSAGE("[_detector_save] component detector=PSD_monitor() SAVE [PSD_monitor:0]");

  if (!nowritefile) {
    DETECTOR_OUT_2D ("PSD monitor", "X position [cm]", "Y position [cm]", xmin * 100.0, xmax * 100.0, ymin * 100.0, ymax * 100.0, nx, ny, &PSD_N[0][0],
                     &PSD_p[0][0], &PSD_p2[0][0], filename);
  }
  #undef nx
  #undef ny
  #undef filename
  #undef xmin
  #undef xmax
  #undef ymin
  #undef ymax
  #undef xwidth
  #undef yheight
  #undef restore_neutron
  #undef nowritefile
  #undef PSD_N
  #undef PSD_p
  #undef PSD_p2
  return(_comp);
} /* class_PSD_monitor_save */



int save(FILE *handle) { /* called by mccode_main for Conditional_test:SAVE */
  if (!handle) siminfo_init(NULL);

  /* call iteratively all components SAVE */









  class_Progress_bar_save(&_a1_var);








  class_Union_logger_1D_save(&_test_logger_1D_var);

  class_Union_logger_2D_space_save(&_test_logger_2D_space_zx_var);

  class_Union_logger_2D_space_save(&_test_logger_2D_space_zy_var);


  class_Union_logger_2D_space_save(&_test_logger_2D_space_zx_con_time_var);


  class_Union_logger_2D_space_save(&_test_logger_2D_space_zx_con_PSD_var);



  class_PSD_monitor_save(&_detector_var);

  class_PSD_monitor_save(&_detector_scat_var);


  if (!handle) siminfo_close(); 

  return(0);
} /* save */

/* *****************************************************************************
* instrument 'Conditional_test' and components FINALLY
***************************************************************************** */

_class_Incoherent_process *class_Incoherent_process_finally(_class_Incoherent_process *_comp
) {
  #define sigma (_comp->_parameters.sigma)
  #define f_QE (_comp->_parameters.f_QE)
  #define gamma (_comp->_parameters.gamma)
  #define packing_factor (_comp->_parameters.packing_factor)
  #define unit_cell_volume (_comp->_parameters.unit_cell_volume)
  #define interact_fraction (_comp->_parameters.interact_fraction)
  #define init (_comp->_parameters.init)
  #define global_process_element (_comp->_parameters.global_process_element)
  #define This_process (_comp->_parameters.This_process)
  #define Incoherent_storage (_comp->_parameters.Incoherent_storage)
  #define effective_my_scattering (_comp->_parameters.effective_my_scattering)
  SIG_MESSAGE("[_Vanadium_incoherent_finally] component Vanadium_incoherent=Incoherent_process() FINALLY [Incoherent_process:0]");

  // Since the process and it's storage is a static allocation, there is nothing to deallocate
  #undef sigma
  #undef f_QE
  #undef gamma
  #undef packing_factor
  #undef unit_cell_volume
  #undef interact_fraction
  #undef init
  #undef global_process_element
  #undef This_process
  #undef Incoherent_storage
  #undef effective_my_scattering
  return(_comp);
} /* class_Incoherent_process_finally */

_class_Union_make_material *class_Union_make_material_finally(_class_Union_make_material *_comp
) {
  #define process_string (_comp->_parameters.process_string)
  #define my_absorption (_comp->_parameters.my_absorption)
  #define absorber (_comp->_parameters.absorber)
  #define refraction_density (_comp->_parameters.refraction_density)
  #define refraction_sigma_coh (_comp->_parameters.refraction_sigma_coh)
  #define refraction_weight (_comp->_parameters.refraction_weight)
  #define refraction_SLD (_comp->_parameters.refraction_SLD)
  #define init (_comp->_parameters.init)
  #define global_material_element (_comp->_parameters.global_material_element)
  #define this_material (_comp->_parameters.this_material)
  #define loop_index (_comp->_parameters.loop_index)
  #define found_process (_comp->_parameters.found_process)
  #define specified_processes (_comp->_parameters.specified_processes)
  #define local_string (_comp->_parameters.local_string)
  #define accepted_processes (_comp->_parameters.accepted_processes)
  SIG_MESSAGE("[_Vanadium_finally] component Vanadium=Union_make_material() FINALLY [Union_make_material:0]");

  // The elements of the scattering array used static allocation and is thus deallocated automatically
  if (this_material.number_of_processes > 0)
    free (this_material.p_scattering_array);
  if (accepted_processes.num_elements > 0)
    free (accepted_processes.elements);

  struct pointer_to_global_geometry_list* global_geometry_list = COMP_GETPAR3 (Union_init, init, global_geometry_list);
  struct pointer_to_global_master_list* global_master_list = COMP_GETPAR3 (Union_init, init, global_master_list);

  // Checking if any Union volumes are defined after the master component
  #ifdef MASTER_DETECTOR
  #ifdef ANY_GEOMETRY_DETECTOR_DECLARE
  #ifndef MASTER_DETECTOR_WARNING

  for (loop_index = 0; loop_index < global_geometry_list->num_elements; loop_index++) {
    if (global_geometry_list->elements[loop_index].component_index > global_master_list->elements[global_master_list->num_elements - 1].component_index) {
      printf ("WARNING: No Union_master component defined after Union volume named %s, this components did not affect the simulation in any way.\n",
              global_geometry_list->elements[loop_index].name);
    }
  }
  // Decided to have this as a warning without exiting the simulation
  // In order to only show this warning once, the MASTER_DETECTOR_WARNING is defined
  #define MASTER_DETECTOR_WARNING dummy
  #endif
  #endif
  #endif

  // Checking if the user remembered to put in a Union_master
  #ifndef MASTER_DETECTOR
  #ifdef ANY_GEOMETRY_DETECTOR_DECLARE
  #ifndef MASTER_DETECTOR_WARNING
  printf ("\nWARNING: No Union_master component used, these components did not affect the simulation in any way:\n");
  for (loop_index = 0; loop_index < global_geometry_list->num_elements; loop_index++)
    printf ("  %s\n", global_geometry_list->elements[loop_index].name);
  printf ("\n");
  // Decided to have this as a warning without exiting the simulation
  // In order to only show this warning once, the MASTER_DETECTOR_WARNING is defined
  #define MASTER_DETECTOR_WARNING dummy
  #endif
  #endif
  #endif
  #undef process_string
  #undef my_absorption
  #undef absorber
  #undef refraction_density
  #undef refraction_sigma_coh
  #undef refraction_weight
  #undef refraction_SLD
  #undef init
  #undef global_material_element
  #undef this_material
  #undef loop_index
  #undef found_process
  #undef specified_processes
  #undef local_string
  #undef accepted_processes
  return(_comp);
} /* class_Union_make_material_finally */

_class_Powder_process *class_Powder_process_finally(_class_Powder_process *_comp
) {
  #define reflections (_comp->_parameters.reflections)
  #define packing_factor (_comp->_parameters.packing_factor)
  #define Vc (_comp->_parameters.Vc)
  #define delta_d_d (_comp->_parameters.delta_d_d)
  #define DW (_comp->_parameters.DW)
  #define nb_atoms (_comp->_parameters.nb_atoms)
  #define d_phi (_comp->_parameters.d_phi)
  #define density (_comp->_parameters.density)
  #define weight (_comp->_parameters.weight)
  #define barns (_comp->_parameters.barns)
  #define Strain (_comp->_parameters.Strain)
  #define interact_fraction (_comp->_parameters.interact_fraction)
  #define format (_comp->_parameters.format)
  #define init (_comp->_parameters.init)
  #define global_process_element (_comp->_parameters.global_process_element)
  #define This_process (_comp->_parameters.This_process)
  #define Powder_storage (_comp->_parameters.Powder_storage)
  #define line_info (_comp->_parameters.line_info)
  #define effective_my_scattering (_comp->_parameters.effective_my_scattering)
  #define columns (_comp->_parameters.columns)
  SIG_MESSAGE("[_Al_Powder_finally] component Al_Powder=Powder_process() FINALLY [Powder_process:0]");

  free (line_info.list);
  free (line_info.q_v);
  free (line_info.w_v);
  free (line_info.my_s_v2);
  #undef reflections
  #undef packing_factor
  #undef Vc
  #undef delta_d_d
  #undef DW
  #undef nb_atoms
  #undef d_phi
  #undef density
  #undef weight
  #undef barns
  #undef Strain
  #undef interact_fraction
  #undef format
  #undef init
  #undef global_process_element
  #undef This_process
  #undef Powder_storage
  #undef line_info
  #undef effective_my_scattering
  #undef columns
  return(_comp);
} /* class_Powder_process_finally */

_class_Progress_bar *class_Progress_bar_finally(_class_Progress_bar *_comp
) {
  #define profile (_comp->_parameters.profile)
  #define percent (_comp->_parameters.percent)
  #define flag_save (_comp->_parameters.flag_save)
  #define minutes (_comp->_parameters.minutes)
  #define IntermediateCnts (_comp->_parameters.IntermediateCnts)
  #define StartTime (_comp->_parameters.StartTime)
  #define EndTime (_comp->_parameters.EndTime)
  #define CurrentTime (_comp->_parameters.CurrentTime)
  #define infostring (_comp->_parameters.infostring)
  SIG_MESSAGE("[_a1_finally] component a1=Progress_bar() FINALLY [Progress_bar:0]");

  time_t NowTime;
  time (&NowTime);
  fprintf (stdout, "\nFinally [%s: %s]. Time: ", instrument_name, dirname ? dirname : ".");
  if (difftime (NowTime, StartTime) < 60.0)
    fprintf (stdout, "%g [s] ", difftime (NowTime, StartTime));
  else if (difftime (NowTime, StartTime) > 3600.0)
    fprintf (stdout, "%g [h] ", difftime (NowTime, StartTime) / 3600.0);
  else
    fprintf (stdout, "%g [min] ", difftime (NowTime, StartTime) / 60.0);
  fprintf (stdout, "\n");
  #undef profile
  #undef percent
  #undef flag_save
  #undef minutes
  #undef IntermediateCnts
  #undef StartTime
  #undef EndTime
  #undef CurrentTime
  #undef infostring
  return(_comp);
} /* class_Progress_bar_finally */

_class_Union_logger_1D *class_Union_logger_1D_finally(_class_Union_logger_1D *_comp
) {
  #define target_geometry (_comp->_parameters.target_geometry)
  #define target_process (_comp->_parameters.target_process)
  #define min_value (_comp->_parameters.min_value)
  #define max_value (_comp->_parameters.max_value)
  #define n1 (_comp->_parameters.n1)
  #define variable (_comp->_parameters.variable)
  #define filename (_comp->_parameters.filename)
  #define order_total (_comp->_parameters.order_total)
  #define order_volume (_comp->_parameters.order_volume)
  #define order_volume_process (_comp->_parameters.order_volume_process)
  #define logger_conditional_extend_index (_comp->_parameters.logger_conditional_extend_index)
  #define init (_comp->_parameters.init)
  #define loop_index (_comp->_parameters.loop_index)
  #define found_process (_comp->_parameters.found_process)
  #define specified_processes (_comp->_parameters.specified_processes)
  #define local_string (_comp->_parameters.local_string)
  #define accepted_processes (_comp->_parameters.accepted_processes)
  #define logger_list_element (_comp->_parameters.logger_list_element)
  #define accepted_volumes (_comp->_parameters.accepted_volumes)
  #define this_logger (_comp->_parameters.this_logger)
  #define this_storage (_comp->_parameters.this_storage)
  #define loggers_on_target_volume (_comp->_parameters.loggers_on_target_volume)
  #define target_volume (_comp->_parameters.target_volume)
  #define temp_string (_comp->_parameters.temp_string)
  SIG_MESSAGE("[_test_logger_1D_finally] component test_logger_1D=Union_logger_1D() FINALLY [Union_logger_1D:0]");

  // Remember to clean up allocated lists
  if (this_storage.temp_1D_data.allocated_elements > 0)
    free (this_storage.temp_1D_data.elements);

  free (this_storage.Detector_1D.Array_N);
  free (this_storage.Detector_1D.Array_p);
  free (this_storage.Detector_1D.Array_p2);

  if (accepted_processes.num_elements > 0)
    free (accepted_processes.elements);
  if (accepted_volumes.num_elements > 0)
    free (accepted_volumes.elements);
  #undef target_geometry
  #undef target_process
  #undef min_value
  #undef max_value
  #undef n1
  #undef variable
  #undef filename
  #undef order_total
  #undef order_volume
  #undef order_volume_process
  #undef logger_conditional_extend_index
  #undef init
  #undef loop_index
  #undef found_process
  #undef specified_processes
  #undef local_string
  #undef accepted_processes
  #undef logger_list_element
  #undef accepted_volumes
  #undef this_logger
  #undef this_storage
  #undef loggers_on_target_volume
  #undef target_volume
  #undef temp_string
  return(_comp);
} /* class_Union_logger_1D_finally */

_class_Union_logger_2D_space *class_Union_logger_2D_space_finally(_class_Union_logger_2D_space *_comp
) {
  #define target_geometry (_comp->_parameters.target_geometry)
  #define target_process (_comp->_parameters.target_process)
  #define D_direction_1 (_comp->_parameters.D_direction_1)
  #define D1_min (_comp->_parameters.D1_min)
  #define D1_max (_comp->_parameters.D1_max)
  #define n1 (_comp->_parameters.n1)
  #define D_direction_2 (_comp->_parameters.D_direction_2)
  #define D2_min (_comp->_parameters.D2_min)
  #define D2_max (_comp->_parameters.D2_max)
  #define n2 (_comp->_parameters.n2)
  #define filename (_comp->_parameters.filename)
  #define order_total (_comp->_parameters.order_total)
  #define order_volume (_comp->_parameters.order_volume)
  #define order_volume_process (_comp->_parameters.order_volume_process)
  #define logger_conditional_extend_index (_comp->_parameters.logger_conditional_extend_index)
  #define init (_comp->_parameters.init)
  #define loop_index (_comp->_parameters.loop_index)
  #define found_process (_comp->_parameters.found_process)
  #define specified_processes (_comp->_parameters.specified_processes)
  #define local_string (_comp->_parameters.local_string)
  #define accepted_processes (_comp->_parameters.accepted_processes)
  #define logger_list_element (_comp->_parameters.logger_list_element)
  #define accepted_volumes (_comp->_parameters.accepted_volumes)
  #define this_logger (_comp->_parameters.this_logger)
  #define this_storage (_comp->_parameters.this_storage)
  #define loggers_on_target_volume (_comp->_parameters.loggers_on_target_volume)
  #define target_volume (_comp->_parameters.target_volume)
  SIG_MESSAGE("[_test_logger_2D_space_zx_finally] component test_logger_2D_space_zx=Union_logger_2D_space() FINALLY [Union_logger_2D_space:0]");

  // Remember to clean up allocated lists
  if (this_storage.temp_2DS_data.allocated_elements > 0)
    free (this_storage.temp_2DS_data.elements);

  free2Ddouble_2DS (this_storage.Detector_2D.Array_N);
  free2Ddouble_2DS (this_storage.Detector_2D.Array_p);
  free2Ddouble_2DS (this_storage.Detector_2D.Array_p2);

  if (accepted_processes.num_elements > 0)
    free (accepted_processes.elements);
  if (accepted_volumes.num_elements > 0)
    free (accepted_volumes.elements);
  #undef target_geometry
  #undef target_process
  #undef D_direction_1
  #undef D1_min
  #undef D1_max
  #undef n1
  #undef D_direction_2
  #undef D2_min
  #undef D2_max
  #undef n2
  #undef filename
  #undef order_total
  #undef order_volume
  #undef order_volume_process
  #undef logger_conditional_extend_index
  #undef init
  #undef loop_index
  #undef found_process
  #undef specified_processes
  #undef local_string
  #undef accepted_processes
  #undef logger_list_element
  #undef accepted_volumes
  #undef this_logger
  #undef this_storage
  #undef loggers_on_target_volume
  #undef target_volume
  return(_comp);
} /* class_Union_logger_2D_space_finally */

_class_Union_conditional_standard *class_Union_conditional_standard_finally(_class_Union_conditional_standard *_comp
) {
  #define target_loggers (_comp->_parameters.target_loggers)
  #define master_tagging (_comp->_parameters.master_tagging)
  #define tagging_extend_index (_comp->_parameters.tagging_extend_index)
  #define time_min (_comp->_parameters.time_min)
  #define time_max (_comp->_parameters.time_max)
  #define E_min (_comp->_parameters.E_min)
  #define E_max (_comp->_parameters.E_max)
  #define total_order_min (_comp->_parameters.total_order_min)
  #define total_order_max (_comp->_parameters.total_order_max)
  #define last_volume_index (_comp->_parameters.last_volume_index)
  #define overwrite_logger_weight (_comp->_parameters.overwrite_logger_weight)
  #define init (_comp->_parameters.init)
  #define loop_index (_comp->_parameters.loop_index)
  #define return_value (_comp->_parameters.return_value)
  #define accepted_loggers_all (_comp->_parameters.accepted_loggers_all)
  #define accepted_loggers_specific (_comp->_parameters.accepted_loggers_specific)
  #define accepted_abs_loggers_all (_comp->_parameters.accepted_abs_loggers_all)
  #define accepted_abs_loggers_specific (_comp->_parameters.accepted_abs_loggers_specific)
  #define found_logger (_comp->_parameters.found_logger)
  #define found_abs_logger (_comp->_parameters.found_abs_logger)
  #define this_conditional_data (_comp->_parameters.this_conditional_data)
  #define this_data_union (_comp->_parameters.this_data_union)
  #define new_tagging_element (_comp->_parameters.new_tagging_element)
  SIG_MESSAGE("[_test_conditional_standard_finally] component test_conditional_standard=Union_conditional_standard() FINALLY [Union_conditional_standard:0]");

  // Remember to clean up allocated lists
  if (accepted_loggers_specific.num_elements > 0)
    free (accepted_loggers_specific.elements);
  if (accepted_loggers_all.num_elements > 0)
    free (accepted_loggers_all.elements);
  if (accepted_abs_loggers_specific.num_elements > 0)
    free (accepted_abs_loggers_specific.elements);
  if (accepted_abs_loggers_all.num_elements > 0)
    free (accepted_abs_loggers_all.elements);
  // if (accepted_tagging.num_elements > 0) free(accepted_tagging.elements);
  #undef target_loggers
  #undef master_tagging
  #undef tagging_extend_index
  #undef time_min
  #undef time_max
  #undef E_min
  #undef E_max
  #undef total_order_min
  #undef total_order_max
  #undef last_volume_index
  #undef overwrite_logger_weight
  #undef init
  #undef loop_index
  #undef return_value
  #undef accepted_loggers_all
  #undef accepted_loggers_specific
  #undef accepted_abs_loggers_all
  #undef accepted_abs_loggers_specific
  #undef found_logger
  #undef found_abs_logger
  #undef this_conditional_data
  #undef this_data_union
  #undef new_tagging_element
  return(_comp);
} /* class_Union_conditional_standard_finally */

_class_Union_conditional_PSD *class_Union_conditional_PSD_finally(_class_Union_conditional_PSD *_comp
) {
  #define target_loggers (_comp->_parameters.target_loggers)
  #define master_tagging (_comp->_parameters.master_tagging)
  #define tagging_extend_index (_comp->_parameters.tagging_extend_index)
  #define xwidth (_comp->_parameters.xwidth)
  #define yheight (_comp->_parameters.yheight)
  #define time_min (_comp->_parameters.time_min)
  #define time_max (_comp->_parameters.time_max)
  #define overwrite_logger_weight (_comp->_parameters.overwrite_logger_weight)
  #define init (_comp->_parameters.init)
  #define loop_index (_comp->_parameters.loop_index)
  #define return_value (_comp->_parameters.return_value)
  #define accepted_loggers_all (_comp->_parameters.accepted_loggers_all)
  #define accepted_loggers_specific (_comp->_parameters.accepted_loggers_specific)
  #define accepted_abs_loggers_all (_comp->_parameters.accepted_abs_loggers_all)
  #define accepted_abs_loggers_specific (_comp->_parameters.accepted_abs_loggers_specific)
  #define found_logger (_comp->_parameters.found_logger)
  #define found_abs_logger (_comp->_parameters.found_abs_logger)
  #define this_conditional_data (_comp->_parameters.this_conditional_data)
  #define this_data_union (_comp->_parameters.this_data_union)
  #define new_tagging_element (_comp->_parameters.new_tagging_element)
  SIG_MESSAGE("[_test_conditional_PSD_finally] component test_conditional_PSD=Union_conditional_PSD() FINALLY [Union_conditional_PSD:0]");

  // Remember to clean up allocated lists
  if (accepted_loggers_specific.num_elements > 0)
    free (accepted_loggers_specific.elements);
  if (accepted_loggers_all.num_elements > 0)
    free (accepted_loggers_all.elements);
  // if (accepted_tagging.num_elements > 0) free(accepted_tagging.elements);
  #undef target_loggers
  #undef master_tagging
  #undef tagging_extend_index
  #undef xwidth
  #undef yheight
  #undef time_min
  #undef time_max
  #undef overwrite_logger_weight
  #undef init
  #undef loop_index
  #undef return_value
  #undef accepted_loggers_all
  #undef accepted_loggers_specific
  #undef accepted_abs_loggers_all
  #undef accepted_abs_loggers_specific
  #undef found_logger
  #undef found_abs_logger
  #undef this_conditional_data
  #undef this_data_union
  #undef new_tagging_element
  return(_comp);
} /* class_Union_conditional_PSD_finally */

_class_Union_master *class_Union_master_finally(_class_Union_master *_comp
) {
  #define enable_refraction (_comp->_parameters.enable_refraction)
  #define enable_reflection (_comp->_parameters.enable_reflection)
  #define verbal (_comp->_parameters.verbal)
  #define list_verbal (_comp->_parameters.list_verbal)
  #define finally_verbal (_comp->_parameters.finally_verbal)
  #define allow_inside_start (_comp->_parameters.allow_inside_start)
  #define enable_tagging (_comp->_parameters.enable_tagging)
  #define history_limit (_comp->_parameters.history_limit)
  #define enable_conditionals (_comp->_parameters.enable_conditionals)
  #define inherit_number_of_scattering_events (_comp->_parameters.inherit_number_of_scattering_events)
  #define weight_ratio_limit (_comp->_parameters.weight_ratio_limit)
  #define init (_comp->_parameters.init)
  #define global_positions_to_transform_list_master (_comp->_parameters.global_positions_to_transform_list_master)
  #define global_rotations_to_transform_list_master (_comp->_parameters.global_rotations_to_transform_list_master)
  #define global_process_list_master (_comp->_parameters.global_process_list_master)
  #define global_material_list_master (_comp->_parameters.global_material_list_master)
  #define global_surface_list_master (_comp->_parameters.global_surface_list_master)
  #define global_geometry_list_master (_comp->_parameters.global_geometry_list_master)
  #define global_all_volume_logger_list_master (_comp->_parameters.global_all_volume_logger_list_master)
  #define global_specific_volumes_logger_list_master (_comp->_parameters.global_specific_volumes_logger_list_master)
  #define global_all_volume_abs_logger_list_master (_comp->_parameters.global_all_volume_abs_logger_list_master)
  #define global_specific_volumes_abs_logger_list_master (_comp->_parameters.global_specific_volumes_abs_logger_list_master)
  #define global_tagging_conditional_list_master (_comp->_parameters.global_tagging_conditional_list_master)
  #define global_master_list_master (_comp->_parameters.global_master_list_master)
  #define starting_volume_warning (_comp->_parameters.starting_volume_warning)
  #define global_master_element (_comp->_parameters.global_master_element)
  #define this_global_master_index (_comp->_parameters.this_global_master_index)
  #define previous_master_index (_comp->_parameters.previous_master_index)
  #define geometry_list_index (_comp->_parameters.geometry_list_index)
  #define intersection_time_table (_comp->_parameters.intersection_time_table)
  #define Volumes (_comp->_parameters.Volumes)
  #define Geometries (_comp->_parameters.Geometries)
  #define Volume_copies (_comp->_parameters.Volume_copies)
  #define starting_lists (_comp->_parameters.starting_lists)
  #define Volume_copies_allocated (_comp->_parameters.Volume_copies_allocated)
  #define r (_comp->_parameters.r)
  #define r_start (_comp->_parameters.r_start)
  #define v (_comp->_parameters.v)
  #define error_msg (_comp->_parameters.error_msg)
  #define component_error_msg (_comp->_parameters.component_error_msg)
  #define string_output (_comp->_parameters.string_output)
  #define number_of_volumes (_comp->_parameters.number_of_volumes)
  #define volume_index (_comp->_parameters.volume_index)
  #define process_index (_comp->_parameters.process_index)
  #define iterator (_comp->_parameters.iterator)
  #define solutions (_comp->_parameters.solutions)
  #define max_number_of_processes (_comp->_parameters.max_number_of_processes)
  #define limit (_comp->_parameters.limit)
  #define solution (_comp->_parameters.solution)
  #define min_solution (_comp->_parameters.min_solution)
  #define ignore_closest (_comp->_parameters.ignore_closest)
  #define ignore_surface_index (_comp->_parameters.ignore_surface_index)
  #define min_volume (_comp->_parameters.min_volume)
  #define time_found (_comp->_parameters.time_found)
  #define intersection_time (_comp->_parameters.intersection_time)
  #define min_intersection_time (_comp->_parameters.min_intersection_time)
  #define process (_comp->_parameters.process)
  #define process_start (_comp->_parameters.process_start)
  #define my_trace (_comp->_parameters.my_trace)
  #define p_my_trace (_comp->_parameters.p_my_trace)
  #define my_trace_fraction_control (_comp->_parameters.my_trace_fraction_control)
  #define k (_comp->_parameters.k)
  #define k_new (_comp->_parameters.k_new)
  #define k_old (_comp->_parameters.k_old)
  #define k_rotated (_comp->_parameters.k_rotated)
  #define v_length (_comp->_parameters.v_length)
  #define my_sum (_comp->_parameters.my_sum)
  #define my_sum_plus_abs (_comp->_parameters.my_sum_plus_abs)
  #define culmative_probability (_comp->_parameters.culmative_probability)
  #define mc_prop (_comp->_parameters.mc_prop)
  #define time_to_scattering (_comp->_parameters.time_to_scattering)
  #define length_to_scattering (_comp->_parameters.length_to_scattering)
  #define length_to_boundary (_comp->_parameters.length_to_boundary)
  #define time_to_boundery (_comp->_parameters.time_to_boundery)
  #define selected_process (_comp->_parameters.selected_process)
  #define scattering_event (_comp->_parameters.scattering_event)
  #define time_propagated_without_scattering (_comp->_parameters.time_propagated_without_scattering)
  #define a_next_volume_found (_comp->_parameters.a_next_volume_found)
  #define next_volume (_comp->_parameters.next_volume)
  #define next_volume_priority (_comp->_parameters.next_volume_priority)
  #define done (_comp->_parameters.done)
  #define current_volume (_comp->_parameters.current_volume)
  #define previous_volume (_comp->_parameters.previous_volume)
  #define ray_sucseeded (_comp->_parameters.ray_sucseeded)
  #define number_of_solutions (_comp->_parameters.number_of_solutions)
  #define number_of_solutions_static (_comp->_parameters.number_of_solutions_static)
  #define check (_comp->_parameters.check)
  #define start (_comp->_parameters.start)
  #define intersection_with_children (_comp->_parameters.intersection_with_children)
  #define geometry_output (_comp->_parameters.geometry_output)
  #define tree_next_volume (_comp->_parameters.tree_next_volume)
  #define pre_allocated1 (_comp->_parameters.pre_allocated1)
  #define pre_allocated2 (_comp->_parameters.pre_allocated2)
  #define pre_allocated3 (_comp->_parameters.pre_allocated3)
  #define ray_position (_comp->_parameters.ray_position)
  #define ray_velocity (_comp->_parameters.ray_velocity)
  #define ray_velocity_rotated (_comp->_parameters.ray_velocity_rotated)
  #define ray_velocity_final (_comp->_parameters.ray_velocity_final)
  #define wavevector (_comp->_parameters.wavevector)
  #define wavevector_rotated (_comp->_parameters.wavevector_rotated)
  #define volume_0_found (_comp->_parameters.volume_0_found)
  #define scattered_flag (_comp->_parameters.scattered_flag)
  #define scattered_flag_VP (_comp->_parameters.scattered_flag_VP)
  #define master_transposed_rotation_matrix (_comp->_parameters.master_transposed_rotation_matrix)
  #define temp_rotation_matrix (_comp->_parameters.temp_rotation_matrix)
  #define temp_transpose_rotation_matrix (_comp->_parameters.temp_transpose_rotation_matrix)
  #define non_rotated_position (_comp->_parameters.non_rotated_position)
  #define rotated_position (_comp->_parameters.rotated_position)
  #define non_isotropic_found (_comp->_parameters.non_isotropic_found)
  #define master_tagging_node_list (_comp->_parameters.master_tagging_node_list)
  #define current_tagging_node (_comp->_parameters.current_tagging_node)
  #define tagging_leaf_counter (_comp->_parameters.tagging_leaf_counter)
  #define stop_tagging_ray (_comp->_parameters.stop_tagging_ray)
  #define stop_creating_nodes (_comp->_parameters.stop_creating_nodes)
  #define number_of_scattering_events (_comp->_parameters.number_of_scattering_events)
  #define real_transmission_probability (_comp->_parameters.real_transmission_probability)
  #define mc_transmission_probability (_comp->_parameters.mc_transmission_probability)
  #define number_of_process_interacts_set (_comp->_parameters.number_of_process_interacts_set)
  #define index_of_lacking_process (_comp->_parameters.index_of_lacking_process)
  #define total_process_interact (_comp->_parameters.total_process_interact)
  #define geometry_component_index_list (_comp->_parameters.geometry_component_index_list)
  #define mask_volume_index_list (_comp->_parameters.mask_volume_index_list)
  #define number_of_masks (_comp->_parameters.number_of_masks)
  #define number_of_masked_volumes (_comp->_parameters.number_of_masked_volumes)
  #define mask_status_list (_comp->_parameters.mask_status_list)
  #define current_mask_intersect_list_status (_comp->_parameters.current_mask_intersect_list_status)
  #define mask_index_main (_comp->_parameters.mask_index_main)
  #define mask_iterator (_comp->_parameters.mask_iterator)
  #define mask_start (_comp->_parameters.mask_start)
  #define mask_check (_comp->_parameters.mask_check)
  #define need_to_run_within_which_volume (_comp->_parameters.need_to_run_within_which_volume)
  #define number_of_processes_array (_comp->_parameters.number_of_processes_array)
  #define p_old (_comp->_parameters.p_old)
  #define log_index (_comp->_parameters.log_index)
  #define conditional_status (_comp->_parameters.conditional_status)
  #define this_logger (_comp->_parameters.this_logger)
  #define this_abs_logger (_comp->_parameters.this_abs_logger)
  #define tagging_conditional_list (_comp->_parameters.tagging_conditional_list)
  #define logger_conditional_extend_array (_comp->_parameters.logger_conditional_extend_array)
  #define abs_logger_conditional_extend_array (_comp->_parameters.abs_logger_conditional_extend_array)
  #define max_conditional_extend_index (_comp->_parameters.max_conditional_extend_index)
  #define tagging_conditional_extend (_comp->_parameters.tagging_conditional_extend)
  #define free_tagging_conditioanl_list (_comp->_parameters.free_tagging_conditioanl_list)
  #define safety_distance (_comp->_parameters.safety_distance)
  #define safety_distance2 (_comp->_parameters.safety_distance2)
  #define temporary_focus_data (_comp->_parameters.temporary_focus_data)
  #define this_focus_data (_comp->_parameters.this_focus_data)
  #define focus_data_index (_comp->_parameters.focus_data_index)
  #define r_old (_comp->_parameters.r_old)
  #define initial_weight (_comp->_parameters.initial_weight)
  #define abs_weight_factor (_comp->_parameters.abs_weight_factor)
  #define time_old (_comp->_parameters.time_old)
  #define absorption_index (_comp->_parameters.absorption_index)
  #define abs_weight_factor_set (_comp->_parameters.abs_weight_factor_set)
  #define my_abs (_comp->_parameters.my_abs)
  #define absorption_event_data (_comp->_parameters.absorption_event_data)
  #define abs_position (_comp->_parameters.abs_position)
  #define transformed_abs_position (_comp->_parameters.transformed_abs_position)
  #define t_abs_propagation (_comp->_parameters.t_abs_propagation)
  #define abs_distance (_comp->_parameters.abs_distance)
  #define abs_max_length (_comp->_parameters.abs_max_length)
  #define longest_surface_stack (_comp->_parameters.longest_surface_stack)
  #define interface_stack (_comp->_parameters.interface_stack)
  SIG_MESSAGE("[_sample_finally] component sample=Union_master() FINALLY [Union_master:0]");

  // write out histories from tagging system if enabled
  if (enable_tagging) {
    if (finally_verbal)
      printf ("Writing tagging tree to disk \n");
    if (finally_verbal)
      printf ("Number of leafs = %d \n", tagging_leaf_counter);
    // While writing the tagging tree to disk, all the leafs are deallocated
    write_tagging_tree (&master_tagging_node_list, Volumes, tagging_leaf_counter, number_of_volumes);
  }
  if (master_tagging_node_list.num_elements > 0)
    free (master_tagging_node_list.elements);

  if (finally_verbal)
    printf ("Freeing variables which are always allocated \n");
  // free allocated arrays specific to this master union component
  free (scattered_flag);
  free (my_trace);
  free (my_trace_fraction_control);
  free (pre_allocated1);
  free (pre_allocated2);
  free (pre_allocated3);
  free (number_of_processes_array);
  free (Geometries);

  if (finally_verbal)
    printf ("Freeing intersection_time_table \n");
  for (iterator = 1; iterator < intersection_time_table.num_volumes; iterator++) {
    free (intersection_time_table.intersection_times[iterator]);
    free (intersection_time_table.normal_vector_x[iterator]);
    free (intersection_time_table.normal_vector_y[iterator]);
    free (intersection_time_table.normal_vector_z[iterator]);
    free (intersection_time_table.surface_index[iterator]);
  }

  free (intersection_time_table.n_elements);
  free (intersection_time_table.calculated);
  free (intersection_time_table.intersection_times);
  free (intersection_time_table.normal_vector_x);
  free (intersection_time_table.normal_vector_y);
  free (intersection_time_table.normal_vector_z);
  free (intersection_time_table.surface_index);

  if (free_tagging_conditioanl_list == 1)
    free (tagging_conditional_list);

  if (finally_verbal)
    printf ("Freeing lists for individual volumes \n");
  for (volume_index = 0; volume_index < number_of_volumes; volume_index++) {

    if (finally_verbal)
      printf ("  Freeing geometry\n");
    if (Volumes[volume_index]->geometry.intersect_check_list.num_elements > 0)
      free (Volumes[volume_index]->geometry.intersect_check_list.elements);
    if (Volumes[volume_index]->geometry.destinations_list.num_elements > 0)
      free (Volumes[volume_index]->geometry.destinations_list.elements);
    if (Volumes[volume_index]->geometry.reduced_destinations_list.num_elements > 0)
      free (Volumes[volume_index]->geometry.reduced_destinations_list.elements);
    if (Volumes[volume_index]->geometry.children.num_elements > 0)
      free (Volumes[volume_index]->geometry.children.elements);
    if (Volumes[volume_index]->geometry.direct_children.num_elements > 0)
      free (Volumes[volume_index]->geometry.direct_children.elements);
    if (Volumes[volume_index]->geometry.masked_by_list.num_elements > 0)
      free (Volumes[volume_index]->geometry.masked_by_list.elements);
    if (Volumes[volume_index]->geometry.masked_by_mask_index_list.num_elements > 0)
      free (Volumes[volume_index]->geometry.masked_by_mask_index_list.elements);
    if (Volumes[volume_index]->geometry.mask_list.num_elements > 0)
      free (Volumes[volume_index]->geometry.mask_list.elements);
    if (Volumes[volume_index]->geometry.mask_intersect_list.num_elements > 0)
      free (Volumes[volume_index]->geometry.mask_intersect_list.elements);
    if (Volumes[volume_index]->geometry.next_volume_list.num_elements > 0)
      free (Volumes[volume_index]->geometry.next_volume_list.elements);

    if (finally_verbal)
      printf ("  Freeing physics\n");
    if (volume_index > 0) { // Volume 0 does not have physical properties allocated
      free (scattered_flag_VP[volume_index]);
      if (Volumes[volume_index]->geometry.process_rot_allocated == 1) {
        free (Volumes[volume_index]->geometry.process_rot_matrix_array);
        free (Volumes[volume_index]->geometry.transpose_process_rot_matrix_array);
        Volumes[volume_index]->geometry.process_rot_allocated = 0;
      }
      if (on_int_list (Volume_copies_allocated, volume_index)) {
        // This is a local copy of a volume, deallocate that local copy (all the allocated memory attachted to it was just deallocated, so this should not leave
        // any leaks)
        free (Volumes[volume_index]);
      } else {
        // Only free p_physics for vacuum volumes for the original at the end (there is a p_physics allocated for each vacuum volume)
        if (Volumes[volume_index]->p_physics->is_vacuum == 1)
          free (Volumes[volume_index]->p_physics);
      }
    }

    if (finally_verbal)
      printf ("  Freeing loggers\n");
    if (Volumes[volume_index]->loggers.num_elements > 0) {
      for (iterator = 0; iterator < Volumes[volume_index]->loggers.num_elements; iterator++) {
        free (Volumes[volume_index]->loggers.p_logger_volume[iterator].p_logger_process);
      }
      free (Volumes[volume_index]->loggers.p_logger_volume);
    }

    if (finally_verbal)
      printf ("  Freeing abs_loggers\n");
    if (Volumes[volume_index]->abs_loggers.num_elements > 0) {
      free (Volumes[volume_index]->abs_loggers.p_abs_logger);
    }
    if (finally_verbal)
      printf ("  Freeing Volumes[index]\n");
    // free(Volumes[volume_index]); // Not able to free
    // if (finally_verbal) printf("  Managed to free Volumes[index]\n");
  }

  free (scattered_flag_VP);

  if (finally_verbal)
    printf ("Freeing starting lists \n");
  if (starting_lists.allowed_starting_volume_logic_list.num_elements > 0)
    free (starting_lists.allowed_starting_volume_logic_list.elements);
  if (starting_lists.reduced_start_list.num_elements > 0)
    free (starting_lists.reduced_start_list.elements);
  if (starting_lists.start_logic_list.num_elements > 0)
    free (starting_lists.start_logic_list.elements);

  if (finally_verbal)
    printf ("Freeing mask lists \n");
  if (mask_status_list.num_elements > 0)
    free (mask_status_list.elements);
  if (current_mask_intersect_list_status.num_elements > 0)
    free (current_mask_intersect_list_status.elements);
  if (mask_volume_index_list.num_elements > 0)
    free (mask_volume_index_list.elements);

  if (finally_verbal)
    printf ("Freeing component index list \n");
  if (geometry_component_index_list.num_elements > 0)
    free (geometry_component_index_list.elements);

  if (finally_verbal)
    printf ("Freeing Volumes \n");
  free (Volumes);

  if (interface_stack.number_of_surfaces > 0)
    free (interface_stack.p_surface_array);

  // Free global allocated arrays if this is the last master union component in the instrument file

  if (global_master_list_master->elements[global_master_list_master->num_elements - 1].component_index == INDEX_CURRENT_COMP) {
    if (finally_verbal)
      printf ("Freeing global arrays because this is the last Union master component\n");

    // Freeing lists allocated in Union_initialization

    if (finally_verbal)
      printf ("Freeing global process list \n");
    if (global_process_list_master->num_elements > 0)
      free (global_process_list_master->elements);

    if (finally_verbal)
      printf ("Freeing global material list \n");
    if (global_material_list_master->num_elements > 0)
      free (global_material_list_master->elements);

    if (finally_verbal)
      printf ("Freeing global surface list \n");
    if (global_surface_list_master->num_elements > 0)
      free (global_surface_list_master->elements);

    if (finally_verbal)
      printf ("Freeing global geometry list \n");
    if (global_geometry_list_master->num_elements > 0)
      free (global_geometry_list_master->elements);

    if (finally_verbal)
      printf ("Freeing global master list \n");
    if (global_master_list_master->num_elements > 0)
      free (global_master_list_master->elements);

    if (finally_verbal)
      printf ("Freeing global logger lists \n");
    for (iterator = 0; iterator < global_all_volume_logger_list_master->num_elements; iterator++) {
      if (global_all_volume_logger_list_master->elements[iterator].logger->conditional_list.num_elements > 0) {
        free (global_all_volume_logger_list_master->elements[iterator].logger->conditional_list.conditional_functions);
        free (global_all_volume_logger_list_master->elements[iterator].logger->conditional_list.p_data_unions);
      }
    }
    if (global_all_volume_logger_list_master->num_elements > 0)
      free (global_all_volume_logger_list_master->elements);

    for (iterator = 0; iterator < global_specific_volumes_logger_list_master->num_elements; iterator++) {
      if (global_specific_volumes_logger_list_master->elements[iterator].logger->conditional_list.num_elements > 0) {
        free (global_specific_volumes_logger_list_master->elements[iterator].logger->conditional_list.conditional_functions);
        free (global_specific_volumes_logger_list_master->elements[iterator].logger->conditional_list.p_data_unions);
      }
    }
    if (global_specific_volumes_logger_list_master->num_elements > 0)
      free (global_specific_volumes_logger_list_master->elements);

    if (finally_verbal)
      printf ("Freeing global abs logger lists \n");
    for (iterator = 0; iterator < global_all_volume_abs_logger_list_master->num_elements; iterator++) {
      if (global_all_volume_abs_logger_list_master->elements[iterator].abs_logger->conditional_list.num_elements > 0) {
        free (global_all_volume_abs_logger_list_master->elements[iterator].abs_logger->conditional_list.conditional_functions);
        free (global_all_volume_abs_logger_list_master->elements[iterator].abs_logger->conditional_list.p_data_unions);
      }
    }
    if (global_all_volume_abs_logger_list_master->num_elements > 0)
      free (global_all_volume_abs_logger_list_master->elements);

    for (iterator = 0; iterator < global_specific_volumes_abs_logger_list_master->num_elements; iterator++) {
      if (global_specific_volumes_abs_logger_list_master->elements[iterator].abs_logger->conditional_list.num_elements > 0) {
        free (global_specific_volumes_abs_logger_list_master->elements[iterator].abs_logger->conditional_list.conditional_functions);
        free (global_specific_volumes_abs_logger_list_master->elements[iterator].abs_logger->conditional_list.p_data_unions);
      }
    }
    if (global_specific_volumes_abs_logger_list_master->num_elements > 0)
      free (global_specific_volumes_abs_logger_list_master->elements);

    if (finally_verbal)
      printf ("Freeing global tagging conditional lists \n");
    for (iterator = 0; iterator < global_tagging_conditional_list_master->num_elements; iterator++) {
      if (global_tagging_conditional_list_master->elements[iterator].conditional_list.num_elements > 0) {
        free (global_tagging_conditional_list_master->elements[iterator].conditional_list.conditional_functions);
        free (global_tagging_conditional_list_master->elements[iterator].conditional_list.p_data_unions);
      }
    }
    if (global_tagging_conditional_list_master->num_elements > 0)
      free (global_tagging_conditional_list_master->elements);
  }
  #undef enable_refraction
  #undef enable_reflection
  #undef verbal
  #undef list_verbal
  #undef finally_verbal
  #undef allow_inside_start
  #undef enable_tagging
  #undef history_limit
  #undef enable_conditionals
  #undef inherit_number_of_scattering_events
  #undef weight_ratio_limit
  #undef init
  #undef global_positions_to_transform_list_master
  #undef global_rotations_to_transform_list_master
  #undef global_process_list_master
  #undef global_material_list_master
  #undef global_surface_list_master
  #undef global_geometry_list_master
  #undef global_all_volume_logger_list_master
  #undef global_specific_volumes_logger_list_master
  #undef global_all_volume_abs_logger_list_master
  #undef global_specific_volumes_abs_logger_list_master
  #undef global_tagging_conditional_list_master
  #undef global_master_list_master
  #undef starting_volume_warning
  #undef global_master_element
  #undef this_global_master_index
  #undef previous_master_index
  #undef geometry_list_index
  #undef intersection_time_table
  #undef Volumes
  #undef Geometries
  #undef Volume_copies
  #undef starting_lists
  #undef Volume_copies_allocated
  #undef r
  #undef r_start
  #undef v
  #undef error_msg
  #undef component_error_msg
  #undef string_output
  #undef number_of_volumes
  #undef volume_index
  #undef process_index
  #undef iterator
  #undef solutions
  #undef max_number_of_processes
  #undef limit
  #undef solution
  #undef min_solution
  #undef ignore_closest
  #undef ignore_surface_index
  #undef min_volume
  #undef time_found
  #undef intersection_time
  #undef min_intersection_time
  #undef process
  #undef process_start
  #undef my_trace
  #undef p_my_trace
  #undef my_trace_fraction_control
  #undef k
  #undef k_new
  #undef k_old
  #undef k_rotated
  #undef v_length
  #undef my_sum
  #undef my_sum_plus_abs
  #undef culmative_probability
  #undef mc_prop
  #undef time_to_scattering
  #undef length_to_scattering
  #undef length_to_boundary
  #undef time_to_boundery
  #undef selected_process
  #undef scattering_event
  #undef time_propagated_without_scattering
  #undef a_next_volume_found
  #undef next_volume
  #undef next_volume_priority
  #undef done
  #undef current_volume
  #undef previous_volume
  #undef ray_sucseeded
  #undef number_of_solutions
  #undef number_of_solutions_static
  #undef check
  #undef start
  #undef intersection_with_children
  #undef geometry_output
  #undef tree_next_volume
  #undef pre_allocated1
  #undef pre_allocated2
  #undef pre_allocated3
  #undef ray_position
  #undef ray_velocity
  #undef ray_velocity_rotated
  #undef ray_velocity_final
  #undef wavevector
  #undef wavevector_rotated
  #undef volume_0_found
  #undef scattered_flag
  #undef scattered_flag_VP
  #undef master_transposed_rotation_matrix
  #undef temp_rotation_matrix
  #undef temp_transpose_rotation_matrix
  #undef non_rotated_position
  #undef rotated_position
  #undef non_isotropic_found
  #undef master_tagging_node_list
  #undef current_tagging_node
  #undef tagging_leaf_counter
  #undef stop_tagging_ray
  #undef stop_creating_nodes
  #undef number_of_scattering_events
  #undef real_transmission_probability
  #undef mc_transmission_probability
  #undef number_of_process_interacts_set
  #undef index_of_lacking_process
  #undef total_process_interact
  #undef geometry_component_index_list
  #undef mask_volume_index_list
  #undef number_of_masks
  #undef number_of_masked_volumes
  #undef mask_status_list
  #undef current_mask_intersect_list_status
  #undef mask_index_main
  #undef mask_iterator
  #undef mask_start
  #undef mask_check
  #undef need_to_run_within_which_volume
  #undef number_of_processes_array
  #undef p_old
  #undef log_index
  #undef conditional_status
  #undef this_logger
  #undef this_abs_logger
  #undef tagging_conditional_list
  #undef logger_conditional_extend_array
  #undef abs_logger_conditional_extend_array
  #undef max_conditional_extend_index
  #undef tagging_conditional_extend
  #undef free_tagging_conditioanl_list
  #undef safety_distance
  #undef safety_distance2
  #undef temporary_focus_data
  #undef this_focus_data
  #undef focus_data_index
  #undef r_old
  #undef initial_weight
  #undef abs_weight_factor
  #undef time_old
  #undef absorption_index
  #undef abs_weight_factor_set
  #undef my_abs
  #undef absorption_event_data
  #undef abs_position
  #undef transformed_abs_position
  #undef t_abs_propagation
  #undef abs_distance
  #undef abs_max_length
  #undef longest_surface_stack
  #undef interface_stack
  return(_comp);
} /* class_Union_master_finally */

_class_PSD_monitor *class_PSD_monitor_finally(_class_PSD_monitor *_comp
) {
  #define nx (_comp->_parameters.nx)
  #define ny (_comp->_parameters.ny)
  #define filename (_comp->_parameters.filename)
  #define xmin (_comp->_parameters.xmin)
  #define xmax (_comp->_parameters.xmax)
  #define ymin (_comp->_parameters.ymin)
  #define ymax (_comp->_parameters.ymax)
  #define xwidth (_comp->_parameters.xwidth)
  #define yheight (_comp->_parameters.yheight)
  #define restore_neutron (_comp->_parameters.restore_neutron)
  #define nowritefile (_comp->_parameters.nowritefile)
  #define PSD_N (_comp->_parameters.PSD_N)
  #define PSD_p (_comp->_parameters.PSD_p)
  #define PSD_p2 (_comp->_parameters.PSD_p2)
  SIG_MESSAGE("[_detector_finally] component detector=PSD_monitor() FINALLY [PSD_monitor:0]");

  destroy_darr2d(PSD_N);
  destroy_darr2d(PSD_p);
  destroy_darr2d(PSD_p2);
  #undef nx
  #undef ny
  #undef filename
  #undef xmin
  #undef xmax
  #undef ymin
  #undef ymax
  #undef xwidth
  #undef yheight
  #undef restore_neutron
  #undef nowritefile
  #undef PSD_N
  #undef PSD_p
  #undef PSD_p2
  return(_comp);
} /* class_PSD_monitor_finally */



int finally(void) { /* called by mccode_main for Conditional_test:FINALLY */
#pragma acc update host(_init_var)
#pragma acc update host(_Vanadium_incoherent_var)
#pragma acc update host(_Vanadium_var)
#pragma acc update host(_Al_incoherent_var)
#pragma acc update host(_Al_Powder_var)
#pragma acc update host(_Al_var)
#pragma acc update host(_Cu_incoherent_var)
#pragma acc update host(_Cu_Powder_var)
#pragma acc update host(_Cu_var)
#pragma acc update host(_a1_var)
#pragma acc update host(_source_var)
#pragma acc update host(_sample_pos_var)
#pragma acc update host(_cryostat_wall_var)
#pragma acc update host(_cryostat_wall_vacuum_var)
#pragma acc update host(_sample_sphere_var)
#pragma acc update host(_sample_box_var)
#pragma acc update host(_sample_cone_var)
#pragma acc update host(_test_logger_1D_var)
#pragma acc update host(_test_logger_2D_space_zx_var)
#pragma acc update host(_test_logger_2D_space_zy_var)
#pragma acc update host(_scat_direction_var)
#pragma acc update host(_test_logger_2D_space_zx_con_time_var)
#pragma acc update host(_test_conditional_standard_var)
#pragma acc update host(_test_logger_2D_space_zx_con_PSD_var)
#pragma acc update host(_test_conditional_PSD_var)
#pragma acc update host(_sample_var)
#pragma acc update host(_detector_var)
#pragma acc update host(_detector_scat_var)
#pragma acc update host(_stop_var)
#pragma acc update host(_instrument_var)

  siminfo_init(NULL);
  save(siminfo_file); /* save data when simulation ends */

  /* call iteratively all components FINALLY */

  class_Incoherent_process_finally(&_Vanadium_incoherent_var);

  class_Union_make_material_finally(&_Vanadium_var);

  class_Incoherent_process_finally(&_Al_incoherent_var);

  class_Powder_process_finally(&_Al_Powder_var);

  class_Union_make_material_finally(&_Al_var);

  class_Incoherent_process_finally(&_Cu_incoherent_var);

  class_Powder_process_finally(&_Cu_Powder_var);

  class_Union_make_material_finally(&_Cu_var);

  class_Progress_bar_finally(&_a1_var);








  class_Union_logger_1D_finally(&_test_logger_1D_var);

  class_Union_logger_2D_space_finally(&_test_logger_2D_space_zx_var);

  class_Union_logger_2D_space_finally(&_test_logger_2D_space_zy_var);


  class_Union_logger_2D_space_finally(&_test_logger_2D_space_zx_con_time_var);

  class_Union_conditional_standard_finally(&_test_conditional_standard_var);

  class_Union_logger_2D_space_finally(&_test_logger_2D_space_zx_con_PSD_var);

  class_Union_conditional_PSD_finally(&_test_conditional_PSD_var);

  class_Union_master_finally(&_sample_var);

  class_PSD_monitor_finally(&_detector_var);

  class_PSD_monitor_finally(&_detector_scat_var);


  siminfo_close(); 

  return(0);
} /* finally */

/* *****************************************************************************
* instrument 'Conditional_test' and components DISPLAY
***************************************************************************** */

  #define magnify     mcdis_magnify
  #define line        mcdis_line
  #define dashed_line mcdis_dashed_line
  #define multiline   mcdis_multiline
  #define rectangle   mcdis_rectangle
  #define box         mcdis_box
  #define circle      mcdis_circle
  #define cylinder    mcdis_cylinder
  #define sphere      mcdis_sphere
  #define cone        mcdis_cone
  #define polygon     mcdis_polygon
  #define polyhedron  mcdis_polyhedron
_class_Progress_bar *class_Progress_bar_display(_class_Progress_bar *_comp
) {
  #define profile (_comp->_parameters.profile)
  #define percent (_comp->_parameters.percent)
  #define flag_save (_comp->_parameters.flag_save)
  #define minutes (_comp->_parameters.minutes)
  #define IntermediateCnts (_comp->_parameters.IntermediateCnts)
  #define StartTime (_comp->_parameters.StartTime)
  #define EndTime (_comp->_parameters.EndTime)
  #define CurrentTime (_comp->_parameters.CurrentTime)
  #define infostring (_comp->_parameters.infostring)
  SIG_MESSAGE("[_a1_display] component a1=Progress_bar() DISPLAY [Progress_bar:0]");

  printf("MCDISPLAY: component %s\n", _comp->_name);

  #undef profile
  #undef percent
  #undef flag_save
  #undef minutes
  #undef IntermediateCnts
  #undef StartTime
  #undef EndTime
  #undef CurrentTime
  #undef infostring
  return(_comp);
} /* class_Progress_bar_display */

_class_Source_div *class_Source_div_display(_class_Source_div *_comp
) {
  #define xwidth (_comp->_parameters.xwidth)
  #define yheight (_comp->_parameters.yheight)
  #define focus_aw (_comp->_parameters.focus_aw)
  #define focus_ah (_comp->_parameters.focus_ah)
  #define E0 (_comp->_parameters.E0)
  #define dE (_comp->_parameters.dE)
  #define lambda0 (_comp->_parameters.lambda0)
  #define dlambda (_comp->_parameters.dlambda)
  #define gauss (_comp->_parameters.gauss)
  #define flux (_comp->_parameters.flux)
  #define sigmah (_comp->_parameters.sigmah)
  #define sigmav (_comp->_parameters.sigmav)
  #define p_init (_comp->_parameters.p_init)
  #define dist (_comp->_parameters.dist)
  #define focus_xw (_comp->_parameters.focus_xw)
  #define focus_yh (_comp->_parameters.focus_yh)
  SIG_MESSAGE("[_source_display] component source=Source_div() DISPLAY [Source_div:0]");

  printf("MCDISPLAY: component %s\n", _comp->_name);
  
  multiline(5, -xwidth/2.0, -yheight/2.0, 0.0,
                xwidth/2.0, -yheight/2.0, 0.0,
                xwidth/2.0,  yheight/2.0, 0.0,
               -xwidth/2.0,  yheight/2.0, 0.0,
               -xwidth/2.0, -yheight/2.0, 0.0);
  if (dist) {
    dashed_line(0,0,0, -focus_xw/2,-focus_yh/2,dist, 4);
    dashed_line(0,0,0,  focus_xw/2,-focus_yh/2,dist, 4);
    dashed_line(0,0,0,  focus_xw/2, focus_yh/2,dist, 4);
    dashed_line(0,0,0, -focus_xw/2, focus_yh/2,dist, 4);
  }
  #undef xwidth
  #undef yheight
  #undef focus_aw
  #undef focus_ah
  #undef E0
  #undef dE
  #undef lambda0
  #undef dlambda
  #undef gauss
  #undef flux
  #undef sigmah
  #undef sigmav
  #undef p_init
  #undef dist
  #undef focus_xw
  #undef focus_yh
  return(_comp);
} /* class_Source_div_display */

_class_Arm *class_Arm_display(_class_Arm *_comp
) {
  SIG_MESSAGE("[_sample_pos_display] component sample_pos=Arm() DISPLAY [Arm:0]");

  printf("MCDISPLAY: component %s\n", _comp->_name);
  /* A bit ugly; hard-coded dimensions. */

  line (0, 0, 0, 0.2, 0, 0);
  line (0, 0, 0, 0, 0.2, 0);
  line (0, 0, 0, 0, 0, 0.2);

  cone (0.2, 0, 0, 0.01, 0.02, 1, 0, 0);
  cone (0, 0.2, 0, 0.01, 0.02, 0, 1, 0);
  cone (0, 0, 0.2, 0.01, 0.02, 0, 0, 1);
  return(_comp);
} /* class_Arm_display */

_class_Union_master *class_Union_master_display(_class_Union_master *_comp
) {
  #define enable_refraction (_comp->_parameters.enable_refraction)
  #define enable_reflection (_comp->_parameters.enable_reflection)
  #define verbal (_comp->_parameters.verbal)
  #define list_verbal (_comp->_parameters.list_verbal)
  #define finally_verbal (_comp->_parameters.finally_verbal)
  #define allow_inside_start (_comp->_parameters.allow_inside_start)
  #define enable_tagging (_comp->_parameters.enable_tagging)
  #define history_limit (_comp->_parameters.history_limit)
  #define enable_conditionals (_comp->_parameters.enable_conditionals)
  #define inherit_number_of_scattering_events (_comp->_parameters.inherit_number_of_scattering_events)
  #define weight_ratio_limit (_comp->_parameters.weight_ratio_limit)
  #define init (_comp->_parameters.init)
  #define global_positions_to_transform_list_master (_comp->_parameters.global_positions_to_transform_list_master)
  #define global_rotations_to_transform_list_master (_comp->_parameters.global_rotations_to_transform_list_master)
  #define global_process_list_master (_comp->_parameters.global_process_list_master)
  #define global_material_list_master (_comp->_parameters.global_material_list_master)
  #define global_surface_list_master (_comp->_parameters.global_surface_list_master)
  #define global_geometry_list_master (_comp->_parameters.global_geometry_list_master)
  #define global_all_volume_logger_list_master (_comp->_parameters.global_all_volume_logger_list_master)
  #define global_specific_volumes_logger_list_master (_comp->_parameters.global_specific_volumes_logger_list_master)
  #define global_all_volume_abs_logger_list_master (_comp->_parameters.global_all_volume_abs_logger_list_master)
  #define global_specific_volumes_abs_logger_list_master (_comp->_parameters.global_specific_volumes_abs_logger_list_master)
  #define global_tagging_conditional_list_master (_comp->_parameters.global_tagging_conditional_list_master)
  #define global_master_list_master (_comp->_parameters.global_master_list_master)
  #define starting_volume_warning (_comp->_parameters.starting_volume_warning)
  #define global_master_element (_comp->_parameters.global_master_element)
  #define this_global_master_index (_comp->_parameters.this_global_master_index)
  #define previous_master_index (_comp->_parameters.previous_master_index)
  #define geometry_list_index (_comp->_parameters.geometry_list_index)
  #define intersection_time_table (_comp->_parameters.intersection_time_table)
  #define Volumes (_comp->_parameters.Volumes)
  #define Geometries (_comp->_parameters.Geometries)
  #define Volume_copies (_comp->_parameters.Volume_copies)
  #define starting_lists (_comp->_parameters.starting_lists)
  #define Volume_copies_allocated (_comp->_parameters.Volume_copies_allocated)
  #define r (_comp->_parameters.r)
  #define r_start (_comp->_parameters.r_start)
  #define v (_comp->_parameters.v)
  #define error_msg (_comp->_parameters.error_msg)
  #define component_error_msg (_comp->_parameters.component_error_msg)
  #define string_output (_comp->_parameters.string_output)
  #define number_of_volumes (_comp->_parameters.number_of_volumes)
  #define volume_index (_comp->_parameters.volume_index)
  #define process_index (_comp->_parameters.process_index)
  #define iterator (_comp->_parameters.iterator)
  #define solutions (_comp->_parameters.solutions)
  #define max_number_of_processes (_comp->_parameters.max_number_of_processes)
  #define limit (_comp->_parameters.limit)
  #define solution (_comp->_parameters.solution)
  #define min_solution (_comp->_parameters.min_solution)
  #define ignore_closest (_comp->_parameters.ignore_closest)
  #define ignore_surface_index (_comp->_parameters.ignore_surface_index)
  #define min_volume (_comp->_parameters.min_volume)
  #define time_found (_comp->_parameters.time_found)
  #define intersection_time (_comp->_parameters.intersection_time)
  #define min_intersection_time (_comp->_parameters.min_intersection_time)
  #define process (_comp->_parameters.process)
  #define process_start (_comp->_parameters.process_start)
  #define my_trace (_comp->_parameters.my_trace)
  #define p_my_trace (_comp->_parameters.p_my_trace)
  #define my_trace_fraction_control (_comp->_parameters.my_trace_fraction_control)
  #define k (_comp->_parameters.k)
  #define k_new (_comp->_parameters.k_new)
  #define k_old (_comp->_parameters.k_old)
  #define k_rotated (_comp->_parameters.k_rotated)
  #define v_length (_comp->_parameters.v_length)
  #define my_sum (_comp->_parameters.my_sum)
  #define my_sum_plus_abs (_comp->_parameters.my_sum_plus_abs)
  #define culmative_probability (_comp->_parameters.culmative_probability)
  #define mc_prop (_comp->_parameters.mc_prop)
  #define time_to_scattering (_comp->_parameters.time_to_scattering)
  #define length_to_scattering (_comp->_parameters.length_to_scattering)
  #define length_to_boundary (_comp->_parameters.length_to_boundary)
  #define time_to_boundery (_comp->_parameters.time_to_boundery)
  #define selected_process (_comp->_parameters.selected_process)
  #define scattering_event (_comp->_parameters.scattering_event)
  #define time_propagated_without_scattering (_comp->_parameters.time_propagated_without_scattering)
  #define a_next_volume_found (_comp->_parameters.a_next_volume_found)
  #define next_volume (_comp->_parameters.next_volume)
  #define next_volume_priority (_comp->_parameters.next_volume_priority)
  #define done (_comp->_parameters.done)
  #define current_volume (_comp->_parameters.current_volume)
  #define previous_volume (_comp->_parameters.previous_volume)
  #define ray_sucseeded (_comp->_parameters.ray_sucseeded)
  #define number_of_solutions (_comp->_parameters.number_of_solutions)
  #define number_of_solutions_static (_comp->_parameters.number_of_solutions_static)
  #define check (_comp->_parameters.check)
  #define start (_comp->_parameters.start)
  #define intersection_with_children (_comp->_parameters.intersection_with_children)
  #define geometry_output (_comp->_parameters.geometry_output)
  #define tree_next_volume (_comp->_parameters.tree_next_volume)
  #define pre_allocated1 (_comp->_parameters.pre_allocated1)
  #define pre_allocated2 (_comp->_parameters.pre_allocated2)
  #define pre_allocated3 (_comp->_parameters.pre_allocated3)
  #define ray_position (_comp->_parameters.ray_position)
  #define ray_velocity (_comp->_parameters.ray_velocity)
  #define ray_velocity_rotated (_comp->_parameters.ray_velocity_rotated)
  #define ray_velocity_final (_comp->_parameters.ray_velocity_final)
  #define wavevector (_comp->_parameters.wavevector)
  #define wavevector_rotated (_comp->_parameters.wavevector_rotated)
  #define volume_0_found (_comp->_parameters.volume_0_found)
  #define scattered_flag (_comp->_parameters.scattered_flag)
  #define scattered_flag_VP (_comp->_parameters.scattered_flag_VP)
  #define master_transposed_rotation_matrix (_comp->_parameters.master_transposed_rotation_matrix)
  #define temp_rotation_matrix (_comp->_parameters.temp_rotation_matrix)
  #define temp_transpose_rotation_matrix (_comp->_parameters.temp_transpose_rotation_matrix)
  #define non_rotated_position (_comp->_parameters.non_rotated_position)
  #define rotated_position (_comp->_parameters.rotated_position)
  #define non_isotropic_found (_comp->_parameters.non_isotropic_found)
  #define master_tagging_node_list (_comp->_parameters.master_tagging_node_list)
  #define current_tagging_node (_comp->_parameters.current_tagging_node)
  #define tagging_leaf_counter (_comp->_parameters.tagging_leaf_counter)
  #define stop_tagging_ray (_comp->_parameters.stop_tagging_ray)
  #define stop_creating_nodes (_comp->_parameters.stop_creating_nodes)
  #define number_of_scattering_events (_comp->_parameters.number_of_scattering_events)
  #define real_transmission_probability (_comp->_parameters.real_transmission_probability)
  #define mc_transmission_probability (_comp->_parameters.mc_transmission_probability)
  #define number_of_process_interacts_set (_comp->_parameters.number_of_process_interacts_set)
  #define index_of_lacking_process (_comp->_parameters.index_of_lacking_process)
  #define total_process_interact (_comp->_parameters.total_process_interact)
  #define geometry_component_index_list (_comp->_parameters.geometry_component_index_list)
  #define mask_volume_index_list (_comp->_parameters.mask_volume_index_list)
  #define number_of_masks (_comp->_parameters.number_of_masks)
  #define number_of_masked_volumes (_comp->_parameters.number_of_masked_volumes)
  #define mask_status_list (_comp->_parameters.mask_status_list)
  #define current_mask_intersect_list_status (_comp->_parameters.current_mask_intersect_list_status)
  #define mask_index_main (_comp->_parameters.mask_index_main)
  #define mask_iterator (_comp->_parameters.mask_iterator)
  #define mask_start (_comp->_parameters.mask_start)
  #define mask_check (_comp->_parameters.mask_check)
  #define need_to_run_within_which_volume (_comp->_parameters.need_to_run_within_which_volume)
  #define number_of_processes_array (_comp->_parameters.number_of_processes_array)
  #define p_old (_comp->_parameters.p_old)
  #define log_index (_comp->_parameters.log_index)
  #define conditional_status (_comp->_parameters.conditional_status)
  #define this_logger (_comp->_parameters.this_logger)
  #define this_abs_logger (_comp->_parameters.this_abs_logger)
  #define tagging_conditional_list (_comp->_parameters.tagging_conditional_list)
  #define logger_conditional_extend_array (_comp->_parameters.logger_conditional_extend_array)
  #define abs_logger_conditional_extend_array (_comp->_parameters.abs_logger_conditional_extend_array)
  #define max_conditional_extend_index (_comp->_parameters.max_conditional_extend_index)
  #define tagging_conditional_extend (_comp->_parameters.tagging_conditional_extend)
  #define free_tagging_conditioanl_list (_comp->_parameters.free_tagging_conditioanl_list)
  #define safety_distance (_comp->_parameters.safety_distance)
  #define safety_distance2 (_comp->_parameters.safety_distance2)
  #define temporary_focus_data (_comp->_parameters.temporary_focus_data)
  #define this_focus_data (_comp->_parameters.this_focus_data)
  #define focus_data_index (_comp->_parameters.focus_data_index)
  #define r_old (_comp->_parameters.r_old)
  #define initial_weight (_comp->_parameters.initial_weight)
  #define abs_weight_factor (_comp->_parameters.abs_weight_factor)
  #define time_old (_comp->_parameters.time_old)
  #define absorption_index (_comp->_parameters.absorption_index)
  #define abs_weight_factor_set (_comp->_parameters.abs_weight_factor_set)
  #define my_abs (_comp->_parameters.my_abs)
  #define absorption_event_data (_comp->_parameters.absorption_event_data)
  #define abs_position (_comp->_parameters.abs_position)
  #define transformed_abs_position (_comp->_parameters.transformed_abs_position)
  #define t_abs_propagation (_comp->_parameters.t_abs_propagation)
  #define abs_distance (_comp->_parameters.abs_distance)
  #define abs_max_length (_comp->_parameters.abs_max_length)
  #define longest_surface_stack (_comp->_parameters.longest_surface_stack)
  #define interface_stack (_comp->_parameters.interface_stack)
  SIG_MESSAGE("[_sample_display] component sample=Union_master() DISPLAY [Union_master:0]");

  printf("MCDISPLAY: component %s\n", _comp->_name);
  // mcdisplay is handled in the component files for each geometry and called here. The line function is only available in this section, and not through
  // functions,
  //   so all the lines to be drawn for each volume are collected in a structure that is then drawn here.
  magnify ("xyz");
  struct lines_to_draw lines_to_draw_master;
  for (volume_index = 1; volume_index < number_of_volumes; volume_index++) {

    if (Volumes[volume_index]->geometry.visualization_on == 1) {
      lines_to_draw_master.number_of_lines = 0;

      Volumes[volume_index]->geometry.mcdisplay_function (&lines_to_draw_master, volume_index, Geometries, number_of_volumes);

      for (iterator = 0; iterator < lines_to_draw_master.number_of_lines; iterator++) {
        if (lines_to_draw_master.lines[iterator].number_of_dashes == 1) {
          line (lines_to_draw_master.lines[iterator].point1.x, lines_to_draw_master.lines[iterator].point1.y, lines_to_draw_master.lines[iterator].point1.z,
                lines_to_draw_master.lines[iterator].point2.x, lines_to_draw_master.lines[iterator].point2.y, lines_to_draw_master.lines[iterator].point2.z);
        } else {
          dashed_line (lines_to_draw_master.lines[iterator].point1.x, lines_to_draw_master.lines[iterator].point1.y,
                       lines_to_draw_master.lines[iterator].point1.z, lines_to_draw_master.lines[iterator].point2.x,
                       lines_to_draw_master.lines[iterator].point2.y, lines_to_draw_master.lines[iterator].point2.z,
                       lines_to_draw_master.lines[iterator].number_of_dashes);
        }
      }
      if (lines_to_draw_master.number_of_lines > 0)
        free (lines_to_draw_master.lines);
    }
  }
  #undef enable_refraction
  #undef enable_reflection
  #undef verbal
  #undef list_verbal
  #undef finally_verbal
  #undef allow_inside_start
  #undef enable_tagging
  #undef history_limit
  #undef enable_conditionals
  #undef inherit_number_of_scattering_events
  #undef weight_ratio_limit
  #undef init
  #undef global_positions_to_transform_list_master
  #undef global_rotations_to_transform_list_master
  #undef global_process_list_master
  #undef global_material_list_master
  #undef global_surface_list_master
  #undef global_geometry_list_master
  #undef global_all_volume_logger_list_master
  #undef global_specific_volumes_logger_list_master
  #undef global_all_volume_abs_logger_list_master
  #undef global_specific_volumes_abs_logger_list_master
  #undef global_tagging_conditional_list_master
  #undef global_master_list_master
  #undef starting_volume_warning
  #undef global_master_element
  #undef this_global_master_index
  #undef previous_master_index
  #undef geometry_list_index
  #undef intersection_time_table
  #undef Volumes
  #undef Geometries
  #undef Volume_copies
  #undef starting_lists
  #undef Volume_copies_allocated
  #undef r
  #undef r_start
  #undef v
  #undef error_msg
  #undef component_error_msg
  #undef string_output
  #undef number_of_volumes
  #undef volume_index
  #undef process_index
  #undef iterator
  #undef solutions
  #undef max_number_of_processes
  #undef limit
  #undef solution
  #undef min_solution
  #undef ignore_closest
  #undef ignore_surface_index
  #undef min_volume
  #undef time_found
  #undef intersection_time
  #undef min_intersection_time
  #undef process
  #undef process_start
  #undef my_trace
  #undef p_my_trace
  #undef my_trace_fraction_control
  #undef k
  #undef k_new
  #undef k_old
  #undef k_rotated
  #undef v_length
  #undef my_sum
  #undef my_sum_plus_abs
  #undef culmative_probability
  #undef mc_prop
  #undef time_to_scattering
  #undef length_to_scattering
  #undef length_to_boundary
  #undef time_to_boundery
  #undef selected_process
  #undef scattering_event
  #undef time_propagated_without_scattering
  #undef a_next_volume_found
  #undef next_volume
  #undef next_volume_priority
  #undef done
  #undef current_volume
  #undef previous_volume
  #undef ray_sucseeded
  #undef number_of_solutions
  #undef number_of_solutions_static
  #undef check
  #undef start
  #undef intersection_with_children
  #undef geometry_output
  #undef tree_next_volume
  #undef pre_allocated1
  #undef pre_allocated2
  #undef pre_allocated3
  #undef ray_position
  #undef ray_velocity
  #undef ray_velocity_rotated
  #undef ray_velocity_final
  #undef wavevector
  #undef wavevector_rotated
  #undef volume_0_found
  #undef scattered_flag
  #undef scattered_flag_VP
  #undef master_transposed_rotation_matrix
  #undef temp_rotation_matrix
  #undef temp_transpose_rotation_matrix
  #undef non_rotated_position
  #undef rotated_position
  #undef non_isotropic_found
  #undef master_tagging_node_list
  #undef current_tagging_node
  #undef tagging_leaf_counter
  #undef stop_tagging_ray
  #undef stop_creating_nodes
  #undef number_of_scattering_events
  #undef real_transmission_probability
  #undef mc_transmission_probability
  #undef number_of_process_interacts_set
  #undef index_of_lacking_process
  #undef total_process_interact
  #undef geometry_component_index_list
  #undef mask_volume_index_list
  #undef number_of_masks
  #undef number_of_masked_volumes
  #undef mask_status_list
  #undef current_mask_intersect_list_status
  #undef mask_index_main
  #undef mask_iterator
  #undef mask_start
  #undef mask_check
  #undef need_to_run_within_which_volume
  #undef number_of_processes_array
  #undef p_old
  #undef log_index
  #undef conditional_status
  #undef this_logger
  #undef this_abs_logger
  #undef tagging_conditional_list
  #undef logger_conditional_extend_array
  #undef abs_logger_conditional_extend_array
  #undef max_conditional_extend_index
  #undef tagging_conditional_extend
  #undef free_tagging_conditioanl_list
  #undef safety_distance
  #undef safety_distance2
  #undef temporary_focus_data
  #undef this_focus_data
  #undef focus_data_index
  #undef r_old
  #undef initial_weight
  #undef abs_weight_factor
  #undef time_old
  #undef absorption_index
  #undef abs_weight_factor_set
  #undef my_abs
  #undef absorption_event_data
  #undef abs_position
  #undef transformed_abs_position
  #undef t_abs_propagation
  #undef abs_distance
  #undef abs_max_length
  #undef longest_surface_stack
  #undef interface_stack
  return(_comp);
} /* class_Union_master_display */

_class_PSD_monitor *class_PSD_monitor_display(_class_PSD_monitor *_comp
) {
  #define nx (_comp->_parameters.nx)
  #define ny (_comp->_parameters.ny)
  #define filename (_comp->_parameters.filename)
  #define xmin (_comp->_parameters.xmin)
  #define xmax (_comp->_parameters.xmax)
  #define ymin (_comp->_parameters.ymin)
  #define ymax (_comp->_parameters.ymax)
  #define xwidth (_comp->_parameters.xwidth)
  #define yheight (_comp->_parameters.yheight)
  #define restore_neutron (_comp->_parameters.restore_neutron)
  #define nowritefile (_comp->_parameters.nowritefile)
  #define PSD_N (_comp->_parameters.PSD_N)
  #define PSD_p (_comp->_parameters.PSD_p)
  #define PSD_p2 (_comp->_parameters.PSD_p2)
  SIG_MESSAGE("[_detector_display] component detector=PSD_monitor() DISPLAY [PSD_monitor:0]");

  printf("MCDISPLAY: component %s\n", _comp->_name);

  multiline (5, (double)xmin, (double)ymin, 0.0, (double)xmax, (double)ymin, 0.0, (double)xmax, (double)ymax, 0.0, (double)xmin, (double)ymax, 0.0, (double)xmin,
             (double)ymin, 0.0);
  #undef nx
  #undef ny
  #undef filename
  #undef xmin
  #undef xmax
  #undef ymin
  #undef ymax
  #undef xwidth
  #undef yheight
  #undef restore_neutron
  #undef nowritefile
  #undef PSD_N
  #undef PSD_p
  #undef PSD_p2
  return(_comp);
} /* class_PSD_monitor_display */


  #undef magnify
  #undef line
  #undef dashed_line
  #undef multiline
  #undef rectangle
  #undef box
  #undef circle
  #undef cylinder
  #undef sphere

int display(void) { /* called by mccode_main for Conditional_test:DISPLAY */
  printf("MCDISPLAY: start\n");

  /* call iteratively all components DISPLAY */









  class_Progress_bar_display(&_a1_var);

  class_Source_div_display(&_source_var);

  class_Arm_display(&_sample_pos_var);









  class_Arm_display(&_scat_direction_var);





  class_Union_master_display(&_sample_var);

  class_PSD_monitor_display(&_detector_var);

  class_PSD_monitor_display(&_detector_scat_var);


  printf("MCDISPLAY: end\n");

  return(0);
} /* display */

void* _getvar_parameters(char* compname)
/* enables settings parameters based use of the GETPAR macro */
{
  #ifdef OPENACC
    #define strcmp(a,b) str_comp(a,b)
  #endif
  if (!strcmp(compname, "init")) return (void *) &(_init_var._parameters);
  if (!strcmp(compname, "Vanadium_incoherent")) return (void *) &(_Vanadium_incoherent_var._parameters);
  if (!strcmp(compname, "Vanadium")) return (void *) &(_Vanadium_var._parameters);
  if (!strcmp(compname, "Al_incoherent")) return (void *) &(_Al_incoherent_var._parameters);
  if (!strcmp(compname, "Al_Powder")) return (void *) &(_Al_Powder_var._parameters);
  if (!strcmp(compname, "Al")) return (void *) &(_Al_var._parameters);
  if (!strcmp(compname, "Cu_incoherent")) return (void *) &(_Cu_incoherent_var._parameters);
  if (!strcmp(compname, "Cu_Powder")) return (void *) &(_Cu_Powder_var._parameters);
  if (!strcmp(compname, "Cu")) return (void *) &(_Cu_var._parameters);
  if (!strcmp(compname, "a1")) return (void *) &(_a1_var._parameters);
  if (!strcmp(compname, "source")) return (void *) &(_source_var._parameters);
  if (!strcmp(compname, "sample_pos")) return (void *) &(_sample_pos_var._parameters);
  if (!strcmp(compname, "cryostat_wall")) return (void *) &(_cryostat_wall_var._parameters);
  if (!strcmp(compname, "cryostat_wall_vacuum")) return (void *) &(_cryostat_wall_vacuum_var._parameters);
  if (!strcmp(compname, "sample_sphere")) return (void *) &(_sample_sphere_var._parameters);
  if (!strcmp(compname, "sample_box")) return (void *) &(_sample_box_var._parameters);
  if (!strcmp(compname, "sample_cone")) return (void *) &(_sample_cone_var._parameters);
  if (!strcmp(compname, "test_logger_1D")) return (void *) &(_test_logger_1D_var._parameters);
  if (!strcmp(compname, "test_logger_2D_space_zx")) return (void *) &(_test_logger_2D_space_zx_var._parameters);
  if (!strcmp(compname, "test_logger_2D_space_zy")) return (void *) &(_test_logger_2D_space_zy_var._parameters);
  if (!strcmp(compname, "scat_direction")) return (void *) &(_scat_direction_var._parameters);
  if (!strcmp(compname, "test_logger_2D_space_zx_con_time")) return (void *) &(_test_logger_2D_space_zx_con_time_var._parameters);
  if (!strcmp(compname, "test_conditional_standard")) return (void *) &(_test_conditional_standard_var._parameters);
  if (!strcmp(compname, "test_logger_2D_space_zx_con_PSD")) return (void *) &(_test_logger_2D_space_zx_con_PSD_var._parameters);
  if (!strcmp(compname, "test_conditional_PSD")) return (void *) &(_test_conditional_PSD_var._parameters);
  if (!strcmp(compname, "sample")) return (void *) &(_sample_var._parameters);
  if (!strcmp(compname, "detector")) return (void *) &(_detector_var._parameters);
  if (!strcmp(compname, "detector_scat")) return (void *) &(_detector_scat_var._parameters);
  if (!strcmp(compname, "stop")) return (void *) &(_stop_var._parameters);
  return 0;
}

void* _get_particle_var(char *token, _class_particle *p)
/* enables setpars based use of GET_PARTICLE_DVAR macro and similar */
{
  return 0;
}

int _getcomp_index(char* compname)
/* Enables retrieving the component position & rotation when the index is not known.
 * Component indexing into MACROS, e.g., POS_A_COMP_INDEX, are 1-based! */
{
  if (!strcmp(compname, "init")) return 1;
  if (!strcmp(compname, "Vanadium_incoherent")) return 2;
  if (!strcmp(compname, "Vanadium")) return 3;
  if (!strcmp(compname, "Al_incoherent")) return 4;
  if (!strcmp(compname, "Al_Powder")) return 5;
  if (!strcmp(compname, "Al")) return 6;
  if (!strcmp(compname, "Cu_incoherent")) return 7;
  if (!strcmp(compname, "Cu_Powder")) return 8;
  if (!strcmp(compname, "Cu")) return 9;
  if (!strcmp(compname, "a1")) return 10;
  if (!strcmp(compname, "source")) return 11;
  if (!strcmp(compname, "sample_pos")) return 12;
  if (!strcmp(compname, "cryostat_wall")) return 13;
  if (!strcmp(compname, "cryostat_wall_vacuum")) return 14;
  if (!strcmp(compname, "sample_sphere")) return 15;
  if (!strcmp(compname, "sample_box")) return 16;
  if (!strcmp(compname, "sample_cone")) return 17;
  if (!strcmp(compname, "test_logger_1D")) return 18;
  if (!strcmp(compname, "test_logger_2D_space_zx")) return 19;
  if (!strcmp(compname, "test_logger_2D_space_zy")) return 20;
  if (!strcmp(compname, "scat_direction")) return 21;
  if (!strcmp(compname, "test_logger_2D_space_zx_con_time")) return 22;
  if (!strcmp(compname, "test_conditional_standard")) return 23;
  if (!strcmp(compname, "test_logger_2D_space_zx_con_PSD")) return 24;
  if (!strcmp(compname, "test_conditional_PSD")) return 25;
  if (!strcmp(compname, "sample")) return 26;
  if (!strcmp(compname, "detector")) return 27;
  if (!strcmp(compname, "detector_scat")) return 28;
  if (!strcmp(compname, "stop")) return 29;
  return -1;
}

/* embedding file "metadata-r.c" */

/** --- Contents of  metadata-r.c ---------------------------------------------------------------------------------- */
// Created by Gregory Tucker, Data Management Software Centre, European Spallation Source ERIC on 07/07/23.
#ifndef MCCODE_NAME
#include "metadata-r.h"
#endif

char * metadata_table_key_component(char* key){
  if (strlen(key) == 0) return NULL;
  char sep[2] = ":\0"; // matches any number of repeated colons
  // look for the separator in the provided key; strtok is allowed to modify the string, so copy it
  char * tok = malloc((strlen(key) + 1) * sizeof(char));
  if (!tok) {
    fprintf(stderr,"Error allocating token\n");
    exit(-1);
  }
  strcpy(tok, key);
  char * pch = strtok(tok, sep); // this *is* the component name (if provided) -- but we need to move the pointer
  char * comp = malloc((1 + strlen(pch)) * sizeof(char));
  if (!comp) {
    fprintf(stderr,"Error allocating comp\n");
    exit(-1);
  }
  strcpy(comp, pch);
  if (tok) free(tok);
  return comp;
}
char * metadata_table_key_literal(char * key){
  if (strlen(key) == 0) return NULL;
  char sep[3] = ":\0";
  char * tok = malloc((strlen(key) + 1 ) * sizeof(char));
  if (!tok) {
    fprintf(stderr,"Error allocating token\n");
    exit(-1);
  }
  strcpy(tok, key);
  char * pch = strtok(tok, sep); // this *is* the component name (if provided)
  if (pch) pch = strtok(NULL, sep); // either NULL or the literal name
  char * name = NULL;
  if (pch) {
    name = malloc((1 + strlen(pch)) * sizeof(char));
    if (!name) {
      fprintf(stderr,"Error allocating name\n");
	exit(-1);
    }
    strcpy(name, pch);
  }
  if (tok) free(tok);
  return name;
}
int metadata_table_defined(int no, metadata_table_t * tab, char * key){
  if (strlen(key) == 0){
    /* This is 0 instead of `no` independent of any wildcard-matching logic
     * because a caller _already_ knows `no` and can verify
     * that `key` is not "" at call-time. So returning `no` is useless.
     */
    return 0;
  }
  char * comp = metadata_table_key_component(key);
  char * name = metadata_table_key_literal(key);
  // look through the table for the matching component and literal names
  int number = 0;
  for (int i=0; i<no; ++i){
    if (!strcmp(comp, tab[i].source)){
      if (name == NULL || !strcmp(name, tab[i].name)) ++number;
    }
  }
  if (comp) free(comp);
  if (name) free(name);
  return number;
}

char * metadata_table_name(int no, metadata_table_t * tab, char *key){
    if (strlen(key) == 0){
        return NULL;
    }
    char * comp = metadata_table_key_component(key);
    char * name = metadata_table_key_literal(key);
    if (name == NULL) {
        for (int i=0; i<no; ++i){
            if (!strcmp(comp, tab[i].source)){
                name = malloc((strlen(tab[i].name) + 1) * sizeof(char));
		if (!name) {
		  fprintf(stderr,"Error allocating metadata_table_name\n");
		  exit(-1);
		}
                strcpy(name, tab[i].name);
                break;
            }
        }
    } else {
        int found=0;
        for (int i=0; i<no; ++i){
            if (!strcmp(comp, tab[i].source) && !strcmp(name, tab[i].name)) {
                found = 1;
                break;
            }
        }
        if (!found) free(name);
    }
    free(comp);
    return name;
}

char * metadata_table_type(int no, metadata_table_t * tab, char * key){
  if (strlen(key) == 0) {
    fprintf(stderr, "Unable to check type of non-existent key\n");
    exit(1);
  }
  char * comp = metadata_table_key_component(key);
  char * name = metadata_table_key_literal(key);
  if (name == NULL){
    fprintf(stderr, "Unable to check type of literal for component %s without its name\n", comp);
    free(comp);
    exit(1);
  }
  char * type = NULL;
  for (int i=0; i<no; ++i){
    if (!strcmp(comp, tab[i].source) && !strcmp(name, tab[i].name)) type = tab[i].type;
  }
  if (comp) free(comp);
  if (name) free(name);
  return type;
}

char * metadata_table_literal(int no, metadata_table_t * tab, char * key){
  if (strlen(key) == 0) {
    fprintf(stderr, "Unable to retrieve literal for non-existent key\n");
    exit(1);
  }
  char * comp = metadata_table_key_component(key);
  char * name = metadata_table_key_literal(key);
  if (name == NULL){
    fprintf(stderr, "Unable to retrieve literal for component %s without its name\n", comp);
    free(comp);
    exit(1);
  }
  char * type = NULL;
  for (int i=0; i<no; ++i){
    if (!strcmp(comp, tab[i].source) && !strcmp(name, tab[i].name)) type = tab[i].value;
  }
  if (comp) free(comp);
  if (name) free(name);
  return type;
}
void metadata_table_print_all_keys(int no, metadata_table_t * tab){
  for (int i=0; i<no; ++i){
    printf("%s::%s ", tab[i].source, tab[i].name);
  }
  printf("\n");
}
int metadata_table_print_all_components(int no, metadata_table_t * tab){
  int count = 0;
  char ** known = malloc(no * sizeof(char*));
  if (!known) {
    fprintf(stderr,"Error allocating table of known metadata\n");
    exit(-1);
  }
  for (int i=0; i<no; ++i){
    int unknown = 1;
    for (int j=0; j<count; ++j) if (!strcmp(tab[i].source, known[j])) unknown = 0;
    if (unknown) known[count++] = tab[i].source;
  }
  size_t nchar = 0;
  for (int i=0; i<count; ++i) nchar += strlen(known[i]) + 1;
  char * line = malloc((nchar + 1) * sizeof(char));
  char * linetmp = malloc((nchar + 1) * sizeof(char));
  if (!line || !linetmp) {
    fprintf(stderr,"Error allocating metadata print arrays\n");
    exit(-1);
  }
  line[0] = '\0';
  for (int i=0; i<count; ++i) sprintf(linetmp, "%s%s ", line, known[i]);
  line=linetmp;
  line[strlen(line)] = '\0'; // eat the trailing space
  printf("%s\n", line);
  free(line);
  free(linetmp);
  free(known);
  return count;
}
int metadata_table_print_component_keys(int no, metadata_table_t * tab, char * key){
  char * comp = metadata_table_key_component(key);
  char * name = metadata_table_key_literal(key);
  int count = 0;
  for (int i=0; i<no; ++i) if (!strcmp(tab[i].source, comp) && (name == NULL || !strcmp(tab[i].name, name))) {
    if (name == NULL) printf("%s ", tab[i].name);
    ++count;
  }
  if (name != NULL) printf("%d", count); // replace count by strlen(tab[i].value)?
  printf("\n");
  return count;
}
/* -------------------------------------------------------------------------------------Contents of  metadata-r.c --- */
/* End of file "metadata-r.c". */

/* embedding file "mccode_main.c" */

/*******************************************************************************
* mccode_main: McCode main() function.
*******************************************************************************/
int mccode_main(int argc, char *argv[])
{
  /*  double run_num = 0; */
  time_t  t;
  clock_t ct;

#ifdef USE_MPI
  char mpi_node_name[MPI_MAX_PROCESSOR_NAME];
  int  mpi_node_name_len;
#endif /* USE_MPI */

#ifdef MAC
  argc = ccommand(&argv);
#endif

#ifdef USE_MPI
  MPI_Init(&argc,&argv);
  MPI_Comm_size(MPI_COMM_WORLD, &mpi_node_count); /* get number of nodes */
  MPI_Comm_rank(MPI_COMM_WORLD, &mpi_node_rank);
  MPI_Comm_set_name(MPI_COMM_WORLD, instrument_name);
  MPI_Get_processor_name(mpi_node_name, &mpi_node_name_len);
#endif /* USE_MPI */

  ct = clock();

  // device and host functional RNG seed
  struct timeval tm;
  gettimeofday(&tm, NULL);
  mcseed = (long) tm.tv_sec*1000000 + tm.tv_usec;
  mcstartdate = (long)tm.tv_sec;  /* set start date before parsing options and creating sim file */
  // init global _particle.randstate for random number use
  // during init(), finally() and display(). NOTE: during trace, a local
  // "_particle" variable is present and thus used instead.
  //
  // PW: srandom deferred until init() since we did not read seed input from commandline
  //srandom(_hash(mcseed-1));

#ifdef USE_MPI
  /* *** print number of nodes *********************************************** */
  if (mpi_node_count > 1) {
    MPI_MASTER(
    printf("Simulation '%s' (%s): running on %i nodes (master is '%s', MPI version %i.%i).\n",
      instrument_name, instrument_source, mpi_node_count, mpi_node_name, MPI_VERSION, MPI_SUBVERSION);
    );
    /* share the same seed, then adapt random seed for each node */
    MPI_Bcast(&mcseed, 1, MPI_LONG, 0, MPI_COMM_WORLD); /* root sends its seed to slaves */
    mcseed += mpi_node_rank; /* make sure we use different seeds per noe */
  }
#endif /* USE_MPI */

#ifdef OPENACC
#ifdef USE_MPI
  int num_devices = acc_get_num_devices(acc_device_nvidia);
  if(num_devices>0){
    int my_device = mpi_node_rank % num_devices;
    acc_set_device_num( my_device, acc_device_nvidia );
    printf("Have found %d GPU devices on rank %d. Will use device %d.\n", num_devices, mpi_node_rank, my_device);
  }else{
    printf("There was an issue probing acc_get_num_devices, fallback to host\n");
    acc_set_device_type( acc_device_host );
  }
#endif
#endif

  /* *** parse options ******************************************************* */
  SIG_MESSAGE("[" __FILE__ "] main START");
  mcformat = getenv(FLAVOR_UPPER "_FORMAT") ?
             getenv(FLAVOR_UPPER "_FORMAT") : FLAVOR_UPPER;
  instrument_exe = argv[0]; /* store the executable path */
  /* read simulation parameters and options */
  mcparseoptions(argc, argv); /* sets output dir and format */


#ifdef USE_MPI
  if (mpi_node_count > 1) {
    /* share the same seed, then adapt random seed for each node */
    MPI_Bcast(&mcseed, 1, MPI_LONG, 0, MPI_COMM_WORLD); /* root sends its seed to slaves */
    mcseed += mpi_node_rank; /* make sure we use different seeds per node */
  }
#endif


/* *** install sig handler, but only once !! after parameters parsing ******* */
#ifndef NOSIGNALS
#ifdef SIGQUIT
  if (signal( SIGQUIT ,sighandler) == SIG_IGN)
    signal( SIGQUIT,SIG_IGN);   /* quit (ASCII FS) */
#endif
#ifdef SIGABRT
  if (signal( SIGABRT ,sighandler) == SIG_IGN)
    signal( SIGABRT,SIG_IGN);   /* used by abort, replace SIGIOT in the future */
#endif
#ifdef SIGTERM
  if (signal( SIGTERM ,sighandler) == SIG_IGN)
    signal( SIGTERM,SIG_IGN);   /* software termination signal from kill */
#endif
#ifdef SIGUSR1
  if (signal( SIGUSR1 ,sighandler) == SIG_IGN)
    signal( SIGUSR1,SIG_IGN);   /* display simulation status */
#endif
#ifdef SIGUSR2
  if (signal( SIGUSR2 ,sighandler) == SIG_IGN)
    signal( SIGUSR2,SIG_IGN);
#endif
#ifdef SIGHUP
  if (signal( SIGHUP ,sighandler) == SIG_IGN)
    signal( SIGHUP,SIG_IGN);
#endif
#ifdef SIGILL
  if (signal( SIGILL ,sighandler) == SIG_IGN)
    signal( SIGILL,SIG_IGN);    /* illegal instruction (not reset when caught) */
#endif
#ifdef SIGFPE
  if (signal( SIGFPE ,sighandler) == SIG_IGN)
    signal( SIGSEGV,SIG_IGN);    /* floating point exception */
#endif
#ifdef SIGBUS
  if (signal( SIGBUS ,sighandler) == SIG_IGN)
    signal( SIGSEGV,SIG_IGN);    /* bus error */
#endif
#ifdef SIGSEGV
  if (signal( SIGSEGV ,sighandler) == SIG_IGN)
    signal( SIGSEGV,SIG_IGN);   /* segmentation violation */
#endif
#endif /* !NOSIGNALS */


  // init executed by master/host
  siminfo_init(NULL); /* open SIM */
  SIG_MESSAGE("[" __FILE__ "] main INITIALISE");
  init();


#ifndef NOSIGNALS
#ifdef SIGINT
  if (signal( SIGINT ,sighandler) == SIG_IGN)
    signal( SIGINT,SIG_IGN);    /* interrupt (rubout) only after INIT */
#endif
#endif /* !NOSIGNALS */

/* ================ main particle generation/propagation loop ================ */
#ifdef USE_MPI
  /* sliced Ncount on each MPI node */
  mcncount = mpi_node_count > 1 ?
    floor(mcncount / mpi_node_count) :
    mcncount; /* number of rays per node */
#endif

// MT specific init, note that per-ray init is empty
#if RNG_ALG == 2
  mt_srandom(mcseed);
#endif


// main raytrace work loop
#ifndef FUNNEL
  // legacy version
  raytrace_all(mcncount, mcseed);
#else
  MPI_MASTER(
  // "funneled" version in which propagation is more parallelizable
  printf("\nNOTE: CPU COMPONENT grammar activated:\n 1) \"FUNNEL\" raytrace algorithm enabled.\n 2) Any SPLIT's are dynamically allocated based on available buffer size. \n");
	     );
  raytrace_all_funnel(mcncount, mcseed);
#endif


#ifdef USE_MPI
 /* merge run_num from MPI nodes */
  if (mpi_node_count > 1) {
  double mcrun_num_double = (double)mcrun_num;
  mc_MPI_Sum(&mcrun_num_double, 1);
  mcrun_num = (unsigned long long)mcrun_num_double;
  }
#endif


  // save/finally executed by master node/thread/host
  finally();


#ifdef USE_MPI
  MPI_Finalize();
#endif /* USE_MPI */


  return 0;
} /* mccode_main */
/* End of file "mccode_main.c". */

/* end of generated C code ./Conditional_test.c */
