Class: TokFile::Eqdsk

Inherits:
Object
  • Object
show all
Defined in:
lib/tokfile/eqdsk.rb

Constant Summary collapse

DATANAMES =

attr_accessor :rboxlen, :zboxlen, :r0exp, :rboxlft

[
	:rboxlen, :zboxlen, :r0exp, :rboxlft, :dummy,
	:raxis, :zaxis, :psiaxis, :dummy, :b0exp,
	:current, :dummy, :dummy, :dummy, :dummy,
	:dummy, :dummy, :dummy, :dummy, :dummy,
	:t, 
	:pr, 
	:ttprime, 
	:pprime, 
	:psi, 
	:q, 
	:nbound, :nlimiter, 
	:bound,
  :limiter
]
FMT =
"%16.9e" * 5 + "\n"

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(file) ⇒ Eqdsk

Returns a new instance of Eqdsk.



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
# File 'lib/tokfile/eqdsk.rb', line 48

def initialize(file)

	iline = 0
	counter = 0

	#filehandle = File.open(file, 'r')

	lines = File.read(file).split("\n")
	#@datastarts = DATANAMES.inject({}){|h, name| h[name] = nil; h}

	#line1 = filehandle.gets
	#@nrbox, @nzbox  = filehandle.gets.split(/\s+/).slice(-2..-1).map{|s| eval(s)}
	@nrbox, @nzbox  = lines[0].split(/\s+/).slice(-2..-1).map{|s| eval(s)}
	#@rboxlen, @zboxlen, @r0exp, @rboxlft, dummy = read(filehandle.gets)
	#@raxis, @zaxis, @psiaxis, dummy, @b0exp = read(filehandle.gets)
	#@current, dummy, dummy, dummy, dummy = read(filehandle.gets)
	#dummy, dummy, dummy, dummy, dummy = read(filehandle.gets)
	array = []
	i = 1
	vb2 = TokFile.verbosity > 1
	
	if vb2
		eputs
		eputs "Reading data from eqdsk file #{file}."
		eputs
	end
	total_size = 0
	DATANAMES.each do |name|
		sz = size(name)
		total_size += sz
		#ep ['name', name, 'size', sz]
		if vb2
			Terminal.erewind(1)
			eputs "#{name}(#{sz})#{Terminal::CLEAR_LINE}" if vb2
		end
		if array.size < sz
			#array += read(filehandle.gets)
			#array += read(lines[i])
			#i+=1
       begin
         array += read(lines.slice(i...(i+=(sz.to_f/5.0).ceil)).join(' '))
       rescue NoMethodError
         $stderr.puts "Finished reading at #{name}"
         break
       end
			#array += lines.slice(i...(i+=(sz.to_f/5.0).ceil)).join(' ')

			#filehandle.gets.scanf("%e"){|scan| array.push scan[0]}
		end
		if array.size == sz
			data = array
			array = []
		else
		  data = []
			while data.size < sz
				data.push array.shift
			end
		end
		self.class.attr_accessor name
		case name
		when :psi
			set(name, GSL::Matrix.alloc(*data.pieces(@nzbox)).transpose)
		when :bound
			data = data.pieces(@nbound).transpose
			set(:rbound, data[0].to_gslv)
			set(:zbound, data[1].to_gslv)
		when :limiter
			data = data.pieces(@nlimiter).transpose
			set(:rlimiter, data[0].to_gslv)
			set(:zlimiter, data[1].to_gslv)
		else
			case sz
			when 1
         #p [name, 'yy', data]
				set(name, data[0])
			else
         #p [name,'xx', data.inspect[0..2000]]
         #p [name,data.to_gslv.inspect[0..10]]
				set(name, data.to_gslv)
			end
		end
	end
	@r = GSL::Vector.linspace(@rboxlft, @rboxlft+@rboxlen, @nrbox)
	@z = GSL::Vector.linspace(-@zboxlen/2.0, @zboxlen/2.0, @nzbox)

	if vb2
	  Terminal.erewind(1)
		eputs  "Read total data size of #{total_size.to_f * 8.0/1.0e6} MB"
	end

	if TokFile.verbosity > 0
	eputs <<EOF
