Changeset - 3663c597352b
[Not reviewed]
default
0 2 0
Roeland Merks - 15 years ago 2010-05-26 16:40:58
roeland.merks@cwi.nl
Oh, oh... my previous solution to clean and then reconstruct a new initial condition did not work in all cases. Now I am calling Init(leaffile) during startup initialization only if the user is passing a leafML file during startup (-l option).

user: Roeland Merks <roeland.merks@cwi.nl>
branch 'default'
changed src/VirtualLeaf.cpp
changed src/modelcatalogue.cpp
2 files changed with 3 insertions and 2 deletions:
0 comments (0 inline, 0 general)
src/VirtualLeaf.cpp
Show inline comments
 
@@ -319,239 +319,240 @@ TIMESTEP {
 
    WallReaction *wall_f = new WallDynamics();
 
			
 
    mesh.ReactDiffuse_New(transport_f, cellreaction_f, wall_f, par.rd_dt);*/
 
				mesh.ReactDiffuse(par.rd_dt);
 
		
 
    Plot(par.resize_stride);
 
			
 
  }
 
	
 
	
 

	
 
		
 
		
 
  i++;
 
  return mesh.getTime();
 
		
 
}
 
		
 
		
 
				
 
/* Called if a cell is clicked */
 
void Cell::OnClick(QMouseEvent *e) {
 
  /* #ifdef HAVE_QWT
 
	// Launch DataPlot window
 
	QStringList curvenames;
 
    for (int i=0;i<NChem();i++) {
 
		curvenames += QString("Chem #%1").arg(i);
 
	}
 
    PlotDialog *plot = new PlotDialog((Main *)main_window, QString("Monitor for Cell %1").arg(Index()), curvenames);
 
    QObject::connect(this, SIGNAL(ChemMonValue(double, double *)),
 
                     plot, SLOT(AddValue(double,double *)));
 
#endif
 
	*/
 
	//getMesh().plugin->OnClick(*this);
 
}
 
				
 

	
 
/* Custom message handler - Default appends a newline character to the end of each line. */ 
 
 void vlMessageOutput(QtMsgType type, const char *msg)
 
 {
 
     switch (type) {
 
     case QtDebugMsg:
 
       //fprintf(stderr, "Debug: %s\n", msg);
 
       cerr << msg << flush;
 
       break;
 
     case QtWarningMsg:
 
       //fprintf(stderr, "Warning: %s\n", msg);
 
       cerr << "Warning: " << msg << flush;
 
       break;
 
     case QtCriticalMsg:
 
       fprintf(stderr, "Critical: %s\n", msg);
 
       cerr << "Critical: " << msg << flush;
 
       break;
 
     case QtFatalMsg:
 
       //fprintf(stderr, "Fatal: %s\n", msg);
 
       cerr << "Fatal: " << msg << flush;
 
       abort();
 
     }
 
 }
 
				
 

	
 
Parameter par;
 
				
 
