Class: CodeRunner::Corfu

Inherits:
Run
  • Object
show all
Defined in:
lib/corfucrmod/corfu.rb

Defined Under Namespace

Classes: Optimisation

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(*args) ⇒ Corfu

Returns a new instance of Corfu.



84
85
86
87
88
89
90
91
92
93
# File 'lib/corfucrmod/corfu.rb', line 84

def initialize(*args)
  @trinity_defaults_strings = []
  @chease_defaults_strings = []
  @ecom_defaults_strings = []
  @convergence = 0.05
  @max_func_evals = 4
  @wall_mins_margin = 5.0
  @wall_mins = 1.0e10
  super(*args)
end

Class Method Details

.run_optimisation(id = ) ⇒ Object



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
# File 'lib/corfucrmod/corfu.rb', line 229

def self.run_optimisation(id = ARGV[-1])
    eputs 'Fetching runner'
    runner = CodeRunner.fetch_runner(Y: '../../', U: true)
    eputs 'Got runner'
    #@run = @runner.run_list[id.to_i]
    #run = self.load(Dir.pwd, runner)
    run = self.new(runner)
    run.read_info
    run.start_time = Time.now.to_i
    eputs 'Loaded run'
    #ep @run
    #raise "Can't find run with id #{id}; #{@runner.run_list.keys}" unless @run
    opt = CodeRunner::Corfu::Optimisation.new(
      run.output, run.search
    )
    eputs 'Created opt'
    trinity_runner = CodeRunner.fetch_runner(Y: 'trinity_runs', X: '/dev/null', C: 'trinity')
    trinity_runner.nprocs = MPI::Comm::WORLD.size
    eputs 'Got trinity runner'
    case run.gs_code
    when 'chease'
      gs_runner = CodeRunner.fetch_runner(Y: 'gs_runs', X: run.chease_exec, C: 'chease')
      gs_runner.nprocs = '1'
    when 'ecom'
      gs_runner = CodeRunner.fetch_runner(Y: 'gs_runs', X: run.ecom_exec, C: 'ecom')
      gs_runner.nprocs = '1'
    else
      raise "Unknown gs_code #{run.gs_code.inspect}"
    end
    eputs 'Got gs runner'
    opt.trinity_runner = trinity_runner
    opt.gs_runner = gs_runner
    opt.serial_optimise(:simplex, run)
end

Instance Method Details

#check_parametersObject



222
223
224
225
226
# File 'lib/corfucrmod/corfu.rb', line 222

def check_parameters
    if delete_final_run
      raise CRFatal.new("Cancelled submission") unless Feedback.get_boolean("You have set delete_final_run which will cause the final run to be deleted. Is this correct?")
    end
end

#evaluate_defaults_file(filename) ⇒ Object



