/[ascend]/trunk/models/johnpye/datareader/datareader.c
ViewVC logotype

Contents of /trunk/models/johnpye/datareader/datareader.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 811 - (show annotations) (download) (as text)
Thu Aug 10 17:08:16 2006 UTC (14 years, 2 months ago) by johnpye
File MIME type: text/x-csrc
File size: 5408 byte(s)
Added vim 'syntax=python' comments to many SConscript files.
Lots more work on the datareader extfn.
The testtmy.a4c file requires the file obtained here ('gzip -d' before use)
http://rredc.nrel.gov/solar/old_data/nsrdb/tmy2/unix/23161.tm2.Z
1 /* ASCEND modelling environment
2 Copyright (C) 2006 Carnegie Mellon University
3
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation; either version 2, or (at your option)
7 any later version.
8
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
13
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software
16 Foundation, Inc., 59 Temple Place - Suite 330,
17 Boston, MA 02111-1307, USA.
18 */
19
20 #include <stdio.h>
21
22 #include <utilities/ascConfig.h>
23 #include <utilities/error.h>
24
25 #include <compiler/fractions.h>
26 #include <compiler/compiler.h>
27 #include <compiler/dimen.h>
28 #include <compiler/child.h>
29 #include <general/list.h>
30 #include <compiler/module.h>
31 #include <compiler/childinfo.h>
32 #include <compiler/parentchild.h>
33 #include <compiler/slist.h>
34 #include <compiler/type_desc.h>
35 #include <compiler/packages.h>
36 #include <compiler/symtab.h>
37 #include <compiler/instquery.h>
38 #include <compiler/instmacro.h>
39 #include <compiler/instance_types.h>
40
41 #include "dr.h"
42
43 /*------------------------------------------------------------------------------
44 GLOBALS
45 */
46
47 static symchar *dr_symbols[1];
48 #define FILENAME_SYM dr_symbols[0]
49
50 /*------------------------------------------------------------------------------
51 BINDINGS FOR THE DATA READER TO THE ASCEND EXTERNAL FUNCTIONS API
52 */
53
54 int asc_datareader_prepare(struct Slv_Interp *slv_interp, struct Instance *data, struct gl_list_t *arglist);
55 int asc_datareader_calc(struct Slv_Interp *slv_interp, int ninputs, int noutputs, double *inputs, double *outputs, double *jacobian);
56 int asc_datareader_close(struct Slv_Interp *slv_interp, struct Instance *data, struct gl_list_t *arglist);
57
58 /**
59 This is the function called from "IMPORT extfntest"
60
61 It sets up the functions in this external function library
62 */
63
64 #ifndef ASC_EXPORT
65 # error "Where is ASC_EXPORT?"
66 #endif
67
68 extern
69 ASC_EXPORT(int) datareader_register(){
70 const char *help = "The is the ASCEND Data Reader, for pulling in"
71 " time-series data such as weather readings for use in simulations.";
72
73 int result = 0;
74
75 ERROR_REPORTER_HERE(ASC_PROG_NOTE,"Initialising data reader...\n");
76
77 (void)CONSOLE_DEBUG("EVALUATION FUNCTION AT %p",asc_datareader_calc);
78
79 result += CreateUserFunctionBlackBox("add_one"
80 , asc_datareader_prepare
81 , asc_datareader_calc /* value */
82 , asc_datareader_calc /* deriv */
83 , NULL /* deriv2 */
84 , asc_datareader_close /* final */
85 , 1,1 /* inputs, outputs */
86 , help
87 ); /* returns 0 on success */
88
89 if(result){
90 ERROR_REPORTER_HERE(ASC_PROG_NOTE,"CreateUserFunction result = %d\n",result);
91 }
92 return result;
93 }
94
95 int asc_datareader_prepare(struct Slv_Interp *slv_interp,
96 struct Instance *data,
97 struct gl_list_t *arglist
98 ){
99 struct Instance *fninst;
100 const char *fn;
101 DataReader *d;
102
103 dr_symbols[0] = AddSymbol("filename");
104
105 /* get the data file name (we will look for this file in the ASCENDLIBRARY path) */
106 fninst = ChildByChar(data,FILENAME_SYM);
107 if(!fninst){
108 ERROR_REPORTER_HERE(ASC_USER_ERROR
109 ,"Couldn't locate 'filename', please check Data Reader usage."
110 );
111 return 1;
112 }
113 if(InstanceKind(fninst)!=SYMBOL_CONSTANT_INST){
114 ERROR_REPORTER_HERE(ASC_USER_ERROR,"'filename' must be a symbol constant");
115 return 1;
116 }
117 fn = SCP(SYMC_INST(fninst)->value);
118 CONSOLE_DEBUG("FILENAME: %s",fn);
119 if(fn==NULL || strlen(fn)==0){
120 ERROR_REPORTER_HERE(ASC_USER_ERROR,"'filename' is NULL or empty");
121 return 1;
122 }
123
124 /* create the data reader and tell it the filename */
125 d = datareader_new(fn);
126 if(datareader_init(d)){
127 CONSOLE_DEBUG("Error initialising data reader");
128 return 1;
129 }
130
131 ERROR_REPORTER_HERE(ASC_PROG_NOTE,"Created data reader at %p...",d);
132 slv_interp->user_data = (void *)d;
133
134 return 0;
135 }
136
137 /* return 0 on success */
138 int asc_datareader_calc(struct Slv_Interp *slv_interp,
139 int ninputs, int noutputs,
140 double *inputs, double *outputs,
141 double *jacobian
142 ){
143 DataReader *d;
144 int i;
145
146 d = (DataReader *)slv_interp->user_data;
147
148 if(ninputs!=datareader_num_inputs(d)){
149 ERROR_REPORTER_HERE(ASC_USER_ERROR
150 ,"Invalid number of inputs, expected %d but received %d"
151 ,datareader_num_inputs(d), ninputs
152 );
153 return 1;
154 }
155
156 if(noutputs > datareader_num_outputs(d)){
157 ERROR_REPORTER_HERE(ASC_USER_ERROR
158 ,"Invalid number of outputs, expected <=%d but received %d"
159 ,datareader_num_outputs(d), noutputs
160 );
161 return 1;
162 }
163
164 for(i=0; i< ninputs; ++i){
165 CONSOLE_DEBUG("inputs[%d] = %f", i, inputs[i]);
166 }
167
168 switch(slv_interp->task){
169 case bb_func_eval:
170 CONSOLE_DEBUG("DATA READER EVALUATION");
171 if(datareader_func(d,inputs,outputs)){
172 CONSOLE_DEBUG("Datareader evaluation error");
173 return 1;
174 }
175 return 0; /* success */
176 case bb_deriv_eval:
177 CONSOLE_DEBUG("DATA READER DERIVATIVE");
178 if(datareader_deriv(d,inputs,outputs)){
179 CONSOLE_DEBUG("Datareader derivative evaluation error");
180 return 1;
181 }
182 return 0; /* success */
183 default:
184 CONSOLE_DEBUG("UNHANDLED REQUEST");
185 return 1;
186 }
187 }
188
189 int asc_datareader_close(struct Slv_Interp *slv_interp,
190 struct Instance *data,
191 struct gl_list_t *arglist
192 ){
193 CONSOLE_DEBUG("NOT IMPLEMENTED");
194 return 1;
195 }

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