Module: CodeRunner::Gs2::GSLTensors

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

Constant Summary collapse

FIELD_VALUES =

end

[:phi]
TRIVIAL_INDICES =
[:graphkit_name]
TIME_VARYING_INDICES =
[:t_index, :begin_element, :end_element, :frame_index, :t_index_window]
IRRELEVANT_INDICES =
FIELD_VALUES + TRIVIAL_INDICES + TIME_VARYING_INDICES

Instance Method Summary collapse

Instance Method Details

#apar_gsl_tensor(options) ⇒ Object



309
310
311
# File 'lib/gs2crmod/gsl_data_3d.rb', line 309

def apar_gsl_tensor(options)
	return GSL::Tensor.new(netcdf_file.var('apar').get)
end

#bpar_gsl_tensor(options) ⇒ Object



312
313
314
# File 'lib/gs2crmod/gsl_data_3d.rb', line 312

def bpar_gsl_tensor(options)
	return GSL::Tensor.new(netcdf_file.var('bpar').get)
end

#cartesian_coordinates_gsl_tensor(options) ⇒ Object



673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
# File 'lib/gs2crmod/gsl_data_3d.rb', line 673

def cartesian_coordinates_gsl_tensor(options)
	cyl = cylindrical_coordinates_gsl_tensor(options)
	shape = cyl.shape
	cart = GSL::Tensor.alloc(*shape)
	for i in 0...shape[1]
		for j in 0...shape[2]
			for k in 0...shape[3]
				r = cyl[0,i,j,k]
				z = cyl[1,i,j,k]
				phi = cyl[2,i,j,k]
				#cart[0,i,j,k] = r # Y
				cart[0,i,j,k] = r*Math.cos(phi) # X
				#cart[1,i,j,k] = phi # X
				cart[1,i,j,k] = r*Math.sin(phi) # Y
				cart[2,i,j,k] = z
			end
		end
	end
	cart
end

#constant_torphi_surface_gsl_tensor(options) ⇒ Object

Returns a rank 2 tensor, which gives, as a function of the x index j and the theta index k, the y index nearest to a poloidal plane at angle options is the torus was filled with periodic copies of the flux surface. Used for making cross sections at a constant toroidal angle.



375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
# File 'lib/gs2crmod/gsl_data_3d.rb', line 375

def constant_torphi_surface_gsl_tensor(options)
	ops = options.dup
	IRRELEVANT_INDICES.each{|v| ops.delete(v)}
	return  cache[[:constant_torphi_surface_gsl_tensor, ops]] if cache[[:constant_torphi_surface_gsl_tensor, ops]]
	correct_3d_options(options)
	torphiout = options[:torphi]
	cyls = cylindrical_coordinates_gsl_tensor(options.absorb({extra_points: :y}))
	shpc = cyls.shape
	factors = geometric_factors_gsl_tensor(options)
	#ep shpc, 'shpc'
	#xsize = case shpc[2]

	yvec = gsl_vector('y', options)
	#ep yvec.to_a ; gets
	x = gsl_vector('x', options)
	dy = yvec[1] - yvec[0]
	torphi_const = GSL::Tensor.int(shpc[2], shpc[3]) # don't include extra x point
	xfac = 1.0 / options[:rho_star_actual]
	yfac = options[:rhoc_actual] / options[:q_actual] / options[:rho_star_actual]	
				#coordinates[2,i,j,k] = y[i] / yfac - factors[2,k] - x[j]/xfac*factors[5,k] # phi
	twopi = Math::PI*2
	for j in 0...shpc[2]
		for k in 0...shpc[3]
			y = yfac * (torphiout + factors[2,k] + x[j]/xfac*factors[5,k])
			if options[:no_copies]
				i = (y/dy).floor 
			else
				i = (y/dy).floor % yvec.size
			end
			torphi_const[j,k] = i
		end
	end
	return torphi_const

	#ep torphi_const; gets
end

#correct_3d_options(options) ⇒ Object

Adjust n0, rho_star_actual and q_actual to ensure periodicity



506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
# File 'lib/gs2crmod/gsl_data_3d.rb', line 506

