units.c 19.1 KB
Newer Older
1
2
/*******************************************************************************
 * This file is part of SWIFT.
3
 * Copyright (c) 2012 Pedro Gonnet (pedro.gonnet@durham.ac.uk),
4
 *                    Matthieu Schaller (matthieu.schaller@durham.ac.uk).
5
 *
6
7
8
9
 * 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.
10
 *
11
12
13
14
 * 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.
15
 *
16
17
 * 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/>.
18
 *
19
20
21
22
23
 ******************************************************************************/

/* Config parameters. */
#include "../config.h"

24
/* Some standard headers. */
25
26
#include <math.h>
#include <stddef.h>
27
28
29
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
30
31

/* MPI headers. */
32
33
34
#ifdef WITH_MPI
#include <mpi.h>
#endif
35

36
37
38
39
/* This object's header. */
#include "units.h"

/* Includes. */
40
#include "adiabatic_index.h"
41
#include "error.h"
Peter W. Draper's avatar
Peter W. Draper committed
42
#include "restart.h"
43

44
/**
45
 * @brief Initialises the unit_system structure with CGS system
46
 *
47
 * @param us The unit_system to initialize
48
 */
49
void units_init_cgs(struct unit_system* us) {
50
51
52
53
54
55
56
57

  us->UnitMass_in_cgs = 1.;
  us->UnitLength_in_cgs = 1.;
  us->UnitTime_in_cgs = 1.;
  us->UnitCurrent_in_cgs = 1.;
  us->UnitTemperature_in_cgs = 1.;
}

58
/**
59
 * @brief Initialises the unit_system structure with the constants given in
lhausamm's avatar
lhausamm committed
60
 * the parameter file.
61
 *
62
 * @param us The unit_system to initialize.
63
 * @param params The parsed parameter file.
64
 * @param category The section of the parameter file to read from.
65
 */
66
void units_init(struct unit_system* us, const struct swift_params* params,
67
                const char* category) {
68

69
70
71
72
73
74
75
  char buffer[200];
  sprintf(buffer, "%s:UnitMass_in_cgs", category);
  us->UnitMass_in_cgs = parser_get_param_double(params, buffer);
  sprintf(buffer, "%s:UnitLength_in_cgs", category);
  us->UnitLength_in_cgs = parser_get_param_double(params, buffer);
  sprintf(buffer, "%s:UnitVelocity_in_cgs", category);
  const double unitVelocity = parser_get_param_double(params, buffer);
76
  us->UnitTime_in_cgs = us->UnitLength_in_cgs / unitVelocity;
77
78
79
80
  sprintf(buffer, "%s:UnitCurrent_in_cgs", category);
  us->UnitCurrent_in_cgs = parser_get_param_double(params, buffer);
  sprintf(buffer, "%s:UnitTemp_in_cgs", category);
  us->UnitTemperature_in_cgs = parser_get_param_double(params, buffer);
81
82
}

83
/**
84
 * @brief Initialises the unit_system structure with the constants given in
lhausamm's avatar
lhausamm committed
85
 * the parameter file. Uses a default if the values are not present in the file.
86
 *
87
 * @param us The unit_system to initialize.
88
89
90
91
 * @param params The parsed parameter file.
 * @param category The section of the parameter file to read from.
 * @param def The default unit system to copy from if required.
 */
92
void units_init_default(struct unit_system* us,
93
                        const struct swift_params* params, const char* category,
94
                        const struct unit_system* def) {
95

96
  if (!def) error("Default unit_system not allocated");
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117

  char buffer[200];
  sprintf(buffer, "%s:UnitMass_in_cgs", category);
  us->UnitMass_in_cgs =
      parser_get_opt_param_double(params, buffer, def->UnitMass_in_cgs);
  sprintf(buffer, "%s:UnitLength_in_cgs", category);
  us->UnitLength_in_cgs =
      parser_get_opt_param_double(params, buffer, def->UnitLength_in_cgs);
  sprintf(buffer, "%s:UnitVelocity_in_cgs", category);
  const double defaultVelocity = def->UnitLength_in_cgs / def->UnitTime_in_cgs;
  const double unitVelocity =
      parser_get_opt_param_double(params, buffer, defaultVelocity);
  us->UnitTime_in_cgs = us->UnitLength_in_cgs / unitVelocity;
  sprintf(buffer, "%s:UnitCurrent_in_cgs", category);
  us->UnitCurrent_in_cgs =
      parser_get_opt_param_double(params, buffer, def->UnitCurrent_in_cgs);
  sprintf(buffer, "%s:UnitTemp_in_cgs", category);
  us->UnitTemperature_in_cgs =
      parser_get_opt_param_double(params, buffer, def->UnitTemperature_in_cgs);
}

