model_config_h

/*******************************************************************************
**	Copyright 1995-2015, Fermi Research Alliance, LLC.  All rights reserved.
*******************************************************************************/

#ifndef MODEL_CONFIG_DEFINED		/* do this only once. */
#define	MODEL_CONFIG_DEFINED

#include "clib.h"
#include "cns_data_structs.h"
#define LEN_MODE_TEXT		26
#define TEV_LEN_MODE_TEXT	LEN_MODE_TEXT	/* text length from tev_mode_text(). */

/********** machine definitions. ************/
/* List of machines available. */
#define MODEL_TEV_RUN_I         1
#define MODEL_TEV               2     /* For Collider mode. */
#define MODEL_TEV_FIXED_TARGET  3
#define MODEL_MAIN_INJECTOR     4     
#define MODEL_8GEV_LINE         5
#define MODEL_RECYCLER          6     
#define MODEL_ACCUMULATOR       7     /* Based on lattice from 6/97. */
#define MODEL_BOOSTER           8     /* Presently not supported. */
#define MODEL_TROMBONE_APPLICATION 9  /* Just for launching this application, no model */
#define MODEL_AP2_LINE		10
#define MODEL_400MEV_LINE	11

/********** lattice which_lattice definitions. ***********/
/* List of lattices available. */

/* 
** tev specific lattices
** The numbering of these lattices got fouled up in the translations done in early 2006.
*/

/** Tevatron in fixed target configuration **/
#define TEV_FIX 	1	/* fixed target. */

/** Tevatron in Collider Run I configuration **/
#define TEV_BD1	        2	/* collider B0 & D0; step 1,  no helix.   */
#define TEV_BD2 	3	/* collider B0 & D0; step 2,  no helix.   */
#define TEV_BD3 	4	/* collider B0 & D0; step 3,  no helix.   */
#define TEV_BD4 	5	/* collider B0 & D0; step 4,  no helix.   */
#define TEV_BD5 	6	/* collider B0 & D0; step 5,  no helix.   */
#define TEV_BD6 	7	/* collider B0 & D0; step 6,  no helix.   */
#define TEV_BD7 	8	/* collider B0 & D0; step 7,  no helix.   */
#define TEV_BD8 	9	/* collider B0 & D0; step 8,  no helix.   */
#define TEV_BD9 	10	/* collider B0 & D0; step 9,  no helix.   */
#define TEV_BD10 	11	/* collider B0 & D0; step 10, no helix.   */
#define TEV_BD11 	12 	/* collider B0 & D0; step 11, no helix.   */
#define TEV_BD12 	13 	/* collider B0 & D0; step 12, no helix.   */
#define TEV_BD13 	14	/* collider B0 & D0; step 13, no helix.   */
#define TEV_BD14 	15	/* collider B0 & D0; step 14, no helix.   */
#define TEV_BD15 	16	/* collider B0 & D0; step 15, no helix.   */
#define TEV_BD16 	17	/* collider B0 & D0; step 16, no helix.   */
#define TEV_BD17	18 	/* collider B0 & D0; step 17, no helix.   */
#define TEV_BDH1        19      /* collider B0 & D0; step 1,  with helix. */
#define TEV_BDH2        20	/* collider B0 & D0; step 2,  with helix. */
#define TEV_BDH3        21	/* collider B0 & D0; step 3,  with helix. */
#define TEV_BDH4        22	/* collider B0 & D0; step 4,  with helix. */
#define TEV_BDH5        23	/* collider B0 & D0; step 5,  with helix. */
#define TEV_BDH6        24	/* collider B0 & D0; step 6,  with helix. */
#define TEV_BDH7        25	/* collider B0 & D0; step 7,  with helix. */
#define TEV_BDH8        26	/* collider B0 & D0; step 8,  with helix. */
#define TEV_BDH9        27	/* collider B0 & D0; step 9,  with helix. */
#define TEV_BDH10       28	/* collider B0 & D0; step 10, with helix. */
#define TEV_BDH11       29	/* collider B0 & D0; step 11, with helix. */
#define TEV_BDH12       30	/* collider B0 & D0; step 12, with helix. */
#define TEV_BDH13       31	/* collider B0 & D0; step 13, with helix. */
#define TEV_BDH14       32	/* collider B0 & D0; step 14, with helix. */
#define TEV_BDH15       33	/* collider B0 & D0; step 15, with helix. */
#define TEV_BDH16       34	/* collider B0 & D0; step 16, with helix. */
#define TEV_BDH17       35	/* collider B0 & D0; step 17, with helix. */