def correct_3d_options(options)
	raise "Please specify options[:rho_star] or options[:n0]" unless options[:rho_star] or options[:n0]
	case @equilibrium_option
	when "s-alpha"
		qinp = epsl / (pk||2*kp)
		#xfac = @epsl**4/options[:rho_star]/4/pka**2/@eps**2
		#xfac_geo = 1
		#yfac = 1/options[:rho_star]/@epsl*2*pka*@eps
		#yfac_geo = 2*pka*@eps/@epsl**2
		#yfac_geo = 2*pka*@eps/@epsl**2
		options[:rhoc_actual] =rhoc =  2 * eps / epsl
	else
		options[:rhoc_actual] = rhoc = @rhoc
		qinp = @qinp
	end
	#eputs "Checking that rho_star and q satisfy periodicity..."
	rho_star_inp = options[:rho_star]
	y = gsl_vector('y', options)
	ly = (y[1]-y[0]) * (y.size) 
	n0_fac = 2.0*Math::PI * rhoc / ly
	n0_inp = options[:n0] || n0_fac / qinp / rho_star_inp 
	if n0_inp%1.0==0.0
		n0 = n0_inp
	else
		#eputs "Input n0 is equal to #{n0_inp}..."
		n0 = n0_inp.ceil
		#eputs "Set n0 to #{n0}..."
	end
	
	if (qinp*n0)%1.0==0.0
		q_actual = qinp
	else
		q_actual = (qinp*n0).round.to_f/n0
		#eputs "Set q to #{q_actual}..."
	end
	options[:q_actual] = q_actual
	unless options[:rho_star_actual] and options[:rho_star_actual] == n0_fac/n0/q_actual
		#eputs "Adjusting rho_star to satisfy periodicity ..."
		options[:rho_star_actual] = n0_fac/n0/q_actual
		#eputs "Set rhostar to #{options[:rho_star_actual]}..."
		#eputs "Note... to avoid adjustment of q specify n0 as an input rather than rho_star. Make sure that n0 is an integer and n0 * q is an integer."
	end
end

#cylindrical_coordinates_gsl_tensor(options) ⇒ Object

Return a rank 4 tensor which give cylindrical coordinates R,Z,torphi as a function of gs2 coordinates y, x, theta.

a = cylindrical_coordinates_gsl_tensor(options)

# pseudocode R(y, x, theta) = a Z(y, x, theta) = a torphi(y, x, theta) = a



561
562
563
564
565
566
567
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
595
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
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
# File 'lib/gs2crmod/gsl_data_3d.rb', line 561

