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



1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
# File 'lib/gs2crmod/gsl_data.rb', line 1182

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



1171
1172
1173
1174
1175
1176
1177
# File 'lib/gs2crmod/gsl_data.rb', line 1171

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



1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
# File 'lib/gs2crmod/gsl_data.rb', line 1288

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



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
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
# File 'lib/gs2crmod/gsl_data.rb', line 1238

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



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
# File 'lib/gs2crmod/gsl_data.rb', line 1204

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



1178
1179
1180
1181
# File 'lib/gs2crmod/gsl_data.rb', line 1178

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