Debian Codegraph @

avogadro/qtplugins/lammpsinput/lammpsinputdialog.cpp

raw
1 /******************************************************************************
2 This source file is part of the Avogadro project.
3 This source code is released under the 3-Clause BSD License, (see "LICENSE").
4 ******************************************************************************/
5
6 #include "lammpsinputdialog.h"
7
8 #include <avogadro/core/atom.h>
9 #include <avogadro/core/bond.h>
10 #include <avogadro/core/elements.h>
11
12 #include <avogadro/io/fileformat.h>
13 #include <avogadro/io/fileformatmanager.h>
14
15 #include <avogadro/qtgui/molecule.h>
16
17 #include <QtWidgets/QFileDialog>
18 #include <QtWidgets/QMessageBox>
19 #include <QtWidgets/QTextEdit>
20
21 #include <QtCore/QDebug>
22 #include <QtCore/QDir>
23 #include <QtCore/QFile>
24 #include <QtCore/QHash>
25 #include <QtCore/QPointer>
26 #include <QtCore/QString>
27 #include <QtCore/QTextStream>
28
29 namespace Avogadro::QtPlugins {
30
31 LammpsInputDialog::LammpsInputDialog(QWidget* parent, Qt::WindowFlags flag)
32 : QDialog(parent, flag), m_molecule(nullptr),
33
34 m_unitType(real), m_title("Title"), m_savePath(""), m_dimensionType(d3),
35 m_xBoundaryType(p), m_yBoundaryType(p), m_zBoundaryType(p),
36
37 m_atomStyle(full),
38
39 m_waterPotential(NONE),
40
41 m_ensemble(NVT), m_temperature(298.15), m_nhChain(1),
42
43 m_timeStep(2.0), m_runSteps(50), m_xReplicate(1), m_yReplicate(1),
44 m_zReplicate(1),
45
46 m_dumpStep(1),
47
48 m_velocityDist(gaussian), m_velocityTemp(298.15), m_zeroMOM(true),
49 m_zeroL(true), m_thermoStyle(one), m_thermoInterval(50),
50
51 m_output(), m_dirty(false), m_warned(false), readData(false),
52
53 m_jobEdit(nullptr), m_moleculeEdit(nullptr)
54 {
55 ui.setupUi(this);
56 // Connect the GUI elements to the correct slots
57 connect(ui.titleLine, SIGNAL(editingFinished()), this, SLOT(setTitle()));
58
59 // now for something useful
60 connect(ui.unitsCombo, SIGNAL(currentIndexChanged(int)), this,
61 SLOT(setUnits(int)));
62 connect(ui.atomStyleCombo, SIGNAL(currentIndexChanged(int)), this,
63 SLOT(setAtomStyle(int)));
64 connect(ui.dimensionCombo, SIGNAL(currentIndexChanged(int)), this,
65 SLOT(setDimensionType(int)));
66 connect(ui.xBoundaryCombo, SIGNAL(currentIndexChanged(int)), this,
67 SLOT(setXBoundaryType(int)));
68 connect(ui.yBoundaryCombo, SIGNAL(currentIndexChanged(int)), this,
69 SLOT(setYBoundaryType(int)));
70 connect(ui.zBoundaryCombo, SIGNAL(currentIndexChanged(int)), this,
71 SLOT(setZBoundaryType(int)));
72 connect(ui.waterPotentialCombo, SIGNAL(currentIndexChanged(int)), this,
73 SLOT(setWaterPotential(int)));
74 connect(ui.readDataLine, SIGNAL(editingFinished()), this,
75 SLOT(setReadData()));
76 connect(ui.ensembleCombo, SIGNAL(currentIndexChanged(int)), this,
77 SLOT(setEnsemble(int)));
78 connect(ui.tempSpin, SIGNAL(valueChanged(double)), this,
79 SLOT(setTemperature(double)));
80 connect(ui.nhChainSpin, SIGNAL(valueChanged(int)), this,
81 SLOT(setNHChain(int)));
82 connect(ui.stepSpin, SIGNAL(valueChanged(double)), this,
83 SLOT(setTimeStep(double)));
84 connect(ui.runSpin, SIGNAL(valueChanged(int)), this, SLOT(setRunSteps(int)));
85 connect(ui.xReplicateSpin, SIGNAL(valueChanged(int)), this,
86 SLOT(setXReplicate(int)));
87 connect(ui.yReplicateSpin, SIGNAL(valueChanged(int)), this,
88 SLOT(setYReplicate(int)));
89 connect(ui.zReplicateSpin, SIGNAL(valueChanged(int)), this,
90 SLOT(setZReplicate(int)));
91 connect(ui.dumpXYZEdit, SIGNAL(editingFinished()), this, SLOT(setDumpXYZ()));
92 connect(ui.dumpStepSpin, SIGNAL(valueChanged(int)), this,
93 SLOT(setDumpStep(int)));
94 connect(ui.velocityDistCombo, SIGNAL(currentIndexChanged(int)), this,
95 SLOT(setVelocityDist(int)));
96 connect(ui.velocityTempSpin, SIGNAL(valueChanged(double)), this,
97 SLOT(setVelocityTemp(double)));
98 connect(ui.zeroMOMCheck, SIGNAL(toggled(bool)), this, SLOT(setZeroMOM(bool)));
99 connect(ui.zeroLCheck, SIGNAL(toggled(bool)), this, SLOT(setZeroL(bool)));
100 connect(ui.thermoStyleCombo, SIGNAL(currentIndexChanged(int)), this,
101 SLOT(setThermoStyle(int)));
102 connect(ui.thermoSpin, SIGNAL(valueChanged(int)), this,
103 SLOT(setThermoInterval(int)));
104
105 connect(ui.generateButton, SIGNAL(clicked()), this, SLOT(generateClicked()));
106 connect(ui.resetButton, SIGNAL(clicked()), this, SLOT(resetClicked()));
107
108 connect(ui.enableFormButton, SIGNAL(clicked()), this,
109 SLOT(enableFormClicked()));
110
111 QSettings settings;
112 readSettings(settings);
113
114 // Generate an initial preview of the input deck
115 updatePreviewText();
116 addMoleculeDataTab();
117 }
118
119 LammpsInputDialog::~LammpsInputDialog()
120 {
121 QSettings settings;
122 writeSettings(settings);
123 }
124
125 void LammpsInputDialog::showEvent(QShowEvent*)
126 {
127 updatePreviewText();
128 addMoleculeDataTab();
129 }
130
131 void LammpsInputDialog::updatePreviewText()
132 {
133 if (!isVisible())
134 return;
135
136 int jobTabPosition = 0;
137
138 // Store the currently displayed tab
139 int currIndex = ui.tabWidget->currentIndex();
140
141 // Generate the input deck and display it
142 if (m_dirty) {
143 QString message =
144 tr("Would you like to update the preview text, losing all "
145 "changes made in the Lammps input deck preview pane?");
146 int response = QMessageBox::question(
147 this, tr("Overwrite modified input files?"), message,
148 QMessageBox::Yes | QMessageBox::No, QMessageBox::No);
149
150 if (static_cast<QMessageBox::StandardButton>(response) == QMessageBox::No) {
151 return;
152 }
153 }
154
155 ui.tabWidget->removeTab(jobTabPosition);
156
157 m_jobFileName =
158 (ui.baseNameEdit->text().isEmpty() ? ui.baseNameEdit->placeholderText()
159 : ui.baseNameEdit->text()) +
160 ".lmp";
161 m_jobEdit = new QTextEdit(this);
162 m_jobEdit->setObjectName(m_jobFileName);
163 m_jobEdit->setFontFamily("monospace");
164 connect(m_jobEdit, SIGNAL(textChanged()), this, SLOT(textEditModified()));
165 m_jobEdit->setText(generateInputDeck());
166 ui.tabWidget->insertTab(jobTabPosition, m_jobEdit, m_jobFileName);
167 deckDirty(false);
168
169 // Restore current tab
170 ui.tabWidget->setCurrentIndex(currIndex);
171 }
172
173 void LammpsInputDialog::addMoleculeDataTab()
174 {
175 int molTabPosition = 1;
176 if (m_molecule) {
177 ui.tabWidget->removeTab(molTabPosition);
178 std::string molOutput, extension = "lmpdat";
179 m_moleculeFileName =
180 (ui.baseNameEdit->text().isEmpty() ? ui.baseNameEdit->placeholderText()
181 : ui.baseNameEdit->text()) +
182 "." + QString::fromStdString(extension);
183 bool writeSDF = Io::FileFormatManager::instance().writeString(
184 *m_molecule, molOutput, extension);
185 if (writeSDF) {
186 m_moleculeEdit = new QTextEdit(this);
187 m_moleculeEdit->setObjectName(m_moleculeFileName);
188 m_moleculeEdit->setFontFamily("monospace");
189 m_moleculeEdit->setText(QString::fromStdString(molOutput));
190 ui.tabWidget->insertTab(molTabPosition, m_moleculeEdit,
191 m_moleculeFileName);
192 }
193 }
194 }
195
196 void LammpsInputDialog::textEditModified()
197 {
198 if (auto* edit = qobject_cast<QTextEdit*>(sender())) {
199 if (edit->document()->isModified()) {
200 deckDirty(true);
201 } else {
202 deckDirty(false);
203 }
204 }
205 }
206
207 void LammpsInputDialog::resetClicked()
208 {
209 // Reset the form to defaults
210 deckDirty(false);
211
212 ui.unitsCombo->setCurrentIndex(1);
213 ui.atomStyleCombo->setCurrentIndex(7);
214 ui.dimensionCombo->setCurrentIndex(1);
215 ui.xBoundaryCombo->setCurrentIndex(0);
216 ui.yBoundaryCombo->setCurrentIndex(0);
217 ui.zBoundaryCombo->setCurrentIndex(0);
218 ui.waterPotentialCombo->setCurrentIndex(0);
219 ui.ensembleCombo->setCurrentIndex(0);
220 ui.tempSpin->setValue(298.15);
221 ui.nhChainSpin->setValue(1);
222 ui.stepSpin->setValue(2.0);
223 ui.runSpin->setValue(50);
224 ui.xReplicateSpin->setValue(1);
225 ui.yReplicateSpin->setValue(1);
226 ui.zReplicateSpin->setValue(1);
227 ui.dumpStepSpin->setValue(1);
228 ui.thermoStyleCombo->setCurrentIndex(0);
229 ui.thermoSpin->setValue(50);
230
231 updatePreviewText();
232 addMoleculeDataTab();
233 }
234
235 void LammpsInputDialog::generateClicked()
236 {
237 QSettings settings;
238 QString directory =
239 settings.value("lammpsInput/outputDirectory", QDir::homePath()).toString();
240 if (directory.isEmpty())
241 directory = QDir::homePath();
242 directory = QFileDialog::getExistingDirectory(
243 this, tr("Select output directory"), directory);
244
245 // User cancel:
246 if (directory.isNull())
247 return;
248
249 settings.setValue("lammpsInput/outputDirectory", directory);
250 QDir dir(directory);
251
252 // Check for problems:
253 QStringList errors;
254 bool fatalError = false;
255
256 do { // Do/while to break on fatal errors
257 if (!dir.exists()) {
258 errors << tr("%1: Directory does not exist!").arg(dir.absolutePath());
259 fatalError = true;
260 break;
261 }
262
263 if (!dir.isReadable()) {
264 errors << tr("%1: Directory cannot be read!").arg(dir.absolutePath());
265 fatalError = true;
266 break;
267 }
268
269 QFileInfo jobFileInfo(dir.absoluteFilePath(m_jobFileName));
270
271 if (jobFileInfo.exists()) {
272 errors << tr("%1: File will be overwritten.")
273 .arg(jobFileInfo.absoluteFilePath());
274 }
275
276 // Attempt to open the file for writing
277 if (!QFile(jobFileInfo.absoluteFilePath()).open(QFile::WriteOnly)) {
278 errors
279 << tr("%1: File is not writable.").arg(jobFileInfo.absoluteFilePath());
280 fatalError = true;
281 break;
282 }
283
284 QFileInfo molFileInfo(dir.absoluteFilePath(m_moleculeFileName));
285
286 if (molFileInfo.exists()) {
287 errors << tr("%1: File will be overwritten.")
288 .arg(molFileInfo.absoluteFilePath());
289 }
290
291 // Attempt to open the file for writing
292 if (!QFile(molFileInfo.absoluteFilePath()).open(QFile::WriteOnly)) {
293 errors
294 << tr("%1: File is not writable.").arg(molFileInfo.absoluteFilePath());
295 fatalError = true;
296 break;
297 }
298 } while (false); // only run once
299
300 // Handle fatal errors:
301 if (fatalError) {
302 QString formattedError;
303 switch (errors.size()) {
304 case 0:
305 formattedError =
306 tr("The input files cannot be written due to an unknown error.");
307 break;
308 case 1:
309 formattedError =
310 tr("The input files cannot be written:\n\n%1").arg(errors.first());
311 break;
312 default: {
313 // If a fatal error occurred, it will be last one in the list. Pop it
314 // off and tell the user that it was the reason we had to stop.
315 QString fatal = errors.last();
316 QStringList tmp(errors);
317 tmp.pop_back();
318 formattedError =
319 tr("The input files cannot be written:\n\n%1\n\nWarnings:\n\n%2")
320 .arg(fatal, tmp.join("\n"));
321 break;
322 }
323 }
324 QMessageBox::critical(this, tr("Output Error"), formattedError);
325 return;
326 }
327
328 // Non-fatal errors:
329 if (!errors.isEmpty()) {
330 QString formattedError = tr("Warning:\n\n%1\n\nWould you like to continue?")
331 .arg(errors.join("\n"));
332
333 QMessageBox::StandardButton reply =
334 QMessageBox::warning(this, tr("Write input files"), formattedError,
335 QMessageBox::Yes | QMessageBox::No, QMessageBox::No);
336
337 if (reply != QMessageBox::Yes)
338 return;
339 }
340
341 bool success = false;
342
343 if (m_jobEdit && m_moleculeEdit) {
344 QFile jobFile(dir.absoluteFilePath(m_jobFileName));
345 if (jobFile.open(QFile::WriteOnly | QFile::Text)) {
346 if (jobFile.write(m_jobEdit->toPlainText().toLocal8Bit()) > 0) {
347 success = true;
348 }
349 jobFile.close();
350 }
351
352 if (!success) {
353 QMessageBox::critical(
354 this, tr("Output Error"),
355 tr("Failed to write to file %1.").arg(jobFile.fileName()));
356 }
357
358 QFile molFile(dir.absoluteFilePath(m_moleculeFileName));
359 if (molFile.open(QFile::WriteOnly | QFile::Text)) {
360 if (molFile.write(m_moleculeEdit->toPlainText().toLocal8Bit()) > 0) {
361 success = true;
362 }
363 molFile.close();
364 }
365
366 if (!success) {
367 QMessageBox::critical(
368 this, tr("Output Error"),
369 tr("Failed to write to file %1.").arg(molFile.fileName()));
370 }
371 }
372 }
373
374 void LammpsInputDialog::enableFormClicked()
375 {
376 updatePreviewText();
377 }
378
379 void LammpsInputDialog::setTitle()
380 {
381 m_title = ui.titleLine->text();
382 updatePreviewText();
383 }
384
385 void LammpsInputDialog::setUnits(int n)
386 {
387 m_unitType = (LammpsInputDialog::unitType)n;
388 ui.unitsCombo->setEnabled(true);
389 updatePreviewText();
390 }
391
392 void LammpsInputDialog::setAtomStyle(int n)
393 {
394 m_atomStyle = (LammpsInputDialog::atomStyle)n;
395 ui.atomStyleCombo->setEnabled(true);
396 updatePreviewText();
397 }
398
399 void LammpsInputDialog::setDimensionType(int n)
400 {
401 m_dimensionType = static_cast<LammpsInputDialog::dimensionType>(n);
402 ui.dimensionCombo->setEnabled(true);
403 if (n == 0) {
404 setZBoundaryType(0);
405 ui.zBoundaryCombo->setCurrentIndex(0);
406 ui.zBoundaryCombo->setEnabled(false);
407 ui.zReplicateSpin->setValue(1);
408 ui.zReplicateSpin->setEnabled(false);
409 }
410 if (n == 1) {
411 ui.zBoundaryCombo->setEnabled(true);
412 ui.zReplicateSpin->setEnabled(true);
413 }
414 updatePreviewText();
415 }
416
417 void LammpsInputDialog::setXBoundaryType(int n)
418 {
419 m_xBoundaryType = static_cast<LammpsInputDialog::boundaryType>(n);
420 ui.xBoundaryCombo->setEnabled(true);
421 updatePreviewText();
422 }
423
424 void LammpsInputDialog::setYBoundaryType(int n)
425 {
426 m_yBoundaryType = static_cast<LammpsInputDialog::boundaryType>(n);
427 ui.yBoundaryCombo->setEnabled(true);
428 updatePreviewText();
429 }
430
431 void LammpsInputDialog::setZBoundaryType(int n)
432 {
433 m_zBoundaryType = static_cast<LammpsInputDialog::boundaryType>(n);
434 // should be careful here
435 // z boundary must be p for 2d!!!
436 ui.zBoundaryCombo->setEnabled(true);
437 updatePreviewText();
438 }
439
440 void LammpsInputDialog::setWaterPotential(int n)
441 {
442 m_waterPotential = static_cast<LammpsInputDialog::waterPotential>(n);
443 ui.waterPotentialCombo->setEnabled(true);
444 if (n == 1) {
445 setAtomStyle(7);
446 ui.atomStyleCombo->setCurrentIndex(7);
447 ui.atomStyleCombo->setEnabled(false);
448 }
449 if (n == 0) {
450 ui.atomStyleCombo->setEnabled(true);
451 }
452 updatePreviewText();
453 }
454
455 void LammpsInputDialog::setReadData()
456 {
457 m_readData = ui.readDataLine->text();
458 if (m_readData != "")
459 readData = true;
460 else
461 readData = false;
462 updatePreviewText();
463 }
464
465 void LammpsInputDialog::setMolecule(QtGui::Molecule* molecule)
466 {
467 // Disconnect the old molecule first...
468 if (molecule == m_molecule)
469 return;
470
471 if (m_molecule)
472 m_molecule->disconnect(this);
473
474 m_molecule = molecule;
475 // Update the preview text whenever primitives are changed
476 connect(molecule, SIGNAL(changed(unsigned int)), SLOT(updatePreviewText()));
477 updatePreviewText();
478 }
479
480 void LammpsInputDialog::setEnsemble(int n)
481 {
482 m_ensemble = static_cast<LammpsInputDialog::ensemble>(n);
483 ui.ensembleCombo->setEnabled(true);
484 if (n == 1) {
485 ui.tempSpin->setValue(0.0);
486 ui.tempSpin->setEnabled(false);
487 ui.nhChainSpin->setValue(0);
488 ui.nhChainSpin->setEnabled(false);
489 } else if (n == 0) {
490 ui.tempSpin->setEnabled(true);
491 ui.nhChainSpin->setEnabled(true);
492 ui.nhChainSpin->setValue(1);
493 }
494 updatePreviewText();
495 }
496
497 void LammpsInputDialog::setTemperature(double n)
498 {
499 m_temperature = n;
500 ui.tempSpin->setEnabled(true);
501 updatePreviewText();
502 }
503
504 void LammpsInputDialog::setNHChain(int n)
505 {
506 m_nhChain = n;
507 ui.nhChainSpin->setEnabled(true);
508 updatePreviewText();
509 }
510
511 void LammpsInputDialog::setTimeStep(double n)
512 {
513 m_timeStep = n;
514 ui.stepSpin->setEnabled(true);
515 updatePreviewText();
516 }
517
518 void LammpsInputDialog::setRunSteps(int n)
519 {
520 m_runSteps = n;
521 ui.runSpin->setEnabled(true);
522 updatePreviewText();
523 }
524
525 void LammpsInputDialog::setXReplicate(int n)
526 {
527 m_xReplicate = n;
528 ui.xReplicateSpin->setEnabled(true);
529 updatePreviewText();
530 }
531
532 void LammpsInputDialog::setYReplicate(int n)
533 {
534 m_yReplicate = n;
535 ui.yReplicateSpin->setEnabled(true);
536 updatePreviewText();
537 }
538
539 void LammpsInputDialog::setZReplicate(int n)
540 {
541 m_zReplicate = n;
542 ui.zReplicateSpin->setEnabled(true);
543 updatePreviewText();
544 }
545
546 void LammpsInputDialog::setDumpStep(int n)
547 {
548 m_dumpStep = n;
549 ui.dumpStepSpin->setEnabled(true);
550 updatePreviewText();
551 }
552
553 void LammpsInputDialog::setDumpXYZ()
554 {
555 m_dumpXYZ = ui.dumpXYZEdit->text();
556 updatePreviewText();
557 }
558
559 void LammpsInputDialog::setVelocityDist(int n)
560 {
561 m_velocityDist = static_cast<LammpsInputDialog::velocityDist>(n);
562 ui.velocityDistCombo->setEnabled(true);
563 updatePreviewText();
564 }
565
566 void LammpsInputDialog::setVelocityTemp(double n)
567 {
568 m_velocityTemp = n;
569 ui.velocityTempSpin->setEnabled(true);
570 updatePreviewText();
571 }
572
573 void LammpsInputDialog::setZeroMOM(bool state)
574 {
575 m_zeroMOM = state;
576 ui.zeroMOMCheck->setEnabled(true);
577 updatePreviewText();
578 }
579
580 void LammpsInputDialog::setZeroL(bool state)
581 {
582 m_zeroL = state;
583 ui.zeroLCheck->setEnabled(true);
584 updatePreviewText();
585 }
586
587 void LammpsInputDialog::setThermoStyle(int n)
588 {
589 m_thermoStyle = static_cast<LammpsInputDialog::thermoStyle>(n);
590 ui.thermoStyleCombo->setEnabled(true);
591 updatePreviewText();
592 }
593
594 void LammpsInputDialog::setThermoInterval(int n)
595 {
596 m_thermoInterval = n;
597 ui.thermoSpin->setEnabled(true);
598 updatePreviewText();
599 }
600
601 QString LammpsInputDialog::generateInputDeck()
602 {
603 // Generate an input deck based on the settings of the dialog
604 QString buffer;
605 QTextStream mol(&buffer);
606
607 mol << "# LAMMPS Input file generated by Avogadro\n";
608 mol << "# " << m_title << "\n\n";
609
610 mol << "# Initialization\n";
611 mol << "units " << getUnitType(m_unitType) << "\n";
612 mol << "dimension " << getDimensionType(m_dimensionType) << "\n";
613 mol << "boundary " << getXBoundaryType(m_xBoundaryType) << " "
614 << getYBoundaryType(m_yBoundaryType) << " "
615 << getZBoundaryType(m_zBoundaryType) << "\n";
616 mol << "atom_style " << getAtomStyle(m_atomStyle) << "\n";
617 mol << "\n";
618
619 mol << "# Atom Definition\n";
620 if (readData)
621 mol << "read_data " << m_readData << "\n";
622 mol << "replicate " << m_xReplicate << " " << m_yReplicate << " "
623 << m_zReplicate << "\n";
624
625 mol << "\n" << getWaterPotential(m_waterPotential) << "\n";
626
627 mol << "# Settings\n";
628 mol << "velocity all create " << Qt::fixed << qSetRealNumberPrecision(2)
629 << m_velocityTemp << " "
630 << "4928459 "
631 << "rot " << getZeroL() << " "
632 << "mom " << getZeroMOM() << " "
633 << "dist " << getVelocityDist(m_velocityDist) << "\n";
634 mol << getEnsemble(m_ensemble) << "\n";
635 mol << "timestep " << Qt::fixed << qSetRealNumberPrecision(1)
636 << m_timeStep << "\n";
637 mol << "\n";
638
639 mol << "# Output\n";
640 if (m_dumpXYZ != "") {
641 mol << "dump dumpXYZ all xyz " << m_dumpStep << " " << m_dumpXYZ
642 << "\n";
643 }
644 mol << "thermo_style " << getThermoStyle(m_thermoStyle) << "\n";
645 mol << "thermo " << m_thermoInterval << "\n";
646 mol << "\n";
647
648 mol << "# Run the simulation\n";
649 mol << "run " << m_runSteps << "\n";
650 mol << "\n";
651
652 return buffer;
653 }
654
655 QString LammpsInputDialog::getUnitType(unitType t)
656 {
657 // Translate the enum to text for the output generation
658 switch (t) {
659 case lj:
660 return "lj";
661 case real:
662 return "real";
663 case metal:
664 return "metal";
665 case si:
666 return "si";
667 case cgs:
668 return "cgs";
669 case u_electron:
670 return "electron";
671 default:
672 return "lj";
673 }
674 }
675
676 QString LammpsInputDialog::getAtomStyle(atomStyle t)
677 {
678 switch (t) {
679 case angle:
680 return "angle";
681 case atomic:
682 return "atomic";
683 case bond:
684 return "bond";
685 case charge:
686 return "charge";
687 case dipole:
688 return "dipole";
689 case a_electron:
690 return "electron";
691 case ellipsoid:
692 return "ellipsoid";
693 case full:
694 return "full";
695 case line:
696 return "line";
697 case meso:
698 return "meso";
699 case molecular:
700 return "molecular";
701 case peri:
702 return "peri";
703 case sphere:
704 return "sphere";
705 case tri:
706 return "tri";
707 case wavepacket:
708 return "wavepacket";
709 default:
710 return "full";
711 }
712 }
713
714 QString LammpsInputDialog::getDimensionType(dimensionType t)
715 {
716 switch (t) {
717 case d2:
718 return "2d";
719 case d3:
720 return "3d";
721 default:
722 return "3d";
723 }
724 }
725
726 QString LammpsInputDialog::getXBoundaryType(boundaryType t)
727 {
728 switch (t) {
729 case p:
730 return "p";
731 case f:
732 return "f";
733 case s:
734 return "s";
735 case m:
736 return "m";
737 case fs:
738 return "fs";
739 case fm:
740 return "fm";
741 default:
742 return "p";
743 }
744 }
745
746 QString LammpsInputDialog::getYBoundaryType(boundaryType t)
747 {
748 switch (t) {
749 case p:
750 return "p";
751 case f:
752 return "f";
753 case s:
754 return "s";
755 case m:
756 return "m";
757 case fs:
758 return "fs";
759 case fm:
760 return "fm";
761 default:
762 return "p";
763 }
764 }
765
766 QString LammpsInputDialog::getZBoundaryType(boundaryType t)
767 {
768 switch (t) {
769 case p:
770 return "p";
771 case f:
772 return "f";
773 case s:
774 return "s";
775 case m:
776 return "m";
777 case fs:
778 return "fs";
779 case fm:
780 return "fm";
781 default:
782 return "p";
783 }
784 }
785
786 QString LammpsInputDialog::getWaterPotential(waterPotential t)
787 {
788 switch (t) {
789 case NONE: {
790 QString waterPotentialInput;
791 QTextStream water(&waterPotentialInput);
792 water << "";
793 return waterPotentialInput;
794 }
795 case SPC: {
796 QString waterPotentialInput;
797 QTextStream water(&waterPotentialInput);
798 int Hydrogen;
799 int Oxygen;
800 determineAtomTypesSPC(Hydrogen, Oxygen);
801 water << "# The SPC water potential\n"
802 << "pair_style lj/cut/coul/cut 9.8 9.8\n"
803 << "pair_coeff " << Oxygen << " " << Oxygen
804 << " 0.15535 3.5533\n"
805 << "pair_coeff "
806 << "* " << Hydrogen << " 0.00000 0.0000\n"
807 << "bond_style harmonic\n"
808 << "angle_style harmonic\n"
809 << "dihedral_style none\n"
810 << "improper_style none\n"
811 << "bond_coeff 1 100.00 1.000\n"
812 << "angle_coeff 1 100.00 109.47\n"
813 << "special_bonds lj/coul 0.0 0.0 0.5\n"
814 << "fix RigidOHBonds all shake 0.0001 20 0 b 1 a 1\n";
815 return waterPotentialInput;
816 }
817 case SPCE: {
818 QString waterPotentialInput;
819 QTextStream water(&waterPotentialInput);
820 int Hydrogen;
821 int Oxygen;
822 determineAtomTypesSPC(Hydrogen, Oxygen);
823 water << "# The SPC/E water potential\n"
824 << "pair_style lj/cut/coul/long 9.8 9.8\n"
825 << "kspace_style pppm 1.0e-4\n"
826 << "pair_coeff " << Oxygen << " " << Oxygen
827 << " 0.15535 3.5533\n"
828 << "pair_coeff "
829 << "* " << Hydrogen << " 0.00000 0.0000\n"
830 << "bond_style harmonic\n"
831 << "angle_style harmonic\n"
832 << "dihedral_style none\n"
833 << "improper_style none\n"
834 << "bond_coeff 1 100.00 1.000\n"
835 << "angle_coeff 1 100.00 109.47\n"
836 << "special_bonds lj/coul 0.0 0.0 0.5\n"
837 << "fix RigidOHBonds all shake 0.0001 20 0 b 1 a 1\n";
838 return waterPotentialInput;
839 }
840 default: {
841 QString waterPotentialInput;
842 QTextStream water(&waterPotentialInput);
843 water << "\n";
844 return waterPotentialInput;
845 }
846 }
847 }
848
849 QString LammpsInputDialog::getEnsemble(ensemble t)
850 {
851 switch (t) {
852 case NVT: {
853 QString ensembleInput;
854 QTextStream fix(&ensembleInput);
855 fix << "fix ensemble all nvt"
856 << " temp " << Qt::fixed << qSetRealNumberPrecision(2)
857 << m_temperature << " " << Qt::fixed << qSetRealNumberPrecision(2)
858 << m_temperature << " 100 "
859 << "tchain " << m_nhChain << "\n";
860 return ensembleInput;
861 }
862 case NVE: {
863 QString ensembleInput;
864 QTextStream fix(&ensembleInput);
865 fix << "fix ensemble all nve\n";
866 return ensembleInput;
867 }
868 default: {
869 QString ensembleInput;
870 QTextStream fix(&ensembleInput);
871 fix << "fix ensemble all nvt"
872 << " temp " << Qt::fixed << qSetRealNumberPrecision(2)
873 << m_temperature << " " << Qt::fixed << qSetRealNumberPrecision(2)
874 << m_temperature << " 100 "
875 << "tchain " << m_nhChain << "\n";
876 return ensembleInput;
877 }
878 }
879 }
880
881 QString LammpsInputDialog::getVelocityDist(velocityDist t)
882 {
883 switch (t) {
884 case gaussian:
885 return "gaussian";
886 case uniform:
887 return "uniform";
888 default:
889 return "gaussian";
890 }
891 }
892
893 QString LammpsInputDialog::getZeroMOM()
894 {
895 if (m_zeroMOM)
896 return "yes";
897 else
898 return "no";
899 }
900
901 QString LammpsInputDialog::getZeroL()
902 {
903 if (m_zeroL)
904 return "yes";
905 else
906 return "no";
907 }
908
909 QString LammpsInputDialog::getThermoStyle(thermoStyle t)
910 {
911 switch (t) {
912 case one:
913 return "one";
914 case multi:
915 return "multi";
916 default:
917 return "one";
918 }
919 }
920
921 void LammpsInputDialog::deckDirty(bool dirty)
922 {
923 m_dirty = dirty;
924 ui.titleLine->setEnabled(!dirty);
925 // ui.calculationCombo->setEnabled(!dirty);
926 // ui.theoryCombo->setEnabled(!dirty);
927 // ui.basisCombo->setEnabled(!dirty);
928 // ui.multiplicitySpin->setEnabled(!dirty);
929 // ui.chargeSpin->setEnabled(!dirty);
930 ui.enableFormButton->setEnabled(dirty);
931 }
932
933 void LammpsInputDialog::readSettings(QSettings& settings)
934 {
935 m_savePath = settings.value("lammps/savepath").toString();
936 }
937
938 void LammpsInputDialog::writeSettings(QSettings& settings) const
939 {
940 settings.setValue("lammps/savepath", m_savePath);
941 }
942
943 void LammpsInputDialog::determineAtomTypesSPC(int& hyd, int& oxy)
944 {
945 double ThisMass;
946 QString ThisAtom;
947
948 // QList<Core::Atom *> atoms = m_molecule->atoms();
949 for (size_t i = 0; i < m_molecule->atomCount(); ++i) {
950 Core::Atom atom = m_molecule->atom(i);
951 ThisMass = Core::Elements::mass(atom.atomicNumber());
952 ThisAtom = Core::Elements::symbol(atom.atomicNumber());
953 AtomMass[ThisAtom] = ThisMass;
954 }
955 int AtomIndex = 0;
956 // Set AtomType integer
957 for (itr = AtomMass.begin(); itr != AtomMass.end(); ++itr) {
958 AtomIndex++;
959 AtomType[itr.key()] = AtomIndex;
960 }
961 // this is on purpose due to the use of
962 // unordered_map in OpenBabel, which
963 // returns a different order for O and H.
964 hyd = AtomType.value("O");
965 oxy = AtomType.value("H");
966 }
967 } // namespace Avogadro::QtPlugins