def cylindrical_coordinates_gsl_tensor(options)
	ops = options.dup
	(IRRELEVANT_INDICES + [:torphi, :torphi_values]).each{|v| ops.delete(v)}
	return  cache[[:cylindrical_coordinates_gsl_tensor, ops]] if cache[[:cylindrical_coordinates_gsl_tensor, ops]]
	#ep ops; gets
	#options = options.dup
	x = gsl_vector('x', options)
	y = gsl_vector('y', options)
	ly = 2*Math::PI*y0#(y[1]-y[0]) * (y.size) 
	if [true,:x].include? options[:extra_points]
		ep "Extending x..."
		x = x.connect([2*x[-1] - x[-2]].to_gslv).dup
	end
	if [true,:y].include? options[:extra_points]
		ep "Extending y..."
		y = y.connect([2*y[-1] - y[-2]].to_gslv).dup
		raise "ly corrected incorrectly #{ly},#{y[-1]},#{y[0]},#{y[-1]-y[0]}" unless (ly-(y[-1] - y[0])).abs / ly.abs < 1.0e-8
	end


	#if options[:xmax] 
	 #if	options[:xmin]
		 #x = x.subvector(options[:xmin], options[:xmax] - options[:xmin])
	 #else
		 #x = x[options[:xmax]].to_gslv
	 #end
	#elsif options[:xmin]
	 #x = x[options[:xmin]].to_gslv
	#end
	#if options[:ymax] 
	 #if	options[:ymin]
		 #y = y.subvector(options[:ymin], options[:ymax] - options[:ymin])
	 #else
		 #y = y[options[:ymax]].to_gslv
	 #end
	#elsif options[:ymin]
	 #y = y[options[:ymin]].to_gslv
	#end



	#ep [options, options[:xmin]||0, (options[:xmax]||x.size-1) - (options[:xmin]||0) + 1]
	x = x.subvector(options[:xmin]||0, (options[:xmax]||x.size-1) - (options[:xmin]||0) + 1).dup # if options[:xout] and options[:xin]
	y = y.subvector(options[:ymin]||0, (options[:ymax]||y.size-1) - (options[:ymin]||0) + 1).dup # if options[:yout] and options[:yin]
	###y = y.subvector(options[:ymin], options[:ymax] - options[:ymin] + 1)# if yi = options[:yout] and options[:yin]
	#	
	###ep 'ncopy', options[:ncopy]
	#y = y + options[:ncopy] * (y[-1]-y[0]) if options[:ncopy]
	y = y + options[:ncopy] * ly if options[:ncopy]
	#ep 'y', y
		#ep y; gets
	#ep options; gets
	theta = gsl_vector('theta', options)
	#ep theta; gets;
	#ep 'thsize', @ntheta, theta.size
	correct_3d_options(options)
	rhoc = options[:rhoc_actual]
	q_actual = options[:q_actual]
	xfac = 1.0 / options[:rho_star_actual]
	yfac = rhoc / q_actual / options[:rho_star_actual]
	factors = geometric_factors_gsl_tensor(options)




	coordinates = GSL::Tensor.alloc(3, y.size, x.size, theta.size)
	for i in 0...y.size
		for j in 0...x.size
			for k in 0...theta.size
				coordinates[0,i,j,k] = factors[0,k] + x[j]/xfac*factors[3,k] # R
				coordinates[1,i,j,k] = factors[1,k] + x[j]/xfac*factors[4,k] # Z
				coordinates[2,i,j,k] = y[i] / yfac - factors[2,k] - x[j]/xfac*factors[5,k] # phi
				#ep [i,j,k], coordinates[0, false, j,k].to_a
				if gs2f = options[:gs2_coordinate_factor]
					rgs2 = (x[j]**2 + y[i]**2)**0.5
					if rgs2 < 1.0e-8
						phigs2 = 0
					else
						phigs2 = Math.acos(x[j]/rgs2)
					end
					coordinates[0,i,j,k] = rgs2 * gs2f + coordinates[0,i,j,k] * (1.0-gs2f)
					coordinates[1,i,j,k] = theta[k] * gs2f + coordinates[1,i,j,k] * (1.0-gs2f)
					coordinates[2,i,j,k] = phigs2 * gs2f + coordinates[2,i,j,k] * (1.0-gs2f)
				end


					
			end
		end
	end
	#exit
	case tp = options[:toroidal_projection]
	when Numeric
		coordinates[2, false] = tp
	end
	cache[[:cylindrical_coordinates_gsl_tensor, ops]] = coordinates
	#save  # save the run to save the hard_cache
	return coordinates
end

#field_gsl_tensor(options) ⇒ Object



155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
# File 'lib/gs2crmod/gsl_data_3d.rb', line 155

def field_gsl_tensor(options)
	if options[:t_index]
		#ep options; gets
		raise CRFatal.new("write_phi_over_time is not enabled so this function won't work") unless @write_phi_over_time
		arr =  GSL::Tensor.new(netcdf_file.var(options[:field_name].to_s + '_t').get({'start' => [0,(options[:thetamin]||0),0,0, options[:t_index] - 1], 'end' => [-1,(options[:thetamax]||-1),(options[:nakx]||0)-1,(options[:naky]||0)-1, options[:t_index] - 1]}))
		#ep 'arr.shape', arr.shape
		arr.reshape!(*arr.shape.slice(1...arr.shape.size))
		
	else
		arr =  GSL::Tensor.new(netcdf_file.var(options[:field_name]).get({'start' => [0,(options[:thetamin]||0),0,0], 'end' => [-1,(options[:thetamax]||-1),(options[:nakx]||0)-1,(options[:naky]||0)-1]}))
		#ep 'arr.shape', arr.shape
	end
	arr[0, true, true, true] = 0.0 if options[:no_zonal]
	#arr = arr[options[:nakx] ? 0...options[:nakx] : true, options[:naky] ? 0...options[:naky] : true, true, true] if options[:nakx] or options[:naky]
	return arr

end

#field_real_space_gsl_tensor(options) ⇒ Object



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
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
# File 'lib/gs2crmod/gsl_data_3d.rb', line 179

