/[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 1209 - (show annotations) (download) (as text)
Tue Jan 23 04:11:50 2007 UTC (13 years, 10 months ago) by johnpye
File MIME type: text/x-csrc
File size: 6384 byte(s)
Cleaning up some redundant compiler dependencies
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/ascPanic.h>
24 #include <utilities/error.h>
25
26 #include <compiler/dimen.h>
27 #include <compiler/child.h>
28 #include <general/list.h>
29 #include <compiler/module.h>
30 #include <compiler/childinfo.h>
31 #include <compiler/parentchild.h>
32 #include <compiler/slist.h>
33 #include <compiler/type_desc.h>
34 #include <compiler/packages.h>
35 #include <compiler/symtab.h>
36 #include <compiler/instquery.h>
37 #include <compiler/instmacro.h>
38 #include <compiler/instance_types.h>
39
40 #include <compiler/extfunc.h>
41
42 #include "dr.h"
43
44 /*------------------------------------------------------------------------------
45 GLOBALS
46 */
47
48 static symchar *dr_symbols[2];
49 #define FILENAME_SYM dr_symbols[0]
50 #define FORMAT_SYM dr_symbols[1]
51
52 /*------------------------------------------------------------------------------
53 BINDINGS FOR THE DATA READER TO THE ASCEND EXTERNAL FUNCTIONS API
54 */
55
56 ExtBBoxInitFunc asc_datareader_prepare;
57 ExtBBoxFunc asc_datareader_calc;
58 ExtBBoxFinalFunc asc_datareader_close;
59
60 #ifndef ASC_EXPORT
61 # error "Where is ASC_EXPORT?"
62 #endif
63
64 /**
65 This is the function called from "IMPORT datareader"
66
67 It sets up the functions in this external function library and tells ASCEND
68 how many inputs and outputs it needs.
69 */
70 extern
71 ASC_EXPORT int datareader_register(){
72 const char *help = "The is the ASCEND Data Reader, for pulling in"
73 " time-series data such as weather readings for use in simulations.";
74
75 int result = 0;
76
77 ERROR_REPORTER_HERE(ASC_PROG_NOTE,"Initialising data reader...\n");
78
79 (void)CONSOLE_DEBUG("EVALUATION FUNCTION AT %p",asc_datareader_calc);
80
81 result += CreateUserFunctionBlackBox("datareader"
82 , asc_datareader_prepare
83 , asc_datareader_calc /* value */
84 , asc_datareader_calc /* deriv */
85 , NULL /* deriv2 */
86 , asc_datareader_close /* final */
87 , 1,5 /* inputs, outputs */
88 , help
89 , 0.0
90 ); /* returns 0 on success */
91
92 if(result){
93 ERROR_REPORTER_HERE(ASC_PROG_NOTE,"CreateUserFunction result = %d\n",result);
94 }
95 return result;
96 }
97
98 /**
99 This function prepares the data that we will use before starting the solver
100 process.
101 */
102 int asc_datareader_prepare(struct BBoxInterp *slv_interp,
103 struct Instance *data,
104 struct gl_list_t *arglist
105 ){
106 struct Instance *fninst, *fmtinst;
107 const char *fn, *fmt;
108 DataReader *d;
109
110 dr_symbols[0] = AddSymbol("filename");
111 dr_symbols[1] = AddSymbol("format");
112
113 /* get the data file name (we will look for this file in the ASCENDLIBRARY path) */
114 fninst = ChildByChar(data,FILENAME_SYM);
115 if(!fninst){
116 ERROR_REPORTER_HERE(ASC_USER_ERROR
117 ,"Couldn't locate 'filename', please check Data Reader usage."
118 );
119 return 1;
120 }
121 if(InstanceKind(fninst)!=SYMBOL_CONSTANT_INST){
122 ERROR_REPORTER_HERE(ASC_USER_ERROR,"'filename' must be a symbol_constant");
123 return 1;
124 }
125 fn = SCP(SYMC_INST(fninst)->value);
126 CONSOLE_DEBUG("FILENAME: %s",fn);
127 if(fn==NULL || strlen(fn)==0){
128 ERROR_REPORTER_HERE(ASC_USER_ERROR,"'filename' is NULL or empty");
129 return 1;
130 }
131
132 /* get the data reader format *//**
133 This is the function called from "IMPORT extfntest"
134
135 It sets up the functions in this external function library
136 */
137
138 fmtinst = ChildByChar(data,FORMAT_SYM);
139 if(!fmtinst){
140 ERROR_REPORTER_HERE(ASC_USER_ERROR
141 ,"Couldn't locate 'format', please check Data Reader usage."
142 );
143 return 1;
144 }
145 if(InstanceKind(fmtinst)!=SYMBOL_CONSTANT_INST){
146 ERROR_REPORTER_HERE(ASC_USER_ERROR,"'format' must be a symbol_constant");
147 return 1;
148 }
149 fmt = SCP(SYMC_INST(fmtinst)->value);
150 CONSOLE_DEBUG("FORMAT: %s",fmt);
151 if(fmt==NULL || strlen(fmt)==0){
152 ERROR_REPORTER_HERE(ASC_USER_ERROR,"'format' is NULL or empty");
153 return 1;
154 }
155
156 CONSOLE_DEBUG("Creating the datareader object...");
157
158 /* create the data reader and tell it the filename */
159 d = datareader_new(fn);
160
161 if(fmt!=NULL){
162 if(datareader_set_format(d,fmt)){
163 CONSOLE_DEBUG("Invalid 'format'");
164 return 1;
165 }
166 }
167
168 if(datareader_init(d)){
169 CONSOLE_DEBUG("Error initialising data reader");
170 return 1;
171 }
172
173 ERROR_REPORTER_HERE(ASC_PROG_NOTE,"Created data reader at %p...",d);
174 slv_interp->user_data = (void *)d;
175
176 return 0;
177 }
178
179 /* return 0 on success */
180 int asc_datareader_calc(struct BBoxInterp *slv_interp,
181 int ninputs, int noutputs,
182 double *inputs, double *outputs,
183 double *jacobian
184 ){
185 DataReader *d;
186 int i;
187
188 d = (DataReader *)slv_interp->user_data;
189 if(!d){
190 ERROR_REPORTER_HERE(ASC_USER_ERROR
191 ,"Datareader was not initialised successfully"
192 );
193 return 1;
194 }
195
196 if(ninputs!=datareader_num_inputs(d)){
197 ERROR_REPORTER_HERE(ASC_USER_ERROR
198 ,"Invalid number of inputs, expected %d but received %d"
199 ,datareader_num_inputs(d), ninputs
200 );
201 return 1;
202 }
203
204 if(noutputs!=datareader_num_outputs(d)){
205 ERROR_REPORTER_HERE(ASC_USER_ERROR
206 ,"Invalid number of outputs, expected <=%d but received %d"
207 ,datareader_num_outputs(d), noutputs
208 );
209 return 1;
210 }
211
212
213 for(i=0; i< ninputs; ++i){
214 CONSOLE_DEBUG("inputs[%d] = %f", i, inputs[i]);
215 }
216
217
218 switch(slv_interp->task){
219 case bb_func_eval:
220 CONSOLE_DEBUG("DATA READER EVALUATION");
221 if(datareader_func(d,inputs,outputs)){
222 CONSOLE_DEBUG("Datareader evaluation error");
223 return 1;
224 }
225 for(i=0; i< noutputs; ++i){
226 CONSOLE_DEBUG("outputs[%d] = %f", i, outputs[i]);
227 }
228 return 0; /* success */
229 case bb_deriv_eval:
230 CONSOLE_DEBUG("DATA READER DERIVATIVE");
231 if(datareader_deriv(d,inputs,outputs)){
232 CONSOLE_DEBUG("Datareader derivative evaluation error");
233 return 1;
234 }
235 return 0; /* success */
236 default:
237 CONSOLE_DEBUG("UNHANDLED REQUEST");
238 return 1;
239 }
240 }
241
242 void asc_datareader_close(struct BBoxInterp *slv_interp){
243 CONSOLE_DEBUG("NOT IMPLEMENTED");
244 }

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