Skip to content

Commit c30bd2d

Browse files
authored
Merge pull request microhh#324 from bartvstratum/main_rho_restart
Write base state `rho` to its own restart file.
2 parents 9c3b9f2 + 2e85efb commit c30bd2d

File tree

6 files changed

+93
-8
lines changed

6 files changed

+93
-8
lines changed

include/fields.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,8 @@ class Fields
117117

118118
void save(int);
119119
void load(int);
120+
void save_rhoref();
121+
void load_rhoref();
120122

121123
TF check_momentum();
122124
TF check_tke();

python/microhh_tools.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -495,6 +495,7 @@ def restart_pre(origin, timestr):
495495
fnames += glob.glob('../' + origin + '/grid.0000000')
496496
fnames += glob.glob('../' + origin + '/fftwplan.0000000')
497497
fnames += glob.glob('../' + origin + '/thermo_basestate.0000000')
498+
fnames += glob.glob('../' + origin + '/rhoref.0000000')
498499
fnames += glob.glob('../' + origin + '/*.' + timestr)
499500
for file in fnames:
500501
shutil.copy(file, '.')

src/fields.cxx

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1208,6 +1208,10 @@ void Fields<TF>::create_stats(Stats<TF>& stats)
12081208
// (Turbulence) Kinetic Energy
12091209
stats.add_prof("ke" , "Kinetic energy" , "m2 s-2", "z", group_name);
12101210
stats.add_prof("tke", "Turbulent kinetic energy" , "m2 s-2", "z", group_name);
1211+
1212+
// Base state density.
1213+
stats.add_fixed_prof("rhoref", "Full level density dynamic core", "kg m-3", "z" , group_name, this->rhoref);
1214+
stats.add_fixed_prof("rhorefh", "Half level density dynamic core", "kg m-3", "zh", group_name, this->rhorefh);
12111215
}
12121216

12131217
// Add time series of scalar surface values
@@ -1245,6 +1249,7 @@ void Fields<TF>::save(int n)
12451249
auto tmp1 = get_tmp();
12461250
auto tmp2 = get_tmp();
12471251

1252+
// Save all prognostic fields.
12481253
int nerror = 0;
12491254
for (auto& f : ap)
12501255
{
@@ -1336,6 +1341,81 @@ void Fields<TF>::load(int n)
13361341
throw std::runtime_error("Error loading fields");
13371342
}
13381343

1344+
template<typename TF>
1345+
void Fields<TF>::save_rhoref()
1346+
{
1347+
auto& gd = grid.get_grid_data();
1348+
1349+
char filename[256];
1350+
int nerror = 0;
1351+
std::sprintf(filename, "%s.%07d", "rhoref", 0);
1352+
master.print_message("Saving \"%s\" ... ", filename);
1353+
1354+
if (master.get_mpiid() == 0)
1355+
{
1356+
FILE *pFile;
1357+
pFile = fopen(filename, "ab");
1358+
1359+
if (pFile == NULL)
1360+
{
1361+
master.print_message("FAILED\n");
1362+
nerror++;
1363+
}
1364+
else
1365+
{
1366+
master.print_message("OK\n");
1367+
1368+
fwrite(&rhoref [gd.kstart], sizeof(TF), gd.kmax, pFile);
1369+
fwrite(&rhorefh[gd.kstart], sizeof(TF), gd.kmax+1, pFile);
1370+
1371+
fclose(pFile);
1372+
}
1373+
}
1374+
1375+
master.sum(&nerror, 1);
1376+
if (nerror)
1377+
throw std::runtime_error("Error in writing basestate density.");
1378+
}
1379+
1380+
1381+
template<typename TF>
1382+
void Fields<TF>::load_rhoref()
1383+
{
1384+
auto& gd = grid.get_grid_data();
1385+
1386+
char filename[256];
1387+
int nerror = 0;
1388+
std::sprintf(filename, "%s.%07d", "rhoref", 0);
1389+
master.print_message("Loading \"%s\" ... ", filename);
1390+
1391+
if (master.get_mpiid() == 0)
1392+
{
1393+
FILE* pFile;
1394+
pFile = fopen(filename, "rb");
1395+
if (pFile == NULL)
1396+
{
1397+
master.print_message("FAILED\n");
1398+
++nerror;
1399+
}
1400+
else
1401+
{
1402+
master.print_message("OK\n");
1403+
1404+
if (fread(&rhoref[gd.kstart], sizeof(TF), gd.ktot , pFile) != (unsigned)gd.ktot )
1405+
++nerror;
1406+
if (fread(&rhorefh[gd.kstart], sizeof(TF), gd.ktot+1, pFile) != (unsigned)gd.ktot+1)
1407+
++nerror;
1408+
fclose(pFile);
1409+
}
1410+
}
1411+
1412+
if (nerror)
1413+
throw std::runtime_error("Error in loading basestate density.");
1414+
1415+
master.broadcast(&rhoref[gd.kstart], gd.ktot );
1416+
master.broadcast(&rhorefh[gd.kstart], gd.ktot+1);
1417+
}
1418+
13391419
#ifndef USECUDA
13401420
template<typename TF>
13411421
TF Fields<TF>::check_momentum()

