Skip to content

Commit 061c3c7

Browse files
committed
change stop to mesa_error, real to real(dp) in colors
1 parent 8a7ba60 commit 061c3c7

5 files changed

Lines changed: 75 additions & 72 deletions

File tree

colors/private/colors_utils.f90

Lines changed: 24 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
module colors_utils
2121
use const_def, only: dp, strlen, mesa_dir
2222
use colors_def, only: Colors_General_Info
23+
use utils_lib, only: mesa_error
2324

2425
implicit none
2526

@@ -39,7 +40,7 @@ subroutine dilute_flux(surface_flux, R, d, calibrated_flux)
3940
! Check that the output array has the same size as the input
4041
if (size(calibrated_flux) /= size(surface_flux)) then
4142
print *, "Error in dilute_flux: Output array must have the same size as input array."
42-
stop 1
43+
call mesa_error(__FILE__, __LINE__)
4344
end if
4445

4546
! Apply the dilution factor (R/d)^2 to each element
@@ -59,25 +60,25 @@ subroutine trapezoidal_integration(x, y, result)
5960
real(dp), intent(out) :: result
6061

6162
integer :: i, n
62-
REAL :: sum
63+
real(dp) :: sum
6364

6465
n = size(x)
65-
sum = 0.0
66+
sum = 0.0_dp
6667

6768
! Validate input sizes
6869
if (size(x) /= size(y)) then
6970
print *, "Error: x and y arrays must have the same size."
70-
stop
71+
call mesa_error(__FILE__, __LINE__)
7172
end if
7273

7374
if (size(x) < 2) then
7475
print *, "Error: x and y arrays must have at least 2 elements."
75-
stop
76+
call mesa_error(__FILE__, __LINE__)
7677
end if
7778

7879
! Perform trapezoidal integration
7980
do i = 1, n - 1
80-
sum = sum + 0.5*(x(i + 1) - x(i))*(y(i + 1) + y(i))
81+
sum = sum + 0.5_dp*(x(i + 1) - x(i))*(y(i + 1) + y(i))
8182
end do
8283

8384
result = sum
@@ -96,15 +97,15 @@ subroutine simpson_integration(x, y, result)
9697
! Validate input sizes
9798
if (size(x) /= size(y)) then
9899
print *, "Error: x and y arrays must have the same size."
99-
stop
100+
call mesa_error(__FILE__, __LINE__)
100101
end if
101102

102103
if (size(x) < 2) then
103104
print *, "Error: x and y arrays must have at least 2 elements."
104-
stop
105+
call mesa_error(__FILE__, __LINE__)
105106
end if
106107

107-
! Perform adaptive Simpsons rule
108+
! Perform adaptive Simpson's rule
108109
do i = 1, n - 2, 2
109110
h1 = x(i + 1) - x(i) ! Step size for first interval
110111
h2 = x(i + 2) - x(i + 1) ! Step size for second interval
@@ -139,12 +140,12 @@ subroutine romberg_integration(x, y, result)
139140
! Validate input sizes
140141
if (size(x) /= size(y)) then
141142
print *, "Error: x and y arrays must have the same size."
142-
stop
143+
call mesa_error(__FILE__, __LINE__)
143144
end if
144145

145146
if (n < 2) then
146147
print *, "Error: x and y arrays must have at least 2 elements."
147-
stop
148+
call mesa_error(__FILE__, __LINE__)
148149
end if
149150

150151
allocate (R(m))
@@ -192,14 +193,14 @@ subroutine load_vega_sed(filepath, wavelengths, flux)
192193
open (unit, file=trim(filepath), status='OLD', action='READ', iostat=status)
193194
if (status /= 0) then
194195
print *, "Error: Could not open Vega SED file ", trim(filepath)
195-
stop
196+
call mesa_error(__FILE__, __LINE__)
196197
end if
197198

198199
! Skip header line
199200
read (unit, '(A)', iostat=status) line
200201
if (status /= 0) then
201202
print *, "Error: Could not read header from Vega SED file ", trim(filepath)
202-
stop
203+
call mesa_error(__FILE__, __LINE__)
203204
end if
204205

205206
! Count the number of data lines
@@ -237,21 +238,21 @@ subroutine load_filter(directory, filter_wavelengths, filter_trans)
237238

238239
character(len=512) :: line
239240
integer :: unit, n_rows, status, i
240-
REAL :: temp_wavelength, temp_trans
241+
real(dp) :: temp_wavelength, temp_trans
241242

242243
! Open the file
243244
unit = 20
244245
open (unit, file=trim(directory), status='OLD', action='READ', iostat=status)
245246
if (status /= 0) then
246247
print *, "Error: Could not open file ", trim(directory)
247-
stop
248+
call mesa_error(__FILE__, __LINE__)
248249
end if
249250

250251
! Skip header line
251252
read (unit, '(A)', iostat=status) line
252253
if (status /= 0) then
253254
print *, "Error: Could not read the file", trim(directory)
254-
stop
255+
call mesa_error(__FILE__, __LINE__)
255256
end if
256257

