00001
00005 #define USE_GLSOLVERS
00006 #include "gridlabd.h"
00007
00008 #ifdef USE_NEWSOLVER
00009
00010 double a=0,n=0,b=0,m=0,c=0;
00011
00012 double ft(double t)
00013 {
00014 return a*exp(t*n) + b*exp(t*m) + c;
00015 }
00016 double dfdt(double t)
00017 {
00018 return a*n*exp(t*n) + b*m*exp(t*m);
00019 }
00020 double e2solve(double _a, double _n, double _b, double _m, double _c, double p, double *e)
00021 {
00022 static glsolver *newton_method = NULL;
00023 static struct {
00024 unsigned int n;
00025 double *x;
00026 double (**f)(double);
00027 double (**df)(double);
00028 double *p;
00029 unsigned int *m;
00030 unsigned char *s;
00031 } data;
00032
00033
00034 if ( newton_method==NULL )
00035 {
00036 newton_method = new glsolver("newton_method");
00037 unsigned int version=0;
00038 if ( newton_method->get("version",&version,NULL)==0 || version!=1 )
00039 throw "newton_method version incorrect";
00040 if ( newton_method->set("dimensions",1,NULL)==0 )
00041 throw "newton_method dimensions can't be set";
00042 if ( newton_method->get("init",&data,NULL)==0 )
00043 throw "newton_method data can't be obtained";
00044 data.f[0] = ft;
00045 data.df[0] = dfdt;
00046 }
00047
00048
00049 data.x[0] = 0.0;
00050 data.p[0] = p;
00051 a=_a; b=_b; c=_c; n=_n; m=_m;
00052 if ( newton_method->solve(&data) )
00053 {
00054 if ( e!=NULL )
00055 *e = data.x[0] / dfdt(data.x[0]);
00056 return data.x[0];
00057 }
00058 else
00059 return NaN;
00060 }
00061
00062 #else
00063
00064 double e2solve(double a, double n, double b, double m, double c, double p, double *e)
00065 {
00066
00067 static glsolver *etp = NULL;
00068 static struct etpdata {
00069 double t,a,n,b,m,c,p,e;
00070 unsigned int i;
00071 } data;
00072 if ( etp==NULL )
00073 {
00074 etp = new glsolver("etp");
00075 int version;
00076 if ( etp->get("version",&version,NULL)==0 || version!=1 )
00077 throw "incorrect ETP solver version";
00078 if ( etp->get("init",&data,NULL)==0 )
00079 throw "unable to initialize ETP solver data";
00080 data.i = 100;
00081 }
00082
00083
00084 data.t = 0;
00085 data.a = a;
00086 data.b = b;
00087 data.c = c;
00088 data.n = n;
00089 data.m = m;
00090 data.p = p;
00091 if ( etp->solve(&data) )
00092 {
00093 if ( e!=NULL )
00094 *e = data.e;
00095 return data.t;
00096 }
00097 else
00098 return NaN;
00099 }
00100
00101 #endif