Skip to content

Commit eb01e87

Browse files
authored
Merge pull request #39 from MennoVeerman/main
some changes to rt_lite and fixing cloud optics bug
2 parents 812c5fa + e018ced commit eb01e87

File tree

7 files changed

+352
-161
lines changed

7 files changed

+352
-161
lines changed

include_rt_kernels/raytracer_kernels_bw.h

Lines changed: 12 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@ constexpr int bw_kernel_grid = 1024;
2323
constexpr int bw_kernel_block = 256;
2424
constexpr int bw_kernel_grid = 256;
2525
#endif
26-
constexpr Float k_null_gas_min = Float(1.e-3);
2726

2827
// sun has a half angle of .266 degrees
2928
constexpr Float cos_half_angle = Float(0.9999891776066407); // cos(half_angle);
@@ -60,24 +59,21 @@ struct Camera
6059
const Float yaw = yaw_deg / Float(180.) * M_PI;
6160
const Float pitch = pitch_deg / Float(180.) * M_PI;
6261
const Float roll = roll_deg / Float(180.) * M_PI;
63-
mx = {cos(yaw)*sin(pitch), (cos(yaw)*cos(pitch)*sin(roll)-sin(yaw)*cos(roll)), (cos(yaw)*cos(pitch)*cos(roll)+sin(yaw)*sin(roll))};
64-
my = {sin(yaw)*sin(pitch), (sin(yaw)*cos(pitch)*sin(roll)+cos(yaw)*cos(roll)), (sin(yaw)*cos(pitch)*cos(roll)-cos(yaw)*sin(roll))};
65-
mz = {-cos(pitch), sin(pitch)*sin(roll), sin(pitch)*cos(roll)};
62+
mx = {cos(yaw)*cos(pitch), (cos(yaw)*sin(pitch)*sin(roll)-sin(yaw)*cos(roll)), (cos(yaw)*sin(pitch)*cos(roll)+sin(yaw)*sin(roll))};
63+
my = {sin(yaw)*cos(pitch), (sin(yaw)*sin(pitch)*sin(roll)+cos(yaw)*cos(roll)), (sin(yaw)*sin(pitch)*cos(roll)-cos(yaw)*sin(roll))};
64+
mz = {-sin(pitch), cos(pitch)*sin(roll), cos(pitch)*cos(roll)};
6665
}
6766

