1 //
2 // File:          MPQC_SimpleDriver_Impl.cc
3 // Symbol:        MPQC.SimpleDriver-v0.2
4 // Symbol Type:   class
5 // Babel Version: 0.10.2
6 // Description:   Server-side implementation for MPQC.SimpleDriver
7 //
8 // WARNING: Automatically generated; only changes within splicers preserved
9 //
10 // babel-version = 0.10.2
11 //
12 #include "MPQC_SimpleDriver_Impl.hh"
13 
14 // DO-NOT-DELETE splicer.begin(MPQC.SimpleDriver._includes)
15 #include <iostream>
16 #include <iomanip>
17 #include <fstream>
18 #include <unistd.h>
19 #include <cstdio>
20 
21 using namespace std;
22 // DO-NOT-DELETE splicer.end(MPQC.SimpleDriver._includes)
23 
24 // user-defined constructor.
_ctor()25 void MPQC::SimpleDriver_impl::_ctor() {
26   // DO-NOT-DELETE splicer.begin(MPQC.SimpleDriver._ctor)
27   // Insert-Code-Here {MPQC.SimpleDriver._ctor} (constructor)
28   // DO-NOT-DELETE splicer.end(MPQC.SimpleDriver._ctor)
29 }
30 
31 // user-defined destructor.
_dtor()32 void MPQC::SimpleDriver_impl::_dtor() {
33   // DO-NOT-DELETE splicer.begin(MPQC.SimpleDriver._dtor)
34   // Insert-Code-Here {MPQC.SimpleDriver._dtor} (destructor)
35   // DO-NOT-DELETE splicer.end(MPQC.SimpleDriver._dtor)
36 }
37 
38 // static class initializer.
_load()39 void MPQC::SimpleDriver_impl::_load() {
40   // DO-NOT-DELETE splicer.begin(MPQC.SimpleDriver._load)
41   // Insert-Code-Here {MPQC.SimpleDriver._load} (class initialization)
42   // DO-NOT-DELETE splicer.end(MPQC.SimpleDriver._load)
43 }
44 
45 // user-defined static methods: (none)
46 
47 // user-defined non-static methods:
48 /**
49  * Starts up a component presence in the calling framework.
50  * @param Svc the component instance's handle on the framework world.
51  * Contracts concerning Svc and setServices:
52  *
53  * The component interaction with the CCA framework
54  * and Ports begins on the call to setServices by the framework.
55  *
56  * This function is called exactly once for each instance created
57  * by the framework.
58  *
59  * The argument Svc will never be nil/null.
60  *
61  * Those uses ports which are automatically connected by the framework
62  * (so-called service-ports) may be obtained via getPort during
63  * setServices.
64  */
65 void
setServices(::gov::cca::Services services)66 MPQC::SimpleDriver_impl::setServices (
67   /* in */ ::gov::cca::Services services )
68 throw (
69   ::gov::cca::CCAException
70 ){
71   // DO-NOT-DELETE splicer.begin(MPQC.SimpleDriver.setServices)
72 
73   services_ = services;
74   if (services_._is_nil()) return;
75 
76   try {
77       services_.addProvidesPort(self, "go","gov.cca.ports.GoPort",
78                                 0);
79       services_.registerUsesPort("ModelFactory", "Chemistry.QC.ModelFactory",
80                                  0);
81       }
82   catch (gov::cca::CCAException e) {
83       std::cout << "Error using services: "
84                 << e.getNote() << std::endl;
85   }
86 
87   // setup parameters
88   try {
89 
90     if (services_._not_nil()) {
91       gov::cca::TypeMap tm = services_.createTypeMap();
92       services_.registerUsesPort("classicParam",
93                                  "gov.cca.ParameterPortFactoryService",tm);
94       gov::cca::Port p = services_.getPort("classicParam");
95       ccaffeine::ports::PortTranslator portX = p;
96       if(portX._not_nil()) {
97         classic::gov::cca::Port *cp
98           =static_cast<classic::gov::cca::Port*>(portX.getClassicPort());
99         if(!cp) {
100           std::cout << "Couldn't get classic port" << std::endl;
101           return;
102         }
103 
104         ConfigurableParameterFactory *cpf
105           = dynamic_cast<ConfigurableParameterFactory *>(cp);
106         ConfigurableParameterPort *pp = setup_parameters(cpf);
107         classic::gov::cca::Port *clscp
108           = dynamic_cast<classic::gov::cca::Port*>(pp);
109         if (!clscp) {
110           std::cout << "Couldn't cast to classic::gov::cca::Port"
111                     << std::endl;
112         }
113         void *vp = static_cast<void*>(clscp);
114         ccaffeine::ports::PortTranslator provideX
115           = ccaffeine::ports::PortTranslator::createFromClassic(vp);
116 
117         services_.addProvidesPort(provideX,
118                                   "configure", "ParameterPort", tm);
119 
120         services_.releasePort("classicParam");
121         services_.unregisterUsesPort("classicParam");
122       }
123     }
124 
125   }
126   catch(std::exception& e) {
127     std::cout << "Exception caught: " << e.what() << std::endl;
128   }
129 
130   return;
131 
132   // DO-NOT-DELETE splicer.end(MPQC.SimpleDriver.setServices)
133 }
134 
135 /**
136  * Execute some encapsulated functionality on the component.
137  * Return 0 if ok, -1 if internal error but component may be
138  * used further, and -2 if error so severe that component cannot
139  * be further used safely.
140  */
141 int32_t
go()142 MPQC::SimpleDriver_impl::go ()
143 throw ()
144 
145 {
146   // DO-NOT-DELETE splicer.begin(MPQC.SimpleDriver.go)
147 
148   std::cout << "\nSIMPLE CHEMISTRY COMPONENT DRIVER\n";
149   std::cout << "----------------------------------\n";
150 
151   std::cout << "SIMPLE DRIVER: getting model factory\n";
152   factory_ = services_.getPort("ModelFactory");
153   if (factory_._is_nil()) {
154       std::cout << "didn't get a model factory\n";
155       abort();
156     }
157 
158   std::cout << "SIMPLE DRIVER: getting model\n";
159   model_ = factory_.get_model();
160   if (model_._is_nil()) {
161       std::cout << "error getting model\n";
162       abort();
163   }
164 
165   std::cout << "SIMPLE DRIVER: getting molecule\n";
166   molecule_ = model_.get_molecule();
167   if (molecule_._is_nil()) {
168       std::cout << "error getting molecule\n";
169       abort();
170   }
171 
172   do_grad_ = grad_param_->value;
173 
174   std::cout << "SIMPLE DRIVER: Evaluating energy\n";
175   int num_coord = molecule_.get_n_atom() * 3;
176   sidl::array<double> coor = molecule_.get_coor();
177   double energy = model_.get_energy();
178 
179   sidl::array<double> cartg;
180   if(do_grad_){
181     std::cout << "SIMPLE DRIVER: Evaluating gradient\n";
182     cartg = model_.get_gradient();
183   }
184 
185   // log some results, used for validation
186   char *init_dir = getenv("CCACHEM_RESULTS_DIR");
187   if (init_dir) {
188     chdir(init_dir);
189     ofstream outfile( "results.txt" );
190     outfile << setprecision(9)
191             << setiosflags( ios::showpoint | ios::fixed);
192     if(outfile) {
193       outfile << "FINAL GEOMETRY:\n";
194       for(int i=0; i<num_coord; i+=3) {
195         outfile << setw(20) << coor.get(i)
196                 << setw(20) << coor.get(i+1)
197                 << setw(20) << coor.get(i+2)
198                 << endl;
199       }
200       outfile << endl;
201 
202       outfile << "FINAL ENERGY:\n";
203       outfile << setw(20) << energy << "\n\n";
204 
205       if( do_grad_ ) {
206         outfile << "FINAL GRADIENT:\n";
207         for(int i=0; i<num_coord; i+=3) {
208           outfile << setw(20) << cartg.get(i)
209                   << setw(20) << cartg.get(i+1)
210                   << setw(20) << cartg.get(i+2)
211                   << endl;
212         }
213         outfile << endl;
214       }
215 
216     }
217     else
218       cout << "\n\nCouldn't open results.txt for writing\n\n";
219     outfile.close();
220   }
221     else
222       cout << "\n\nCCACHEM_RESULTS_DIR environment variable not found,\n"
223            << "  don't know where to log results\n\n";
224 
225   if(model_._not_nil())
226     model_.finalize();
227   if(factory_._not_nil())
228     factory_.finalize();
229   services_.releasePort("ModelFactory");
230 
231   return 0;
232 
233   // DO-NOT-DELETE splicer.end(MPQC.SimpleDriver.go)
234 }
235 
236 
237 // DO-NOT-DELETE splicer.begin(MPQC.SimpleDriver._misc)
238 
239 ConfigurableParameterPort *
setup_parameters(ConfigurableParameterFactory * cpf)240 MPQC::SimpleDriver_impl::setup_parameters(ConfigurableParameterFactory *cpf)
241 {
242   ConfigurableParameterPort * pp = cpf->createConfigurableParameterPort();
243 
244   pp->setBatchTitle("PortTranslatorStarter Configuration");
245   pp->setGroupName("IntegralTest Input");
246 
247   grad_param_  = new BoolParameter("do_gradient",
248                                    "Perform gradient evaluation",
249                                    "do_gradient",0);
250   pp->addRequest(grad_param_);
251 
252   return pp;
253 }
254 
255 // DO-NOT-DELETE splicer.end(MPQC.SimpleDriver._misc)
256 
257