/[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 960 - (hide annotations) (download) (as text)
Sun Dec 10 03:14:24 2006 UTC (13 years, 11 months ago) by johnpye
File MIME type: text/x-csrc
File size: 6448 byte(s)
Able to compile now on Solaris.
Fixed a problem with datareader.c returning non-void.
Remains a problem with std::logic_error not being linked to _ascpy
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     #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 johnpye 811 #include <compiler/parentchild.h>
34 johnpye 801 #include <compiler/slist.h>
35     #include <compiler/type_desc.h>
36     #include <compiler/packages.h>
37 johnpye 811 #include <compiler/symtab.h>
38     #include <compiler/instquery.h>
39     #include <compiler/instmacro.h>
40     #include <compiler/instance_types.h>
41 johnpye 801
42 johnpye 908 #include <compiler/extfunc.h>
43    
44 johnpye 811 #include "dr.h"
45 johnpye 801
46     /*------------------------------------------------------------------------------
47 johnpye 811 GLOBALS
48     */
49    
50 johnpye 812 static symchar *dr_symbols[2];
51 johnpye 811 #define FILENAME_SYM dr_symbols[0]
52 johnpye 812 #define FORMAT_SYM dr_symbols[1]
53 johnpye 811
54     /*------------------------------------------------------------------------------
55 johnpye 801 BINDINGS FOR THE DATA READER TO THE ASCEND EXTERNAL FUNCTIONS API
56     */
57    
58 johnpye 908 ExtBBoxInitFunc asc_datareader_prepare;
59     ExtBBoxFunc asc_datareader_calc;
60     ExtBBoxFinalFunc asc_datareader_close;
61 johnpye 801
62     #ifndef ASC_EXPORT
63     # error "Where is ASC_EXPORT?"
64     #endif
65    
66 johnpye 812 /**
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 johnpye 801 extern
73     ASC_EXPORT(int) datareader_register(){
74 johnpye 811 const char *help = "The is the ASCEND Data Reader, for pulling in"
75 johnpye 801 " 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 johnpye 811 (void)CONSOLE_DEBUG("EVALUATION FUNCTION AT %p",asc_datareader_calc);
82 johnpye 801
83 johnpye 812 result += CreateUserFunctionBlackBox("datareader"
84 johnpye 801 , asc_datareader_prepare
85     , asc_datareader_calc /* value */
86     , asc_datareader_calc /* deriv */
87     , NULL /* deriv2 */
88     , asc_datareader_close /* final */
89 johnpye 819 , 1,5 /* inputs, outputs */
90 johnpye 811 , help
91 johnpye 908 , 0.0
92 johnpye 801 ); /* 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 johnpye 812 /**
101     This function prepares the data that we will use before starting the solver
102     process.
103     */
104 johnpye 908 int asc_datareader_prepare(struct BBoxInterp *slv_interp,
105 johnpye 801 struct Instance *data,
106     struct gl_list_t *arglist
107     ){
108 johnpye 812 struct Instance *fninst, *fmtinst;
109     const char *fn, *fmt;
110 johnpye 801 DataReader *d;
111    
112 johnpye 811 dr_symbols[0] = AddSymbol("filename");
113 johnpye 812 dr_symbols[1] = AddSymbol("format");
114 johnpye 811
115 johnpye 801 /* get the data file name (we will look for this file in the ASCENDLIBRARY path) */
116 johnpye 811 fninst = ChildByChar(data,FILENAME_SYM);
117 johnpye 801 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 johnpye 812 ERROR_REPORTER_HERE(ASC_USER_ERROR,"'filename' must be a symbol_constant");
125 johnpye 801 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 johnpye 812 /* 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 johnpye 801 /* create the data reader and tell it the filename */
161     d = datareader_new(fn);
162 johnpye 812
163     if(fmt!=NULL){
164     if(datareader_set_format(d,fmt)){
165     CONSOLE_DEBUG("Invalid 'format'");
166     return 1;
167 johnpye 908 }
168 johnpye 812 }
169 johnpye 908
170 johnpye 801 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 johnpye 908 int asc_datareader_calc(struct BBoxInterp *slv_interp,
183 johnpye 801 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 johnpye 819 if(!d){
192     ERROR_REPORTER_HERE(ASC_USER_ERROR
193     ,"Datareader was not initialised successfully"
194     );
195     return 1;
196     }
197 johnpye 801
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 johnpye 819 if(noutputs!=datareader_num_outputs(d)){
207 johnpye 801 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 johnpye 908
215 johnpye 801 for(i=0; i< ninputs; ++i){
216     CONSOLE_DEBUG("inputs[%d] = %f", i, inputs[i]);
217     }
218    
219 johnpye 908
220 johnpye 811 switch(slv_interp->task){
221 johnpye 801 case bb_func_eval:
222 johnpye 819 CONSOLE_DEBUG("DATA READER EVALUATION");
223 johnpye 801 if(datareader_func(d,inputs,outputs)){
224     CONSOLE_DEBUG("Datareader evaluation error");
225     return 1;
226     }
227 johnpye 819 for(i=0; i< noutputs; ++i){
228     CONSOLE_DEBUG("outputs[%d] = %f", i, outputs[i]);
229     }
230 johnpye 801 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 johnpye 811 default:
239     CONSOLE_DEBUG("UNHANDLED REQUEST");
240     return 1;
241 johnpye 801 }
242     }
243 johnpye 811
244 johnpye 908 void asc_datareader_close(struct BBoxInterp *slv_interp){
245 johnpye 811 CONSOLE_DEBUG("NOT IMPLEMENTED");
246     }

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