/** Tevatron in Collider Run II configuration **/

#define TEV01      37      /* Collider Run II lattice; step 1, no helix */
#define TEV02      38      /* Collider Run II lattice; step 2, no helix */
#define TEV03      39      /* Collider Run II lattice; step 3, no helix */
#define TEV04      40      /* Collider Run II lattice; step 4, no helix */
#define TEV05      41      /* Collider Run II lattice; step 5, no helix */
#define TEV06      42      /* Collider Run II lattice; step 6, no helix */
#define TEV07      43      /* Collider Run II lattice; step 7, no helix */
#define TEV08      44      /* Collider Run II lattice; step 8, no helix */
#define TEV09      45      /* Collider Run II lattice; step 9, no helix */
#define TEV10      46      /* Collider Run II lattice; step 10, no helix*/
#define TEV11      47      /* Collider Run II lattice; step 11, no helix*/
#define TEV12      48      /* Collider Run II lattice; step 12, no helix*/
#define TEV13      49      /* Collider Run II lattice; step 13, no helix*/
#define TEV14      50      /* Collider Run II lattice; step 14, no helix*/
#define TEV15      51      /* Collider Run II lattice; step 15, no helix*/
#define TEV16      52      /* Collider Run II lattice; 
                                -store, with collimator phase bumps, no helix*/
#define TEV_COUPLED_LOWBETA	53

#define	MAX_TEV_MODES	52	/* # of Tev lattices available. */

#define TEV_INJECTION_HELIX  1
#define TEV_COLLISION_HELIX  2

/*
** Main Injector lattices
*/
#define MAX_MI_LATTICES     5
#define MI_LEN_MODE_TEXT	LEN_MODE_TEXT	/* text length from mi_mode_text(). */

#define MI_8_GEV         1   /* MI ring 8 GeV */
#define MI_26_GEV        2   /* MI ring 26 GeV */
#define MI_50_GEV        3   /* MI ring 50 GeV */
#define MI_120_GEV       4   /* MI ring 120 GeV */
#define MI_150_GEV       5   /* MI ring 150 GeV */
                              /* These are for experimentally determined transfer
                                  constants    */
#define MI_8_GEV_EXP     6   /* MI ring 8 GeV */
#define MI_26_GEV_EXP    7   /* MI ring 26 GeV */
#define MI_50_GEV_EXP    8   /* MI ring 50 GeV */
#define MI_120_GEV_EXP   9   /* MI ring 120 GeV */
#define MI_150_GEV_EXP   10  /* MI ring 150 GeV */

/* THE FOLLOWING MI BEAMLINES ARE NOT IMPLEMENTED YET */
#define MI_M10      2  /* MI as a beamline sector 10 only.         */
#define MI_M10_M20  3  /* MI as a beamline from sector 10 to sector 20.  */
#define MI_M10_M30  4  /* MI as a beamline from sector 10 to sector 30.  */
#define MI_M10_M40  5  /* MI as a beamline from sector 10 to sector 40.  */
#define MI_M10_M50  6  /* MI as a beamline from sector 10 to sector 50.  */
#define MI_M10_M60  7  /* MI as a beamline from sector 10 to sector 60.  */

/*
** 8 GeV Line lattices
*/
#define INJ_8GEV_PM        1    /* Booster -> Main Injector beamline. */
#define INJ_8GEV_MR        2    /* Syphers Booster->Mainring.         */
#define INJ_8GEV_EXP       3    /* Booster -> Main Injector beamline with experimentally determined
                        		      transfer constants. */

/*
** Recycler lattices
*/
#define RECYCLER        1

/*
** Accumulator
*/
#define ACCUMULATOR_2        1
#define ACCUMULATOR_LATTICE  2
#define AP2_LINE_LATTICE     3

/*
** Booster
*/
#define BOOSTER          1
#define B400_MEV_LATTICE 2

/*
** This header file contains general UL_MODEL defines as well as #includes
** of all the other headers used in UL_MODEL.  They have been grouped by
** function in order to help the user better grasp what #defines go with
** what routines.
*/

/********* which_data. ************/
/* What kind of data can be gotten from the model.  */

#define MODEL_TWISS_DATA      1     /* Use the MODEL_TWISS_STRUCT to retrieve 
				    ** this data.  The data returned
				    ** is DEPENDENT on the setting of
				    ** the filter.
				    */