118
119
/**
 * @brief Returns the base unit conversion factor for a given unit system
120
 * @param us The unit_system used
121
 * @param baseUnit The base unit
122
 */
123
124
double units_get_base_unit(const struct unit_system* us,
                           enum base_units baseUnit) {
125
126
127
128
129
130
131
132
133
134
135
136
137
138
  switch (baseUnit) {
    case UNIT_MASS:
      return us->UnitMass_in_cgs;
    case UNIT_LENGTH:
      return us->UnitLength_in_cgs;
    case UNIT_TIME:
      return us->UnitTime_in_cgs;
    case UNIT_CURRENT:
      return us->UnitCurrent_in_cgs;
    case UNIT_TEMPERATURE:
      return us->UnitTemperature_in_cgs;
    default:
      error("Invalid base Unit");
  }
139
  return 0.0;
140
141
142
}

/**
143
 * @brief Returns the base unit symbol used internally
144
 * @param baseUnit The base unit
145
 */
146
const char* units_get_base_unit_internal_symbol(enum base_units baseUnit) {
147
148
149
150
151
152
153
154
155
156
157
158
159
160
  switch (baseUnit) {
    case UNIT_MASS:
      return "U_M";
    case UNIT_LENGTH:
      return "U_L";
    case UNIT_TIME:
      return "U_t";
    case UNIT_CURRENT:
      return "U_I";
    case UNIT_TEMPERATURE:
      return "U_T";
    default:
      error("Invalid base Unit");
  }
161
  return "";
162
163
164
165
}

/**
 * @brief Returns the base unit symbol in the cgs system
166
 * @param baseUnit The base unit
167
 */
168
const char* units_get_base_unit_cgs_symbol(enum base_units baseUnit) {
169
170
171
172
173
174
175
176
177
178
179
180
181
182
  switch (baseUnit) {
    case UNIT_MASS:
      return "g";
    case UNIT_LENGTH:
      return "cm";
    case UNIT_TIME:
      return "s";
    case UNIT_CURRENT:
      return "A";
    case UNIT_TEMPERATURE:
      return "K";
    default:
      error("Invalid base Unit");
  }
183
  return "";
184
185
}

