File: MOPACInputFile.C

package info (click to toggle)
ball 1.5.0%2Bgit20180813.37fc53c-6
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 239,888 kB
  • sloc: cpp: 326,149; ansic: 4,208; python: 2,303; yacc: 1,778; lex: 1,099; xml: 958; sh: 322; makefile: 95
file content (100 lines) | stat: -rw-r--r-- 2,349 bytes parent folder | download | duplicates (7)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
// -*- Mode: C++; tab-width: 2; -*-
// vi: set ts=2:
//

#include <BALL/FORMAT/MOPACInputFile.h>

#include <BALL/KERNEL/system.h>
#include <BALL/KERNEL/atom.h>
#include <BALL/KERNEL/PDBAtom.h>
#include <BALL/KERNEL/bond.h>
#include <BALL/KERNEL/PTE.h>

namespace BALL
{
	MOPACInputFile::MOPACInputFile()
		: GenericMolFile(),
			default_mopac_keywords_("AM1 MMOK GEO-OK BONDS PRECISE")
	{
	}

	MOPACInputFile::MOPACInputFile(const String& name, File::OpenMode open_mode)
		: GenericMolFile(),
			default_mopac_keywords_("AM1 MMOK GEO-OK BONDS PRECISE")
	{
		open(name, open_mode);
	}

	MOPACInputFile::~MOPACInputFile()
	{
	}

	void MOPACInputFile::writeHeader_(AtomContainer const& ac)
	{
		String mopac_keywords = default_mopac_keywords_;
		if (ac.hasProperty("MOPAC-Keywords"))
			mopac_keywords = ac.getProperty("MOPAC-Keywords").toString();

		// TODO: take the correct charge
		String charge = "CHARGE=0";

		String multiplicity = "SINGLET";
		if (ac.hasProperty("SpinMultiplicity"))
			multiplicity = ac.getProperty("SpinMultiplicity").toString();

		mopac_keywords += " " + charge + " " + multiplicity;

		String comment = "Generated by BALL for " + ac.getName() + "\n";

		File& f = static_cast<File&>(*this);

		f << mopac_keywords << std::endl;
		f << comment << std::endl;
	}

	// TODO: warn if odd number of electrons and charge / multiplicity not set
	void MOPACInputFile::writeAtoms_(const AtomContainer& ac)
	{
		File& f = static_cast<File&>(*this);

		Size number_of_electrons = 0;
		for (AtomConstIterator at_it = ac.beginAtom(); +at_it; ++at_it)
		{
			String line(256, "%5s%12.4lf  1  %12.4lf  1  %12.4lf  1   \n", 
					at_it->getElement().getSymbol().c_str(), at_it->getPosition().x,
					at_it->getPosition().y, at_it->getPosition().z);

			f << line;

			number_of_electrons += at_it->getElement().getAtomicNumber();
		}

		f << std::endl;
	}

	bool MOPACInputFile::write(const System& system)
	{
		if (!isOpen() || getOpenMode() != std::ios::out)
		{
			throw(File::CannotWrite(__FILE__, __LINE__, name_));
		}

		writeHeader_(system);
		writeAtoms_(system);

		return true;
	}

	bool MOPACInputFile::write(const Molecule& molecule)
	{
		if (!isOpen() || getOpenMode() != std::ios::out)
		{
			throw(File::CannotWrite(__FILE__, __LINE__, name_));
		}

		writeHeader_(molecule);
		writeAtoms_(molecule);

		return true;
	}
}