Module: CodeRunner::Gs2::GSLMatrices

Included in:
CodeRunner::Gs2
Defined in:
lib/gs2crmod/gsl_data.rb

Instance Method Summary collapse

Instance Method Details

#es_heat_flux_over_ky_over_kx_gsl_matrix(options) ⇒ Object



1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
# File 'lib/gs2crmod/gsl_data.rb', line 1069

def es_heat_flux_over_ky_over_kx_gsl_matrix(options)
Dir.chdir(@directory) do
    raise "Heat flux spectrum makes no sense for single modes" if @grid_option == "single"
    options.convert_to_index(:t) if options[:t] or options[:t_element]
    options[:t_index] ||= list(:t).keys.max
    #es_heat_by_k index order (in Fortran) is kx, ky, t
    es_heat_narray = netcdf_file.var("es_heat_by_k").get('start' => [0, 0, 0, options[:t_index] - 1], 'end' => [-1, -1, 0, options[:t_index] - 1])
    es_heat_narray.reshape!(*es_heat_narray.shape.slice(0..1))
    
    gm =  es_heat_narray.to_gm.move_cols_from_box_order
    if options[:limit]
      for i in 0...gm.shape[0]
        for j in 0...gm.shape[1]
#             j+= extra if 
          gm[i, j] = [[gm[i,j], (options[:limit][0] or gm[i,j])].max, (options[:limit][1] or gm[i,j])].min
#             mat[i, j+extra] = gm[i,-j] unless j==0
        end
      end
    end
    return gm
end
end

#growth_rate_over_ky_over_kx_gsl_matrix(options) ⇒ Object



1058
1059
1060
1061
1062
1063
1064
# File 'lib/gs2crmod/gsl_data.rb', line 1058

def growth_rate_over_ky_over_kx_gsl_matrix(options)
  if @growth_rate_at_ky_at_kx.nil?
     raise("The CodeRunner variable growth_rate_at_ky_at_kx does not seem to have been calculated for this run. This may result when the environment variable GS2_CALCULATE_ALL is not set when the run was analyzed. Try setting GS2_CALCULATE_ALL and then re-analyze the run using, e.g. from the command line,\n $ coderunner rc 'cgrf\' -j #{@id}")
        end
  array = @growth_rate_at_ky_at_kx.values.map{|h| h.values}
  return GSL::Matrix.alloc(array.flatten, array.size, array[0].size)
end

#phi0_over_x_over_y_gsl_matrix(options) ⇒ Object



1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
# File 'lib/gs2crmod/gsl_data.rb', line 1175

def phi0_over_x_over_y_gsl_matrix(options)
Dir.chdir(@directory) do

    #options.convert_to_index(:t) if options[:t] or options[:t_element]
    options.convert_to_index(self, :t) if options[:t] or options[:t_element]
    options[:t_index] ||= list(:t).keys.max
    phi_re_narray = netcdf_file.var("phi0").get('start' => [0, 0, 0, options[:t_index] - 1], 'end' => [0, -1, -1, options[:t_index] - 1])
    phi_re_narray.reshape!(*phi_re_narray.shape.slice(1..2))
    # The narray has index order ky, kx, but we want kx, ky for historical reasons, hence the transpose. 
    gm_re = phi_re_narray.to_gm
    phi_im_narray = netcdf_file.var("phi0").get('start' => [1, 0, 0, options[:t_index] - 1], 'end' => [1, -1, -1, options[:t_index] - 1])
    phi_im_narray.reshape!(*phi_im_narray.shape.slice(1..2))
    # The narray has index order ky, kx, but we want kx, ky for historical imasons, hence the transpose. 
    gm_im = phi_im_narray.to_gm
    gm = GSL::Matrix::Complex.re_im(gm_re, gm_im)

    ntheta0_temp = gm.shape[1]
    naky_temp = gm.shape[0]
    

    # Due to a strange GS2 convention, non zonal modes must be divided by 2:
    for i in 1...naky_temp
            for j in 0...ntheta0_temp
        gm[i,j] = gm[i,j]/2.0
      end
    end

    if options[:no_zonal]       
      for i in 0...gm.shape[1]
        gm[0,i] = GSL::Complex.alloc([0,0])
      end
    end
    if xres = (options[:xres] or options[:x_resolution])
       if xres < nx
          puts "Warning: xres should be at least nx. Using nx instead of the xres you specified."
          xres = nx
       end
    else
       xres = nx
    end
    if yres = (options[:yres] or options[:y_resolution])
       if yres < ny
          puts "Warning: yres should be at least ny. Using ny instead of the yres you specified."
          yres = ny
       end
    else
       yres = ny
    end

    # Next, pad with 0's:
    padded = GSL::Matrix::Complex.calloc(yres, xres)
    # Zonal modes first:
    for ix in 0...((ntheta0_temp+1)/2)
        padded[0, ix] = gm[0, ix]
    end
    for ix in ((ntheta0_temp+1)/2)...ntheta0_temp
        padded[0, ix+xres-ntheta0_temp] = gm[0, ix]
    end
    # Now include the non-zonal modes in the padded matrix mat:
    for iy in 1...naky_temp
        for ix in 0...((ntheta0_temp + 1)/2)
           padded[iy, ix] = gm[iy, ix]
        end
        for ix in ((ntheta0_temp+1)/2)...ntheta0_temp
           padded[iy, ix+xres-ntheta0_temp] = gm[iy, ix]
        end
        padded[yres-iy, 0] = gm[iy,0].conj
        for ix in 1...xres
           padded[yres-iy, ix] = padded[iy, xres-ix].conj
        end
     end
    gm = padded

    gm = gm.backward_cols_c2c(false).backward_rows_c2c(false)
    # At this point, gm should be purely real (within machine precision), but let's check to be sure:
    should_be_zero = gm.imag.abs.max
    if should_be_zero > 1.0e-10
       puts "should_be_zero = #{should_be_zero}"
       raise "Something went wrong - reconstructed phi is not purely real."
    end

    gm = gm.real

    if options[:limit]
      for i in 0...gm.shape[0]
        for j in 0...gm.shape[1]
          gm[i, j] = [[gm[i,j], options[:limit][0]].max, options[:limit][1]].min
        end
      end
    end

    return gm
