source: ThirdParty/mpqc_open/src/lib/math/optimize/function.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: 8.2 KB
Line 
1//
2// function.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#include <float.h>
34#include <string.h>
35
36#include <util/misc/formio.h>
37#include <util/state/stateio.h>
38#include <math/scmat/matrix.h>
39#include <math/scmat/elemop.h>
40#include <util/keyval/keyval.h>
41
42#include <math/optimize/function.h>
43
44using namespace std;
45using namespace sc;
46
47////////////////////////////////////////////////////////////////////////
48
49static ClassDesc Function_cd(
50 typeid(Function),"Function",1,"virtual public SavableState",
51 0, 0, 0);
52
53Function::Function():
54 value_(this),
55 gradient_(this),
56 hessian_(this)
57{
58 matrixkit_ = SCMatrixKit::default_matrixkit();
59 value_.set_desired_accuracy(DBL_EPSILON);
60 gradient_.set_desired_accuracy(DBL_EPSILON);
61 hessian_.set_desired_accuracy(DBL_EPSILON);
62}
63
64Function::Function(const Function& func):
65 value_(func.value_,this),
66 gradient_(func.gradient_,this),
67 hessian_(func.hessian_,this)
68{
69 matrixkit_ = func.matrixkit_;
70 dim_ = func.dim_;
71 x_ = func.x_;
72}
73
74Function::Function(const Ref<KeyVal>&kv, double funcacc,
75 double gradacc, double hessacc):
76 value_(this),
77 gradient_(this),
78 hessian_(this)
79{
80 matrixkit_ << kv->describedclassvalue("matrixkit");
81
82 if (matrixkit_.null()) matrixkit_ = SCMatrixKit::default_matrixkit();
83
84 KeyValValuedouble funcaccval(funcacc);
85 value_.set_desired_accuracy(kv->doublevalue("value_accuracy",funcaccval));
86 if (value_.desired_accuracy() < DBL_EPSILON)
87 value_.set_desired_accuracy(DBL_EPSILON);
88
89 KeyValValuedouble gradaccval(gradacc);
90 gradient_.set_desired_accuracy(kv->doublevalue("gradient_accuracy",
91 gradaccval));
92 if (gradient_.desired_accuracy() < DBL_EPSILON)
93 gradient_.set_desired_accuracy(DBL_EPSILON);
94
95 KeyValValuedouble hessaccval(hessacc);
96 hessian_.set_desired_accuracy(kv->doublevalue("hessian_accuracy",
97 hessaccval));
98 if (hessian_.desired_accuracy() < DBL_EPSILON)
99 hessian_.set_desired_accuracy(DBL_EPSILON);
100}
101
102Function::Function(StateIn&s):
103 SavableState(s),
104 value_(s,this),
105 gradient_(this),
106 hessian_(this)
107{
108 matrixkit_ = SCMatrixKit::default_matrixkit();
109 dim_ << SavableState::restore_state(s);
110 x_ = matrixkit_->vector(dim_);
111 x_.restore(s);
112
113 gradient_.result_noupdate() = matrixkit()->vector(dim_);
114 gradient_.restore_state(s);
115 gradient_.result_noupdate().restore(s);
116
117 hessian_.result_noupdate() = matrixkit()->symmmatrix(dim_);
118 hessian_.restore_state(s);
119 hessian_.result_noupdate().restore(s);
120}
121
122Function::~Function()
123{
124}
125
126Function &
127Function::operator=(const Function& func)
128{
129 matrixkit_ = func.matrixkit_;
130 dim_ = func.dim_;
131 x_ = func.x_;
132 value_ = func.value_;
133 gradient_ = func.gradient_;
134 hessian_ = func.hessian_;
135 return *this;
136}
137
138void
139Function::save_data_state(StateOut&s)
140{
141 value_.save_data_state(s);
142 SavableState::save_state(dim_.pointer(),s);
143 x_.save(s);
144 gradient_.save_data_state(s);
145 gradient_.result_noupdate().save(s);
146 hessian_.save_data_state(s);
147 hessian_.result_noupdate().save(s);
148}
149
150Ref<SCMatrixKit>
151Function::matrixkit() const
152{
153 return matrixkit_;
154}
155
156RefSCDimension
157Function::dimension() const
158{
159 return dim_;
160}
161
162void
163Function::set_x(const RefSCVector&v)
164{
165 x_.assign(v);
166 obsolete();
167}
168
169double
170Function::value()
171{
172 return value_;
173}
174
175int
176Function::value_needed() const
177{
178 return value_.needed();
179}
180
181int
182Function::do_value(int f)
183{
184 return value_.compute(f);
185}
186
187void
188Function::set_value(double e)
189{
190 value_.result_noupdate() = e;
191 value_.computed() = 1;
192}
193
194void
195Function::set_desired_value_accuracy(double a)
196{
197 value_.set_desired_accuracy(a);
198}
199
200void
201Function::set_actual_value_accuracy(double a)
202{
203 value_.set_actual_accuracy(a);
204}
205
206double
207Function::desired_value_accuracy() const
208{
209 return value_.desired_accuracy();
210}
211
212double
213Function::actual_value_accuracy() const
214{
215 return value_.actual_accuracy();
216}
217
218RefSCVector
219Function::gradient()
220{
221 RefSCVector ret = gradient_.result();
222 return ret;
223}
224
225int
226Function::gradient_needed() const
227{
228 return gradient_.needed();
229}
230
231int
232Function::do_gradient(int f)
233{
234 return gradient_.compute(f);
235}
236
237void
238Function::set_gradient(RefSCVector&g)
239{
240 gradient_.result_noupdate() = g;
241 gradient_.computed() = 1;
242}
243
244void
245Function::set_desired_gradient_accuracy(double a)
246{
247 gradient_.set_desired_accuracy(a);
248}
249
250void
251Function::set_actual_gradient_accuracy(double a)
252{
253 gradient_.set_actual_accuracy(a);
254}
255
256double
257Function::actual_gradient_accuracy() const
258{
259 return gradient_.actual_accuracy();
260}
261
262double
263Function::desired_gradient_accuracy() const
264{
265 return gradient_.desired_accuracy();
266}
267
268RefSymmSCMatrix
269Function::hessian()
270{
271 return hessian_.result();
272}
273
274int
275Function::hessian_needed() const
276{
277 return hessian_.needed();
278}
279
280int
281Function::do_hessian(int f)
282{
283 return hessian_.compute(f);
284}
285
286void
287Function::set_hessian(RefSymmSCMatrix&h)
288{
289 hessian_.result_noupdate() = h;
290 hessian_.computed() = 1;
291}
292
293// the default guess hessian is the unit diagonal
294void
295Function::guess_hessian(RefSymmSCMatrix&hessian)
296{
297 Ref<SCElementOp> op(new SCElementShiftDiagonal(1.0));
298 hessian.assign(0.0);
299 hessian.element_op(op);
300}
301
302RefSymmSCMatrix
303Function::inverse_hessian(RefSymmSCMatrix&hessian)
304{
305 return hessian.gi();
306}
307
308void
309Function::set_desired_hessian_accuracy(double a)
310{
311 hessian_.set_desired_accuracy(a);
312}
313
314void
315Function::set_actual_hessian_accuracy(double a)
316{
317 hessian_.set_actual_accuracy(a);
318}
319
320double
321Function::desired_hessian_accuracy() const
322{
323 return hessian_.desired_accuracy();
324}
325
326double
327Function::actual_hessian_accuracy() const
328{
329 return hessian_.actual_accuracy();
330}
331
332void
333Function::print(ostream&o) const
334{
335 const char *computed = " (computed)";
336 const char *notcomputed = "";
337 o << indent << "Function Parameters:\n" << incindent
338 << indent << scprintf("value_accuracy = %e (%e)%s\n",
339 actual_value_accuracy(), desired_value_accuracy(),
340 (value_.computed()?computed:notcomputed))
341 << indent << scprintf("gradient_accuracy = %e (%e)%s\n",
342 actual_gradient_accuracy(),
343 desired_gradient_accuracy(),
344 (gradient_.computed()?computed:notcomputed))
345 << indent << scprintf("hessian_accuracy = %e (%e)%s\n",
346 actual_hessian_accuracy(),
347 desired_hessian_accuracy(),
348 (hessian_.computed()?computed:notcomputed))
349 << decindent << endl;
350}
351
352void
353Function::set_matrixkit(const Ref<SCMatrixKit>& kit)
354{
355 matrixkit_ = kit;
356}
357
358void
359Function::set_dimension(const RefSCDimension& dim)
360{
361 dim_ = dim;
362 x_ = matrixkit_->vector(dim);
363 x_.assign(0.0);
364 gradient_ = matrixkit()->vector(dim);
365 gradient_.result_noupdate().assign(0.0);
366 hessian_ = matrixkit()->symmmatrix(dim);
367 hessian_.result_noupdate().assign(0.0);
368}
369
370int
371Function::value_implemented() const
372{
373 return 0;
374}
375
376int
377Function::gradient_implemented() const
378{
379 return 0;
380}
381
382int
383Function::hessian_implemented() const
384{
385 return 0;
386}
387
388Ref<NonlinearTransform>
389Function::change_coordinates()
390{
391 return 0;
392}
393
394void
395Function::do_change_coordinates(const Ref<NonlinearTransform> &t)
396{
397 if (t.null())
398 return;
399
400 t->transform_coordinates(x_);
401 obsolete();
402}
403
404/////////////////////////////////////////////////////////////////////////////
405
406// Local Variables:
407// mode: c++
408// c-file-style: "CLJ"
409// End:
Note: See TracBrowser for help on using the repository browser.