source: src/lib/chemistry/cca/MPQC_Chemistry_QC_ModelFactory_Impl.cc@ 09bc09

Last change on this file since 09bc09 was 5d30c1, checked in by Frederik Heber <heber@…>, 13 years ago

Initial commit based on 3.0.0alpha (here claimed as 2.4).

  • simply added all files.
  • Property mode set to 100644
File size: 14.0 KB
Line 
1//
2// File: MPQC_Chemistry_QC_ModelFactory_Impl.cc
3// Symbol: MPQC.Chemistry_QC_ModelFactory-v0.2
4// Symbol Type: class
5// Babel Version: 0.10.2
6// Description: Server-side implementation for MPQC.Chemistry_QC_ModelFactory
7//
8// WARNING: Automatically generated; only changes within splicers preserved
9//
10// babel-version = 0.10.2
11//
12#include "MPQC_Chemistry_QC_ModelFactory_Impl.hh"
13
14// DO-NOT-DELETE splicer.begin(MPQC.Chemistry_QC_ModelFactory._includes)
15
16#include <iostream>
17#include <sstream>
18#include <iomanip>
19#include <MPQC_Chemistry_QC_Model.hh>
20#include <Chemistry_MoleculeFactory.hh>
21
22using namespace std;
23using namespace sc;
24// DO-NOT-DELETE splicer.end(MPQC.Chemistry_QC_ModelFactory._includes)
25
26// user-defined constructor.
27void MPQC::Chemistry_QC_ModelFactory_impl::_ctor() {
28 // DO-NOT-DELETE splicer.begin(MPQC.Chemistry_QC_ModelFactory._ctor)
29
30 /////////////////////////////////////////////////////////////
31 // Since we're setting up groups here, bad things will
32 // probably happen if multiple MPQC factories are instantiated
33 /////////////////////////////////////////////////////////////
34
35 // ccaffeine has main, could use stovepipe to get command-line vars,
36 // but for now use environmental variables and fake argc/argv
37 int fake_argc=0;
38 char** fake_argv=0;
39
40 // always use MPI message group
41 //grp_ = new sc::MessageGrp( &fake_argc, &fake_argv);
42 //if (grp_.nonnull())
43 // sc::MessageGrp::set_default_messagegrp(grp_);
44 grp_ = sc::MessageGrp::get_default_messagegrp();
45
46 // get thread group
47 thread_ = sc::ThreadGrp::initial_threadgrp(fake_argc, fake_argv);
48 if( thread_.nonnull() )
49 sc::ThreadGrp::set_default_threadgrp(thread_);
50
51 // get memory group
52 memory_ = sc::MemoryGrp::initial_memorygrp(fake_argc, fake_argv);
53 if (memory_.nonnull())
54 sc::MemoryGrp::set_default_memorygrp(memory_);
55
56 std::cout << " Using " << grp_->class_name()
57 << " for message passing (number of nodes = " << grp_->n()
58 << ").\n";
59 if( thread_.nonnull() )
60 std::cout << " Using " << thread_->class_name()
61 << " for threading (number of threads = " << thread_->nthread()
62 << ").\n";
63 if( memory_.nonnull() )
64 std::cout << " Using " << memory_->class_name()
65 << " for distributed shared memory.\n";
66 if( grp_.nonnull() && thread_.nonnull() )
67 std::cout << " Total number of processors = "
68 << grp_->n() * thread_->nthread() << endl;
69
70 // DO-NOT-DELETE splicer.end(MPQC.Chemistry_QC_ModelFactory._ctor)
71}
72
73// user-defined destructor.
74void MPQC::Chemistry_QC_ModelFactory_impl::_dtor() {
75 // DO-NOT-DELETE splicer.begin(MPQC.Chemistry_QC_ModelFactory._dtor)
76 // add destruction details here
77 // DO-NOT-DELETE splicer.end(MPQC.Chemistry_QC_ModelFactory._dtor)
78}
79
80// static class initializer.
81void MPQC::Chemistry_QC_ModelFactory_impl::_load() {
82 // DO-NOT-DELETE splicer.begin(MPQC.Chemistry_QC_ModelFactory._load)
83 // guaranteed to be called at most once before any other method in this class
84 // DO-NOT-DELETE splicer.end(MPQC.Chemistry_QC_ModelFactory._load)
85}
86
87// user-defined static methods: (none)
88
89// user-defined non-static methods:
90/**
91 * Starts up a component presence in the calling framework.
92 * @param Svc the component instance's handle on the framework world.
93 * Contracts concerning Svc and setServices:
94 *
95 * The component interaction with the CCA framework
96 * and Ports begins on the call to setServices by the framework.
97 *
98 * This function is called exactly once for each instance created
99 * by the framework.
100 *
101 * The argument Svc will never be nil/null.
102 *
103 * Those uses ports which are automatically connected by the framework
104 * (so-called service-ports) may be obtained via getPort during
105 * setServices.
106 */
107void
108MPQC::Chemistry_QC_ModelFactory_impl::setServices (
109 /* in */ ::gov::cca::Services services )
110throw (
111 ::gov::cca::CCAException
112){
113 // DO-NOT-DELETE splicer.begin(MPQC.Chemistry_QC_ModelFactory.setServices)
114
115 services_ = services;
116 if (services_._is_nil()) return;
117
118 try {
119 services_.addProvidesPort(self, "ModelFactory",
120 "gov.cca.Port", 0);
121 services_.registerUsesPort("BasisName",
122 "Util.StringProvider", 0);
123 services_.registerUsesPort("TheoryName",
124 "Util.StringProvider", 0);
125 services_.registerUsesPort("MoleculeFile",
126 "Util.StringProvider", 0);
127 services_.registerUsesPort("MoleculeFactory",
128 "Chemistry.MoleculeFactory", 0);
129 services_.registerUsesPort("IntegralEvaluatorFactory",
130 "Chemistry.QC.GaussianBasis.IntegralEvaluatorFactory",0);
131 }
132 catch (gov::cca::CCAException e) {
133 std::cout << "Error using services: "
134 << e.getNote() << std::endl;
135 }
136
137 molecule_ = 0;
138
139 // setup parameters
140 try {
141
142 if (services_._not_nil()) {
143 gov::cca::TypeMap tm = services_.createTypeMap();
144 services_.registerUsesPort("classicParam",
145 "gov.cca.ParameterPortFactoryService",tm);
146 gov::cca::Port p = services_.getPort("classicParam");
147 ccaffeine::ports::PortTranslator portX = p;
148 if(portX._not_nil()) {
149 classic::gov::cca::Port *cp
150 =static_cast<classic::gov::cca::Port*>(portX.getClassicPort());
151 if(!cp) {
152 std::cout << "Couldn't get classic port" << std::endl;
153 return;
154 }
155 ConfigurableParameterFactory *cpf
156 = dynamic_cast<ConfigurableParameterFactory *>(cp);
157 ConfigurableParameterPort *pp = setup_parameters(cpf);
158 classic::gov::cca::Port *clscp
159 = dynamic_cast<classic::gov::cca::Port*>(pp);
160 if (!clscp) {
161 std::cout << "Couldn't cast to classic::gov::cca::Port"
162 << std::endl;
163 }
164 void *vp = static_cast<void*>(clscp);
165 ccaffeine::ports::PortTranslator provideX
166 = ccaffeine::ports::PortTranslator::createFromClassic(vp);
167
168 services_.addProvidesPort(provideX,
169 "configure", "ParameterPort", tm);
170
171 services_.releasePort("classicParam");
172 services_.unregisterUsesPort("classicParam");
173 }
174 }
175
176 }
177 catch(std::exception& e) {
178 std::cout << "Exception caught: " << e.what() << std::endl;
179 }
180
181 // DO-NOT-DELETE splicer.end(MPQC.Chemistry_QC_ModelFactory.setServices)
182}
183
184/**
185 * Set the theory name for Model's created with get_model.
186 * @param theory A string giving the name of the theory, for example, B3LYP.
187 */
188void
189MPQC::Chemistry_QC_ModelFactory_impl::set_theory (
190 /* in */ const ::std::string& theory )
191throw ()
192{
193 // DO-NOT-DELETE splicer.begin(MPQC.Chemistry_QC_ModelFactory.set_theory)
194 theory_ = theory;
195 // DO-NOT-DELETE splicer.end(MPQC.Chemistry_QC_ModelFactory.set_theory)
196}
197
198/**
199 * Set the basis set name for Model's created with get_model.
200 * @param basis The basis set name to use, for example, aug-cc-pVDZ.
201 */
202void
203MPQC::Chemistry_QC_ModelFactory_impl::set_basis (
204 /* in */ const ::std::string& basis )
205throw ()
206{
207 // DO-NOT-DELETE splicer.begin(MPQC.Chemistry_QC_ModelFactory.set_basis)
208 basis_ = basis;
209 // DO-NOT-DELETE splicer.end(MPQC.Chemistry_QC_ModelFactory.set_basis)
210}
211
212/**
213 * Set the Molecule to use for Model's created with get_model.
214 * @param molecule An object of type Molecule.
215 */
216void
217MPQC::Chemistry_QC_ModelFactory_impl::set_molecule (
218 /* in */ ::Chemistry::Molecule molecule )
219throw ()
220{
221 // DO-NOT-DELETE splicer.begin(MPQC.Chemistry_QC_ModelFactory.set_molecule)
222 molecule_ = molecule;
223 // DO-NOT-DELETE splicer.end(MPQC.Chemistry_QC_ModelFactory.set_molecule)
224}
225
226/**
227 * Set the object to use to compute integrals for Model's created with get_model.
228 *
229 * @param intfact An object of type GaussianBasis.IntegralEvaluatorFactory.
230 */
231void
232MPQC::Chemistry_QC_ModelFactory_impl::set_integral_factory (
233 /* in */ ::Chemistry::QC::GaussianBasis::IntegralEvaluatorFactory intfact )
234throw ()
235{
236 // DO-NOT-DELETE splicer.begin(MPQC.Chemistry_QC_ModelFactory.set_integral_factory)
237 // insert implementation here
238 // DO-NOT-DELETE splicer.end(MPQC.Chemistry_QC_ModelFactory.set_integral_factory)
239}
240
241/**
242 * Returns a newly created Model. Before get_model can be called, set_theory,
243 * set_basis, and set_molecule must be called.
244 * @return The new Model instance.
245 */
246::Chemistry::QC::Model
247MPQC::Chemistry_QC_ModelFactory_impl::get_model ()
248throw (
249 ::sidl::BaseException
250)
251{
252 // DO-NOT-DELETE splicer.begin(MPQC.Chemistry_QC_ModelFactory.get_model)
253
254 int i;
255
256 /*
257 Currently two possibilities for molecule specification:
258 1) we are using python GUI, set_molecule() has already been called
259 and !molecule evaluates to FALSE
260 2) we are using caffeine proper, we execute the following block to get
261 a molecule from the molecule factory
262 !!MOLECULE NOT ALLOWED IN KEYVAL INPUT FILE!!
263 */
264
265 if( !molecule_ ) {
266 molecule_filename_ =
267 std::string( molecule_filename_param_->getValueString() );
268 molecule_factory_ = services_.getPort("MoleculeFactory");
269 molecule_factory_.set_molecule_filename(molecule_filename_);
270 molecule_ = molecule_factory_.get_molecule();
271 }
272
273 std::ostringstream input;
274
275 // form molecule section of keyval
276 // we do not allow a molecule in keyval input files
277 double conv = molecule_.get_units().convert_to("bohr");
278 input
279 << " molecule<Molecule>: (" << std::endl
280 << " symmetry = auto" << std::endl
281 << " unit = bohr" << std::endl
282 << " {n atoms geometry } = {" << std::endl;
283 for(i=0;i<molecule_.get_n_atom();++i) {
284 input << setprecision(16);
285 input << "\t" << i << "\t" << molecule_.get_atomic_number(i)
286 << "\t[ " << molecule_.get_cart_coor(i,0)*conv
287 << " " << molecule_.get_cart_coor(i,1)*conv
288 << " " << molecule_.get_cart_coor(i,2)*conv << " ]\n";
289 }
290 input << " }\n";
291 input << " )" << std::endl;
292
293 /*
294 Currently two possibilities for obtaining model keyval:
295 1) theory and basis are supplied by built-in parameter port
296 and we can construct a simple model keyval input
297 2) keyval filename is supplied for us to read from
298 */
299 std::string keyval_filename =
300 std::string(keyval_filename_param_->getValueString());
301 if( keyval_filename.size() > 0 ) {
302 ifstream infile(keyval_filename.c_str());
303 if( !infile ) {
304 std::cout << "\nerror: could not open keyval file\n";
305 abort();
306 }
307 int i;
308 while( (i=infile.get()) && i!=EOF )
309 input << char(i);
310 }
311 else {
312
313 theory_ = std::string( theory_param_->getValueString() );
314 basis_ = std::string( basis_param_->getValueString() );
315
316 if (theory_ == "HF") {
317 input << " model<CLHF>:(" << std::endl;
318 }
319 else if (theory_ == "B3LYP") {
320 input << " model<CLKS>:(" << std::endl;
321 input << " functional<StdDenFunctional>:(name=B3LYP)" << std::endl;
322 }
323 else {
324 std::cout << "bad theory" << std::endl;
325 abort();
326 }
327
328 input << " molecule=$:molecule" << std::endl
329 << " basis<GaussianBasisSet>:(" << std::endl
330 << " name = \"" << basis_ << "\"" << std::endl
331 << " molecule = $..:molecule" << std::endl
332 << " )" << std::endl << " )" << std::endl;
333 }
334
335 // currently needed for integrals stuff
336 if( basis_.size() == 0 )
337 basis_ = std::string( basis_param_->getValueString() );
338
339 std::cout << " model input:" << std::endl << input.str() << std::endl;
340
341 // hook into integrals component (optional)
342 try { eval_factory_ = services_.getPort("IntegralEvaluatorFactory"); }
343 catch (...) {}
344 if( eval_factory_._not_nil() ) {
345 bool use_opaque;
346 std::string buffer_str = std::string(integral_buffer_param_->getValueString());
347 if( buffer_str == "opaque") use_opaque=true;
348 else if(buffer_str == "array") use_opaque=false;
349 else { std::cerr << "\bunrecognized integral buffer option"; abort(); }
350 intcca_ = new IntegralCCA(eval_factory_,use_opaque);
351 eval_factory_.set_molecule(molecule_);
352 eval_factory_.set_integral_package("intv3");
353 Integral::set_default_integral( Ref<Integral>(intcca_.pointer()) );
354 }
355
356 MPQC::Chemistry_QC_Model model = MPQC::Chemistry_QC_Model::_create();
357 model.initialize_parsedkeyval("model",input.str());
358 model.set_molecule(molecule_);
359
360 return model;
361
362 // DO-NOT-DELETE splicer.end(MPQC.Chemistry_QC_ModelFactory.get_model)
363}
364
365/**
366 * This can be called when this Model object is no longer needed. No other
367 * members may be called after finalize.
368 */
369int32_t
370MPQC::Chemistry_QC_ModelFactory_impl::finalize ()
371throw ()
372
373{
374 // DO-NOT-DELETE splicer.begin(MPQC.Chemistry_QC_ModelFactory.finalize)
375 if (molecule_factory_._not_nil())
376 services_.releasePort("MoleculeFactory");
377 return 0;
378 // DO-NOT-DELETE splicer.end(MPQC.Chemistry_QC_ModelFactory.finalize)
379}
380
381
382// DO-NOT-DELETE splicer.begin(MPQC.Chemistry_QC_ModelFactory._misc)
383
384ConfigurableParameterPort *
385MPQC::Chemistry_QC_ModelFactory_impl::setup_parameters(ConfigurableParameterFactory *cpf)
386{
387 ConfigurableParameterPort * pp = cpf->createConfigurableParameterPort();
388
389 pp->setBatchTitle("PortTranslatorStarter Configuration");
390 pp->setGroupName("Model Factory Input");
391
392 theory_param_ = new StringParameter("theory", "Theory name",
393 "theory", "HF");
394 basis_param_ = new StringParameter("basis", "AO basis name",
395 "basis", "STO-3G");
396 molecule_filename_param_ =
397 new StringParameter("molecule_filename",
398 "Molecule filename",
399 "molecule_filename", "");
400 keyval_filename_param_ =
401 new StringParameter("keyval_filename",
402 "Keyval input filename",
403 "keyval_filename", "");
404 /* integral buffer method: opaque or (sidl) array */
405 integral_buffer_param_ =
406 new StringParameter("integral_buffer",
407 "Integral buffer method",
408 "integral_buffer", "opaque");
409
410 pp->addRequest(theory_param_);
411 pp->addRequest(basis_param_);
412 pp->addRequest(molecule_filename_param_);
413 pp->addRequest(keyval_filename_param_);
414 pp->addRequest(integral_buffer_param_);
415
416 return pp;
417}
418
419// DO-NOT-DELETE splicer.end(MPQC.Chemistry_QC_ModelFactory._misc)
420
Note: See TracBrowser for help on using the repository browser.