Class: CodeRunner::Trinity

Inherits:
Run::FortranNamelist
  • Object
show all
Includes:
ActualParameterValues, OutputFiles, ReadNetcdf, TrinityGraphKits, TrinityMultiKits
Defined in:
lib/trinitycrmod/trinity.rb,
lib/trinitycrmod/read_netcdf.rb,
lib/trinitycrmod/trinity_gs2.rb,
lib/trinitycrmod/output_files.rb,
lib/trinitycrmod/check_parameters.rb,
lib/trinitycrmod/flux_interpolator.rb,
lib/trinitycrmod/actual_parameter_values.rb,
lib/trinitycrmod/graphs.rb,
lib/trinitycrmod/chease.rb,
lib/trinitycrmod/ecom.rb

Overview

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

Defined Under Namespace

Modules: ActualParameterValues, OutputFiles, ReadNetcdf, TrinityComponent, TrinityGraphKits, TrinityMultiKits Classes: FluxOptionError, NetcdfSmartReader, SmartGraphKitError

Class Method Summary collapse

Instance Method Summary collapse

Methods included from TrinityMultiKits

#calibration_graphkit, #geometry_graphkit, #profiles_graphkit

Methods included from TrinityGraphKits

#ang_mom_prof_graphkit, #dens_prof_graphkit, #eln_hflux_calibration_graphkit, #eln_hflux_gb_prof_graphkit, #eln_hflux_prof_graphkit, #eln_pwr_prof_graphkit, #eln_temp_prof_graphkit, #fluxes_prof_graphkit, #integrated_profiles_graphkit, #ion_hflux_calibration_graphkit, #ion_hflux_gb_prof_graphkit, #ion_hflux_prof_graphkit, #ion_pwr_prof_graphkit, #ion_temp_prof_graphkit, #lflux_gb_prof_graphkit, #nt_prof_graphkit, #pbalance_prof_graphkit, #pflux_calibration_graphkit, #prof_graphkit, #pwr_prof_graphkit, #torque_prof_graphkit

Methods included from ActualParameterValues

#dflx_stencil_actual, #equal_ion_temps_actual, #evolve_density_actual, #evolve_flow_actual, #evolve_grads_only_actual, #evolve_temperature_actual, #fork_flag_actual, #te_equal_ti_actual, #te_fixed_actual

Methods included from OutputFiles

#fluxes_outfile, #geo_outfile, #info_outfile, #nt_outfile, #pbalance_outfile, #pwr_outfile, #time_outfile, #view_outfiles

Methods included from ReadNetcdf

#new_ncclose, #new_netcdf_file, #new_netcdf_filename

Class Method Details

.defaults_file_headerObject



648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
# File 'lib/trinitycrmod/trinity.rb', line 648

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

.flux_class(code) ⇒ Object



140
141
142
143
144
145
146
147
# 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

.get_input_help_from_source_code(source_folder) ⇒ Object



596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
# File 'lib/trinitycrmod/trinity.rb', line 596

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

.interpolate_fluxes(old_run_folder, run_folder, grad_option, ntspec) ⇒ Object

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



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

.use_new_defaults_file_with_flux(name = ARGV[-4], trinity_input_file = ARGV[-3], flux_input_file = ARGV[-2], fluxcode = ARGV[-1]) ⇒ Object

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.



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

Instance Method Details

#chease_runObject



5
6
7
8
9
10
11
12
# 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

#check_flux_optionObject



21
22
23
24
25
# 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

#check_geometeryObject



11
12
13
# 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

#check_parallelisationObject



15
16
17
18
19
# 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

#check_parametersObject



3
4
5
6
7
8
9
# File 'lib/trinitycrmod/check_parameters.rb', line 3

def check_parameters

	check_geometery
	check_parallelisation
	check_flux_option

end

#ecom_runObject



5
6
7
8
9
10
11
12
# 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

#evaluate_defaults_file(filename) ⇒ Object

Override standard CodeRunner method to allow for flux code variables



117
118
119
120
121
122
123
124
125
126
127
# 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

#flux_classObject



148
149
150
# File 'lib/trinitycrmod/trinity_gs2.rb', line 148

def flux_class
  self.class.flux_class(@flux_option)
end

#flux_folder_name(i) ⇒ Object



170
171
172
173
174
175
176
177
178
179
# 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

#flux_gryfx?Boolean

Is the flux tube code being used gryfx?

Returns:

  • (Boolean)


156
157
158
# File 'lib/trinitycrmod/trinity_gs2.rb', line 156

def flux_gryfx?
	@flux_option == "gryfx"
end

#flux_gs2?Boolean

Is the flux tube code being used gs2?

Returns:

  • (Boolean)


152
153
154
# File 'lib/trinitycrmod/trinity_gs2.rb', line 152

def flux_gs2?
	@flux_option == "gs2"
end

#flux_run_name(i) ⇒ Object



160
161
162
163
164
165
166
167
168
169
# 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

#flux_runsObject

Generates the component runs for GS2 and returns the hash Raises an error if flux_option != “gs2”

Raises:



98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
# 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

#generate_component_runsObject



430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
# File 'lib/trinitycrmod/trinity.rb', line 430

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