#define MODEL_ORBIT_DATA      2     /* Use the MODEL_ORBIT_STRUCT to
                                    ** retrieve this data.  The data
                                    ** returned is DEPENDENT on the
                                    ** setting of the filter.
				    */
#define MODEL_RING_DATA       3     /* Use the MODEL_RING_STRUCT to
                                    ** retrieve this data.  Since this
                                    ** data represents parameters of
                                    ** the whole machine (tune, etc.),
                                    ** the setting of the filter is
                                    ** ignored. 
				    */
#define MODEL_CIRCUIT_DATA    4     /* Use the
                                    ** MODEL_EDIT_CIRCUIT_STRUCT to
                                    ** retrieve this data.  The data
                                    ** returned is INDEPENDENT of the
                                    ** setting of the filter.  The
                                    ** same #define can be used for
                                    ** both getting circuit data and
                                    ** setting circuit data.
				    */
#define MODEL_ELEMENT_DATA    5     /* Use the
                                    ** MODEL_EDIT_ELEMENT_STRUCT to
                                    ** retrieve this data.  The data
                                    ** returned is DEPENDENT of the
                                    ** setting of the filter.  If the
                                    ** filter is set to non-editable
                                    ** elements, it will default to
                                    ** horizontal correction elements.
                                    ** The same #define can be used
                                    ** for both getting element data
                                    ** and setting element data. 
				    */
#define MODEL_BEAMBEAM_DATA   6
#define MODEL_RMATRIX_DATA    7     /* Use the MODEL_R_MATRIX_STRUCT
                                    ** to retrieve this data.  The
                                    ** data returned is DEPENDENT of
                                    ** the setting of the filter.
				    */
#define MODEL_LUMINOSITY      8     /* Use the MODEL_LUMINOSITY_STRUCT
                                    ** to retrieve this data.  The
                                    ** data returned is INDEPENDENT of
                                    ** the setting of the filter. 
				    */
#define MODEL_LUMINOSITY_DIST 9
#define MODEL_LIST_DATA       10    /* use MODEL_LIST_STRUCT.         */
#define MODEL_RASTER_QUERY    11    /* use MODEL_RASTER_SCALE_STRUCT. */
#define MODEL_SWIC_QUERY      12    /* use MODEL_RASTER_SCALE_STRUCT! */
#define MODEL_TRANSFER_CONST  13    /* use MODEL_TRANSFER_CONST_STRUCT to
                                    ** retrieve the transfer constants
				    ** for a particular machine.  The
				    ** names will correspond to ACNET
				    ** names and the transfer constants
				    ** will be in units of:
                                    ** quadrupoles  : Tesla/meter/Amp
				    ** dipole corr  : radians/Amp
				    ** sbends/rbends: Tesla/Amp
				    **
				    ** The value of the filter is ignored.
				    */
#define MODEL_INIT_TWISS      23    /* Use the MODEL_TWISS_STRUCT to
                                    ** retrieve this data.  The data
                                    ** returned is independent of the
                                    ** setting of the filter as it
                                    ** represents the initial Twiss of
                                    ** the beamline or ring.  The same
                                    ** #define can be used for both
                                    ** setting and getting data. 
				    */
#define MODEL_INIT_LAUNCH     24    /* Use the MODEL_ORBIT_STRUCT to
                                    ** retrieve this data.  The data
                                    ** returned is independent of the
                                    ** setting of the filter as it
                                    ** represents the initial
                                    ** trajectory of the beamline or
                                    ** the fixed point of the ring.
                                    ** The same #define can be used
                                    ** for both setting and getting
                                    ** data.
				    */
#define MODEL_BEAMSIG_DATA    25    /* Use the MODEL_BEAMSIG_STRUCT to 
				    ** retrieve this data.  The data will
				    ** be returned for the locations specified
				    ** by the filter.  The data returned is 
				    ** technically the covariance matrix, so
				    ** the diagonal terms are the beam sigma
				    ** squared.  The same #define can be used
				    ** for both set and get.
				    */
#define MODEL_COMB_FUNC_QUERY 26    /* THIS IS NOT SUPPORTED YET!!
				    ** use MODEL_PARAMETER_STRUCT.
				    */
#define MODEL_TOTAL_PHASE_ADV 27    /* Use the MODEL_TWISS_STRUCT to
                                    ** retrieve this data.  The data
                                    ** returned is independent of the
                                    ** setting of the filter as it
                                    ** represents the Twiss at the end
                                    ** of the beamline or ring.  of
                                    ** the beamline or ring.  It
                                    ** includes the total phase
                                    ** advance around the riing.  The
                                    ** same #define can be used for
                                    ** both setting and getting
                                    ** data. 
				    */