94
95
96
97
98
99
100
101
102
103
104
105
106
107
# File 'lib/corfucrmod/corfu.rb', line 94

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
		next if var_name == :code_run_environment
		unless rcp.variables.include? var_name or (CodeRunner::Trinity.rcp.variables.include? var_name) or (CodeRunner::Chease.rcp.variables.include? var_name or CodeRunner::Gryfx.rcp.variables.include? var_name or CodeRunner::Gs2.rcp.variables.include? var_name or CodeRunner::Ecom.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
    eputs '@nstep_is', @nstep
    eputs '@nstep_first', @nstep_first
end

#generate_component_runsObject



280
281
282
# File 'lib/corfucrmod/corfu.rb', line 280

def generate_component_runs
	#puts "HERE"
end

#generate_input_fileObject

This is a hook which gets called just before submitting a simulation. It sets up the folder and generates any necessary input files.



146
147
148
149
150
151
152
153
154
155
156
157
158
# File 'lib/corfucrmod/corfu.rb', line 146

def generate_input_file
		check_parameters
		if @restart_id
			@runner.run_list[@restart_id].restart(self)
      else
        FileUtils.makedirs('trinity_runs')
        FileUtils.makedirs('gs_runs')
		end
      File.open("driver_script.rb", "w"){|f| f.puts optimisation_script}
      #FileUtils.ln_s("../../#{@trinity_defaults}_defaults.rb", "trinity_runs")
      #FileUtils.ln_s("../../#{@gs_defaults}_defaults.rb", "gs_runs")
      save
end

#get_statusObject



301
302
303
# File 'lib/corfucrmod/corfu.rb', line 301

def get_status
	return :Unknown
end

#graphkit(name, options) ⇒ Object



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
# File 'lib/corfucrmod/corfu.rb', line 310

def graphkit(name, options)
  results = eval(File.read(@directory + '/results'))
  case name
  when 'evolution'
    data = {}
    pars_already_listed = []
    results[:func_calls].reverse.each do |pars, res|
      next if pars_already_listed.include? pars
      pars_already_listed.push pars
      pars.each do |code,cpars|
        cpars.each do |vname, val|
          label = "#{code}_#{vname}"
          data[label] ||= []
          data[label].push val
        end
      end
      data['results']||=[]
      data['results'].push res
    end
    kit = GraphKit.quick_create(data.values)
    data.keys.zip(GraphKit::AXES).each{|lbl,ax| kit.set(ax + :label, lbl)}
    kit
  when 'iteration'
    require 'tokfile/ogyropsi'
    step = options[:step_index]
    raise "Please specify step_index" unless step
    iter, _trinstep = trinrunstep(step)[0]
      kit = TokFile::Ogyropsi.new("#@directory/trinity_runs/v/id_#{iter}/chease/ogyropsi#{sprintf("%05d", step)}.dat").summary_graphkit
      kit.gp.multiplot = " layout 3,3 title 'Iteration: #{iter}; Step: #{step}; Pars: #{results[:func_calls][iter-1][0]}; Result: #{results[:func_calls][iter-1][1].inspect}'"
      kit[-1].gp.size = "ratio 1"
      #pp 'kit', kit
    return kit
  end
end

#gs_defaults_stringsObject



50
51
52
53
54
55
56
57
# File 'lib/corfucrmod/corfu.rb', line 50

def gs_defaults_strings
  case @gs_code
  when 'chease'
    @chease_defaults_strings ||= @gs_defaults_strings # For backwards compatibility
  else
    @ecom_defaults_strings
  end
end

#gs_defaults_strings=(strs) ⇒ Object



58
59
60
# File 'lib/corfucrmod/corfu.rb', line 58

def gs_defaults_strings=(strs)
  @chease_defaults_strings = strs
end

#gs_parsObject



61
62
63
64
65
66
67
68
# File 'lib/corfucrmod/corfu.rb', line 61

def gs_pars
  case @gs_code
  when 'chease'
    @chease_pars ||= @gs_pars
  when 'ecom'
    @gs_pars
  end
end

#gs_pars=(pars) ⇒ Object

For backwards compatibility



71
72
73
# File 'lib/corfucrmod/corfu.rb', line 71

def gs_pars=(pars)
  @chease_pars = pars
end

#input_file_extensionObject



306
307
308
# File 'lib/corfucrmod/corfu.rb', line 306

def input_file_extension
	''
end

#max_stepObject



360
361
362
# File 'lib/corfucrmod/corfu.rb', line 360

def max_step
  trinstep_mappings[-1][1]
end

#optimisation_scriptObject



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
# File 'lib/corfucrmod/corfu.rb', line 160

def optimisation_script
  return <<EOF
  require 'mpi'
  class MPI::Comm
    def Bcast_string(str)
      @arr = NArray.int(1)
      if rank==0
        @arr[0] = str.length
        Bcast(@arr, 0)
        Bcast(str, 0)
      else
        Bcast(@arr,0)
        str << " " while str.length < @arr[0]
        Bcast(str,0)
      end
    end
  end
  puts 'Initializing mpi'
MPI.Init
  comm = MPI::Comm::WORLD
  rank = comm.rank
  puts ['Rank is: ', rank]
  if rank==0
    begin
      require 'coderunner'
      CodeRunner.setup_run_class('trinity')
      CodeRunner.setup_run_class('corfu')
      #require 'corfucrmod'
      require 'corfucrmod/trinitycrdriver'
      CodeRunner::Corfu.run_optimisation(#@id)
    rescue =>err
      arr = NArray.int(1)
      arr[0] = 0
      comm.Bcast(arr,0)
      raise err
    end
    arr = NArray.int(1)
    arr[0] = 0
    comm.Bcast(arr,0)
  else
    require 'corfucrmod/trinitycrdriver'
    arr = NArray.int(1)
    puts "Proc \#{comm.rank} waiting for message"
    loop do
      comm.Bcast(arr, 0)
      puts "Proc \#{comm.rank} received instructions: \#{arr[0]}"
      dir = ""
      run_name  = ""
      if arr[0] == 1
        comm.Bcast_string(dir)
        comm.Bcast_string(run_name)
        puts "Proc \#{comm.rank} calling Trinity with run_name: \#{run_name}"
        Dir.chdir(dir){CodeRunner::Trinity.new.run_trinity(run_name+'.trin', comm)}
      else
        break
      end
    end
  end
  MPI.Finalize
EOF
end

#parameter_stringObject

Parameters which follow the Trinity executable, in this case just the input file.



273
274
275
# File 'lib/corfucrmod/corfu.rb', line 273

def parameter_string
	" driver_script.rb #@id"
end

#parameter_transitionObject



277
278
# File 'lib/corfucrmod/corfu.rb', line 277

def parameter_transition
end

A hook which gets called when printing the standard run information to the screen using the status command.



110
111
112
113
114
115
116
117
118
119
120
121
# File 'lib/corfucrmod/corfu.rb', line 110

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
	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_specificObject

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).



290
291
292
293
294
295
296
297
298
299
# File 'lib/corfucrmod/corfu.rb', line 290

def process_directory_code_specific
	get_status
	#p ['id is', id, 'ctd is ', ctd]
	#if ctd
		#get_global_results 
	#end
	#p ['fusionQ is ', fusionQ]
	#@percent_complete = completed_timesteps.to_f / ntstep.to_f * 100.0
    @percent_complete = 0.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



129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
# File 'lib/corfucrmod/corfu.rb', line 129

def restart(new_run)
	#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.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.trinity_pars = @trinity_pars.absorb(new_run.trinity_pars) if @trinity_pars and new_run.trinity_pars
	new_run.naming_pars.delete(:restart_id)
	new_run.generate_run_name
    raise "trinity_runs directory already exists" if FileTest.exist? new_run.directory + '/trinity_runs'
    FileUtils.ln_s(@directory + '/trinity_runs', new_run.directory + '/trinity_runs')
    FileUtils.ln_s(@directory + '/gs_runs', new_run.directory + '/gs_runs')
end

#trinrunstep(overall_step) ⇒ Object



354
355
356
357
358
# File 'lib/corfucrmod/corfu.rb', line 354

def trinrunstep(overall_step)
  p 'trinstep_mappings', trinstep_mappings
  trinrun, cusum = trinstep_mappings.to_a.find{|t, c| c>=overall_step}
  [trinrun, cusum-overall_step + 1]
end

#trinstep_mappingsObject



345
346
347
348
349
350
351
352
# File 'lib/corfucrmod/corfu.rb', line 345

def trinstep_mappings
  @trinstep_mappings = (
    trinity_runner = CodeRunner.fetch_runner(Y: @directory + '/trinity_runs')
    sizes = trinity_runner.run_list.values.sort_by{|r| r.id}.map{|r| r.get_completed_timesteps; r.completed_timesteps}
    i = 0; cs = 0
    sizes.inject({}){|h,sz| cs+=sz; h[i+=1]=cs; h}
  )
end

#vim_outputObject Also known as: vo



266
267
268
# File 'lib/corfucrmod/corfu.rb', line 266

def vim_output
  system "vim -Ro #{output_file} #{error_file}"
end