def field_real_space_gsl_tensor(options)
	fieldc = field_gsl_tensor_complex(options)
	shape = fieldc.shape
	workspacex = GSL::Vector::Complex.alloc(shape[1])
	workspacey = GSL::Vector.alloc(shape[0]*2-2+shape[0]%2)
	field_real_space = GSL::Tensor.alloc(workspacey.size, shape[1], shape[2])
	for j in 0...shape[2] #theta
		for i in 0...shape[0] #ky
			#narr = fieldc[i, true, j] 
			for k in 0...shape[1]
				workspacex[k] = GSL::Complex.alloc(fieldc[i,k,j].real, fieldc[i,k,j].imag)
			end
			workspacex = workspacex.backward
			for k in 0...shape[1]
				fieldc[i,k,j] = Complex(*workspacex[k].to_a)
			end
		end
		for k in 0...shape[1] #kx
			m = 0
			for i in 0...shape[0] #ky
				workspacey[m] = fieldc[i,k,j].real
				m+=1
				next if i==0 or (shape[0]%2==0 and i == shape[0]/2 + 1)
				workspacey[m] = fieldc[i,k,j].imag
				m+=1
			end
			workspacey = workspacey.backward
			for i in 0...workspacey.size
				field_real_space[i,k,j] = workspacey[i]
			end
		end
	end
	shp = field_real_space.shape
	#ep options
	field_real_space = field_real_space[options[:ymin]||0..options[:ymax]||(shp[0]-1), options[:xmin]||0..options[:xmax]||(shp[1]-1), true] 
	if kint = options[:interpolate_theta]
		shape = field_real_space.shape
		new_shape = shape.dup
		new_shape[-1] = ((shape[-1]-1)*kint+1)
		field_real_space_new = GSL::Tensor.float(*new_shape)
		#p shape,new_shape
		for i in 0...(new_shape[0])
		for j in 0...(new_shape[1])
		field_real_space_new[i,j, new_shape[-1]-1] = field_real_space[i,j,shape[-1]-1] # set the endpoint
		for k in 0...(new_shape[-1]-1)
			km = k%kint
			frac = km.to_f/kint.to_f
			#kold = (k-km)/(new_shape[-1]-1)*(shape[-1]-1)
			kold = (k-km)/kint
			#ep ['k', k, 'kold', kold]
			field_real_space_new[i,j, k] = field_real_space[i,j, kold] * (1.0-frac) + field_real_space[i,j, kold+1] * frac
		end
		end
		end
		field_real_space = field_real_space_new
	end	

	return field_real_space

end

#field_real_space_gsl_tensor_2(options) ⇒ Object



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
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
# File 'lib/gs2crmod/gsl_data_3d.rb', line 239

def field_real_space_gsl_tensor_2(options)
	field = field_gsl_tensor(options)
	field_narray = field.narray
	shape = field.shape
	workspacex = GSL::Vector::Complex.alloc(shape[1])
	workspacey = GSL::Vector.alloc(shape[0]*2-2+shape[0]%2)
	field_real_space = GSL::Tensor.alloc(workspacey.size, shape[1], shape[2])
	field_real_space_narray = field_real_space.narray
	for j in 0...shape[2] #theta
		for i in 0...shape[0] #ky
			#narr = fieldc[i, true, j] 
			for k in 0...shape[1]
				workspacex[k] = GSL::Complex.alloc(field_narray[0,j,k,i], field_narray[1,j,k,i])
			end
			workspacex = workspacex.backward
			for k in 0...shape[1]
				field_narray[0,j,k,i] = workspacex[k].real
				field_narray[1,j,k,i] = workspacex[k].imag
			end
		end
		for k in 0...shape[1] #kx
			m = 0
			for i in 0...shape[0] #ky
				workspacey[m] = field_narray[0,j,k,i]
				m+=1
				next if i==0 or (shape[0]%2==0 and i == shape[0]/2 + 1)
				workspacey[m] = field_narray[1,j,k,i]
				m+=1
			end
			workspacey = workspacey.backward
			for i in 0...workspacey.size
				field_real_space_narray[j,k,i] = workspacey[i]
			end
		end
	end
	shp = field_real_space.shape
	#p 'test', field_real_space[0,2,3]
	#ep options
	field_real_space = field_real_space[options[:ymin]||0..options[:ymax]||(shp[0]-1), options[:xmin]||0..options[:xmax]||(shp[1]-1), true] 
	#p 'test2', field_real_space[0,2,3]
	if kint = options[:interpolate_theta]
		shape = field_real_space.shape
		new_shape = shape.dup
		new_shape[-1] = ((shape[-1]-1)*kint+1)
		field_real_space_new = GSL::Tensor.float(*new_shape)
		field_real_space_new_narray = field_real_space_new.narray
		#p shape,new_shape
		for i in 0...(new_shape[0])
		for j in 0...(new_shape[1])
		field_real_space_new_narray[new_shape[-1]-1, j, i] = field_real_space_narray[shape[-1]-1, j, i] # set the endpoint
		for k in 0...(new_shape[-1]-1)
			km = k%kint
			frac = km.to_f._orig_div(kint.to_f)
			#kold = (k-km)/(new_shape[-1]-1)*(shape[-1]-1)
			kold = (k-km)._orig_div(kint)
			#ep ['k', k, 'kold', kold]
			field_real_space_new_narray[k,j,i] = field_real_space_narray[kold,j,i]._orig_mul(1.0-frac) + field_real_space_narray[kold+1,j,i]._orig_mul(frac)
			#if (i==0 and j==2 and k==3)
				#p ['frac', frac]
			#end
		end
		end
		end
		field_real_space = field_real_space_new
	end	
	#p field_real_space_new.shape;

	return field_real_space

