001: /*
002:  *  dde-model.c
003:  *  dde
004:  *
005:  *  Created by ashley on 03/12/2009.
006:  *  Copyright 2009 __MyCompanyName__. All rights reserved.
007:  *
008:  */
009: 
010: #include "dde-model.h"
011: #include "plot-win.h"
012: 
013: usercontrol *makeUsercontrol()
014: {
015:         usercontrol *out = (usercontrol *)g_malloc(sizeof(usercontrol));
016: 
017:         out->nvariables = 0;
018:         out->nparameters = 0;
019:         out->fout = NULL;
020:         out->lines = NULL;
021:         out->index = NULL;
022:         out->label = NULL;
023:         out->cname = NULL;
024:         out->cinfo = NULL;
025:         out->wname = NULL;
026:         out->range = NULL;
027:         out->c = NULL;
028: 
029:         return out;
030: }
031: 
032: void freeUsercontrol(usercontrol *out)
033: {
034: }
035: 
036: void initialiseUsercontrol(usercontrol *out)
037: {
038:         int nvariables;
039:         int nparameters;
040:         initcons(&nvariables, &nparameters);
041: 
042:         out->nvariables = nvariables;
043:         out->nparameters = nparameters;
044:         out->fout = (int *)g_malloc((size_t)nvariables*sizeof(int));
045:         out->lines = (int *)g_malloc((size_t)nvariables*sizeof(int));
046:         out->index = (wctype *)g_malloc((size_t)nvariables*sizeof(wctype));
047:         out->label = (char **)g_malloc((size_t)nvariables*sizeof(char *));
048:         out->cname = (char **)g_malloc((size_t)nparameters*sizeof(char *));
049:         out->cinfo = (char **)g_malloc((size_t)nparameters*sizeof(char *));
050:         out->wname = (char **)g_malloc((size_t)nvariables*sizeof(char *));
051:         out->range = (y0y1type *)g_malloc((size_t)nvariables*sizeof(y0y1type));
052:         out->c = (double *)g_malloc((size_t)nparameters*sizeof(double));
053: 
054:         int i;
055:         for (i = 0; i<nparameters; i++) {
056:                 out->cinfo[i] = "";
057:         }
058:         out->initialtext = ""; 
059:         out->initialtitle = "";
060:         out->xlabel = "";
061:         
062:         for(i = 0; i<nvariables; i++) { 
063:                 out->label[i] = (char *)NULL;
064:                 out->index[i].win = -1;
065:                 out->index[i].cur = -1;
066:         }
067: 
068:         g_printf("%08x %08x\n", (int)out, (int)(out->index));
069:         initout(out);
070: 
071:         if(out->plots==NULL) {
072:                 out->plots = g_malloc(sizeof(plotwindef *)*out->no_windows);
073:                 
074:                 for(i=0; i<out->no_windows; i++) {
075:                         plotwindef *plotwin = g_malloc(sizeof(plotwindef));
076:                         
077:                         plotwin->xmin = out->t0;
078:                         plotwin->xmax = out->t1;
079:                         plotwin->ymin = out->range[i].y0;
080:                         plotwin->ymax = out->range[i].y1;
081:                         plotwin->ordinate = -1;
082:                         plotwin->nlines = 0;
083:                         plotwin->lines = g_malloc(sizeof(int)*nvariables);
084:                         plotwin->xgrid = TRUE;
085:                         plotwin->ygrid = TRUE;
086:                         plotwin->xlabel = out->xlabel;
087:                         plotwin->ylabel = g_strdup("");
088:                         plotwin->title = g_strdup(out->wname[i]);
089:                         
090:                         out->plots[i] = plotwin;
091:                 }
092:                 
093:                 for(i=0; i<nvariables; i++) {
094:                         int win = out->index[i].win;
095:                         if (win>=0) {
096:                                 if (win>=out->no_windows) {
097:                                         g_printf("Oops: index[i].win = %d out->no_windows = %d\n", win, out->no_windows);
098:                                         continue;
099:                                 }
100:                                 out->plots[win]->lines[out->plots[win]->nlines] = i;
101:                                 out->plots[win]->nlines++;
102:                         }
103:                 }
104:         }
105:         
106: }
107: 
108: void error(const char *message)
109: {
110: //      fprintf(cgiOut, "%s<br>\n", message);
111: }
112: 
113: void output(double *s, double t, void *data)
114: {
115: //      DDEMainWindow *window = (DDEMainWindow *)data;
116: //      usercontrol *out = window->out;
117:         usercontrol *out = (usercontrol *)data;
118:         
119: /*      gdouble fraction = (t - out->t0)/(out->t1 - out->t0);
120:         GtkWidget *progress_bar = window->progress_bar;
121:         gtk_progress_bar_set_fraction (GTK_PROGRESS_BAR (progress_bar), fraction); */
122:         
123:         g_array_append_val(out->buffer, t);
124:         int i;
125:         for(i=0; i<out->nvariables; i++) {
126:                 g_array_append_val(out->buffer, s[i]);
127:         }
128:         out->count++;
129: 
130: }
131: 
132: gboolean run_model(usercontrol *out) 
133: {
134:         double dt = out->dt;
135:         int reset = 1;
136:         int fixstep = 0;
137:         
138:         double *state;
139:         state = (double *)g_malloc(out->nvariables*sizeof(double));
140:         initialstate(state, out->c, out->t0);
141: 
142:         out->count = 0;
143:         gint length = 2*out->nvariables*(int)((out->t1 - out->t0)/out->dt);
144:         if (out->buffer!=NULL) {
145:                 g_array_free(out->buffer, TRUE);
146:         }
147:         out->buffer = g_array_sized_new(FALSE, FALSE, sizeof(double), length);
148:         
149:         initialise_dde();
150:         dde(state, out->c, out->t0, out->t1, &dt, out->tol, out->dt, out->nvariables, out->nsw, out->nhv, out->hbsize, 
151:                 out->nlag, reset, fixstep, out);
152:         
153:         free(state);
154: 
155:         int i;
156:         int j;
157:         for(j = 0; j<out->count; j++) {
158:                 for(i=0; i<(out->nvariables + 1); i++) {
159:                         int index = j*(out->nvariables + 1) + i;
160:                         double value = g_array_index(out->buffer, double, index);
161:                         fprintf(stdout, "%g ", value);
162:                 }
163:                 fprintf(stdout, "\n");
164:         }
165:         
166:         if (out->no_windows>0) {
167:                 DDEPlotWin *plotwin = make_plotwin(out);
168:                 out->plotwindow = plotwin;
169:         }
170: 
171:         return TRUE;
172: }