6867
void setup_normal_camera(const Camera camera)
6968
{
7069
if (!fisheye)
7170
{
72-
const Vector<Float> dir_tmp = {0, 0, 1};
73-
const Vector<Float> dir_cam = normalize(Vector<Float>({dot(camera.mx,dir_tmp), dot(camera.my,dir_tmp), dot(camera.mz,dir_tmp)*Float(-1)}));
74-
Vector<Float> dir_up;
75-
if ( (int(dir_cam.z)==1) || (int(dir_cam.z)==-1) )
76-
dir_up = {1, 0, 0};
77-
else
78-
dir_up = {0, 0, 1};
79-
80-
cam_width = normalize(cross(dir_cam, dir_up));
71+
const Vector<Float> dir_tmp = {1, 0, 0};
72+
const Vector<Float> dir_cam = normalize(Vector<Float>({dot(camera.mx,dir_tmp), dot(camera.my,dir_tmp), dot(camera.mz,dir_tmp)}));
73+
const Vector<Float> dir_up_tmp = {0, 0, 1};
74+
const Vector<Float> dir_up = normalize(Vector<Float>({dot(camera.mx,dir_up_tmp), dot(camera.my,dir_up_tmp), dot(camera.mz,dir_up_tmp)}));
75+
76+
cam_width = Float(-1) * normalize(cross(dir_cam, dir_up));
8177
cam_height = normalize(cross(dir_cam, cam_width));
8278
cam_depth = dir_cam / tan(fov/Float(180)*M_PI/Float(2.));
8379

@@ -91,17 +87,18 @@ struct Camera
9187
// size of output arrays, either number of horizontal and vertical pixels, or number of zenith/azimuth angles of fisheye lens. Default to 1024
9288
int ny = 1024;
9389
int nx = 1024;
90+
Int npix;
9491
};
9592

9693

9794
__global__
9895
void ray_tracer_kernel_bw(
9996
const int igpt,
100-
const int photons_per_pixel,
97+
const Int photons_per_pixel,
10198
const Grid_knull* __restrict__ k_null_grid,
10299
Float* __restrict__ camera_count,
103100
Float* __restrict__ camera_shot,
104-
int* __restrict__ counter,
101+
Int* __restrict__ counter,
105102
const Float* __restrict__ k_ext, const Optics_scat* __restrict__ scat_asy,
106103
const Float* __restrict__ k_ext_bg, const Optics_scat* __restrict__ scat_asy_bg,
107104
const Float* __restrict__ z_lev_bg,

python/set_virtual_camera.py

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
import netCDF4 as nc
1212
import numpy as np
1313
import argparse
14+
import os
1415

1516
camera_variables = {
1617
"yaw": "Horizontal direction of camera, 0: east, 90: north, 180/-180: weast, -90/270: south",
@@ -39,7 +40,10 @@
3940
args = vars(parser.parse_args())
4041

4142
# open netcdf file
42-
ncf = nc.Dataset(args['name'],"r+")
43+
if os.path.isfile(args['name']):
44+
ncf = nc.Dataset(args['name'],"r+")
45+
else:
46+
print("file does not exist")
4347

4448
# add group if it does not exsist yet
4549
if not "camera-settings" in ncf.groups:
@@ -94,7 +98,7 @@
9498
print("Camera settings:")
9599
for v in camera_variables.keys():
96100
print("{:6}{:>8}".format(v, str(cam[v][:])))
97-
print("{:6}{:>8}".format("sza", str(np.round(np.rad2deg(np.arccos(ncf['mu0'][0,0])),1))))
98-
print("{:6}{:>8}".format("azi", str(np.round(np.rad2deg(ncf['azi'][0,0]),1))))
101+
print("{:6}{:>8}".format("sza", str(np.round(np.rad2deg(np.arccos(ncf['mu0'][:].flatten()[0])),1))))
102+
print("{:6}{:>8}".format("azi", str(np.round(np.rad2deg(ncf['azi'][:].flatten()[0]),1))))
99103

100104
ncf.close()

src_cuda_rt/Cloud_optics_rt.cu

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -43,11 +43,10 @@ namespace
4343

4444
if (mask[idx])
4545
{
46-
const Float reff= min(Float(21.5), re[idx]);
47-
const int index = min(int((reff - offset) / step_size) + 1, nsteps-1) - 1;
46+
const int index = min(int((re[idx] - offset) / step_size) + 1, nsteps-1) - 1;
4847

4948
const int idx_ib = index + ibnd*nsteps;
50-
const Float fint = (reff - offset) /step_size - (index);
49+
const Float fint = (re[idx] - offset) /step_size - (index);
5150

5251
const Float tau_local = cwp[idx] *
5352
(tau_table[idx_ib] + fint * (tau_table[idx_ib+1] - tau_table[idx_ib]));

src_cuda_rt/Raytracer_bw.cu

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -456,11 +456,12 @@ void Raytracer_bw::trace_rays(
456456

457457
Array_gpu<Float,2> camera_count({camera.nx, camera.ny});
458458
Array_gpu<Float,2> shot_count({camera.nx, camera.ny});
459-
Array_gpu<int,1> counter({1});
459+
Array_gpu<Int,1> counter({1});
460460

461461
Gas_optics_rrtmgp_kernels_cuda_rt::zero_array(camera.nx, camera.ny, camera_count.ptr());
462462
Gas_optics_rrtmgp_kernels_cuda_rt::zero_array(camera.nx, camera.ny, shot_count.ptr());
463-
Gas_optics_rrtmgp_kernels_cuda_rt::zero_array(1, counter.ptr());
463+
464+
counter.fill(Int(0));
464465

465466
// domain sizes
466467
const Vector<Float> grid_size = grid_d * grid_cells;
@@ -604,11 +605,11 @@ void Raytracer_bw::trace_rays_bb(
604605

605606
Array_gpu<Float,2> camera_count({camera.nx, camera.ny});
606607
Array_gpu<Float,2> shot_count({camera.nx, camera.ny});
607-
Array_gpu<int,1> counter({1});
608+
Array_gpu<Int,1> counter({1});
608609

609610
Gas_optics_rrtmgp_kernels_cuda_rt::zero_array(camera.nx, camera.ny, camera_count.ptr());
610611
Gas_optics_rrtmgp_kernels_cuda_rt::zero_array(camera.nx, camera.ny, shot_count.ptr());
611-
Gas_optics_rrtmgp_kernels_cuda_rt::zero_array(1, counter.ptr());
612+
counter.fill(Int(0));
612613

613614
// domain sizes
614615
const Vector<Float> grid_size = grid_d * grid_cells;

src_kernels_cuda_rt/raytracer_kernels_bw.cu

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -175,7 +175,7 @@ namespace
175175
Photon& photon,
176176
Float* __restrict__ camera_count,
177177
Float* __restrict__ camera_shot,
178-
const int ij_cam, const int n,
178+
const Int ij_cam, const int n,
179179
Random_number_generator<Float>& rng,
180180
const Vector<Float>& sun_direction,
181181
const Grid_knull* __restrict__ k_null_grid,
@@ -278,11 +278,11 @@ namespace
278278
__global__
279279
void ray_tracer_kernel_bw(
280280
const int igpt,
281-
const int photons_per_pixel,
281+
const Int photons_per_pixel,
282282
const Grid_knull* __restrict__ k_null_grid,
283283
Float* __restrict__ camera_count,
284284
Float* __restrict__ camera_shot,
285-
int* __restrict__ counter,
285+
Int* __restrict__ counter,
286286
const Float* __restrict__ k_ext, const Optics_scat* __restrict__ scat_asy,
287287
const Float* __restrict__ k_ext_bg, const Optics_scat* __restrict__ scat_asy_bg,
288288
const Float* __restrict__ z_lev_bg,
@@ -338,13 +338,13 @@ void ray_tracer_kernel_bw(
338338

339339
const Float s_min = max(max(grid_size.z, grid_size.x), grid_size.y) * Float_epsilon;
340340
const Float s_min_bg = max(max(grid_size.x, grid_size.y), z_top) * Float_epsilon;
341-
342-
while (counter[0] < camera.nx*camera.ny*photons_per_pixel)
341+
342+
while (counter[0] < camera.npix*photons_per_pixel)
343343
{
344-
const int count = atomicAdd(&counter[0], 1);
345-
const int ij_cam = count / photons_per_pixel;
344+
const Int count = atomicAdd(&counter[0], 1);
345+
const Int ij_cam = count / photons_per_pixel;
346346

347-
if (ij_cam >= camera.nx*camera.ny)
347+
if (ij_cam >= camera.npix)
348348
return;
349349

350350
Float weight;

0 commit comments

Comments
 (0)