--------------------------------------
              Tokfile
--------------------------------------
Successfully read an eqdsk file called
#{file}
with the following parameters:

nrbox = #@nrbox
nzbox = #@nzbox
nbound = #@nbound
raxis = #@raxis
--------------------------------------
EOF

	end







	#@lines = File.read(file).split("\n").map{|str| str.sub(/\A\s+/, '').sub(/\s+\Z/, '')}
	#@nrbox, @nzbox  = @lines[0].split(/\s+/).slice(-2..-1).map{|s| eval(s)}
	#in_data = true; i = 1
	#@atoms = []
	#@lines_columns = []
	#while in_data
		#@lines_columns[i] = @lines[i].split(/\s+|(?<=\d)[+-]/).map{|s| eval(s)}
		#@atoms += @lines_columns[i]
		#if @nbound = @atoms[start(:nbound)] 
			#if @atoms.size > start(:limiter) 
				#in_data = false
			#end
		#end
		#i+=1
	#end

	#[:t, :p, :pprime, :ttprime, :psi, :q, :rzbound].each do |var|
		#attr_accessor var
		#st = start(var)
		#set(var, @atoms.slice(st...(st+@nrbox))).to_gslv
	#end


	#@rboxlen, @zboxlen, @r0exp, @rboxlft, dummy = @lines_columns[1]
	#ep ['lines_columns', @lines_columns[1], @lines[1]]
	

   if not send(:nbound)
     $stderr.puts("Boundary missing: trying to reconstruct")
     reconstruct_boundary
   end
end

Instance Attribute Details

#boundObject

Returns the value of attribute bound.



20
21
22
# File 'lib/tokfile/eqdsk.rb', line 20

def bound
  @bound
end

#limiterObject

Returns the value of attribute limiter.



20
21
22
# File 'lib/tokfile/eqdsk.rb', line 20

def limiter
  @limiter
end

#nboundObject

Returns the value of attribute nbound.



20
21
22
# File 'lib/tokfile/eqdsk.rb', line 20

def nbound
  @nbound
end

#nlimiterObject

Returns the value of attribute nlimiter.



20
21
22
# File 'lib/tokfile/eqdsk.rb', line 20

def nlimiter
  @nlimiter
end

#nrboxObject

Returns the value of attribute nrbox.



3
4
5
# File 'lib/tokfile/eqdsk.rb', line 3

def nrbox
  @nrbox
end

#nzboxObject

Returns the value of attribute nzbox.



3
4
5
# File 'lib/tokfile/eqdsk.rb', line 3

def nzbox
  @nzbox
end

#rboundObject

Returns the value of attribute rbound.



20
21
22
# File 'lib/tokfile/eqdsk.rb', line 20

def rbound
  @rbound
end

#rlimiterObject

Returns the value of attribute rlimiter.



20
21
22
# File 'lib/tokfile/eqdsk.rb', line 20

def rlimiter
  @rlimiter
end

#zboundObject

Returns the value of attribute zbound.



20
21
22
# File 'lib/tokfile/eqdsk.rb', line 20

def zbound
  @zbound
end

#zlimiterObject

Returns the value of attribute zlimiter.



20
21
22
# File 'lib/tokfile/eqdsk.rb', line 20

def zlimiter
  @zlimiter
end

Instance Method Details

#convert(format) ⇒ Object



38
39
40
41
42
43
44
45
# File 'lib/tokfile/eqdsk.rb', line 38

def convert(format)
  case format
  when 'eqdsk'
    self.dup
  else
    raise "Conversion from eqdsk to #{format} not implemented yet"
  end
end

#read(line) ⇒ Object



23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/tokfile/eqdsk.rb', line 23

