forked from ESCOMP/CLUBB_CESM
-
Notifications
You must be signed in to change notification settings - Fork 0
/
calc_roots.F90
310 lines (246 loc) · 10.4 KB
/
calc_roots.F90
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
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
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
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
309
310
!---------------------------------------------------------------------------
! $Id$
!===============================================================================
module calc_roots
implicit none
public :: cubic_solve, &
quadratic_solve, &
cube_root
private ! Set Default Scope
contains
!=============================================================================
pure function cubic_solve( nz, a_coef, b_coef, c_coef, d_coef ) &
result( roots )
! Description:
! Solve for the roots of x in a cubic equation.
!
! The cubic equation has the form:
!
! f(x) = a*x^3 + b*x^2 + c*x + d;
!
! where a /= 0. When f(x) = 0, the cubic formula is used to solve:
!
! a*x^3 + b*x^2 + c*x + d = 0.
!
! The cubic formula is also called Cardano's Formula.
!
! The three solutions for x are:
!
! x(1) = -(1/3)*(b/a) + ( S + T );
! x(2) = -(1/3)*(b/a) - (1/2) * ( S + T ) + (1/2)i * sqrt(3) * ( S - T );
! x(3) = -(1/3)*(b/a) - (1/2) * ( S + T ) - (1/2)i * sqrt(3) * ( S - T );
!
! where:
!
! S = ( R + sqrt( D ) )^(1/3); and
! T = ( R - sqrt( D ) )^(1/3).
!
! The determinant, D, is given by:
!
! D = R^2 + Q^3.
!
! The values of R and Q relate back to the a, b, c, and d coefficients:
!
! Q = ( 3*(c/a) - (b/a)^2 ) / 9; and
! R = ( 9*(b/a)*(c/a) - 27*(d/a) - 2*(b/a)^3 ) / 54.
!
! When D < 0, there are three unique, real-valued roots. When D = 0, there
! are three real-valued roots, but one root is a double root or a triple
! root. When D > 0, there is one real-valued root and there are two roots
! that are complex conjugates.
! References:
! http://mathworld.wolfram.com/CubicFormula.html
!-----------------------------------------------------------------------
use constants_clubb, only: &
three, & ! Constant(s)
two, &
one_half, &
one_third, &
zero
use clubb_precision, only: &
core_rknd ! Variable(s)
implicit none
! Input Variables
integer, intent(in) :: &
nz ! Number of vertical levels
real( kind = core_rknd ), dimension(nz), intent(in) :: &
a_coef, & ! Coefficient a (of x^3) in a*x^3 + b*x^2 + c^x + d = 0 [-]
b_coef, & ! Coefficient b (of x^2) in a*x^3 + b*x^2 + c^x + d = 0 [-]
c_coef, & ! Coefficient c (of x) in a*x^3 + b*x^2 + c^x + d = 0 [-]
d_coef ! Coefficient d in a*x^3 + b*x^2 + c^x + d = 0 [-]
! Return Variables
complex( kind = core_rknd ), dimension(nz,3) :: &
roots ! Roots of x that satisfy a*x^3 + b*x^2 + c*x + d = 0 [-]
! Local Variables
real( kind = core_rknd ), dimension(nz) :: &
cap_Q_coef, & ! Coefficient Q in cubic formula [-]
cap_R_coef, & ! Coefficient R in cubic formula [-]
determinant ! Determinant D in cubic formula [-]
complex( kind = core_rknd ), dimension(nz) :: &
sqrt_det, & ! Square root of determinant D in cubic formula [-]
cap_S_coef, & ! Coefficient S in cubic formula [-]
cap_T_coef ! Coefficient T in cubic formula [-]
complex( kind = core_rknd ), parameter :: &
i_cmplx = ( 0.0_core_rknd, 1.0_core_rknd ) ! i = sqrt(-1)
complex( kind = core_rknd ) :: &
sqrt_3, & ! Sqrt 3 (complex data type)
one_half_cmplx, & ! 1/2 (complex data type)
one_third_cmplx ! 1/3 (complex data type)
! Declare some constants as complex data types in order to prevent
! data-type conversion warning messages.
sqrt_3 = cmplx( sqrt( three ), kind = core_rknd )
one_half_cmplx = cmplx( one_half, kind = core_rknd )
one_third_cmplx = cmplx( one_third, kind = core_rknd )
! Find the value of the coefficient Q; where
! Q = ( 3*(c/a) - (b/a)^2 ) / 9.
cap_Q_coef = ( three * (c_coef/a_coef) - (b_coef/a_coef)**2 ) &
/ 9.0_core_rknd
! Find the value of the coefficient R; where
! R = ( 9*(b/a)*(c/a) - 27*(d/a) - 2*(b/a)^3 ) / 54.
cap_R_coef = ( 9.0_core_rknd * (b_coef/a_coef) * (c_coef/a_coef) &
- 27.0_core_rknd * (d_coef/a_coef) &
- two * (b_coef/a_coef)**3 ) / 54.0_core_rknd
! Find the value of the determinant D; where
! D = R^2 + Q^3.
determinant = cap_Q_coef**3 + cap_R_coef**2
! Calculate the square root of the determinant. This will be a complex
! number.
sqrt_det = sqrt( cmplx( determinant, kind = core_rknd ) )
! Find the value of the coefficient S; where
! S = ( R + sqrt( D ) )^(1/3).
cap_S_coef &
= ( cmplx( cap_R_coef, kind = core_rknd ) + sqrt_det )**one_third_cmplx
! Find the value of the coefficient T; where
! T = ( R - sqrt( D ) )^(1/3).
cap_T_coef &
= ( cmplx( cap_R_coef, kind = core_rknd ) - sqrt_det )**one_third_cmplx
! Find the values of the roots.
! This root is always real-valued.
! x(1) = -(1/3)*(b/a) + ( S + T ).
roots(:,1) &
= - one_third_cmplx * cmplx( b_coef/a_coef, kind = core_rknd ) &
+ ( cap_S_coef + cap_T_coef )
! This root is real-valued when D < 0 (even though the square root of the
! determinant is a complex number), as well as when D = 0 (when it is part
! of a double or triple root). When D > 0, this root is a complex number.
! It is the complex conjugate of roots(3).
! x(2) = -(1/3)*(b/a) - (1/2) * ( S + T ) + (1/2)i * sqrt(3) * ( S - T ).
roots(:,2) &
= - one_third_cmplx * cmplx( b_coef/a_coef, kind = core_rknd ) &
- one_half_cmplx * ( cap_S_coef + cap_T_coef ) &
+ one_half_cmplx * i_cmplx * sqrt_3 * ( cap_S_coef - cap_T_coef )
! This root is real-valued when D < 0 (even though the square root of the
! determinant is a complex number), as well as when D = 0 (when it is part
! of a double or triple root). When D > 0, this root is a complex number.
! It is the complex conjugate of roots(2).
! x(3) = -(1/3)*(b/a) - (1/2) * ( S + T ) - (1/2)i * sqrt(3) * ( S - T ).
roots(:,3) &
= - one_third_cmplx * cmplx( b_coef/a_coef, kind = core_rknd ) &
- one_half_cmplx * ( cap_S_coef + cap_T_coef ) &
- one_half_cmplx * i_cmplx * sqrt_3 * ( cap_S_coef - cap_T_coef )
return
end function cubic_solve
!=============================================================================
pure function quadratic_solve( nz, a_coef, b_coef, c_coef ) &
result( roots )
! Description:
! Solve for the roots of x in a quadratic equation.
!
! The equation has the form:
!
! f(x) = a*x^2 + b*x + c;
!
! where a /= 0. When f(x) = 0, the quadratic formula is used to solve:
!
! a*x^2 + b*x + c = 0.
!
! The two solutions for x are:
!
! x(1) = ( -b + sqrt( b^2 - 4*a*c ) ) / (2*a); and
! x(2) = ( -b - sqrt( b^2 - 4*a*c ) ) / (2*a).
!
! The determinant, D, is given by:
!
! D = b^2 - 4*a*c.
!
! When D > 0, there are two unique, real-valued roots. When D = 0, there
! are two real-valued roots, but they are a double root. When D < 0, there
! there are two roots that are complex conjugates.
! References:
!-----------------------------------------------------------------------
use constants_clubb, only: &
four, & ! Constant(s)
two, &
zero
use clubb_precision, only: &
core_rknd ! Variable(s)
implicit none
! Input Variables
integer, intent(in) :: &
nz ! Number of vertical levels
real( kind = core_rknd ), dimension(nz), intent(in) :: &
a_coef, & ! Coefficient a (of x^2) in a*x^2 + b*x + c = 0 [-]
b_coef, & ! Coefficient b (of x) in a*x^2 + b*x + c = 0 [-]
c_coef ! Coefficient c in a*x^2 + b*x + c = 0 [-]
! Return Variables
complex( kind = core_rknd ), dimension(nz,2) :: &
roots ! Roots of x that satisfy a*x^2 + b*x + c = 0 [-]
! Local Variables
real( kind = core_rknd ), dimension(nz) :: &
determinant ! Determinant D in quadratic formula [-]
complex( kind = core_rknd ), dimension(nz) :: &
sqrt_det ! Square root of determinant D in quadratic formula [-]
! Find the value of the determinant D; where
! D = b^2 - 4*a*c.
determinant = b_coef**2 - four * a_coef * c_coef
! Calculate the square root of the determinant. This will be a complex
! number.
sqrt_det = sqrt( cmplx( determinant, kind = core_rknd ) )
! Find the values of the roots.
! This root is real-valued when D > 0, as well as when D = 0 (when it is
! part of a double root). When D < 0, this root is a complex number. It is
! the complex conjugate of roots(2).
! x(1) = ( -b + sqrt( b^2 - 4*a*c ) ) / (2*a); and
roots(:,1) = ( -cmplx( b_coef, kind = core_rknd ) + sqrt_det ) &
/ cmplx( two * a_coef, kind = core_rknd )
! This root is real-valued when D > 0, as well as when D = 0 (when it is
! part of a double root). When D < 0, this root is a complex number. It is
! the complex conjugate of roots(1).
! x(2) = ( -b - sqrt( b^2 - 4*a*c ) ) / (2*a).
roots(:,2) = ( -cmplx( b_coef, kind = core_rknd ) - sqrt_det ) &
/ cmplx( two * a_coef, kind = core_rknd )
return
end function quadratic_solve
!=============================================================================
pure function cube_root( x )
! Description:
! Calculates the cube root of x.
!
! When x >= 0, this code simply calculates x^(1/3). When x < 0, this code
! uses x^(1/3) = -|x|^(1/3). This eliminates numerical errors when the
! exponent of 1/3 is not treated as exactly 1/3, which would sometimes
! result in values of NaN.
!
! References:
!-----------------------------------------------------------------------
use constants_clubb, only: &
one_third, & ! Constant(s)
zero
use clubb_precision, only: &
core_rknd ! Variable(s)
implicit none
! Input Variables
real( kind = core_rknd ), intent(in) :: &
x ! Variable x
! Return Variables
real( kind = core_rknd ) :: &
cube_root ! Cube root of x
if ( x >= zero ) then
cube_root = x**one_third
else ! x < 0
cube_root = -abs(x)**one_third
endif ! x >= 0
return
end function cube_root
!===============================================================================
end module calc_roots