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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 3337 - (show 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 #ifndef FPROPS_TEST_H
2 #define FPROPS_TEST_H
3
4 #include <CUnit/CUnit.h>
5
6 #include "rundata.h"
7 #include "color.h"
8 //#include "sat.h"
9
10 /* Macros and type declarations for running simple test cases */
11
12 /**
13 Set up test environment, if required.
14 */
15 void test_init();
16
17 typedef struct{double T,p,rho,u,h,s,cv,cp,cp0,a;} TestData;
18
19 typedef struct{double T,p,rhof,rhog,hf,hg,sf,sg;} TestDataSat;
20
21 /*, {Temperature, Pressure, Density, Int. Energy, Enthalpy, Entropy, Cp, Cp0, Helmholtz} */
22
23 /**
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 @param temp_unit Set to 'C' for celsius or 'K' for Kelvin.
28
29 @return 1 if any failures occurred.
30 */
31 int helm_run_test_cases(const PureFluid *d, unsigned ntd, const TestData *td, int temp_unit);
32
33 /**
34 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 int helm_run_saturation_tests(const PureFluid *d, unsigned nsd, const TestDataSat *td, int temp_unit);
42
43 /**
44 Calculate values of 'c' and 'm' for the IdealData, based on expected values of h, s at reference
45 state.
46
47 TODO incorporate this with *_prepare functions somehow? Move to fprops.h?
48 */
49 int helm_calc_offsets(double Tref, double rhoref, double href, double sref, const PureFluid *fluid);
50
51
52 /**
53 Check 'u' values and output discrepancy for plotting.
54 */
55 int helm_check_u(const PureFluid *d, unsigned ntd, const TestData *td);
56
57 /**
58 Check dp/dT values (rho constant) by comparison with finite
59 difference estimates (check that helmholtz_p is working first!)
60 */
61 int helm_check_p_T(const PureFluid *d, unsigned ntd, const TestData *td);
62
63
64 int helm_check_d2pdrho2_T(const PureFluid *d, unsigned ntd, const TestData *td);
65
66 /*
67 Check the value of critical pressure given in the species data.
68 */
69 int helm_check_p_c(const HelmholtzData *d);
70
71 //#define TEST_VERBOSE
72
73 #ifdef TEST_VERBOSE
74 # define TEST_SUCCESS(FN,PARAM1,PARAM2,PARAM3,VAL) \
75 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 ,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 # 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 # define TEST_MSG(MSG,...) fprintf(stderr,MSG "\n",##__VA_ARGS__)
99 #else
100 # define TEST_SUCCESS(...)
101 # define TEST_SUCCESS_PROP(...)
102 # define TEST_MSG(MSG,ARGS...)
103 # define TEST_SUCCESS2(...)
104 #endif
105
106 #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 CU_TEST_FATAL(FACT)\
114 }
115 /* a simple macro to actually do the testing */
116 #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 CU_TEST_FATAL(fabs(x_err)<=fabs(TOL));\
135 }
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 CU_TEST(fabs(x_err)<=fabs(TOL));\
156 }
157
158 /* a simple macro to actually do the testing */
159 #define ASSERT_TOL_3(FN,PARAM1,PARAM2,PARAM3,VAL,TOL) {\
160 double cval; cval = FN(PARAM1,PARAM2,PARAM3);\
161 double x_err = cval - (double)(VAL);\
162 double relerrpc = (cval-(VAL))/(VAL)*100;\
163 if(fabs(relerrpc)>maxerr)maxerr=fabs(relerrpc);\
164 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 );\
174 }else{\
175 TEST_SUCCESS(#FN,PARAM1,PARAM2,PARAM3,VAL);\
176 }\
177 CU_TEST_FATAL(fabs(x_err)<=fabs(TOL));\
178 }
179
180 /* even simpler testing of an assertion */
181 #define ASSERT_TOL_VAL(CALC,REF,TOL) {\
182 double cval = (CALC);\
183 double rval = (REF);\
184 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 );\
196 }else{\
197 TEST_SUCCESS2(CALC,rval,relerr);\
198 }\
199 CU_TEST_FATAL(fabs(x_err)<=fabs(TOL));\
200 }
201
202
203 #endif

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