Log In | Get Help   
Home My Page Projects Code Snippets Project Openings Mareframe
Summary Activity Forums Tracker Lists Tasks Docs Surveys News SCM Files
[mareframe] Annotation of /trunk/gadget/conversionindex.cc
[mareframe] / trunk / gadget / conversionindex.cc Repository:
ViewVC logotype

Annotation of /trunk/gadget/conversionindex.cc

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1 - (view) (download)

1 : agomez 1 #include "conversionindex.h"
2 :     #include "errorhandler.h"
3 :     #include "gadget.h"
4 :     #include "global.h"
5 :    
6 :     ConversionIndex::ConversionIndex(const LengthGroupDivision* const L1,
7 :     const LengthGroupDivision* const L2, int interp) {
8 :    
9 :     int i, j, k, nc, nf;
10 :     const LengthGroupDivision* Lf; //will be the finer length group division
11 :     const LengthGroupDivision* Lc; //will be the coarser length group division
12 :     double tmpmin = max(L1->minLength(), L2->minLength());
13 :     double tmpmax = min(L1->maxLength(), L2->maxLength());
14 :    
15 :     error = samedl = offset = isfiner = 0;
16 :     interpolate = interp;
17 :    
18 :     //check to see if the intersection is empty
19 :     if ((tmpmin > tmpmax) || (isEqual(tmpmin, tmpmax))) {
20 :     handle.logMessage(LOGWARN, "Error when checking length structure - empty intersection");
21 :     error = 1;
22 :     return;
23 :     }
24 :    
25 :     if (isZero(L1->dl()) || isZero(L2->dl())) {
26 :     if (!checkLengthGroupStructure(L1, L2)) {
27 :     error = 1;
28 :     return;
29 :     }
30 :     Lf = L1;
31 :     Lc = L2;
32 :    
33 :     } else if (isSmall(L1->dl() - L2->dl())) {
34 :     Lf = L1;
35 :     Lc = L2;
36 :     //check that the length group divisions are aligned
37 :     double check = (Lf->minLength() - Lc->minLength()) / Lf->dl();
38 :     offset = int(check + verysmall);
39 :     if (isEqual(check, floor(check)))
40 :     samedl = 1;
41 :    
42 :     } else if (L1->dl() > L2->dl()) {
43 :     isfiner = 1;
44 :     Lf = L2;
45 :     Lc = L1;
46 :    
47 :     } else {
48 :     Lf = L1;
49 :     Lc = L2;
50 :     }
51 :    
52 :     nf = Lf->numLengthGroups();
53 :     nc = Lc->numLengthGroups();
54 :     //set minlength and maxlength
55 :     for (i = 0; i < nf; i++) {
56 :     if ((Lf->minLength(i) > Lc->minLength()) || (isSmall(Lf->minLength(i) - Lc->minLength()))) {
57 :     minlength = i;
58 :     break;
59 :     }
60 :     }
61 :    
62 :     for (i = nf - 1; i >= 0; i--) {
63 :     if ((Lf->maxLength(i) < Lc->maxLength()) || (isSmall(Lf->maxLength(i) - Lc->maxLength()))) {
64 :     maxlength = i + 1;
65 :     break;
66 :     }
67 :     }
68 :    
69 :     k = 0;
70 :     pos.resize(nf, 0);
71 :     for (i = minlength; i < maxlength; i++) {
72 :     for (j = k; j < nc; j++) {
73 :     if ((Lf->meanLength(i) > Lc->minLength(j)) && (Lf->meanLength(i) < Lc->maxLength(j))) {
74 :     pos[i] = j;
75 :     k = j;
76 :     break;
77 :     }
78 :     }
79 :     }
80 :    
81 :     for (i = maxlength; i < nf; i++)
82 :     pos[i] = nc;
83 :    
84 :     //if minpos and maxpos are needed
85 :     if (!samedl) {
86 :     minpos.resize(nc, nf - 1); //initialised to Lf->Size() - 1
87 :     for (i = minlength; i < maxlength; i++)
88 :     if (i < minpos[pos[i]])
89 :     minpos[pos[i]] = i;
90 :    
91 :     for (i = nc - 1; i > 0; i--)
92 :     if (minpos[i - 1] > minpos[i])
93 :     minpos[i - 1] = minpos[i];
94 :    
95 :     maxpos.resize(nc, 0);
96 :     for (i = minlength; i < maxlength; i++)
97 :     if (i > maxpos[pos[i]])
98 :     maxpos[pos[i]] = i;
99 :    
100 :     for (i = 0; i < nc - 1; i++)
101 :     if (maxpos[i + 1] < maxpos[i])
102 :     maxpos[i + 1] = maxpos[i];
103 :    
104 :     //if number in each length group is needed
105 :     if (isfiner) {
106 :     numpos.resize(nf, 1);
107 :     for (i = minlength; i < maxlength; i++)
108 :     numpos[i] = maxpos[pos[i]] - minpos[pos[i]] + 1;
109 :     }
110 :    
111 :     //if the conversionindex is to be used for interpolation
112 :     if (interpolate) {
113 :     iratio.resize(nf, -1.0);
114 :     ipos.resize(nf, -1);
115 :     k = 0;
116 :     for (i = minlength; i < maxlength; i++) {
117 :     for (j = k; j < nc - 1; j++) {
118 :     if (((Lf->meanLength(i) > Lc->meanLength(j)) || (isSmall(Lf->meanLength(i) - Lc->meanLength(j)))) && (Lf->meanLength(i) < Lc->meanLength(j + 1))) {
119 :    
120 :     ipos[i] = j;
121 :     k = j;
122 :     break;
123 :     }
124 :     }
125 :     }
126 :    
127 :     for (i = 0; i < nf; i++) {
128 :     if (ipos[i] == -1) {
129 :     if (Lf->meanLength(i) < Lc->meanLength(0))
130 :     ipos[i] = 0;
131 :     else
132 :     ipos[i] = nc - 1;
133 :    
134 :     } else {
135 :     iratio[i] = (Lf->meanLength(i) - Lc->meanLength(ipos[i])) /
136 :     (Lc->meanLength(ipos[i] + 1) - Lc->meanLength(ipos[i]));
137 :     }
138 :     }
139 :     }
140 :     }
141 :     }
142 :    
143 :     //The function interpolates values calculated on a coarse length distribution
144 :     //Vc to a finer length distribution Vf using the conversionindex CI
145 :     void ConversionIndex::interpolateLengths(DoubleVector& Vf, const DoubleVector& Vc) {
146 :    
147 :     if (!interpolate)
148 :     handle.logMessage(LOGFAIL, "Error in conversionindex - cannot interpolate between lengthgroups");
149 :    
150 :     int i;
151 :     if (samedl) {
152 :     if (minlength > 0)
153 :     for (i = 0; i < minlength; i++)
154 :     Vf[i] = Vc[0];
155 :    
156 :     for (i = minlength; i < maxlength; i++)
157 :     Vf[i] = Vc[i + offset];
158 :    
159 :     if (maxlength < Vf.Size())
160 :     for (i = maxlength; i < Vf.Size(); i++)
161 :     Vf[i] = Vc[Vc.Size() - 1];
162 :    
163 :     } else {
164 :     for (i = 0; i < Vf.Size(); i++) {
165 :     if (isEqual(iratio[i], -1.0))
166 :     Vf[i] = Vc[ipos[i]];
167 :     else
168 :     Vf[i] = (Vc[ipos[i]] * (1.0 - iratio[i])) + (Vc[ipos[i] + 1] * iratio[i]);
169 :     }
170 :     }
171 :     }

root@forge.cesga.es
ViewVC Help
Powered by ViewVC 1.0.0  

Powered By FusionForge