end

#geometric_factors_gsl_tensor(options) ⇒ Object

Order is R0,Z0,a0,Rprim,Zprim,aprim



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
344
345
346
347
348
349
# File 'lib/gs2crmod/gsl_data_3d.rb', line 316

def geometric_factors_gsl_tensor(options)
	#ops = options.dup; ops.delete :phi
#ep ops; gets
	case @equilibrium_option
	when "s-alpha"
		return geometric_factors_salpha_gsl_tensor(options)
	else
		theta_vec = gsl_vector(:theta, options)
		factors = GSL::Tensor.alloc(6,theta_vec.size)
		values = File.read("#@directory/#@run_name.g").split(/\s*\n\s*/)
		3.times{values.shift}
		values = values.map{|str| str.split(/\s+/).map{|s| s.to_f}}.transpose
		#ep values
		shape = factors.shape
		for i in 0...shape[0]
				unless options[:interpolate_theta]
					for j in 0...shape[1]
						factors[i,j] = values[i+1][j]
					end
				else
					opts = options.dup
					opts[:interpolate_theta] = nil
					theta_vec_short = gsl_vector(:theta, {})
					p 'sizes', [theta_vec_short.size, values[i+1].to_gslv.size]
					interp = GSL::ScatterInterp.alloc(:linear, [theta_vec_short, values[i+1].to_gslv], true)
					for j in 0...shape[1]
						factors[i,j] = interp.eval(theta_vec[j])
					end
				end
		end
		#ep factors
		return factors
	end
end

#moment_gsl_tensor(options) ⇒ Object



136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
# File 'lib/gs2crmod/gsl_data_3d.rb', line 136

def moment_gsl_tensor(options)
	if options[:t_index]
		raise ArgumentError.new("Moments are not written out as a function of time currently")
		#ep options; gets
		raise CRFatal.new("write_phi_over_time is not enabled so this function won't work") unless @write_phi_over_time
		arr =  GSL::Tensor.new(netcdf_file.var(options[:field_name].to_s + '_t').get({'start' => [0,(options[:thetamin]||0),0,0, options[:t_index] - 1], 'end' => [-1,(options[:thetamax]||-1),(options[:nakx]||0)-1,(options[:naky]||0)-1, options[:t_index] - 1]}))
		#ep 'arr.shape', arr.shape
		arr.reshape!(*arr.shape.slice(1...arr.shape.size))
		
	else
		arr =  GSL::Tensor.new(netcdf_file.var(options[:moment_name]).get({'start' => [0,(options[:thetamin]||0),0,0,options[:species_element]], 'end' => [-1,(options[:thetamax]||-1),(options[:nakx]||0)-1,(options[:naky]||0)-1,options[:species_element]]}))
		#ep 'arr.shape', arr.shape
	end
	arr.reshape!(*arr.shape.slice(1...arr.shape.size))
	arr[0, true, true, true] = 0.0 if options[:no_zonal]
	#arr = arr[options[:nakx] ? 0...options[:nakx] : true, options[:naky] ? 0...options[:naky] : true, true, true] if options[:nakx] or options[:naky]
	return arr

end

#phi_real_space_gsl_tensor(options) ⇒ Object

Returns a rank 3 tensor which is the real potential (i.e. Fourier transformed from the GS2 output) as a function of the y index, the x index and the theta index.



175
176
177
# File 'lib/gs2crmod/gsl_data_3d.rb', line 175

def phi_real_space_gsl_tensor(options)
	return field_real_space_gsl_tensor(options.absorb(field_name: :phi))
end