PageRenderTime 28ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 1ms

/source/VIEW/DIALOGS/FDPBDialog.C

https://bitbucket.org/okohlbacher/ball
C | 346 lines | 288 code | 44 blank | 14 comment | 57 complexity | 7b02e3d8aee3637e81b98d33dcd27263 MD5 | raw file
  1. // -*- Mode: C++; tab-width: 2; -*-
  2. // vi: set ts=2:
  3. //
  4. #include <BALL/VIEW/DIALOGS/FDPBDialog.h>
  5. #include <BALL/VIEW/KERNEL/message.h>
  6. #include <BALL/SYSTEM/path.h>
  7. #include <BALL/FORMAT/INIFile.h>
  8. #include <BALL/VIEW/KERNEL/mainControl.h>
  9. #include <BALL/VIEW/KERNEL/threads.h>
  10. #include <BALL/VIEW/DATATYPE/standardDatasets.h>
  11. #include <QtGui/QLineEdit>
  12. #include <QtGui/QPushButton>
  13. #include <QtGui/QRadioButton>
  14. #include <QtGui/QCheckBox>
  15. #include <QtGui/QFileDialog>
  16. namespace BALL
  17. {
  18. namespace VIEW
  19. {
  20. FDPBDialog::FDPBDialog(QWidget* parent, const char* name, bool modal, Qt::WFlags fl)
  21. : QDialog(parent, fl),
  22. Ui_FDPBDialogData(),
  23. ModularWidget("FDPBDialog"),
  24. PreferencesEntry(),
  25. system_(0),
  26. thread_(0)
  27. {
  28. #ifdef BALL_VIEW_DEBUG
  29. Log.error() << "new FDPBDialog " << this << std::endl;
  30. #endif
  31. registerWidget(this);
  32. setINIFileSectionName("FDPB");
  33. setupUi(this);
  34. setObjectName(name);
  35. setModal(modal);
  36. // signals and slots connections
  37. connect( radii_data_browse, SIGNAL( clicked() ), this, SLOT( browseRadiiData() ) );
  38. connect( radii_rules_browse, SIGNAL( clicked() ), this, SLOT( browseRadiiRules() ) );
  39. connect( charges_rules_browse, SIGNAL( pressed() ), this, SLOT( browseChargesRules() ) );
  40. connect( charges_data_browse, SIGNAL( clicked() ), this, SLOT( browseChargesData() ) );
  41. registerWidgets_();
  42. }
  43. FDPBDialog::~FDPBDialog()
  44. {
  45. #ifdef BALL_VIEW_DEBUG
  46. Log.info() << "Destructing object " << this << " of class FDPBDialog" << std::endl;
  47. #endif
  48. if (thread_ != 0)
  49. {
  50. if (thread_->isRunning()) thread_->terminate();
  51. if (thread_->isRunning()) thread_->wait();
  52. delete thread_;
  53. }
  54. }
  55. // ------------------------- SLOTS ------------------------------------------------
  56. // --------------------------------------------------------------------------------
  57. void FDPBDialog::browseChargesData()
  58. {
  59. selectFile_(*charges_data_lineedit);
  60. }
  61. void FDPBDialog::browseChargesRules()
  62. {
  63. selectFile_(*charges_rules_lineedit);
  64. }
  65. void FDPBDialog::browseRadiiData()
  66. {
  67. selectFile_(*radii_data_lineedit);
  68. }
  69. void FDPBDialog::browseRadiiRules()
  70. {
  71. selectFile_(*radii_rules_lineedit);
  72. }
  73. void FDPBDialog::accept()
  74. {
  75. QDialog::accept();
  76. calculate();
  77. }
  78. void FDPBDialog::clicked(QAbstractButton* button)
  79. {
  80. if(buttonBox->buttonRole(button) == QDialogButtonBox::ResetRole)
  81. {
  82. resetPressed();
  83. }
  84. }
  85. void FDPBDialog::resetPressed()
  86. {
  87. restoreDefaultValues();
  88. }
  89. // ------------------------- helper functions -------------------------------------
  90. // --------------------------------------------------------------------------------
  91. void FDPBDialog::selectFile_(QLineEdit& lineedit)
  92. {
  93. QString s = QFileDialog::getOpenFileName(
  94. 0,
  95. "Choose a file",
  96. "",
  97. "");
  98. if (s == QString::null) return;
  99. lineedit.setText(s);
  100. }
  101. bool FDPBDialog::calculate()
  102. {
  103. if (system_ == 0) system_ = getMainControl()->getSelectedSystem();
  104. if (system_ == 0)
  105. {
  106. Log.error() << "No system given! Aborting..." << std::endl;
  107. return false;
  108. }
  109. if (!lockComposites())
  110. {
  111. setStatusbarText(tr("Can not calculate FDPB, since I can not lock the molecular data. Is a simulation running?"), true);
  112. return false;
  113. }
  114. bool use_mt = false;
  115. // currently doesnt work:
  116. #ifdef BALL_QT_HAS_THREADS
  117. if (getMainControl()->useMultithreading())
  118. {
  119. use_mt = true;
  120. if (thread_ == 0)
  121. {
  122. thread_ = new CalculateFDPBThread();
  123. }
  124. else
  125. {
  126. if (thread_->isRunning())
  127. {
  128. Log.error() << "Thread already running in" << " " << __FILE__ << " " << __LINE__<< std::endl;
  129. return false;
  130. }
  131. }
  132. thread_->setFDPBDialog(this);
  133. thread_->start();
  134. Position pos = 3;
  135. QString dots;
  136. Position i = 0;
  137. while (thread_->isRunning())
  138. {
  139. setStatusbarText(tr("Calculating FDPB grid ") + dots, false);
  140. qApp->processEvents();
  141. if (i > 10)
  142. {
  143. if (pos < 40)
  144. {
  145. pos ++;
  146. dots +=".";
  147. }
  148. else
  149. {
  150. pos = 3;
  151. dots = "...";
  152. }
  153. i = 0;
  154. }
  155. i++;
  156. thread_->wait(10);
  157. }
  158. }
  159. #endif
  160. if (!use_mt) calculate_();
  161. setStatusbarText(tr("Finished FDPB grid"), true);
  162. RegularData3DDataset* set = new RegularData3DDataset();
  163. set->setData(fdpb_.phi_grid);
  164. set->setComposite(system_);
  165. set->setName("FDPB_" + system_->getName());
  166. set->setType(RegularData3DController::type);
  167. DatasetMessage* message = new DatasetMessage(set, DatasetMessage::ADD);
  168. notify_(message);
  169. fdpb_.phi_grid = 0;
  170. system_ = 0;
  171. unlockComposites();
  172. return true;
  173. }
  174. void FDPBDialog::calculate_()
  175. {
  176. applyValues_();
  177. applyProcessors_();
  178. fdpb_.setup(*system_, options_);
  179. fdpb_.solve();
  180. }
  181. void FDPBDialog::applyValues_()
  182. {
  183. /// ------------------------------
  184. options_[FDPB::Option::SOLVENT_DC] = dc_solvent->text().toFloat();
  185. options_[FDPB::Option::SOLUTE_DC] = dc_interior->text().toFloat();
  186. options_[FDPB::Option::PROBE_RADIUS] = probe_radius->text().toFloat();
  187. options_[FDPB::Option::IONIC_STRENGTH] = ionic_strenght->text().toFloat();
  188. options_[FDPB::Option::ION_RADIUS] = ion_radius->text().toFloat();
  189. options_[FDPB::Option::SPACING] = spacing->text().toFloat();
  190. options_[FDPB::Option::BORDER] = border->text().toFloat();
  191. options_[FDPB::Option::TEMPERATURE] = temperature->text().toFloat();
  192. options_[FDPB::Option::MAX_ITERATIONS] = max_iterations->text().toFloat();
  193. /// ------------------------------
  194. if (boundary_zero->isChecked())
  195. {
  196. options_[FDPB::Option::BOUNDARY] = FDPB::Boundary::ZERO;
  197. }
  198. else if (boundary_debye->isChecked())
  199. {
  200. options_[FDPB::Option::BOUNDARY] = FDPB::Boundary::DEBYE;
  201. }
  202. else if (boundary_coulomb->isChecked())
  203. {
  204. options_[FDPB::Option::BOUNDARY] = FDPB::Boundary::COULOMB;
  205. }
  206. else if (boundary_dipole->isChecked())
  207. {
  208. options_[FDPB::Option::BOUNDARY] = FDPB::Boundary::DIPOLE;
  209. }
  210. else if (boundary_focusing->isChecked())
  211. {
  212. options_[FDPB::Option::BOUNDARY] = FDPB::Boundary::FOCUSING;
  213. }
  214. /// ------------------------------
  215. if (smoothing_none->isChecked())
  216. {
  217. options_[FDPB::Option::DIELECTRIC_SMOOTHING] = FDPB::DielectricSmoothing::NONE;
  218. }
  219. else if (smoothing_uniform->isChecked())
  220. {
  221. options_[FDPB::Option::DIELECTRIC_SMOOTHING] = FDPB::DielectricSmoothing::UNIFORM;
  222. }
  223. else if (smoothing_harmonic->isChecked())
  224. {
  225. options_[FDPB::Option::DIELECTRIC_SMOOTHING] = FDPB::DielectricSmoothing::HARMONIC;
  226. }
  227. /// ------------------------------
  228. if (charge_uniform->isChecked())
  229. {
  230. options_[FDPB::Option::CHARGE_DISTRIBUTION] = FDPB::ChargeDistribution::UNIFORM;
  231. }
  232. else if (charge_trilinear->isChecked())
  233. {
  234. options_[FDPB::Option::CHARGE_DISTRIBUTION] = FDPB::ChargeDistribution::TRILINEAR;
  235. }
  236. options_[FDPB::Option::VERBOSITY] = 100;
  237. }
  238. bool FDPBDialog::applyProcessors_()
  239. {
  240. if (system_ == 0)
  241. {
  242. Log.error() << "No system given! Aborting..." << std::endl;
  243. return false;
  244. }
  245. if (add_hydrogens->isChecked())
  246. {
  247. if (!system_->apply(getFragmentDB().add_hydrogens)) return false;
  248. if (!system_->apply(getFragmentDB().normalize_names)) return false;
  249. }
  250. else if (normalize_names->isChecked())
  251. {
  252. if (!system_->apply(getFragmentDB().normalize_names)) return false;
  253. }
  254. if (build_bonds->isChecked())
  255. {
  256. if (!system_->apply(getFragmentDB().build_bonds)) return false;
  257. }
  258. try
  259. {
  260. if (assign_charges->isChecked())
  261. {
  262. if (charges_data_button->isChecked())
  263. {
  264. charge_processor_.setFilename(ascii(charges_data_lineedit->text()));
  265. if (!system_->apply(charge_processor_)) return false;
  266. }
  267. else
  268. {
  269. INIFile inifile(ascii(charges_rules_lineedit->text()));
  270. charge_rule_processor_ = ChargeRuleProcessor(inifile);
  271. if (!system_->apply(charge_rule_processor_)) return false;
  272. }
  273. }
  274. if (assign_radii->isChecked())
  275. {
  276. if (radii_data_button->isChecked())
  277. {
  278. radius_processor_.setFilename(ascii(radii_data_lineedit->text()));
  279. if (!system_->apply(radius_processor_)) return false;
  280. }
  281. else
  282. {
  283. INIFile inifile(ascii(radii_rules_lineedit->text()));
  284. radius_rule_processor_ = RadiusRuleProcessor(inifile);
  285. if (!system_->apply(radius_rule_processor_)) return false;
  286. }
  287. }
  288. }
  289. catch (Exception::FileNotFound& e)
  290. {
  291. Log.error() << "Invalid file " << e.getFilename() << std::endl;
  292. return false;
  293. }
  294. // notify MainControl to update all Representations for the Composite
  295. CompositeMessage* msg = new CompositeMessage(*system_,
  296. CompositeMessage::CHANGED_COMPOSITE,
  297. true);;
  298. qApp->postEvent(getMainControl(), new MessageEvent(msg));
  299. return true;
  300. }
  301. } // namespace VIEW
  302. } // namespace BALL