Module: NumRu::GPhys::Derivative

Defined in:
lib/numru/gphys/derivative.rb

Constant Summary collapse

CYCLIC_OR_LINEAR =
-1
LINEAR_EXT =

1

NumRu::Derivative::LINEAR_EXT
CYCLIC_EXT =

2

NumRu::Derivative::CYCLIC_EXT
MIRROR_A =

3

NumRu::Derivative::MIRROR_A
MIRROR_B =

3

NumRu::Derivative::MIRROR_B

Class Method Summary collapse

Class Method Details

.__deriv(gp, dim, bc, altcoord, dtype = "cderiv", deriv_order = 1) ⇒ Object



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
# File 'lib/numru/gphys/derivative.rb', line 123

def __deriv(gp,dim,bc, altcoord, dtype="cderiv", deriv_order=1)
  if bc == CYCLIC_OR_LINEAR
    bc = ( gp.axis(dim).cyclic_extendible? ? CYCLIC_EXT : LINEAR_EXT )
  end

	# <<get dimention number>>
	if (dim.is_a?(Numeric) && dim < 0)
	  dim += gp.rank
	elsif dim.is_a?(String)
	  dim = gp.axnames.index(dim) 
	end

	# <<derivate gp>>
	v_data = gp.data                   # get varray
  if altcoord
    v_x = altcoord
  else
    v_x = gp.coord(dim)
  end
	n_data = v_data.val                # get narray
  n_x = v_x.val
  case dtype
  when "cderiv"
    n_dgpdx = NumRu::Derivative::cderiv(n_data,n_x,dim,bc)
  when "threepoint_O2nd_deriv"
    n_dgpdx = NumRu::Derivative::threepoint_O2nd_deriv(n_data,n_x,dim,bc) 
  when "deriv2nd"
    n_dgpdx = NumRu::Derivative::deriv2nd(n_data,n_x,dim,bc)
  else
    raise ArgumentError, "Unsupported derivative type :'#{dtype}'"
  end
  case deriv_order
  when 1
    name = "d#{gp.name}_d#{v_x.name}"                      # ex. "dT_dx"
  when 2
    name = "d2#{gp.name}_d#{v_x.name}2"                   # ex. "d2T_dx2"
  end
 	v_dgpdx = VArray.new(n_dgpdx, gp.data, name)           # make varray 
	g_dgpdx = GPhys.new( gp.grid_copy, v_dgpdx )           # make gphys 

  # <<set attribute>>
	u_data = v_data.units
  u_x = v_x.units
  case deriv_order
  when 1
    g_dgpdx.units = u_data/u_x                             # set units
    if v_data.get_att('long_name') && v_x.get_att('long_name')
      long_name = "d_#{v_data.get_att('long_name')}_d_#{v_x.get_att('long_name')}"
    else
      long_name = name
    end
  when 2
    g_dgpdx.units = u_data/u_x**2                         # set units
    if v_data.get_att('long_name') && v_x.get_att('long_name')
      long_name = "d2(#{v_data.get_att('long_name')})_d(#{v_x.get_att('long_name')})2"
    else
      long_name = name
    end
  end
	g_dgpdx.data.set_att("long_name",long_name)            # set long_name
	return g_dgpdx
end

.cderiv(gp, dim, bc = CYCLIC_OR_LINEAR, altcoord = nil) ⇒ Object



115
116
117
# File 'lib/numru/gphys/derivative.rb', line 115

def cderiv(gp, dim, bc=CYCLIC_OR_LINEAR, altcoord=nil)
  __deriv(gp, dim, bc, altcoord, "cderiv",1)
end

.deriv2nd(gp, dim, bc = CYCLIC_OR_LINEAR, altcoord = nil) ⇒ Object



119
120
121
# File 'lib/numru/gphys/derivative.rb', line 119

def deriv2nd(gp, dim, bc=CYCLIC_OR_LINEAR, altcoord=nil)
  __deriv(gp, dim, bc, altcoord, "deriv2nd",2)
end

.threepoint_O2nd_deriv(gp, dim, bc = CYCLIC_OR_LINEAR, altcoord = nil) ⇒ Object



111
112
113
# File 'lib/numru/gphys/derivative.rb', line 111

def threepoint_O2nd_deriv(gp, dim, bc=CYCLIC_OR_LINEAR, altcoord=nil)
  __deriv(gp, dim, bc, altcoord, "threepoint_O2nd_deriv",1)
end