end
end

#spectrum_over_ky_over_kpar_gsl_matrix(options) ⇒ Object



1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
# File 'lib/gs2crmod/gsl_data.rb', line 1125

def spectrum_over_ky_over_kpar_gsl_matrix(options)
Dir.chdir(@directory) do

    #:re, :theta, :kx, :ky
    lkx = list(:kx)
  
    if options[:t_index] or options[:t]
      #extra option required is t_index
      raise CRFatal.new("write_phi_over_time is not enabled so this function won't work") unless @write_phi_over_time
      options.convert_to_index(self, :t)
    end
    temp = phi_av = (lkx.keys.map do |kx_index|   
      if options[:t_index]
        phi =  netcdf_file.var('phi_t').get({'start' => [0,0,kx_index-1,0, options[:t_index] - 1], 'end' => [-1,-2,kx_index-1,-1, options[:t_index] - 1]})
      else
        phi = netcdf_file.var('phi').get({'start' => [0, 0, kx_index - 1, 0], 'end' => [-1, -2, kx_index-1, -1]})
      end
      #ep phi.shape
      phi.reshape(*phi.shape.values_at(0,1,3))
    end).sum / lkx.size

    phi_t = phi_av.to_a #.map{|arr| arr.transpose}.transpose.map{|a| a.transpose}
    #ep 'phi_t', phi_t.size, phi_t[0].size, phi_t[0][0].size
    gvky = gsl_vector('ky')
    gm = GSL::Matrix.alloc(gvky.size, gsl_vector('theta').size-1)
    for ky_element in 0...gm.shape[0]
      #p phi_t[ky_element].transpose[0]
      spectrum = GSL::Vector::Complex.alloc(phi_t[ky_element]).forward.square
      if options[:no_kpar0]
        spectrum[0]=0.0
      end
       #ep spectrum.size
      spectrum = spectrum.from_box_order
      #ep spectrum.shape
      spectrum = spectrum*gvky[ky_element]**2 unless options[:phi2_only]
       #ep gm.size
       #ep spectrum.size
      gm.set_row(ky_element, spectrum)
    end
    if options[:no_zonal]
      gm.row(0).set_all(0.0)
    end
    if options[:log]
      gm = gm.log
    end
    
    return gm
end
end

#spectrum_over_ky_over_kx_gsl_matrix(options) ⇒ Object



1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
# File 'lib/gs2crmod/gsl_data.rb', line 1091

def spectrum_over_ky_over_kx_gsl_matrix(options)
Dir.chdir(@directory) do
    raise "Spectrum makes no sense for single modes" if @grid_option == "single"
    options.convert_to_index(:t) if options[:t] or options[:t_element] 
    options[:t_index] ||= list(:t).keys.max
    #phi2_by_mode index order (in Fortran) is kx, ky, t
    phi_narray = netcdf_file.var("phi2_by_mode").get('start' => [0, 0, options[:t_index] - 1], 'end' => [-1, -1, options[:t_index] - 1])
    phi_narray.reshape!(*phi_narray.shape.slice(0..1))
    
    gm =  phi_narray.to_gm.move_cols_from_box_order
    if options[:times_kx4] or options[:times_kx2]
#         puts 'normalising'
      vals = list(:kx).values.sort
      for i in 0...gm.shape[0]
        for j in 0...gm.shape[1]
#             p vals[j]
          gm[i,j] =  gm[i,j] * (vals[j])**4 if options[:times_kx4]
          gm[i,j] =  gm[i,j] * (vals[j])**2 if options[:times_kx2]
        end
      end
    end
          if options[:no_zonal]
      
      for i in 0...gm.shape[1]
        gm[0,i] = 0.0
      end
    end
    if options[:log]
      gm = gm.log
    end

    return gm
end
end

#transient_amplification_over_ky_over_kx_gsl_matrix(options) ⇒ Object



1065
1066
1067
1068
# File 'lib/gs2crmod/gsl_data.rb', line 1065

def transient_amplification_over_ky_over_kx_gsl_matrix(options)
    array = @transient_amplification_at_ky_at_kx.values.map{|h| h.values}
    return GSL::Matrix.alloc(array.flatten, array.size, array[0].size)
end