source: ThirdParty/mpqc_open/src/lib/math/optimize/opttest.cc@ 1513599

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 1513599 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: 4.2 KB
Line 
1//
2// opttest.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#include <iostream>
29#include <util/state/stateio.h>
30#include <math/optimize/function.h>
31#include <math/optimize/opt.h>
32#include <util/keyval/keyval.h>
33#include <math/scmat/local.h>
34#include <math/scmat/matrix.h>
35#include <math/scmat/elemop.h>
36#include <math/optimize/linkage.h>
37
38using namespace std;
39using namespace sc;
40
41class Quadratic: public Function
42{
43 private:
44 RefSCVector x0;
45 RefSCVector g0;
46 RefSymmSCMatrix h0;
47 RefSymmSCMatrix hguess;
48 public:
49 Quadratic(StateIn&);
50 Quadratic(const Ref<KeyVal>&);
51 void save_data_state(StateOut&);
52 void compute();
53 void guess_hessian(RefSymmSCMatrix&);
54};
55static ClassDesc Quadratic_cd(
56 typeid(Quadratic),"Quadratic",1,"public Function",
57 0, create<Quadratic>, create<Quadratic>);
58Quadratic::Quadratic(StateIn&s):
59 SavableState(s),
60 Function(s)
61{
62 x0 = matrixkit_->vector(dim_);
63 x0.restore(s);
64 g0 = matrixkit_->vector(dim_);
65 g0.restore(s);
66 h0 = matrixkit_->symmmatrix(dim_);
67 h0.restore(s);
68}
69void
70Quadratic::save_data_state(StateOut&s)
71{
72 Function::save_data_state(s);
73 x0.save(s);
74 g0.save(s);
75 h0.save(s);
76}
77Quadratic::Quadratic(const Ref<KeyVal>&keyval):
78 Function(keyval)
79{
80 set_dimension(new SCDimension(keyval->count("x0")));
81 x0 = matrixkit_->vector(dim_);
82 g0 = matrixkit_->vector(dim_);
83 h0 = matrixkit_->symmmatrix(dim_);
84 hguess = matrixkit_->symmmatrix(dim_);
85 hguess.assign(0.0);
86 Ref<SCElementOp> op(new SCElementShiftDiagonal(1.0));
87 hguess.element_op(op);
88
89 int dim = dimension()->n();
90 for (int i=0; i<dim; i++) {
91 x0(i) = keyval->doublevalue("x0",i);
92 g0(i) = keyval->doublevalue("g0",i);
93 for (int j=0; j<=i; j++) {
94 h0(i,j) = keyval->doublevalue("h0",i,j);
95 hguess(i,j) = keyval->doublevalue("hguess",i,j);
96 }
97 }
98}
99// this computes everything, whether or not it was requested
100void
101Quadratic::compute()
102{
103 cout << "Quadratic::compute(): entered\n";
104
105 // compute the displacement from x0
106 RefSCVector d = x_ - x0;
107
108 // compute h * d
109 RefSCVector h0d = h0 * d;
110// RefSCVector h0d(h0.dim());
111// int n=h0.dim().n();
112// for (int i=0; i<n; i++) {
113// double tmp = 0;
114// for (int j=0; j<n; j++) {
115// tmp += h0(i,j) * d(j);
116// }
117// h0d(i) = tmp;
118// }
119
120 // compute the value
121 value_.result_noupdate() = d.scalar_product(g0)
122 + 0.5 * d.scalar_product(h0d);
123 value_.computed() = 1;
124
125 // compute the gradient
126 gradient_.result_noupdate() = g0 + h0d;
127 gradient_.computed() = 1;
128
129 // compute the hessian
130 hessian_.result_noupdate() = h0;
131 hessian_.computed() = 1;
132}
133void
134Quadratic::guess_hessian(RefSymmSCMatrix&gh)
135{
136 gh.assign(hguess);
137}
138
139main()
140{
141 Ref<KeyVal> kv = new ParsedKeyVal( SRCDIR "/opttest.in");
142 Ref<KeyVal> pkv = new PrefixKeyVal(kv,"opt");
143
144 for (int i=0; i<pkv->count(); i++) {
145 Ref<Optimize> opt; opt << pkv->describedclassvalue(i);
146 if (opt.nonnull()) {
147 RefSCVector oldx = opt->function()->get_x();
148 opt->optimize();
149 // restore the orginal x, in case the function is used again
150 opt->function()->set_x(oldx);
151 }
152 }
153}
154
155/////////////////////////////////////////////////////////////////////////////
156
157// Local Variables:
158// mode: c++
159// c-file-style: "CLJ"
160// End:
Note: See TracBrowser for help on using the repository browser.