Compare commits

...
21 Commits
Author SHA1 Message Date
wyj 96c7350aa1 update: allow -DFLAT=1 to set a flat spacetime without black hole during compilation 2026-02-28 02:04:51 -05:00
wyj 4be6add815 update: multiple improvement 2025-12-12 13:47:23 -05:00
wyj de6d3bc04a update: use a single integrand function 2025-12-07 22:47:53 -05:00
wyj 480c49dcf0 fix: fix intergrad 2025-12-07 22:41:59 -05:00
wyj bbbebf1fdf update: rerun MC integral 2025-12-07 18:53:12 -05:00
wyj 37b7fdce8f fix: memcpy 2025-12-07 18:52:39 -05:00
wyj fb2a2fce59 clean: clean and omp 2025-12-07 17:22:50 -05:00
wyj b96381c56e fix: rng performance 2025-12-07 17:14:40 -05:00
wyj 3954fd7f24 update: add openmp parallel 2025-12-07 16:33:52 -05:00
wyj 5ddfc05da1 update: more tests 2025-12-07 13:15:18 -05:00
wyj 7eca677774 clean: clean printf 2025-12-07 13:15:05 -05:00
wyj 1f7eb8cb36 new: add quick_select 2025-12-07 13:14:36 -05:00
wyj 17165cca66 update: add FLAT 2025-12-07 13:13:50 -05:00
wyj 41f24e88e9 update: change buffer to double* 2025-12-07 12:14:14 -05:00
wyj b00e652394 fix: add fabs on the Jacobian 2025-12-07 11:18:20 -05:00
wyj 1819d58778 fix: if cotpsi is larger than cotpsi_max, color is black 2025-12-07 05:36:55 -05:00
wyj 1e1c38da4e new: test_png.c 2025-12-07 05:30:24 -05:00
wyj be941f6a4a fix: the correct should be (*buffer)[3] 2025-12-07 05:26:56 -05:00
wyj b3ccb2e930 update: **buffer to *buffer[3] 2025-12-07 05:18:37 -05:00
wyj e14214a92b update: update render.h 2025-12-07 05:01:30 -05:00
wyj af45e3de24 new: render.h 2025-12-07 05:00:19 -05:00
9 changed files with 230 additions and 86 deletions
+17 -3
View File
@@ -1,15 +1,28 @@
#ifndef __COMMON_H
#define __COMMON_H
#include <stdio.h>
#include <stdlib.h>
#include <gsl/gsl_errno.h>
#include <math.h>
#define POW2(x) ((x)*(x))
#ifndef FLAT
#define FLAT 0
#endif
#define PI 3.1415926535897932384626433832795028841971693993751058
#define Rs 1
#define M (0.5*Rs)
#define M (FLAT ? 0 : 0.5*Rs)
#define R0 (15*Rs)
#define f(r) ((1.0-Rs/((double)r)))
#define bmin (1.5*sqrt(3.0)+5e-10)
#define f(r) ((1.0-(2*M)/((double)(r))))
#define bmin (FLAT ? 1e-10 : 1.5*sqrt(3.0)+5e-10)
#define cotpsi_max (sqrt(R0*R0/(bmin*bmin*f(R0)) - 1))
#define tanpsi_min (1.0/cotpsi_max)
#define THETAERROR 100000
#define color_index(i,j,c) (j*W*3+i*3+c)
#define cutperc (FLAT ? 1.0 : 0.99)
#define SCALE 1
typedef struct {
int W;
@@ -18,3 +31,4 @@ typedef struct {
int (*angle_to_pixel)(double *, double *);
} System;
#endif // !__COMMON_H
+8 -10
View File
@@ -63,8 +63,8 @@ static inline double find_root_5ord(double u1, double u2, double v1, double v2,
pow(u2,2))*v1*v2);
}
// \chi(cotpsi) is the totol deflection angle with parameter cotpsi=-dr/(r √f dphi)
double chi(double cotpsi){
// \chi(tanpsi) is the totol deflection angle with parameter tanpsi=-(r √f dphi)/dr
double chi(double tanpsi){
gsl_odeiv2_system sys = {func, jac, 2, NULL};
gsl_odeiv2_driver *d = gsl_odeiv2_driver_alloc_y_new(&sys, gsl_odeiv2_step_rk8pd, 1e-3, 1e-12, 1e-10);
@@ -72,7 +72,7 @@ double chi(double cotpsi){
double y[2] = {
u0,
//sqrt(1.0/(b*b) - u0*u0 + 2.0*M*u0*u0*u0)
u0*sqrt(f(R0))*cotpsi
u0*sqrt(f(R0))/tanpsi
};
double phi = 0;
double phi_max = 8*PI;
@@ -142,15 +142,13 @@ int init(double rmax, double rela_err_limit, int *size, double **x, double **y){
sample.size=1;
sample.x = malloc(sizeof(double) * 1);
sample.y = malloc(sizeof(double) * 1);
double cotpsi_min = 1/rmax;
sample.x[0] = cotpsi_min;
sample.y[0] = chi(cotpsi_min);
double cotpsi_max = sqrt(R0*R0/(bmin*bmin*f(R0)) - 1);
double chi_cotpsimax = chi(cotpsi_max);
sample.x[0] = tanpsi_min;
sample.y[0] = chi(tanpsi_min);
double chi_tanpsimax = chi(rmax);
refine_interval(cotpsi_min, cotpsi_max, sample.y[0], chi_cotpsimax, rela_err_limit, &sample, 1);
refine_interval(tanpsi_min, rmax, sample.y[0], chi_tanpsimax, rela_err_limit, &sample, 1);
sample_push(&sample, cotpsi_max, chi_cotpsimax);
sample_push(&sample, rmax, chi_tanpsimax);
//sort
+55
View File
@@ -0,0 +1,55 @@
#include <string.h>
#include <stdlib.h>
#include "quick_select.h"
static inline void swap_double(double *a, double *b) {
double t = *a;
*a = *b;
*b = t;
}
// Lomuto partition
static size_t partition(double *arr, size_t lo, size_t hi) {
double pivot = arr[hi];
size_t i = lo;
for (size_t j = lo; j < hi; ++j) {
if (arr[j] < pivot) {
swap_double(&arr[i], &arr[j]);
++i;
}
}
swap_double(&arr[i], &arr[hi]);
return i;
}
// 选择第 k 小的元素 (0-based),平均 O(n),最坏 O(n^2)
static double select_k(double *arr, size_t n, size_t k) {
size_t lo = 0;
size_t hi = n - 1;
for (;;) {
size_t p = partition(arr, lo, hi);
if (p == k) {
return arr[p];
} else if (k < p) {
hi = p - 1;
} else {
lo = p + 1;
}
}
return arr[k];
}
double percentile(const double *arr, size_t n, double perc) {
if (n == 0) {
return 0.0;
}
double *arr_copy = malloc(sizeof(double)*n);
memcpy(arr_copy, arr, n*sizeof(double));
// nearest-rank: ceil(0.9*n) - 1
size_t k = (size_t)(perc*n) - 1;
double ans = select_k(arr_copy, n, k);
free(arr_copy);
return ans;
}
+2
View File
@@ -0,0 +1,2 @@
#include <stddef.h>
double percentile(const double *arr, size_t n, double perc);
+55 -53
View File
@@ -3,10 +3,13 @@
#include <gsl/gsl_rng.h>
#include <gsl/gsl_spline.h>
#include <gsl/gsl_monte_miser.h>
#include <math.h>
#include <stdio.h>
#include "render.h"
#include "common.h"
#include "init.h"
typedef struct {
int c;
const System *system;
const Spline_data *spline_data;
} Integrand_params;
@@ -18,37 +21,18 @@ inline double xy_to_b(double x, double y) {
return b;
}
double integrand_r(double *xy, size_t dim, void *integrand_params_void) {
double cotpsi = 1.0/hypot(xy[0], xy[1]);
double integrand(double *xy, size_t dim, void *integrand_params_void) {
double tanpsi = hypot(xy[0], xy[1]);
double cotpsi = 1.0/tanpsi;
if (tanpsi <= tanpsi_min) return 0;
Integrand_params *integrand_params = (Integrand_params *)integrand_params_void;
double theta = gsl_spline_eval(integrand_params->spline_data->spline, cotpsi, integrand_params->spline_data->acc);
double theta = gsl_spline_eval(integrand_params->spline_data->spline, tanpsi, integrand_params->spline_data->acc);
double phi = atan2(xy[1], xy[0]);
double rgb[3] = {0};
double theta_phi[2] = {theta, phi};
integrand_params->system->angle_to_pixel(theta_phi, rgb);
return rgb[0]*gsl_spline_eval_deriv(integrand_params->spline_data->spline, cotpsi, integrand_params->spline_data->acc);
}
double integrand_g(double *xy, size_t dim, void *integrand_params_void) {
double cotpsi = 1.0/hypot(xy[0], xy[1]);
Integrand_params *integrand_params = (Integrand_params *)integrand_params_void;
double theta = gsl_spline_eval(integrand_params->spline_data->spline, cotpsi, integrand_params->spline_data->acc);
double phi = atan2(xy[1], xy[0]);
double rgb[3] = {0};
double theta_phi[2] = {theta, phi};
integrand_params->system->angle_to_pixel(theta_phi, rgb);
return rgb[1]*gsl_spline_eval_deriv(integrand_params->spline_data->spline, cotpsi, integrand_params->spline_data->acc);
}
double integrand_b(double *xy, size_t dim, void *integrand_params_void) {
double cotpsi = 1.0/hypot(xy[0], xy[1]);
Integrand_params *integrand_params = (Integrand_params *)integrand_params_void;
double theta = gsl_spline_eval(integrand_params->spline_data->spline, cotpsi, integrand_params->spline_data->acc);
double phi = atan2(xy[1], xy[0]);
double rgb[3] = {0};
double theta_phi[2] = {theta, phi};
integrand_params->system->angle_to_pixel(theta_phi, rgb);
return rgb[2]*gsl_spline_eval_deriv(integrand_params->spline_data->spline, cotpsi, integrand_params->spline_data->acc);
//printf("(x,y)=(%g,%g), tanpsi=%g, theta=%g, %g\n", xy[0], xy[1], tanpsi, theta, POW2(sin(theta))*(2+POW2(cotpsi)+POW2(tanpsi)));
return rgb[integrand_params->c]*fabs(gsl_spline_eval_deriv(integrand_params->spline_data->spline, tanpsi, integrand_params->spline_data->acc))*POW2(sin(theta))*(2+POW2(tanpsi)+POW2(cotpsi));
}
int MC_pixel_render(const System *system, int i, int j, const Spline_data spline_data, double *rgb, int pixel_render_max, double pixel_render_err, gsl_rng *r){
@@ -57,34 +41,42 @@ int MC_pixel_render(const System *system, int i, int j, const Spline_data spline
double y_lu = -j*dw + (system->H)*dw/2;
double xl[2] = {x_lu, y_lu - dw};
double xu[2] = {x_lu + dw, y_lu};
double err_dw = pixel_render_err*dw*dw;
//printf("integrating for (i,j)=(%d, %d)\n", i, j);
//TODO: fill the MC intergral
// linear rgb = ∫ rgb(χ, ϕ) sin^2(χ) |χ'(ψ)| dx dy
// χ'(ψ) = χ'(cot(ψ)) * cot'(ψ) = - χ'(cot(ψ))/\sin^2(ψ)
// linear rgb = ∫ rgb(χ, ϕ) sin^2(χ)/sin^2(ψ) |χ'(ψ)| dx dy
// χ'(ψ) = χ'(tan(ψ)) * tan'(ψ) = - χ'(tan(ψ))/cos^2(ψ) = - χ'(tan(ψ))(1+tan^2(ψ))
gsl_monte_miser_state *miser_state = gsl_monte_miser_alloc(2);
double color;
double err;
Integrand_params integrand_params = {system, &spline_data};
Integrand_params integrand_params = {0, system, &spline_data};
gsl_monte_function F;
F.f = integrand;
F.params = &integrand_params;
F.dim = 2;
//printf("i=%d, j=%d, rgb=( ", i, j);
for (int c = 0; c < 3; c++) {
switch (c) {
case 0:
F.f = integrand_r;
break;
case 1:
F.f = integrand_g;
break;
case 2:
F.f = integrand_b;
break;
integrand_params.c = c;
if (i==0 && j==0) {
//printf("(x,y) = (%g, %g), intergrand[%d]=%g\n", xl[0], xl[1], c, integrand(xl, 2, &integrand_params));
}
gsl_monte_miser_init(miser_state);
gsl_monte_miser_integrate(&F, xl, xu, 2, 100, r, miser_state, &color, &err);
rgb[c] = color;
//printf("%g +- %g ", color, err);
if (err > err_dw) {
//int ncalls = 100*POW2(err/err_dw);
//printf("(i,j)=(%d,%d), err=%g, err_dw=%g, ncalls=%d\n", i, j, err, err_dw, ncalls);
//if (ncalls < 0 || ncalls > pixel_render_max) ncalls = pixel_render_max;
int ncalls = pixel_render_max;
//printf("(i,j)=(%d,%d), err=%g, err_dw=%g, ncalls=%d\n", i, j, err, err_dw, ncalls);
gsl_monte_miser_integrate(&F, xl, xu, 2, ncalls, r, miser_state, &color, &err);
}
rgb[c] = color/(dw*dw);
}
//printf("finished for (i,j)=(%d, %d)\n", i, j);
return 0;
}
@@ -93,30 +85,40 @@ int pixel_render(const System *system, int i, int j, const Spline_data spline_da
return MC_pixel_render(system, i, j, spline_data, rgb, pixel_render_max, pixel_render_err, r);
}
int render(System *system, double **buffer, int pixel_render_max, double pixel_render_err, double chi_rela_err) {
int render(System *system, double *buffer, int pixel_render_max, double pixel_render_err, double chi_rela_err) {
double h = (system->H)*(system->w)/(system->W);
double tanpsi2 = (system->w)*(system->w)+h*h;
double cotpsi2 = 1/tanpsi2;
//double tanpsi2 = (system->w)*(system->w)+h*h;
//double cotpsi2 = 1/tanpsi2;
//double bmax = R0/sqrt(f(R0)*(1+cotpsi2));
double rmax = sqrt(tanpsi2);
gsl_rng *r = gsl_rng_alloc(gsl_rng_default);
double rmax = hypot(system->w, h);
double *x=NULL;
double *y = NULL;
int size;
int W = system->W;
int H = system->H;
init(rmax, chi_rela_err, &size, &x, &y);
gsl_interp_accel *acc = gsl_interp_accel_alloc();
gsl_spline *spline = gsl_spline_alloc(gsl_interp_steffen, size);
Spline_data spline_data = {spline, acc};
gsl_spline_init(spline, x, y, size);
for(int j = 0; j < system->H; j++) {
for (int i = 0; i < system->W; i++) {
pixel_render(system, i, j, spline_data, buffer[j*(system->W) + i], pixel_render_max, pixel_render_err, r);
}
}
#pragma omp parallel
{
gsl_interp_accel *acc = gsl_interp_accel_alloc();
Spline_data spline_data = {spline, acc};
gsl_rng *r = gsl_rng_alloc(gsl_rng_default);
#pragma omp for
for(int j = 0; j < H; j++) {
for (int i = 0; i < W; i++) {
pixel_render(system, i, j, spline_data, buffer+color_index(i,j,0), pixel_render_max, pixel_render_err, r);
}
}
gsl_interp_accel_free(acc);
gsl_rng_free(r);
}
printf("rendering finished.\n");
return 0;
}
+4
View File
@@ -0,0 +1,4 @@
#include "common.h"
#include "init.h"
int render(System *system, double *buffer, int pixel_render_max, double pixel_render_err, double chi_rela_err);
+73
View File
@@ -0,0 +1,73 @@
#include "render.h"
#include "write_png.h"
int angle_to_pixel_white(double *angle, double *rgb) {
rgb[0] = 1.0;
rgb[1] = 1.0;
rgb[2] = 1.0;
return 0;
}
int test(double *thetaphi, double *rgb){
int i = floor(8.0*thetaphi[0]/PI);
int j = floor(8.0*(thetaphi[1]+PI)/PI);
int k = (i+j)&1;
int l = floor(2*thetaphi[1]/PI);
rgb[0] = k;
rgb[1] = k;
rgb[2] = k;
switch (l%4) {
case 1:
rgb[1] = rgb[2] = 0;
break;
case 2:
rgb[0] = rgb[2] = 0;
break;
case 3:
rgb[0] = rgb[1] = 0;
break;
default:
break;
}
return 0;
}
int test2(double *thetaphi, double *rgb){
double tantheta = tan(PI-thetaphi[0]);
int i = (int)floor(4*tantheta*cos(thetaphi[1]));
int j = (int)floor(4*tantheta*sin(thetaphi[1]));
int k = (i+j)&1;
rgb[0] = 1-k;
rgb[1] = 1-k;
rgb[2] = 1-k;
return 0;
}
int test3(double *thetaphi, double *rgb){
double x = sin(thetaphi[0])*sin(thetaphi[1]);
double y = sin(thetaphi[0])*cos(thetaphi[1]);
double z = cos(thetaphi[0]);
double theta1 = atan2(hypot(z, x), y);
double phi1 = atan2(-z, x);
int i = (int)floor(16*theta1/PI);
int j = (int)floor(16*phi1/PI);
int k = (i+j)&1;
rgb[0] = 1-k;
rgb[1] = 1-k;
rgb[2] = 1-k;
return 0;
}
int main(){
int W = 1600;
int H = 900;
double w = 2;
System system = {W, H, w, test};
double *buffer = malloc(sizeof(double)*W*H*3);
render(&system, buffer, 10000, 1.0/256, 1e-1);
write_png("test_1.png", buffer, W, H);
free(buffer);
return 0;
}
+15 -16
View File
@@ -1,4 +1,10 @@
#include <spng.h>
#include <math.h>
#include <stdint.h>
#include <stdio.h>
#include "write_png.h"
#include "common.h"
#include "quick_select.h"
static inline double linear_to_srgb(double x){
if (x <= 0.0) return 0.0;
@@ -7,40 +13,33 @@ static inline double linear_to_srgb(double x){
return 1.055 * pow(x, 1.0/2.4) - 0.055;
}
int buffer_normalize_srgb(double **buffer, int W, int H) {
double max = 0;
int buffer_normalize_srgb(double *buffer, int W, int H) {
#pragma omp parallel for
for (int j = 0; j < H; j++) {
for (int i = 0; i < W; i++) {
for (int c = 0; c < 3; c++) {
if (max < buffer[j*W + i][c]) max = buffer[j*W + i][c];
}
}
}
if (max == 0) return 1;
for (int j = 0; j < H; j++) {
for (int i = 0; i < W; i++) {
for (int c = 0; c < 3; c++) {
buffer[j*W + i][c] /= max;
buffer[j*W+i][c] = linear_to_srgb(buffer[j*W+i][c]);
buffer[color_index(i, j, c)] = linear_to_srgb(SCALE*buffer[color_index(i, j, c)]);
}
}
}
return 0;
}
int write_png(char *filename, double **buffer, int W, int H) {
int write_png(char *filename, double *buffer, int W, int H) {
buffer_normalize_srgb(buffer, W, H);
size_t bufsize = W*H*3;
uint8_t *img = (uint8_t *)malloc(bufsize);
//printf("bufsize: %d\n", bufsize);
uint8_t *img = (uint8_t *)malloc(bufsize*sizeof(uint8_t));
if (!img) {return 1;}
for (int j = 0; j < H; j++) {
for (int i = 0; i < W; i++) {
for (int c = 0; c < 3; c++) {
int v = (int)lrint(buffer[j*W*i][c]);
int v = (int)lrint(buffer[color_index(i,j,c)]*255);
if (v < 0) v = 0;
if (v > 255) v = 255;
img[j*W*3+i*3+c] = (uint8_t) v;
img[color_index(i, j, c)] = (uint8_t) v;
//printf("i: %d, j: %d, c:%d, buffer=%g, v=%d\n", i,j,c,buffer[color_index(i, j, c)],v);
}
}
}
+2 -5
View File
@@ -1,6 +1,3 @@
#include <spng.h>
#include <math.h>
#include <stdint.h>
#include "common.h"
int write_png(char *filename, double **buffer, int W, int H);
int write_png(char *filename, double *buffer, int W, int H);