-
Notifications
You must be signed in to change notification settings - Fork 2
/
NetCDFIO.cpp
371 lines (283 loc) · 16.8 KB
/
NetCDFIO.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
#include "NetCDFIO.hpp"
#include "MirrorPlasma.hpp"
// Code for NetCDF interface
//
using namespace netCDF;
NetCDFIO::NetCDFIO()
{
}
void NetCDFIO::Open( const std::string &file )
{
filename = file;
data_file.open( file, netCDF::NcFile::FileMode::replace );
TimeDim = data_file.addDim( "t" );
TimeVar = data_file.addVar( "t", netCDF::NcDouble(), TimeDim );
TimeVar.putAtt( "description", "Time since start of simulation" );
TimeVar.putAtt( "units", "s" );
TimeVar.putVar( {0}, 0.0 );
}
void NetCDFIO::Close()
{
filename = "";
data_file.close();
}
NetCDFIO::~NetCDFIO()
{
if ( filename != "" )
Close();
}
void NetCDFIO::AddScalarVariable( std::string name, std::string description, std::string units, double value )
{
NcVar newvar = data_file.addVar( name, netCDF::NcDouble() );
newvar.putAtt( "description", description );
if ( units != "" )
newvar.putAtt( "units", units );
double tmp = value;
newvar.putVar( &tmp );
}
void NetCDFIO::AddTextVariable( std::string name, std::string description, std::string units, std::string text )
{
NcVar newvar = data_file.addVar( name, netCDF::NcString() );
newvar.putAtt( "description", description );
if ( units != "" )
newvar.putAtt( "units", units );
std::string value( text );
const char *pStr = value.c_str();
newvar.putVar( &pStr );
}
void NetCDFIO::AddTimeSeries( std::string name, std::string description, std::string units, double InitialValue )
{
NcVar newvar = data_file.addVar( name, netCDF::NcDouble(), TimeDim );
newvar.putAtt( "description", description );
if ( units != "" )
newvar.putAtt( "units", units );
newvar.putVar( {0}, InitialValue );
}
size_t NetCDFIO::AddTimeSlice( double T )
{
size_t next = TimeDim.getSize();
std::vector<size_t> v = {next};
TimeVar.putVar( v, T );
return next;
}
void NetCDFIO::AppendToTimeSeries( std::string const& name, double value, size_t tIndex )
{
NcVar variable = data_file.getVar( name );
std::vector<size_t> v = {tIndex};
variable.putVar( v, value );
}
// MirrorPlasma routines that use NetCDFIO
//
void MirrorPlasma::InitialiseNetCDF()
{
if ( !isTimeDependent )
return;
if ( NetcdfOutputFile == "" )
return;
nc_output.Open( NetcdfOutputFile );
nc_output.AddScalarVariable( "R_min","Innermost plasma radius", "m", PlasmaInnerRadius() );
nc_output.AddScalarVariable( "R_max","Outermost plasma radius", "m", PlasmaOuterRadius() );
nc_output.AddScalarVariable( "R_plasma","Plasma radius on centreline", "m", PlasmaCentralRadius() );
nc_output.AddScalarVariable( "IonDensity","Density of bulk ion species in the central cell", "10^20 m^-3", IonDensity );
nc_output.AddScalarVariable( "ElectronDensity","Density of electrons in the central cell", "10^20 m^-3", ElectronDensity );
nc_output.AddScalarVariable( "MirrorRatio","Ratio of Minimum to Maximum B along a field line", "", MirrorRatio );
// Time Dependent Variables
nc_output.AddTimeSeries( "Voltage", "Voltage drop across the plasma","V", ImposedVoltage );
nc_output.AddTimeSeries( "AmbipolarPhi", "Parallel phi drop","V", AmbipolarPhi() );
nc_output.AddTimeSeries( "MachNumber", "Plasma velocity divided by Sqrt(T_e/m_i)", "", MachNumber );
nc_output.AddTimeSeries( "IonTemperature", "Temperature of the bulk ion species", "keV", IonTemperature );
nc_output.AddTimeSeries( "ElectronTemperature", "Temperature of the bulk ion species", "keV", ElectronTemperature );
nc_output.AddTimeSeries( "Current", "Radial current through the plasma","A", RadialCurrent() );
nc_output.AddTimeSeries( "ViscousTorque", "Viscous Torque","", ViscousTorque() );
nc_output.AddTimeSeries( "ParAngMomLoss", "Parallel Angular Momentum Loss","", ParallelAngularMomentumLossRate() );
nc_output.AddTimeSeries( "ViscousHeating", "Viscous Heating","W/m^3", ViscousHeating() );
nc_output.AddTimeSeries( "ParIonHeatLoss", "Parallel Ion Heat Loss","W/m^3", ParallelIonHeatLoss() );
nc_output.AddTimeSeries( "ParElecHeatLoss", "Parallel Electron Heat Loss","W/m^3", ParallelElectronHeatLoss() );
nc_output.AddTimeSeries( "PerpHeatLoss", "Perp Ion Heat Loss","W/m^3", ClassicalIonHeatLoss() );
}
void MirrorPlasma::WriteTimeslice( double tNew )
{
if ( !isTimeDependent )
return;
if ( NetcdfOutputFile == "" )
return;
int tIndex = nc_output.AddTimeSlice( tNew );
if ( ::fabs( tNew - time ) > 1e-6 )
{
std::cerr << "Irregularity in output times" << std::endl;
}
nc_output.AppendToTimeSeries( "Voltage", ImposedVoltage, tIndex );
nc_output.AppendToTimeSeries( "AmbipolarPhi", AmbipolarPhi(), tIndex );
nc_output.AppendToTimeSeries( "MachNumber", MachNumber, tIndex );
nc_output.AppendToTimeSeries( "IonTemperature", IonTemperature, tIndex );
nc_output.AppendToTimeSeries( "ElectronTemperature", ElectronTemperature, tIndex );
nc_output.AppendToTimeSeries( "Current", RadialCurrent(), tIndex );
nc_output.AppendToTimeSeries( "ViscousTorque", ViscousTorque(), tIndex );
nc_output.AppendToTimeSeries( "ParAngMomLoss", ParallelAngularMomentumLossRate(), tIndex );
nc_output.AppendToTimeSeries( "ViscousHeating", ViscousHeating(), tIndex );
nc_output.AppendToTimeSeries( "ParIonHeatLoss", ParallelIonHeatLoss(), tIndex );
nc_output.AppendToTimeSeries( "ParElecHeatLoss", ParallelElectronHeatLoss(), tIndex );
nc_output.AppendToTimeSeries( "PerpHeatLoss", ClassicalIonHeatLoss(), tIndex );
}
void MirrorPlasma::FinaliseNetCDF()
{
if ( !isTimeDependent )
return;
if ( NetcdfOutputFile == "" )
return;
nc_output.Close();
}
void MirrorPlasma::ReadVoltageFile( std::string const& fName )
{
NcFile voltage_file( fName, NcFile::FileMode::read );
if ( voltage_file.isNull() )
throw std::runtime_error( "Could Not Open Voltage File " + fName + " as a NetCDF file" );
NcVar time = voltage_file.getVar( "Time" );
NcDim time_dim = voltage_file.getDim( "Time" );
NcVar volt = voltage_file.getVar( "Voltage" );
size_t nEntries = time_dim.getSize();
std::vector<double> time_data( nEntries ),volt_data( nEntries );
time.getVar( time_data.data() );
volt.getVar( volt_data.data() );
#ifdef DEBUG
std::cerr << "Last time in voltage file is " << time_data.back() << " with voltage " << volt_data.back() << std::endl;
#endif
constexpr double epsilon = 1e-10; // extend the data just beyond the simulation end time to cope with floating point
time_data.emplace_back( time_data.back() + epsilon );
volt_data.emplace_back( volt_data.back() ); // Extend with final value
VoltageFunction = std::make_unique<interpolant>( std::move( time_data ), std::move( volt_data ) );
}
void MirrorPlasma::WriteNetCDFReport(const std::map<std::string, double>* parameterMap, int currentRun, int totalRuns)
{
/*
*
*/
if ( isTimeDependent )
return;
if ( NetcdfOutputFile == "" )
return;
std::string outputFileName = NetcdfOutputFile;
if ( totalRuns > 1 )
outputFileName = NetcdfOutputFile.insert( std::min(NetcdfOutputFile.find_last_of("."), NetcdfOutputFile.size()) , "_" + std::to_string(currentRun + 1));
nc_output.Open( outputFileName );
if( totalRuns > 1 && parameterMap != nullptr )
{
nc_output.AddScalarVariable( "BatchRunIndex","","",currentRun + 1.0 );
for (auto const &pair: *parameterMap)
{
nc_output.AddScalarVariable( pair.first, "Input Parameter","",pair.second );
}
}
nc_output.AddTextVariable( "FuelName", "Fuel ion species", "", IonSpecies.Name );
nc_output.AddScalarVariable( "ZEff", "Effective ion charge", "", Zeff );
nc_output.AddScalarVariable( "IonTemperature", "Temperature of the bulk ion species", "keV", IonTemperature );
nc_output.AddScalarVariable( "ElectronTemperature", "Temperature of the bulk ion species", "keV", ElectronTemperature );
nc_output.AddScalarVariable( "IonDensity","Density of bulk ion species in the central cell", "10^20 m^-3", IonDensity );
nc_output.AddScalarVariable( "ElectronDensity","Density of electrons in the central cell", "10^20 m^-3", ElectronDensity );
nc_output.AddScalarVariable( "R_min","Innermost plasma radius", "m", PlasmaInnerRadius() );
nc_output.AddScalarVariable( "R_max","Outermost plasma radius", "m", PlasmaOuterRadius() );
nc_output.AddScalarVariable( "R_plasma","Plasma mid-radius on midplane", "m", PlasmaCentralRadius() );
nc_output.AddScalarVariable( "R_wall", "Radius of central cell (first wall)", "m", WallRadius );
nc_output.AddScalarVariable( "R_throat", "Outer Radius of the plasma at the mirror throat", "m", (PlasmaColumnWidth + AxialGapDistance) /::sqrt( MirrorRatio ) );
nc_output.AddScalarVariable( "L_plasma", "Axial Length of the plasma", "m", PlasmaLength );
nc_output.AddScalarVariable( "V_plasma", "Plasma Volume", "m^3", PlasmaVolume() );
nc_output.AddScalarVariable( "B_midplane", "Magnetic Field Strength in the Central Cell", "T", CentralCellFieldStrength );
nc_output.AddScalarVariable( "B_throat", "Magnetic Field Strength in the mirror throat", "T", CentralCellFieldStrength * MirrorRatio );
nc_output.AddScalarVariable( "RhoIon", "Ion Larmor Radius in the central cell", "m", IonLarmorRadius() );
nc_output.AddScalarVariable( "a", "Typical plasma scale length", "m", PlasmaColumnWidth/2.0 );
nc_output.AddScalarVariable( "RhoStar", "Normalised larmor radius - rho_i/a", "", PlasmaColumnWidth / ( 2.0 * IonLarmorRadius() ) );
nc_output.AddScalarVariable( "AuxiliaryHeating", "Auxiliary electron heating.", "W", AuxiliaryHeating * 1e6 );
if ( IncludeAlphaHeating ) {
nc_output.AddScalarVariable( "AlphaHeating", "Electron heating from alpha particles", "MW", AlphaHeating()*PlasmaVolume()*1e6 ) ;
nc_output.AddScalarVariable( "AlphaPromptLossRate", "Prompt loss rate of energetic alpha particles", "s^-1", PromptAlphaLossFraction() * AlphaProductionRate() * PlasmaVolume() );
nc_output.AddScalarVariable( "AlphaPromptHeating", "Energy loss rate from prompt alpha particles", "W", AlphaPromptLosses() * PlasmaVolume() * 1e6 );
nc_output.AddScalarVariable( "AlphaSlowingDown", "Alpha particle slowing down time", "s", SlowingDownTime() );
}
nc_output.AddScalarVariable( "Mach", "Operating Mach Number", "", MachNumber );
nc_output.AddScalarVariable( "AlfvenMach", "Alfven Mach Number", "", AlfvenMachNumber() );
double v = SoundSpeed() * MachNumber;
nc_output.AddScalarVariable( "Velocity","Peak rotational velocity", "m/s", v );
double Rmid = PlasmaCentralRadius();
nc_output.AddScalarVariable( "AngularVelocity","Angular veloticy at the plasma centre", "s^-1", v/Rmid );
nc_output.AddScalarVariable( "ViscousHeating", "Viscous Heating", "W", ViscousHeating() * PlasmaVolume() );
if ( Zeff > 0.0 )
{
nc_output.AddScalarVariable( "BremsstrahlungLosses", "Bremsstrahlung losses","W",BremsstrahlungLosses()*PlasmaVolume() );
nc_output.AddScalarVariable( "CyclotronLosses","Cyclotron emission losses","W", CyclotronLosses()*PlasmaVolume() );
}
if ( IncludeCXLosses ) {
nc_output.AddScalarVariable( "CXLosses", "Heat loss due to charge exchange with neutrals", "W", CXHeatLosses()*PlasmaVolume() );
}
nc_output.AddScalarVariable( "Voltage", "Total potential difference across the plasma","V",ElectricPotential() );
double JRadial = ::fabs( RadialCurrent() );
nc_output.AddScalarVariable( "JRadial", "Radial Current Drawn from Power Supply", "A", JRadial );
nc_output.AddScalarVariable( "RotationPower", "Power Required (at the plasma) to support rotation","W", ElectricPotential() * JRadial );
double omega = v/Rmid;
nc_output.AddScalarVariable( "ViscousTorque","Power Loss from viscous torque", "W", ViscousTorque()*omega*PlasmaVolume() );
nc_output.AddScalarVariable( "ParallelRotationLosses","Power Loss from parallel losses", "W", ParallelAngularMomentumLossRate()*omega*PlasmaVolume() );
if ( IncludeCXLosses ) {
nc_output.AddScalarVariable( "CXRotationLosses","Rotational power loss from charge exchange", "W", CXMomentumLosses()*omega*PlasmaVolume() );
}
double KineticStoredEnergy = KineticEnergy();
double ThermalStoredEnergy = ThermalEnergy();
nc_output.AddScalarVariable( "KineticEnergy", "Plasma kinetic energy", "J", KineticStoredEnergy );
nc_output.AddScalarVariable( "ThermalEnergy", "Thermal energy", "J", ThermalStoredEnergy );
nc_output.AddScalarVariable( "StoredEnergy", "Total stored energy", "J", KineticStoredEnergy + ThermalStoredEnergy );
nc_output.AddScalarVariable( "TauE", "Energy Confinement Time", "s", EnergyConfinementTime() );
double ParallelConfinementTime = ( 1.5 * ElectronDensity * ElectronTemperature + 1.5 * IonDensity * IonTemperature ) * ( ReferenceDensity * ReferenceTemperature )/( ParallelElectronHeatLoss() + ParallelIonHeatLoss() );
double PerpConfinementTime = ( 1.5 * IonDensity * IonTemperature * ReferenceDensity * ReferenceTemperature )/ClassicalIonHeatLoss();
double CXConfinementTime = IonDensity*ReferenceDensity / CXLossRate();
nc_output.AddScalarVariable( "ParallelConfinementTime", "Confinement time from parallel losses", "s", ParallelConfinementTime );
nc_output.AddScalarVariable( "PerpConfinementTime", "Confinement time from perpendicular losses", "s", PerpConfinementTime );
if ( IncludeCXLosses ) {
nc_output.AddScalarVariable( "CXConfinementTime", "Confinement time from charge-exchange losses", "s", CXConfinementTime );
}
double ParticleConfinementTime = ( IonDensity * ReferenceDensity ) /( ParallelIonParticleLoss() + ClassicalIonParticleLosses() + CXLossRate() );
nc_output.AddScalarVariable( "ParticleConfinementTime", "Particle (Ion) Confinement Time ~= ", "s", ParticleConfinementTime );
nc_output.AddScalarVariable( "EquilibrationTime", "Ion-Electron Temperature Equilibration Time", "s", CollisionalTemperatureEquilibrationTime() );
double ElectronLossRate = ParallelElectronParticleLoss() + ClassicalElectronParticleLosses();
nc_output.AddScalarVariable( "ParticleLossRate","Total plasma loss rate ","electrons/s", ElectronLossRate*PlasmaVolume() );
nc_output.AddScalarVariable( "NeutralDensity","Density of neutral particles", "m^-3", NeutralDensity * ReferenceDensity );
if ( !FixedNeutralDensity ) {
ComputeSteadyStateNeutrals();
}
if ( IncludeCXLosses ) {
double CXR = CXLossRate() * PlasmaVolume();
nc_output.AddScalarVariable( "CXRate","Rate of charge-exchange reactions","s^-1", CXR );
nc_output.AddScalarVariable( "CXHeatLoss","Thermal energy loss from charge-exchange","W", CXR * IonTemperature * ReferenceTemperature );
}
double Resistance = ElectricPotential() / JRadial;
double Capacitance = 2.0*KineticStoredEnergy / ( ElectricPotential() * ElectricPotential() );
nc_output.AddScalarVariable( "Resistance","Electrical resistance of the plasma","Ohms", Resistance );
nc_output.AddScalarVariable( "Capacitance","Electrical capacitance of the plasma","Farads", Capacitance );
nc_output.AddScalarVariable( "Beta","Plasma beta","%",Beta()*100 );
nc_output.AddScalarVariable( "IonCollisionality","Ratio of ion collision frequency to ion bounce time","",NuStar() );
nc_output.AddScalarVariable( "HallParameter","Ratio of ion collision frequency to cyclotron frequency ( Omega_i * tau_ii)","",IonCyclotronFrequency()*IonCollisionTime() );
double TripleProduct = IonDensity * ReferenceDensity * IonTemperature * EnergyConfinementTime();
nc_output.AddScalarVariable( "TripleProduct", "n_i T_i τ_E", "keV s/m^3", TripleProduct );
if ( ReportThrust ) {
nc_output.AddScalarVariable( "IonThrust","Thrust provided by fuel ions lost from the central cell","", ParallelIonThrust() );
if ( IncludeAlphaHeating )
nc_output.AddScalarVariable( "AlphaThrust","Thrust provided by alpha particles promptly lost from the central cell","", PromptAlphaThrust() );
}
if ( ReportNuclearDiagnostics ) {
if ( IonSpecies.Name == "Deuterium" ) {
nc_output.AddScalarVariable( "DDNeutrons", "D/D Neutrons (2.45 MeV) per second","n/s", DDNeutronRate() );
double Yield = ( 14.1/3.52 + 1.0 ) * FusionAlphaPowerDensity()*PlasmaVolume();
nc_output.AddScalarVariable( "DTEquivalentQ","Effective D/T Figure of Merit, assuming an identical plasma. Scientific D-T equivalent Q.","",Yield/( ElectricPotential()*JRadial ) );
} else if ( IonSpecies.Name == "Deuterium/Tritium Fuel" ) {
double FusionAlphaPower = FusionAlphaPowerDensity()*PlasmaVolume();
double FusionNeutronPower = ( 14.1/3.52 ) * FusionAlphaPower;
nc_output.AddScalarVariable( "FusionAlphaPower", "Fusion Power Output as Alphas","W",FusionAlphaPower*1e6 );
nc_output.AddScalarVariable( "FusionNeutronPower", "Fusion Power Output as Alphas","W",FusionNeutronPower*1e6 );
nc_output.AddScalarVariable( "NeutronWallLoading","Power impinging on the first wall as energetic neutrons","W/m^2",NeutronWallLoading()*1e6 );
nc_output.AddScalarVariable( "ThermalOutputPower","Total Thermal Power Output","W", ThermalPowerOutput() *1e6 );
double TotalOutputPower1 = FusionNeutronPower + FusionAlphaPower;
double TotalInputPower1 = ElectricPotential() * JRadial / 1e6; // Because the output is in MW
nc_output.AddScalarVariable( "ScientificQ","Scientific Q-factor, not including tritium breeding, or engineering efficiencies","", TotalOutputPower1/TotalInputPower1 );
} else {
std::cerr << "Nuclear Reaction Diagnostics requested but no diagnostics exist for ion species: " << IonSpecies.Name << std::endl;
}
}
nc_output.Close();
}