00001
00002
00003
00004
00005
00006
00007 #ifndef SCORE_DATATYPES_HEADER
00008 #define SCORE_DATATYPES_HEADER
00009
00010
00011 #include <clipper/clipper.h>
00012 using clipper::Vec3;
00013 using clipper::Mat33;
00014 using clipper::String;
00015 using clipper::Metric_tensor;
00016 typedef clipper::Vec3<double> DVect3;
00017 typedef clipper::Mat33<double> DMat33;
00018 typedef clipper::Vec3<float> FVect3;
00019 typedef clipper::Mat33<float> FMat33;
00020
00021 #include "cmtzlib.h"
00022 #include "csymlib.h"
00023 #include "matvec_utils.hh"
00024 #include "Output.hh"
00025 #include "hkl_datatypes.hh"
00026 #include "globalcontrols.hh"
00027
00028 #include "util.hh"
00029
00030 typedef float Rtype;
00031 typedef double Dtype;
00032
00033 typedef std::pair<int,int> IndexPair;
00034 typedef std::pair<float,float> RPair;
00035 typedef std::pair<double,double> DPair;
00036
00037
00038
00039 namespace scala
00040 {
00041
00042 class LinearFit
00043
00044 {
00045 public:
00046 LinearFit()
00047 : sumw(0.0),sumwx(0.0),sumwy(0.0),sumwxx(0.0),sumwxy(0.0),np(0)
00048 {maxy=0.0;miny=0.0;}
00049
00050
00051 void add(const float& x, const float& y, const float& w);
00052
00053 RPair result() const;
00054
00055 double slope(const float& b=0.0) const;
00056
00057 int Number() const {return np;}
00058
00059 private:
00060 double sumw, sumwx, sumwy, sumwxx, sumwxy;
00061 float miny,maxy;
00062 int np;
00063 };
00064
00065
00066 class IsigNorm {
00067
00068 public:
00069 IsigNorm(){}
00070 IsigNorm(const IsigI& Is, const float& f)
00071 : Isig(Is), fac(f) {}
00072
00073 IsigI Isig;
00074 float fac;
00075 };
00076
00077
00078 class IsPair {
00079 public:
00080
00081
00082 IsPair(){}
00083 IsPair(const IsigI& Is1_in, const float& f1_in,
00084 const IsigI& Is2_in, const float& f2_in)
00085 : IsN1(Is1_in, f1_in), IsN2(Is2_in, f2_in) {}
00086
00087 void Set(const IsigI& Is1_in, const float& f1_in,
00088 const IsigI& Is2_in, const float& f2_in)
00089 {
00090 IsN1.Isig = Is1_in;
00091 IsN1.fac = f1_in;
00092 IsN2.Isig = Is2_in;
00093 IsN2.fac = f2_in;
00094 }
00095
00096 IsigI Is1() const {return IsN1.Isig;}
00097
00098
00099 IsigI Is2() const {return IsN2.Isig;}
00100
00101
00102 IsigI Is1norm() const {return IsN1.Isig.scaleIs(IsN1.fac);}
00103 IsigI Is2norm() const {return IsN2.Isig.scaleIs(IsN2.fac);}
00104
00105 private:
00106 IsigNorm IsN1, IsN2;
00107 };
00108
00109
00110 class IKode
00111 {
00112 public:
00113 IKode() {}
00114 IKode(const IsigI& Is_in, const int& kode_in, const float& invres,
00115 const int& irun_in, const float& time_in)
00116 : Is(Is_in), kode(kode_in), sSqr(invres), irun(irun_in), time(time_in) {}
00117 IKode(const IsigI& Is_in, const int& kode_in, const float& invres)
00118 : Is(Is_in), kode(kode_in), sSqr(invres), irun(-1), time(-1.0) {}
00119
00120
00121 IsigI Is;
00122 float sSqr;
00123 int irun;
00124 float time;
00125 int kode;
00126 };
00127
00128 bool operator < (const IKode& a,const IKode& b);
00129
00130
00131
00132 class ValCount
00133 {
00134 public:
00135 ValCount(const Rtype v, const int n) : val(v), count(n) {}
00136
00137 Rtype val;
00138 int count;
00139 };
00140
00141
00142 class correl_coeff
00143 {
00144 public:
00145
00146 correl_coeff()
00147 : sum_wx(0.0), sum_wx2(0.0), sum_wy(0.0),
00148 sum_wy2(0.0), sum_wxy(0.0), sum_w(0.0),
00149 sum_wwx(0.0), sum_wwy(0.0), sum_w2x(0.0), sum_w2y(0.0),
00150 n(0), nw(0) {};
00151
00152 void zero();
00153
00154
00155 void add(const Rtype& x, const Rtype& y, const Rtype& w=1.0);
00156
00157 void add(const IsigI& x, const IsigI& y, const Rtype& w=1.0);
00158
00159
00160 void add(const Rtype& x, const Rtype& y,
00161 const Rtype& w, const Rtype& wx, const Rtype& wy);
00162
00163 int Number() const;
00164
00165 void SetCC();
00166
00167
00168 ValCount result() const;
00169
00170 void dump() const;
00171
00172 correl_coeff& operator+=(const correl_coeff& other);
00173 friend correl_coeff& operator+ (const correl_coeff& a, const correl_coeff& b);
00174
00175 private:
00176 double sum_wx, sum_wx2, sum_wy, sum_wy2, sum_wxy, sum_w;
00177 double sum_wwx, sum_wwy, sum_w2x, sum_w2y;
00178 int n, nw;
00179 };
00180
00181 class MSdiff
00182
00183
00184
00185
00186 {
00187 public:
00188 MSdiff() : sum_ndf2(0.0), sum_w(0.0), n_f(0) {}
00189
00190 void zero();
00191
00192
00193 void add(const IsigI& Is1, const IsigI& Is2, const double& w=1.0,
00194 const double& VarK=0.0);
00195
00196 ValCount result() const
00197 {
00198 if (n_f == 0)
00199 {return ValCount(0.0,0);}
00200 else
00201 {return ValCount(-sqrt(sum_ndf2/sum_w), n_f);}
00202 }
00203
00204 MSdiff& operator+=(const MSdiff& other);
00205 friend MSdiff& operator+ (const MSdiff& a, const MSdiff& b);
00206
00207 private:
00208 double sum_ndf2;
00209 double sum_w;
00210 int n_f;
00211 };
00212
00213 class Rfactor
00214 {
00215 public:
00216 Rfactor() : sum_df(0.0), sum_f(0.0), n_f(0) {}
00217
00218
00219 void add(const double& df, const double& f, const double& w)
00220 {sum_df += fabs(df) * w; sum_f += f; n_f += 1;}
00221
00222 ValCount result() const {
00223 if (n_f == 0) {
00224 return ValCount(0.0,0);
00225 } else {
00226 return ValCount(sum_df/sum_f, n_f);
00227 }
00228 }
00229 Rtype R() const {
00230 if (n_f == 0) {
00231 return 0.0;
00232 } else {
00233 return sum_df/sum_f;
00234 }
00235 }
00236
00237
00238 void SetR() {n_f=0;}
00239
00240 Rfactor& operator+=(const Rfactor& other);
00241 friend Rfactor& operator+ (const Rfactor& a, const Rfactor& b);
00242
00243 private:
00244 double sum_df, sum_f;
00245 int n_f;
00246 };
00247
00248
00249 class PairSet
00250 {
00251 public:
00252 PairSet() {};
00253 PairSet(const int& I1, const int& I2, const int& SymElement);
00254 PairSet(const int& I1, const int& I2, const int& SymElement,
00255 const double& wt, const float& fac1, const float& fac2);
00256
00257
00258
00259
00260
00261
00262 bool AddPair(const int& I1, const int& I2, const int& SymElement,
00263 const float& fac1, const float& fac2);
00264
00265
00266 int SymElement() const {return SymElmt;}
00267 std::vector<IndexPair> Pairs() const {return pairs;}
00268 std::vector<RPair> Fac() const {return fac12;}
00269 std::vector<int> ObsIndices() const {return obsindices;}
00270 double Weight() const {return weight;}
00271 int Nobs() const {return obsindices.size();}
00272
00273 void dump()
00274 {
00275 std::cout << "PairSet: \n"
00276 << " SymEl = " << SymElmt << "\n"
00277 << " Npair = " << pairs.size() << "\n";
00278 for (int i=0;i<pairs.size();i++) std::cout << pairs[i].first << " - "
00279 << pairs[i].second << "\n";
00280 std::cout << " Nobs = " << obsindices.size() << "\n";
00281 for (int i=0;i<obsindices.size();i++) std::cout << obsindices[i] << "\n";
00282
00283 }
00284
00285
00286 private:
00287 std::vector<IndexPair> pairs;
00288 std::vector<RPair> fac12;
00289 std::vector<int> obsindices;
00290 double weight;
00291 int SymElmt;
00292 };
00293
00294 template<class T> RPair Mean(const std::vector<T>& list)
00295
00296 {
00297 int n = list.size();
00298 double sum_sc = 0.0;
00299 double sum_sc2 = 0.0;
00300 double sc;
00301 double sd;
00302 int count = 0;
00303
00304 if (n<1) return RPair(0.0,0.0);
00305 if (n==1)
00306 {
00307 sc = list[0];
00308 sd = 0.0;
00309 }
00310 else
00311 {
00312 for (int i = 0;i<n;i++)
00313 {
00314 sc = list[i];
00315 sum_sc += sc;
00316 sum_sc2 += sc*sc;
00317 }
00318
00319 sc = sum_sc/double(n);
00320
00321 sd = sqrt((sum_sc2 - double(n)*sc*sc)/double(n-1));
00322 }
00323
00324 return RPair(sc, sd);
00325 }
00326
00327 template<class T> class Array3D
00328
00329 {
00330 public:
00331
00332 inline Array3D() : nu(0),nv(0),nw(0),u0(0),v0(0),w0(0), offset(false) {data.clear();}
00333
00334 inline Array3D(const int& iu, const int& iv, const int& iw)
00335 : nu(iu),nv(iv),nw(iw), u0(0),v0(0),w0(0), offset(false)
00336 {data.resize(iu*iv*iw);}
00337
00338 inline Array3D(const int& iu, const int& iv, const int& iw,
00339 const int& ku, const int& kv, const int& kw)
00340 : nu(iu),nv(iv),nw(iw), u0(ku),v0(kv),w0(kw)
00341 {data.resize(iu*iv*iw);
00342 offset=false;if(ku!=0||kv!=0||kw!=0)offset = true;}
00343
00344 void Offset(const int& ku, const int& kv, const int& kw)
00345
00346 {u0=ku; v0=kv; w0=kw; offset=false;if(ku!=0||kv!=0||kw!=0)offset = true;}
00347
00348
00349 inline void init(const int& iu, const int& iv, const int& iw, const T& val)
00350 {
00351 nu=iu; nv=iv; nw=iw; u0=0; v0=0; w0=0;
00352 offset = false;
00353 data.assign(iu*iv*iw, val);
00354 }
00355
00356 inline const T& operator ()(const int& i, const int& j, const int& k) const
00357 {
00358 if (offset) return data[i-u0+nu*(j-v0+nv*(k-w0))];
00359 return data[i+nu*(j+nv*k)]; }
00360
00361 inline T& operator ()(const int& i, const int& j, const int& k)
00362 {
00363 if (offset) return data[i-u0+nu*(j-v0+nv*(k-w0))];
00364 return data[i+nu*(j+nv*k)]; }
00365
00366 inline const T& operator ()(clipper::Vec3<int> v) const
00367 {
00368 if (offset) return data[v[0]-u0+nu*(v[1]-v0+nv*(v[2]-w0))];
00369 return data[v[0]+nu*(v[1]+nv*v[2])]; }
00370
00371 inline T& operator ()(clipper::Vec3<int> v)
00372 {
00373 if (offset) return data[v[0]-u0+nu*(v[1]-v0+nv*(v[2]-w0))];
00374 return data[v[0]+nu*(v[1]+nv*v[2])]; }
00375
00376
00377 inline const T& operator ()(const int& i) const
00378 {return data[i];}
00379
00380 inline T& operator ()(const int& i)
00381 {return data[i];}
00382
00383 void Zero() {data.assign(nu*nv*nw, T(0));}
00384
00385 int size() const {return nu*nv*nw;}
00386
00387 bool Test(const int& i, const int& j, const int& k)
00388
00389 {
00390 if (i<u0 || i-u0>=nu || j<v0 || j-v0>=nv || k<w0 || k-w0>=nw) return false;
00391 return true;
00392 }
00393 bool Test(const clipper::Vec3<int> v)
00394
00395 {
00396 if (v[0]<u0 || v[0]-u0>=nu || v[1]<v0 || v[1]-v0>=nv ||
00397 v[2]<w0 || v[2]-w0>=nw) return false;
00398 return true;
00399 }
00400
00401 private:
00402 int nu,nv,nw;
00403 int u0,v0,w0;
00404 bool offset;
00405 std::vector<T> data;
00406 };
00407
00408 class ValidElementObserved
00409
00410
00411
00412 {
00413 public:
00414 ValidElementObserved()
00415 : numelementLattice(0),
00416 numelementpresent(0),
00417 numoriglgelement(0),
00418 numoriglgelementpresent(0){}
00419
00420
00421
00422
00423
00424
00425 ValidElementObserved(const int& NumElementLattice,
00426 const int& NumElementPresent,
00427 const int& NumOrigLGElement,
00428 const int& NumOrigLGElementPresent)
00429 : numelementLattice(NumElementLattice),
00430 numelementpresent(NumElementPresent),
00431 numoriglgelement(NumOrigLGElement),
00432 numoriglgelementpresent(NumOrigLGElementPresent){}
00433
00434 double ObservedInLatticeGroup() const
00435 {
00436 if (numelementLattice-1 <= 0) return 1.0;
00437 return (double(numelementpresent)/double(numelementLattice-1));
00438 }
00439
00440 double ObservedInOrigGroup() const
00441 {
00442 if (numoriglgelement <= 0) return 1.0;
00443 return (double(numoriglgelementpresent)/double(numoriglgelement));
00444 }
00445
00446 bool Valid(const double ThresholdLattice = 0.49,
00447 const double ThresholdOrig = 0.49) const
00448
00449
00450 {
00451 if (numelementLattice == 0) return false;
00453
00454
00455
00457 return ((ObservedInLatticeGroup() >= ThresholdLattice) &&
00458 (ObservedInOrigGroup() >= ThresholdOrig));
00459 }
00460
00461 private:
00462
00463 int numelementLattice;
00464
00465 int numelementpresent;
00466 int numoriglgelement;
00467 int numoriglgelementpresent;
00468
00469 };
00470
00471 class PossibleSpaceGroup
00472 {
00473 public:
00474 PossibleSpaceGroup(){}
00475
00476
00477
00478
00479
00480
00481
00482
00483
00484
00485
00486 PossibleSpaceGroup(const std::string& Sname, const int& number,
00487 const std::string& Rname,
00488 const std::string& Scond, const std::string& ScondLG,
00489 const ReindexOp& Sreindex, const Chirality& Schiral,
00490 const double& probin, const std::vector<int>& ZonesinGroup);
00491
00492 void StoreAcenProb(const double& probacen);
00493 void StoreLaueGroupName(const std::string& LGname) {lauegroupname = LGname;;}
00494 void StorePointGroupName(const std::string& PGname) {pointgroupname = PGname;;}
00495 void StoreLaueGroupProb(const double& problg, const double& confidence);
00496 void StoreSGname(const std::string& SGname) {name = SGname;}
00497 void Accept() {accepted=true;}
00498 void StoreOrigReindex(const ReindexOp& LGreindex) {reindex_orig = LGreindex;}
00499 void StoreReindex(const ReindexOp& Reindex) {reindex = Reindex;}
00500
00501 std::vector<int> ZoneList() const {return zonesingroup;}
00502
00503
00504
00505 std::string Name(const bool& nameSG=true) const;
00506 std::string Refname() const {return refname;};
00507 int Sgnumber() const {return sgnumber;}
00508
00509
00510
00511 std::string Condition(const bool& nameSG=true) const;
00512
00513 std::string ConditionLG() const {return conditionLG;}
00514
00515
00516 ReindexOp Reindex(const bool& Orig=false) const;
00517
00518 ReindexOp ReindexOrig() const {return reindex_orig;}
00519 Chirality Chiral() const {return chiral;}
00520
00521 double Prob() const {return prob;}
00522
00523 double SysAbsProb() const {return sysabsprob;}
00524
00525 double CentroProb() const {return centroprob;}
00526
00527 double LaueGroupProb() const {return lauegroupprob;}
00528
00529 double LaueGroupConfidence() const {return lauegroupconfidence;}
00530 std::string LaueGroupName() const {return lauegroupname;}
00531 std::string PointGroupName() const {return pointgroupname;}
00532
00533 bool Accepted() const {return accepted;}
00534
00535 bool IsPIForthorhombic() const;
00536
00537 bool IsI2() const;
00538
00539
00540
00541
00542
00543 bool ReindexRhombohedral(const bool& OrigIsR);
00544
00545 private:
00546 std::string name;
00547 std::string refname;
00548 int sgnumber;
00549 std::string lauegroupname;
00550 std::string pointgroupname;
00551 std::string condition;
00552
00553 std::string conditionLG;
00554
00555 ReindexOp reindex;
00556
00557 ReindexOp reindex_orig;
00558 Chirality chiral;
00559 double prob;
00560 double lauegroupprob;
00561 double lauegroupconfidence;
00562 double sysabsprob;
00563 double centroprob;
00564 bool accepted;
00565 std::vector<int> zonesingroup;
00566
00567
00568 friend bool operator == (const PossibleSpaceGroup& a,
00569 const PossibleSpaceGroup& b)
00570 {return a.name == b.name;}
00571 friend bool operator != (const PossibleSpaceGroup& a,
00572 const PossibleSpaceGroup& b)
00573 {return a.name != b.name;}
00574
00575 friend bool operator < (const PossibleSpaceGroup& a,
00576 const PossibleSpaceGroup& b)
00577 {return a.prob > b.prob;}
00578 };
00579
00580
00581
00582 void ResetRhombohedralHexLatticestoR
00583 (std::vector<scala::PossibleSpaceGroup>& groups);
00584 }
00585 #endif
00586
00587