Class: CodeRunner::Gene
- Inherits:
-
Run::FortranNamelist
- Object
- Run::FortranNamelist
- CodeRunner::Gene
- Defined in:
- lib/genecrmod/gene.rb,
lib/genecrmod/hdf5.rb,
lib/genecrmod/check_parameters.rb
Overview
This is a customised subclass of the CodeRunner::Run class which allows CodeRunner to run and analyse the GENE gyrokinetic code (see www2.ipp.mpg.de/~fsj/gene/)
Class Method Summary collapse
Instance Method Summary collapse
- #check_parameters ⇒ Object
- #generate_component_runs ⇒ Object
-
#generate_input_file ⇒ Object
This is a hook which gets called just before submitting a simulation.
- #get_completed_timesteps ⇒ Object
- #get_growth_rates ⇒ Object
- #get_h5_narray_all(file, dataset) ⇒ Object
- #get_status ⇒ Object
-
#graphkit(name, options = {}) ⇒ Object
This is a temporary hack…
- #input_file_extension ⇒ Object
- #input_file_header ⇒ Object
- #namelist_test_failed(namelist, tst) ⇒ Object
- #namelist_text(namelist, enum = nil) ⇒ Object
-
#parameter_string ⇒ Object
Parameters which follow the Trinity executable, in this case just the input file.
- #parameter_transition ⇒ Object
-
#print_out_line ⇒ Object
A hook which gets called when printing the standard run information to the screen using the status command.
-
#process_directory_code_specific ⇒ Object
This method, as its name suggests, is called whenever CodeRunner is asked to analyse a run directory.
-
#restart(new_run) ⇒ Object
Modify new_run so that it becomes a restart of self.
- #run_namelist_tests(namelist, hash, enum = nil) ⇒ Object
- #test_failed(namelist, var, gs2_var, tst) ⇒ Object
- #test_variable(namelist, var, var_hash, ext, value) ⇒ Object
- #vim_output ⇒ Object (also: #vo)
-
#write_input_file ⇒ Object
This command uses the infrastructure provided by Run::FortranNamelist, provided by CodeRunner itself.
Class Method Details
.defaults_file_header ⇒ Object
222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 |
# File 'lib/genecrmod/gene.rb', line 222 def self.defaults_file_header "############################################################################\n# #\n# Automatically generated defaults file for the GENE CodeRunner module #\n# #\n# This defaults file specifies a set of defaults for GENE which are #\n# used by CodeRunner to set up and run GENE simulations. #\n# #\n############################################################################\n\n# Created: \#{Time.now.to_s}\n\n@defaults_file_description = \"\"\n" end |
.parse_input_file(input_file, strict = true) ⇒ Object
87 88 89 90 91 92 93 94 95 96 |
# File 'lib/genecrmod/gene.rb', line 87 def self.parse_input_file(input_file, strict=true) if FileTest.file? input_file text = File.read(input_file) else text = input_file end i = 0 text.gsub!(/^(&species)/i){p $~; "#{$1}_#{i+=1}"} super(text) end |
Instance Method Details
#check_parameters ⇒ Object
22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 |
# File 'lib/genecrmod/check_parameters.rb', line 22 def check_parameters rcp.namelists.each do |namelist, hash| next if hash[:should_include].kind_of? String and not eval(hash[:should_include]) if en = hash[:enumerator] #ep 'en', en, namelist next unless send(en[:name]) send(en[:name]).times do |i| run_namelist_tests(namelist, hash, i+1) end else run_namelist_tests(namelist, hash) end end warning("Very little analysis will be possible without write_h5 = '.true.'") if not (@write_h5 and @write_h5.fortran_true?) end |
#generate_component_runs ⇒ Object
146 147 148 |
# File 'lib/genecrmod/gene.rb', line 146 def generate_component_runs #puts "HERE" end |
#generate_input_file ⇒ Object
This is a hook which gets called just before submitting a simulation. It sets up the folder and generates any necessary input files.
74 75 76 77 78 79 80 81 82 83 84 85 |
# File 'lib/genecrmod/gene.rb', line 74 def generate_input_file check_parameters if @restart_id @runner.run_list[@restart_id].restart(self) end @diagdir = "." @n_procs_sim = actual_number_of_processors # Simulation time limit is set to ~ 90% of wall mins to allow GENE to # exit gracefully @timelim = @wall_mins * 55 if @wall_mins write_input_file end |
#get_completed_timesteps ⇒ Object
190 191 192 193 194 |
# File 'lib/genecrmod/gene.rb', line 190 def get_completed_timesteps Dir.chdir(@directory) do @completed_timesteps = %x[grep '^\\s\\+\\S\\+\\s*$' nrg.dat 2>/dev/null].split("\n").size end end |
#get_growth_rates ⇒ Object
244 245 246 247 248 249 250 251 252 253 254 255 256 |
# File 'lib/genecrmod/gene.rb', line 244 def get_growth_rates Dir.chdir(@directory) do if FileTest.exist?(ofile = 'omega.dat.h5') @kyvals = get_h5_narray_all(ofile, '/ky').to_gslv @growth_rates = get_h5_narray_all(ofile, '/gamma').to_gslv @frequencies = get_h5_narray_all(ofile, '/omega').to_gslv elsif FileTest.exist?('omega.dat') @kyvals, @growth_rates, @frequencies = GSL::Vector.filescan('omega.dat') else @kyvals, @growth_rates, @frequencies = [nil] * 3 end end end |
#get_h5_narray_all(file, dataset) ⇒ Object
3 4 5 6 7 8 9 10 |
# File 'lib/genecrmod/hdf5.rb', line 3 def get_h5_narray_all(file, dataset) Dir.chdir(@directory) do file = Hdf5::H5File.new(file) narray = file.dataset(dataset).narray_all file.close return narray end end |
#get_status ⇒ Object
167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 |
# File 'lib/genecrmod/gene.rb', line 167 def get_status if @running if @status != :Queueing get_completed_timesteps if completed_timesteps == 0 @status = :NotStarted else @status = :Incomplete end end else get_completed_timesteps if @completed_timesteps == @ntimesteps @status = :Complete else if FileTest.exist?('GENE.finished') @status = :Complete else @status = :Failed end end end end |
#graphkit(name, options = {}) ⇒ Object
This is a temporary hack… we should do this properly!!!
258 259 260 261 262 263 264 265 266 |
# File 'lib/genecrmod/gene.rb', line 258 def graphkit(name, ={}) case name when /growth_rates_vs_ky/ return GraphKit.quick_create([@kyvals, @growth_rates]) when /ion_n2_vs_time/ return GraphKit.quick_create([get_h5_narray_all('nrg.dat.h5', '/nrgIons/time').to_gslv, get_h5_narray_all('nrg.dat.h5', '/nrgIons/n2').to_gslv]) end end |
#input_file_extension ⇒ Object
240 241 242 |
# File 'lib/genecrmod/gene.rb', line 240 def input_file_extension '' end |
#input_file_header ⇒ Object
200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 |
# File 'lib/genecrmod/gene.rb', line 200 def input_file_header "!==============================================================================\n! GENE input file automatically generated by CodeRunner\n!==============================================================================\n!\n! GENE is a code for solving the nonlinear gyrokinetic equation.\n!\n! See http://www2.ipp.mpg.de/~fsj/gene/\n!\n! CodeRunner is a framework for the automated running and analysis\n! of large simulations.\n!\n! See http://coderunner.sourceforge.net\n!\n! Created \#{Time.now.to_s}\n! by CodeRunner version \#{CodeRunner::CODE_RUNNER_VERSION.to_s}\n!\n!==============================================================================\n\n" end |
#namelist_test_failed(namelist, tst) ⇒ Object
57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 |
# File 'lib/genecrmod/check_parameters.rb', line 57 def namelist_test_failed(namelist, tst) return "\n---------------------------\n Test Failed\n---------------------------\n\nNamelist: \#{namelist} \nTest: \#{tst[:test]}\nExplanation: \#{tst[:explanation]}\n\n---------------------------\n" end |
#namelist_text(namelist, enum = nil) ⇒ Object
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 |
# File 'lib/genecrmod/gene.rb', line 97 def namelist_text(namelist, enum = nil) hash = rcp.namelists[namelist] text = "" ext = enum ? "_#{enum}" : "" text << "!#{'='*30}\n!#{hash[:description]} #{enum} \n!#{'='*30}\n" if hash[:description] text << "&#{namelist}\n" hash[:variables].each do |var, var_hash| code_var = (var_hash[:code_name] or var) cr_var = var+ext.to_sym value = send(cr_var) if send(cr_var) and (not var_hash[:should_include] or eval(var_hash[:should_include])) if value.kind_of? Array value.each_with_index do |v, i| output = formatted_variable_output(v) text << " #{code_var}(#{i+1}) = #{output} #{var_hash[:description] ? "! #{var_hash[:description]}": ""}\n" end else output = formatted_variable_output(value) text << " #{code_var} = #{output} #{var_hash[:description] ? "! #{var_hash[:description]}": ""}\n" end elsif rcp.namelists_to_print_not_specified? and rcp.namelists_to_print_not_specified.include?(namelist) text << " ! #{code_var} not specified --- #{var_hash[:description]}\n" end end text << "/\n\n" text end |
#parameter_string ⇒ Object
Parameters which follow the Trinity executable, in this case just the input file.
139 140 141 |
# File 'lib/genecrmod/gene.rb', line 139 def parameter_string "" end |
#parameter_transition ⇒ Object
143 144 |
# File 'lib/genecrmod/gene.rb', line 143 def parameter_transition end |
#print_out_line ⇒ Object
A hook which gets called when printing the standard run information to the screen using the status command.
39 40 41 42 43 44 45 46 47 48 49 |
# File 'lib/genecrmod/gene.rb', line 39 def print_out_line 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 #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 |
#process_directory_code_specific ⇒ Object
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).
156 157 158 159 160 161 162 163 164 165 |
# File 'lib/genecrmod/gene.rb', line 156 def process_directory_code_specific get_status if ctd #get_global_results if !nonlinear or nonlinear.fortran_false? get_growth_rates end end @percent_complete = completed_timesteps.to_f * (istep_nrg||10) / ntimesteps.to_f * 100.0 end |
#restart(new_run) ⇒ Object
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
57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 |
# File 'lib/genecrmod/gene.rb', line 57 def restart(new_run) raise "Restart not tested yet" #new_run = self.dup (rcp.variables).each{|v| new_run.set(v, send(v)) if send(v)} new_run.is_a_restart = true new_run.restart_id = @id new_run.restart_run_name = @run_name new_run.nopt = -1 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 eputs 'Copying GENE Restart file' FileUtils.cp("#@directory/NOUT", "#{new_run.directory}/NIN") end |
#run_namelist_tests(namelist, hash, enum = nil) ⇒ Object
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
# File 'lib/genecrmod/check_parameters.rb', line 3 def run_namelist_tests(namelist, hash, enum = nil) ext = enum ? "_#{enum}" : "" hash[:must_pass].each do |tst| error(namelist_test_failed(namelist, tst)) unless instance_eval(tst[:test]) end if hash[:must_pass] hash[:should_pass].each do |tst| warning(namelist_test_failed(namelist, tst)) unless instance_eval(tst[:test]) end if hash[:should_pass] hash[:variables].each do |var, var_hash| #gs2_var = (var_hash[:gs2_name] or var) cr_var = var+ext.to_sym value = send(cr_var) if value.kind_of? Array value.each{|v| test_variable(namelist, var, var_hash, ext, v)} else test_variable(namelist, var, var_hash, ext, value) end end end |
#test_failed(namelist, var, gs2_var, tst) ⇒ Object
37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 |
# File 'lib/genecrmod/check_parameters.rb', line 37 def test_failed(namelist, var, gs2_var, tst) return "\n---------------------------\n Test Failed\n---------------------------\n\nNamelist: \#{namelist} \nVariable: \#{var}\nGS2 Name: \#{gs2_var}\nValue: \#{send(var)}\nTest: \#{tst[:test]}\nExplanation: \#{tst[:explanation]}\n\n---------------------------\n" end |
#test_variable(namelist, var, var_hash, ext, value) ⇒ Object
72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 |
# File 'lib/genecrmod/check_parameters.rb', line 72 def test_variable(namelist, var, var_hash, ext, value) gs2_var = (var_hash[:gs2_name] or var) cr_var = var+ext.to_sym if value and (not var_hash[:should_include] or eval(var_hash[:should_include])) var_hash[:must_pass].each do |tst| error(test_failed(namelist, cr_var, gs2_var, tst)) unless value.instance_eval(tst[:test]) end if var_hash[:must_pass] var_hash[:should_pass].each do |tst| warning(test_failed(namelist, cr_var, gs2_var, tst)) unless value.instance_eval(tst[:test]) end if var_hash[:should_pass] if (var_hash[:allowed_values] or var_hash[:text_options]) tst = {test: "#{(var_hash[:allowed_values] or var_hash[:text_options]).inspect}.include? self", explanation: "The variable must have one of these values"} error(test_failed(namelist, cr_var, gs2_var, tst)) unless value.instance_eval(tst[:test]) end end end |
#vim_output ⇒ Object Also known as: vo
128 129 130 |
# File 'lib/genecrmod/gene.rb', line 128 def vim_output system "vim -Ro #{output_file} #{error_file}" end |
#write_input_file ⇒ Object
This command uses the infrastructure provided by Run::FortranNamelist, provided by CodeRunner itself.
134 135 136 |
# File 'lib/genecrmod/gene.rb', line 134 def write_input_file File.open("parameters", 'w'){|file| file.puts input_file_text} end |