1 | //
|
---|
2 | // File: MPQC_ChemistryOpt_CoordinateModel_Impl.cc
|
---|
3 | // Symbol: MPQC.ChemistryOpt_CoordinateModel-v0.2
|
---|
4 | // Symbol Type: class
|
---|
5 | // Babel Version: 0.10.2
|
---|
6 | // Description: Server-side implementation for MPQC.ChemistryOpt_CoordinateModel
|
---|
7 | //
|
---|
8 | // WARNING: Automatically generated; only changes within splicers preserved
|
---|
9 | //
|
---|
10 | // babel-version = 0.10.2
|
---|
11 | //
|
---|
12 | #include "MPQC_ChemistryOpt_CoordinateModel_Impl.hh"
|
---|
13 |
|
---|
14 | // DO-NOT-DELETE splicer.begin(MPQC.ChemistryOpt_CoordinateModel._includes)
|
---|
15 | #include <iostream>
|
---|
16 | #include <sstream>
|
---|
17 | #include "util/keyval/keyvalval.h"
|
---|
18 | #include "math/scmat/matrix.h"
|
---|
19 | #include "math/scmat/local.h"
|
---|
20 | #include "math/scmat/repl.h"
|
---|
21 |
|
---|
22 | #define DEFAULT_COORTYPE symm
|
---|
23 |
|
---|
24 | sidl::array<double>
|
---|
25 | vector_to_array(const sc::RefSCVector &v);
|
---|
26 |
|
---|
27 | sc::RefSCVector
|
---|
28 | array_to_vector(sidl::array<double>, const sc::RefSCVector &v);
|
---|
29 |
|
---|
30 | sidl::array<double>
|
---|
31 | matrix_to_array(const sc::RefSymmSCMatrix &v);
|
---|
32 |
|
---|
33 | sc::RefSymmSCMatrix
|
---|
34 | array_to_matrix(sidl::array<double>, const sc::RefSymmSCMatrix &v);
|
---|
35 |
|
---|
36 | // DO-NOT-DELETE splicer.end(MPQC.ChemistryOpt_CoordinateModel._includes)
|
---|
37 |
|
---|
38 | // user-defined constructor.
|
---|
39 | void MPQC::ChemistryOpt_CoordinateModel_impl::_ctor() {
|
---|
40 | // DO-NOT-DELETE splicer.begin(MPQC.ChemistryOpt_CoordinateModel._ctor)
|
---|
41 | have_guess_h_ = 0;
|
---|
42 | // DO-NOT-DELETE splicer.end(MPQC.ChemistryOpt_CoordinateModel._ctor)
|
---|
43 | }
|
---|
44 |
|
---|
45 | // user-defined destructor.
|
---|
46 | void MPQC::ChemistryOpt_CoordinateModel_impl::_dtor() {
|
---|
47 | // DO-NOT-DELETE splicer.begin(MPQC.ChemistryOpt_CoordinateModel._dtor)
|
---|
48 | // add destruction details here
|
---|
49 | // DO-NOT-DELETE splicer.end(MPQC.ChemistryOpt_CoordinateModel._dtor)
|
---|
50 | }
|
---|
51 |
|
---|
52 | // static class initializer.
|
---|
53 | void MPQC::ChemistryOpt_CoordinateModel_impl::_load() {
|
---|
54 | // DO-NOT-DELETE splicer.begin(MPQC.ChemistryOpt_CoordinateModel._load)
|
---|
55 | // guaranteed to be called at most once before any other method in this class
|
---|
56 | // DO-NOT-DELETE splicer.end(MPQC.ChemistryOpt_CoordinateModel._load)
|
---|
57 | }
|
---|
58 |
|
---|
59 | // user-defined static methods: (none)
|
---|
60 |
|
---|
61 | // user-defined non-static methods:
|
---|
62 | /**
|
---|
63 | * Registers and gets ports, and requests Model object(s) from the
|
---|
64 | * ModelFactory component(s). This must be the first method called
|
---|
65 | * following instantiation.
|
---|
66 | */
|
---|
67 | int32_t
|
---|
68 | MPQC::ChemistryOpt_CoordinateModel_impl::initialize ()
|
---|
69 | throw ()
|
---|
70 |
|
---|
71 | {
|
---|
72 | // DO-NOT-DELETE splicer.begin(MPQC.ChemistryOpt_CoordinateModel.initialize)
|
---|
73 |
|
---|
74 | int i;
|
---|
75 |
|
---|
76 | std::cout << "\nInitializing MPQC::ChemistryOpt_CoordinateModel\n";
|
---|
77 |
|
---|
78 | CcaChemGeneric::CoordinateModel::set_tolerances(grad_rms_->value,
|
---|
79 | grad_max_->value,
|
---|
80 | disp_rms_->value,
|
---|
81 | disp_max_->value);
|
---|
82 | CcaChemGeneric::CoordinateModel::initialize(services_);
|
---|
83 |
|
---|
84 | //get matrix kits
|
---|
85 | kit_ = new sc::LocalSCMatrixKit;
|
---|
86 | rkit_ = new sc::ReplSCMatrixKit;
|
---|
87 |
|
---|
88 | //get coordinate type
|
---|
89 | std::string coorString;
|
---|
90 | coorString = std::string( coordinates_->getValueString() );
|
---|
91 | std::cout << " Using coordinate type: " << coorString << std::endl;
|
---|
92 | if(coorString == "cartesian") coorType_ = cart;
|
---|
93 | else if(coorString == "symmetrized") coorType_ = symm;
|
---|
94 | else if(coorString == "redundant") coorType_ = redund;
|
---|
95 | else {
|
---|
96 | std::cout << " Unrecognized coordinate type, using default\n";
|
---|
97 | coorType_ = DEFAULT_COORTYPE;
|
---|
98 | }
|
---|
99 | services_.releasePort("CoordinateType");
|
---|
100 |
|
---|
101 | //get extra_bonds
|
---|
102 | std::string bondsString( extra_bonds_->getValueString() );
|
---|
103 |
|
---|
104 | //get model and molecule
|
---|
105 | model_ = CcaChemGeneric::CoordinateModel::get_model();
|
---|
106 | molecule_ = model_.get_molecule();
|
---|
107 | double conv = molecule_.get_units().convert_to("bohr");
|
---|
108 | convFrom_ = molecule_.get_units().convert_from("bohr");
|
---|
109 | int natom = molecule_.get_n_atom();
|
---|
110 |
|
---|
111 | std::cout << "\n CoordinateModel: setting up coordinates\n";
|
---|
112 |
|
---|
113 | //create input strings for MPQC classes
|
---|
114 | std::ostringstream input;
|
---|
115 | input
|
---|
116 | << " molecule<Molecule>:(\n"
|
---|
117 | << " symmetry = auto\n"
|
---|
118 | << " unit = bohr\n"
|
---|
119 | << " { n atoms geometry }={\n";
|
---|
120 | for(i=0;i<natom;++i) {
|
---|
121 | input
|
---|
122 | << "\t" << i << "\t" << molecule_.get_atomic_number(i)
|
---|
123 | << "\t[ " << molecule_.get_cart_coor(i,0)*conv
|
---|
124 | << " " << molecule_.get_cart_coor(i,1)*conv
|
---|
125 | << " " << molecule_.get_cart_coor(i,2)*conv << " ]\n";
|
---|
126 | }
|
---|
127 | input
|
---|
128 | << " }\n )\n";
|
---|
129 |
|
---|
130 | std::cout << input.str();
|
---|
131 |
|
---|
132 | switch(coorType_) {
|
---|
133 | case cart:
|
---|
134 | input << " coor<CartMolecularCoor>: (\n"
|
---|
135 | << " molecule = $..:molecule\n"
|
---|
136 | << " )\n";
|
---|
137 | break;
|
---|
138 | case symm:
|
---|
139 | input << " coor<SymmMolecularCoor>: (\n"
|
---|
140 | << " molecule = $..:molecule\n"
|
---|
141 | << " update_bmat = 1\n"
|
---|
142 | << " cartesian_tolerance = 1e-9\n"
|
---|
143 | << " generator<IntCoorGen>: (\n"
|
---|
144 | << " molecule = $..:..:molecule\n";
|
---|
145 | if( bondsString.size() != 0 )
|
---|
146 | input << " extra_bonds = [" << bondsString << "]\n";
|
---|
147 | input << " )\n )\n";
|
---|
148 | break;
|
---|
149 | case redund:
|
---|
150 | input << " coor<RedundMolecularCoor>: (\n"
|
---|
151 | << " molecule = $..:molecule\n"
|
---|
152 | << " update_bmat = 1\n"
|
---|
153 | << " cartesian_tolerance = 1e-9\n"
|
---|
154 | << " generator<IntCoorGen>: (\n"
|
---|
155 | << " molecule = $..:..:molecule\n";
|
---|
156 | if( bondsString.size() != 0 )
|
---|
157 | input << " extra_bonds = [" << bondsString << "]\n";
|
---|
158 | input << " )\n )\n";
|
---|
159 | break;
|
---|
160 | }
|
---|
161 |
|
---|
162 | std::cout << input.str();
|
---|
163 |
|
---|
164 | //create the MPQC classes
|
---|
165 | sc::Ref<sc::ParsedKeyVal> kv = new sc::ParsedKeyVal();
|
---|
166 | kv->parse_string(input.str().c_str());
|
---|
167 | sc::Ref<sc::DescribedClass> dccoor = kv->describedclassvalue("coor");
|
---|
168 | sc::Ref<sc::DescribedClass> dcmol = kv->describedclassvalue("molecule");
|
---|
169 | scMol_ = dynamic_cast<sc::Molecule*>(dcmol.pointer());
|
---|
170 | switch(coorType_) {
|
---|
171 | case cart:
|
---|
172 | scCoor_ = dynamic_cast<sc::CartMolecularCoor*>(dccoor.pointer());
|
---|
173 | break;
|
---|
174 | case symm:
|
---|
175 | scCoor_ = dynamic_cast<sc::SymmMolecularCoor*>(dccoor.pointer());
|
---|
176 | break;
|
---|
177 | case redund:
|
---|
178 | scCoor_ = dynamic_cast<sc::RedundMolecularCoor*>(dccoor.pointer());
|
---|
179 | break;
|
---|
180 | }
|
---|
181 |
|
---|
182 | scCoor_->print();
|
---|
183 | numCoor_ = scCoor_->dim().n();
|
---|
184 | natom3_ = scCoor_->dim_natom3().n();
|
---|
185 | std::cout << "\n";
|
---|
186 |
|
---|
187 | // convergence checking needs this method invoked
|
---|
188 | CcaChemGeneric::CoordinateModel::get_n_coor();
|
---|
189 |
|
---|
190 | return 0;
|
---|
191 | // DO-NOT-DELETE splicer.end(MPQC.ChemistryOpt_CoordinateModel.initialize)
|
---|
192 | }
|
---|
193 |
|
---|
194 | /**
|
---|
195 | * Releases and unregisters ports. This should be called when the
|
---|
196 | * CoordinateModel object is no longer needed.
|
---|
197 | */
|
---|
198 | int32_t
|
---|
199 | MPQC::ChemistryOpt_CoordinateModel_impl::finalize ()
|
---|
200 | throw ()
|
---|
201 |
|
---|
202 | {
|
---|
203 | // DO-NOT-DELETE splicer.begin(MPQC.ChemistryOpt_CoordinateModel.finalize)
|
---|
204 | return CcaChemGeneric::CoordinateModel::finalize();
|
---|
205 | // DO-NOT-DELETE splicer.end(MPQC.ChemistryOpt_CoordinateModel.finalize)
|
---|
206 | }
|
---|
207 |
|
---|
208 | /**
|
---|
209 | * Sets the contained chemistry Model object (currently unused as the
|
---|
210 | * chemistry Model object is normally obtained from a ModelFactory
|
---|
211 | * during initialization).
|
---|
212 | * @param model The chemistry model object.
|
---|
213 | */
|
---|
214 | void
|
---|
215 | MPQC::ChemistryOpt_CoordinateModel_impl::set_model (
|
---|
216 | /* in */ ::Chemistry::QC::Model model )
|
---|
217 | throw ()
|
---|
218 | {
|
---|
219 | // DO-NOT-DELETE splicer.begin(MPQC.ChemistryOpt_CoordinateModel.set_model)
|
---|
220 | CcaChemGeneric::CoordinateModel::set_model( model );
|
---|
221 | // DO-NOT-DELETE splicer.end(MPQC.ChemistryOpt_CoordinateModel.set_model)
|
---|
222 | }
|
---|
223 |
|
---|
224 | /**
|
---|
225 | * Returns the contained chemistry Model object.
|
---|
226 | * @return The chemistry Model object.
|
---|
227 | */
|
---|
228 | ::Chemistry::QC::Model
|
---|
229 | MPQC::ChemistryOpt_CoordinateModel_impl::get_model ()
|
---|
230 | throw ()
|
---|
231 |
|
---|
232 | {
|
---|
233 | // DO-NOT-DELETE splicer.begin(MPQC.ChemistryOpt_CoordinateModel.get_model)
|
---|
234 | return CcaChemGeneric::CoordinateModel::get_model();
|
---|
235 | // DO-NOT-DELETE splicer.end(MPQC.ChemistryOpt_CoordinateModel.get_model)
|
---|
236 | }
|
---|
237 |
|
---|
238 | /**
|
---|
239 | * Returns the number of coordinates.
|
---|
240 | * @return The number of coordinates.
|
---|
241 | */
|
---|
242 | int32_t
|
---|
243 | MPQC::ChemistryOpt_CoordinateModel_impl::get_n_coor ()
|
---|
244 | throw ()
|
---|
245 |
|
---|
246 | {
|
---|
247 | // DO-NOT-DELETE splicer.begin(MPQC.ChemistryOpt_CoordinateModel.get_n_coor)
|
---|
248 | return numCoor_;
|
---|
249 | // DO-NOT-DELETE splicer.end(MPQC.ChemistryOpt_CoordinateModel.get_n_coor)
|
---|
250 | }
|
---|
251 |
|
---|
252 | /**
|
---|
253 | * Returns the array of (cartesian or internal) coordinates which are
|
---|
254 | * being optimized.
|
---|
255 | * @return The array of coordinates which are being optimized.
|
---|
256 | */
|
---|
257 | ::sidl::array<double>
|
---|
258 | MPQC::ChemistryOpt_CoordinateModel_impl::get_coor ()
|
---|
259 | throw ()
|
---|
260 |
|
---|
261 | {
|
---|
262 | // DO-NOT-DELETE splicer.begin(MPQC.ChemistryOpt_CoordinateModel.get_coor)
|
---|
263 |
|
---|
264 | ::sidl::array<double> sidlCoor;
|
---|
265 |
|
---|
266 | switch(coorType_) {
|
---|
267 | case cart:
|
---|
268 | sidlCoor = molecule_.get_coor();
|
---|
269 | break;
|
---|
270 | default:
|
---|
271 | sc::Ref<sc::SCMatrixKit> kit = new sc::LocalSCMatrixKit;
|
---|
272 | sc::RefSCVector scCoor = kit->vector(scCoor_->dim());
|
---|
273 | scCoor_->to_internal(scCoor);
|
---|
274 | sidlCoor = vector_to_array(scCoor);
|
---|
275 | break;
|
---|
276 | }
|
---|
277 |
|
---|
278 | return sidlCoor;
|
---|
279 | // DO-NOT-DELETE splicer.end(MPQC.ChemistryOpt_CoordinateModel.get_coor)
|
---|
280 | }
|
---|
281 |
|
---|
282 | /**
|
---|
283 | * Returns the energy of the currently contained model with the values
|
---|
284 | * of the optimization coordinates given in x. This requires
|
---|
285 | * that the CoordinateModel updates the cartesian coordinates of a
|
---|
286 | * contained Molecule object (possibly requiring transformation) and set
|
---|
287 | * this Molecule object on a contained Model object, prior to calling
|
---|
288 | * get_energy() on the Model object.
|
---|
289 | * @param x The optimization coordinate values.
|
---|
290 | * @return The energy of the chemistry model at x.
|
---|
291 | */
|
---|
292 | double
|
---|
293 | MPQC::ChemistryOpt_CoordinateModel_impl::get_energy (
|
---|
294 | /* in */ ::sidl::array<double> x )
|
---|
295 | throw ()
|
---|
296 | {
|
---|
297 | // DO-NOT-DELETE splicer.begin(MPQC.ChemistryOpt_CoordinateModel.get_energy)
|
---|
298 |
|
---|
299 | std::cout << "***** MPQC ChemistryOpt Calculate Energy *****\n";
|
---|
300 | double f;
|
---|
301 |
|
---|
302 | //get energy, transform coordinates if neeeded
|
---|
303 | switch(coorType_) {
|
---|
304 | case cart:
|
---|
305 | f = CcaChemGeneric::CoordinateModel::get_energy(x);
|
---|
306 | break;
|
---|
307 | default:
|
---|
308 | sc::RefSCVector scCoor = kit_->vector(scCoor_->dim());
|
---|
309 | sidl::array<double> cartx =
|
---|
310 | sidl::array<double>::create1d(natom3_);
|
---|
311 | array_to_vector(x,scCoor);
|
---|
312 | scCoor_->to_cartesian(scMol_,scCoor);
|
---|
313 | for(int i=0; i<(natom3_/3); ++i)
|
---|
314 | for( int j=0; j<3; ++j)
|
---|
315 | cartx.set(i*3+j, scMol_->r(i,j)*convFrom_);
|
---|
316 | f = CcaChemGeneric::CoordinateModel::get_energy(cartx);
|
---|
317 | scCoor_->print();
|
---|
318 | break;
|
---|
319 | }
|
---|
320 |
|
---|
321 | return f;
|
---|
322 |
|
---|
323 | // DO-NOT-DELETE splicer.end(MPQC.ChemistryOpt_CoordinateModel.get_energy)
|
---|
324 | }
|
---|
325 |
|
---|
326 | /**
|
---|
327 | * Returns the energy gradient of the currently contained model with
|
---|
328 | * the values of the optimization coordinates given in x. This requires
|
---|
329 | * that the CoordinateModel updates the cartesian coordinates of a
|
---|
330 | * contained Molecule object (possibly requiring transformation) and set
|
---|
331 | * this Molecule object on a contained Model object, prior to calling
|
---|
332 | * get_gradient() on the Model object. If the optimization coordinate
|
---|
333 | * system is not cartesian, the gradient is transformed.
|
---|
334 | * @param x The optimization coordinate values.
|
---|
335 | * @return The energy gradient of the chemistry model at x.
|
---|
336 | */
|
---|
337 | ::sidl::array<double>
|
---|
338 | MPQC::ChemistryOpt_CoordinateModel_impl::get_gradient (
|
---|
339 | /* in */ ::sidl::array<double> x )
|
---|
340 | throw ()
|
---|
341 | {
|
---|
342 | // DO-NOT-DELETE splicer.begin(MPQC.ChemistryOpt_CoordinateModel.get_gradient)
|
---|
343 |
|
---|
344 | std::cout << "***** MPQC ChemistryOpt Calculate Gradient *****\n";
|
---|
345 |
|
---|
346 | ::sidl::array<double> cartg = ::sidl::array<double>::create1d(natom3_);
|
---|
347 |
|
---|
348 | //get gradient, transform coordinates if needed
|
---|
349 | switch(coorType_) {
|
---|
350 | case cart:
|
---|
351 | cartg.copy(CcaChemGeneric::CoordinateModel::get_gradient(x));
|
---|
352 | break;
|
---|
353 | default:
|
---|
354 | sc::RefSCVector scCoor = kit_->vector(scCoor_->dim());
|
---|
355 | array_to_vector(x,scCoor);
|
---|
356 | scCoor_->to_cartesian(scMol_,scCoor);
|
---|
357 | sidl::array<double> cartx =
|
---|
358 | sidl::array<double>::create1d(natom3_);
|
---|
359 | for(int i=0; i<(natom3_/3); ++i)
|
---|
360 | for( int j=0; j<3; ++j)
|
---|
361 | cartx.set(i*3+j, scMol_->r(i,j)*convFrom_);
|
---|
362 | cartg.copy(CcaChemGeneric::CoordinateModel::get_gradient(cartx));
|
---|
363 | scCoor_->print();
|
---|
364 | break;
|
---|
365 | }
|
---|
366 |
|
---|
367 | //transform gradient if using internals
|
---|
368 | ::sidl::array<double> g;
|
---|
369 | switch(coorType_) {
|
---|
370 | case cart:
|
---|
371 | g = cartg;
|
---|
372 | break;
|
---|
373 | default:
|
---|
374 | sc::RefSCVector scCartGrad = rkit_->vector(scCoor_->dim_natom3());
|
---|
375 | sc::RefSCVector scGrad = rkit_->vector(scCoor_->dim());
|
---|
376 | array_to_vector(cartg,scCartGrad);
|
---|
377 | scCoor_->to_internal(scGrad,scCartGrad);
|
---|
378 | g.copy(vector_to_array(scGrad));
|
---|
379 | break;
|
---|
380 | }
|
---|
381 |
|
---|
382 | return g;
|
---|
383 |
|
---|
384 | // DO-NOT-DELETE splicer.end(MPQC.ChemistryOpt_CoordinateModel.get_gradient)
|
---|
385 | }
|
---|
386 |
|
---|
387 | /**
|
---|
388 | * Returns the energy Hessian of the currently contained model with
|
---|
389 | * the values of the optimization coordinates given in x. This requires
|
---|
390 | * that the CoordinateModel updates the cartesian coordinates of a
|
---|
391 | * contained Molecule object (possibly requiring transformation) and set
|
---|
392 | * this Molecule object on a contained Model object, prior to calling
|
---|
393 | * get_hessian() on the Model object. If the optimization coordinate
|
---|
394 | * system is not cartesian, the Hessian is transformed.
|
---|
395 | * @param x The optimization coordinate values.
|
---|
396 | * @return The energy Hessian of the chemistry model at x.
|
---|
397 | */
|
---|
398 | ::sidl::array<double>
|
---|
399 | MPQC::ChemistryOpt_CoordinateModel_impl::get_hessian (
|
---|
400 | /* in */ ::sidl::array<double> x )
|
---|
401 | throw ()
|
---|
402 | {
|
---|
403 | // DO-NOT-DELETE splicer.begin(MPQC.ChemistryOpt_CoordinateModel.get_hessian)
|
---|
404 |
|
---|
405 | std::cout << "***** MPQC ChemistryOpt Calculate Hessian *****\n";
|
---|
406 | std::cout << " WARNING: this method has not been tested yet\n";
|
---|
407 |
|
---|
408 | ::sidl::array<double> cartH =
|
---|
409 | ::sidl::array<double>::create2dRow(natom3_,natom3_);
|
---|
410 |
|
---|
411 | //get hessian, transform coordinates if needed
|
---|
412 | switch(coorType_) {
|
---|
413 | case cart:
|
---|
414 | cartH.copy(CcaChemGeneric::CoordinateModel::get_hessian(x));
|
---|
415 | break;
|
---|
416 | default:
|
---|
417 | sc::RefSCVector scCoor = kit_->vector(scCoor_->dim());
|
---|
418 | array_to_vector(x,scCoor);
|
---|
419 | scCoor_->to_cartesian(scMol_,scCoor);
|
---|
420 | sidl::array<double> cartx =
|
---|
421 | sidl::array<double>::create1d(natom3_);
|
---|
422 | for(int i=0; i<(natom3_/3); ++i)
|
---|
423 | for( int j=0; j<3; ++j)
|
---|
424 | cartx.set(i*3+j, scMol_->r(i,j)*convFrom_);
|
---|
425 | cartH.copy(CcaChemGeneric::CoordinateModel::get_hessian(cartx));
|
---|
426 | scCoor_->print();
|
---|
427 | break;
|
---|
428 | }
|
---|
429 |
|
---|
430 | //transform Hessian if using internals
|
---|
431 | ::sidl::array<double> H;
|
---|
432 | switch(coorType_) {
|
---|
433 | case cart:
|
---|
434 | H = cartH;
|
---|
435 | break;
|
---|
436 | default:
|
---|
437 | sc::RefSymmSCMatrix scCartH =
|
---|
438 | rkit_->symmmatrix(scCoor_->dim_natom3());
|
---|
439 | sc::RefSymmSCMatrix scH =
|
---|
440 | rkit_->symmmatrix(scCoor_->dim());
|
---|
441 | array_to_matrix(cartH,scCartH);
|
---|
442 | scCoor_->to_internal(scH,scCartH);
|
---|
443 | H = matrix_to_array(scH);
|
---|
444 | break;
|
---|
445 | }
|
---|
446 |
|
---|
447 | return H;
|
---|
448 |
|
---|
449 | // DO-NOT-DELETE splicer.end(MPQC.ChemistryOpt_CoordinateModel.get_hessian)
|
---|
450 | }
|
---|
451 |
|
---|
452 | /**
|
---|
453 | * Sets f and g to the energy and energy gradient, respectively,
|
---|
454 | * of the chemistry model at x. This is similar to calling
|
---|
455 | * get_energy() and get_gradient() separately, but set_molecule()
|
---|
456 | * must be called on the Model object only once. This is necessary
|
---|
457 | * for some model implementations, as a second molecule update
|
---|
458 | * would invalidate results from an energy computation. An alternative
|
---|
459 | * would be to always return the energy as well when get_gradient() is
|
---|
460 | * called.
|
---|
461 | * @param x The optimization coordinate values.
|
---|
462 | * @param f Variable that energy will be assigned to.
|
---|
463 | * @param g Array that the gradient will be assigned to.
|
---|
464 | */
|
---|
465 | void
|
---|
466 | MPQC::ChemistryOpt_CoordinateModel_impl::get_energy_and_gradient (
|
---|
467 | /* in */ ::sidl::array<double> x,
|
---|
468 | /* out */ double& f,
|
---|
469 | /* in */ ::sidl::array<double> g )
|
---|
470 | throw ()
|
---|
471 | {
|
---|
472 | // DO-NOT-DELETE splicer.begin(MPQC.ChemistryOpt_CoordinateModel.get_energy_and_gradient)
|
---|
473 | std::cout << "***** MPQC ChemistryOpt Calculate Energy and Gradient *****\n";
|
---|
474 |
|
---|
475 | ::sidl::array<double> cartg = ::sidl::array<double>::create1d(natom3_);
|
---|
476 |
|
---|
477 | //get gradient, transform coordinates if needed
|
---|
478 | switch(coorType_) {
|
---|
479 | case cart:
|
---|
480 | CcaChemGeneric::CoordinateModel::get_energy_and_gradient(x,&f,g);
|
---|
481 | break;
|
---|
482 | default:
|
---|
483 | sc::RefSCVector scCoor = kit_->vector(scCoor_->dim());
|
---|
484 | array_to_vector(x,scCoor);
|
---|
485 | scCoor_->to_cartesian(scMol_,scCoor);
|
---|
486 | sidl::array<double> cartx =
|
---|
487 | sidl::array<double>::create1d(natom3_);
|
---|
488 | for(int i=0; i<(natom3_/3); ++i)
|
---|
489 | for( int j=0; j<3; ++j)
|
---|
490 | cartx.set(i*3+j, scMol_->r(i,j)*convFrom_);
|
---|
491 | CcaChemGeneric::CoordinateModel::get_energy_and_gradient(cartx,&f,cartg);
|
---|
492 | scCoor_->print();
|
---|
493 | break;
|
---|
494 | }
|
---|
495 |
|
---|
496 | //transform gradient if using internals
|
---|
497 | switch(coorType_) {
|
---|
498 | case cart:
|
---|
499 | break;
|
---|
500 | default:
|
---|
501 | sc::RefSCVector scCartGrad = rkit_->vector(scCoor_->dim_natom3());
|
---|
502 | sc::RefSCVector scGrad = rkit_->vector(scCoor_->dim());
|
---|
503 | array_to_vector(cartg,scCartGrad);
|
---|
504 | scCoor_->to_internal(scGrad,scCartGrad);
|
---|
505 | g.copy(vector_to_array(scGrad));
|
---|
506 | break;
|
---|
507 | }
|
---|
508 |
|
---|
509 | // DO-NOT-DELETE splicer.end(MPQC.ChemistryOpt_CoordinateModel.get_energy_and_gradient)
|
---|
510 | }
|
---|
511 |
|
---|
512 | /**
|
---|
513 | * Returns the product of the guess hessian inverse and an effective
|
---|
514 | * gradient. Probably unique to TAO's limited memory variable metric
|
---|
515 | * algorithm, which uses this method to accomodate dense guess hessians.
|
---|
516 | * "first_geom_ptr" provides the Cartesian coordinates for which the
|
---|
517 | * guess Hessian should be computed (first_geom_ptr=0 for current
|
---|
518 | * geometry).
|
---|
519 | * @param effective_grad An effective gradient.
|
---|
520 | * @param effective_step Array that effective step is assigned to.
|
---|
521 | * @param first_geom Pointer to array of Cartesians
|
---|
522 | */
|
---|
523 | void
|
---|
524 | MPQC::ChemistryOpt_CoordinateModel_impl::guess_hessian_solve (
|
---|
525 | /* in */ ::sidl::array<double> effective_grad,
|
---|
526 | /* in */ ::sidl::array<double> effective_step,
|
---|
527 | /* in */ void* first_geom )
|
---|
528 | throw ()
|
---|
529 | {
|
---|
530 | // DO-NOT-DELETE splicer.begin(MPQC.ChemistryOpt_CoordinateModel.guess_hessian_solve)
|
---|
531 |
|
---|
532 | sidl::array<double> *sidl_geom_ptr =
|
---|
533 | static_cast< sidl::array<double>* >( first_geom );
|
---|
534 |
|
---|
535 | if(multiple_guess_h_->value && !use_current_geom_->value && sidl_geom_ptr){
|
---|
536 | std::cout << "Using geometry for first correction pair\n";
|
---|
537 | sc::RefSCVector scCoor = kit_->vector(scCoor_->dim());
|
---|
538 | array_to_vector( *sidl_geom_ptr, scCoor );
|
---|
539 | scCoor_->to_cartesian(scMol_,scCoor);
|
---|
540 | }
|
---|
541 |
|
---|
542 | if( multiple_guess_h_->value || !have_guess_h_ ) {
|
---|
543 | std::cout << "Determining approximate Hessian\n";
|
---|
544 | sc::RefSymmSCMatrix hess = rkit_->symmmatrix(scCoor_->dim());
|
---|
545 | scCoor_->guess_hessian(hess);
|
---|
546 | ihess_ = scCoor_->inverse_hessian(hess);
|
---|
547 | have_guess_h_ = 1;
|
---|
548 | }
|
---|
549 |
|
---|
550 | std::cout << "Solving approximate Hessian system\n";
|
---|
551 | sc::RefSCVector scV = rkit_->vector(scCoor_->dim());
|
---|
552 | array_to_vector(effective_grad, scV);
|
---|
553 | sc::RefSCVector result = ihess_*scV;
|
---|
554 | effective_step.copy(vector_to_array( result ));
|
---|
555 |
|
---|
556 | // DO-NOT-DELETE splicer.end(MPQC.ChemistryOpt_CoordinateModel.guess_hessian_solve)
|
---|
557 | }
|
---|
558 |
|
---|
559 | /**
|
---|
560 | * Determines if the optimization has converged, flag is set to 1
|
---|
561 | * if convergence has been achieved and 0 otherwise.
|
---|
562 | * @param flag Variable that convergence value is assigned to.
|
---|
563 | */
|
---|
564 | void
|
---|
565 | MPQC::ChemistryOpt_CoordinateModel_impl::checkConvergence (
|
---|
566 | /* inout */ int32_t& flag )
|
---|
567 | throw ()
|
---|
568 | {
|
---|
569 | // DO-NOT-DELETE splicer.begin(MPQC.ChemistryOpt_CoordinateModel.checkConvergence)
|
---|
570 | CcaChemGeneric::CoordinateModel::checkConvergence( flag );
|
---|
571 | // DO-NOT-DELETE splicer.end(MPQC.ChemistryOpt_CoordinateModel.checkConvergence)
|
---|
572 | }
|
---|
573 |
|
---|
574 | /**
|
---|
575 | * For visualization, possibly unused (?). CoordinateModel objects
|
---|
576 | * may callback to viewers that implement the Chemistry.MoleculeViewer
|
---|
577 | * interface, such as the cca-chem python GUI, making this method
|
---|
578 | * unnecessary.
|
---|
579 | */
|
---|
580 | void
|
---|
581 | MPQC::ChemistryOpt_CoordinateModel_impl::monitor ()
|
---|
582 | throw ()
|
---|
583 |
|
---|
584 | {
|
---|
585 | // DO-NOT-DELETE splicer.begin(MPQC.ChemistryOpt_CoordinateModel.monitor)
|
---|
586 | // insert implementation here
|
---|
587 | // DO-NOT-DELETE splicer.end(MPQC.ChemistryOpt_CoordinateModel.monitor)
|
---|
588 | }
|
---|
589 |
|
---|
590 | /**
|
---|
591 | * Starts up a component presence in the calling framework.
|
---|
592 | * @param Svc the component instance's handle on the framework world.
|
---|
593 | * Contracts concerning Svc and setServices:
|
---|
594 | *
|
---|
595 | * The component interaction with the CCA framework
|
---|
596 | * and Ports begins on the call to setServices by the framework.
|
---|
597 | *
|
---|
598 | * This function is called exactly once for each instance created
|
---|
599 | * by the framework.
|
---|
600 | *
|
---|
601 | * The argument Svc will never be nil/null.
|
---|
602 | *
|
---|
603 | * Those uses ports which are automatically connected by the framework
|
---|
604 | * (so-called service-ports) may be obtained via getPort during
|
---|
605 | * setServices.
|
---|
606 | */
|
---|
607 | void
|
---|
608 | MPQC::ChemistryOpt_CoordinateModel_impl::setServices (
|
---|
609 | /* in */ ::gov::cca::Services services )
|
---|
610 | throw (
|
---|
611 | ::gov::cca::CCAException
|
---|
612 | ){
|
---|
613 | // DO-NOT-DELETE splicer.begin(MPQC.ChemistryOpt_CoordinateModel.setServices)
|
---|
614 |
|
---|
615 | services_ = services;
|
---|
616 | if (services_._is_nil()) return;
|
---|
617 |
|
---|
618 | try {
|
---|
619 | services_.addProvidesPort(self,
|
---|
620 | "CoordinateModel",
|
---|
621 | "ChemistryOpt.CoordinateModel",
|
---|
622 | 0);
|
---|
623 | services_.registerUsesPort("ModelFactory",
|
---|
624 | "Chemistry.QC.ModelFactory",
|
---|
625 | 0);
|
---|
626 | services_.registerUsesPort("BackupModelFactory",
|
---|
627 | "Chemistry.QC.ModelFactory",
|
---|
628 | 0);
|
---|
629 | services_.registerUsesPort("MoleculeViewer",
|
---|
630 | "Chemistry.MoleculeViewer",
|
---|
631 | 0);
|
---|
632 | services_.registerUsesPort("CoordinateType",
|
---|
633 | "Util.StringProvider",
|
---|
634 | 0);
|
---|
635 | }
|
---|
636 | catch (gov::cca::CCAException e) {
|
---|
637 | std::cout << "Error using services: "
|
---|
638 | << e.getNote() << std::endl;
|
---|
639 | }
|
---|
640 |
|
---|
641 | // setup parameters
|
---|
642 | try {
|
---|
643 |
|
---|
644 | if (services_._not_nil()) {
|
---|
645 | gov::cca::TypeMap tm = services_.createTypeMap();
|
---|
646 | ::gov::cca::Port self_port = self;
|
---|
647 | services_.addProvidesPort(self_port,
|
---|
648 | "string",
|
---|
649 | "Util.StringProvider",tm);
|
---|
650 |
|
---|
651 | services_.registerUsesPort("classicParam",
|
---|
652 | "gov.cca.ParameterPortFactoryService",tm);
|
---|
653 | gov::cca::Port p = services_.getPort("classicParam");
|
---|
654 | ccaffeine::ports::PortTranslator portX = p;
|
---|
655 | if(portX._not_nil()) {
|
---|
656 | classic::gov::cca::Port *cp
|
---|
657 | =static_cast<classic::gov::cca::Port*>(portX.getClassicPort());
|
---|
658 | if(!cp) {
|
---|
659 | std::cout << "Couldn't get classic port" << std::endl;
|
---|
660 | return;
|
---|
661 | }
|
---|
662 | ConfigurableParameterFactory *cpf
|
---|
663 | = dynamic_cast<ConfigurableParameterFactory *>(cp);
|
---|
664 | ConfigurableParameterPort *pp = setup_parameters(cpf);
|
---|
665 | classic::gov::cca::Port *clscp
|
---|
666 | = dynamic_cast<classic::gov::cca::Port*>(pp);
|
---|
667 | if (!clscp) {
|
---|
668 | std::cout << "Couldn't cast to classic::gov::cca::Port"
|
---|
669 | << std::endl;
|
---|
670 | }
|
---|
671 | void *vp = static_cast<void*>(clscp);
|
---|
672 | ccaffeine::ports::PortTranslator provideX
|
---|
673 | = ccaffeine::ports::PortTranslator::createFromClassic(vp);
|
---|
674 |
|
---|
675 | services_.addProvidesPort(provideX,
|
---|
676 | "configure", "ParameterPort", tm);
|
---|
677 |
|
---|
678 | services_.releasePort("classicParam");
|
---|
679 | services_.unregisterUsesPort("classicParam");
|
---|
680 | }
|
---|
681 | }
|
---|
682 |
|
---|
683 | }
|
---|
684 | catch(std::exception& e) {
|
---|
685 | std::cout << "Exception caught: " << e.what() << std::endl;
|
---|
686 | }
|
---|
687 |
|
---|
688 | // DO-NOT-DELETE splicer.end(MPQC.ChemistryOpt_CoordinateModel.setServices)
|
---|
689 | }
|
---|
690 |
|
---|
691 |
|
---|
692 | // DO-NOT-DELETE splicer.begin(MPQC.ChemistryOpt_CoordinateModel._misc)
|
---|
693 |
|
---|
694 | ConfigurableParameterPort*
|
---|
695 | MPQC::ChemistryOpt_CoordinateModel_impl::setup_parameters(ConfigurableParameterFactory *cpf)
|
---|
696 | {
|
---|
697 | ConfigurableParameterPort * pp = cpf->createConfigurableParameterPort();
|
---|
698 |
|
---|
699 | pp->setBatchTitle("PortTranslatorStarter Configuration");
|
---|
700 | pp->setGroupName("Coordinate Model Input");
|
---|
701 | grad_rms_ = new DoubleParameter("grad_rms",
|
---|
702 | "RMS gradient convergence tolerance",
|
---|
703 | "grad_rms", 0.00030,0,1000000);
|
---|
704 | grad_max_ = new DoubleParameter("grad_max",
|
---|
705 | "Max gradient convergence tolerance",
|
---|
706 | "grad_max", 0.00045,0,1000000);
|
---|
707 | disp_rms_ = new DoubleParameter("disp_rms",
|
---|
708 | "RMS displacement convergence tolerance",
|
---|
709 | "disp_rms", 0.00120,0,1000000);
|
---|
710 | disp_max_ = new DoubleParameter("disp_max",
|
---|
711 | "Max displacement convergence tolerance",
|
---|
712 | "disp_max", 0.00180,0,1000000);
|
---|
713 | multiple_guess_h_ = new BoolParameter("multiple_guess_h",
|
---|
714 | "Guess H at every guess_hessian_solve",
|
---|
715 | "multiple_guess_h",1);
|
---|
716 | use_current_geom_ = new BoolParameter("use_current_geom",
|
---|
717 | "Guess Hessian at current geometry",
|
---|
718 | "use_current_geom",0);
|
---|
719 | coordinates_ = new StringParameter("coordinate_type",
|
---|
720 | "Coordinate type: cartesian, symmetrized, or redundant",
|
---|
721 | "coordinate_type", "symmetrized");
|
---|
722 | extra_bonds_ = new StringParameter("extra_bonds", "extra_bonds vector",
|
---|
723 | "extra_bonds", "");
|
---|
724 | pp->addRequest(grad_rms_);
|
---|
725 | pp->addRequest(grad_max_);
|
---|
726 | pp->addRequest(disp_rms_);
|
---|
727 | pp->addRequest(disp_max_);
|
---|
728 | pp->addRequest(multiple_guess_h_);
|
---|
729 | pp->addRequest(use_current_geom_);
|
---|
730 | pp->addRequest(coordinates_);
|
---|
731 | pp->addRequest(extra_bonds_);
|
---|
732 |
|
---|
733 | return pp;
|
---|
734 | }
|
---|
735 |
|
---|
736 | // DO-NOT-DELETE splicer.end(MPQC.ChemistryOpt_CoordinateModel._misc)
|
---|
737 |
|
---|