source: ThirdParty/mpqc_open/src/lib/math/optimize/conv.cc@ 47b463

Action_Thermostats Add_AtomRandomPerturbation Add_RotateAroundBondAction Add_SelectAtomByNameAction Adding_Graph_to_ChangeBondActions Adding_MD_integration_tests Adding_StructOpt_integration_tests AutomationFragmentation_failures Candidate_v1.6.0 Candidate_v1.6.1 ChangeBugEmailaddress ChangingTestPorts ChemicalSpaceEvaluator Combining_Subpackages Debian_Package_split Debian_package_split_molecuildergui_only Disabling_MemDebug Docu_Python_wait EmpiricalPotential_contain_HomologyGraph_documentation Enable_parallel_make_install Enhance_userguide Enhanced_StructuralOptimization Enhanced_StructuralOptimization_continued Example_ManyWaysToTranslateAtom Exclude_Hydrogens_annealWithBondGraph FitPartialCharges_GlobalError Fix_ChronosMutex Fix_StatusMsg Fix_StepWorldTime_single_argument Fix_Verbose_Codepatterns ForceAnnealing_goodresults ForceAnnealing_oldresults ForceAnnealing_tocheck ForceAnnealing_with_BondGraph ForceAnnealing_with_BondGraph_continued ForceAnnealing_with_BondGraph_continued_betteresults ForceAnnealing_with_BondGraph_contraction-expansion GeometryObjects Gui_displays_atomic_force_velocity IndependentFragmentGrids_IntegrationTest JobMarket_RobustOnKillsSegFaults JobMarket_StableWorkerPool JobMarket_unresolvable_hostname_fix ODR_violation_mpqc_open PartialCharges_OrthogonalSummation PythonUI_with_named_parameters QtGui_reactivate_TimeChanged_changes Recreated_GuiChecks RotateToPrincipalAxisSystem_UndoRedo StoppableMakroAction Subpackage_levmar Subpackage_vmg ThirdParty_MPQC_rebuilt_buildsystem TremoloParser_IncreasedPrecision TremoloParser_MultipleTimesteps Ubuntu_1604_changes stable
Last change on this file since 47b463 was 860145, checked in by Frederik Heber <heber@…>, 8 years ago

Merge commit '0b990dfaa8c6007a996d030163a25f7f5fc8a7e7' as 'ThirdParty/mpqc_open'

  • Property mode set to 100644
