/[ascend]/trunk/models/johnpye/fprops/test.h
ViewVC logotype

Annotation of /trunk/models/johnpye/fprops/test.h

Parent Directory Parent Directory | Revision Log Revision Log


Revision 3337 - (hide annotations) (download) (as text)
Sun Mar 25 00:44:38 2018 UTC (6 years, 4 months ago) by jpye
File MIME type: text/x-chdr
File size: 6177 byte(s)
testing for fractions, starting on dimen

1 jpye 2654 #ifndef FPROPS_TEST_H
2     #define FPROPS_TEST_H
3 jpye 1886
4 jpye 3329 #include <CUnit/CUnit.h>
5    
6 jpye 2654 #include "rundata.h"
7     #include "color.h"
8     //#include "sat.h"
9 jpye 1886
10     /* Macros and type declarations for running simple test cases */
11    
12 jpye 2654 /**
13     Set up test environment, if required.
14     */
15     void test_init();
16    
17 jpye 1886 typedef struct{double T,p,rho,u,h,s,cv,cp,cp0,a;} TestData;
18    
19 jpye 2292 typedef struct{double T,p,rhof,rhog,hf,hg,sf,sg;} TestDataSat;
20    
21 jpye 2654 /*, {Temperature, Pressure, Density, Int. Energy, Enthalpy, Entropy, Cp, Cp0, Helmholtz} */
22 jpye 2111
23 jpye 1886 /**
24     Run tests for p, u, h, s, a against values from a user-provided TestData array
25     Tolerances are specified in the cdoe, in test.c.
26    
27 jpye 2111 @param temp_unit Set to 'C' for celsius or 'K' for Kelvin.
28    
29 jpye 1886 @return 1 if any failures occurred.
30     */
31 jpye 2654 int helm_run_test_cases(const PureFluid *d, unsigned ntd, const TestData *td, int temp_unit);
32 jpye 1886
33 jpye 1891 /**
34 jpye 2292 Run tests data in the saturation region from a user-provided TestData array
35     Tolerances are specified in the code, in test.c.
36    
37     @param temp_unit Set to 'C' for celsius or 'K' for Kelvin.
38    
39     @return 1 if any failures occurred.
40     */
41 jpye 2654 int helm_run_saturation_tests(const PureFluid *d, unsigned nsd, const TestDataSat *td, int temp_unit);
42 jpye 2292
43     /**
44 jpye 2285 Calculate values of 'c' and 'm' for the IdealData, based on expected values of h, s at reference
45     state.
46 jpye 2654
47     TODO incorporate this with *_prepare functions somehow? Move to fprops.h?
48 jpye 2285 */
49 jpye 2654 int helm_calc_offsets(double Tref, double rhoref, double href, double sref, const PureFluid *fluid);
50 jpye 2285
51    
52     /**
53 jpye 1891 Check 'u' values and output discrepancy for plotting.
54     */
55 jpye 2654 int helm_check_u(const PureFluid *d, unsigned ntd, const TestData *td);
56 jpye 1891
57 jpye 1905 /**
58     Check dp/dT values (rho constant) by comparison with finite
59     difference estimates (check that helmholtz_p is working first!)
60     */
61 jpye 2654 int helm_check_p_T(const PureFluid *d, unsigned ntd, const TestData *td);
62 jpye 1905
63 jpye 2235
64 jpye 2654 int helm_check_d2pdrho2_T(const PureFluid *d, unsigned ntd, const TestData *td);
65 jpye 2235
66 jpye 2246 /*
67     Check the value of critical pressure given in the species data.
68     */
69     int helm_check_p_c(const HelmholtzData *d);
70 jpye 2235
71 jpye 3337 //#define TEST_VERBOSE
72 jpye 2111
73     #ifdef TEST_VERBOSE
74     # define TEST_SUCCESS(FN,PARAM1,PARAM2,PARAM3,VAL) \
75 jpye 2654 fprintf(stderr," ");\
76     color_on(stderr,ASC_FG_GREEN);\
77     fprintf(stderr,"OK");\
78     color_off(stderr);\
79     fprintf(stderr,", %s(%f,%f,%s) = %8.2e with %.6f%% err.\n"\
80 jpye 3329 ,FN,PARAM1,PARAM2,#PARAM3,VAL,relerrpc)
81    
82     # define TEST_SUCCESS2(CALC,RVAL,RELERR) \
83     fprintf(stderr," ");\
84     color_on(stderr,ASC_FG_GREEN);\
85     fprintf(stderr,"OK");\
86     color_off(stderr);\
87     fprintf(stderr,", %s = %8.2e with %0.6f%% err.\n"\
88     ,#CALC,RVAL,RELERR*100);
89    
90 jpye 2654 # define TEST_SUCCESS_PROP(PROP,STATE,VAL) \
91     fprintf(stderr," ");\
92     color_on(stderr,ASC_FG_GREEN);\
93     fprintf(stderr,"OK");\
94     color_off(stderr);\
95     fprintf(stderr,", %s(T=%f,rho=%f) = %8.2e with %.6f%% err.\n"\
96     ,PROP,STATE.T,STATE.rho,VAL,relerrpc\
97     )
98 jpye 3333 # define TEST_MSG(MSG,...) fprintf(stderr,MSG "\n",##__VA_ARGS__)
99 jpye 2111 #else
100 jpye 3329 # define TEST_SUCCESS(...)
101     # define TEST_SUCCESS_PROP(...)
102     # define TEST_MSG(MSG,ARGS...)
103     # define TEST_SUCCESS2(...)
104 jpye 2111 #endif
105    
106 jpye 2654 #define ASSERT(FACT) {\
107     if(!(FACT)){\
108     color_on(stderr,ASC_BG_RED|ASC_FG_BLACK);\
109     fprintf(stderr,"ERROR");\
110     color_off(stderr);\
111     fprintf(stderr," %s:%d: failed assertion '%s'\n",__FILE__,__LINE__,#FACT);\
112     }\
113 jpye 3329 CU_TEST_FATAL(FACT)\
114 jpye 2654 }
115 jpye 1886 /* a simple macro to actually do the testing */
116 jpye 2654 #define ASSERT_TOL(FN,PARAM1,PARAM2,PARAM3,PARAM4,VAL,TOL) {\
117     double cval; cval = FN(PARAM1,PARAM2,PARAM3,PARAM4);\
118     double x_err = cval - (double)(VAL);\
119     double relerrpc = (cval-(VAL))/(VAL)*100;\
120     if(fabs(relerrpc)>maxerr)maxerr=fabs(relerrpc);\
121     if(fabs(x_err)>fabs(TOL)){\
122     fprintf(stderr," ");\
123     color_on(stderr,ASC_FG_BRIGHTRED);\
124     fprintf(stderr,"ERROR");\
125     color_off(stderr);\
126     fprintf(stderr," %s:%d: value of '%s(%f,%f,%s)' = %.5e,"\
127     " should be %.5e, error is %.10e (%.2f%%, %1e)!\n"\
128     , __FILE__,__LINE__, #FN,PARAM1,PARAM2,#PARAM3, cval, VAL,cval-(VAL)\
129     ,relerrpc,relerrpc/100\
130     );\
131     }else{\
132     TEST_SUCCESS(#FN,PARAM1,PARAM2,PARAM3,VAL);\
133     }\
134 jpye 3329 CU_TEST_FATAL(fabs(x_err)<=fabs(TOL));\
135 jpye 2654 }
136    
137     #define ASSERT_PROP(PROP,STATE,ERR1,VAL,TOL){\
138     double cval = fprops_##PROP(STATE,ERR1);\
139     double x_err = cval - (double)VAL;\
140     double relerrpc = (cval - (VAL))/(VAL)*100;\
141     if(fabs(relerrpc)>maxerr)maxerr=fabs(relerrpc);\
142     if(fabs(x_err)>fabs(TOL)){\
143     fprintf(stderr," ");\
144     color_on(stderr,ASC_FG_BRIGHTRED);\
145     fprintf(stderr,"ERROR");\
146     color_off(stderr);\
147     fprintf(stderr," %s:%d: value of '%s(T=%f,rho=%f)' = %.5e,"\
148     " should be %.5e, error is %.10e (%.2f%%, %1e)!\n"\
149     , __FILE__,__LINE__, #PROP,STATE.T,STATE.rho, cval, VAL,x_err\
150     ,relerrpc,relerrpc/100\
151     );\
152     }else{\
153     TEST_SUCCESS_PROP(#PROP,STATE,VAL);\
154     }\
155 jpye 3333 CU_TEST(fabs(x_err)<=fabs(TOL));\
156 jpye 2654 }
157    
158     /* a simple macro to actually do the testing */
159     #define ASSERT_TOL_3(FN,PARAM1,PARAM2,PARAM3,VAL,TOL) {\
160 jpye 1886 double cval; cval = FN(PARAM1,PARAM2,PARAM3);\
161 jpye 2654 double x_err = cval - (double)(VAL);\
162 jpye 1886 double relerrpc = (cval-(VAL))/(VAL)*100;\
163     if(fabs(relerrpc)>maxerr)maxerr=fabs(relerrpc);\
164 jpye 2654 if(fabs(x_err)>fabs(TOL)){\
165     fprintf(stderr," ");\
166     color_on(stderr,ASC_FG_BRIGHTRED);\
167     fprintf(stderr,"ERROR");\
168     color_off(stderr);\
169     fprintf(stderr," %s:%d: value of '%s(%f,%f,%s)' = %.5e,"\
170     " should be %.5e, error is %.10e (%.2f%%, %1e)!\n"\
171     , __FILE__,__LINE__, #FN,PARAM1,PARAM2,#PARAM3, cval, VAL,cval-(VAL)\
172     ,relerrpc,relerrpc/100\
173 jpye 1886 );\
174     }else{\
175 jpye 2111 TEST_SUCCESS(#FN,PARAM1,PARAM2,PARAM3,VAL);\
176 jpye 1886 }\
177 jpye 3329 CU_TEST_FATAL(fabs(x_err)<=fabs(TOL));\
178 jpye 1886 }
179    
180 jpye 2262 /* even simpler testing of an assertion */
181     #define ASSERT_TOL_VAL(CALC,REF,TOL) {\
182     double cval = (CALC);\
183     double rval = (REF);\
184 jpye 2654 double x_err = cval - rval;\
185     double relerr = (cval-rval)/rval;\
186     if(fabs(relerr*100)>maxerr)maxerr=fabs(relerr*100);\
187     if(fabs(x_err)>fabs(TOL)){\
188     fprintf(stderr," ");\
189     color_on(stderr,ASC_FG_BRIGHTRED);\
190     fprintf(stderr,"ERROR");\
191     color_off(stderr);\
192     fprintf(stderr," %s:%d: value of '%s' = %.5e,"\
193     " should be %.5e, error is %.10e (%.2f%%, %1e)!\n"\
194     ,__FILE__,__LINE__,#CALC,cval,rval,cval-rval,relerr*100,relerr\
195 jpye 2262 );\
196     }else{\
197 jpye 3329 TEST_SUCCESS2(CALC,rval,relerr);\
198 jpye 2262 }\
199 jpye 3329 CU_TEST_FATAL(fabs(x_err)<=fabs(TOL));\
200 jpye 2262 }
201    
202 jpye 3329
203 jpye 1886 #endif

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