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

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