Main Page | Class Hierarchy | Alphabetical List | Class List | Directories | File List | Class Members | File Members | Related Pages

clustersim.cpp

Go to the documentation of this file.
00001 /***************************************************************************************************
00002 *****           Copyright (C) 2005  John Schneiderman <JohnMS@member.fsf.org>                  *****
00003 *****                                                                                          *****
00004 *****           This program is free software; you can redistribute it and/or modify           *****
00005 *****           it under the terms of the GNU General Public License as published by           *****
00006 *****           the Free Software Foundation; either version 2 of the License, or              *****
00007 *****           (at your option) any later version.                                            *****
00008 *****                                                                                          *****
00009 *****           This program is distributed in the hope that it will be useful,                *****
00010 *****           but WITHOUT ANY WARRANTY; without even the implied warranty of                 *****
00011 *****           MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the                  *****
00012 *****           GNU General Public License for more details.                                   *****
00013 *****                                                                                          *****
00014 *****           You should have received a copy of the GNU General Public License              *****
00015 *****           along with this program; if not, write to the Free Software                    *****
00016 *****           Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA     *****
00017 ***************************************************************************************************/
00018 #ifdef HAVE_CONFIG_H
00019     #include <config.h>
00020 #endif
00021 
00022 #include <iostream>
00023 using std::cout;
00024 using std::cerr;
00025 using std::cin;
00026 using std::endl;
00027 #include <fstream>
00028 using std::ofstream;
00029 
00030 // User interaction classes
00031 #include "menu.h"
00032 #include "configurationdatabase.h"
00033 //Known clusters
00034 #include "fcclattice.h"
00035 //Known simulations
00036 #include "mdsimulation.h"
00037 #include "mcsimulation.h"
00038 #include "mdljsimulation.h"
00039 #include "mcljsimulation.h"
00040 
00047 string createCluster(Cluster &cluster, const ConfigurationDatabase &conf);
00055 string selectSimulation(Cluster &cluster, string prevName, const ConfigurationDatabase &conf);
00061 void interactiveRun(ConfigurationDatabase &conf, Cluster &cluster);
00068 void nonInteractiveRun(ConfigurationDatabase &conf, ConfigurationDatabase runSetup, Cluster &cluster);
00069 
00083 int main(int argc, char *argv[])
00084 {
00085     //Parse command line arguments
00086     string confFile="clustersim.conf";
00087     bool interactive=true;
00088     string nonInteractiveFilename;
00089 
00090     if (argc > 1)
00091         for (int i=1; i < argc; i++)
00092             if (strcmp(argv[i], "-v") == 0)
00093             {
00094                 cout << "ClusterSim version " << VERSION << ", Copyright (C) 2005 John Schneiderman" << endl;
00095                 return EXIT_SUCCESS;
00096             }
00097             else if (strcmp(argv[i], "-c") == 0)
00098                 confFile=argv[++i];
00099             else if (strcmp(argv[i], "-n") == 0)
00100             {
00101                 nonInteractiveFilename=argv[++i];
00102                 interactive=false;
00103             }
00104             else if (strcmp(argv[i], "-h") == 0)
00105             {
00106                 cout << "usage: clustersim [-(v)ersion] [-(h)elp] [-(c)onf file] [-(n)on-interactive file]" << endl;
00107                 return EXIT_SUCCESS;
00108             }
00109             else
00110             {
00111                 cout << argv[i] << " is not an option." << endl;
00112                 cout << "usage: clustersim [-(v)ersion] [-(h)elp] [-(c)onf file] [-(n)on-interactive file]" << endl;
00113                 return EXIT_SUCCESS;
00114             }
00115 
00116     //Display notice
00117     cout << "ClusterSim version " << VERSION << ", Copyright (C) 2005, 2006 John Schneiderman" << endl;
00118     cout << "ClusterSim comes with ABSOLUTELY NO WARRANTY; This is free software," << endl;
00119     cout << "and you are welcome to redistribute it under certain conditions; see" << endl;
00120     cout << "the COPYING file for details, or the Free Software Foundation's GPL." << endl << endl;
00121     try
00122     {
00123         ConfigurationDatabase conf(confFile);
00124         Cluster cluster;
00125 
00126         if (interactive)
00127             interactiveRun(conf, cluster);
00128         else
00129             nonInteractiveRun(conf, ConfigurationDatabase(nonInteractiveFilename), cluster);
00130         return EXIT_SUCCESS;
00131     }
00132     catch (const char *message)
00133     {
00134         cerr << message << endl;
00135         return EXIT_FAILURE;
00136     }
00137     catch (string message)
00138     {
00139         cerr << message << endl;
00140         return EXIT_FAILURE;
00141     }
00142     catch (...)
00143     {
00144         cerr << "Unknown error occured." << endl;
00145         return EXIT_FAILURE;
00146     }
00147 }
00148 
00149 string createCluster(Cluster &cluster, const ConfigurationDatabase &conf)
00150 {
00151     Menu clusterMenu("Cluster Creations", "Enter type of lattice to create: ");
00152     string baseName="";
00153     int baseSize, menuSelected;
00154     double reducedDensity;
00155     FCC_Lattice fcc;
00156 
00157     baseSize=conf.intValue("clusters", "baseSize");
00158     reducedDensity=conf.doubleValue("clusters", "reducedDensity");
00159     clusterMenu.addMenuItem("FCC Lattice");
00160     while(true)
00161     {
00162         clusterMenu.draw();
00163         menuSelected = clusterMenu.selectMenuItem();
00164         if (menuSelected == clusterMenu.menuItem("FCC Lattice"))
00165         {
00166             cout << "Enter base size, or 0 for default (" << baseSize <<  "): ";
00167             cin >> baseSize;
00168             if (baseSize <= 0)
00169                 baseSize=conf.intValue("clusters", "baseSize");
00170             cout << "Enter the reduced density, or 0 for default (" << reducedDensity <<  "): ";
00171             cin >> reducedDensity;
00172             if (reducedDensity <= 0)
00173                 reducedDensity=conf.doubleValue("clusters", "reducedDensity");
00174             fcc.createLattice(baseSize, reducedDensity, cluster);
00175             cout << "Enter base name to save the Cluster to: ";
00176             cin >> baseName;
00177             cluster.write(baseName);
00178             return baseName;
00179         }
00180         else if (menuSelected == Menu::MENU_EXIT)
00181             return "";
00182     }
00183 }
00184 
00185 string selectSimulation(Cluster &cluster, string prevName, const ConfigurationDatabase &conf)
00186 {
00187     Menu simMenu("Simulation Options", "Enter simulation type to run: ");
00188     BaseSimulation *sim;
00189     MC_Simulation *mcSim;
00190     string currentName;
00191     int steps, printInterval, menuSelected;
00192     double changeInSteps=0.0;
00193 
00194     simMenu.addMenuItem("Molecular Dynamic Lenard-Jones Simulation");
00195     simMenu.addMenuItem("Monte Carlo Lenard-Jones Simulation");
00196     // Make sure our cluster is good to go.
00197     if (cluster.size() < 2)
00198     {
00199         simMenu.setFeedback("Cluster is not set up!");
00200         return prevName;
00201     }
00202     // Lets rock
00203     while(true)
00204     {
00205         // Process menu and user's interaction
00206         simMenu.draw();
00207         menuSelected = simMenu.selectMenuItem();
00208         if (menuSelected == simMenu.menuItem("Molecular Dynamic Lenard-Jones Simulation"))
00209         {
00210             sim = new MDLJ_Simulation(conf);
00211             break;
00212         }
00213         else if (menuSelected == simMenu.menuItem("Monte Carlo Lenard-Jones Simulation"))
00214         {
00215             double temp=conf.doubleValue("sims", "temperature");
00216 
00217             mcSim = new MCLJ_Simulation(conf);
00218             cout << "Enter the temperature for this system, 0 for default (" << temp <<  "): ";
00219             cin >> temp;
00220             if (temp == 0)
00221                 temp=conf.doubleValue("sims", "temperature");
00222             mcSim->setTemperature(temp);
00223             sim = mcSim;
00224             break;
00225         }
00226         else if (menuSelected == Menu::MENU_EXIT)
00227             return prevName;
00228     }
00229     // Are we ready to start a simulation?
00230     if (sim != NULL)
00231     {
00232         // Get default values
00233         steps=conf.intValue("sims", "steps");
00234         printInterval=conf.intValue("sims", "printInterval");
00235         changeInSteps=conf.doubleValue("sims", "changeInSteps");
00236         // Lets see if the user wants to change any of those values
00237         cout << "Enter the number of steps for this run, 0 for default (" << steps <<  "): ";
00238         cin >> steps;
00239         if (steps <= 0)
00240             steps=conf.intValue("sims", "steps");
00241         cout << "Enter the interval between steps, 0 for default (" << changeInSteps <<  "): ";
00242         cin >> changeInSteps;
00243         if (changeInSteps <= 0)
00244             changeInSteps=conf.doubleValue("sims", "changeInSteps");
00245         cout << "Enter the print interval, 0 for default (" << printInterval <<  "): ";
00246         cin >> printInterval;
00247         if (printInterval <= 0)
00248             printInterval=conf.intValue("sims", "printInterval");
00249         cout << "Enter the new run name (i.e. run001): ";
00250         cin >> currentName;
00251         // Time to set and run those values
00252         sim->setRunSteps(steps);
00253         sim->setPrintInterval(printInterval);
00254         sim->setChangeInStep(changeInSteps);
00255         sim->run(cluster, currentName, prevName);
00256         delete sim;
00257         return currentName;
00258     }
00259     else
00260     {
00261         return prevName;
00262     }
00263 }
00264 
00265 void interactiveRun(ConfigurationDatabase &conf, Cluster &cluster)
00266 {
00267     Menu mainMenu("ClusterSim Programme", "Please enter selection: ");
00268     string currentRunName="";
00269     int menuSelected;
00270 
00271     //Create and Process menu
00272     mainMenu.addMenuItem("Create a Cluster");
00273     mainMenu.addMenuItem("Run a simulation");
00274     mainMenu.addMenuItem("Load a previous simulation run");
00275     mainMenu.addMenuItem("Save the current simulation run");
00276     mainMenu.addMenuItem("Save settings as defaults");
00277     while(true)
00278     {
00279         mainMenu.draw();
00280         menuSelected = mainMenu.selectMenuItem();
00281         if (menuSelected == mainMenu.menuItem("Create a Cluster"))
00282             currentRunName=createCluster(cluster, conf);
00283         else if (menuSelected == mainMenu.menuItem("Run a simulation"))
00284             currentRunName=selectSimulation(cluster, currentRunName, conf);
00285         else if (menuSelected == mainMenu.menuItem("Load a previous simulation run"))
00286         {
00287             cout << "Enter base name of the run to load: ";
00288             cin >> currentRunName;
00289             try
00290             {
00291                 cluster.read(currentRunName);
00292                 mainMenu.setFeedback("Simulation data successfully read.");
00293             }
00294             catch (const char *errMessage)
00295             {
00296                 mainMenu.setFeedback("File " + currentRunName + " not found.");
00297                 cluster.clear();
00298             }
00299         }
00300         else if (menuSelected == mainMenu.menuItem("Save the current simulation run"))
00301         {
00302             if (cluster.size() > 1)
00303                 try
00304                 {
00305                     cluster.write(currentRunName);
00306                     mainMenu.setFeedback("Simulation data successfully written.");
00307                 }
00308                 catch (const char *errMessage)
00309                 {
00310                     mainMenu.setFeedback("File " + currentRunName + " failed to save.");
00311                 }
00312             else
00313                 mainMenu.setFeedback("Cluster is empty!");
00314         }
00315         else if (menuSelected == mainMenu.menuItem("Save settings as defaults"))
00316         {
00317             string path;
00318 
00319             cout << "Enter path to save the configuration file, or - to return:";
00320             cin >> path;
00321             if (path != "-")
00322             {
00323                 path += "/clustersim.conf";
00324                 ofstream out(path.c_str());
00325                 out << conf;
00326                 mainMenu.setFeedback("Saved Configuration file:" + path);
00327             }
00328         }
00329         else if (menuSelected == Menu::MENU_EXIT)
00330             break;
00331     }
00332 }
00333 
00334 void nonInteractiveRun(ConfigurationDatabase &conf, ConfigurationDatabase runSetup, Cluster &cluster)
00335 {
00336     vector<string> headers=runSetup.headers();
00337     string simType;
00338     BaseSimulation *sim;
00339     MD_Simulation *mdSim;
00340     MC_Simulation *mcSim;
00341 
00342     for (int i=0; i < headers.size(); i++)
00343     {
00344         cout << "Starting simulation " << headers[i] << " ..." << endl;
00345         try
00346         {
00347             cluster.read(runSetup.stringValue(headers[i], "previousName"));
00348         }
00349         catch (const char *errMessage)
00350         {
00351             cerr << errMessage << endl;
00352             return;
00353         }
00354         simType=runSetup.stringValue(headers[i], "simType");
00355         if (simType == "mdlj")
00356         {
00357             try
00358             {
00359                 mdSim = new MDLJ_Simulation(conf);
00360                 sim = mdSim;
00361             }
00362             catch (string message)
00363             {
00364                 cerr << message << endl;
00365                 return;
00366             }
00367         }
00368         else
00369         {
00370             try
00371             {
00372                 mcSim = new MCLJ_Simulation(conf);
00373                 mcSim->setTemperature(runSetup.doubleValue(headers[i], "temperature"));
00374                 sim = mcSim;
00375             }
00376             catch (string message)
00377             {
00378                 cerr << message << endl;
00379                 return;
00380             }
00381         }
00382         sim->setRunSteps(runSetup.intValue(headers[i], "steps"));
00383         sim->setChangeInStep(runSetup.doubleValue(headers[i], "changeInSteps"));
00384         sim->setPrintInterval(runSetup.intValue(headers[i], "printInterval"));
00385         sim->run(cluster, runSetup.stringValue(headers[i], "currentName"), runSetup.stringValue(headers[i], "previousName"));
00386         cluster.write(runSetup.stringValue(headers[i], "currentName"));
00387         cout << "Finished simulation " << headers[i] << "." << endl;
00388         cluster.clear();
00389         if (sim != NULL)
00390             delete sim;
00391     }
00392 }
00393 

Generated on Tue Mar 28 23:28:03 2006 for ClusterSim by  doxygen 1.4.4