def read(line)
		#@lines_columns[i] = @lines[i].split(/\s+|(?<=\d)[+-]/).map{|s| eval(s)}
	line.sub(/\A\s+/, '').sub(/\s+\Z/, '').split(/\s+|(?<=\d)(?=[+-])/).map{|s| eval(s)}
	#line.scan(/.{15}/).map{|s| eval(s)}
	
	#ep ['line', line]
	#arr = []
	#res = line.scanf("%16.9E"){|n| arr.push n}
	#line.scanf("%E"){|n| arr.push n[0]}
	#ep ['res', res, arr]
	#ep ['res', arr]
	#arr

end

#reconstruct_boundaryObject



231
232
233
234
235
236
237
238
239
240
241
242
243
244
# File 'lib/tokfile/eqdsk.rb', line 231

def reconstruct_boundary
  require 'gsl_extras'
  $stderr.puts "Reconstructing boundary"
  contour = GSL::Contour.new(@r, @z, @psi)
 #psikit = GraphKit.quick_create([@r, @z, @psi])
#psikit.data[0].gp.with = 'pm3d'
#psikit.gp.view = "map"
  ck = contour.graphkit(0.0)
  #psikit+= GraphKit.quick_create([ck.data[0].x.data, ck.data[0].y.data, ck.data[0].y.data.collect{0.0}])
  #psikit.gnuplot
  @nbound = @nlimiter = ck.data[0].x.data.size
  @rbound = @rlimiter = ck.data[0].x.data
  @zbound = @zlimiter = ck.data[0].y.data
end

#size(var) ⇒ Object



267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
# File 'lib/tokfile/eqdsk.rb', line 267

def size(var)
	case var
	#when :nbound, :nlimiter
	 #1
	when :bound
	 @nbound * 2
	when :limiter
		@nlimiter * 2
	when :psi
	 @nrbox * @nzbox
	when :t, :pr, :pprime, :ttprime, :q 
	 @nrbox
	else
		1
	end
end

#summary_graphkitObject



245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
# File 'lib/tokfile/eqdsk.rb', line 245

def summary_graphkit
	psivec = GSL::Vector.linspace(@psi.min, 0.0, @nrbox)
	multkit = GraphKit::MultiWindow.new([:pr, :pprime, :t, :ttprime, :q].map{|name|
		kit = GraphKit.quick_create([psivec, send(name)])
		kit.title = name.to_s
		kit.ylabel = nil
		kit.xlabel = 'psi'
		kit
	})
  psikit = GraphKit.quick_create([@r, @z, @psi])
	psikit.data[0].gp.with = 'pm3d'
	psikit.gp.view = "map"
	boundkit = GraphKit.quick_create([@rbound, @zbound, @rbound.collect{0.0}])
	psikit += boundkit
	psikit.gp.key = "off"
	psikit.title = 'psi'
	psikit.xlabel = 'R'
	psikit.ylabel = 'Z'
	multkit.push psikit
	multkit.gp.multiplot = "layout 2,3"
	multkit
end

#write_file(filename) ⇒ Object



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
# File 'lib/tokfile/eqdsk.rb', line 195

def write_file(filename)
  File.open(filename, 'w') do |file|
    printf(file, "%48s %3d %5d %5d\n", " EQDSK GEN.BY TOKFILE" , 1, @nrbox, @nzbox)
    array = []
    DATANAMES.each do |dname|
      next if [:zlimiter, :nlimiter].include? dname
      sz = size(dname)
      data = sz==1 ? [send(dname)] : send(dname).to_a
      case dname
      when :psi
        data = data.transpose.flatten
      when :bound
        data = [send(:rbound).to_a, send(:zbound).to_a].transpose.flatten
      when :limiter
        data = [send(:rlimiter).to_a, send(:zlimiter).to_a].transpose.flatten
      end
      unless dname==:nbound
        array += data
      end
      #p [dname]
      while array.size > 5 
        out = []
        5.times{out.push(array.shift)}
        printf(file, FMT, *out)
      end
      if [:nbound, :limiter].include? dname
        # Clear buffer
        printf(file, "%16.9e"*array.size + "\n", *array)
        array = []
      end
      if dname == :nbound
        printf(file, "%5d%5d\n", data[0], send(:nlimiter))
      end
    end
  end
end