Module: CodeRunner::Gs2::GSLTensors
- Included in:
- CodeRunner::Gs2
- Defined in:
- lib/gs2crmod/gsl_data_3d.rb
Constant Summary collapse
- FIELD_VALUES =
end
[:phi, :density, :apar, :bpar]
- 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
- #apar_gsl_tensor(options) ⇒ Object
- #bpar_gsl_tensor(options) ⇒ Object
- #cartesian_coordinates_gsl_tensor(options) ⇒ Object
-
#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.
-
#correct_3d_options(options) ⇒ Object
Adjust n0, rho_star_actual and q_actual to ensure periodicity.
-
#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.
- #field_gsl_tensor(options) ⇒ Object
- #field_netcdf_name(field_name, time_varying = false) ⇒ Object
- #field_real_space_gsl_tensor(options) ⇒ Object
- #field_real_space_gsl_tensor_2(options) ⇒ Object
- #field_species_element(options) ⇒ Object
-
#geometric_factors_gsl_tensor(options) ⇒ Object
Order is R0,Z0,a0,Rprim,Zprim,aprim.
- #moment_gsl_tensor(options) ⇒ Object
-
#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.
Instance Method Details
#apar_gsl_tensor(options) ⇒ Object
373 374 375 |
# File 'lib/gs2crmod/gsl_data_3d.rb', line 373 def apar_gsl_tensor() return GSL::Tensor.new(netcdf_file.var('apar').get) end |
#bpar_gsl_tensor(options) ⇒ Object
376 377 378 |
# File 'lib/gs2crmod/gsl_data_3d.rb', line 376 def bpar_gsl_tensor() return GSL::Tensor.new(netcdf_file.var('bpar').get) end |
#cartesian_coordinates_gsl_tensor(options) ⇒ Object
702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 |
# File 'lib/gs2crmod/gsl_data_3d.rb', line 702 def cartesian_coordinates_gsl_tensor() cyl = cylindrical_coordinates_gsl_tensor() 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.
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 |
# File 'lib/gs2crmod/gsl_data_3d.rb', line 440 def constant_torphi_surface_gsl_tensor() ops = .dup IRRELEVANT_INDICES.each{|v| ops.delete(v)} return cache[[:constant_torphi_surface_gsl_tensor, ops]] if cache[[:constant_torphi_surface_gsl_tensor, ops]] () if [:torphi] torphiout = [:torphi] else raise 'Need to specify a torphi value' end cyls = cylindrical_coordinates_gsl_tensor(.absorb({extra_points: :y})) shpc = cyls.shape factors = geometric_factors_gsl_tensor() #ep shpc, 'shpc' #xsize = case shpc[2] yvec = gsl_vector('y', ) #ep yvec.to_a ; gets x = gsl_vector('x', ) dy = yvec[1] - yvec[0] torphi_const = GSL::Tensor.int(shpc[2], shpc[3]) # don't include extra x point xfac = 1.0 / [:rho_star_actual] yfac = [:rhoc_actual] / [:q_actual] / [: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 [: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
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 |
# File 'lib/gs2crmod/gsl_data_3d.rb', line 575 def () raise "Please specify options[:rho_star] or options[:n0]" unless [:rho_star] or [: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 [:rhoc_actual] =rhoc = 2 * eps / epsl else [:rhoc_actual] = rhoc = @rhoc qinp = @qinp end #eputs "Checking that rho_star and q satisfy periodicity..." rho_star_inp = [:rho_star] y = gsl_vector('y', ) ly = (y[1]-y[0]) * (y.size) n0_fac = 2.0*Math::PI * rhoc / ly n0_inp = [: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 [:q_actual] = q_actual unless [:rho_star_actual] and [:rho_star_actual] == n0_fac/n0/q_actual #eputs "Adjusting rho_star to satisfy periodicity ..." [: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
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 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 |
# File 'lib/gs2crmod/gsl_data_3d.rb', line 630 def cylindrical_coordinates_gsl_tensor() ops = .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', ) y = gsl_vector('y', ) ly = 2*Math::PI*y0#(y[1]-y[0]) * (y.size) if [true,:x].include? [:extra_points] ep "Extending x..." x = x.connect([2*x[-1] - x[-2]].to_gslv).dup end if [true,:y].include? [:extra_points] ep "Extending y..." y = y.connect([2.0*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-6 end x = x.subvector([:xmin]||0, ([:xmax]||x.size-1) - ([:xmin]||0) + 1).dup # if options[:xout] and options[:xin] y = y.subvector([:ymin]||0, ([:ymax]||y.size-1) - ([:ymin]||0) + 1).dup # if options[:yout] and options[:yin] y = y + [:ncopy] * ly if [:ncopy] theta = gsl_vector('theta', ) () rhoc = [:rhoc_actual] q_actual = [:q_actual] xfac = 1.0 / [:rho_star_actual] yfac = rhoc / q_actual / [:rho_star_actual] factors = geometric_factors_gsl_tensor() 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 if gs2f = [:gs2_coordinate_factor] rgs2 = (x[j]**2 + y[i]**2 )**0.5*(1.0 + 2.0 * Float::EPSILON) 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 case tp = [: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
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 |
# File 'lib/gs2crmod/gsl_data_3d.rb', line 180 def field_gsl_tensor() species_element = field_species_element() #ep 'species_element', species_element if [: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(field_netcdf_name([:field_name], true)).get({'start' => [0,([:thetamin]||0),0,0, species_element, [:t_index] - 1].compact, 'end' => [-1,([:thetamax]||-1),([:nakx]||0)-1,([:naky]||0)-1, species_element, [:t_index] - 1].compact})) #ep 'arr.shape', arr.shape arr.reshape!(*arr.shape.slice(1...arr.shape.size)) else arr = GSL::Tensor.new(netcdf_file.var(field_netcdf_name([:field_name])).get({'start' => [0,([:thetamin]||0),0,0, species_element].compact, 'end' => [-1,([:thetamax]||-1),([:nakx]||0)-1,([:naky]||0)-1, species_element].compact})) #ep 'arr.shape', arr.shape end if species_element arr.reshape!(*arr.shape.slice(1...arr.shape.size)) end if [:interpolate_x] shape = arr.narray.shape #p 'shape', shape shape[2] = (shape[2]-1)*[:interpolate_x] + 1 #p shape arr = GSL::Tensor.new(arr.narray.(*shape, 0.0)) end if [:interpolate_y] shape = arr.narray.shape #p 'shape', shape shape[3] = (shape[3]-1)*[:interpolate_y] + 1 #p shape arr = GSL::Tensor.new(arr.narray.(*shape, 0.0)) end if gryfx? and [:periodic] shape = arr.narray.shape shape[1]+=1 arr = GSL::Tensor.new(arr.narray.(*shape, 0.0)) shpe = arr.shape for i in 0...shpe[0] for j in 0...shpe[1] for r in 0...shpe[3] arr[i, j, -1, r] = arr[i, j, 0, r] end end end end #if options[:t_index] arr[0, true, true, true] = 0.0 if [:no_zonal] #else #arr[0, true, true] = 0.0 if options[:no_zonal] #end #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_netcdf_name(field_name, time_varying = false) ⇒ Object
155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 |
# File 'lib/gs2crmod/gsl_data_3d.rb', line 155 def field_netcdf_name(field_name, = false) #p field_name.to_s name = case field_name.to_s when /phi/ ? 'phi_t' : 'phi' when /density/ ? 'ntot_t' : 'density' when /apar/ ? 'apar_t' : 'apar' else raise "Unknown field name: #{field_name}" end #p name return name end |
#field_real_space_gsl_tensor(options) ⇒ Object
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 |
# File 'lib/gs2crmod/gsl_data_3d.rb', line 243 def field_real_space_gsl_tensor() fieldc = field_gsl_tensor_complex() 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[[:ymin]||0..[:ymax]||(shp[0]-1), [:xmin]||0..[:xmax]||(shp[1]-1), true] if kint = [: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
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 331 332 333 334 335 336 337 338 339 340 341 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 |
# File 'lib/gs2crmod/gsl_data_3d.rb', line 303 def field_real_space_gsl_tensor_2() field = field_gsl_tensor() 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[[:ymin]||0..[:ymax]||(shp[0]-1), [:xmin]||0..[:xmax]||(shp[1]-1), true] #p 'test2', field_real_space[0,2,3] if kint = [: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 |
#field_species_element(options) ⇒ Object
170 171 172 173 174 175 176 177 178 179 |
# File 'lib/gs2crmod/gsl_data_3d.rb', line 170 def field_species_element() case [:field_name].to_s when /density/ .convert_to_index(self, :species) #ep 'options', options [:species_index] - 1 else nil end end |
#geometric_factors_gsl_tensor(options) ⇒ Object
Order is R0,Z0,a0,Rprim,Zprim,aprim
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 411 412 413 414 |
# File 'lib/gs2crmod/gsl_data_3d.rb', line 380 def geometric_factors_gsl_tensor() #ep 'STARTING' #ops = options.dup; ops.delete :phi #ep ops; gets case @equilibrium_option when "s-alpha" return geometric_factors_salpha_gsl_tensor() else theta_vec = gsl_vector(:theta, ) factors = GSL::Tensor.alloc(6,theta_vec.size) values = File.read([:geometry_file]||"#@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[0] #ep values[3] shape = factors.shape for i in 0...shape[0] unless [:interpolate_theta] for j in 0...shape[1] factors[i,j] = values[i+1][j] end else opts = .dup opts[:interpolate_theta] = nil theta_vec_short = gsl_vector(:theta, {}) interp = GSL::ScatterInterp.alloc(:thin_plate_splines, [values[0], values[i+1].to_gslv], false) for j in 0...theta_vec.size 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() if [: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([:field_name].to_s + '_t').get({'start' => [0,([:thetamin]||0),0,0, [:t_index] - 1], 'end' => [-1,([:thetamax]||-1),([:nakx]||0)-1,([:naky]||0)-1, [: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([:moment_name]).get({'start' => [0,([:thetamin]||0),0,0,[:species_element]], 'end' => [-1,([:thetamax]||-1),([:nakx]||0)-1,([:naky]||0)-1,[: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 [: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.
239 240 241 |
# File 'lib/gs2crmod/gsl_data_3d.rb', line 239 def phi_real_space_gsl_tensor() return field_real_space_gsl_tensor(.absorb(field_name: :phi)) end |