units.h 4.86 KB
Newer Older
1
2
/*******************************************************************************
 * This file is part of SWIFT.
3
 * Copyright (c) 2012 Matthieu Schaller (matthieu.schaller@durham.ac.uk).
4
 *
5
6
7
8
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published
 * by the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
9
 *
10
11
12
13
 * 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.
14
 *
15
16
 * You should have received a copy of the GNU Lesser General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
17
 *
18
 ******************************************************************************/
19
20
#ifndef SWIFT_UNITS_H
#define SWIFT_UNITS_H
21

22
23
24
25
26
27
/* Config parameters. */
#include "../config.h"

/* Local includes. */
#include "parser.h"

28
29
30
/**
 * @brief The unit system used internally.
 *
31
 * This structure contains the conversion factors to the 7 cgs base units to the
32
33
 * internal units. It is used everytime a conversion is performed or an i/o
 * function is called.
34
 **/
35
struct UnitSystem {
36

37
  /*! Conversion factor from grams to internal mass units */
38
  double UnitMass_in_cgs;
39
40

  /*! Conversion factor from centimeters to internal length unit */
41
  double UnitLength_in_cgs;
42

43
  /*! Conversion factor from seconds to internal time units */
44
  double UnitTime_in_cgs;
45

46
  /*! Conversion factor from Ampere to internal current units */
47
  double UnitCurrent_in_cgs;
48

49
  /*! Conversion factor from Kelvins to internal temperature units. */
50
  double UnitTemperature_in_cgs;
51
52
53
};

/**
54
55
 * @brief The base units used in the cgs (and internal) system. All units are
 * derived from those.
56
 */
57
58
59
60
61
62
63
enum BaseUnits {
  UNIT_MASS = 0,
  UNIT_LENGTH = 1,
  UNIT_TIME = 2,
  UNIT_CURRENT = 3,
  UNIT_TEMPERATURE = 4
};
64
65
66
67

/**
 * @brief  The different conversion factors supported by default
 */
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
enum UnitConversionFactor {
  UNIT_CONV_NO_UNITS,
  UNIT_CONV_MASS,
  UNIT_CONV_LENGTH,
  UNIT_CONV_TIME,
  UNIT_CONV_DENSITY,
  UNIT_CONV_SPEED,
  UNIT_CONV_ACCELERATION,
  UNIT_CONV_FORCE,
  UNIT_CONV_ENERGY,
  UNIT_CONV_ENERGY_PER_UNIT_MASS,
  UNIT_CONV_ENTROPY,
  UNIT_CONV_ENTROPY_PER_UNIT_MASS,
  UNIT_CONV_POWER,
  UNIT_CONV_PRESSURE,
  UNIT_CONV_FREQUENCY,
  UNIT_CONV_ELECTRIC_CHARGE,
  UNIT_CONV_ELECTRIC_VOLTAGE,
  UNIT_CONV_ELECTRIC_CAPACITANCE,
  UNIT_CONV_ELECTRIC_RESISTANCE,
  UNIT_CONV_ELECTRIC_CONDUCTANCE,
  UNIT_CONV_MAGNETIC_FLUX,
  UNIT_CONV_MAGNETIC_FIELD,
  UNIT_CONV_MAGNETIC_INDUCTANCE,
92
93
  UNIT_CONV_TEMPERATURE,
  UNIT_CONV_VOLUME
94
};
95

96
void units_init_cgs(struct UnitSystem*);
97
98
void units_init(struct UnitSystem*, const struct swift_params*,
                const char* category);
99
100
101
102
void units_init_default(struct UnitSystem* us,
                        const struct swift_params* params, const char* category,
                        const struct UnitSystem* def);

103
104
105
int units_are_equal(const struct UnitSystem* a, const struct UnitSystem* b);

/* Base units */
106
double units_get_base_unit(const struct UnitSystem*, enum BaseUnits);
107
108
109
110
const char* units_get_base_unit_internal_symbol(enum BaseUnits);
const char* units_get_base_unit_cgs_symbol(enum BaseUnits);

/* Cosmology factors */
111
float units_general_h_factor(const struct UnitSystem* us,
112
                             const float baseUnitsExponants[5]);
113
114
115
float units_h_factor(const struct UnitSystem* us,
                     enum UnitConversionFactor unit);
float units_general_a_factor(const struct UnitSystem* us,
116
                             const float baseUnitsExponants[5]);
117
118
float units_a_factor(const struct UnitSystem* us,
                     enum UnitConversionFactor unit);
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137

/* Conversion to CGS */
double units_general_cgs_conversion_factor(const struct UnitSystem* us,
                                           const float baseUnitsExponants[5]);
double units_cgs_conversion_factor(const struct UnitSystem* us,
                                   enum UnitConversionFactor unit);
void units_general_cgs_conversion_string(char* buffer,
                                         const struct UnitSystem* us,
                                         const float baseUnitsExponants[5]);
void units_cgs_conversion_string(char* buffer, const struct UnitSystem* us,
                                 enum UnitConversionFactor unit);

/* Conversion between systems */
double units_general_conversion_factor(const struct UnitSystem* from,
                                       const struct UnitSystem* to,
                                       const float baseUnitsExponants[5]);
double units_conversion_factor(const struct UnitSystem* from,
                               const struct UnitSystem* to,
                               enum UnitConversionFactor unit);
138
139

#endif /* SWIFT_UNITS_H */