#define MODEL_SAVE_FILE       28    /* Used for retieving a list of files
				    ** that can be used to restore the model
				    ** to a previous state.  Information is 
				    ** passed in the MODEL_SAVE_FILE_STRUCT.
				    */
#define MODEL_INIT_SIGMA      29    /* Used for retrieving the initial beam
				    ** sigmas at the start of the line.  Data
				    ** is sent back using the 
				    ** MODEL_ORBIT_STRUCT.  The setting of
				    ** the filter has NO bearing on the data
				    ** retrieved.
				    */
#define MODEL_CALC_LAUNCH     30    /* Used for retrieving the initial launch
				    ** data at the start of the line as 
				    ** calculated by backpropagating BPMs.  Data
				    ** is sent back using the 
				    ** MODEL_ORBIT_STRUCT.  The setting of
				    ** the filter has NO bearing on the data
				    ** retrieved.
				    */
#define MODEL_TUNE_DATA       31   /* Only fills the tunes part of the
				   ** MODEL_RING_DATA struct.
				   */
#define MODEL_PROP_ORBIT      32   /* Propagates a single particle from
				    * the beginning of the beamline and
				    * modifies the orbit data as if a closed
				    * orbit has been calculated 
				    */
#define MODEL_PROP_TWISS      33   /* Propagates the initial twiss functions
				    * from the beginning of the beamline
				    * and modifies the twiss data as if
				    * a closed orbit has been calculated
				    * and closed-orbit twiss data has been
				    * calculated
				    */
#define MODEL_TARGET_TUNE     34   /* Change the lattice so that the target
				    * tunes are achieved.
				    */

#define MODEL_PROP_BEAMSIG    35   /* Propagates the initial covariance matrix
				    * from the beginning of the beamline
				    * and modifies the cov matrix as if
				    * a closed orbit has been calculated
				    * and closed-orbit cov matrix has been
				    * calculated
				    */
#define MODEL_INIT_COVARIANCE  36  /* Initial covariance matrix for this beamline */

#define MODEL_ADJUSTOR_DATA    37     /* Use the
				      ** MODEL_EDIT_ADJUSTOR_STRUCT to
				      ** retrieve this data.  
				      ** The same #define can be used
				      ** for both getting element data
				      ** and setting element data. 
				      */

#define MODEL_ACCURACY         38    /* The type of calculation to be done */
/********** which_parameter. ***********/
/* What kind of parameters can be set.   */

#define MODEL_TEV_SEPARATOR_ON    1     /* This is a switch that has
					** no data associated with it.
					** It turns the separators on.
					*/
#define MODEL_TEV_SEPARATOR_OFF   2     /* This is a switch that has
					** no data associated with it.
					** It turns the separators
					** off.
					*/
#define MODEL_TEV_SEP_SWITCH_POLARITY 3 /* This is a switch that has
					** no data associated with it.
					** It switches the polarity of
					** separators.
					*/
#define MODEL_CIRCUIT_DATA        4     /* This is used to set circuit
					** data using the
					** MODEL_EDIT_CIRCUIT_STRUCT.
					** This #define can be used
					** for 'set' and 'get'.
					*/
#define MODEL_ELEMENT_DATA        5     /* This is used to set element
					** data using the
					** MODEL_EDIT_ELEMENT_STRUCT.
					** This #define can be used
					** for 'set' and 'get'.
					*/
#define MODEL_BUNCH_INTENSITY     6     /* This can be used to set the
					** intensity of the Proton and
					** Pbar bunches when using the
					** Tevatron Collider model.
					** The settings are done via
					** the MODEL_PARAMETER_STRUCT
					** putting the name in the
					** 'name' part and the new value
					** in the 'valueD' part.
					*/
#define MODEL_FLYING_WIRE_SIGMA   7     /* use MODEL_PARAMETER_STRUCT. use   */
                                        /* name and valueD in struct.        */
#define MODEL_P_NUMBER_BUNCH      8     /* use MODEL_PARAMETER_STRUCT. use   */
					/* valueI in struct.                 */
#define MODEL_PBAR_NUMBER_BUNCH   9     /* use MODEL_PARAMETER_STRUCT. use
                                        ** valueI in struct.
                                        */
#define MODEL_INSERT_PLOT         10    /* use MODEL_PARAMETER_STRUCT. use
                                        ** valueI in struct.
                                        */
