Class: NMatrix::IO::HarwellBoeing::Reader

Inherits:
Object
  • Object
show all
Defined in:
lib/nmatrix/io/harwell_boeing.rb

Instance Method Summary collapse

Constructor Details

#initialize(file_name) ⇒ Reader

Returns a new instance of Reader

Raises:

  • (IOError)


80
81
82
83
84
85
86
87
# File 'lib/nmatrix/io/harwell_boeing.rb', line 80

def initialize file_name
  raise(IOError, "Unsupported file format. Specify file as \
    file_name.rua.") if !file_name.match(/.*\.[rR][uU][aA]/)

  @file_name   = file_name
  @header      = {}
  @body        = nil
end

Instance Method Details

#headerObject

Raises:

  • (IOError)


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
# File 'lib/nmatrix/io/harwell_boeing.rb', line 89

def header
  return @header if !@header.empty?
  @file = File.open @file_name, "r"

  line = @file.gets

  @header[:title] = line[0...72].strip
  @header[:key]   = line[72...80].strip

  line = @file.gets

  @header[:totcrd] = line[0...14] .strip.to_i
  @header[:ptrcrd] = line[14...28].strip.to_i
  @header[:indcrd] = line[28...42].strip.to_i
  @header[:valcrd] = line[42...56].strip.to_i
  @header[:rhscrd] = line[56...70].strip.to_i

  raise(IOError, "Right hand sides not supported.") \
   if @header[:rhscrd] > 0

  line = @file.gets

  @header[:mxtype] = line[0...3]

  raise(IOError, "Currently supports only real, assembled, unsymmetric \
    matrices.") if !@header[:mxtype].match(/RUA/)

  @header[:nrow]   = line[13...28].strip.to_i
  @header[:ncol]   = line[28...42].strip.to_i
  @header[:nnzero] = line[42...56].strip.to_i
  @header[:neltvl] = line[56...70].strip.to_i

  line = @file.gets

  fortran_reader = NMatrix::IO::FortranFormat::Reader

  @header[:ptrfmt] = fortran_reader.new(line[0...16].strip) .parse
  @header[:indfmt] = fortran_reader.new(line[16...32].strip).parse
  @header[:valfmt] = fortran_reader.new(line[32...52].strip).parse
  @header[:rhsfmt] = fortran_reader.new(line[52...72].strip).parse

  @header
end

#valuesObject



133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
# File 'lib/nmatrix/io/harwell_boeing.rb', line 133

def values
  @header      = header if @header.empty?
  @file.lineno = 5      if @file.lineno != 5
  @matrix      = NMatrix.new([ @header[:nrow], @header[:ncol] ], 
                              0, dtype: :float64)

  read_column_pointers
  read_row_indices
  read_values

  @file.close
  
  assemble_matrix

  @matrix
end