FOX/ObjCryst++  2022
LSQNumObj.h
1 /* ObjCryst++ Object-Oriented Crystallographic Library
2  (c) 2000-2002 Vincent Favre-Nicolin vincefn@users.sourceforge.net
3  2000-2001 University of Geneva (Switzerland)
4 
5  This program is free software; you can redistribute it and/or modify
6  it under the terms of the GNU General Public License as published by
7  the Free Software Foundation; either version 2 of the License, or
8  (at your option) any later version.
9 
10  This program is distributed in the hope that it will be useful,
11  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  GNU General Public License for more details.
14 
15  You should have received a copy of the GNU General Public License
16  along with this program; if not, write to the Free Software
17  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18 */
19 /* LSQNumObj.h
20 * header file for Least-Squares refinement witn Numerical derivatives Objects
21 *
22 */
23 #ifndef _LSQOBJNUM_H
24 #define _LSQOBJNUM_H
25 
26 #include "ObjCryst/CrystVector/CrystVector.h"
27 #include "ObjCryst/RefinableObj/RefinableObj.h"
28 #include <string>
29 #include <map>
30 #include <list>
31 
32 namespace ObjCryst
33 {
38 class LSQNumObj
39 {
40  public:
41  LSQNumObj(std::string objName="Unnamed LSQ object");
42  ~LSQNumObj();
46  void SetParIsFixed(const std::string& parName,const bool fix);
50  void SetParIsFixed(const RefParType *type,const bool fix);
59  void SetParIsFixed(RefinablePar &par,const bool fix);
67  void SetParIsFixed(RefinableObj &obj,const bool fix);
71  void UnFixAllPar();
75  void SetParIsUsed(const std::string& parName,const bool use);
79  void SetParIsUsed(const RefParType *type,const bool use);
96  void Refine (int nbCycle=1,bool useLevenbergMarquardt=false,
97  const bool silent=false, const bool callBeginEndOptimization=true,
98  const float minChi2var=0.01);
111  bool SafeRefine(std::list<RefinablePar*> vnewpar, std::list<const RefParType*> vnewpartype,
112  REAL maxChi2factor=1.01,
113  int nbCycle=1, bool useLevenbergMarquardt=false,
114  const bool silent=false, const bool callBeginEndOptimization=true,
115  const float minChi2var=0.01);
116  CrystVector_REAL Sigma()const;
117  CrystMatrix_REAL CorrelMatrix()const;
118  void CalcRfactor()const;
119  REAL Rfactor()const;
120  void CalcRwFactor()const;
121  REAL RwFactor()const;
122  void CalcChiSquare() const;
123  REAL ChiSquare()const; //uses the weight if specified
136  void SetRefinedObj(RefinableObj &obj, const unsigned int LSQFuncIndex=0, const bool init=true, const bool recursive=false);
137  // Access to the full list of refined objects. The list is initially built
138  // recursively from one object. This function allows to modify the list
139  // of sub-objects before refinement (such as for removing certain types
140  // of objects).
141  //ObjRegistry<RefinableObj> &GetRefinedObjList();
146  const std::map<RefinableObj*,unsigned int>& GetRefinedObjMap() const;
151  std::map<RefinableObj*,unsigned int>& GetRefinedObjMap();
169  const RefinableObj& GetCompiledRefinedObj()const;
170  void SetUseSaveFileOnEachCycle(bool yesOrNo=true);
171  void SetSaveFile(std::string fileName="refine.save");
172  void PrintRefResults()const;
173  void SetDampingFactor(const REAL newDampFact);
174  void PurgeSaveFile();
175  void WriteReportToFile()const;
176 
177  void OptimizeDerivativeSteps();
178  const std::map<pair<const RefinablePar*,const RefinablePar*>,REAL > &GetVarianceCovarianceMap()const;
193  void PrepareRefParList(const bool copy_param=false);
194 
196  const CrystVector_REAL& GetLSQCalc() const;
198  const CrystVector_REAL& GetLSQObs() const;
200  const CrystVector_REAL& GetLSQWeight() const;
202  const CrystVector_REAL& GetLSQDeriv(RefinablePar&par);
203  const std::map<RefinablePar*,CrystVector_REAL>& GetLSQ_FullDeriv();
206  void BeginOptimization(const bool allowApproximations=false, const bool enableRestraints=false);
209  void EndOptimization();
210  protected:
211  private:
212  // Refined object
229  std::string mName;
231  std::string mSaveFileName;
232  mutable REAL mR,mRw,mChiSq;
234  CrystMatrix_REAL mCorrelMatrix;
236  std::map<pair<const RefinablePar*,const RefinablePar*>,REAL > mvVarCovar;
238  CrystVector_REAL mObs;
240  CrystVector_REAL mWeight;
243  int mIndexValuesSetInitial, mIndexValuesSetLast;
246  // The optimized object
247  //RefinableObj *mpRefinedObj;
248  // The index of the LSQ function in the refined object (if there are several...)
249  //unsigned int mLSQFuncIndex;
250 
256  std::map<RefinableObj*,unsigned int> mvRefinedObjMap;
258  mutable std::map<RefinableObj*,unsigned int> mvRefinedObjLSQSize;
259 
266  mutable CrystVector_REAL mLSQObs,mLSQCalc,mLSQWeight,mLSQDeriv;
267  mutable std::map<RefinablePar*,CrystVector_REAL> mLSQ_FullDeriv;
268 #ifdef __WX__CRYST__
269  public:
270  virtual WXCrystObjBasic* WXCreate(wxWindow* parent);
271  WXCrystObjBasic* WXGet();
272  void WXDelete();
273  void WXNotifyDelete();
274  protected:
275  WXCrystObjBasic *mpWXCrystObj;
276 #endif
277 };
278 
279 }//namespace
280 #endif //_LSQOBJNUM_H
The namespace which includes all objects (crystallographic and algorithmic) in ObjCryst++.
Definition: doc-main.h:25
(Quick & dirty) Least-Squares Refinement Object with Numerical derivatives
Definition: LSQNumObj.h:39
CrystVector_REAL mLSQObs
Temporary arrays for LSQ functions evaluation - used when using recursive LSQ function.
Definition: LSQNumObj.h:266
bool mSaveReportOnEachCycle
Save result to file after each cycle ?
Definition: LSQNumObj.h:227
void SetParIsFixed(const std::string &parName, const bool fix)
Fix one parameter.
bool mCopyRefPar
If true, then parameters to be refined will be copied instead of referenced.
Definition: LSQNumObj.h:263
const CrystVector_REAL & GetLSQCalc() const
Get the LSQ calc vector (using either only the top or the hierarchy of object)
Definition: LSQNumObj.cpp:927
ObjRegistry< RefinableObj > mRecursiveRefinedObjList
The recursive list of all refined sub-objects.
Definition: LSQNumObj.h:214
void EndOptimization()
Tell all refined object that the refinement is finished.
Definition: LSQNumObj.cpp:1046
bool SafeRefine(std::list< RefinablePar * > vnewpar, std::list< const RefParType * > vnewpartype, REAL maxChi2factor=1.01, int nbCycle=1, bool useLevenbergMarquardt=false, const bool silent=false, const bool callBeginEndOptimization=true, const float minChi2var=0.01)
Run a refinement in a 'safe' way: if the Chi2 value increases by more that a given factor the paramet...
Definition: LSQNumObj.cpp:706
void BeginOptimization(const bool allowApproximations=false, const bool enableRestraints=false)
Tell all refined object that the refinement is beginning.
Definition: LSQNumObj.cpp:1040
const std::map< RefinableObj *, unsigned int > & GetRefinedObjMap() const
Get the map of refined objects - this is a recursive list of all the objects that are taken into acco...
Definition: LSQNumObj.cpp:837
std::map< pair< const RefinablePar *, const RefinablePar * >, REAL > mvVarCovar
Variance-Covariance matrix, as a std::map.
Definition: LSQNumObj.h:236
std::string mName
Name of the refined object.
Definition: LSQNumObj.h:229
RefinableObj mRefParList
The refinable par list used during refinement.
Definition: LSQNumObj.h:223
CrystMatrix_REAL mCorrelMatrix
Correlation matrix between all refined parameters.
Definition: LSQNumObj.h:234
const CrystVector_REAL & GetLSQWeight() const
Get the LSQ weight vector (using either only the top or the hierarchy of object)
Definition: LSQNumObj.cpp:966
const CrystVector_REAL & GetLSQObs() const
Get the LSQ obs vector (using either only the top or the hierarchy of object)
Definition: LSQNumObj.cpp:946
bool mStopAfterCycle
If true, then stop at the end of the cycle. Used in multi-threading environment.
Definition: LSQNumObj.h:245
void Refine(int nbCycle=1, bool useLevenbergMarquardt=false, const bool silent=false, const bool callBeginEndOptimization=true, const float minChi2var=0.01)
Do the refinement.
Definition: LSQNumObj.cpp:106
CrystVector_REAL mObs
Observed values.
Definition: LSQNumObj.h:238
void SetParIsUsed(const std::string &parName, const bool use)
Set a parameter to be used.
std::map< RefinableObj *, unsigned int > mvRefinedObjLSQSize
Size of each object LSQ data. This is initialized in LSQNumObj::GetLSQObs()
Definition: LSQNumObj.h:258
REAL mDampingFactor
Damping factor for the refinement (unused yet...)
Definition: LSQNumObj.h:225
std::string mSaveFileName
File name where refinement info is saved.
Definition: LSQNumObj.h:231
void SetRefinedObj(RefinableObj &obj, const unsigned int LSQFuncIndex=0, const bool init=true, const bool recursive=false)
Choose the object to refine.
Definition: LSQNumObj.cpp:824
RefinableObj & GetCompiledRefinedObj()
Access to the RefinableObj which is the compilation of all parameters from the object supplied for op...
Definition: LSQNumObj.cpp:848
const CrystVector_REAL & GetLSQDeriv(RefinablePar &par)
Get the LSQ deriv vector (using either only the top or the hierarchy of object)
Definition: LSQNumObj.cpp:985
void UnFixAllPar()
UnFix All parameters.
Definition: LSQNumObj.cpp:89
std::map< RefinableObj *, unsigned int > mvRefinedObjMap
Map of the recursive list of the objects to be refined.
Definition: LSQNumObj.h:256
CrystVector_REAL mWeight
Weight corresponding to all observed values.
Definition: LSQNumObj.h:240
int mIndexValuesSetInitial
Index of the set of saved values for all refinable parameters, before refinement and before the last ...
Definition: LSQNumObj.h:243
void PrepareRefParList(const bool copy_param=false)
Prepare the full parameter list for the refinement.
Definition: LSQNumObj.cpp:913
class of refinable parameter types.
Definition: RefinableObj.h:80
Generic class for parameters of refinable objects.
Definition: RefinableObj.h:225
Object Registry.
Definition: RefinableObj.h:645
Generic Refinable Object.
Definition: RefinableObj.h:784
Abstract base class for all objects in wxCryst.
Definition: wxCryst.h:128