src/model.cxx

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -252,6 +252,8 @@ void Model<TF>::load()
252252

253253
// Load the fields, and create the field statistics
254254
fields->load(timeloop->get_iotime());
255+
fields->load_rhoref();
256+
255257
fields->create_stats(*stats);
256258
fields->create_column(*column);
257259

@@ -315,6 +317,7 @@ void Model<TF>::save()
315317

316318
thermo->create_basestate(*input, *input_nc, *timeloop);
317319
thermo->save(timeloop->get_iotime());
320+
fields->save_rhoref();
318321

319322
boundary->create_cold_start(*input_nc);
320323
boundary->save(timeloop->get_iotime(), *thermo);

src/thermo_dry.cxx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -662,11 +662,11 @@ void Thermo_dry<TF>::create_stats(Stats<TF>& stats)
662662
if (stats.get_switch())
663663
{
664664
bs_stats = bs;
665+
665666
// Add base state profiles to statistics
666-
stats.add_fixed_prof("rhoref", "Full level basic state density", "kg m-3", "z", group_name, fields.rhoref );
667-
stats.add_fixed_prof("rhorefh", "Half level basic state density", "kg m-3", "zh", group_name, fields.rhorefh);
668667
stats.add_fixed_prof("thref", "Full level basic state potential temperature", "K", "z" , group_name, bs_stats.thref);
669668
stats.add_fixed_prof("threfh", "Half level basic state potential temperature", "K", "zh", group_name, bs_stats.thref);
669+
670670
if (bs_stats.swbasestate == Basestate_type::anelastic)
671671
{
672672
stats.add_fixed_prof("phydro", "Full level hydrostatic pressure", "Pa", "z" , group_name, bs_stats.pref );

src/thermo_moist.cxx

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1916,24 +1916,23 @@ void Thermo_moist<TF>::create_stats(Stats<TF>& stats)
19161916
// Add variables to the statistics
19171917
if (stats.get_switch())
19181918
{
1919-
/* Add fixed base-state density and temperature profiles. Density should probably be in fields (?), but
1920-
there the statistics are initialized before thermo->create() is called */
1921-
stats.add_fixed_prof("rhoref", "Full level basic state density", "kg m-3", "z" , group_name, bs.rhoref );
1922-
stats.add_fixed_prof("rhorefh", "Half level basic state density", "kg m-3", "zh", group_name, bs.rhorefh);
1919+
// Do we also want this time dependent with `swupdatebasestate`?
19231920
stats.add_fixed_prof("thvref", "Full level basic state virtual potential temperature", "K", "z" , group_name, bs.thvref);
19241921
stats.add_fixed_prof("thvrefh", "Half level basic state virtual potential temperature", "K", "zh", group_name, bs.thvrefh);
19251922

19261923
if (bs_stats.swupdatebasestate)
19271924
{
19281925
stats.add_prof("phydro", "Full level hydrostatic pressure", "Pa", "z" , group_name);
19291926
stats.add_prof("phydroh", "Half level hydrostatic pressure", "Pa", "zh", group_name);
1930-
stats.add_prof("rho", "Full level density", "kg m-3", "z" , group_name);
1931-
stats.add_prof("rhoh", "Half level density", "kg m-3", "zh", group_name);
1927+
stats.add_prof("rho", "Full level thermodynamic density", "kg m-3", "z" , group_name);
1928+
stats.add_prof("rhoh", "Half level thermodynamic density", "kg m-3", "zh", group_name);
19321929
}
19331930
else
19341931
{
19351932
stats.add_fixed_prof("pydroh", "Full level hydrostatic pressure", "Pa", "z" , group_name, bs.pref);
19361933
stats.add_fixed_prof("phydroh", "Half level hydrostatic pressure", "Pa", "zh", group_name, bs.prefh);
1934+
stats.add_fixed_prof("rho", "Full level thermodynamic density", "kg m-3", "z" , group_name, bs.rhoref);
1935+
stats.add_fixed_prof("rhoh", "Half level thermodynamic density", "kg m-3", "zh", group_name, bs.rhorefh);
19371936
}
19381937

19391938
auto thv = fields.get_tmp();

0 commit comments

Comments
 (0)