186
void units_get_base_unit_exponants_array(float baseUnitsExp[5],
187
                                         enum unit_conversion_factor unit) {
188
  switch (unit) {
189
190
191
    case UNIT_CONV_NO_UNITS:
      break;

192
193
194
    case UNIT_CONV_MASS:
      baseUnitsExp[UNIT_MASS] = 1.f;
      break;
195

196
197
198
    case UNIT_CONV_LENGTH:
      baseUnitsExp[UNIT_LENGTH] = 1.f;
      break;
199

200
201
202
    case UNIT_CONV_TIME:
      baseUnitsExp[UNIT_TIME] = 1.f;
      break;
203

204
205
206
    case UNIT_CONV_FREQUENCY:
      baseUnitsExp[UNIT_TIME] = -1.f;
      break;
207

208
209
210
211
    case UNIT_CONV_DENSITY:
      baseUnitsExp[UNIT_MASS] = 1.f;
      baseUnitsExp[UNIT_LENGTH] = -3.f;
      break;
212

213
214
215
216
    case UNIT_CONV_SPEED:
      baseUnitsExp[UNIT_LENGTH] = 1.f;
      baseUnitsExp[UNIT_TIME] = -1.f;
      break;
217

218
219
220
221
    case UNIT_CONV_ACCELERATION:
      baseUnitsExp[UNIT_LENGTH] = 1.f;
      baseUnitsExp[UNIT_TIME] = -2.f;
      break;
222

223
224
225
226
227
    case UNIT_CONV_POTENTIAL:
      baseUnitsExp[UNIT_LENGTH] = 2.f;
      baseUnitsExp[UNIT_TIME] = -2.f;
      break;

228
229
230
231
232
    case UNIT_CONV_FORCE:
      baseUnitsExp[UNIT_MASS] = 1.f;
      baseUnitsExp[UNIT_LENGTH] = 1.f;
      baseUnitsExp[UNIT_TIME] = -2.f;
      break;
233

234
235
236
237
238
    case UNIT_CONV_ENERGY:
      baseUnitsExp[UNIT_MASS] = 1.f;
      baseUnitsExp[UNIT_LENGTH] = 2.f;
      baseUnitsExp[UNIT_TIME] = -2.f;
      break;
239

240
241
242
243
    case UNIT_CONV_ENERGY_PER_UNIT_MASS:
      baseUnitsExp[UNIT_LENGTH] = 2.f;
      baseUnitsExp[UNIT_TIME] = -2.f;
      break;
244

245
    case UNIT_CONV_ENTROPY:
246
247
      baseUnitsExp[UNIT_MASS] = 1.f - hydro_gamma;
      baseUnitsExp[UNIT_LENGTH] = 3.f * hydro_gamma - 1.f;
248
249
      baseUnitsExp[UNIT_TIME] = -2.f;
      break;
250

251
    case UNIT_CONV_ENTROPY_PER_UNIT_MASS:
252
253
      baseUnitsExp[UNIT_MASS] = -hydro_gamma;
      baseUnitsExp[UNIT_LENGTH] = 3.f * hydro_gamma - 1.f;
254
255
      baseUnitsExp[UNIT_TIME] = -2.f;
      break;
256

257
258
259
260
261
    case UNIT_CONV_POWER:
      baseUnitsExp[UNIT_MASS] = 1.f;
      baseUnitsExp[UNIT_LENGTH] = 2.f;
      baseUnitsExp[UNIT_TIME] = -3.f;
      break;
262

263
264
265
266
267
    case UNIT_CONV_PRESSURE:
      baseUnitsExp[UNIT_MASS] = 1.f;
      baseUnitsExp[UNIT_LENGTH] = -1.f;
      baseUnitsExp[UNIT_TIME] = -2.f;
      break;
268

269
    case UNIT_CONV_ELECTRIC_CHARGE:
270
271
272
      baseUnitsExp[UNIT_TIME] = 1.f;
      baseUnitsExp[UNIT_CURRENT] = 1.f;
      break;
273
274

    case UNIT_CONV_ELECTRIC_VOLTAGE:
275
276
277
278
279
280
      baseUnitsExp[UNIT_MASS] = 1.f;
      baseUnitsExp[UNIT_LENGTH] = 2.f;
      baseUnitsExp[UNIT_TIME] = -3.f;
      baseUnitsExp[UNIT_CURRENT] = -1.f;
      break;

281
    case UNIT_CONV_ELECTRIC_CAPACITANCE:
282
283
284
285
286
      baseUnitsExp[UNIT_MASS] = -1.f;
      baseUnitsExp[UNIT_LENGTH] = -2.f;
      baseUnitsExp[UNIT_TIME] = 4;
      baseUnitsExp[UNIT_CURRENT] = 2.f;
      break;
287
288

    case UNIT_CONV_ELECTRIC_RESISTANCE:
289
290
291
292
293
      baseUnitsExp[UNIT_MASS] = 1.f;
      baseUnitsExp[UNIT_LENGTH] = 2.f;
      baseUnitsExp[UNIT_TIME] = -3.f;
      baseUnitsExp[UNIT_CURRENT] = -2.f;
      break;
294
295

    case UNIT_CONV_ELECTRIC_CONDUCTANCE:
296
297
298
299
300
301
      baseUnitsExp[UNIT_MASS] = -1.f;
      baseUnitsExp[UNIT_LENGTH] = -2.f;
      baseUnitsExp[UNIT_TIME] = 3.f;
      baseUnitsExp[UNIT_CURRENT] = 2.f;
      break;

302
    case UNIT_CONV_MAGNETIC_FLUX:
303
304
305
306
307
308
      baseUnitsExp[UNIT_MASS] = 1.f;
      baseUnitsExp[UNIT_LENGTH] = 2.f;
      baseUnitsExp[UNIT_TIME] = -2.f;
      baseUnitsExp[UNIT_CURRENT] = -1.f;
      break;

309
    case UNIT_CONV_MAGNETIC_FIELD:
310
311
312
313
      baseUnitsExp[UNIT_MASS] = 1.f;
      baseUnitsExp[UNIT_TIME] = -2.f;
      baseUnitsExp[UNIT_CURRENT] = -1.f;
      break;
314
315

    case UNIT_CONV_MAGNETIC_INDUCTANCE:
316
317
318
319
320
      baseUnitsExp[UNIT_MASS] = 1.f;
      baseUnitsExp[UNIT_LENGTH] = 2.f;
      baseUnitsExp[UNIT_TIME] = -2.f;
      baseUnitsExp[UNIT_CURRENT] = -2.f;
      break;
321
322

    case UNIT_CONV_TEMPERATURE:
323
      baseUnitsExp[UNIT_TEMPERATURE] = 1.f;
324
325
326
      break;

    case UNIT_CONV_VOLUME:
327
      baseUnitsExp[UNIT_LENGTH] = 3.f;
328
      break;
329
330

    case UNIT_CONV_INV_VOLUME:
331
      baseUnitsExp[UNIT_LENGTH] = -3.f;
332
333
334
335
336
      break;

    default:
      error("Invalid choice of pre-defined units");
      break;
337
  }
338
339
340
}

/**
341
342
 * @brief Returns the conversion factor for a given unit in the chosen unit
 * system
343
344
345
 * @param us The system of units in use
 * @param unit The unit to convert
 */
346
347
double units_cgs_conversion_factor(const struct unit_system* us,
                                   enum unit_conversion_factor unit) {
348
  float baseUnitsExp[5] = {0.f};
349

350
  units_get_base_unit_exponants_array(baseUnitsExp, unit);
351

352
  return units_general_cgs_conversion_factor(us, baseUnitsExp);
353
354
355
356
357
358
359
}

/**
 * @brief Returns the h factor exponentiation for a given unit
 * @param us The system of units in use
 * @param unit The unit to convert
 */
360
361
float units_h_factor(const struct unit_system* us,
                     enum unit_conversion_factor unit) {
362
  float baseUnitsExp[5] = {0.f};
363

364
  units_get_base_unit_exponants_array(baseUnitsExp, unit);
365

366
  return units_general_h_factor(us, baseUnitsExp);
367
368
369
370
371
372
373
}

/**
 * @brief Returns the scaling factor exponentiation for a given unit
 * @param us The system of units in use
 * @param unit The unit to convert
 */
374
375
float units_a_factor(const struct unit_system* us,
                     enum unit_conversion_factor unit) {
376
  float baseUnitsExp[5] = {0.f};
377

378
  units_get_base_unit_exponants_array(baseUnitsExp, unit);
379

380
  return units_general_a_factor(us, baseUnitsExp);
381
382
}

383
/**
384
 * @brief Returns a string containing the exponents of the base units making up
385
 * the conversion factors
386
 */
387
388
void units_cgs_conversion_string(char* buffer, const struct unit_system* us,
                                 enum unit_conversion_factor unit) {
389
  float baseUnitsExp[5] = {0.f};
390

391
  units_get_base_unit_exponants_array(baseUnitsExp, unit);
392

393
  units_general_cgs_conversion_string(buffer, us, baseUnitsExp);
394
395
}

396
/**
397
398
 * @brief Returns the conversion factor for a given unit (expressed in terms of
 * the 5 fundamental units) in the chosen unit system
399
 * @param us The unit system used
400
 * @param baseUnitsExponants The exponent of each base units required to form
401
 * the desired quantity. See conversionFactor() for a working example
402
 */
403
double units_general_cgs_conversion_factor(const struct unit_system* us,
404
                                           const float baseUnitsExponants[5]) {
405
406
  double factor = 1.;

407
  for (int i = 0; i < 5; ++i)
408
    if (baseUnitsExponants[i] != 0)
409
      factor *= pow(units_get_base_unit(us, (enum base_units)i),
410
                    baseUnitsExponants[i]);
411
  return factor;
412
413
414
}

/**
415
416
 * @brief Returns the h factor exponentiation for a given unit (expressed in
 * terms of the 5 fundamental units)
417
 * @param us The unit system used
418
 * @param baseUnitsExponants The exponent of each base units required to form
419
 * the desired quantity. See conversionFactor() for a working example
420
 */
421
float units_general_h_factor(const struct unit_system* us,
422
                             const float baseUnitsExponants[5]) {
423
  float factor_exp = 0.f;
424

425
426
427
  factor_exp += -baseUnitsExponants[UNIT_MASS];
  factor_exp += -baseUnitsExponants[UNIT_LENGTH];
  factor_exp += -baseUnitsExponants[UNIT_TIME];
428

429
  return factor_exp;
430
431
432
}

