/[ascend]/trunk/models/johnpye/nrel/asc_sunpos.c
ViewVC logotype

Diff of /trunk/models/johnpye/nrel/asc_sunpos.c

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 2590 by jpye, Tue Apr 10 16:11:39 2012 UTC revision 2591 by jpye, Wed Apr 11 21:23:18 2012 UTC
# Line 46  Line 46 
46  # define PI 3.14159265358979  # define PI 3.14159265358979
47  #endif  #endif
48    
49  ExtBBoxInitFunc sunpos_nrel_prepare;  static ExtBBoxInitFunc sunpos_nrel_prepare;
50  ExtBBoxFunc sunpos_nrel_calc;  static ExtBBoxFunc sunpos_nrel_calc;
51    static ExtBBoxInitFunc julian_day_nrel_prepare;
52    static ExtBBoxFunc julian_day_nrel_calc;
53    
54  static const char *sunpos_nrel_help = "Calculate sun position (local zenith, azimuth "  static const char *sunpos_nrel_help = "Calculate sun position (local zenith, azimuth "
55      "angles) given time, pressure and temperature, using NREL algorithm. DATA "      "angles) given time, pressure and temperature, using NREL algorithm. DATA "
# Line 57  static const char *sunpos_nrel_help = "C Line 59  static const char *sunpos_nrel_help = "C
59      "Julian Date into seconds automatically. The JD should be in the range"      "Julian Date into seconds automatically. The JD should be in the range"
60      "-2000 BC to 6000 AD (12:00pm 1 Jan 2000 GMT is 2451545.0 JD)";      "-2000 BC to 6000 AD (12:00pm 1 Jan 2000 GMT is 2451545.0 JD)";
61    
62    static const char *julian_day_nrel_help = "Calculate the Julian Day from "
63        "year, month, day, hour, minute, second and timezone inputs. "
64        "Intended for once-off use in ASCEND models to calculate the time offset "
65        "eg for the start of a weather file. Acceptable dates are in the range "
66        "of -2000 BC to AD 6000. All of the inputs should be as 'factor' type "
67        "variables (to avoid needless time unit conversions), except for the "
68        "timezone, which should be in time units eg '8{h}'.";
69    
70  /*------------------------------------------------------------------------------  /*------------------------------------------------------------------------------
71    REGISTRATION FUNCTION    REGISTRATION FUNCTION
72  */  */
73    
74  /**  /**
75      This is the function called from 'IMPORT "johnpye/grena/sunpos";'      This is the function called from 'IMPORT "johnpye/nrel/sunpos_nrels";'
76    
77      It sets up the functions contained in this external library      It sets up the functions contained in this external library
78  */  */
# Line 70  extern Line 80  extern
80  ASC_EXPORT int sunpos_nrel_register(){  ASC_EXPORT int sunpos_nrel_register(){
81      int result = 0;      int result = 0;
82    
83      ERROR_REPORTER_HERE(ASC_USER_WARNING,"SUNPOS is still EXPERIMENTAL. Use with caution.\n");      ERROR_REPORTER_HERE(ASC_USER_WARNING,"SUNPOS_NREL is still EXPERIMENTAL. Use with caution.\n");
84    
85  #define CALCFN(NAME,INPUTS,OUTPUTS) \  #define CALCFN(NAME,INPUTS,OUTPUTS) \
86      result += CreateUserFunctionBlackBox(#NAME \      result += CreateUserFunctionBlackBox(#NAME \
87          , sunpos_nrel_prepare \          , NAME##_prepare \
88          , NAME##_calc /* value */ \          , NAME##_calc /* value */ \
89          , (ExtBBoxFunc*)NULL /* derivatives not provided yet*/ \          , (ExtBBoxFunc*)NULL /* derivatives not provided yet*/ \
90          , (ExtBBoxFunc*)NULL /* hessian not provided yet */ \          , (ExtBBoxFunc*)NULL /* hessian not provided yet */ \
# Line 85  ASC_EXPORT int sunpos_nrel_register(){ Line 95  ASC_EXPORT int sunpos_nrel_register(){
95      ) /* returns 0 on success */      ) /* returns 0 on success */
96    
97      CALCFN(sunpos_nrel,3,2);      CALCFN(sunpos_nrel,3,2);
98        CALCFN(julian_day_nrel,7,1);
99    
100  #undef CALCFN  #undef CALCFN
101    
# Line 94  ASC_EXPORT int sunpos_nrel_register(){ Line 105  ASC_EXPORT int sunpos_nrel_register(){
105      return result;      return result;
106  }  }
107    
   
 /**  
     This function is called when the black-box relation is being instantiated.  
   
    'sunpos_prepare' just gets the data member and checks that it's  
     valid, and stores it in the blackbox data field.  
 */  
 int sunpos_nrel_prepare(struct BBoxInterp *bbox,  
        struct Instance *data,  
        struct gl_list_t *arglist  
 ){  
     struct Instance *inst;  
     double latitude, longitude, elevation;  
   
     /* fetch DATA items for geographical location, timezone etc */  
108  #define GET_CHILD_VAL(NAME) \  #define GET_CHILD_VAL(NAME) \
109      inst = ChildByChar(data,AddSymbol(#NAME)); \      inst = ChildByChar(data,AddSymbol(#NAME)); \
110      if(!inst){ \      if(!inst){ \
# Line 123  int sunpos_nrel_prepare(struct BBoxInter Line 119  int sunpos_nrel_prepare(struct BBoxInter
119      }\      }\
120      NAME = RC_INST(inst)->value;      NAME = RC_INST(inst)->value;
121    
122    /**
123        This function is called when the black-box relation is being instantiated.
124    
125        This just gets the data member and checks that it's valid, and stores
126        it in the blackbox data field.
127    */
128    static int sunpos_nrel_prepare(struct BBoxInterp *bbox,
129           struct Instance *data,
130           struct gl_list_t *arglist
131    ){
132        struct Instance *inst;
133        double latitude, longitude, elevation;
134    
135      /* get the latitude */      /* get the latitude */
136      GET_CHILD_VAL(latitude);      GET_CHILD_VAL(latitude);
137      CONSOLE_DEBUG("Latitude: %0.3f",latitude);      CONSOLE_DEBUG("Latitude: %0.3f",latitude);
# Line 147  int sunpos_nrel_prepare(struct BBoxInter Line 156  int sunpos_nrel_prepare(struct BBoxInter
156          return 1;          return 1;
157      }      }
158    
 #undef GET_CHILD_VAL  
   
159      spa_data *S = ASC_NEW(spa_data);      spa_data *S = ASC_NEW(spa_data);
160      S->latitude = latitude * 180/PI;      S->latitude = latitude * 180/PI;
161      S->longitude = longitude * 180/PI;      S->longitude = longitude * 180/PI;
# Line 177  int sunpos_nrel_prepare(struct BBoxInter Line 184  int sunpos_nrel_prepare(struct BBoxInter
184      Evaluation function for 'sunpos'      Evaluation function for 'sunpos'
185      @return 0 on success      @return 0 on success
186  */  */
187  int sunpos_nrel_calc(struct BBoxInterp *bbox,  static int sunpos_nrel_calc(struct BBoxInterp *bbox,
188          int ninputs, int noutputs,          int ninputs, int noutputs,
189          double *inputs, double *outputs,          double *inputs, double *outputs,
190          double *jacobian          double *jacobian
# Line 206  int sunpos_nrel_calc(struct BBoxInterp * Line 213  int sunpos_nrel_calc(struct BBoxInterp *
213      return res;      return res;
214  }  }
215    
216    /*---------- SUNPOS_JULIAN_DAY ------------*/
217    
218    static int julian_day_nrel_prepare(struct BBoxInterp *bbox,
219           struct Instance *data,
220           struct gl_list_t *arglist
221    ){
222        bbox->user_data = NULL;
223        return 0;
224    }
225    
226    static int julian_day_nrel_calc(struct BBoxInterp *bbox,
227            int ninputs, int noutputs,
228            double *inputs, double *outputs,
229            double *jacobian
230    ){
231        CALCPREPARE(7,1);
232        (void)sunpos1;
233    
234        int y,mon,d,h,m,s;
235        double tz;
236    
237        y = inputs[0]; /* convert from seconds to years */
238        mon = inputs[1]; /* convert from seconds to months */
239        d = inputs[2]; /* convert from seconds to days */
240        h = inputs[3]; /* seconds to hours */
241        m = inputs[4]; /* seconds to minutes */
242        s = inputs[5];
243        tz = inputs[6] / 3600.; /* seconds to hours */
244    
245        double t = julian_day(y,mon,d, h,m,s, tz) * 3600 * 24;
246        
247        outputs[0] = t;
248        return 0;
249    }
250    
251    

Legend:
Removed from v.2590  
changed lines
  Added in v.2591

john.pye@anu.edu.au
ViewVC Help
Powered by ViewVC 1.1.22