class CodeRunner::Trinity
This module reads data from the new diagnostics output file <run_name>.out.nc.
It defines a new generic reader function which can read any variable in the new netcdf file using a standard set of index constraints
This is a customised subclass of the CodeRunner::Run class which allows CodeRunner
to run and analyse the multiscale gyrokinetic turbulent transport solver Trinity
.
It generates the Trinity
input file, and both analyses the results and allows easy plotting of them. It also interfaces with the GS2 CodeRunner
module to allow analysis of the individual GS2 results if GS2 is being used as the flux code.
Public Class Methods
# File lib/trinitycrmod/trinity.rb, line 669 def self.defaults_file_header <<EOF1 ############################################################################ # # # Automatically generated defaults file for the Trinity CodeRunner module # # # # This defaults file specifies a set of defaults for Trinity which are # # used by CodeRunner to set up and run Trinity simulations. # # # ############################################################################ # Created: #{Time.now.to_s} @defaults_file_description = "" EOF1 end
# File lib/trinitycrmod/trinity_gs2.rb, line 140 def self.flux_class(code) case code when "gs2" CodeRunner::Trinity::TrinityComponent::Gs2 when "gryfx" CodeRunner::Trinity::TrinityComponent::Gryfx end end
# File lib/trinitycrmod/trinity.rb, line 617 def self.get_input_help_from_source_code(source_folder) source = get_aggregated_source_code_text(source_folder) rcp.namelists.each do |nmlst, hash| hash[:variables].each do |var, var_hash| # next unless var == :w_antenna var = var_hash[:code_name] || var values_text = source.scan(Regexp.new("\\W#{var}\\s*=\\s*.+")).join("\n") ep values_text values = scan_text_for_variables(values_text).map{|(_v,val)| val} values.uniq! # ep values if var == :nbeta values.delete_if{|val| val.kind_of? String} if values.find{|val| val.kind_of? Numeric} values.delete_if{|val| val.kind_of? String and not String::FORTRAN_BOOLS.include? val} if values.find{|val| val.kind_of? String and String::FORTRAN_BOOLS.include? val} # values.sort! # ep var # ep values sample_val = values[0] p sample_val help = values_text.scan(/ !(.*)/).flatten[0] p help #gets var_hash[:help] = help var_hash[:description] = help save_namelists end end end
To be used in conjunction with the shell script option in Trinity
. Read the output of old_run_name (which may be a commma-separated list of multiple runs) and read the contents of <run_name>_flux_inputs.dat and use them to interpolate esitmates of the fluxes
# File lib/trinitycrmod/flux_interpolator.rb, line 15 def interpolate_fluxes(old_run_folder, run_folder, grad_option, ntspec) old_run_name = nil Dir.chdir(old_run_folder) do old_cc = Dir.entries(Dir.pwd).find{|f| f =~ /\.cc$/} raise "Can't find cc file in #{old_run_folder}" unless old_cc old_run_name = File.expand_path(old_cc).sub(/.cc$/,'') end run_name = nil Dir.chdir(run_folder) do new_inp = Dir.entries(Dir.pwd).find{|f| f =~ /.flux_inputs$/} raise "Can't find flux_inputs in #{run_folder}" unless new_inp run_name = File.expand_path(new_inp).sub(/.flux_inputs$/,'') end ccfile = TextDataTools::Column::DataFile.new(old_run_name + '.cc', true, /\S+/, /(?:\#\s+)?\d+:.*?(?=\d+:|\Z)/) geofile = TextDataTools::Column::DataFile.new(old_run_name + '.geo', true, /\S+/, /(?:\#\s+)?\d+:\D*?(?=\d:|\d\d:|\Z)/) radius_data = ccfile.get_1d_array_float(/radius/) radius_uniq = radius_data.uniq p 'radius_uniq', radius_uniq ncc = radius_uniq.size np = radius_data.size/ncc p 'np is ', np, 'ncc is', ncc #p 'ion_tprim_perturb_data',ion_tprim_perturb_data = ccfile.get_1d_array_float(/11:/).pieces(np) perturb = { fprim: /10:/, ion_tprim: /11:/, eln_tprim: /12:/, dens: /18:/, ion_temp: /19:/, eln_temp: /20:/ } perturb_data = perturb.inject({}) do |h,(k,v)| begin h[k] = ccfile.get_1d_array_float(v).pieces(np).transpose rescue=>err p ccfile.get_1d_array(v) puts "Error reading: #{k}, #{v}" raise err end h end fluxes = { ion_hflux: /5: i/, eln_hflux: /6: e/, pflux: /3: i/, lflux: /9: v/, ion_heat: /7: i/, eln_heat: /8: e/ } fluxes_data = fluxes.inject({}) do |h,(k,v)| h[k] = ccfile.get_1d_array_float(v).pieces(np).transpose h end area = geofile.get_1d_array_float(/area/) grho = geofile.get_1d_array_float(/grho/) case grad_option when 'tigrad' jacobian_vecs = [:ion_temp,:ion_tprim] jacobian_vecs = [:ion_tprim] interp_vecs = [:ion_hflux] njac = 2 when 'ntgrads' jacobian_vecs = [:ion_tprim, :eln_tprim, :fprim] jacobian_vecs = [:ion_tprim, :fprim] interp_vecs = [:ion_hflux, :eln_hflux, :pflux] njac = 4 else raise "Unknown grad_option: #{grad_option}" end GraphKit.quick_create([perturb_data[:ion_tprim][0].to_gslv, fluxes_data[:ion_hflux][0].to_gslv])#.gnuplot #p perturb_data[:ion_tprim][0] interp = fluxes_data.inject({}) do |h, (k,v)| h[k] = radius_uniq.size.times.map do |i| if interp_vecs.include?(k) #puts 'i is', i GSL::ScatterInterp.alloc( :linear, jacobian_vecs.map{|name| arr = perturb_data[name] arr[i].to_gslv + GSL::Vector.linspace(0,1e-9,arr[i].size) } + [v[i].to_gslv], false, 1.0 ) else ZeroEval.new end end h end if false and run_name != 'dummy' arr = [] File.read(run_name + '.flux_inputs').scanf("%e"){|m| p m; arr+= m} # each of these quantities is a flat array with # radial index varying fastest, then jacobian index # then species index where appropriate i = 0 #p arr dens = arr[i...(i+=ncc*njac)] temp = arr[i...(i+=ncc*njac*2)].pieces(2) # This is hardwired to 2 fprim = arr[i...(i+=ncc*njac)] tprim = arr[i...(i+=ncc*njac*2)].pieces(2) # This is hardwired to 2 inputs = {} inputs[:dens] = dens inputs[:ion_temp] = temp[0] inputs[:eln_temp] = temp[1] inputs[:fprim] = fprim inputs[:ion_tprim] = tprim[0] inputs[:eln_tprim] = tprim[1] #p 'inputs', inputs.values.map{|v| v.size}, inputs[:eln_tprim] File.open(run_name + '.flux_results', 'w') do |file| njac.times{|ij| (ntspec+1).times{ ncc.times{|ic| file.printf("%e ",interp[:pflux][ic].eval(*jacobian_vecs.map{|v| inputs[v][ic + ij*ncc]}))}}} file.printf("\n") njac.times{|ij| ncc.times{|ic| file.printf("%e ",interp[:ion_hflux][ic].eval(*jacobian_vecs.map{|v| inputs[v][ic + ij*ncc]}))} ntspec.times{ ncc.times{|ic| file.printf("%e ",interp[:eln_hflux][ic].eval(*jacobian_vecs.map{|v| inputs[v][ic + ij*ncc]}))} } } file.printf("\n") njac.times{|ij| ncc.times{|ic| file.printf("%e ",interp[:ion_heat][ic].eval(*jacobian_vecs.map{|v| inputs[v][ic + ij*ncc]}))} ntspec.times{ ncc.times{|ic| file.printf("%e ",interp[:eln_heat][ic].eval(*jacobian_vecs.map{|v| inputs[v][ic + ij*ncc]}))} } } file.printf("\n") njac.times{|ij| ncc.times{|ic| file.printf("%e ",interp[:lflux][ic].eval(*jacobian_vecs.map{|v| inputs[v][ic + ij*ncc]}))}} file.printf("\n") area.each{|v| file.printf("%e ", v)} file.printf("\n") grho.each{|v| file.printf("%e ", v)} file.printf("\n") end else vec = GSL::Vector.linspace(perturb_data[:ion_tprim][0].min, perturb_data[:ion_tprim][0].max,20) vec2 = GSL::Vector.linspace(perturb_data[:fprim][0].min, perturb_data[:fprim][0].max,20) mat = GSL::Matrix.alloc(20,20) for i in 0...20 for j in 0...20 mat[i,j] = interp[:ion_hflux][0].eval(vec[i], vec2[j]) end end vec3 = GSL::Vector.linspace(perturb_data[:ion_tprim][1].min, perturb_data[:ion_tprim][1].max,20) vec4 = GSL::Vector.linspace(perturb_data[:fprim][1].min, perturb_data[:fprim][1].max,20) mat2 = GSL::Matrix.alloc(20,20) for i in 0...20 for j in 0...20 mat2[i,j] = interp[:ion_hflux][1].eval(vec3[i], vec4[j]) end end #vec.collect{|x| vec2.collect{|y| mat(interp[:ion_hflux][0].eval(x)} #GraphKit.quick_create([vec, vec2, mat], [perturb_data[:ion_tprim][0].to_gslv, perturb_data[:fprim][0], fluxes_data[:ion_hflux][0].to_gslv], [vec3, vec4, mat2], [perturb_data[:ion_tprim][1].to_gslv, perturb_data[:fprim][1], fluxes_data[:ion_hflux][1].to_gslv]).gnuplot live: true vec5 = GSL::Vector.linspace(perturb_data[:ion_tprim][2].min, perturb_data[:ion_tprim][2].max,20) vec6 = GSL::Vector.linspace(perturb_data[:fprim][2].min, perturb_data[:fprim][2].max,20) mat3 = GSL::Matrix.alloc(20,20) for i in 0...20 for j in 0...20 mat3[i,j] = interp[:ion_hflux][2].eval(vec5[i], vec6[j]) end end #vec.collect{|x| vec2.collect{|y| mat(interp[:ion_hflux][0].eval(x)} #GraphKit.quick_create([vec, vec2, mat], [perturb_data[:ion_tprim][0].to_gslv, perturb_data[:fprim][0], fluxes_data[:ion_hflux][0].to_gslv], [vec3, vec4, mat2], [perturb_data[:ion_tprim][1].to_gslv, perturb_data[:fprim][1], fluxes_data[:ion_hflux][1].to_gslv],[vec5, vec6, mat3], [perturb_data[:ion_tprim][2].to_gslv, perturb_data[:fprim][2], fluxes_data[:ion_hflux][2].to_gslv]).gnuplot live: true vec7 = GSL::Vector.linspace(perturb_data[:ion_tprim][3].min, perturb_data[:ion_tprim][3].max,20) vec8 = GSL::Vector.linspace(perturb_data[:fprim][3].min, perturb_data[:fprim][3].max,20) mat4 = GSL::Matrix.alloc(20,20) for i in 0...20 for j in 0...20 mat4[i,j] = interp[:ion_hflux][3].eval(vec7[i], vec8[j]) end end #vec.collect{|x| vec2.collect{|y| mat(interp[:ion_hflux][0].eval(x)} GraphKit.quick_create([vec, vec2, mat], [perturb_data[:ion_tprim][0].to_gslv, perturb_data[:fprim][0], fluxes_data[:ion_hflux][0].to_gslv], [vec3, vec4, mat2], [perturb_data[:ion_tprim][1].to_gslv, perturb_data[:fprim][1], fluxes_data[:ion_hflux][1].to_gslv],[vec5, vec6, mat3], [perturb_data[:ion_tprim][2].to_gslv, perturb_data[:fprim][2], fluxes_data[:ion_hflux][2].to_gslv],[vec7, vec8, mat4], [perturb_data[:ion_tprim][3].to_gslv, perturb_data[:fprim][3], fluxes_data[:ion_hflux][3].to_gslv]).gnuplot live: true end #vec = GSL::Vector.linspace(perturb_data[:ion_tprim][0].min, perturb_data[:ion_tprim][0].max,20) #GraphKit.quick_create([vec, vec.collect{|x| interp[:ion_hflux][0].eval(x)}], [perturb_data[:ion_tprim][0].to_gslv, fluxes_data[:ion_hflux][0].to_gslv]).gnuplot #vec = GSL::Vector.linspace(perturb_data[:ion_tprim][1].min, perturb_data[:ion_tprim][1].max,20) #GraphKit.quick_create([vec, vec.collect{|x| interp[:ion_hflux][1].eval(x)}], [perturb_data[:ion_tprim][1].to_gslv, fluxes_data[:ion_hflux][1].to_gslv]).gnuplot #vec = GSL::Vector.linspace(perturb_data[:ion_tprim][2].min, perturb_data[:ion_tprim][2].max,80) #GraphKit.quick_create([vec, vec.collect{|x| interp[:ion_hflux][2].eval(x)}], [perturb_data[:ion_tprim][2].to_gslv, fluxes_data[:ion_hflux][2].to_gslv]).gnuplot #STDIN.gets #GraphKit.quick_create([vec, vec.collect{|x| interp[:ion_hflux][0].eval(perturb_data[:ion_tprim][0].to_gslv.min, x)}]).gnuplot #p [tprim_perturb_data, fprim_perturb_data] end
This function creates a new Trinity
defaults file, with Trinity
parameters taken from trinity_input_file, and GS2 parameters taken from gs2_input_file. The file is then moved to the CodeRunner
central defaults location, the current folder is configured to use the defaults file.
# File lib/trinitycrmod/trinity_gs2.rb, line 62 def self.use_new_defaults_file_with_flux(name = ARGV[-4], trinity_input_file = ARGV[-3], flux_input_file = ARGV[-2], fluxcode=ARGV[-1]) raise "Please specify a name, a trinity input file and a flux code input file" if name == "use_new_defaults_file_with_flux" defaults_filename = "#{name}_defaults.rb" tmp_filename = "#{name}_flxtmp_defaults.rb" central_defaults_filename = rcp.user_defaults_location + "/" + defaults_filename #FileUtils.rm(name + '_defaults.rb') if FileTest.exist?(name + '_defaults.rb') #FileUtils.rm(central_defaults_filename) if FileTest.exist?(central_defaults_filename) FileUtils.rm(tmp_filename) if FileTest.exist?(tmp_filename) raise "Defaults file: #{central_defaults_filename} already exists" if FileTest.exist? central_defaults_filename flxclass = flux_class(fluxcode) make_new_defaults_file(name, trinity_input_file) flxclass.make_new_defaults_file(name + '_flxtmp', flux_input_file) File.open(defaults_filename, 'a'){|file| file.puts <<EOF2 @set_flux_defaults_procs.push(Proc.new do flux_runs.each do |run| run.instance_eval do #{File.read(tmp_filename).gsub(/\A|\n/, "\n ")} end end end) EOF2 } #FileUtils.mv(defaults_filename, central_defaults_filename) FileUtils.rm(tmp_filename) CodeRunner.fetch_runner(C: rcp.code, m: (rcp.modlet? ? rcp.modlet : nil), D: name) end
Public Instance Methods
# File lib/trinitycrmod/chease.rb, line 5 def chease_run CodeRunner.setup_run_class('chease') chrun = CodeRunner::Chease.new(@runner) raise 'No chease input file' unless FileTest.exist?(chfile=@directory + '/chease/chease_namelist') chrun.instance_eval(CodeRunner::Chease.defaults_file_text_from_input_file(chfile)) #puts ['chrun1', chrun.nsurf, self.class.defaults_file_text_from_input_file(chfile), chfile] return chrun end
# File lib/trinitycrmod/check_parameters.rb, line 21 def check_flux_option if @flux_option == "gs2" error("subfolders must be .true. ") unless @subfolders and @subfolders.fortran_true? end end
# File lib/trinitycrmod/check_parameters.rb, line 11 def check_geometery error( "Can't find geo_file #@geo_file (the path of geo file should be either absolute or set relative to the run folder #@directory). If you are not using a geometry file for this run please unset the parameter geo_file.") if @geo_file and not FileTest.exist? @geo_file end
# File lib/trinitycrmod/check_parameters.rb, line 15 def check_parallelisation error("nrad must be explicitly specified") unless @nrad error("Number of jobs: #{n_flux_tubes_jac} must evenly divide the number of processors: #{actual_number_of_processors} when fork_flag is .true.") if fork_flag_actual.fortran_true? and not actual_number_of_processors%n_flux_tubes_jac == 0 end
# File lib/trinitycrmod/check_parameters.rb, line 3 def check_parameters check_geometery check_parallelisation check_flux_option end
# File lib/trinitycrmod/trinity.rb, line 283 def confinement_time area = new_netcdf_file.var('area_grid').get('start' => [0,0], 'end'=> [-1,0])[true,0].to_a.to_gslv grho = new_netcdf_file.var('grho_grid').get('start' => [0,0], 'end'=> [-1,0])[true,0].to_a.to_gslv pressure = new_netcdf_file.var('pres_grid').get('start' => [0,0,0,-1], 'end' => [-1,-1,0,-1])[true,true,0,0].to_a #pressure = pressure.map{|arr| arr.to_gslv}.sum rad_in = @rad_out/(2.0*@nrad.to_f-1) drad = (@rad_out - rad_in) / (@nrad.to_f - 1) if @evolve_temperature.fortran_true? if @te_equal_ti.fortran_true? if @equal_ion_temps.fortran_true? stored_energy = area.subvector(@nrad-1)*pressure[1].to_gslv.subvector(@nrad-1)*grho.subvector(@nrad-1)*drad stored_energy = stored_energy.sum*1e3*1.6e-19*1e20 power = @i_powerin_1*1e6 return stored_energy/power end end end end
# File lib/trinitycrmod/ecom.rb, line 5 def ecom_run CodeRunner.setup_run_class('ecom') ecrun = CodeRunner::Ecom.new(@runner) raise 'No ecom input file' unless FileTest.exist?(chfile=@directory + '/chease/ecom.in') ecrun.instance_eval(CodeRunner::Ecom.defaults_file_text_from_input_file(chfile)) #puts ['ecrun1', ecrun.nsurf, self.class.defaults_file_text_from_input_file(chfile), chfile] return ecrun end
Override standard CodeRunner
method to allow for flux code variables
# File lib/trinitycrmod/trinity_gs2.rb, line 117 def evaluate_defaults_file(filename) text = File.read(filename) instance_eval(text) text.scan(/^\s*@(\w+)/) do var_name = $~[1].to_sym next if var_name == :defaults_file_description unless rcp.variables.include? var_name or (flux_gs2? and Gs2.rcp.variables.include? var_name) or (flux_gryfx? and Gryfx.rcp.variables.include? var_name) warning("---#{var_name}---, specified in #{File.expand_path(filename)}, is not a variable. This could be an error") end end end
# File lib/trinitycrmod/trinity_gs2.rb, line 148 def flux_class self.class.flux_class(@flux_option) end
# File lib/trinitycrmod/trinity_gs2.rb, line 170 def flux_folder_name(i) if i >= n_flux_tubes_jac jn = i - n_flux_tubes_jac + 1 folder = "calibrate_#{jn}" else jn = i + 1 folder = "flux_tube_#{jn}" end folder end
Is the flux tube code being used gryfx?
# File lib/trinitycrmod/trinity_gs2.rb, line 156 def flux_gryfx? @flux_option == "gryfx" end
Is the flux tube code being used gs2?
# File lib/trinitycrmod/trinity_gs2.rb, line 152 def flux_gs2? @flux_option == "gs2" end
# File lib/trinitycrmod/trinity_gs2.rb, line 160 def flux_run_name(i) if i >= n_flux_tubes_jac jn = i - n_flux_tubes_jac + 1 run_name = "calibrate_" + @run_name + (jn).to_s else jn = i + 1 run_name = @run_name + (jn).to_s end run_name end
Generates the component runs for GS2 and returns the hash Raises an error if flux_option != “gs2”
# File lib/trinitycrmod/trinity_gs2.rb, line 98 def flux_runs raise FluxOptionError.new("flux_runs called and flux_option != gs2 or gryfx") if not (flux_gs2? or flux_gryfx?) #puts "2@COMMMMMMMMMMMMMPOOOOOOOOOOOOOONNNETN", @component_runs generate_component_runs if not (@component_runs and @component_runs.size == n_flux_tubes) #p ["@COMMMMMMMMMMMMMPOOOOOOOOOOOOOONNNETN", @component_runs] @component_runs #@gs2_run_list ||= {} #raise TypeError.new("@runner is nil") if @runner.nil? #@gs2_run_list[key] ||= Gs2.new(@runner) ##if key != :base ##raise "key in gs2_run must be either :base or an integer" unless key.kind_of? Integer ##@gs2_run_list[key] ||= @gs2_run_list[:base].dup ##end #@gs2_run_list[key] end
# File lib/trinitycrmod/trinity.rb, line 451 def generate_component_runs #puts "HERE" #<<<<<<< HEAD @component_runs ||= [] if @running if (existing = @runner.component_run_list.values.find_all{|r| r.real_id==@id}).size > 0 @component_runs = existing.sort_by{|r| -r.id} #======= #if (existing = @runner.component_run_list.values.find_all{|r| r.real_id==@id}).size > 0 #@component_runs = existing.sort_by{|r| -r.id} #if @running #>>>>>>> 90ef5ae701681b63da68f6eb1bd1ec1169873f8b return end end @component_runs ||= [] if flux_gryfx? or flux_gs2? fclass = flux_class eprint '...getting Trinity components..' for i in 0...n_flux_tubes component = @component_runs[i] #p [i, '9,', component, '4', !@component_runs[i]]; STDIN.gets if not component #p "HEELO" #p [i, '3,', component, '4', @component_runs.size] component = @component_runs[i] = fclass.new(@runner, self, i).create_component #component.instance_variable_set(:@output_file, output_file) #p [i, '3,', component, '4', @component_runs.size] if false if i > 0 and @component_runs[i-1] component.rcp.variables.each do |var| val = @component_runs[i-1].send(var) component.set(var, val) if val end end end end #p [i,'1', component, '2', @component_runs.size]; STDIN.gets component = @component_runs[i] #p [i,'1', component, '2']; STDIN.gets component.component_runs = [] #component.runner = nil #pp component; STDIN.gets #component.instance_variables.each{|var| puts var; pp var; puts Marshal.dump(component.instance_variable_get(var)); STDIN.gets} #puts Marshal.dump(component); STDIN.gets #pp component; STDIN.gets #p component.class component.job_no = @job_no #component.status = @status #p ["HERE2", @component_runs.size, @component_runs[i]] #Dir.chdir(@directory) { compdir = flux_folder_name(i) # "flux_tube_#{i+1}" # Stop it actually checking the flux codes every time. component.instance_variable_set(:@status, :Complete) Dir.chdir(compdir){ eprint '.' component.process_directory } if FileTest.exist? compdir #} component.component_runs = [] component.trinity_run = self #@component_runs.push component component.real_id = @id #@gs2_run_list[i] = component #pp component; STDIN.gets #component.runner = nil #puts Marshal.dump(component); STDIN.gets #pp component; STDIN.gets #component.component_runs = [] end eputs 'done' end end
Writes the gs2 input files, creating separate subfolders for them if @subfolders is .true.
# File lib/trinitycrmod/trinity.rb, line 363 def generate_flux_input_files # At the moment we must use subfolders for i in 0...n_flux_tubes #gs2run = gs2_run(:base).dup #gs2_run(i).instance_variables.each do |var| #gs2run.instance_variable_set(var, gs2_run(i).instance_variable_get(var)) #end fluxrun = flux_runs[i] #ep ['gs2_runs[i] in generate', gs2_runs[i].nwrite] #p ['i',i] #if i >= n_flux_tubes_jac #jn = i - n_flux_tubes_jac + 1 #run_name = "calibrate_" + @run_name + (jn).to_s #folder = "calibrate_#{jn}" #else #jn = i + 1 #run_name = @run_name + (jn).to_s #folder = "flux_tube_#{jn}" #end folder = flux_folder_name(i) run_name = flux_run_name(i) if @subfolders and @subfolders.fortran_true? fluxrun.directory = @directory + "/" + folder FileUtils.makedirs(fluxrun.directory) fluxrun.relative_directory = @relative_directory + "/" + folder fluxrun.restart_dir = fluxrun.directory + "/nc" else fluxrun.directory = @directory fluxrun.relative_directory = @relative_directory end fluxrun.run_name = run_name fluxrun.nprocs = @nprocs if i==0 block = Proc.new{check_parameters} else block = Proc.new{} end #if @restart_id #gs2run.restart_id = Dir.chdir(fluxrun.directory) do fluxrun.generate_input_file(&block) fluxrun.write_info end ### Hack the input file so that gs2 gets the location of # the restart dir correctly within trinity if @subfolders and @subfolders.fortran_true? infile = fluxrun.directory + "/" + fluxrun.run_name + ".in" text = File.read(infile) File.open(infile, 'w'){|f| f.puts text.sub(/restart_dir\s*=\s*"nc"/, "restart_dir = \"#{folder}/nc\"")} end end end
This is a hook which gets called just before submitting a simulation. It sets up the folder and generates any necessary input files.
# File lib/trinitycrmod/trinity.rb, line 207 def generate_input_file @run_name += "_t" if @restart_id @runner.run_list[@restart_id].restart(self) else make_initial_profiles if self.respond_to?(:make_initial_profiles) end if uses_ecom? setup_ecom elsif uses_chease? setup_chease end #eputs "nwrite 6 is ", new_run.flux_runs[0].nwrite @avail_cpu_time = (@wall_mins-1.0) * 60 if @wall_mins if flux_gs2? or flux_gryfx? @avail_cpu_time = (@wall_mins-6.0) * 60 if @wall_mins end check_parameters write_input_file #eputs "nwrite 7 is ", new_run.flux_runs[0].nwrite generate_flux_input_files if flux_gs2? or flux_gryfx? #eputs "nwrite 8 is ", new_run.flux_runs[0].nwrite end
Override CodeRunner::Run method to deal with flux_pars properly when generating run_name
# File lib/trinitycrmod/trinity.rb, line 303 def generate_run_name return super if CodeRunner::GLOBAL_OPTIONS[:short_run_name] @run_name = %[v#@version] + @naming_pars.inject("") do |str, par| case par when :flux_pars str+="_flx_#{send(par).map{|k,v| "#{k}_#{v.to_s[0..8]}"}.join("_")}}" else str+="_#{par}_#{send(par).to_s[0...8]}" end end @run_name = @run_name.gsub(/\s+/, "_").gsub(/[\/{}"><:=]/, '') + "_id_#@id" end
# File lib/trinitycrmod/output_files.rb, line 58 def get_1d_array_float(outfile, column_header) cache[:array_1d] = {} unless [:Complete, :Failed].include? @status cache[:array_1d] ||= {} cache[:array_1d][[outfile, column_header]] ||= send(outfile + :_outfile).get_1d_array_float(column_header) end
Return a two-dimensional array of floatingpoint numbers from the file ending in outfile, from the column whose header matches column_header, indexed by index_header. See TextDataTools::Column::DataFile for more information. Outfile is a symbol, use e.g. :nt for data from 'run_name.nt'.
# File lib/trinitycrmod/output_files.rb, line 52 def get_2d_array_float(outfile, column_header, index_header) cache[:array_2d] = {} unless [:Complete, :Failed].include? @status cache[:array_2d] ||= {} cache[:array_2d][[outfile, column_header, index_header]] ||= send(outfile + :_outfile).get_2d_array_float(column_header, index_header) end
# File lib/trinitycrmod/trinity.rb, line 580 def get_completed_timesteps Dir.chdir(@directory) do @completed_timesteps = time_outfile.exists? ? time_outfile.get_1d_array_integer(/itstep/).max : 0 end end
# File lib/trinitycrmod/trinity.rb, line 589 def get_global_results if FileTest.exist? new_netcdf_filename @plasma_volume = new_netcdf_file.var('plasma_volume').get[-1] @pfus = new_netcdf_file.var('fusion_power').get[-1] @fusionQ = new_netcdf_file.var('fusion_gain').get[-1] @pnet = new_netcdf_file.var('net_power').get[-1] @alpha_power = new_netcdf_file.var('alpha_power_total').get[-1] @aux_power = new_netcdf_file.var('aux_power_total').get[-1] @rad_power = new_netcdf_file.var('radiate_power_total').get[-1] @ne0 = new_netcdf_file.var('ne_core').get[-1] @te0 = new_netcdf_file.var('te_core').get[-1] @ti0 = new_netcdf_file.var('ti_core').get[-1] @omega0 = new_netcdf_file.var('omega_core').get[-1] else # If the netcdf file is missing try the info file @fusionQ = info_outfile.get_variable_value('Q').to_f @pfus = info_outfile.get_variable_value(/fusion\s+power/i).to_f @pnet = info_outfile.get_variable_value(/net\s+power/i).to_f @aux_power = info_outfile.get_variable_value(/aux.*\s+power/i).to_f @alpha_power = info_outfile.get_variable_value(/alpha\s+power/i).to_f @rad_power = info_outfile.get_variable_value(/radiated\s+power/i).to_f @ne0 = info_outfile.get_variable_value(/core\s+density|Core\s+electron\s+density/i).to_f @ti0 = info_outfile.get_variable_value(/[Cc]ore\s+T_i/i).to_f @te0 = info_outfile.get_variable_value(/[Cc]ore\s+T_e/i).to_f @omega0 = info_outfile.get_variable_value(/[Cc]ore\s+omega/i).to_f rescue 0.0 # Old info files don't have omega end end
# File lib/trinitycrmod/trinity.rb, line 558 def get_status if @running get_completed_timesteps if completed_timesteps == 0 @status = :NotStarted else @status = :Incomplete end else get_completed_timesteps if @completed_timesteps == @ntstep @status = :Complete else if FileTest.exist?(output_file) and File.read(output_file) =~/trinity\s+finished/i @status = :Complete else @status = :Failed end end end end
This is the hook that is called by CodeRunner
, providing the graphkit with the given name and functions to the CodeRunner
framework
# File lib/trinitycrmod/graphs.rb, line 227 def graphkit(name, options) NetcdfSmartReader.dimensions.map{|d| d.to_sym}.each do |var| #ep 'index', var if options[var].class == Symbol and options[var] == :all options[var] = list(var).values elsif options[var+:_index].class == Symbol and options[var+:_index] == :all #ep 'Symbol' options[var+:_index] = list(var).keys end if options[var].class == Array return options[var].map{|value| graphkit(name, options.dup.absorb({var => value}))}.sum elsif options[var+:_index].class == Array #ep 'Array' return options[var+:_index].map{|value| graphkit(name, options.dup.absorb({var+:_index => value}))}.sum end if options[var].class == Symbol and options[var] == :max options[var] = list(var).values.max elsif options[var+:_index].class == Symbol and options[var+:_index] == :max ep 'Symbol' options[var+:_index] = list(var).keys.max end end if (meth = TrinityGraphKits.instance_methods.find{|m| m.to_s == name + '_graphkit'} or meth = TrinityMultiKits.instance_methods.find{|m| m.to_s == name + '_graphkit'}) return send(meth, options) elsif name =~ /^nc_/ return smart_graphkit(options.absorb({graphkit_name: name})) else raise "GraphKit not found: #{name}" end end
An array of arrays containing the GS2 run times for each iteration. Produced unscientifically by scanning the stdout.
# File lib/trinitycrmod/trinity_gs2.rb, line 131 def gs2_run_times raise FluxOptionError.new("gs2_run_times called and flux_option != gs2") if not flux_gs2? run_times = [] File.open(@directory + '/' + output_file, "r").each_line{|l| l.scan(/Job.*timer.*(\b\d+\.\d+\b)/){run_times.push $~[1].to_f}} sz = run_times.size.to_f return run_times.pieces((sz / n_flux_tubes.to_f).ceil) end
# File lib/trinitycrmod/trinity.rb, line 699 def input_file_extension '.trin' end
# File lib/trinitycrmod/trinity.rb, line 647 def input_file_header <<EOF !============================================================================== ! Trinity INPUT FILE automatically generated by CodeRunner !============================================================================== ! ! Trinity is a multiscale turbulent transport code for fusion plasmas. ! ! See http://gyrokinetics.sourceforge.net ! ! CodeRunner is a framework for the automated running and analysis ! of large simulations. ! ! See http://coderunner.sourceforge.net ! ! Created #{Time.now.to_s} ! by CodeRunner version #{CodeRunner::CODE_RUNNER_VERSION.to_s} ! !============================================================================== EOF end
# File lib/trinitycrmod/trinity.rb, line 529 def is_converged? Dir.chdir(@directory) do if FileTest.exist?(new_netcdf_filename) and @convergetol and new_netcdf_file.var('convergeval') and new_netcdf_file.dim('t').length > 2 and new_netcdf_file.var('convergeval').get[0,-1] < @convergetol return true else return false end end end
Returns a hash of the specified dimension in the form {index=> value} where index is 1-based Dimension can be:
:t :rho :rho_cc
# File lib/trinitycrmod/output_files.rb, line 69 def list(var) case var.to_s when *NetcdfSmartReader.dimensions hash = {} #get_2d_array_float(:nt, /1:\s+time/, /1:\s+time/).map{|arr| arr[0]}.each_with_index{|t,i| hash[i+1] = t} ax = netcdf_smart_reader.axiskit(var.to_s, {}) ax.data.to_a.each_with_index{|v,i| hash[i+1] = v} hash end end
# File lib/trinitycrmod/trinity.rb, line 352 def n_flux_tubes if @neval_calibrate and @neval_calibrate > 0 raise "neval_calibrate set but ncc_calibrate not set" unless @ncc_calibrate n_flux_tubes_jac + @ncc_calibrate else n_flux_tubes_jac end end
The number of separate flux tube results needed for the jacobian
# File lib/trinitycrmod/trinity.rb, line 317 def n_flux_tubes_jac d1 = dflx_stencil_actual - 1 ngrads = d1 * case @grad_option when nil n = 0 n = n + @n_ion_spec if (evolve_density_actual.fortran_true?) if (evolve_temperature_actual.fortran_true?) if (equal_ion_temps_actual.fortran_true?) then n = n + 1 else n = n + n_ion_spec end n=n+1 if (te_equal_ti_actual.fortran_false? and te_fixed_actual.fortran_false?) n = n + 1 if (evolve_flow_actual.fortran_true?) end n when "tigrad", "ngrad", "lgrad" 1 when "tgrads" 2 when "ltgrads", "ntgrads" 3 when "all" 4 else raise "unknown grad_option: #@grad_option" end if evolve_grads_only_actual.fortran_true? njac = ngrads + 1 else njac = 2*ngrads+1 end #p 'nraaad', @nrad (@nrad-1) * njac end
# File lib/trinitycrmod/read_netcdf.rb, line 153 def netcdf_smart_reader NetcdfSmartReader.new(new_netcdf_file) end
Parameters which follow the Trinity
executable, in this case just the input file.
# File lib/trinitycrmod/trinity.rb, line 444 def parameter_string @run_name + ".trin" end
# File lib/trinitycrmod/trinity.rb, line 448 def parameter_transition end
A hook which gets called when printing the standard run information to the screen using the status command.
# File lib/trinitycrmod/trinity.rb, line 78 def print_out_line #p ['id', id, 'ctd', ctd] #p rcp.results.zip(rcp.results.map{|r| send(r)}) name = @run_name name += " (res: #@restart_id)" if @restart_id name += " real_id: #@real_id" if @real_id beginning = sprintf("%2d:%d %-60s %1s:%2.1f(%s) %3s%1s", @id, @job_no, name, @status.to_s[0,1], @run_time.to_f / 60.0, @nprocs.to_s, percent_complete, "%") if ctd and fusionQ beginning += sprintf("Q:%f, Pfusion:%f MW, Ti0:%f keV, Te0:%f keV, n0:%f x10^20", fusionQ, pfus, ti0, te0, ne0) end beginning += " ---#{@comment}" if @comment beginning end
This method, as its name suggests, is called whenever CodeRunner
is asked to analyse a run directory. This happens if the run status is not :Complete, or if the user has specified recalc_all(-A on the command line) or reprocess_all (-a on the command line).
# File lib/trinitycrmod/trinity.rb, line 548 def process_directory_code_specific get_status #p ['id is', id, 'ctd is ', ctd] if ctd get_global_results rescue nil end #p ['fusionQ is ', fusionQ] @percent_complete = completed_timesteps.to_f / ntstep.to_f * 100.0 end
Modify new_run so that it becomes a restart of self. Adusts all the parameters of the new run to be equal to the parameters of the run that calls this function, and sets up its run name correctly
# File lib/trinitycrmod/trinity.rb, line 104 def restart(new_run) #new_run = self.dup (rcp.variables).each{|v| new_run.set(v, send(v)) if send(v) or new_run.send(v)} @naming_pars.delete(:preamble) SUBMIT_OPTIONS.each{|v| new_run.set(v, self.send(v)) unless new_run.send(v)} #(rcp.results + rcp.gs2_run_info).each{|result| new_run.set(result, nil)} new_run.is_a_restart = true new_run.restart_id = @id new_run.restart_run_name = @run_name new_run.init_option = "restart" new_run.iternt_file = @run_name + ".iternt" new_run.iterflx_file = @run_name + ".iterflx" new_run.itercalib_file = @run_name + ".itercalib" new_run.restart_file = @run_name + ".out.nc" new_run.init_file = @run_name + ".tmp" new_run.flux_pars = @flux_pars.absorb(new_run.flux_pars) if @flux_pars and new_run.flux_pars new_run.run_name = nil new_run.naming_pars = @naming_pars new_run.update_submission_parameters(new_run.parameter_hash.inspect, false) if new_run.parameter_hash new_run.naming_pars.delete(:restart_id) new_run.generate_run_name new_run.run_name += '_t' if @flux_option == "gs2" flux_runs.each_with_index do |run, i| CodeRunner::Gs2.rcp.variables.each{|v| next if [:ginit_option, :delt_option].include? v and new_run.no_restart_gs2 new_run.flux_runs[i].set(v, run.send(v)) if run.send(v) or new_run.flux_runs[i].send(v) } end elsif @flux_option == "gryfx" flux_runs.each_with_index do |run, i| CodeRunner::Gryfx.rcp.variables.each{|v| new_run.flux_runs[i].set(v, run.send(v)) if run.send(v) or new_run.flux_runs[i].send(v) } eputs "parameter_hash_string is " + new_run.flux_runs[i].parameter_hash_string eputs "nwrite is ", new_run.flux_runs[i].nwrite, run.nwrite new_run.flux_runs[i].update_submission_parameters(new_run.flux_runs[i].parameter_hash_string, false) eputs "nwrite 2 is ", new_run.flux_runs[i].nwrite, run.nwrite end end @runner.nprocs = @nprocs if @runner.nprocs == "1" # 1 is the default so this means the user probably didn't specify nprocs # This is unnecessary for single restart file. warning( "Restart is not on the same number of processors as the previous run: new is #{new_run.nprocs.inspect} and old is #{@nprocs.inspect}... this is only OK if you are using parallel netcdf and single restart files.") if flux_gs2? and not new_run.no_restart_gs2 and (!new_run.nprocs or new_run.nprocs != @nprocs) raise "Restart cannot have a different sized jacobian: new is #{new_run.n_flux_tubes_jac} and old is #{n_flux_tubes_jac}" unless new_run.n_flux_tubes_jac == n_flux_tubes_jac eputs 'Copying Trinity Restart files', '' #system "ls #@directory" ['iternt', 'iterflx', 'tmp', 'itercalib', 'out.nc'].each do |ext| next if ['itercalib', 'out.nc'].include? ext and not FileTest.exist?("#@directory/#@run_name.#{ext}") # Unlike gs2, trinity always uses the current run name to generate the # restart files. Thus, the name of the restart files changes with every # run. FileUtils.cp("#@directory/#@run_name.#{ext}", "#{new_run.directory}/.") end if (new_run.flux_gs2? and flux_gs2?) and not new_run.no_restart_gs2 for i in 0...n_flux_tubes break if i >= new_run.n_flux_tubes folder = flux_folder_name(i) new_run.flux_runs[i].directory = new_run.directory + "/#{folder}" FileUtils.makedirs(new_run.flux_runs[i].directory) flux_runs[i].restart(new_run.flux_runs[i]) if new_run.neval_calibrate and new_run.neval_calibrate > 0 and new_run.flux_runs[i].nonlinear_mode == "off" new_run.flux_runs[i].ginit_option = "noise" new_run.flux_runs[i].delt_option = "default" new_run.flux_runs[i].is_a_restart = false new_run.flux_runs[i].restart_id = nil end end end if (new_run.flux_gryfx? and flux_gryfx?) and not new_run.no_restart_gs2 for i in 0...n_flux_tubes break if i >= new_run.n_flux_tubes next if not FileTest.exist? flux_runs[i].directory + '/' + flux_runs[i].run_name + '.restart.cdf' eputs " old new nx #{flux_runs[i].nx} #{new_run.flux_runs[i].nx}" if not ( flux_runs[i].nx == new_run.flux_runs[i].nx and flux_runs[i].ny == new_run.flux_runs[i].ny and flux_runs[i].ngauss == new_run.flux_runs[i].ngauss and flux_runs[i].negrid == new_run.flux_runs[i].negrid and flux_runs[i].ntheta == new_run.flux_runs[i].ntheta ) new_run.flux_runs[i].ginit_option = "noise" new_run.flux_runs[i].delt_option = "default" new_run.flux_runs[i].is_a_restart = false new_run.flux_runs[i].restart_id = nil new_run.flux_runs[i].restart = "off" next end folder = flux_folder_name(i) new_run.flux_runs[i].directory = new_run.directory + "/#{folder}" FileUtils.makedirs(new_run.flux_runs[i].directory) #eputs "nwrite 3 is ", new_run.flux_runs[i].nwrite flux_runs[i].set_restart(new_run.flux_runs[i]) #eputs "nwrite 4 is ", new_run.flux_runs[i].nwrite end end #eputs "nwrite 5 is ", new_run.flux_runs[0].nwrite new_run end
# File lib/trinitycrmod/trinity.rb, line 686 def run_heuristic_analysis ep 'run_heuristic_analysis', Dir.pwd infiles = Dir.entries.grep(/^[^\.].*\.trin$/) ep infiles raise CRMild.new('No input file') unless infiles[0] raise CRError.new("More than one input file in this directory: \n\t#{infiles}") if infiles.size > 1 input_file = infiles[0] #ep 'asdf' @nprocs ||= "1" @executable ||= "/dev/null" make_info_file(input_file, false) end
# File lib/trinitycrmod/chease.rb, line 13 def setup_chease ep "Setting up chease files..." if evolve_geometry.fortran_true? FileUtils.cp_r(@gs_folder, 'chease') chrun = chease_run chrun.neqdsk = 0 # Use EXPEQ chrun.nsurf = 6 # Outer surface from EXPEQ puts ['chrun2', chrun.nsurf] chrun.nppfun = 4 # Pres profile from EXPEQ chrun.nfunc = 4 # Current func chrun.nopt = -1 # Use prev soln for initial chrun.nblopt = 0 # Don't modify the pressure profile Dir.chdir(@directory + '/chease/'){ chrun.write_input_file FileUtils.cp('NOUT', 'NIN') } elsif @gs_folder FileUtils.cp_r(@gs_folder, 'chease') else FileUtils.mkdir('chease') unless FileTest.exist? 'chease' origfile = @runner.root_folder + '/ogyropsi.dat' FileUtils.cp(origfile, 'chease/ogyropsi.dat') if FileTest.exist? origfile end end
# File lib/trinitycrmod/ecom.rb, line 13 def setup_ecom ep "Setting up ecom files..." if evolve_geometry.fortran_true? FileUtils.cp_r(@gs_folder, 'chease') ecrun = ecom_run ecrun.iptype = 2 # Use numerical pressure ecrun.iptable = 2 # File contains rho_tor and pressure ecrun.file_prof = 'Profile.dat' # Trinity always expects the file to be called this #ecrun.neqdsk = 0 # Use EXPEQ #ecrun.nsurf = 6 #puts ['ecrun2', ecrun.nsurf] #ecrun.nppfun = 4 # Pres profile from EXPEQ #ecrun.nfunc = 4 # Current func #ecrun.nopt = -1 # Use prev soln for initial Dir.chdir(@directory + '/chease/'){ecrun.write_input_file} else FileUtils.mkdir('chease') unless FileTest.exist? 'chease' origfile = @runner.root_folder + '/ogyropsi.dat' FileUtils.cp(origfile, 'chease/ogyropsi.dat') if FileTest.exist? origfile end end
# File lib/trinitycrmod/read_netcdf.rb, line 160 def smart_graphkit(options) case options[:command] when :help "A smart graphkit is a direct plot of a given variable from the new netcdf file. The name of the graphkit is the name of the variable prefixed by 'cdf_'. To plot, for example, the heat flux vs time, you would give the graph name cdf_heat_flux_tot. You can use index specifiers in the the options; for example, to plot the potential as a function of kx and ky for a given time index, you would use the graph name cdf_phi2_by_mode, and the options {t_index: n}. To plot the potential as function of kx for a given ky and time would use the options {t_index, n, Y_index: m}. For each dimension you can specify the index, or a minium and/or maximum." when :options [:nmat_index, :t_index, :tspec_index, :iter_index] else case options[:graphkit_name] when /_vs_/ kits = options[:graphkit_name].sub(/^nc_/, '').split(/_vs_/).map{|n| netcdf_smart_reader.graphkit(n, options)} kit = kits[-1] raise SmartGraphKitError.new("Number of axes does not match number of variables") if kits.size != kit.naxes for i in 0...kit.data.size if kit.naxes > 1 kit.data[i].x = kits[0].data[i].y end if kit.naxes > 2 kit.data[i].x = kits[0].data[i].z kit.data[i].y = kits[1].data[i].z end end kit.autocreate kit else netcdf_smart_reader.graphkit(options[:graphkit_name].sub(/^nc_/, ''), options) end end end
Update submission parameters in the normal way then deal with parameters for the flux code. Each flux code will behave differently.
-
Gs2
flux_pars: {nx: 43, delt: {1=> 0.01, 2=>0.05}}
will set nx for all runs to be 43, and delt for run 1 to 0.01, delt for run 2 to be 0.05
# File lib/trinitycrmod/trinity.rb, line 239 def update_submission_parameters(parameters, start_from_defaults=true) @set_flux_defaults_procs ||= [] super(parameters, start_from_defaults) if flux_gs2? or flux_gryfx? raise "No set_flux_defaults_procs defined" unless @set_flux_defaults_procs.size > 0 @set_flux_defaults_procs.each{|prc| prc.call} flux_parameter_hashes = {} if @flux_pars @flux_pars.each do |par, val| if val.kind_of? Hash #val.each{|n,v| gs2_runs[n].set(par, v)} val.each do |n,v| if n == :jac range = 0...n_flux_tubes_jac elsif n == :calib range = n_flux_tubes_jac...n_flux_tubes else range = n..n end for i in range flux_parameter_hashes[i] ||= {} flux_parameter_hashes[i][par] = v end #gs2_parameter_hashes[n] ||= {} #gs2_parameter_hashes[n][par] = v end else for i in 0...n_flux_tubes #gs2_runs.each{|r| r.set(par, val)} flux_parameter_hashes[i] ||= {} flux_parameter_hashes[i][par] = val end end end end for i in 0...n_flux_tubes flux_runs[i].parameter_hash = (flux_parameter_hashes[i] || {}).inspect flux_runs[i].update_submission_parameters(flux_runs[i].parameter_hash, false) end end self end
# File lib/trinitycrmod/chease.rb, line 2 def uses_chease? @geo_option == "chease" or @init_option == "chease" or @species_option == "chease" end
# File lib/trinitycrmod/ecom.rb, line 2 def uses_ecom? @geo_option == "ecom" end
# File lib/trinitycrmod/trinity.rb, line 433 def vim_output system "vim -Ro #{output_file} #{error_file} #@directory/#@run_name.error #@directory/#@run_name.out " end
# File lib/trinitycrmod/trinity.rb, line 419 def watch_calibration_status if @flux_option == "gs2" command = gs2_runs.map{|r| FileTest.exist?(fn="#{r.directory}/#{r.run_name}.out.nc") ? "ncdump #{fn} | grep UNLIM" : nil }.compact command = "watch '#{command.join(' && ')}'" puts command system command else raise "watch_calibration_status only implemented for gs2" end end
This command uses the infrastructure provided by Run::FortranNamelist, provided by CodeRunner
itself.
# File lib/trinitycrmod/trinity.rb, line 439 def write_input_file File.open(@run_name + ".trin", 'w'){|file| file.puts input_file_text} end