cloudy  trunk
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
grid_xspec.cpp
Go to the documentation of this file.
1 /* This file is part of Cloudy and is copyright (C)1978-2022 by Gary J. Ferland and
2  * others. For conditions of distribution and use see copyright notice in license.txt */
3 /*gridXspec handles all grid calculations, called by griddo */
4 /*GridRetrieveXSPECData - obtain the correct spectrum for each grid point */
5 #include "cddefines.h"
6 #include "cddrive.h"
7 #include "continuum.h"
8 #include "rfield.h"
9 #include "grid.h"
10 #include "ipoint.h"
11 #include "called.h"
12 #include "prt.h"
13 
14 /*gridXspec handles all grid calculations, called by grid_do */
15 void gridXspec(realnum xc[], long int nInterpVars)
16 {
17  long int i;
18 
19  DEBUG_ENTRY( "gridXspec()" );
20 
21  if( nInterpVars > LIMPAR )
22  {
23  fprintf( ioQQQ, "grid_do: too many parameters are varied, increase LIMPAR\n" );
25  }
26 
27  optimize.nOptimiz = 0;
28  grid.nintparm = nInterpVars;
29 
30  /* if this is changed there must be some change made to actually
31  * stuff the additional parameter information. */
32  grid.naddparm = 0;
33 
35 
36  grid.totNumModels = 1;
37  /* >>chng 06 aug 21, allow the number of parameter values to be different for different parameters. */
38  for( i=0; i<nInterpVars; i++ )
39  {
40  /* >>chng 06 sep 4, use grid variable instead of passing to routine. */
42 
43  if( grid.paramValuesFromList[i].size() != 0 && grid.lgSaveXspec )
44  {
45  fprintf( ioQQQ, " PROBLEM The 'save XSPEC' and 'grid list' options do not work together.\n" );
46  fprintf( ioQQQ, " If any 'save XSPEC' command is given, all grid commands must follow this syntax:\n" );
47  fprintf( ioQQQ, " grid <p1> <p2> <p3> \n" );
48  fprintf( ioQQQ, " where p1 and p2 are the limits and p3 is a regular increment.\n Sorry.\n" );
50  }
51  }
52 
53  // option to cycle through grid multiple times, default is 1
55  ASSERT( grid.totNumModels > 1 );
56 
57  grid.paramNames = (char**)MALLOC(sizeof(char*)*(unsigned)(nInterpVars+grid.naddparm) );
58  grid.paramMethods = (long*)MALLOC(sizeof(long)*(unsigned)(nInterpVars+grid.naddparm) );
59  grid.paramRange = (realnum**)MALLOC(sizeof(realnum*)*(unsigned)(nInterpVars+grid.naddparm) );
60  grid.paramData = (realnum**)MALLOC(sizeof(realnum*)*(unsigned)(nInterpVars+grid.naddparm) );
61  grid.interpParameters = (realnum**)MALLOC(sizeof(realnum*)*(unsigned)(grid.totNumModels) );
62 
63  for( i=0; i<nInterpVars+grid.naddparm; i++ )
64  {
65  grid.paramNames[i] = (char*)MALLOC(sizeof(char)*(unsigned)(12) );
66  grid.paramRange[i] = (realnum*)MALLOC(sizeof(realnum)*(unsigned)(6) );
67  grid.paramData[i] = (realnum*)MALLOC(sizeof(realnum)*(unsigned)(grid.numParamValues[i]) );
68 
69  sprintf( grid.paramNames[i], "%s%ld", "PARAM", i+1 );
70  /* Method is 0 for linear, 1 for logarithmic */
71  grid.paramMethods[i] = 0;
72  /* Initial */
73  grid.paramRange[i][0] = xc[i]+grid.paramIncrements[i]*(grid.numParamValues[i]-1.f)/2.f;
74  /* Delta */
75  grid.paramRange[i][1] = grid.paramIncrements[i]/10.f;
76  /* Minimum */
77  grid.paramRange[i][2] = xc[i];
78  /* Bottom */
79  grid.paramRange[i][3] = xc[i]+grid.paramIncrements[i]/10.f;
80  /* Top */
81  grid.paramRange[i][4] = xc[i]+grid.paramIncrements[i]*(grid.numParamValues[i]-1.f)-grid.paramIncrements[i]/10.f;
82  /* Maximum */
83  grid.paramRange[i][5] = xc[i]+grid.paramIncrements[i]*(grid.numParamValues[i]-1.f);
84 
85  for( long j=0; j<grid.numParamValues[i]; j++ )
86  {
87  grid.paramData[i][j] = xc[i]+grid.paramIncrements[i]*j;
88  }
89  }
90 
91  for( i=0; i<grid.totNumModels; i++ )
92  {
93  grid.interpParameters[i] = (realnum*)MALLOC(sizeof(realnum)*(unsigned)(nInterpVars) );
94  }
95 
96  for( i=0; i< grid.totNumModels; i++ )
97  {
98  long j;
99  realnum variableVector[LIMPAR];
100 
101  for( j=0; j<nInterpVars; j++ )
102  {
103  int index;
104  long volumeOtherDimensions = 1;
105 
106  /* by "volume", we simply mean the product of the parameter dimensions
107  * AFTER the present index, i.e.:
108  * first "volume" is product of grid.numParamValues[1]*grid.numParamValues[2]*....grid.numParamValues[n]
109  * second "volume" is product of grid.numParamValues[2]*grid.numParamValues[3]*....grid.numParamValues[n]
110  * last "volume" is unity. */
111  for( long k=j+1; k<nInterpVars; k++ )
112  {
113  volumeOtherDimensions *= grid.numParamValues[k];
114  }
115 
116  /* For each successive parameter, the "volume" is less than the previous one.
117  * So the left-hand side of this modulus operation increases for each parameter,
118  * which causes the index of each parameter to be incremented more often than the
119  * index of the previous parameter. Thus, the last dimension is incremented
120  * every time, then the second to last dimension is incremented with each repeat
121  * of the last dimension. This repeats until, finally, the first dimension is
122  * incremented. For example, the indices of the parameter vectors for a 2x2x3
123  * box would be ordered as such:
124  * [0][0][0]
125  * [0][0][1]
126  * [0][0][2]
127  * [0][1][0]
128  * [0][1][1]
129  * [0][1][2]
130  * [1][0][0]
131  * [1][0][1]
132  * [1][0][2]
133  * [1][1][0]
134  * [1][1][1]
135  * [1][1][2]
136  */
137  index = (int)( (i/volumeOtherDimensions)%(grid.numParamValues[j]) );
138 
139  /* this prevents parameter incrementation for debugging purposes. */
140  if( grid.lgStrictRepeat )
141  variableVector[j] = xc[j];
142  else if( grid.paramValuesFromList[j].size() != 0U )
143  variableVector[j] = grid.paramValuesFromList[j][index];
144  else
145  variableVector[j] = xc[j] + grid.paramIncrements[j]*index;
146 
147  grid.interpParameters[i][j] = variableVector[j];
148 
150  variableVector[j] = log10(variableVector[j]);
151  }
152 
153  for( j=nInterpVars; j<LIMPAR; j++ )
154  {
155  variableVector[j] = xc[j];
156  }
157 
158  if( i == grid.totNumModels - 1 )
159  {
160  fixit("is this needed ??");
161  called.lgTalk = cpu.i().lgMPI_talk();
163  prt.lgFaintOn = true;
164  grid.lgGridDone = true;
165  }
166 
167  (void)optimize_func(variableVector,i);
168  }
169  return;
170 }
171 
172 /*GridRetrieveXSPECData - obtain the correct spectrum for each grid point */
173 void GridRetrieveXSPECData(int option)
174 {
175  DEBUG_ENTRY( "GridRetrieveXSPECData()" );
176 
177  if( !grid.lgGrid )
178  {
179  fprintf( ioQQQ," Cannot save xspec files unless doing a grid.\n" );
181  }
182 
183  /* malloc some arrays if first call and save continuum energies. */
184  if( grid.Spectra.empty() )
185  {
187  for( long i1=0; i1 < NUM_OUTPUT_TYPES; i1++ )
188  {
189  if( grid.lgOutputTypeOn[i1] )
190  {
192  for( long i2=0; i2 < grid.totNumModels; i2++ )
193  {
195  }
196  }
197  }
198  grid.Spectra.alloc();
199  }
200 
202  ASSERT( grid.lgOutputTypeOn[option] );
203 
204  /* grab spectrum for xspec and store it in grid.Spectra */
205  cdSPEC2( option, &grid.Spectra[option][optimize.nOptimiz][0]);
206 }
void GridRetrieveXSPECData(int option)
Definition: grid_xspec.cpp:173
realnum ** paramData
Definition: grid.h:30
bool lgGrid
Definition: grid.h:41
bool lgGridDone
Definition: grid.h:42
t_cpu_i & i()
Definition: cpu.h:419
chi2_type optimize_func(const realnum param[], int grid_index=-1)
long int nOptimiz
Definition: optimize.h:250
void cdSPEC2(int Option, realnum ReturnedSpectrum[])
Definition: save_do.cpp:148
long * paramMethods
Definition: grid.h:28
bool lgStrictRepeat
Definition: grid.h:45
FILE * ioQQQ
Definition: cddefines.cpp:7
bool lgTalk
Definition: called.h:12
realnum ** interpParameters
Definition: grid.h:31
long nintparm
Definition: grid.h:58
#define MALLOC(exp)
Definition: cddefines.h:554
long totNumModels
Definition: grid.h:61
long numParamValues[LIMPAR]
Definition: grid.h:60
bool lgSaveXspec
Definition: grid.h:38
t_rfield rfield
Definition: rfield.cpp:9
float realnum
Definition: cddefines.h:124
const int NUM_OUTPUT_TYPES
Definition: grid.h:22
#define EXIT_FAILURE
Definition: cddefines.h:168
bool lgFaintOn
Definition: prt.h:245
#define cdEXIT(FAIL)
Definition: cddefines.h:482
bool lgMPI_talk() const
Definition: cpu.h:397
const long LIMPAR
Definition: optimize.h:61
vector< realnum > paramValuesFromList[LIMPAR]
Definition: grid.h:35
multi_arr< realnum, 3 > Spectra
Definition: grid.h:26
t_optimize optimize
Definition: optimize.cpp:6
t_grid grid
Definition: grid.cpp:5
t_prt prt
Definition: prt.cpp:14
bool lgOptimizeAsLinear[LIMPAR]
Definition: optimize.h:184
bool lgLinearIncrements[LIMPAR]
Definition: grid.h:36
#define ASSERT(exp)
Definition: cddefines.h:613
void reserve(size_type i1)
#define DEBUG_ENTRY(funcname)
Definition: cddefines.h:723
bool empty() const
bool lgOutputTypeOn[NUM_OUTPUT_TYPES]
Definition: grid.h:66
realnum paramIncrements[LIMPAR]
Definition: grid.h:34
int fprintf(const Output &stream, const char *format,...)
Definition: service.cpp:1121
long nCycle
Definition: grid.h:64
void gridXspec(realnum *, long)
#define fixit(a)
Definition: cddefines.h:417
static t_cpu cpu
Definition: cpu.h:427
long int nflux
Definition: rfield.h:46
realnum ** paramRange
Definition: grid.h:29
t_called called
Definition: called.cpp:4
char ** paramNames
Definition: grid.h:27
bool lgTalkIsOK
Definition: called.h:23
long naddparm
Definition: grid.h:59