cloudy  trunk
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
parse_grid.cpp
Go to the documentation of this file.
1 /* This file is part of Cloudy and is copyright (C)1978-2013 by Gary J. Ferland and
2  * others. For conditions of distribution and use see copyright notice in license.txt */
3 /*ParseGrid parse the grid command lines */
4 #include "cddefines.h"
5 #include "optimize.h"
6 #include "grid.h"
7 #include "parser.h"
8 
9 /* ParseGrid - called from ParseCommands if GRID command found */
10 void ParseGrid(
11  /* command line, which was changed to all caps in main parsing routine */
12  Parser &p)
13 {
14  DEBUG_ENTRY( "ParseGrid()" );
15 
16  /* RP fake optimizer to run a grid of calculations, also accepts
17  * keyword XSPEC */
18  strcpy( optimize.chOptRtn, "XSPE" );
19  grid.lgGrid = true;
20 
21  if( p.nMatch("REPE") )
22  {
23  /* just keep repeating, don't actually change the values in the grid.
24  * useful for debugging unintentional crosstalk */
25  grid.lgStrictRepeat = true;
26  }
27 
28  /* 06 aug 22, change to accept three parameters: lower and upper limit and number of points. */
29  /* scan off range for the previously selected variable */
30  if( optimize.nparm > 0 )
31  {
33 
37  grid.lgLinearIncrements[optimize.nparm-1] = p.nMatch("LINE") ? true : false ;
40 
41  /* the increase step should not be 0 */
42  if( grid.paramIncrements[optimize.nparm-1] == 0. )
43  {
44  fprintf( ioQQQ," The increment (third parameter) should not be zero.\n" );
45  fprintf( ioQQQ," Sorry.\n" );
47  }
48 
50  {
51  fprintf( ioQQQ," The increment (third parameter) must not be greater than the difference between the limits (first and second parameters).\n" );
52  fprintf( ioQQQ," Sorry.\n" );
54  }
55 
56  if( p.lgEOL() )
57  {
58  fprintf( ioQQQ," This command has changed since the definition given in Porter et al. 2006, PASP, 118, 920.\n" );
59  fprintf( ioQQQ," The grid command now requires three parameters: lower limit, upper limit, and increment.\n" );
60  fprintf( ioQQQ," The keywords RANGE and STEPS are no longer necessary.\n" );
61  fprintf( ioQQQ," Sorry.\n" );
63  }
64  else
65  {
67  }
68 
71 
72  /* Alert if the uplimit and lowlimit are wrong */
73  if( ratio < realnum(0.) )
74  {
75  fprintf( ioQQQ, "The increment (third parameter) has the wrong sign. It doesn't take you from the initial to the final grid value (first and second parameter, resp.).\n" );
76  fprintf( ioQQQ," Sorry.\n" );
78  }
79 
80  // this takes care of the blowup in the error due to cancellation in limits[1]-limits[0]
81  // it assumes that limits[1]-limits[0] is accurate within 3*eps*(limits[0]+limits[1])/2
82  // which should be a very conservative estimate...
83  realnum feps = realnum(1.5)*
86  long eps = max(nint(abs(feps)),3);
87 
88  // this will blow for pathologically narrow grid ranges
89  ASSERT( eps <= INT_MAX );
90 
91  // take special care if step is integer fraction of max-min (which is nearly always the case)
92  if( fp_equal( ratio, realnum(nint(ratio)), int(eps) ) )
93  grid.numParamValues[optimize.nparm-1] = nint(ratio) + 1;
94  else
95  grid.numParamValues[optimize.nparm-1] = long(ratio) + 1;
96 
97  if( grid.numParamValues[optimize.nparm-1] < 2 )
98  fprintf( ioQQQ, " NOTE must have at least two grid points\n" );
99 
101 
102  // Create some buffer area in the allowed range of parameter values to prevent
103  // accidentally going over the limit due to roundoff error. The buffer is 1/10th
104  // of a step, so should still guard against doing too many steps due to bugs.
105  realnum safety = 0.001f*grid.paramIncrements[optimize.nparm-1];
106 
108  {
109  if( grid.paramLimits[optimize.nparm-1][0]-safety<=0. )
110  {
111  fprintf(ioQQQ,"The current implementation of the grid command works with log parameter values even when you specify LINEAR.\n");
112  fprintf(ioQQQ,"A non-positive value was entered. The grid command cannot deal with this.\n");
113  cdEXIT( EXIT_FAILURE );
114  }
115  optimize.varang[optimize.nparm-1][0] = log10(grid.paramLimits[optimize.nparm-1][0]-safety);
116  optimize.varang[optimize.nparm-1][1] = log10(grid.paramLimits[optimize.nparm-1][1]+safety);
117  }
118  else
119  {
122  }
123  }
124 
125  return;
126 }
bool nMatch(const char *chKey) const
Definition: parser.h:135
double FFmtRead(void)
Definition: parser.cpp:353
long int nRangeSet
Definition: optimize.h:200
bool lgGrid
Definition: grid.h:40
#define MAX2
Definition: cddefines.h:786
realnum varang[LIMPAR][2]
Definition: optimize.h:197
bool lgStrictRepeat
Definition: grid.h:40
FILE * ioQQQ
Definition: cddefines.cpp:7
void ParseGrid(Parser &p)
Definition: parse_grid.cpp:10
Definition: parser.h:31
bool fp_equal(sys_float x, sys_float y, int n=3)
Definition: cddefines.h:816
long numParamValues[LIMPAR]
Definition: grid.h:47
long int nparm
Definition: optimize.h:200
float realnum
Definition: cddefines.h:107
#define EXIT_FAILURE
Definition: cddefines.h:144
long max(int a, long b)
Definition: cddefines.h:779
#define cdEXIT(FAIL)
Definition: cddefines.h:438
const long LIMPAR
Definition: optimize.h:61
t_optimize optimize
Definition: optimize.cpp:5
t_grid grid
Definition: grid.cpp:5
char chOptRtn[5]
Definition: optimize.h:264
bool lgLinearIncrements[LIMPAR]
Definition: grid.h:35
bool lgNegativeIncrements
Definition: grid.h:36
#define ASSERT(exp)
Definition: cddefines.h:582
#define DEBUG_ENTRY(funcname)
Definition: cddefines.h:688
bool lgEOL(void) const
Definition: parser.h:98
realnum paramIncrements[LIMPAR]
Definition: grid.h:34
long nint(double x)
Definition: cddefines.h:723
realnum paramLimits[LIMPAR][2]
Definition: grid.h:33