File: chemistry.cc

package info (click to toggle)
gnome-chemistry-utils 0.14.9-1
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 17,836 kB
  • ctags: 7,337
  • sloc: cpp: 72,977; sh: 11,381; xml: 6,304; makefile: 1,663; ansic: 1,061
file content (176 lines) | stat: -rw-r--r-- 4,422 bytes parent folder | download
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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
// -*- C -*-

/*
 * Gnome Chemisty Utils
 * chemistry.cc
 *
 * Copyright (C) 2003-2011 Jean Bréfort <jean.brefort@normalesup.org>
 *
 * This program 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.
 *
 * This program 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 this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301
 * USA
 */

#include "config.h"
#include "chemistry.h"
#include "element.h"
#include <sstream>
#include <cstdarg>
#include <cstring>

extern "C"
{

using namespace gcu;
const double* gcu_element_get_default_color (int Z)
{
	return Element::GetElement (Z)->GetDefaultColor ();
}

char const *gcu_element_get_symbol (int Z)
{
	return Element::Symbol (Z);
}

char const *gcu_element_get_name (int Z)
{
	return Element::GetElement (Z)->GetName ();
}

int gcu_element_get_Z (char* symbol)
{
	return Element::Z (symbol);
}

int gcu_element_get_radius (GcuAtomicRadius *radius)
{
	return Element::GetRadius (radius);
}

int gcu_element_get_electronegativity (GcuElectronegativity *en)
{
	return Element::GetElectronegativity(en);
}

const GcuAtomicRadius** gcu_element_get_radii (int Z)
{
	return Element::GetElement (Z)->GetRadii ();
}

const GcuElectronegativity** gcu_element_get_electronegativities (int Z)
{
	return Element::GetElement (Z)->GetElectronegativities ();
}

void gcu_element_load_databases (char const *name, ...)
{
	va_list l;
	char const *base = name;
	va_start (l, name);
	while (base != NULL) {
		if (!strcmp (base, "radii"))
			Element::LoadRadii ();
		else if (!strcmp (base, "elecprops"))
			Element::LoadElectronicProps ();
		else if (!strcmp (base, "isotopes"))
			Element::LoadIsotopes ();
		base = va_arg (l, char const*);
	}
	va_end (l);
}

char* gcu_value_get_string (GcuValue const *value)
{
	std::ostringstream s;
	char const *cloc = setlocale (LC_NUMERIC, NULL);
	if (cloc != NULL) {
		std::locale loc (cloc);
		s.imbue (loc);
	}
	char *str;
	if (value->value < 0)
		s << "−";
	if (value->delta > 0) {
		int delta = value->delta, prec = value->prec;
		while (delta >= 100) {
			delta /= 10;
			prec--;
		}
		// FIXME: what to do if we have a negative precision?
		s.precision (prec);
		s << std::fixed <<  fabs (value->value) << '(' << delta << ')';
	} else {
		s.precision (value->prec);
		s << std::fixed << fabs (value->value);
	}
	str = strdup (s.str ().c_str ());
	return str;
}

char *gcu_dimensional_value_get_string (GcuDimensionalValue const *value)
{
	std::ostringstream s;
	char const *cloc = setlocale (LC_NUMERIC, NULL);
	if (cloc != NULL) {
		std::locale loc (cloc);
		s.imbue (loc);
	}
	char *str;
	if (value->value < 0)
		s << "−";
	if (value->delta > 0) {
		int delta = value->delta, prec = value->prec;
		while (delta >= 100) {
			delta /= 10;
			prec--;
		}
		// FIXME: what to do if we have a negative precision?
		s.precision (prec);
		s << std::fixed << fabs (value->value) << '(' << delta << ") " << value->unit;
	} else {
		s.precision (value->prec);
		s << std::fixed <<  fabs (value->value) << " " << value->unit;
	}
	str = strdup (s.str ().c_str ());
	return str;
}

GcuDimensionalValue const *gcu_element_get_ionization_energy (int Z, int rank)
{
	return Element::GetElement(Z)->GetIonizationEnergy (rank);
}

GcuDimensionalValue const *gcu_element_get_electron_affinity (int Z, int rank)
{
	return Element::GetElement(Z)->GetElectronAffinity (rank);
}

char *gcu_element_get_weight_as_string (int Z)
{
	Element *elt = Element::GetElement (Z);
	gcu::DimensionalValue const *value = (elt)? elt->GetWeight (): NULL;
	if (value) {
		GcuDimensionalValue val = value->GetValue ();
		return elt->GetStability ()? gcu_value_get_string (reinterpret_cast < GcuValue * > (&val)): g_strdup_printf("(%g)", val.value);
	} else
		return NULL;
}

char const *gcu_element_get_electronic_configuration (int Z)
{
	Element *elt = Element::GetElement (Z);
	return (elt)? elt->GetElectronicConfiguration ().c_str (): NULL;
}

} //extern "C"