Skip to content
Snippets Groups Projects
Commit 93c52425 authored by Matthieu Schaller's avatar Matthieu Schaller
Browse files

Make the unit system more robust and const-correct

parent 357989a2
No related branches found
No related tags found
1 merge request!140First version of main() using a parameter file to get constants.
......@@ -145,11 +145,9 @@ static int is_empty(const char *str) {
/**
* @brief Parses a line from a file and stores any parameters in a structure.
*
* @param fp File pointer to file to be read
* @param params Structure to be populated from file
*
* @param line Line to be parsed.
* @param params Structure to be populated from file.
*/
static void parse_line(char *line, struct swift_params *params) {
/* Parse line if it doesn't begin with a comment. */
if (*line != PARSER_COMMENT_CHAR) {
......@@ -383,7 +381,7 @@ double parser_get_param_double(const struct swift_params *params,
*
* @param params Structure that holds the parameters
* @param name Name of the parameter to be found
* @param retParam of the parameter found
* @param retParam (return) Value of the parameter found
*/
void parser_get_param_string(const struct swift_params *params,
const char *name, char *retParam) {
......@@ -402,7 +400,6 @@ void parser_get_param_string(const struct swift_params *params,
* @brief Prints the contents of the parameter structure.
*
* @param params Structure that holds the parameters
*
*/
void parser_print_params(const struct swift_params *params) {
printf("\n--------------------------\n");
......@@ -421,9 +418,7 @@ void parser_print_params(const struct swift_params *params) {
*
* @param params Structure that holds the parameters
* @param file_name Name of file to be written
*
*/
void parser_write_params_to_file(const struct swift_params *params,
const char *file_name) {
FILE *file = fopen(file_name, "w");
......
......@@ -43,10 +43,11 @@
/**
* @brief Initialises the UnitSystem structure with the constants given in
* const.h
* @param us The UnitSystem to initialize
* rhe parameter file.
*
* @param us The UnitSystem to initialize.
* @param params The parsed parameter file.
*/
void initUnitSystem(struct UnitSystem* us, const struct swift_params* params) {
us->UnitMass_in_cgs =
......@@ -67,7 +68,7 @@ void initUnitSystem(struct UnitSystem* us, const struct swift_params* params) {
* @param us The UnitSystem used
* @param baseUnit The base unit
*/
double getBaseUnit(struct UnitSystem* us, enum BaseUnits baseUnit) {
double getBaseUnit(const struct UnitSystem* us, enum BaseUnits baseUnit) {
switch (baseUnit) {
case UNIT_MASS:
return us->UnitMass_in_cgs;
......@@ -271,7 +272,8 @@ void getBaseUnitExponantsArray(float baseUnitsExp[5],
* @param us The system of units in use
* @param unit The unit to convert
*/
double conversionFactor(struct UnitSystem* us, enum UnitConversionFactor unit) {
double conversionFactor(const struct UnitSystem* us,
enum UnitConversionFactor unit) {
float baseUnitsExp[5] = {0.f};
getBaseUnitExponantsArray(baseUnitsExp, unit);
......@@ -284,7 +286,7 @@ double conversionFactor(struct UnitSystem* us, enum UnitConversionFactor unit) {
* @param us The system of units in use
* @param unit The unit to convert
*/
float hFactor(struct UnitSystem* us, enum UnitConversionFactor unit) {
float hFactor(const struct UnitSystem* us, enum UnitConversionFactor unit) {
float baseUnitsExp[5] = {0.f};
getBaseUnitExponantsArray(baseUnitsExp, unit);
......@@ -297,7 +299,7 @@ float hFactor(struct UnitSystem* us, enum UnitConversionFactor unit) {
* @param us The system of units in use
* @param unit The unit to convert
*/
float aFactor(struct UnitSystem* us, enum UnitConversionFactor unit) {
float aFactor(const struct UnitSystem* us, enum UnitConversionFactor unit) {
float baseUnitsExp[5] = {0.f};
getBaseUnitExponantsArray(baseUnitsExp, unit);
......@@ -309,7 +311,7 @@ float aFactor(struct UnitSystem* us, enum UnitConversionFactor unit) {
* @brief Returns a string containing the exponents of the base units making up
* the conversion factors
*/
void conversionString(char* buffer, struct UnitSystem* us,
void conversionString(char* buffer, const struct UnitSystem* us,
enum UnitConversionFactor unit) {
float baseUnitsExp[5] = {0.f};
......@@ -325,7 +327,7 @@ void conversionString(char* buffer, struct UnitSystem* us,
* @param baseUnitsExponants The exponent of each base units required to form
* the desired quantity. See conversionFactor() for a working example
*/
double generalConversionFactor(struct UnitSystem* us,
double generalConversionFactor(const struct UnitSystem* us,
float baseUnitsExponants[5]) {
double factor = 1.;
int i;
......@@ -343,7 +345,7 @@ double generalConversionFactor(struct UnitSystem* us,
* @param baseUnitsExponants The exponent of each base units required to form
* the desired quantity. See conversionFactor() for a working example
*/
float generalhFactor(struct UnitSystem* us, float baseUnitsExponants[5]) {
float generalhFactor(const struct UnitSystem* us, float baseUnitsExponants[5]) {
float factor_exp = 0.f;
factor_exp += -baseUnitsExponants[UNIT_MASS];
......@@ -360,7 +362,7 @@ float generalhFactor(struct UnitSystem* us, float baseUnitsExponants[5]) {
* @param baseUnitsExponants The exponent of each base units required to form
* the desired quantity. See conversionFactor() for a working example
*/
float generalaFactor(struct UnitSystem* us, float baseUnitsExponants[5]) {
float generalaFactor(const struct UnitSystem* us, float baseUnitsExponants[5]) {
float factor_exp = 0.f;
factor_exp += baseUnitsExponants[UNIT_LENGTH];
......@@ -377,7 +379,7 @@ float generalaFactor(struct UnitSystem* us, float baseUnitsExponants[5]) {
* @param baseUnitsExponants The exponent of each base units required to form
* the desired quantity. See conversionFactor() for a working example
*/
void generalConversionString(char* buffer, struct UnitSystem* us,
void generalConversionString(char* buffer, const struct UnitSystem* us,
float baseUnitsExponants[5]) {
char temp[14];
double a_exp = generalaFactor(us, baseUnitsExponants);
......
......@@ -92,74 +92,21 @@ enum UnitConversionFactor {
UNIT_CONV_TEMPERATURE
};
/**
* @brief Initialises the UnitSystem structure with the constants given in
* const.h
*/
void initUnitSystem(struct UnitSystem*, const struct swift_params*);
/**
* @brief Returns the base unit conversion factor for a given unit system
*/
double getBaseUnit(struct UnitSystem*, enum BaseUnits);
/**
* @brief Returns the base unit symbol in the cgs system
*/
double getBaseUnit(const struct UnitSystem*, enum BaseUnits);
const char* getBaseUnitSymbol(enum BaseUnits);
/**
* @brief Returns the base unit symbol in the cgs system
*/
const char* getBaseUnitCGSSymbol(enum BaseUnits);
/**
* @brief Returns the conversion factor for a given unit (expressed in terms of
* the 5 fundamental units) in the chosen unit system
*/
double generalConversionFactor(struct UnitSystem* us,
double generalConversionFactor(const struct UnitSystem* us,
float baseUnitsExponants[5]);
/**
* @brief Returns the conversion factor for a given unit in the chosen unit
* system
*/
double conversionFactor(struct UnitSystem* us, enum UnitConversionFactor unit);
/**
* @brief Returns the h factor for a given unit (expressed in terms of the 5
* fundamental units) in the chosen unit system
*/
float generalhFactor(struct UnitSystem* us, float baseUnitsExponants[5]);
/**
* @brief Returns the h factor for a given unit in the chosen unit system
*/
float hFactor(struct UnitSystem* us, enum UnitConversionFactor unit);
/**
* @brief Returns the scaling factor for a given unit (expressed in terms of the
* 5 fundamental units) in the chosen unit system
*/
float generalaFactor(struct UnitSystem* us, float baseUnitsExponants[5]);
/**
* @brief Returns the scaling factor for a given unit in the chosen unit system
*/
float aFactor(struct UnitSystem* us, enum UnitConversionFactor unit);
/**
* @brief Returns a string containing the exponents of the base units making up
* the conversion factors (expressed in terms of the 5 fundamental units)
*/
void generalConversionString(char* buffer, struct UnitSystem* us,
double conversionFactor(const struct UnitSystem* us,
enum UnitConversionFactor unit);
float generalhFactor(const struct UnitSystem* us, float baseUnitsExponants[5]);
float hFactor(const struct UnitSystem* us, enum UnitConversionFactor unit);
float generalaFactor(const struct UnitSystem* us, float baseUnitsExponants[5]);
float aFactor(const struct UnitSystem* us, enum UnitConversionFactor unit);
void generalConversionString(char* buffer, const struct UnitSystem* us,
float baseUnitsExponants[5]);
/**
* @brief Returns a string containing the exponents of the base units making up
* the conversion factors
*/
void conversionString(char* buffer, struct UnitSystem* us,
void conversionString(char* buffer, const struct UnitSystem* us,
enum UnitConversionFactor unit);
#endif /* SWIFT_UNITS_H */
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment