001: #include <stdio.h>
002: #include <stdlib.h>
003: #include <math.h>
004: #include <strings.h>
005: #include <cgic.h>
006: #include "control.h"
007: #include "dverk.h"
008: 
009: #include "dynamo.h"
010: #include "rossler.h"
011: #include "2sp-osc.h"
012: #include "rinaldi.h"
013: 
014: #define STRMAX 256
015: 
016: /*void initialise(int *nconstants, int *nvariables, int *nplots);
017: void initcontrol(control *c);
018: void fcn(int n, double t, double *y, double *yprime, void *fdata);
019: void output(int n, double *y, double t);
020: control *makeproblem(); */
021: 
022: int cgiMain () 
023: {
024:         int i;
025: 
026:         cgiHeaderContentType("text/json");
027: //      printf("Header line (ignored)\n");
028:         printf("{\"header\":\"odemodels.cgi\"\n");
029: 
030:         char *errorstring = NULL;
031: 
032:         char modelName[STRMAX];
033:         control *c = NULL;
034:         int nProbems = 4;
035:         char *problemList[] = {"dynamo", "rossler", "2sp-osc", "rinaldi"};
036:         control *(*makeFunctions[])() = {makedynamo, makerossler, make2sposc, makerinaldi};
037:         
038:         cgiFormStringNoNewlines("problem", modelName, STRMAX);
039:         
040:         int problemNumber = -1;
041:         for(i=0; i<nProbems; i++) {
042:                 if(strcmp(problemList[i], modelName)==0) {
043:                         problemNumber = i;
044:                         break;
045:                 }
046:         }
047:         
048: /*      if (strncmp(modelName, "dynamo", STRMAX)==0) {
049:                 c = makedynamo();
050:         } else if (strncmp(modelName, "rossler", STRMAX)==0) {
051:                 c = makerossler();
052:         } else if (strncmp(modelName, "2sp-osc", STRMAX)==0) {
053:                 c = make2sposc();
054:         } else if (strncmp(modelName, "rinaldi", STRMAX)==0) {
055:                 c = makerinaldi(); */
056:         if (problemNumber>=0) {
057:                 c = makeFunctions[problemNumber]();
058:         } else {
059:                 printf(",\"problems\":[");
060:                 for(i=0; i<nProbems; i++) {
061:                         if (i>0) {
062:                                 printf(", ");
063:                         }
064:                         printf("\"%s\"", problemList[i]);
065:                 }
066:                 printf("]\n");
067: //              errorstring = astrcpy("No/invalid problem specified.");
068:         }
069:         
070:         if (c!=NULL) {
071:                 cgiFormDoubleBounded("starttime", &(c->t0), 0.0, 1.0e38, c->t0);
072:                 cgiFormDoubleBounded("stoptime", &(c->t1), 0.0, 1.0e38, c->t1);
073:                 cgiFormDoubleBounded("timestep", &(c->dt), 0.0, 1.0e38, c->dt);
074:                 cgiFormDoubleBounded("tolerance", &(c->tol), 0.0, 1.0e38, c->tol);
075:                 
076:                 for(i=0; i<c->nparameters; i++) {
077:                         cgiFormDoubleBounded(c->parameters[i]->name, &(c->parameters[i]->value), -1.0e38, 1.0e38, c->parameters[i]->value);
078:                         //              fprintf(cgiOut, "c[%d] = %f<BR>\n", i, out->c[i]);
079:                 }
080:         }
081:         
082:         cgiFormStringNoNewlines("header", modelName, STRMAX);
083:         int printheader = strncmp(modelName, "yes", STRMAX)==0;
084:         if (printheader && (c!=NULL)) {
085: //      Header
086:                 printf(",\"modelname\":\"%s\",\n", c->title);
087: //              printf("%d\n", 1);
088:                 printf("\"description\":[\"%s\"],\n", c->description);
089:                 printf("\"integratortype\":%d,\n", (int)c->integrator);
090:                 printf("\"controlparams\":[%g, %g, %g, %g],\n", c->t0, c->t1, c->dt, c->tol);
091:                 printf("\"nrows\":%d,\n", c->nrows);
092:                 printf("\"ncolumns\":%d,\n", c->ncolumns);
093:                 
094: //      Constants
095:                 printf("\"nvars\":%d,\n", c->nvariables);
096:                 printf("\"nconsts\":%d,\n", c->nparameters);
097:                 printf("\"constants\":[\n");
098:                 for(i=0; i<c->nparameters; i++) {
099:                         if (i>0) {
100:                                 printf(",");
101:                         }
102:                         printf("{\"val\":%g, \n\"name\":\"%s\", \n\"info\":\"%s\",\n", c->parameters[i]->value, c->parameters[i]->name, c->parameters[i]->info);
103:                         printf("\"row\":%d, ", c->parameters[i]->row);
104:                         printf("\"column\":%d", c->parameters[i]->column);
105:                         printf("}\n");
106:                 }
107:                 printf("],\n");
108:                 
109: //      Variables
110: /*              printf("%g\n%s\n%s\n", y[0], "w", "w");
111:                 printf("%g\n%s\n%s\n", y[0], "y", "y");
112:                 printf("%g\n%s\n%s\n", y[0], "z", "z"); */
113:                 char *colours[] = {"black", "blue", "red", "green", "yellow", "pink", "salmon", "aqua"};
114:                 int ncolour = 0;
115:                 int ncolours = 8;
116:                 printf("\"variables\":[{\"name\":\"t\",\n \"lineColour\": \"black\",\n \"lineStyle\": 0,\n \"markerColour\":\"\",\n");
117:                 printf(" \"markerStyle\":0,\n \"markerSize\":0,\n \"lineThickness\":1}\n");
118:                 for(i=0; i<c->nvariables; i++) {
119:                         printf(",{");
120:                         char *colour = colours[ncolour++];
121:                         printf("\"name\":\"%s\",\n", c->variables[i]->name);
122:                         printf(" \"lineColour\": \"%s\",\n", colour);
123:                         printf(" \"lineStyle\": %d,\n", 1);
124:                         printf(" \"markerColour\":\"%s\",\n", colour);
125:                         printf(" \"markerStyle\":0,\n");
126:                         printf(" \"markerSize\":%d,\n", 5);
127:                         printf(" \"lineThickness\":%d", 1);
128:                         printf("}\n");
129:                         if (ncolour==ncolours) ncolour = 0;
130:                 }
131:                 printf("],\n");
132: 
133: //      Plots
134:                 printf("\"nplots\":%d,\n\"plots\":[\n", c->nplots);
135:                 for(i=0; i<c->nplots; i++) {
136:                         if (i>0) {
137:                                 printf(",");
138:                         }
139:                         printf("{\"title\":\"%s\",\n", c->plots[i]->title);                                     // Title
140:                         printf("\"label\":\"%s\",\n", c->plots[i]->label);                                      // Label for selector
141:                         printf("\"xlabel\":\"%s\",\n", c->plots[i]->ylabel);                            //      y-label
142:                         printf("\"ylabel\":\"%s\",\n", c->plots[i]->xlabel);                            // x-label
143: //                      printf("\"%d\",\n", c->plots[i]->nlines);
144:                         printf("\"lines\":[%d,", c->plots[i]->ordinate);
145:                         int j;
146:                         for(j=0; j<c->plots[i]->nlines; j++) {
147:                                 if (j>0) {
148:                                         printf(",");
149:                                 }
150:                                 printf(" %d", c->plots[i]->abscissae[j]);               // variables to print (0 is time)
151:                         }
152:                         printf("]}\n");
153:                 }
154:                 printf("]\n");
155:                 
156: //      } else {
157:         }
158: 
159:         dverkcomm *dvc = NULL;
160:         double *y = NULL;
161:         if (c!=NULL) {
162:                 y = calloc(c->nvariables, sizeof(double));
163:                 initvariables(c, y);
164:                 dvc = makeDverkCommunication(c);
165:                 dvc->ctrl = c;
166:                 
167:                 c->maxoutput = floor(1.5*(c->t1 - c->t0)/c->dt);
168:                 c->results = (double *)calloc((c->nvariables + 1)*c->maxoutput, sizeof(double));
169:                 c->outputcounter = 0;
170:                 
171: //              double t = c->t0;
172: //              double tol = c->tol;
173: //              int n = c->nvariables;
174:                 c->output(c, c->nvariables, y, c->t0);
175:         }
176: 
177: /*      while(t<=c->t1) {
178:                 double tend = t + c->dt;
179:                 dvc->solver(dvc, c->fcn, t, y, tend, tol, &ind, n, c);
180:                 t = tend;
181:                 if (t<=c->t1) c->output(n, y, t);
182:         } */
183:         
184: //      printresults(c);
185: //      int comma = 0;
186:         
187:         cgiFormStringNoNewlines("results", modelName, STRMAX);
188:         if ((strncmp(modelName, "yes", STRMAX)==0) && (c!=NULL)) {
189:                 int ind = 1;
190:                 dvc->solver(dvc, c->fcn, c->t0, y, c->t1, c->tol, &ind, c->nvariables, c);
191:                 printf(",\"results\":[");
192:                 int j;
193:                 for(j=0; j<=c->nvariables; j++) {
194:                         double minimum;
195:                         double maximum;
196:                         printf("{");
197:                         printf("\"data\":[");
198:                         for(i=0; i<c->outputcounter; i++) {
199:                                 double y = c->results[i*(c->nvariables + 1) + j];
200:                                 if (i>0) {
201:                                         printf(", ");
202:                                         if (y>maximum) {
203:                                                 maximum = y;
204:                                         }
205:                                         if (y<minimum) {
206:                                                 minimum = y;
207:                                         }
208:                                 } else {
209:                                         maximum = y;
210:                                         minimum = y;
211:                                 }
212:                                 printf("%g", y);
213:                         }
214:                         printf("]");
215:                         printf(", \"minimum\": %g", minimum);
216:                         printf(", \"maximum\": %g", maximum);
217:                         printf(", \"increment\": %g", (maximum - minimum)/4.0);
218:                         printf("}");
219:                         if (j<c->nvariables) {
220:                                 printf(", ");
221:                         }
222:                         printf("\n"); 
223:                 }
224:                 printf("]\n");
225:         }
226:         printf(",\"errors\":[");
227:         if (errorstring!=NULL) printf("\"%s\"", errorstring);
228:         printf("]\n");
229:         printf("}\n");
230: 
231:         if (dvc!=NULL) freeDverkCommunication(dvc);
232:         if (y!=NULL) free(y);
233:         if (errorstring!=NULL) free(errorstring);
234: 
235:     return 0;
236: }