/**
433
434
 * @brief Returns the scaling factor exponentiation for a given unit (expressed
 * in terms of the 5 fundamental units)
435
 * @param us The unit system used
436
 * @param baseUnitsExponants The exponent of each base units required to form
437
 * the desired quantity. See conversionFactor() for a working example
438
 */
439
float units_general_a_factor(const struct unit_system* us,
440
                             const float baseUnitsExponants[5]) {
441
  float factor_exp = 0.f;
442

443
  factor_exp += baseUnitsExponants[UNIT_LENGTH];
444
445

  return factor_exp;
446
}
447
448

/**
449
 * @brief Returns a string containing the exponents of the base units making up
450
451
452
 * the conversion factors (expressed in terms of the 5 fundamental units)
 * @param buffer The buffer in which to write (The buffer must be long enough,
 * 140 chars at most)
453
454
 * @param us The UnitsSystem in use.
 * @param baseUnitsExponants The exponent of each base units required to form
455
 * the desired quantity. See conversionFactor() for a working example
456
 */
457
void units_general_cgs_conversion_string(char* buffer,
458
                                         const struct unit_system* us,
459
                                         const float baseUnitsExponants[5]) {
460
  char temp[14];
461
462
  const double a_exp = units_general_a_factor(us, baseUnitsExponants);
  const double h_exp = units_general_h_factor(us, baseUnitsExponants);
463

464
465
  /* Check whether we are unitless or not */
  char isAllNonZero = 1;
466
  for (int i = 0; i < 5; ++i)
467
    if (baseUnitsExponants[i] != 0.) isAllNonZero = 0;
468

469
470
471
472
  if (isAllNonZero) {
    sprintf(buffer, "[ - ] ");
    return;
  }
473

474
  /* Add a-factor */
475
  if (a_exp == 0)
476
    sprintf(buffer, " ");
477
  else if (a_exp == 1)
478
    sprintf(buffer, "a ");
479
480
  else if (remainder(a_exp, 1.) == 0)
    sprintf(buffer, "a^%d ", (int)a_exp);
481
482
483
484
  else
    sprintf(buffer, "a^%7.4f ", a_exp);

  /* Add h-factor */
485
  if (h_exp == 0)
486
    sprintf(temp, " ");
487
  else if (h_exp == 1)
488
    sprintf(temp, "h ");
489
490
  else if (remainder(h_exp, 1.) == 0)
    sprintf(temp, "h^%d ", (int)h_exp);
491
492
493
494
495
  else
    sprintf(temp, "h^%7.4f ", h_exp);
  strncat(buffer, temp, 12);

  /* Add conversion units */
496
  for (int i = 0; i < 5; ++i)
497
498
499
500
    if (baseUnitsExponants[i] != 0) {
      if (baseUnitsExponants[i] == 0.)
        sprintf(temp, " ");
      else if (baseUnitsExponants[i] == 1.)
501
        sprintf(temp, "%s ",
502
                units_get_base_unit_internal_symbol((enum base_units)i));
503
      else if (remainder(baseUnitsExponants[i], 1.) == 0)
504
        sprintf(temp, "%s^%d ",
505
                units_get_base_unit_internal_symbol((enum base_units)i),
506
507
                (int)baseUnitsExponants[i]);
      else
508
        sprintf(temp, "%s^%7.4f ",
509
                units_get_base_unit_internal_symbol((enum base_units)i),
510
                baseUnitsExponants[i]);
511
512
      strncat(buffer, temp, 12);
    }
513
514

  /* Add CGS units */
515
  strncat(buffer, " [ ", 3);
516

517
  for (int i = 0; i < 5; ++i) {
518
519
520
521
    if (baseUnitsExponants[i] != 0) {
      if (baseUnitsExponants[i] == 0.)
        continue;
      else if (baseUnitsExponants[i] == 1.)
522
523
        sprintf(temp, "%s ",
                units_get_base_unit_cgs_symbol((enum base_units)i));
524
      else if (remainder(baseUnitsExponants[i], 1.) == 0)
525
        sprintf(temp, "%s^%d ",
526
                units_get_base_unit_cgs_symbol((enum base_units)i),
527
528
                (int)baseUnitsExponants[i]);
      else
529
        sprintf(temp, "%s^%7.4f ",
530
                units_get_base_unit_cgs_symbol((enum base_units)i),
531
532
                baseUnitsExponants[i]);
      strncat(buffer, temp, 12);
533
    }
534
535
  }

536
537
  strncat(buffer, "]", 2);
}
538
539
540
541