#define MODEL_DISPLAY             11    /* use MODEL_PARAMETER_STRUCT.  use
                                        ** name to pass W-window display name.
                                        */
#define MODEL_NUMBER_PARTICLES    12    /* use MODEL_PARAMETER_STRUCT.  use
                                        ** valueI to pass the number.
                                        */
#define MODEL_NUMBER_TURNS        13   /* use MODEL_PARAMETER_STRUCT.  */
#define MODEL_DELETE_PLOT         14   /* use MODEL_PARAMETER_STRUCT.  */
#define MODEL_RASTER_SET          15   /* use MODEL_PARAMETER_STRUCT.  */
#define MODEL_INSERT_SWIC         16   /* use MODEL_PARAMETER_STRUCT.  */
#define MODEL_SET_SWIC            17   /* use MODEL_PARAMETER_STRUCT.  */
#define MODEL_SEND_COMMAND        18   /* use MODEL_PARAMETER_STRUCT.  */
#define MODEL_SWIC_SET            19
#define MODEL_DELETE_SWIC         20   /* use MODEL_PARAMETER_STRUCT.    */
#define MODEL_CIRCUIT_CURRENT     21   /* Use the
                                       ** MODEL_EDIT_CIRCUIT_STRUCT to
                                       ** when setting this data.
                                       ** This can be used to set the
                                       ** current of a circuit
                                       ** previously retrieved using
                                       ** MODEL_CIRCUIT_DATA.
				       */
#define MODEL_ELEMENT_CURRENT 22       /* Use the
                                       ** MODEL_EDIT_ELEMENT_STRUCT to
                                       ** when setting this data.
                                       ** This can be used to set the
                                       ** current of an element
                                       ** previously retrieved using
                                       ** MODEL_ELEMENT_DATA.
				       */
#define MODEL_INIT_TWISS          23   /* use MODEL_TWISS_STRUCT. */
                                       /* This can be used for set and get. */
#define MODEL_INIT_LAUNCH         24   /* use MODEL_ORBIT_STRUCT.  */ 
                                       /* This can be used for set and get. */
#define MODEL_BEAMSIG_DATA        25   /* This can be used for set and get.
				       ** use the MODEL_BEAMSIG_STRUCT.
				       */
#define MODEL_SAVE_FILE           28   /* Used for saving the present model
				       ** lattice to a file on the compute 
				       ** server.  Information is passed in
				       ** the MODEL_SAVE_FILE_STRUCT.
				       */
#define MODEL_USE_FILE            29   /* Used for selecting the particular
				       ** file to use for startup.  
				       ** Information is passed in
				       ** the MODEL_SAVE_FILE_STRUCT.
				       */
#define MODEL_DP_OVER_P           30   /* Used for setting the DP/P of the 
				       ** closed orbit particles.  Normally
                                       ** this is zero.  Information is 
				       ** passed in the MODEL_ORBIT_STRUCT.
				       */
#define MODEL_SET_INIT_SIGMA	31     /* use MODEL_ORBIT_STRUCT.  This can 
				       ** be used for setting the initial 
				       ** beam sigmas after a calculation
				       ** of the initial beam sigma.
				       */

#define MODEL_LAUNCH_FILE       32    /* use MODEL_PARAMETER_STRUCT.  This can 
				      ** be used for setting the BPM file
				      ** number to be retrieved from database
				      ** tables already written using 
				      ** model_set_data(MODEL_SET_BPM_DATA...).
				       */

#define MODEL_TARGET_TUNES	33	/* use MODEL_RING_STRUCT.  Used to 
					 * initiate the calculation to bring
					 * the lattice in to the target tune
					 * values.
					 */


/********** which_command. **********/
/* What commands can be sent.    */

/*
** Both of the commands below are useful for displaying data when SWICS or 
** phasespace monitors are in the beamline.  These should not be used to 
** get orbit data, as the data produced is not saved or written to any file.
** Use the MODEL_ORBIT_DATA to get orbit data.
*/

#define MODEL_CMD_TRACK           1   /* Track a proton through the machine. */
#define MODEL_CMD_TRACK_SWYD      2   /* Track a proton through switchyard. */

/********** filter. ***********/
/* Model element filter.   */

#define MODEL_FILTER_ALL         0x00000000
#define MODEL_FILTER_DRIFT       0x00000001
#define MODEL_FILTER_QUAD        0x00000002
#define MODEL_FILTER_SEXT        0x00000004
#define MODEL_FILTER_HCORR       0x00000008
#define MODEL_FILTER_VCORR       0x00000010
#define MODEL_FILTER_HBPM        0x00000020
#define MODEL_FILTER_VBPM        0x00000040
#define MODEL_FILTER_BEND        0x00000080
#define MODEL_FILTER_MARKER      0x00000100
#define MODEL_FILTER_OCTUPOLE    0x00000200
#define MODEL_FILTER_COMBINED    0x00000400
#define MODEL_FILTER_RFCAVITY    0x00000800
#define MODEL_FILTER_FLYING_WIRE 0x00001000
#define MODEL_FILTER_SYNC_LIGHT  0x00002000
#define MODEL_FILTER_SEPTA       0x00004000
#define MODEL_FILTER_LAMBERTSON  0x00008000
#define MODEL_FILTER_RASTER      0x00010000
#define MODEL_FILTER_TCL         0x00020000
#define MODEL_FILTER_MWIRE       0x00040000
#define MODEL_FILTER_MONITOR     0x00080000
#define MODEL_FILTER_SEPARATOR   0x00100000
#define MODEL_FILTER_COLLIMATOR  0x00200000

/***** which_line definitions. Useful for Tevatron in Fixed Target mode. ****/
/* Model beamline filter.   */

#define MODEL_BEAMLINE_RING           0x00000001
#define MODEL_BEAMLINE_TEV_NEUTRINO   0x00000002
#define MODEL_BEAMLINE_TEV_PROTON     0x00000004
#define MODEL_BEAMLINE_TEV_MESON      0x00000008
#define MODEL_BEAMLINE_TEV_MUON       0x00000010

/* Infinite s value */
#define MODEL_REALLY_BIG_S_VALUE 9.87654321012E+30

/* #DEFINES to be used for the MODEL_SET_DATA() routine. */

#define MODEL_SET_BEAMSIG_DATA 1    /* Use the MODEL_PARAMETER_STRUCT to 
				    ** send this data with the ACNET name of 
				    ** the multiwire/SWIC in the name field
				    ** and the beam sigma in the valueD field.
				    */
#define MODEL_SET_BPM_DATA     2    /* Used for sending BPM data to the model
				    ** via Sybase tables for calculations of
				    ** the initial launch.  Use the 
				    ** MODEL_PARAMETER_STRUCT to send the data.
				    ** The file number must be the first 
				    ** element of the struct* sent over.  Fill
				    ** in the valueI with the BPM file number
				    ** and leave the name blank.  The BPM data
				    ** follows with the ACNET BPM name copied
				    ** into 'name' and the BPM reading in 
				    ** valueD.  The number of rows sent should
				    ** include ONLY the BPM data and not
				    ** include the file number entry.
				    */



/* What kind of structures the data comes back in.  */

#define MODEL_LATTICE_NAME_LENGTH    60
#define MODEL_AUTHOR_NAME_LENGTH     30
#define MODEL_ELEMENT_NAME_LENGTH    50
#define MODEL_FILE_DESCRIPTION_LENGTH    60

/*
** 'element_type' and circuit 'type'  can be a character string such as:
**  "hkick"
**  "octupole"
**  "thinrfcavity"
**  "vkick"
**  "monitor"
**  "hmonitor"
**  "vmonitor"
**  "marker"
**  "drift"
**  "rbend"
**  "sbend"
**  "quadrupole"
**  "thinQuad"
**  "thinSextupole"
**  "thinOctupole"
**  "thinDecapole"
**  "thin12pole"
**  "thin14pole"
**  "thin16pole"
**  "thin18pole"
**  "thinMultipole"
**  "sextupole"
**  "thinSeptum"
**  "thinLamb"
*/

typedef struct {
  int slot;
  char element_name[MODEL_ELEMENT_NAME_LENGTH];
  char element_type[MODEL_LATTICE_NAME_LENGTH];   /* See above */
  double s_value;   /* Units of meters  */
  double alpha_x;   /* unitless */
  double beta_x;    /* Units of meters  */
  double psi_x;     /* units of radians */
  double disp_x;    /* units of meters  */
  double d_prime_x; /* unitless */
  double alpha_y;   /* unitless */
  double beta_y;    /* units of meters  */
  double psi_y;     /* units of radians */
  double disp_y;    /* units of meters  */
  double d_prime_y; /* unitless */
} __attribute__((packed)) /* Added by the PACKINATOR(tm) */ MODEL_TWISS_STRUCT;

typedef struct {
  int slot;
  char element_name[MODEL_ELEMENT_NAME_LENGTH];
  char element_type[MODEL_LATTICE_NAME_LENGTH];  /* See above */
  double s_value;   /* units of meters */
  double x;         /* units of meters */
  double x_prime;   /* units of radians*/
  double y;         /* units of meters */
  double y_prime;   /* units of radians*/
  double c_delta_t; /* units of meters */
  double delta_p;   /* unitless        */
} __attribute__((packed)) /* Added by the PACKINATOR(tm) */ MODEL_ORBIT_STRUCT;

typedef struct {
  int slot;
  char element_name[MODEL_ELEMENT_NAME_LENGTH];
  char element_type[MODEL_LATTICE_NAME_LENGTH];   /* See above */
  double s_value;    /* units of meters */
  /* The matrix elements are the the top 'half' of the covariance matrix
  ** including the diagonal where the covariance matrix is:
  **  x*x        x*y      x*cDeltaT        x*x'       x*y'        x*dp/p
  **  y*x        y*y      y*cDeltaT        y*x'       y*y'        y*dp/p
  **  cDeltaT*x cDeltaT*y cDeltaT*cDeltaT cDeltaT*x' cDeltaT*y' cDeltaT*dp/p
  **  x'*x       x'*y     x'*cDeltaT       x'*x'      x'*y'       x'*dp/p
  **  y'*x       y'*y     y'*cDeltaT       y'*x'      y'*y'       y'*dp/p
  **  dp/p*x     dp/p*y   dp/p*cDeltaT     dp/p*x'    dp/p*y'     dp/p*dp/p
  **
  ** matrix[0] = x*x
  ** matrix[1] = x*y
  ** matrix[2] = x*cDeltaT
  ** matrix[3] = x*x'
  ** matrix[4] = x*y'
  ** matrix[5] = x*dp/p

  ** matrix[6] = y*y
  ** matrix[7] = y*cDeltaT
  ** matrix[8] = y*x'
  ** matrix[9] = y*y'
  ** matrix[10]= y*dp/p

  ** matrix[10]= cDeltaT*cDeltaT
  ** matrix[11]= cDeltaT*x'
  ** matrix[12]= cDeltaT*y'
  ** matrix[13]= cDeltaT*dp/p

  **  and so on...
  */
  double matrix[21];  
} __attribute__((packed)) /* Added by the PACKINATOR(tm) */ MODEL_BEAMSIG_STRUCT;

typedef struct {
  double nu_x;     /* Tune in the X plane. */
  double nu_y;     /* Tune in the Y plane. */
  double c_x;      /* Chromaticity in the X plane. */
  double c_y;      /* Chromaticity in the Y plane. */
  double gamma_t;  /* GammaT for the machine, if it is a ring. */
  double p_synch;  /* Momentum of the particle used in the table generation. */
} __attribute__((packed)) /* Added by the PACKINATOR(tm) */ MODEL_RING_STRUCT;

/* 
** strength is in MKS units:
**  "hkick"         radians
**  "octupole"      ??
**  "thinrfcavity"  Not applicable
**  "vkick"         radians
**  "monitor"       Not applicable
**  "hmonitor"      Not applicable
**  "vmonitor"      Not applicable
**  "marker"        Not applicable
**  "drift"         Not applicable
**  "rbend"         Tesla
**  "sbend"         Tesla
**  "quadrupole"    Tesla/meter
**  "thinQuad"      Tesla (zero length quadrupole)
**  "thinSextupole" Tesla/meter (zero length sextupole)
**  "thinOctupole"  Tesla/meter/meter (zero length octupole)
**  "sextupole"     Tesla/meter/meter 
**  "thinSeptum"    radians
**  "thinLamb"      Not applicable
*/
typedef struct {
  char name[MODEL_ELEMENT_NAME_LENGTH];
  char type[MODEL_LATTICE_NAME_LENGTH];  /* See above */
  double strength;    /* See above */
  double current;     /* units in amperes or normal ACNET engineering units. */
  double x_offset;    /* units in meters   */
  double y_offset;    /* units in meters   */
  double roll;        /* units in radians  */
} __attribute__((packed)) /* Added by the PACKINATOR(tm) */ MODEL_EDIT_CIRCUIT_STRUCT;

