summaryrefslogtreecommitdiff
path: root/noncore/apps/oxygen/kmolelements.h
blob: 06d146921cf20cc5f5e6f144445047c05ed4ceab (plain)
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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
/*
 * kmolelements.h 
 *
 * Copyright (C) 2000 Tomislav Gountchev <tomi@idiom.com>
 */

// classes that store and manipulate chemical formulas represented as
// lists of elements

#ifndef KMOLELEMENTS_H
#define KMOLELEMENTS_H


#ifdef HAVE_CONFIG_H
#include <config.h>
#endif


#include <qlist.h>
#include <qdict.h>
#include <qstring.h>
#include <qtextstream.h>

class ElementCoef;
class ElementList;

/**
 * A generic chemical entity. Can be an element or a group. 
 */
class SubUnit {
 public:

  SubUnit();
  
  virtual ~SubUnit();

  /**
   * Construct a subunit and return a pointer to it. The syntax of LINE is 
   * the one used in the element definition file.
   */
  static SubUnit* makeSubUnit(QString line);
  
  /**
   * Get the molecular weight of THIS, based on the data from ELSTABLE.
   */
  virtual double getWeight(QDict<SubUnit>* elstable) const;

  /**
   * Add THIS to ELS.
   */
  virtual void addTo(ElementList& els, double coef) = 0;
  
  virtual QString getName() const;
  
  /**
   * Write THIS to LINE, in the format used in the definition file.
   */
  virtual void writeOut(QString& line) = 0;
};
	       

/**
 * A group of elements. 
 */
class ElementList : public SubUnit {
 public:
  ElementList ();
  ElementList (QString name);
  virtual ~ElementList();
  double getWeight(QDict<SubUnit>* elstable) const;
  
  /**
   * Return a string representing the elemental composition of THIS, as 
   * a tab-separated element - percentage pairs, separated by newlines.
   */
  QString getEA(QDict<SubUnit>* elstable, double mw = 0) const;

  /**
   * Return a string representing THIS as an empirical chemical formula.
   */
  QString getEmpFormula() const;
  
  /**
   * Multiply THIS (i.e. the coefficient of each element) by coef.
   */
  void multiplyBy(double coef);

  /**
   * Add THIS to ELS. THIS is not modified; ELS is.
   */
  void addTo(ElementList& els, double coef);

  /**
   * Add an element to THIS, with a coefficient COEF.
   */
  void addElement(const QString& name, double coef);

  /**
   * True iff THIS contains element named NAME.
   */
  bool contains(const QString& name);


  bool isEmpty();

  /**
   * The name of THIS, as a chemical group.
   */
  QString getName() const;

  /**
   * Write THIS to LINE, in a format suitable for the element definition file.
   */
  void writeOut(QString& line);

 private:
  QString name;
  QList<ElementCoef>* elements;
};

/**
 * A chemical element.
 */
class Element : public SubUnit {
 public:
  Element(const QString& name, double weight);
  virtual ~Element();
  double getWeight(QDict<SubUnit>* elstable) const;

  /**
   * Add THIS to ELS, with a coefficient COEF.  
   */
  void addTo(ElementList& els, double coef);

  QString getName() const;

  void writeOut(QString& line);

 private:
  double weight;
  QString name;
};


/**
 * An element - coefficient pair. Used to represent elements within an 
 * element list.
 */
class ElementCoef {
  friend class ElementList;
 public:
  ElementCoef(const QString& name, double coef = 1.0);
 private:
  QString name;
  double coef;
};


#endif