/**
 * @brief Are the two unit systems equal ?
 *
542
543
 * @param a The First #unit_system
 * @param b The second #unit_system
544
545
 * @return 1 if the systems are the same, 0 otherwise
 */
546
int units_are_equal(const struct unit_system* a, const struct unit_system* b) {
547
548
549
550
551
552
553
554
555
556
557
558
559

  if (a->UnitMass_in_cgs != b->UnitMass_in_cgs) return 0;
  if (a->UnitLength_in_cgs != b->UnitLength_in_cgs) return 0;
  if (a->UnitTime_in_cgs != b->UnitTime_in_cgs) return 0;
  if (a->UnitCurrent_in_cgs != b->UnitCurrent_in_cgs) return 0;
  if (a->UnitTemperature_in_cgs != b->UnitTemperature_in_cgs) return 0;

  return 1;
}

/**
 * @brief Return the unit conversion factor between two systems
 *
560
561
 * @param from The #unit_system we are converting from
 * @param to The #unit_system we are converting to
562
563
564
 * @param baseUnitsExponants The exponent of each base units required to form
 * the desired quantity. See conversionFactor() for a working example
 */
565
566
double units_general_conversion_factor(const struct unit_system* from,
                                       const struct unit_system* to,
567
568
569
570
571
572
573
574
575
576
577
578
579
                                       const float baseUnitsExponants[5]) {

  const double from_cgs =
      units_general_cgs_conversion_factor(from, baseUnitsExponants);
  const double to_cgs =
      units_general_cgs_conversion_factor(to, baseUnitsExponants);

  return from_cgs / to_cgs;
}

/**
 * @brief Return the unit conversion factor between two systems
 *
580
581
 * @param from The #unit_system we are converting from
 * @param to The #unit_system we are converting to
Matthieu Schaller's avatar
Matthieu Schaller committed
582
 * @param unit The unit we are converting
583
 *
Matthieu Schaller's avatar
Matthieu Schaller committed
584
 * @return The conversion factor
585
 */
586
587
588
double units_conversion_factor(const struct unit_system* from,
                               const struct unit_system* to,
                               enum unit_conversion_factor unit) {
589
590
591
592
593
594
595

  float baseUnitsExp[5] = {0.f};

  units_get_base_unit_exponants_array(baseUnitsExp, unit);

  return units_general_conversion_factor(from, to, baseUnitsExp);
}
lhausamm's avatar
lhausamm committed
596
597
598
599
600
601
602

/**
 * @brief print a #unit_system
 *
 * @param us The #unit_system
 */
void units_print(const struct unit_system* us) {
lhausamm's avatar
lhausamm committed
603

lhausamm's avatar
lhausamm committed
604
605
606
607
608
609
610
  message("Units:");
  message("\tUnit Mass:        %g", us->UnitMass_in_cgs);
  message("\tUnit Length:      %g", us->UnitLength_in_cgs);
  message("\tUnit Time:        %g", us->UnitTime_in_cgs);
  message("\tUnit Current:     %g", us->UnitCurrent_in_cgs);
  message("\tUnit Temperature: %g", us->UnitTemperature_in_cgs);
}
Peter W. Draper's avatar
Peter W. Draper committed
611
612
613
614
615
616
617

/**
 * @brief Write a units struct to the given FILE as a stream of bytes.
 *
 * @param us the units
 * @param stream the file stream
 */
Peter W. Draper's avatar
Peter W. Draper committed
618
619
void units_struct_dump(const struct unit_system* us, FILE* stream) {
  restart_write_blocks((void*)us, sizeof(struct unit_system), 1, stream,
620
                       "units", "units");
Peter W. Draper's avatar
Peter W. Draper committed
621
622
623
624
625
626
627
628
}

/**
 * @brief Restore a units struct from the given FILE as a stream of bytes.
 *
 * @param us the units
 * @param stream the file stream
 */
Peter W. Draper's avatar
Peter W. Draper committed
629
void units_struct_restore(const struct unit_system* us, FILE* stream) {
Peter W. Draper's avatar
Peter W. Draper committed
630
631
  restart_read_blocks((void*)us, sizeof(struct unit_system), 1, stream, NULL,
                      "units");
Peter W. Draper's avatar
Peter W. Draper committed
632
}