Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Julia: Code being sent does not conform the original one. #449

Open
ChuanqiChenCC opened this issue Dec 13, 2024 · 11 comments
Open

Julia: Code being sent does not conform the original one. #449

ChuanqiChenCC opened this issue Dec 13, 2024 · 11 comments

Comments

@ChuanqiChenCC
Copy link

ChuanqiChenCC commented Dec 13, 2024

using FFTW
using Plots
using ProgressMeter
using Interpolations
using LinearAlgebra
using Printf
using Statistics
N_POINTS_Y = 360
ASPECT_RATIO = 16/9
KINEMATIC_VISCOSITY = 1.0 / 1000.0
TIME_STEP_LENGTH = 0.001
N_TIME_STEPS = 750

FORCING_WAVENUMBER = 8
FORCING_SCALE = 100.0

n_points_x = Int(ASPECT_RATIO * N_POINTS_Y)
x_extent = n_points_x / N_POINTS_Y - 1.0e-7
x_interval = range(0.0, x_extent, length=n_points_x)
y_interval = range(0.0, 1.0, length=N_POINTS_Y)
coordinates_x = [x for x in x_interval, y in y_interval]
coordinates_y = [y for x in x_interval, y in y_interval]
wavenumbers_1d_x = rfftfreq(n_points_x) .* n_points_x
n_fft_points_x = length(wavenumbers_1d_x)
wavenumbers_1d_y = fftfreq(N_POINTS_Y) .* N_POINTS_Y
n_fft_points_y = length(wavenumbers_1d_y)
wavenumbers_x = [k_x for k_x in wavenumbers_1d_x, k_y in wavenumbers_1d_y]
wavenumbers_x = [k_x for k_x in wavenumbers_1d_x, k_y in wavenumbers_1d_y]
wavenumbers_x = [k_x for k_x in wavenumbers_1d_x, k_y in wavenumbers_1d_y]
for i in 1:5
    print(i)
    print(i)
end

For the above julia code, select all text under VIM visual-line mode, and then ctrl-c ctrl-c to send to julia console.
The last "for" loop code will be sent with the wrong indent.
With more contents in the for loop, the code being sent will become more messy.

If I just copy / paste the above code to julia console, it works properly.

@jpalardy
Copy link
Owner

hi @ChuanqiChenCC

which target are you using?

does indentation never work, or just in this case?

does sending smaller block make things better/worse?

@ChuanqiChenCC
Copy link
Author

ChuanqiChenCC commented Dec 14, 2024

I am using the tmux as target.

The specific settings are:

let g:slime_target = "tmux"
let g:slime_default_config = {'socket_name': 'default', 'target_pane': '{last}'}
let g:slime_bracketed_paste = 1
let g:slime_dont_ask_default = 1

The indention does not work just in this case.

Sending small block, for example only the "for" loop itself can solve the problem.

@jpalardy
Copy link
Owner

does the value of g:slime_bracketed_paste matter? (do you get a different result?)

it's possible this is happening due to an unfortunate alignment of chunk_size with that paragraph (which wc clocks at 1039 bytes). In that case, you could try to reduce/increase the size to see if it's a boundary condition

@ChuanqiChenCC
Copy link
Author

if set g:slime_bracketed_paste to be 0, it will send the code block line by line to julia console which will make the indention become even worse.

Tune the chunk size cannot solve the problem.

@ChuanqiChenCC
Copy link
Author

using FFTW
using Plots
using ProgressMeter
using Interpolations
using LinearAlgebra
using Printf
using Statistics

N_POINTS_Y = 256
ASPECT_RATIO = 1.0
KINEMATIC_VISCOSITY = 1.0 / 1000.0
TIME_STEP_LENGTH = 0.001
N_TIME_STEPS = 10000

FORCING_WAVENUMBER = 8
FORCING_SCALE = 100.0

n_points_x = Int(ASPECT_RATIO * N_POINTS_Y)
x_extent = n_points_x / N_POINTS_Y - 1.0e-7
x_interval = range(0.0, x_extent, length=n_points_x)
y_interval = range(0.0, 1.0, length=N_POINTS_Y)

coordinates_x = [x for x in x_interval, y in y_interval]
coordinates_y = [y for x in x_interval, y in y_interval]

wavenumbers_1d_x = rfftfreq(n_points_x) .* n_points_x
n_fft_points_x = length(wavenumbers_1d_x)
wavenumbers_1d_y = fftfreq(N_POINTS_Y) .* N_POINTS_Y
n_fft_points_y = length(wavenumbers_1d_y)

wavenumbers_x = [k_x for k_x in wavenumbers_1d_x, k_y in wavenumbers_1d_y]
wavenumbers_y = [k_y for k_x in wavenumbers_1d_x, k_y in wavenumbers_1d_y]
wavenumbers_norm = [norm([k_x, k_y]) for k_x in wavenumbers_1d_x, k_y in wavenumbers_1d_y]

decay = exp.(- TIME_STEP_LENGTH .* KINEMATIC_VISCOSITY .* wavenumbers_norm.^2)
wavenumbers_norm[iszero.(wavenumbers_norm)] .= 1.0
normalized_wavenumbers_x = wavenumbers_x ./ wavenumbers_norm
normalized_wavenumbers_y = wavenumbers_y ./ wavenumbers_norm

force_x = FORCING_SCALE * sin.(FORCING_WAVENUMBER * pi * coordinates_y)

# Preallocate arrays
backtraced_coordinates_x = zeros(Float32, n_points_x, N_POINTS_Y)
backtraced_coordinates_y = zeros(Float32, n_points_x, N_POINTS_Y)