File size: 5.2 KB
Line 
1//
2// conv.cc
3//
4// Copyright (C) 1996 Limit Point Systems, Inc.
5//
6// Author: Curtis Janssen <cljanss@limitpt.com>
7// Maintainer: LPS
8//
9// This file is part of the SC Toolkit.
10//
11// The SC Toolkit is free software; you can redistribute it and/or modify
12// it under the terms of the GNU Library General Public License as published by
13// the Free Software Foundation; either version 2, or (at your option)
14// any later version.
15//
16// The SC Toolkit is distributed in the hope that it will be useful,
17// but WITHOUT ANY WARRANTY; without even the implied warranty of
18// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19// GNU Library General Public License for more details.
20//
21// You should have received a copy of the GNU Library General Public License
22// along with the SC Toolkit; see the file COPYING.LIB. If not, write to
23// the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
24//
25// The U.S. Government is granted a limited license as per AL 91-7.
26//
27
28#ifdef __GNUC__
29#pragma implementation
30#endif
31
32#include <math.h>
33
34#include <util/misc/formio.h>
35#include <util/keyval/keyval.h>
36#include <util/state/stateio.h>
37#include <math/optimize/conv.h>
38
39using namespace std;
40using namespace sc;
41
42/////////////////////////////////////////////////////////////////////////
43// Convergence
44
45static ClassDesc Convergence_cd(
46 typeid(Convergence),"Convergence",1,"virtual public SavableState",
47 0, create<Convergence>, create<Convergence>);
48
49Convergence::Convergence()
50{
51 set_defaults();
52}
53
54Convergence::Convergence(double tolerance)
55{
56 set_defaults();
57 max_disp_ = tolerance;
58 max_grad_ = tolerance;
59 rms_disp_ = tolerance;
60 rms_grad_ = tolerance;
61 graddisp_ = tolerance;
62}
63
64Convergence::Convergence(StateIn&s):
65 SavableState(s)
66{
67 s.get(use_max_disp_);
68 s.get(use_max_grad_);
69 s.get(use_rms_disp_);
70 s.get(use_rms_grad_);
71 s.get(use_graddisp_);
72 s.get(max_disp_);
73 s.get(max_grad_);
74 s.get(rms_disp_);
75 s.get(rms_grad_);
76 s.get(graddisp_);
77}
78
79Convergence::Convergence(const Ref<KeyVal>&keyval)
80{
81 use_max_disp_ = keyval->exists("max_disp");
82 use_max_grad_ = keyval->exists("max_grad");
83 use_rms_disp_ = keyval->exists("rms_disp");
84 use_rms_grad_ = keyval->exists("rms_grad");
85 use_graddisp_ = keyval->exists("graddisp");
86 if (use_max_disp_) max_disp_ = keyval->doublevalue("max_disp");
87 if (use_max_grad_) max_grad_ = keyval->doublevalue("max_grad");
88 if (use_rms_disp_) rms_disp_ = keyval->doublevalue("rms_disp");
89 if (use_rms_grad_) rms_grad_ = keyval->doublevalue("rms_grad");
90 if (use_graddisp_) graddisp_ = keyval->doublevalue("graddisp");
91
92 if (!use_max_disp_ && !use_max_grad_
93 && !use_rms_disp_ && !use_rms_grad_
94 && !use_graddisp_) {
95 set_defaults();
96 }
97}
98
99Convergence::~Convergence()
100{
101}
102
103void
104Convergence::save_data_state(StateOut&s)
105{
106 s.put(use_max_disp_);
107 s.put(use_max_grad_);
108 s.put(use_rms_disp_);
109 s.put(use_rms_grad_);
110 s.put(use_graddisp_);
111 s.put(max_disp_);
112 s.put(max_grad_);
113 s.put(rms_disp_);
114 s.put(rms_grad_);
115 s.put(graddisp_);
116}
117
118void
119Convergence::set_defaults()
120{
121 use_max_disp_ = 0;
122 use_max_grad_ = 1;
123 use_rms_disp_ = 0;
124 use_rms_grad_ = 1;
125 use_graddisp_ = 0;
126 max_grad_ = 4.0e-6;
127 rms_grad_ = 1.0e-6;
128}
129
130void
131Convergence::get_x(const Ref<Function> &f)
132{
133 x_ = f->get_x();
134}
135
136void
137Convergence::set_nextx(const RefSCVector &x)
138{
139 nextx_ = x->copy();
140}
141
142void
143Convergence::get_grad(const Ref<Function> &f)
144{
145 grad_ = f->gradient();
146}
147
148int
149Convergence::converged()
150{
151 int fail = 0;
152 int pass = 0;
153
154 RefSCVector disp;
155 if (x_.nonnull() && nextx_.nonnull()) disp = nextx_ - x_;
156
157 ExEnv::out0() << endl;
158
159 if (use_max_grad_ && grad_.nonnull()) {
160 check_conv("Max Gradient ", grad_.maxabs(), max_grad_, pass, fail);
161 }
162 if (use_rms_grad_ && grad_.nonnull()) {
163 check_conv("RMS Gradient ",
164 sqrt(grad_.scalar_product(grad_)/grad_.n()),
165 rms_grad_, pass, fail);
166 }
167 if (use_max_disp_ && disp.nonnull()) {
168 check_conv("Max Displacement ", disp.maxabs(), max_disp_, pass, fail);
169 }
170 if (use_rms_disp_ && disp.nonnull()) {
171 check_conv("RMS Displacement ",
172 sqrt(disp.scalar_product(disp)/disp.n()),
173 rms_disp_, pass, fail);
174 }
175 if (use_graddisp_ && disp.nonnull() && grad_.nonnull()) {
176 check_conv("Gradient*Displace", fabs(disp.scalar_product(grad_)),
177 graddisp_, pass, fail);
178 }
179 if (fail + pass == 0) {
180 ExEnv::errn() << "ERROR: Convergence::converged: no applicable convergence tests"
181 << endl;
182 abort();
183 }
184 if (!fail) {
185 ExEnv::out0() << endl
186 << indent << "All convergence criteria have been met."
187 << endl;
188 }
189 return !fail;
190}
191
192void
193Convergence::check_conv(const char *heading,
194 double val, double bound,
195 int &pass, int &fail)
196{
197 int converged = val <= bound;
198 ExEnv::out0() << indent << heading << ": "
199 << scprintf("%14.10f ", val)
200 << scprintf("%14.10f ", bound)
201 << (converged?"yes":"no")
202 << endl;
203 if (converged) pass++;
204 else fail++;
205}
206
207void
208Convergence::reset()
209{
210 grad_ = 0;
211 x_ = 0;
212 nextx_ = 0;
213}
214
215/////////////////////////////////////////////////////////////////////////////
216
217// Local Variables:
218// mode: c++
219// c-file-style: "CLJ"
220// End:
Note: See TracBrowser for help on using the repository browser.