CPD.pm

filter: Statementstmt 98.4% 124 / 126 Branchbran 50.0% 4 / 8 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 97.7% 43 / 44 Podpod 0.0% 0 / 31 totaltotal 81.8% 171 / 209 CC 5 SCAR 2.1
CC
5
Cov
96%
coulomb_logarithm
3.1
_lookup_species
2.0
mean_free_path
2.0
CRAP 1.2
« CPD/Stellarator.pm ↑ Summary
1 package Physics::CPD;
2  
3 6 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 6 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 6 ▶use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 6 ▶use Carp qw(croak);
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 6 our $VERSION = '0.03';
9  
10 #---------------------------------------------------------------------------
11 # Physical constants (SI units, CODATA 2018)
12 #---------------------------------------------------------------------------
13 6 ▶use constant {
Subroutine BEGIN: called CC=1 SCAR=0.0
14 6 ELEMENTARY_CHARGE => 1.602176634e-19, # C
15 ELECTRON_MASS => 9.1093837015e-31, # kg
16 PROTON_MASS => 1.67262192369e-27, # kg
17 ATOMIC_MASS_UNIT => 1.66053906660e-27, # kg
18 BOLTZMANN => 1.380649e-23, # J/K
19 VACUUM_PERMITTIVITY => 8.8541878128e-12, # F/m
20 VACUUM_PERMEABILITY => 1.25663706212e-6, # H/m
21 SPEED_OF_LIGHT => 299792458, # m/s
22 EV_TO_JOULE => 1.602176634e-19, # J per eV
23 EV_TO_KELVIN => 11604.51812, # K per eV
24 PI => 3.14159265358979,
25 6 };
26  
27 # atomic species table: name => [ mass number A, charge number Z ]
28 6 my %SPECIES = (
29 'e' => [ 5.48579909065e-4, -1 ],
30 'H' => [ 1.00782503207, 1 ],
31 'p' => [ 1.00727646688, 1 ],
32 'D' => [ 2.01410177812, 1 ],
33 'T' => [ 3.01604928199, 1 ],
34 'He' => [ 4.002602, 2 ],
35 'He3' => [ 3.01602932, 2 ],
36 'He4' => [ 4.002602, 2 ],
37 'C' => [ 12.011, 6 ],
38 'O' => [ 15.999, 8 ],
39 );
40  
41 #---------------------------------------------------------------------------
42 # Attributes (all plasma quantities are given in SI unless noted)
43 # * temperatures are specified in electron-volts (eV), the plasma standard
44 #---------------------------------------------------------------------------
45 has electron_density => ( # n_e [m^-3]
46 is => 'rw',
47 4 ▶ default => sub { 1e20 },
Subroutine __ANON__: called CC=1 SCAR=0.0
48 6 );
49  
50 has electron_temperature => ( # T_e [eV]
51 is => 'rw',
52 5 ▶ default => sub { 1000 },
Subroutine __ANON__: called CC=1 SCAR=0.0
53 6 );
54  
55 has ion_temperature => ( # T_i [eV] (defaults to T_e)
56 is => 'rw',
57 lazy => 1,
58 1 ▶ default => sub { $_[0]->electron_temperature },
Subroutine __ANON__: called CC=1 SCAR=0.0
59 6 );
60  
61 has magnetic_field => ( # B [T]
62 is => 'rw',
63 2 ▶ default => sub { 1 },
Subroutine __ANON__: called CC=1 SCAR=0.0
64 6 );
65  
66 has ion_species => ( # chemical symbol, see %SPECIES
67 is => 'rw',
68 11 ▶ default => sub { 'H' },
Subroutine __ANON__: called CC=1 SCAR=0.0
69 6 );
70  
71 6 has ion_mass => ( # m_i [kg]
72 is => 'rw',
73 lazy => 1,
74 builder => '_build_ion_mass',
75 );
76  
77 6 has ion_charge => ( # Z (charge number)
78 is => 'rw',
79 lazy => 1,
80 builder => '_build_ion_charge',
81 );
82  
83 8 ▶sub _lookup_species {
Subroutine _lookup_species: called CC=2 SCAR=7.1
84 8 my ($self) = @_;
85 8 ▶ my $s = $SPECIES{ $self->ion_species }
Branch: 1 branch
BranchTrueFalseTotal
my $s = $SPECIES{$self->ion_species} or croak(q[Unknown ion species '] . $self->ion_species . q[']) 8 0 8
86 or croak "Unknown ion species '" . $self->ion_species . "'";
87 8 return $s;
88 }
89  
90 3 ▶sub _build_ion_mass {
Subroutine _build_ion_mass: called CC=1 SCAR=0.0
91 3 my ($self) = @_;
92 3 return $self->_lookup_species->[0] * ATOMIC_MASS_UNIT;
93 }
94  
95 5 ▶sub _build_ion_charge {
Subroutine _build_ion_charge: called CC=1 SCAR=0.0
96 5 my ($self) = @_;
97 5 return $self->_lookup_species->[1];
98 }
99  
100 #---------------------------------------------------------------------------
101 # Temperature / energy helpers
102 #---------------------------------------------------------------------------
103 31 ▶sub electron_temperature_joules { $_[0]->electron_temperature * EV_TO_JOULE }
Subroutine electron_temperature_joules: called CC=1 SCAR=0.0
Pod undocumented
104 20 ▶sub ion_temperature_joules { $_[0]->ion_temperature * EV_TO_JOULE }
Subroutine ion_temperature_joules: called CC=1 SCAR=0.0
Pod undocumented
105 2 ▶sub electron_temperature_kelvin { $_[0]->electron_temperature * EV_TO_KELVIN }
Subroutine electron_temperature_kelvin: called CC=1 SCAR=0.0
Pod undocumented
106 0 ▶sub ion_temperature_kelvin { $_[0]->ion_temperature * EV_TO_KELVIN }
Subroutine ion_temperature_kelvin: not called CC=1 SCAR=6.9
Pod undocumented
107  
108 # quasineutral ion density Z * n_i = n_e
109 22 ▶sub ion_density {
Subroutine ion_density: called CC=1 SCAR=0.0
Pod undocumented
110 22 my ($self) = @_;
111 22 return $self->electron_density / abs( $self->ion_charge );
112 }
113  
114 4 ▶sub mass_density { # ion mass density rho [kg/m^3]
Subroutine mass_density: called CC=1 SCAR=0.0
Pod undocumented
115 4 my ($self) = @_;
116 4 return $self->ion_density * $self->ion_mass;
117 }
118  
119 #---------------------------------------------------------------------------
120 # Characteristic frequencies
121 #---------------------------------------------------------------------------
122 # Electron plasma (Langmuir) frequency [rad/s]
123 3 ▶sub electron_plasma_frequency {
Subroutine electron_plasma_frequency: called CC=1 SCAR=0.0
Pod undocumented
124 3 my ($self) = @_;
125 3 my $ne = $self->electron_density;
126 3 return sqrt( $ne * ELEMENTARY_CHARGE**2
127 / ( VACUUM_PERMITTIVITY * ELECTRON_MASS ) );
128 }
129 3 ▶sub electron_plasma_frequency_hz { $_[0]->electron_plasma_frequency / ( 2 * PI ) }
Subroutine electron_plasma_frequency_hz: called CC=1 SCAR=0.0
Pod undocumented
130  
131 # Ion plasma frequency [rad/s]
132 1 ▶sub ion_plasma_frequency {
Subroutine ion_plasma_frequency: called CC=1 SCAR=0.0
Pod undocumented
133 1 my ($self) = @_;
134 1 my $ni = $self->ion_density;
135 1 my $q = $self->ion_charge * ELEMENTARY_CHARGE;
136 1 return sqrt( $ni * $q**2 / ( VACUUM_PERMITTIVITY * $self->ion_mass ) );
137 }
138 1 ▶sub ion_plasma_frequency_hz { $_[0]->ion_plasma_frequency / ( 2 * PI ) }
Subroutine ion_plasma_frequency_hz: called CC=1 SCAR=0.0
Pod undocumented
139  
140 # Electron cyclotron (gyro) frequency [rad/s]
141 4 ▶sub electron_cyclotron_frequency {
Subroutine electron_cyclotron_frequency: called CC=1 SCAR=0.0
Pod undocumented
142 4 my ($self) = @_;
143 4 return ELEMENTARY_CHARGE * $self->magnetic_field / ELECTRON_MASS;
144 }
145 3 ▶sub electron_cyclotron_frequency_hz { $_[0]->electron_cyclotron_frequency / ( 2 * PI ) }
Subroutine electron_cyclotron_frequency_hz: called CC=1 SCAR=0.0
Pod undocumented
146  
147 # Ion cyclotron frequency [rad/s]
148 2 ▶sub ion_cyclotron_frequency {
Subroutine ion_cyclotron_frequency: called CC=1 SCAR=0.0
Pod undocumented
149 2 my ($self) = @_;
150 2 return abs( $self->ion_charge ) * ELEMENTARY_CHARGE * $self->magnetic_field
151 / $self->ion_mass;
152 }
153 2 ▶sub ion_cyclotron_frequency_hz { $_[0]->ion_cyclotron_frequency / ( 2 * PI ) }
Subroutine ion_cyclotron_frequency_hz: called CC=1 SCAR=0.0
Pod undocumented
154  
155 #---------------------------------------------------------------------------
156 # Characteristic lengths and speeds
157 #---------------------------------------------------------------------------
158 # Debye length [m]
159 7 ▶sub debye_length {
Subroutine debye_length: called CC=1 SCAR=0.0
Pod undocumented
160 7 my ($self) = @_;
161 7 return sqrt( VACUUM_PERMITTIVITY * $self->electron_temperature_joules
162 / ( $self->electron_density * ELEMENTARY_CHARGE**2 ) );
163 }
164  
165 # Electron thermal speed v = sqrt(k T / m) [m/s]
166 5 ▶sub electron_thermal_velocity {
Subroutine electron_thermal_velocity: called CC=1 SCAR=0.0
Pod undocumented
167 5 my ($self) = @_;
168 5 return sqrt( $self->electron_temperature_joules / ELECTRON_MASS );
169 }
170  
171 # Ion thermal speed v = sqrt(k T / m) [m/s]
172 4 ▶sub ion_thermal_velocity {
Subroutine ion_thermal_velocity: called CC=1 SCAR=0.0
Pod undocumented
173 4 my ($self) = @_;
174 4 return sqrt( $self->ion_temperature_joules / $self->ion_mass );
175 }
176  
177 # Electron Larmor (gyro) radius using thermal speed [m]
178 3 ▶sub electron_gyroradius {
Subroutine electron_gyroradius: called CC=1 SCAR=0.0
Pod undocumented
179 3 my ($self) = @_;
180 3 return ELECTRON_MASS * $self->electron_thermal_velocity
181 / ( ELEMENTARY_CHARGE * $self->magnetic_field );
182 }
183  
184 # Ion Larmor (gyro) radius using thermal speed [m]
185 3 ▶sub ion_gyroradius {
Subroutine ion_gyroradius: called CC=1 SCAR=0.0
Pod undocumented
186 3 my ($self) = @_;
187 3 return $self->ion_mass * $self->ion_thermal_velocity
188 / ( abs( $self->ion_charge ) * ELEMENTARY_CHARGE * $self->magnetic_field );
189 }
190  
191 # Alfven speed v_A = B / sqrt(mu0 rho) [m/s]
192 4 ▶sub alfven_velocity {
Subroutine alfven_velocity: called CC=1 SCAR=0.0
Pod undocumented
193 4 my ($self) = @_;
194 4 return $self->magnetic_field
195 / sqrt( VACUUM_PERMEABILITY * $self->mass_density );
196 }
197  
198 # Ion-acoustic (sound) speed c_s = sqrt(Z k T_e / m_i) [m/s]
199 2 ▶sub ion_sound_speed {
Subroutine ion_sound_speed: called CC=1 SCAR=0.0
Pod undocumented
200 2 my ($self) = @_;
201 2 return sqrt( abs( $self->ion_charge ) * $self->electron_temperature_joules
202 / $self->ion_mass );
203 }
204  
205 #---------------------------------------------------------------------------
206 # Pressures, beta and stored-energy density
207 #---------------------------------------------------------------------------
208 # Kinetic plasma pressure p = n_e k T_e + n_i k T_i [Pa]
209 15 ▶sub plasma_pressure {
Subroutine plasma_pressure: called CC=1 SCAR=0.0
Pod undocumented
210 15 my ($self) = @_;
211 15 return $self->electron_density * $self->electron_temperature_joules
212 + $self->ion_density * $self->ion_temperature_joules;
213 }
214  
215 # Magnetic pressure B^2 / (2 mu0) [Pa]
216 10 ▶sub magnetic_pressure {
Subroutine magnetic_pressure: called CC=1 SCAR=0.0
Pod undocumented
217 10 my ($self) = @_;
218 10 return $self->magnetic_field**2 / ( 2 * VACUUM_PERMEABILITY );
219 }
220  
221 # Plasma beta = kinetic pressure / magnetic pressure (dimensionless)
222 8 ▶sub plasma_beta {
Subroutine plasma_beta: called CC=1 SCAR=0.0
Pod undocumented
223 8 my ($self) = @_;
224 8 return $self->plasma_pressure / $self->magnetic_pressure;
225 }
226  
227 #---------------------------------------------------------------------------
228 # Collisional parameters (NRL Plasma Formulary)
229 #---------------------------------------------------------------------------
230 # Coulomb logarithm for electron-ion collisions
231 8 ▶sub coulomb_logarithm {
Subroutine coulomb_logarithm: called CC=3 SCAR=11.3
Pod undocumented
232 8 my ($self) = @_;
233 8 my $ne_cm3 = $self->electron_density * 1e-6; # convert to cm^-3
234 8 my $Te = $self->electron_temperature; # eV
235 8 my $Z = abs( $self->ion_charge );
236 8 my $ln;
237 8 ▶ if ( $Te > 10 * $Z**2 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($Te > 10 * $Z ** 2) { } 8 0 8
238 8 $ln = 24 - log( sqrt($ne_cm3) / $Te );
239 }
240 else {
241 0 $ln = 23 - log( sqrt($ne_cm3) * $Z * $Te**-1.5 );
242 }
243 8 ▶ return $ln < 1 ? 1 : $ln;
Branch: 1 branch
BranchTrueFalseTotal
$ln < 1 ? : 0 8 8
244 }
245  
246 # Electron-ion collision frequency [s^-1]
247 2 ▶sub collision_frequency {
Subroutine collision_frequency: called CC=1 SCAR=0.0
Pod undocumented
248 2 my ($self) = @_;
249 2 my $ne_cm3 = $self->electron_density * 1e-6;
250 2 my $Te = $self->electron_temperature;
251 2 my $Z = abs( $self->ion_charge );
252 2 return 2.91e-6 * $Z * $ne_cm3 * $self->coulomb_logarithm * $Te**-1.5;
253 }
254  
255 # Electron mean free path [m]
256 1 ▶sub mean_free_path {
Subroutine mean_free_path: called CC=2 SCAR=7.1
Pod undocumented
257 1 my ($self) = @_;
258 1 my $nu = $self->collision_frequency;
259 1 ▶ return $nu > 0 ? $self->electron_thermal_velocity / $nu : 'inf';
Branch: 1 branch
BranchTrueFalseTotal
$nu > 0 ? : 1 0 1
260 }
261  
262 # Parallel Spitzer resistivity [Ohm m]
263 2 ▶sub spitzer_resistivity {
Subroutine spitzer_resistivity: called CC=1 SCAR=0.0
Pod undocumented
264 2 my ($self) = @_;
265 2 my $Z = abs( $self->ion_charge );
266 2 my $Te = $self->electron_temperature; # eV
267 2 return 5.2e-5 * $Z * $self->coulomb_logarithm * $Te**-1.5;
268 }
269  
270 #---------------------------------------------------------------------------
271 # Dimensionless plasma parameter
272 #---------------------------------------------------------------------------
273 # Number of particles in a Debye sphere N_D = (4/3) pi n lambda_D^3
274 3 ▶sub plasma_parameter {
Subroutine plasma_parameter: called CC=1 SCAR=0.0
Pod undocumented
275 3 my ($self) = @_;
276 3 return ( 4 / 3 ) * PI * $self->electron_density * $self->debye_length**3;
277 }
278  
279 #---------------------------------------------------------------------------
280 # Reporting
281 #---------------------------------------------------------------------------
282 1 ▶sub as_hash {
Subroutine as_hash: called CC=1 SCAR=0.0
Pod undocumented
283 1 my ($self) = @_;
284 return {
285 1 electron_density => $self->electron_density,
286 electron_temperature_eV => $self->electron_temperature,
287 ion_temperature_eV => $self->ion_temperature,
288 magnetic_field_T => $self->magnetic_field,
289 ion_species => $self->ion_species,
290 electron_plasma_frequency_Hz => $self->electron_plasma_frequency_hz,
291 ion_plasma_frequency_Hz => $self->ion_plasma_frequency_hz,
292 electron_cyclotron_freq_Hz => $self->electron_cyclotron_frequency_hz,
293 ion_cyclotron_frequency_Hz => $self->ion_cyclotron_frequency_hz,
294 debye_length_m => $self->debye_length,
295 electron_gyroradius_m => $self->electron_gyroradius,
296 ion_gyroradius_m => $self->ion_gyroradius,
297 electron_thermal_velocity_ms => $self->electron_thermal_velocity,
298 ion_thermal_velocity_ms => $self->ion_thermal_velocity,
299 alfven_velocity_ms => $self->alfven_velocity,
300 ion_sound_speed_ms => $self->ion_sound_speed,
301 plasma_pressure_Pa => $self->plasma_pressure,
302 magnetic_pressure_Pa => $self->magnetic_pressure,
303 plasma_beta => $self->plasma_beta,
304 coulomb_logarithm => $self->coulomb_logarithm,
305 collision_frequency_Hz => $self->collision_frequency,
306 mean_free_path_m => $self->mean_free_path,
307 spitzer_resistivity_Ohm_m => $self->spitzer_resistivity,
308 plasma_parameter => $self->plasma_parameter,
309 };
310 }
311  
312 1 ▶sub report {
Subroutine report: called CC=1 SCAR=0.0
Pod undocumented
313 1 my ($self) = @_;
314 1 my @lines;
315 1 push @lines, "== Computational Plasma Dynamics (Physics::CPD) ==";
316 1 push @lines, sprintf( " electron density n_e = %.3e m^-3", $self->electron_density );
317 1 push @lines, sprintf( " electron temperature T_e = %.3g eV (%.3g K)",
318 $self->electron_temperature, $self->electron_temperature_kelvin );
319 1 push @lines, sprintf( " ion temperature T_i = %.3g eV (%s, Z=%d)",
320 $self->ion_temperature, $self->ion_species, $self->ion_charge );
321 1 push @lines, sprintf( " magnetic field B = %.3g T", $self->magnetic_field );
322 1 push @lines, " -- derived quantities --";
323 1 push @lines, sprintf( " plasma frequency f_pe = %.3e Hz", $self->electron_plasma_frequency_hz );
324 1 push @lines, sprintf( " e- cyclotron freq f_ce = %.3e Hz", $self->electron_cyclotron_frequency_hz );
325 1 push @lines, sprintf( " ion cyclotron freq f_ci = %.3e Hz", $self->ion_cyclotron_frequency_hz );
326 1 push @lines, sprintf( " Debye length lambda_D= %.3e m", $self->debye_length );
327 1 push @lines, sprintf( " e- gyroradius r_Le = %.3e m", $self->electron_gyroradius );
328 1 push @lines, sprintf( " ion gyroradius r_Li = %.3e m", $self->ion_gyroradius );
329 1 push @lines, sprintf( " Alfven speed v_A = %.3e m/s", $self->alfven_velocity );
330 1 push @lines, sprintf( " ion sound speed c_s = %.3e m/s", $self->ion_sound_speed );
331 1 push @lines, sprintf( " plasma pressure p = %.3e Pa", $self->plasma_pressure );
332 1 push @lines, sprintf( " plasma beta beta = %.3f %%", 100 * $self->plasma_beta );
333 1 push @lines, sprintf( " Coulomb log lnLambda= %.2f", $self->coulomb_logarithm );
334 1 push @lines, sprintf( " plasma parameter N_D = %.3e", $self->plasma_parameter );
335 1 return join( "\n", @lines ) . "\n";
336 }
337  
338 6 1;
339  
340 __END__
« CPD/Stellarator.pm ↑ Summary