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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1766 - (hide annotations) (download) (as text)
Mon Apr 7 09:06:56 2008 UTC (12 years, 6 months ago) by jpye
File MIME type: text/x-csrc
File size: 6363 byte(s)
Fixed heading numbers in book.pdf, some tinkering to expose bug in IDA when there are derivs with out undiff vars.
1 johnpye 801 /* 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 johnpye 819 #include <utilities/ascPanic.h>
24 johnpye 801 #include <utilities/error.h>
25    
26 johnpye 1211
27 johnpye 801 #include <compiler/child.h>
28     #include <general/list.h>
29     #include <compiler/module.h>
30     #include <compiler/childinfo.h>
31 johnpye 811 #include <compiler/parentchild.h>
32 johnpye 801 #include <compiler/slist.h>
33     #include <compiler/type_desc.h>
34     #include <compiler/packages.h>
35 johnpye 811 #include <compiler/symtab.h>
36     #include <compiler/instquery.h>
37     #include <compiler/instmacro.h>
38     #include <compiler/instance_types.h>
39 johnpye 801
40 johnpye 908 #include <compiler/extfunc.h>
41    
42 johnpye 811 #include "dr.h"
43 johnpye 801
44     /*------------------------------------------------------------------------------
45 johnpye 811 GLOBALS
46     */
47    
48 johnpye 812 static symchar *dr_symbols[2];
49 johnpye 811 #define FILENAME_SYM dr_symbols[0]
50 johnpye 812 #define FORMAT_SYM dr_symbols[1]
51 johnpye 811
52     /*------------------------------------------------------------------------------
53 johnpye 801 BINDINGS FOR THE DATA READER TO THE ASCEND EXTERNAL FUNCTIONS API
54     */
55    
56 johnpye 908 ExtBBoxInitFunc asc_datareader_prepare;
57     ExtBBoxFunc asc_datareader_calc;
58     ExtBBoxFinalFunc asc_datareader_close;
59 johnpye 801
60     #ifndef ASC_EXPORT
61     # error "Where is ASC_EXPORT?"
62     #endif
63    
64 johnpye 812 /**
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 johnpye 801 extern
71 johnpye 1063 ASC_EXPORT int datareader_register(){
72 johnpye 811 const char *help = "The is the ASCEND Data Reader, for pulling in"
73 johnpye 801 " 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 jpye 1766 /* (void)CONSOLE_DEBUG("EVALUATION FUNCTION AT %p",asc_datareader_calc); */
80 johnpye 801
81 johnpye 812 result += CreateUserFunctionBlackBox("datareader"
82 johnpye 801 , asc_datareader_prepare
83     , asc_datareader_calc /* value */
84     , asc_datareader_calc /* deriv */
85     , NULL /* deriv2 */
86     , asc_datareader_close /* final */
87 johnpye 819 , 1,5 /* inputs, outputs */
88 johnpye 811 , help
89 johnpye 908 , 0.0
90 johnpye 801 ); /* 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 johnpye 812 /**
99     This function prepares the data that we will use before starting the solver
100     process.
101     */
102 johnpye 908 int asc_datareader_prepare(struct BBoxInterp *slv_interp,
103 johnpye 801 struct Instance *data,
104     struct gl_list_t *arglist
105     ){
106 johnpye 812 struct Instance *fninst, *fmtinst;
107     const char *fn, *fmt;
108 johnpye 801 DataReader *d;
109    
110 johnpye 811 dr_symbols[0] = AddSymbol("filename");
111 johnpye 812 dr_symbols[1] = AddSymbol("format");
112 johnpye 811
113 johnpye 801 /* get the data file name (we will look for this file in the ASCENDLIBRARY path) */
114 johnpye 811 fninst = ChildByChar(data,FILENAME_SYM);
115 johnpye 801 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 johnpye 812 ERROR_REPORTER_HERE(ASC_USER_ERROR,"'filename' must be a symbol_constant");
123 johnpye 801 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 johnpye 812 /* 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 johnpye 801 /* create the data reader and tell it the filename */
159     d = datareader_new(fn);
160 johnpye 812
161     if(fmt!=NULL){
162     if(datareader_set_format(d,fmt)){
163     CONSOLE_DEBUG("Invalid 'format'");
164     return 1;
165 johnpye 908 }
166 johnpye 812 }
167 johnpye 908
168 johnpye 801 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 johnpye 908 int asc_datareader_calc(struct BBoxInterp *slv_interp,
181 johnpye 801 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 johnpye 819 if(!d){
190     ERROR_REPORTER_HERE(ASC_USER_ERROR
191     ,"Datareader was not initialised successfully"
192     );
193     return 1;
194     }
195 johnpye 801
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 johnpye 819 if(noutputs!=datareader_num_outputs(d)){
205 johnpye 801 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 johnpye 908
213 johnpye 801 for(i=0; i< ninputs; ++i){
214     CONSOLE_DEBUG("inputs[%d] = %f", i, inputs[i]);
215     }
216    
217 johnpye 908
218 johnpye 811 switch(slv_interp->task){
219 johnpye 801 case bb_func_eval:
220 johnpye 819 CONSOLE_DEBUG("DATA READER EVALUATION");
221 johnpye 801 if(datareader_func(d,inputs,outputs)){
222     CONSOLE_DEBUG("Datareader evaluation error");
223     return 1;
224     }
225 johnpye 819 for(i=0; i< noutputs; ++i){
226     CONSOLE_DEBUG("outputs[%d] = %f", i, outputs[i]);
227     }
228 johnpye 801 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 johnpye 811 default:
237     CONSOLE_DEBUG("UNHANDLED REQUEST");
238     return 1;
239 johnpye 801 }
240     }
241 johnpye 811
242 johnpye 908 void asc_datareader_close(struct BBoxInterp *slv_interp){
243 johnpye 811 CONSOLE_DEBUG("NOT IMPLEMENTED");
244     }

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