int main(int argc,char **argv) {
 
					
 
  try {
 
						
 

	
 
    int c;
 

	
 
						
 
    char *leaffile=0;
 
    char *modelfile=0;
 
						
 
    while (1) {
 
							
 
      //int this_option_optind = optind ? optind : 1;
 
      int option_index = 0;
 
      static struct option long_options[] = {
 
	{"batch", no_argument, NULL, 'b'},
 
	{"leaffile", required_argument, NULL, 'l'},
 
	{"model", required_argument, NULL, 'm'} 
 
      };
 
		
 
      // short option 'p' creates trouble for non-commandline usage on MacOSX. Option -p changed to -P (capital)
 
      static char *short_options = "blm";
 
      c = getopt_long (argc, argv, "bl:m:",
 
		       long_options, &option_index);
 
      if (c == -1)
 
	break;
 
		
 
		
 
      if (c==0) {
 
	printf ("option %s", long_options[option_index].name);
 
	if (optarg)
 
	  printf (" with arg %s", optarg);
 
	printf ("\n");
 
			
 
	c = short_options[option_index];
 
      }
 
		
 
      switch (c) {
 
      case 'b':
 
	cerr << "Running in batch mode\n";
 
	batch=true;
 
	break;
 
				
 
      case 'l':
 
	leaffile=strdup(optarg);
 
	if (!leaffile) {
 
	  throw("Out of memory");
 
	}
 
	printf("Reading leaf state file '%s'\n", leaffile);
 
	break;
 

	
 
      case 'm':
 
	modelfile=strdup(optarg);
 
	if (!modelfile) {
 
	  throw("Out of memory");
 
	}
 
	break;
 
					
 
      case '?':
 
	break;
 
				
 
      default:
 
	printf ("?? getopt returned character code 0%o ??\n", c);
 
      }
 
    }
 
	  
 
	  
 
    if (optind < argc) {
 
      printf ("non-option ARGV-elements: ");
 
      while (optind < argc)
 
	printf ("%s ", argv[optind++]);
 
      printf ("\n");
 
    }
 

	
 
    bool useGUI = !batch;
 
    qInstallMsgHandler(vlMessageOutput); // custom message handler
 
    QApplication app(argc,argv,useGUI);
 
						
 

	
 
    
 
    QPalette tooltippalette = QToolTip::palette();
 
    QColor transparentcolor = QColor(tooltippalette.brush(QPalette::Window).color());
 

	
 
    tooltippalette.setBrush (QPalette::Window, QBrush (transparentcolor) );
 
    QToolTip::setPalette( tooltippalette );
 

	
 
    QGraphicsScene canvas(0,0,8000,6000);
 

	
 
	  
 
    if (useGUI) {
 
         main_window=new Main(canvas, mesh);
 
      if ( QApplication::desktop()->width() > ((Main *)main_window)->width() + 10
 
	   && QApplication::desktop()->height() > ((Main *)main_window)->height() +30 ) {
 

	
 
	((Main *)main_window)->show();
 
	((Main *)main_window)->resize( ((Main *)main_window)->sizeHint());
 
    } else {
 
        ((Main *)main_window)->showMaximized();
 
    }
 
  } else {
 
        main_window=new MainBase(canvas, mesh);
 

	
 
 }
 

	
 
    
 
	  
 
    canvas.setSceneRect(QRectF());
 
    if (!batch) {
 
      QObject::connect( qApp, SIGNAL(lastWindowClosed()), qApp, SLOT(quit()) );
 
    }
 

	
 
	  // Load plugins
 
	  /*QVector<SimPluginInterface *> plugins = LoadPlugins();
 
	  InstallPlugin(plugins[0], main_window);
 
	  
 
	  cerr << "List of models:" << endl;
 
	  foreach (SimPluginInterface *p, plugins) {
 
		  cerr << p->ModelID().toStdString() << endl;
 
	  }
 
	  */
 

	
 
	 	  
 
    // Install model or read catalogue of models
 
    ModelCatalogue model_catalogue(&mesh, useGUI?(Main *)main_window:0,modelfile);
 
    if (useGUI)
 
      model_catalogue.PopulateModelMenu();
 
    model_catalogue.InstallFirstModel();
 
    
 
    mesh.Clean();
 

	
 
    if (leaffile) 
 
    main_window->Init(leaffile);
 
	  
 
    Cell::SetMagnification(1);
 
    Cell::setOffset(0,0);
 
						
 
    main_window->FitLeafToCanvas();
 
							
 
						
 
    main_window->Plot();
 

	
 
						
 

	
 
    if (batch) {
 
      double t=0.;
 
      do {
 
	t = main_window->TimeStep();
 
      } while (t < par.maxt);
 
							
 
    } else
 
      return app.exec();
 
	  
 
						
 
  } catch (const char *message) {
 
    if (batch) { 
 
      cerr << "Exception caught:" << endl;
 
      cerr << message << endl;
 
      abort();
 
    } else {
 
      QString qmess=QString("Exception caught: %1").arg(message);
 
      QMessageBox::critical(0, "Critical Error", qmess);
 
      abort();
 
    }
 
  } catch (ios_base::failure) {
 
    stringstream error_message;
 
    error_message << "I/O failure: " << strerror(errno);
 
    if (batch) {
 
      cerr << error_message.str() <<endl;
 
      abort();
 
    } else {
 
      QString qmess(error_message.str().c_str());
 
      QMessageBox::critical(0, "I/O Error", qmess );
 
      abort();
 
    }
 
  }
 
					
 
}
src/modelcatalogue.cpp
Show inline comments
 
/*
 
 *
 
 *  This file is part of the Virtual Leaf.
 
 *
 
 *  The Virtual Leaf is free software: you can redistribute it and/or modify
 
 *  it under the terms of the GNU General Public License as published by
 
 *  the Free Software Foundation, either version 3 of the License, or
 
 *  (at your option) any later version.
 
 *
 
 *  The Virtual Leaf is distributed in the hope that it will be useful,
 
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
 *  GNU General Public License for more details.
 
 *
 
 *  You should have received a copy of the GNU General Public License
 
 *  along with the Virtual Leaf.  If not, see <http://www.gnu.org/licenses/>.
 
 *
 
 *  Copyright 2010 Roeland Merks.
 
 *
 
 */
 

	
 
#include <string>
 
#include "modelcatalogue.h"
 
#include <QVariant>
 

	
 
static const std::string _module_id("$Id$");
 

	
 
ModelCatalogue::ModelCatalogue(Mesh *_mesh, Main *_mainwin, const char *model=0) {
 
	mesh = _mesh;
 
	mainwin = _mainwin;
 
	if (model) {
 
	  cerr << "Loading model: " << model << endl;
 
		LoadPlugin(model);
 
	} else {
 
	  cerr << "Loading all models." << endl;
 
		LoadPlugins();
 
	}
 
}
 

	
 
