-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathoutput.h
465 lines (447 loc) · 16.4 KB
/
output.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
//
// CDpal
//
// This software is intended to be used for curve fitting of CD thermal
// denaturation data.
//
// Copyright (C) 2015 Markus Niklasson and Patrik Lundström
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see http://www.gnu.org/licenses/.
//
// Programmers: Markus Niklasson and Patrik Lundström
//
// Adress correspondence to: patlu@ifm.liu.se
// Date: 18 August, 2017
// Version: 2.18
//
#include "cstdlib"
#include "iostream"
#include "globals.h"
#define NO_PLOTSTEP 1000
using namespace std;
struct Output
{
std::vector< double > x, y, err, parVec;
void (*function)(const double, std::vector< double > &, double &, std::vector< double > &);
std::vector< double > jackerr;
std::vector< std::vector< double > > variance;
int size;
int parSize;
int DoF;
double chi;
char *plotfile;
char *plotfileFit;
std::string deltaH, Tm, ddeltaH, dTm;
Output() {}
Output(std::vector< double > &xVec, std::vector< double > &yVec, std::vector< double > &error, std::vector< double > ¶ms,
void (*fitData)(const double, std::vector< double > &, double &, std::vector< double > &), std::vector< double > &jjackerr,
std::vector< std::vector< double > > &variance2, double chi2, int dof, char *pplotfile, char *pplotfileFit) :
x(xVec), y(yVec), err(error), parVec(params), function(fitData),jackerr(jjackerr),
variance(variance2), size(x.size()), parSize(parVec.size()),
DoF(dof), chi(chi2), plotfile(pplotfile), plotfileFit(pplotfileFit) { }
void print(bool fjack=false) {
extern std::string resultStr;
extern std::vector< std::string> rV;
rV.clear();
extern bool fitRes;
if(fitRes)
{
std::stringstream ss, ss2;
std::string str, str2;
ss.width(7);
ss.precision(7);
ss << parVec[0];
ss >> str;
ss2 << parVec[1];
ss2 >> str2;
resultStr = str + " " + str2;
return;
}
resultStr="Number of points = ";
std::string str, str2, str3;
std::stringstream ss, ss2, ss3;
ss << size;
ss >> str;
rV.push_back(str);
resultStr += str;
resultStr +="<br>Degrees of freedom = ";
ss2 << DoF;
ss2 >> str2;
rV.push_back(str2);
resultStr += str2;
resultStr += "<br>Sum of residuals = ";
ss3 << chi;
ss3 >> str3;
rV.push_back(str3);
resultStr += str3;
resultStr += "<br><br>";
printLine(-1,fjack);
for (int i = 0; i < parSize; i++)
{
printLine(i,fjack);
}
//Print deltaS, calculated from deltaH/Tm, also with Error propagation
resultStr+="<FONT COLOR='#0101DF'>ΔS<sub> </sub></FONT>";
std::stringstream ss4;
ss4.width(7);
ss4.precision(7);
std::string VAL;
if(atof(Tm.c_str())>0)
{
ss4 << atof(deltaH.c_str())/atof(Tm.c_str());
ss4 >> VAL;
}
else
VAL="N/A";
int c1=0;
if(VAL[0]!='-')
{
resultStr += " ";
c1++;
}
rV.push_back(VAL);
resultStr += VAL;
for(int i= VAL.length(); i<=12-c1; i++)
resultStr += " ";
resultStr+=" ";
std::stringstream ss5;
ss5.width(7);
ss5.precision(7);
if(atof(Tm.c_str())>0)
{
double exp1, exp2;
exp1 = atof(ddeltaH.c_str())/atof(Tm.c_str());
exp2 = atof(dTm.c_str())*atof(deltaH.c_str())/(atof(Tm.c_str())*atof(Tm.c_str()));
exp1 *= exp1;
exp2 *= exp2;
ss5 << sqrt(exp1 + exp2);
ss5 >> VAL;
}
else
VAL = "N/A";
if(VAL[0]!='-')
resultStr+=" ";
rV.push_back(VAL);
resultStr+=VAL;
resultStr+="<br>";
}
void plot(bool fplot=true, bool fplotFit=true)
{
std::vector< double > dydx(parSize);
double yplot;
double step;
if (fplot)
{
{
const int M=7,NSTACK=64;
int i,ir,j,k,jstack=-1,l=0,n=x.size();
double a;
double b;
double c;
std::vector< int > istack(NSTACK);
ir=n-1;
for (;;) {
if (ir-l < M) {
for (j=l+1;j<=ir;j++) {
a=x[j];
b=y[j];
c=err[j];
for (i=j-1;i>=l;i--) {
if (x[i] <= a) break;
x[i+1]=x[i];
y[i+1]=y[i];
err[i+1]=err[i];
}
x[i+1]=a;
y[i+1]=b;
err[i+1]=c;
}
if (jstack < 0) break;
ir=istack[jstack--];
l=istack[jstack--];
} else {
k=(l+ir) >> 1;
std::swap(x[k],x[l+1]);
std::swap(y[k],y[l+1]);
std::swap(err[k],err[l+1]);
if (x[l] > x[ir]) {
std::swap(x[l],x[ir]);
std::swap(y[l],y[ir]);
std::swap(err[l],err[ir]);
}
if (x[l+1] > x[ir]) {
std::swap(x[l+1],x[ir]);
std::swap(y[l+1],y[ir]);
std::swap(err[l+1],err[ir]);
}
if (x[l] > x[l+1]) {
std::swap(x[l],x[l+1]);
std::swap(y[l],y[l+1]);
std::swap(err[l],err[l+1]);
}
i=l+1;
j=ir;
a=x[l+1];
b=y[l+1];
c=err[l+1];
for (;;) {
do i++; while (x[i] < a);
do j--; while (x[j] > a);
if (j < i) break;
std::swap(x[i],x[j]);
std::swap(y[i],y[j]);
std::swap(err[i],err[j]);
}
x[l+1]=x[j];
x[j]=a;
y[l+1]=y[j];
y[j]=b;
err[l+1]=err[j];
err[j]=c;
jstack += 2;
if (jstack >= NSTACK) throw("NSTACK too small in sort2.");
if (ir-i+1 >= j-l) {
istack[jstack]=ir;
istack[jstack-1]=i;
ir=j-1;
} else {
istack[jstack]=j-1;
istack[jstack-1]=l;
l=i;
}
}
}
}
extern std::vector< std::string > xplotfit, yplotfit;
xplotfit.clear();
yplotfit.clear();
for (unsigned int i=0; i<static_cast<unsigned int>(x.size()); i++)
{
function(x[i], parVec, yplot, dydx);
std::stringstream ss,ss2;
std::string str, str2;
ss << x[i];
ss >> str;
xplotfit.push_back(str);
ss2 << yplot;
ss2 >> str2;
yplotfit.push_back(str2);
}
//myFile.close();
}
if (fplotFit)
{
{
const int M=7,NSTACK=64;
int i,ir,j,k,jstack=-1,l=0,n=x.size();
double a;
double b;
double c;
std::vector< int > istack(NSTACK);
ir=n-1;
for (;;) {
if (ir-l < M) {
for (j=l+1;j<=ir;j++) {
a=x[j];
b=y[j];
c=err[j];
for (i=j-1;i>=l;i--) {
if (x[i] <= a) break;
x[i+1]=x[i];
y[i+1]=y[i];
err[i+1]=err[i];
}
x[i+1]=a;
y[i+1]=b;
err[i+1]=c;
}
if (jstack < 0) break;
ir=istack[jstack--];
l=istack[jstack--];
} else {
k=(l+ir) >> 1;
std::swap(x[k],x[l+1]);
std::swap(y[k],y[l+1]);
std::swap(err[k],err[l+1]);
if (x[l] > x[ir]) {
std::swap(x[l],x[ir]);
std::swap(y[l],y[ir]);
std::swap(err[l],err[ir]);
}
if (x[l+1] > x[ir]) {
std::swap(x[l+1],x[ir]);
std::swap(y[l+1],y[ir]);
std::swap(err[l+1],err[ir]);
}
if (x[l] > x[l+1]) {
std::swap(x[l],x[l+1]);
std::swap(y[l],y[l+1]);
std::swap(err[l],err[l+1]);
}
i=l+1;
j=ir;
a=x[l+1];
b=y[l+1];
c=err[l+1];
for (;;) {
do i++; while (x[i] < a);
do j--; while (x[j] > a);
if (j < i) break;
std::swap(x[i],x[j]);
std::swap(y[i],y[j]);
std::swap(err[i],err[j]);
}
x[l+1]=x[j];
x[j]=a;
y[l+1]=y[j];
y[j]=b;
err[l+1]=err[j];
err[j]=c;
jstack += 2;
if (jstack >= NSTACK) throw("NSTACK too small in sort2.");
if (ir-i+1 >= j-l) {
istack[jstack]=ir;
istack[jstack-1]=i;
ir=j-1;
} else {
istack[jstack]=j-1;
istack[jstack-1]=l;
l=i;
}
}
}
}
extern std::vector< std::string > xplotfit, yplotfit;
xplotfit.clear();
yplotfit.clear();
step = (x[x.size()-1]-x[0])/(double)NO_PLOTSTEP;
for (int i=0; i<=NO_PLOTSTEP; i++) {
function(x[0]+i*step, parVec, yplot, dydx);
std::stringstream ss,ss2;
std::string str, str2;
ss << x[0]+i*step;
ss >> str;
xplotfit.push_back(str);
ss2 << yplot;
ss2 >> str2;
yplotfit.push_back(str2);
}
}
}
private:
double showDecimals(const double& x, const int& numDecimals) {
int y=x;
double z=x-y;
double m=pow(10,numDecimals);
double q=z*m;
double r=round(q);
return static_cast<double>(y)+(1.0/m)*r;
}
void printLine(int index, bool fjack)
{
extern std::string resultStr;
extern std::vector< std::string> rV;
std::stringstream ss,ss4;
std::string str,str4;
if (index > -1)
{
if(index==0)
resultStr+="<FONT COLOR='#FF0000'>ΔH<sub> </sub></FONT>";
else if(index==1)
resultStr+="<FONT COLOR='#00FF00'>T<sub>m</sub> </FONT>";
else if(index==2)
resultStr+="<FONT COLOR='#0000FF'>I<sub>N</sub> </FONT>";
else if(index==3)
resultStr+="<FONT COLOR='#F000F0'>I<sub>D</sub> </FONT>";
else if(index==4)
resultStr+="<FONT COLOR='#00F0F0'>k<sub>N</sub> </FONT>";
else if(index==5)
resultStr+="<FONT COLOR='#330000'>k<sub>D</sub> </FONT>";
else if(index==6)
resultStr+="<FONT COLOR='#FF0000'>ΔCp<sub> </sub></FONT>";
else if(index==7)
resultStr+="<FONT COLOR='#00FF00'>ΔH<sub>2</sub></FONT>";
else if(index==8)
resultStr+="<FONT COLOR='#0000FF'>T<sub>m</sub>2</FONT>";
else if(index==9)
resultStr+="<FONT COLOR='#F000F0'>I<sub>I</sub> </FONT>";
else if(index==10)
resultStr+="<FONT COLOR='#00F0F0'>k<sub>I</sub> </FONT>";
else if(index==11)
resultStr+="<FONT COLOR='#330000'>ΔCp<sub>2</sub></FONT>";
else if(index==12)
resultStr+="<FONT COLOR='#FF0000'>P<sub>0</sub> </FONT>";
ss.width(7);
ss.precision(7);
if(index==0 || index==7)
ss << parVec[index]/1000.0;
else if(index==12)
ss << parVec[index]*1000000.0;
else
ss << parVec[index];
ss >> str;
int c1=0;
if(str[0]!='-' && index!=6 && index!=11)
{
resultStr+=" ";
c1++;
}
else if(index==11 || index==6)
{
c1++;
}
rV.push_back(str);
resultStr += str;
if(index==0)
deltaH=str;
else if(index==1)
Tm=str;
for(int i= str.length(); i<=12-c1; i++)
resultStr += " ";
}
else
{
resultStr += " <sub> </sub>Fitted ";
}
if (fjack) {
if (index > -1)
{
resultStr+=" ";
ss4.width(7);
ss4.precision(7);
if(index==0 || index==7)
ss4 << jackerr[index]/1000.0;
else if(index==12)
ss4 << jackerr[index]*1000000.0;
else
ss4 << jackerr[index];
ss4 >> str4;
if(index==0)
ddeltaH = str4;
else if(index==1)
dTm = str4;
if(str4[0]!='-')
resultStr+=" ";
rV.push_back(str4);
resultStr += str4;
}
else
{
resultStr += "Jackknife error";
}
}
resultStr += "<br>";
}
};
#undef NO_PLOTSTEP