Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
SWIFTsim
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Deploy
Releases
Model registry
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
SWIFT
SWIFTsim
Commits
93c52425
Commit
93c52425
authored
9 years ago
by
Matthieu Schaller
Browse files
Options
Downloads
Patches
Plain Diff
Make the unit system more robust and const-correct
parent
357989a2
No related branches found
No related tags found
1 merge request
!140
First version of main() using a parameter file to get constants.
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
src/parser.c
+3
-8
3 additions, 8 deletions
src/parser.c
src/units.c
+14
-12
14 additions, 12 deletions
src/units.c
src/units.h
+10
-63
10 additions, 63 deletions
src/units.h
with
27 additions
and
83 deletions
src/parser.c
+
3
−
8
View file @
93c52425
...
...
@@ -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"
);
...
...
This diff is collapsed.
Click to expand it.
src/units.c
+
14
−
12
View file @
93c52425
...
...
@@ -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
);
...
...
This diff is collapsed.
Click to expand it.
src/units.h
+
10
−
63
View file @
93c52425
...
...
@@ -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 */
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment