| 1 | /* Project: MoleCuilder | 
|---|
| 2 | * Description: creates and alters molecular systems | 
|---|
| 3 | * Copyright (C)  2017 Frederik Heber. All rights reserved. | 
|---|
| 4 | * | 
|---|
| 5 | * | 
|---|
| 6 | *   This file is part of MoleCuilder. | 
|---|
| 7 | * | 
|---|
| 8 | *    MoleCuilder is free software: you can redistribute it and/or modify | 
|---|
| 9 | *    it under the terms of the GNU General Public License as published by | 
|---|
| 10 | *    the Free Software Foundation, either version 2 of the License, or | 
|---|
| 11 | *    (at your option) any later version. | 
|---|
| 12 | * | 
|---|
| 13 | *    MoleCuilder is distributed in the hope that it will be useful, | 
|---|
| 14 | *    but WITHOUT ANY WARRANTY; without even the implied warranty of | 
|---|
| 15 | *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the | 
|---|
| 16 | *    GNU General Public License for more details. | 
|---|
| 17 | * | 
|---|
| 18 | *    You should have received a copy of the GNU General Public License | 
|---|
| 19 | *    along with MoleCuilder.  If not, see <http://www.gnu.org/licenses/>. | 
|---|
| 20 | */ | 
|---|
| 21 |  | 
|---|
| 22 | /** \file MPCJob_Work.cpp | 
|---|
| 23 | * | 
|---|
| 24 | * Function implementations for the class vector. | 
|---|
| 25 | * | 
|---|
| 26 | */ | 
|---|
| 27 |  | 
|---|
| 28 | // include config.h | 
|---|
| 29 | #ifdef HAVE_CONFIG_H | 
|---|
| 30 | #include <config.h> | 
|---|
| 31 | #endif | 
|---|
| 32 |  | 
|---|
| 33 | #include <sstream> | 
|---|
| 34 |  | 
|---|
| 35 | #include "MPQCJob.hpp" | 
|---|
| 36 |  | 
|---|
| 37 | #include "CodePatterns/Log.hpp" | 
|---|
| 38 |  | 
|---|
| 39 | #include <boost/archive/text_oarchive.hpp> | 
|---|
| 40 | #ifdef HAVE_JOBMARKET | 
|---|
| 41 | // include headers that implement a archive in simple text format | 
|---|
| 42 | // otherwise BOOST_CLASS_EXPORT_IMPLEMENT has no effect | 
|---|
| 43 | #include <boost/archive/text_iarchive.hpp> | 
|---|
| 44 | #include <boost/tokenizer.hpp> | 
|---|
| 45 |  | 
|---|
| 46 | #include "JobMarket/Results/FragmentResult.hpp" | 
|---|
| 47 |  | 
|---|
| 48 | #include <fstream> | 
|---|
| 49 | #include <iostream> | 
|---|
| 50 | #include <stdlib.h> | 
|---|
| 51 |  | 
|---|
| 52 | #include <util/class/scexception.h> | 
|---|
| 53 | #include <util/group/message.h> | 
|---|
| 54 |  | 
|---|
| 55 | #include "mpqc.h" | 
|---|
| 56 |  | 
|---|
| 57 | using namespace std; | 
|---|
| 58 |  | 
|---|
| 59 | using namespace sc; | 
|---|
| 60 |  | 
|---|
| 61 | FragmentResult::ptr MPQCJob::Work() | 
|---|
| 62 | { | 
|---|
| 63 | char mpqc[] = "mpqc" ; | 
|---|
| 64 | char **argv = new char*[1]; | 
|---|
| 65 | argv[0] = &mpqc[0]; | 
|---|
| 66 | int argc = 1; | 
|---|
| 67 |  | 
|---|
| 68 | // now comes the actual work | 
|---|
| 69 | mpqc::InitValues initvalues; | 
|---|
| 70 | int nfilebase = (int) inputfile.length(); | 
|---|
| 71 | initvalues.in_char_array = new char[nfilebase + 1]; | 
|---|
| 72 | strncpy(initvalues.in_char_array, inputfile.c_str(), nfilebase); | 
|---|
| 73 | initvalues.in_char_array[nfilebase] = '\0'; | 
|---|
| 74 | initvalues.grp = MessageGrp::get_default_messagegrp(); | 
|---|
| 75 | initvalues.verbose = verbose; | 
|---|
| 76 | // create unique, temporary name and check whether it exists | 
|---|
| 77 | char *tempfilename = NULL; | 
|---|
| 78 | { | 
|---|
| 79 | std::ifstream test; | 
|---|
| 80 | do { | 
|---|
| 81 | if (initvalues.output != NULL) // free buffer from last round | 
|---|
| 82 | delete initvalues.output; | 
|---|
| 83 | char filename_template[] = "mpqc_temp_XXXXXX\0"; | 
|---|
| 84 | char filename_suffix[] = ".in\0"; | 
|---|
| 85 | tempfilename = (char *) malloc ( (strlen(filename_template)+strlen(filename_suffix)+2)*(sizeof(char))); | 
|---|
| 86 | strncpy(tempfilename, mktemp(filename_template), strlen(filename_template)); | 
|---|
| 87 | tempfilename[strlen(filename_template)] = '\0'; | 
|---|
| 88 | strncat(tempfilename, filename_suffix, strlen(filename_suffix)); | 
|---|
| 89 | initvalues.output = tempfilename; | 
|---|
| 90 | //free(tempfilename); // don't free! output takes over pointer! | 
|---|
| 91 | test.open(initvalues.output); | 
|---|
| 92 | } while (test.good()); // test whether file does not(!) exist | 
|---|
| 93 | test.close(); | 
|---|
| 94 | } | 
|---|
| 95 | // put info how to sample the density into MPQCData | 
|---|
| 96 | MPQCData data(grid); | 
|---|
| 97 | data.DoLongrange = DoLongrange; // set whether we sample the density | 
|---|
| 98 | data.DoValenceOnly = DoValenceOnly; // set whether we sample just valence electron and nuclei densities | 
|---|
| 99 | // now call work horse | 
|---|
| 100 | try { | 
|---|
| 101 | mpqc::mainFunction(initvalues, argc, argv, static_cast<void *>(&data)); | 
|---|
| 102 | } | 
|---|
| 103 | catch (SCException &e) { | 
|---|
| 104 | cout << argv[0] << ": ERROR: SC EXCEPTION RAISED:" << endl | 
|---|
| 105 | << e.what() | 
|---|
| 106 | << endl; | 
|---|
| 107 | detail::clean_up(); | 
|---|
| 108 | } | 
|---|
| 109 |  | 
|---|
| 110 | //delete[] in_char_array; // is deleted in mainFunction() | 
|---|
| 111 | if (initvalues.output != 0) { | 
|---|
| 112 | free(tempfilename); | 
|---|
| 113 | } | 
|---|
| 114 | delete[] argv; | 
|---|
| 115 | initvalues.grp = NULL; | 
|---|
| 116 |  | 
|---|
| 117 | // place into returnstream | 
|---|
| 118 | std::stringstream returnstream; | 
|---|
| 119 | boost::archive::text_oarchive oa(returnstream); | 
|---|
| 120 | oa << data; | 
|---|
| 121 |  | 
|---|
| 122 | FragmentResult::ptr s( new FragmentResult(getId(), returnstream.str()) ); | 
|---|
| 123 | if (s->exitflag != 0) | 
|---|
| 124 | cerr << "Job #" << s->getId() << " failed to reach desired accuracy." << endl; | 
|---|
| 125 |  | 
|---|
| 126 | return s; | 
|---|
| 127 | } | 
|---|
| 128 | #else | 
|---|
| 129 | FragmentResult::ptr MPQCJob::Work() | 
|---|
| 130 | { | 
|---|
| 131 | // instantiate empty data | 
|---|
| 132 | const MPQCData data; | 
|---|
| 133 | std::stringstream returnstream; | 
|---|
| 134 | boost::archive::text_oarchive oa(returnstream); | 
|---|
| 135 | oa << data; | 
|---|
| 136 |  | 
|---|
| 137 | // create the result | 
|---|
| 138 | FragmentResult::ptr ptr( new FragmentResult(getId(), returnstream.str()) ); | 
|---|
| 139 | if (data.desired_accuracy != 0.) { | 
|---|
| 140 | ptr->exitflag = data.accuracy < data.desired_accuracy ? 0 : 1; | 
|---|
| 141 | if (ptr->exitflag != 0) | 
|---|
| 142 | ELOG(1, "Job #" << ptr->getId() << " failed to reach desired accuracy."); | 
|---|
| 143 | } else { | 
|---|
| 144 | LOG(3, "INFO: No precision returned from MPQC job, not checking."); | 
|---|
| 145 | } | 
|---|
| 146 |  | 
|---|
| 147 | return ptr; | 
|---|
| 148 | } | 
|---|
| 149 | #endif | 
|---|
| 150 |  | 
|---|
| 151 | // we need to explicitly instantiate the serialization functions as | 
|---|
| 152 | // its is only serialized through its base class FragmentJob | 
|---|
| 153 | BOOST_CLASS_EXPORT_IMPLEMENT(MPQCJob) | 
|---|