velocity_x = zeros(Float32, n_points_x, N_POINTS_Y)
velocity_y = zeros(Float32, n_points_x, N_POINTS_Y)

velocity_x_prev = zeros(Float32, n_points_x, N_POINTS_Y)
velocity_y_prev = zeros(Float32, n_points_x, N_POINTS_Y)

velocity_x_fft = zeros(Complex{Float32}, n_fft_points_x, n_fft_points_y)
velocity_y_fft = zeros(Complex{Float32}, n_fft_points_x, n_fft_points_y)
pressure_fft = zeros(Complex{Float32}, n_fft_points_x, n_fft_points_y)

d_u_d_y_fft = zeros(Complex{Float32}, n_fft_points_x, n_fft_points_y)
d_v_d_x_fft = zeros(Complex{Float32}, n_fft_points_x, n_fft_points_y)
curl_fft = zeros(Complex{Float32}, n_fft_points_x, n_fft_points_y)
curl = zeros(Float32, n_points_x, N_POINTS_Y)
curl_lst = zeros(Float32, N_TIME_STEPS, n_points_x, N_POINTS_Y)

interpolator = interpolate(
    (x_interval, y_interval),
    velocity_x,
    Gridded(Linear()),
)

theme(:dark)

@showprogress "Timestepping ..." for iter in 1:N_TIME_STEPS
    # (1) Apply the forces
    velocity_x_prev .+= force_x * TIME_STEP_LENGTH

    # (2) Self-Advection by backtracing and interpolation
    backtraced_coordinates_x .= mod1.(
        coordinates_x - TIME_STEP_LENGTH * velocity_x_prev,
        x_extent,
    ) 
    backtraced_coordinates_y .= mod1.(
        coordinates_y - TIME_STEP_LENGTH * velocity_y_prev,
        1.0,
    )
    interpolator.coefs .= velocity_x_prev
    velocity_x .= interpolator.(backtraced_coordinates_x, backtraced_coordinates_y)
    interpolator.coefs .= velocity_y_prev
    velocity_y .= interpolator.(backtraced_coordinates_x, backtraced_coordinates_y)

    # (3) Stabilize by subtracting the mean velocities
    velocity_x .-= mean(vec(velocity_x))
    velocity_y .-= mean(vec(velocity_y))

    # (4.1) Transform into Fourier domain
    velocity_x_fft = rfft(velocity_x)
    velocity_y_fft = rfft(velocity_y)

    # (4.2) Diffuse by low-pass filtering
    velocity_x_fft .*= decay
    velocity_y_fft .*= decay

    # (4.3) Compute the Pseudo-Pressure by Divergence in Fourier Domain
    pressure_fft = (
        velocity_x_fft .* normalized_wavenumbers_x
        +
        velocity_y_fft .* normalized_wavenumbers_y
    )

    # (4.4) Project the velocities to be incompressible
    velocity_x_fft -= pressure_fft .* normalized_wavenumbers_x
    velocity_y_fft -= pressure_fft .* normalized_wavenumbers_y

    # (4.5) Transform back into spatial domain
    velocity_x = irfft(velocity_x_fft, n_points_x)
    velocity_y = irfft(velocity_y_fft, n_points_x)

    # (5) Stabilize by subtracting the mean velocities
    velocity_x .-= mean(vec(velocity_x))
    velocity_y .-= mean(vec(velocity_y))

    # (6) Advance in time
    velocity_x_prev = velocity_x
    velocity_y_prev = velocity_y

    # Visualize
    d_u_d_y_fft = im .* wavenumbers_y .* velocity_x_fft
    d_v_d_x_fft = im .* wavenumbers_x .* velocity_y_fft
    curl_fft = d_v_d_x_fft - d_u_d_y_fft
    curl = irfft(curl_fft, n_points_x)

    curl = sign.(curl) .* sqrt.(abs.(curl) ./ quantile(vec(curl), 0.8))
    curl_lst[iter, :, :] = curl

end

This is the full code that I have encountered the problem.
It will make the tmux record history blow up and make the contents in the for loop become very messy.

Copy / Paste to Julia console does not have this problem.

@jpalardy
Copy link
Owner

I'll need to play with this code and Julia to see if I can reproduce.

Do I need to install anything else for this code to work, on the Julia side?

@ChuanqiChenCC
Copy link
Author

Only the packages at the beginning of the code are needed.

using Pkg
Pkg.add(<name>)

You can use the code above to install any missing packages.

@jpalardy
Copy link
Owner

I tried to run this ^

Screenshot_2024-12-14_12-10-49

as far as I can tell

  • it ran top-to-bottom
  • text was indented "funny", but that didn't break functionality

what do you see?

@ChuanqiChenCC
Copy link
Author

Yes, my output is the same as yours.

If you select the Julia console and use Ctrl-[ to get into copy mode, you can find that there are many repeated code text. Almost 2000 lines.

It should also be noted that the results cannot be guaranteed since Copy/Paste works properly in the Julia console.

@jpalardy
Copy link
Owner

I'm not sure vim-repl can get to 100%-like-paste — each REPL does a fair degree of interpretation and key capture

I see the same "problem" in R. I can see how the paste mangles and might duplicate, based on how fast pasting versus executing is happening.

Generally I don't let it bother me, because the alternatives all seem worse 😬 Though I try to proceed in with pasting small-ish paragraphs, one at a time.

Other targets (not tmux) might have better inject-like-a-paste semantics, you can experiment.

Let me know

@ChuanqiChenCC
Copy link
Author

Thanks for all your comments and explanations!

I like the vim-slime and enjoy using this great work!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants