ViewVC Help
View File | Revision Log | Show Annotations | View Changeset | Root Listing
root/OpenMD/trunk/src/applications/sequentialProps/ContactAngle2.cpp
(Generate patch)

Comparing trunk/src/applications/sequentialProps/ContactAngle2.cpp (file contents):
Revision 2035 by gezelter, Tue Nov 4 15:31:51 2014 UTC vs.
Revision 2071 by gezelter, Sat Mar 7 21:41:51 2015 UTC

# Line 48 | Line 48
48   #include "primitives/Molecule.hpp"
49   #include "utils/NumericConstant.hpp"
50   #include "utils/PhysicalConstants.hpp"
51 < #include "math/Polynomial.hpp"
51 > #include "math/Eigenvalue.hpp"
52  
53   namespace OpenMD {
54 <
54 >  
55    ContactAngle2::ContactAngle2(SimInfo* info, const std::string& filename,
56                                 const std::string& sele, RealType solidZ,
57 <                               RealType threshDens, int nrbins, int nzbins)
58 <    : SequentialAnalyzer(info, filename), selectionScript_(sele),
59 <      evaluator_(info), seleMan_(info), solidZ_(solidZ),
60 <      threshDens_(threshDens), nRBins_(nrbins), nZBins_(nzbins) {
57 >                               RealType threshDens, RealType bufferLength,
58 >                               int nrbins, int nzbins)
59 >  : SequentialAnalyzer(info, filename), solidZ_(solidZ),
60 >    threshDens_(threshDens), bufferLength_(bufferLength), nRBins_(nrbins),
61 >    nZBins_(nzbins), selectionScript_(sele), seleMan_(info),
62 >    evaluator_(info) {
63      
64      setOutputName(getPrefix(filename) + ".ca2");
65      
# Line 77 | Line 79 | namespace OpenMD {
79      Mat3x3d hmat = info_->getSnapshotManager()->getCurrentSnapshot()->getHmat();
80      RealType len = std::min(hmat(0, 0), hmat(1, 1));
81      RealType zLen = hmat(2,2);
82 +
83      RealType dr = len / (RealType) nRBins_;
84      RealType dz = zLen / (RealType) nZBins_;
85  
86      std::vector<std::vector<RealType> > histo;
87      histo.resize(nRBins_);
85    for (int i = 0 ; i < nRBins_; ++i) {
86      histo[i].resize(nZBins_);
87    }
88      for (unsigned int i = 0; i < histo.size(); ++i){
89 +      histo[i].resize(nZBins_);
90        std::fill(histo[i].begin(), histo[i].end(), 0.0);
91      }      
92          
# Line 115 | Line 116 | namespace OpenMD {
116      for (sd = seleMan_.beginSelected(i); sd != NULL;
117           sd = seleMan_.nextSelected(i)) {      
118        pos = sd->getPos() - com;
119 <      
119 >
120 >      // r goes from zero upwards
121        r = sqrt(pow(pos.x(), 2) + pow(pos.y(), 2));
122 <      z = pos.z() - solidZ_;
122 >      // z is possibly symmetric around 0
123 >      z = pos.z();
124 >          
125 >      std::size_t whichRBin = int(r / dr);
126 >      std::size_t whichZBin = int( (zLen/2.0 + z) / dz);
127        
128 <      int whichRBin = int(r / dr);
123 <      int whichZBin = int(z/ dz);
124 <      
125 <      if ((r <= len) && (z <= zLen))
128 >      if ((whichRBin < nRBins_) && (whichZBin >= 0) && (whichZBin < nZBins_))
129          histo[whichRBin][whichZBin] += sd->getMass();
130        
131      }
# Line 133 | Line 136 | namespace OpenMD {
136        RealType rU = rL + dr;
137        RealType volSlice = NumericConstant::PI * dz * (( rU*rU ) - ( rL*rL ));
138  
139 <      for (unsigned int j = 0; j < histo[i].size(); ++j){
139 >      for (unsigned int j = 0; j < histo[i].size(); ++j) {
140          histo[i][j] *= PhysicalConstants::densityConvert / volSlice;
141        }
142      }
143  
144 <    for (unsigned int i = 0; i < histo.size(); ++i) {
145 <      RealType ther = dr * (i + 0.5);
146 <      for(unsigned int j = 0; j < histo[i].size(); ++j) {
147 <        if (histo[i][j] <= threshDens_) {
148 <          RealType thez = dz * (j + 0.5);
149 <          cerr << ther << "\t" << thez << "\n";
150 <          break;
144 >    std::vector<Vector<RealType, 2> > points;
145 >    points.clear();
146 >    
147 >    for (unsigned int j = 0; j < nZBins_;  ++j) {
148 >
149 >      // The z coordinates were measured relative to the selection
150 >      // center of mass.  However, we're interested in the elevation
151 >      // above the solid surface.  Also, the binning was done around
152 >      // zero with enough bins to cover the zLength of the box:
153 >      
154 >      RealType thez =  com.z() - solidZ_  - zLen/2.0 + dz * (j + 0.5);
155 >      bool aboveThresh = false;
156 >      bool foundThresh = false;
157 >      int rloc = 0;
158 >      
159 >      for (std::size_t i = 0; i < nRBins_;  ++i) {
160 >
161 >        if (histo[i][j] >= threshDens_) aboveThresh = true;
162 >
163 >        if (aboveThresh && (histo[i][j] <= threshDens_)) {
164 >          rloc = i;
165 >          foundThresh = true;
166 >          aboveThresh = false;
167          }
168 +
169        }
170 +      if (foundThresh) {
171 +        Vector<RealType,2> point;
172 +        point[0] = dr*(rloc+0.5);
173 +        point[1] = thez;
174 +
175 +        if (thez > bufferLength_) {
176 +          points.push_back( point );
177 +        }
178 +      }      
179      }
180  
181 <    // values_.push_back( acos(maxct)*(180.0/M_PI) );
181 >    int numPoints = points.size();
182 >
183 >    // Compute the average of the data points.
184 >    Vector<RealType, 2> average = points[0];
185 >    int i0;
186 >    for (i0 = 1; i0 < numPoints; ++i0) {
187 >      average += points[i0];
188 >    }
189 >    RealType invNumPoints = ((RealType)1)/(RealType)numPoints;
190 >    average *= invNumPoints;
191      
192 +    DynamicRectMatrix<RealType> mat(4, 4);
193 +    int row, col;
194 +    for (row = 0; row < 4; ++row) {
195 +      for (col = 0; col < 4; ++col){
196 +        mat(row,col) = 0.0;        
197 +      }
198 +    }
199 +    for (int i = 0; i < numPoints; ++i) {
200 +      RealType x = points[i][0];
201 +      RealType y = points[i][1];
202 +      RealType x2 = x*x;
203 +      RealType y2 = y*y;
204 +      RealType xy = x*y;
205 +      RealType r2 = x2+y2;
206 +      RealType xr2 = x*r2;
207 +      RealType yr2 = y*r2;
208 +      RealType r4 = r2*r2;
209 +
210 +      mat(0,1) += x;
211 +      mat(0,2) += y;
212 +      mat(0,3) += r2;
213 +      mat(1,1) += x2;
214 +      mat(1,2) += xy;
215 +      mat(1,3) += xr2;
216 +      mat(2,2) += y2;
217 +      mat(2,3) += yr2;
218 +      mat(3,3) += r4;
219 +    }
220 +    mat(0,0) = (RealType)numPoints;
221 +
222 +    for (row = 0; row < 4; ++row) {
223 +      for (col = 0; col < row; ++col) {
224 +        mat(row,col) = mat(col,row);
225 +      }
226 +    }
227 +
228 +    for (row = 0; row < 4; ++row) {
229 +      for (col = 0; col < 4; ++col) {
230 +        mat(row,col) *= invNumPoints;
231 +      }
232 +    }
233 +
234 +    JAMA::Eigenvalue<RealType> eigensystem(mat);
235 +    DynamicRectMatrix<RealType> evects(4, 4);
236 +    DynamicVector<RealType> evals(4);
237 +
238 +    eigensystem.getRealEigenvalues(evals);
239 +    eigensystem.getV(evects);
240 +
241 +    DynamicVector<RealType> evector = evects.getColumn(0);
242 +    RealType inv = ((RealType)1)/evector[3];  // beware zero divide
243 +    RealType coeff[3];
244 +    for (row = 0; row < 3; ++row) {
245 +      coeff[row] = inv*evector[row];
246 +    }
247 +
248 +    Vector<RealType, 2> center;
249 +    
250 +    center[0] = -((RealType)0.5)*coeff[1];
251 +    center[1] = -((RealType)0.5)*coeff[2];
252 +    RealType radius = sqrt(fabs(center[0]*center[0] + center[1]*center[1]
253 +                                - coeff[0]));
254 +
255 +    int i1;
256 +    for (i1 = 0; i1 < 100; ++i1) {
257 +      // Update the iterates.
258 +      Vector<RealType, 2> current = center;
259 +      
260 +      // Compute average L, dL/da, dL/db.
261 +      RealType lenAverage = (RealType)0;
262 +      Vector<RealType, 2> derLenAverage = Vector<RealType, 2>(0.0);
263 +      for (i0 = 0; i0 < numPoints; ++i0) {
264 +        Vector<RealType, 2> diff = points[i0] - center;
265 +        RealType length = diff.length();
266 +        if (length > 1e-6) {
267 +          lenAverage += length;
268 +          RealType invLength = ((RealType)1)/length;
269 +          derLenAverage -= invLength*diff;
270 +        }
271 +      }
272 +      lenAverage *= invNumPoints;
273 +      derLenAverage *= invNumPoints;
274 +
275 +      center = average + lenAverage*derLenAverage;
276 +      radius = lenAverage;
277 +
278 +      Vector<RealType, 2> diff = center - current;
279 +      if (fabs(diff[0]) <= 1e-6 &&  fabs(diff[1]) <= 1e-6) {
280 +        break;
281 +      }
282 +    }
283 +
284 +    RealType zCen = center[1];
285 +    RealType rDrop = radius;
286 +    RealType ca;
287 +
288 +    if (fabs(zCen) > rDrop) {
289 +      ca = 180.0;
290 +    } else {
291 +      ca = 90.0 + asin(zCen/rDrop)*(180.0/M_PI);
292 +    }
293 +
294 +    values_.push_back( ca );
295 +    
296    }  
297   }
298  

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines