[0b990d] | 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 |
|
---|
| 39 | using namespace std;
|
---|
| 40 | using namespace sc;
|
---|
| 41 |
|
---|
| 42 | /////////////////////////////////////////////////////////////////////////
|
---|
| 43 | // Convergence
|
---|
| 44 |
|
---|
| 45 | static ClassDesc Convergence_cd(
|
---|
| 46 | typeid(Convergence),"Convergence",1,"virtual public SavableState",
|
---|
| 47 | 0, create<Convergence>, create<Convergence>);
|
---|
| 48 |
|
---|
| 49 | Convergence::Convergence()
|
---|
| 50 | {
|
---|
| 51 | set_defaults();
|
---|
| 52 | }
|
---|
| 53 |
|
---|
| 54 | Convergence::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 |
|
---|
| 64 | Convergence::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 |
|
---|
| 79 | Convergence::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 |
|
---|
| 99 | Convergence::~Convergence()
|
---|
| 100 | {
|
---|
| 101 | }
|
---|
| 102 |
|
---|
| 103 | void
|
---|
| 104 | Convergence::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 |
|
---|
| 118 | void
|
---|
| 119 | Convergence::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 |
|
---|
| 130 | void
|
---|
| 131 | Convergence::get_x(const Ref<Function> &f)
|
---|
| 132 | {
|
---|
| 133 | x_ = f->get_x();
|
---|
| 134 | }
|
---|
| 135 |
|
---|
| 136 | void
|
---|
| 137 | Convergence::set_nextx(const RefSCVector &x)
|
---|
| 138 | {
|
---|
| 139 | nextx_ = x->copy();
|
---|
| 140 | }
|
---|
| 141 |
|
---|
| 142 | void
|
---|
| 143 | Convergence::get_grad(const Ref<Function> &f)
|
---|
| 144 | {
|
---|
| 145 | grad_ = f->gradient();
|
---|
| 146 | }
|
---|
| 147 |
|
---|
| 148 | int
|
---|
| 149 | Convergence::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 |
|
---|
| 192 | void
|
---|
| 193 | Convergence::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 |
|
---|
| 207 | void
|
---|
| 208 | Convergence::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:
|
---|