typedef struct {
  char name[MODEL_ELEMENT_NAME_LENGTH];
  char element_type[MODEL_LATTICE_NAME_LENGTH];  /* See above */
  double s_value;     /* units in meters */
  int slot;
  double length;      /* Needed for some types of calculations */
  double strength;    /* See above */
  double current;     /* units in amperes or normal ACNET engineering units. */
  double x_offset;    /* units in meters  */
  double y_offset;    /* units in meters  */
  double roll;        /* units in radians */
} __attribute__((packed)) /* Added by the PACKINATOR(tm) */ MODEL_EDIT_ELEMENT_STRUCT;

typedef struct {
  int slot;
  char element_name[MODEL_ELEMENT_NAME_LENGTH];
  char element_type[MODEL_LATTICE_NAME_LENGTH];  /* See above */
  double s_value;     /* units in meters */
  double matrix[36];
} __attribute__((packed)) /* Added by the PACKINATOR(tm) */ MODEL_R_MATRIX_STRUCT;

typedef struct {
  int    slot;
  char   name[MODEL_ELEMENT_NAME_LENGTH];
  char   element_type[MODEL_LATTICE_NAME_LENGTH];
  double s_value;    /* units in meters */
} __attribute__((packed)) /* Added by the PACKINATOR(tm) */ MODEL_LIST_STRUCT;

/* Use the following struct for things which don't fit in the  */
/* the previous structs. */

typedef struct {
  char* name;
  double valueD;
  int valueI;
} __attribute__((packed)) /* Added by the PACKINATOR(tm) */ MODEL_PARAMETER_STRUCT;

typedef struct {
  double B0Lum;
  double D0Lum;
  int npts;
  double* B0Dist;
  double* D0Dist;
} __attribute__((packed)) /* Added by the PACKINATOR(tm) */ MODEL_LUMINOSITY_STRUCT;

/* The next struct does double-duty for both RASTER and SWIC requests. */
typedef struct {
  int    slot;
  char   name[MODEL_ELEMENT_NAME_LENGTH];
  double limits[12];
} __attribute__((packed)) /* Added by the PACKINATOR(tm) */ MODEL_RASTER_SCALE_STRUCT;

typedef struct {
  char   machine[MODEL_LATTICE_NAME_LENGTH];
  char   name[MODEL_LATTICE_NAME_LENGTH];
  double ItoFieldValue;
} __attribute__((packed)) /* Added by the PACKINATOR(tm) */ MODEL_TRANSFER_CONST_STRUCT;

typedef struct {
  int    file_number;
  char   file_name[MODEL_LATTICE_NAME_LENGTH];
  char   author[MODEL_AUTHOR_NAME_LENGTH];
  char   description[MODEL_FILE_DESCRIPTION_LENGTH];
} __attribute__((packed)) /* Added by the PACKINATOR(tm) */ MODEL_SAVE_FILE_STRUCT;

typedef struct {
  char name[MODEL_ELEMENT_NAME_LENGTH];
  char element_type[MODEL_LATTICE_NAME_LENGTH];  /* See above */
  double s_value;     /* units in meters */
  int slot;
  int h_tune_adjust;
  int v_tune_adjust;
  int h_chrom_adjust;
  int v_chrom_adjust;
} __attribute__((packed)) /* Added by the PACKINATOR(tm) */ MODEL_EDIT_ADJUSTOR_STRUCT;

/* Macros for memory management  */

#ifdef DEVEL    /* for development purposes. */
#define MY_CLIB_FREE(x)  clib_free(&x)
#define MY_CLIB_CALLOC   clib_calloc
#else           /* for normal operational version. */
#include <stdlib.h>
#define MY_CLIB_FREE(x)  free(x)
#define MY_CLIB_CALLOC   calloc
#endif

/*****************************************************************************
**  prototypes for online model
**
******************************************************************************/

extern "C" {

int model_tev_lattice_menu(int row, int col);
int model_mi_lattice_menu(int row, int col);
int tev_mode_text(int mode, char *text);
int mi_mode_text(int mode, char *text);

/******************************************************************************
**  prototypes for lattice database query
**
******************************************************************************/

int model_db_set_machine(int which_model, int which_lattice);
int model_db_set_machine_cpp(int which_model, int which_lattice,
			     char *which_client);
int model_db_set_beamline(int which_line);
int model_db_query(int which_data, int filter, void** value,
		   int* num_rows, char** err_msg);
int model_db_query_cpp(int which_data, char *filter, void** value,
		       int* num_rows, char** err_msg);
int model_db_query_name(int which_data, char* name, void** value,
			int* num_rows, char** err_msg);
int model_db_query_location(int which_data, int filter, double from_s, 
			    double to_s, void** value,
			    int* num_rows, char** err_msg);

}

#endif