void ModelCatalogue::LoadPlugins() {
 
	
 
	QDir pluginDir(QApplication::applicationDirPath()); 
 
	QStringList plugin_filters; // filter for plugins, i.e "*.dll", "*.dylib"
 
#if defined(Q_OS_WIN) 
 
	if (pluginDir.dirName().toLower() =="debug" 
 
		||pluginDir.dirName().toLower() =="release") 
 
		pluginDir.cdUp(); 
 
	plugin_filters << "*.dll";
 
	#elif defined(Q_OS_MAC) 
 
	if (pluginDir.dirName() =="MacOS"){ 
 
		pluginDir.cdUp(); 
 
		pluginDir.cdUp(); 
 
		pluginDir.cdUp(); 
 
	} 
 
	plugin_filters << "*.dylib";
 
#endif 
 
	pluginDir.setNameFilters(plugin_filters);
 

	
 
	if (!pluginDir.cd("models")) {
 
		MyWarning::error("Directory 'models' not found!");
 
	}
 
	
 
	
 
	//QVector<SimPluginInterface *> plugins;
 
	foreach (QString fileName, pluginDir.entryList(QDir::Files)){ 
 
		QPluginLoader loader(pluginDir.absoluteFilePath(fileName)); 
 
		if (SimPluginInterface *plugin = 
 
			qobject_cast<SimPluginInterface *>(loader.instance())) {
 
			models.append(plugin); 
 
		} else {
 
			MyWarning::warning("Could not load plugin %s",fileName.toStdString().c_str());
 
		}
 
	}
 
}
 

	
 
void ModelCatalogue::LoadPlugin(const char *model) {
 

	
 
	
 
  QDir pluginDir(QApplication::applicationDirPath()); 
 
  QStringList plugin_filters; // filter for plugins, i.e "*.dll", "*.dylib"
 
	
 
	
 
#if defined(Q_OS_WIN) 
 
  if (pluginDir.dirName().toLower() =="debug" 
 
      ||pluginDir.dirName().toLower() =="release") 
 
    pluginDir.cdUp(); 
 
  //plugin_filters << "*.dll";
 
#elif defined(Q_OS_MAC) 
 
  if (pluginDir.dirName() =="MacOS"){ 
 
    pluginDir.cdUp(); 
 
    pluginDir.cdUp(); 
 
    pluginDir.cdUp(); 
 
  } 
 
  //plugin_filters << "*.dylib";
 
#endif
 
  plugin_filters << model;
 
  pluginDir.setNameFilters(plugin_filters);
 
	
 
  if (!pluginDir.cd("models")) {
 
    MyWarning::error("Directory 'models' not found!");
 
  }
 
	
 
	
 
  //QVector<SimPluginInterface *> plugins;
 
	
 
  QStringList modelnames=pluginDir.entryList(QDir::Files);
 
  if (modelnames.empty()) {
 
    MyWarning::error("Model %s not found - hint: do not include path in filename.",model);
 
  }
 
  foreach (QString fileName, modelnames){ 
 
    QPluginLoader loader(pluginDir.absoluteFilePath(fileName)); 
 
		
 
    if (SimPluginInterface *plugin = 
 
	qobject_cast<SimPluginInterface *>(loader.instance())) {
 
      models.append(plugin); 
 
      MyWarning::warning("Successfully loaded model %s",fileName.toStdString().c_str());
 
      //MyWarning::warning("Successfully loaded model %s",fileName.toStdString().c_str());
 
    } else {
 
      MyWarning::warning("Could not load plugin %s",fileName.toStdString().c_str());
 
    }
 
  }
 
}
 

	
 
void ModelCatalogue::InstallFirstModel() {
 
	InstallModel(models[0]);
 
}
 
void ModelCatalogue::PopulateModelMenu() {
 
	foreach (SimPluginInterface *model, models) {
 
		QAction *modelaction = new QAction(model->ModelID(), mainwin); 
 
		QVariant data;
 
		data.setValue(model);
 
		modelaction->setData(data);
 
		mainwin->modelmenu->addAction(modelaction);
 
		
 
	}
 
	connect(mainwin->modelmenu, SIGNAL(triggered(QAction *)), this, SLOT(InstallModel(QAction *)) );
 
}	
 

	
 
void ModelCatalogue::InstallModel(QAction *modelaction) {
 
	QVariant data = modelaction->data();
 
	SimPluginInterface *model = data.value<SimPluginInterface *>();
 
	cerr << "You chose model " << model->ModelID().toStdString() << "!\n";
 
	mesh->Clean();
 
	InstallModel(model);
 
}
 

	
 
void ModelCatalogue::InstallModel(SimPluginInterface *plugin) {
 
	
 
  // make sure both main and plugin use the same static datamembers (ncells, nchems...)
 
  plugin->SetCellsStaticDatamembers(CellBase::GetStaticDataMemberPointer());
 
	
 
  mesh->SetSimPlugin(plugin);
 
  Cell::SetNChem(plugin->NChem());
 
  plugin->SetParameters(&par);
 

	
 
  if (mainwin) {
 
    mainwin->RefreshInfoBar();
 
    mainwin->Init(0);
 
  }
 
  //	mesh->StandardInit();
 
	
 
}
0 comments (0 inline, 0 general)