257258
! Count rows in the file
@@ -272,7 +273,7 @@ subroutine load_filter(directory, filter_wavelengths, filter_trans)
272273
read (unit, '(A)', iostat=status) line
273274
if (status /= 0) then
274275
print *, "Error: Could not rewind file", trim(directory)
275-
stop
276+
call mesa_error(__FILE__, __LINE__)
276277
end if
277278
if (line(1:1) /= "#") exit
278279
end do
@@ -313,14 +314,14 @@ subroutine load_lookup_table(lookup_file, lookup_table, out_file_names, &
313314
open (unit, file=lookup_file, status='old', action='read', iostat=status)
314315
if (status /= 0) then
315316
print *, "Error: Could not open file", lookup_file
316-
stop
317+
call mesa_error(__FILE__, __LINE__)
317318
end if
318319

319320
! Read header line
320321
read (unit, '(A)', iostat=status) line
321322
if (status /= 0) then
322323
print *, "Error: Could not read header line"
323-
stop
324+
call mesa_error(__FILE__, __LINE__)
324325
end if
325326

326327
call split_line(line, delimiter, headers)
@@ -474,15 +475,15 @@ subroutine load_sed(directory, index, wavelengths, flux)
474475
open (unit, file=trim(directory), status='OLD', action='READ', iostat=status)
475476
if (status /= 0) then
476477
print *, "Error: Could not open file ", trim(directory)
477-
stop
478+
call mesa_error(__FILE__, __LINE__)
478479
end if
479480

480481
! Skip header lines
481482
do
482483
read (unit, '(A)', iostat=status) line
483484
if (status /= 0) then
484485
print *, "Error: Could not read the file", trim(directory)
485-
stop
486+
call mesa_error(__FILE__, __LINE__)
486487
end if
487488
if (line(1:1) /= "#") exit
488489
end do
@@ -505,7 +506,7 @@ subroutine load_sed(directory, index, wavelengths, flux)
505506
read (unit, '(A)', iostat=status) line
506507
if (status /= 0) then
507508
print *, "Error: Could not rewind file", trim(directory)
508-
stop
509+
call mesa_error(__FILE__, __LINE__)
509510
end if
510511
if (line(1:1) /= "#") exit
511512
end do
@@ -584,7 +585,7 @@ subroutine read_strings_from_file(colors_settings, strings, n, ierr)
584585
if (status /= 0) then
585586
ierr = -1
586587
print *, "Error: Could not open file", filename
587-
stop
588+
call mesa_error(__FILE__, __LINE__)
588589
end if
589590

590591
do

colors/private/knn_interp.f90

Lines changed: 22 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
module knn_interp
2525
use const_def, only: dp
2626
use colors_utils, only: dilute_flux, load_sed
27+
use utils_lib, only: mesa_error
2728
implicit none
2829

2930
private
@@ -80,8 +81,8 @@ subroutine construct_sed_knn(teff, log_g, metallicity, R, d, file_names, &
8081
distances(i) = sqrt((lu_teff(closest_indices(i)) - teff)**2 + &
8182
(lu_logg(closest_indices(i)) - log_g)**2 + &
8283
(lu_meta(closest_indices(i)) - metallicity)**2)
83-
if (distances(i) == 0.0) distances(i) = 1.0d-10 ! Prevent division by zero
84-
weights(i) = 1.0/distances(i)
84+
if (distances(i) == 0.0_dp) distances(i) = 1.0d-10 ! Prevent division by zero
85+
weights(i) = 1.0_dp/distances(i)
8586
end do
8687

8788
! Normalize weights
@@ -91,7 +92,7 @@ subroutine construct_sed_knn(teff, log_g, metallicity, R, d, file_names, &
9192
! Allocate output arrays
9293
allocate (wavelengths(n_points), fluxes(n_points))
9394
wavelengths = common_wavelengths
94-
fluxes = 0.0
95+
fluxes = 0.0_dp
9596

9697
! Perform weighted combination of the model fluxes (still at the stellar surface)
9798
do i = 1, 4
@@ -139,15 +140,15 @@ subroutine get_closest_stellar_models(teff, log_g, metallicity, lu_teff, &
139140
! Allocate and scale lookup table values
140141
allocate (scaled_lu_teff(n), scaled_lu_logg(n), scaled_lu_meta(n))
141142

142-
if (teff_max - teff_min > 0.00) then
143+
if (teff_max - teff_min > 0.0_dp) then
143144
scaled_lu_teff = (lu_teff - teff_min)/(teff_max - teff_min)
144145
end if
145146

146-
if (logg_max - logg_min > 0.00) then
147+
if (logg_max - logg_min > 0.0_dp) then
147148
scaled_lu_logg = (lu_logg - logg_min)/(logg_max - logg_min)
148149
end if
149150

150-
if (meta_max - meta_min > 0.00) then
151+
if (meta_max - meta_min > 0.0_dp) then
151152
scaled_lu_meta = (lu_meta - meta_min)/(meta_max - meta_min)
152153
end if
153154

@@ -156,38 +157,35 @@ subroutine get_closest_stellar_models(teff, log_g, metallicity, lu_teff, &
156157
norm_logg = (log_g - logg_min)/(logg_max - logg_min)
157158
norm_meta = (metallicity - meta_min)/(meta_max - meta_min)
158159

160+
! Detect dummy axes once (outside the loop)
161+
use_teff_dim = .not. (all(lu_teff == 0.0_dp) .or. all(lu_teff == 999.0_dp) .or. all(lu_teff == -999.0_dp))
162+
use_logg_dim = .not. (all(lu_logg == 0.0_dp) .or. all(lu_logg == 999.0_dp) .or. all(lu_logg == -999.0_dp))
163+
use_meta_dim = .not. (all(lu_meta == 0.0_dp) .or. all(lu_meta == 999.0_dp) .or. all(lu_meta == -999.0_dp))
164+
159165
! Find closest models
160166
do i = 1, n
161-
teff_dist = 0.0
162-
logg_dist = 0.0
163-
meta_dist = 0.0
167+
teff_dist = 0.0_dp
168+
logg_dist = 0.0_dp
169+
meta_dist = 0.0_dp
164170

165-
if (teff_max - teff_min > 0.00) then
171+
if (teff_max - teff_min > 0.0_dp) then
166172
teff_dist = scaled_lu_teff(i) - norm_teff
167173
end if
168174

169-
if (logg_max - logg_min > 0.00) then
175+
if (logg_max - logg_min > 0.0_dp) then
170176
logg_dist = scaled_lu_logg(i) - norm_logg
171177
end if
172178

173-
if (meta_max - meta_min > 0.00) then
179+
if (meta_max - meta_min > 0.0_dp) then
174180
meta_dist = scaled_lu_meta(i) - norm_meta
175181
end if
176182

177-
! Detect dummy axes once
178-
179-
use_teff_dim = .not. (all(lu_teff == 0.0_dp) .or. all(lu_teff == 999.0_dp) .or. all(lu_teff == -999.0_dp))
180-
use_logg_dim = .not. (all(lu_logg == 0.0_dp) .or. all(lu_logg == 999.0_dp) .or. all(lu_logg == -999.0_dp))
181-
use_meta_dim = .not. (all(lu_meta == 0.0_dp) .or. all(lu_meta == 999.0_dp) .or. all(lu_meta == -999.0_dp))
182-
183-
! Inside the loop:
183+
! Compute distance using only valid dimensions
184184
distance = 0.0_dp
185185
if (use_teff_dim) distance = distance + teff_dist**2
186186
if (use_logg_dim) distance = distance + logg_dist**2
187187
if (use_meta_dim) distance = distance + meta_dist**2
188188

189-
distance = teff_dist**2 + logg_dist**2 + meta_dist**2
190-
191189
do j = 1, 4
192190
if (distance < min_distances(j)) then
193191
! Shift larger distances down
@@ -262,17 +260,17 @@ subroutine interpolate_array(x_in, y_in, x_out, y_out)
262260
! Validate input sizes
263261
if (size(x_in) < 2 .or. size(y_in) < 2) then
264262
print *, "Error: x_in or y_in arrays have fewer than 2 points."
265-
stop
263+
call mesa_error(__FILE__, __LINE__)
266264
end if
267265

268266
if (size(x_in) /= size(y_in)) then
269267
print *, "Error: x_in and y_in arrays have different sizes."
270-
stop
268+
call mesa_error(__FILE__, __LINE__)
271269
end if
272270

273271
if (size(x_out) <= 0) then
274272
print *, "Error: x_out array is empty."
275-
stop
273+
call mesa_error(__FILE__, __LINE__)
276274
end if
277275

278276
do i = 1, size(x_out)

colors/private/linear_interp.f90

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
module linear_interp
2525
use const_def, only: dp
2626
use colors_utils, only: dilute_flux
27+
use utils_lib, only: mesa_error
2728
implicit none
2829

2930
private
@@ -68,15 +69,17 @@ subroutine construct_sed_linear(teff, log_g, metallicity, R, d, file_names, &
6869
INQUIRE (file=bin_filename, EXIST=file_exists)
6970

7071
if (.not. file_exists) then
71-
stop 'Missing required binary file for interpolation'
72+
print *, 'Missing required binary file for interpolation'
73+
call mesa_error(__FILE__, __LINE__)
7274
end if
7375

7476
! Load the data from binary file
7577
call load_binary_data(bin_filename, teff_grid, logg_grid, meta_grid, &
7678
wavelengths, precomputed_flux_cube, status)
7779

7880
if (status /= 0) then
79-
stop 'Binary data loading error'
81+
print *, 'Binary data loading error'
82+
call mesa_error(__FILE__, __LINE__)
8083
end if
8184

8285
n_teff = size(teff_grid)

0 commit comments

Comments
 (0)