#generate_flux_input_filesObject

Writes the gs2 input files, creating separate subfolders for them if @subfolders is .true.



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
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
# File 'lib/trinitycrmod/trinity.rb', line 342

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

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



207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
# File 'lib/trinitycrmod/trinity.rb', line 207

def generate_input_file
    @run_name += "_t"
    if @restart_id
      @runner.run_list[@restart_id].restart(self)
    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

#generate_run_nameObject

Override CodeRunner::Run method to deal with flux_pars properly when generating run_name



282
283
284
285
286
287
288
289
290
291
292
293
# File 'lib/trinitycrmod/trinity.rb', line 282

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

#get_1d_array_float(outfile, column_header) ⇒ Object



58
59
60
61
62
# 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

#get_2d_array_float(outfile, column_header, index_header) ⇒ Object

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


52
53
54
55
56
# 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

#get_completed_timestepsObject



559
560
561
562
563
564
565
# File 'lib/trinitycrmod/trinity.rb', line 559

def get_completed_timesteps
  Dir.chdir(@directory) do
    @completed_timesteps = time_outfile.exists? ?
      time_outfile.get_1d_array_integer(/itstep/).max :
      0
  end
end

#get_global_resultsObject



568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
# File 'lib/trinitycrmod/trinity.rb', line 568

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

#get_statusObject



537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
# File 'lib/trinitycrmod/trinity.rb', line 537

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

#graphkit(name, options) ⇒ Object

This is the hook that is called by CodeRunner, providing the graphkit with the given name and functions to the CodeRunner framework



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

#gs2_run_timesObject

An array of arrays containing the GS2 run times for each iteration. Produced unscientifically by scanning the stdout.

Raises:



131
132
133
134
135
136
137
138
# 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

#input_file_extensionObject



678
679
680
# File 'lib/trinitycrmod/trinity.rb', line 678

def input_file_extension
  '.trin'
end

#input_file_headerObject



626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
# File 'lib/trinitycrmod/trinity.rb', line 626

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

#is_converged?Boolean

Returns:

  • (Boolean)


508
509
510
511
512
513
514
515
516
517
518
519
520
# File 'lib/trinitycrmod/trinity.rb', line 508

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

#list(var) ⇒ Object

Returns a hash of the specified dimension in the form value where index is 1-based Dimension can be: :t :rho :rho_cc



69
70
71
72
73
74
75
76
77
78
# 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

#n_flux_tubesObject



331
332
333
334
335
336
337
338
# File 'lib/trinitycrmod/trinity.rb', line 331

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

#n_flux_tubes_jacObject

The number of separate flux tube results needed for the jacobian



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
# File 'lib/trinitycrmod/trinity.rb', line 296

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

#netcdf_smart_readerObject



153
154
155
# File 'lib/trinitycrmod/read_netcdf.rb', line 153

def netcdf_smart_reader
  NetcdfSmartReader.new(new_netcdf_file)
end

#parameter_stringObject

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



423
424
425
# File 'lib/trinitycrmod/trinity.rb', line 423

def parameter_string
  @run_name + ".trin"
end

#parameter_transitionObject



427
428
# File 'lib/trinitycrmod/trinity.rb', line 427

def parameter_transition
end

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



78
79
80
81
82
83
84
85
86
87
88
89
90
# 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

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



527
528
529
530
531
532
533
534
535
# File 'lib/trinitycrmod/trinity.rb', line 527

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

#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



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

#run_heuristic_analysisObject

Raises:

  • (CRMild)


665
666
667
668
669
670
671
672
673
674
675
676
# File 'lib/trinitycrmod/trinity.rb', line 665

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

#setup_cheaseObject



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# 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

#setup_ecomObject



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# 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

#smart_graphkit(options) ⇒ Object



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
# 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(parameters, start_from_defaults = true) ⇒ Object

Update submission parameters in the normal way then deal with parameters for the flux code. Each flux code will behave differently.

  • Gs2 flux_pars: 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


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
# File 'lib/trinitycrmod/trinity.rb', line 237

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

#uses_chease?Boolean

Returns:

  • (Boolean)


2
3
4
# File 'lib/trinitycrmod/chease.rb', line 2

def uses_chease?
	@geo_option == "chease" or @init_option == "chease" or @species_option == "chease"
end

#uses_ecom?Boolean

Returns:

  • (Boolean)


2
3
4
# File 'lib/trinitycrmod/ecom.rb', line 2

def uses_ecom?
	@geo_option == "ecom" 
end

#vim_outputObject Also known as: vo



412
413
414
# File 'lib/trinitycrmod/trinity.rb', line 412

def vim_output
  system "vim -Ro #{output_file} #{error_file} #@directory/#@run_name.error #@directory/#@run_name.out "
end

#watch_calibration_statusObject



398
399
400
401
402
403
404
405
406
407
408
409
410
# File 'lib/trinitycrmod/trinity.rb', line 398

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

#write_input_fileObject

This command uses the infrastructure provided by Run::FortranNamelist, provided by CodeRunner itself.



418
419
420
# File 'lib/trinitycrmod/trinity.rb', line 418

def write_input_file
  File.open(@run_name + ".trin", 'w'){|file| file.puts input_file_text}
end