@@ -370,29 +370,62 @@ function _steadystate_fourier(
370370 n_list = (- n_max): n_max
371371
372372 weight = 1
373- Mn = sparse (ones (Ns), [Ns * (j - 1 ) + j for j in 1 : Ns], fill (weight, Ns), N, N)
373+ rows = _dense_similar (L_0_mat, Ns)
374+ cols = _dense_similar (L_0_mat, Ns)
375+ vals = _dense_similar (L_0_mat, Ns)
376+ fill! (rows, 1 )
377+ for j in 1 : Ns
378+ cols[j] = Ns * (j - 1 ) + j
379+ end
380+ fill! (vals, weight)
381+ Mn = _sparse_similar (L_0_mat, rows, cols, vals, N, N)
374382 L = L_0_mat + Mn
375383
376- M = spzeros (T, n_fourier * N, n_fourier * N)
377- M += kron (spdiagm (1 => ones (n_fourier - 1 )), L_m_mat)
378- M += kron (spdiagm (- 1 => ones (n_fourier - 1 )), L_p_mat)
384+ M = _sparse_similar (L_0_mat, n_fourier * N, n_fourier * N)
385+
386+ # Add superdiagonal blocks (L_m)
387+ for i in 1 : (n_fourier- 1 )
388+ rows_block = _dense_similar (L_0_mat, N)
389+ cols_block = _dense_similar (L_0_mat, N)
390+ fill! (rows_block, i)
391+ fill! (cols_block, i+ 1 )
392+ block = _sparse_similar (L_0_mat, rows_block, cols_block, ones (N), n_fourier, n_fourier)
393+ M += kron (block, L_m_mat)
394+ end
395+
396+ # Add subdiagonal blocks (L_p)
397+ for i in 1 : (n_fourier- 1 )
398+ rows_block = _dense_similar (L_0_mat, N)
399+ cols_block = _dense_similar (L_0_mat, N)
400+ fill! (rows_block, i+ 1 )
401+ fill! (cols_block, i)
402+ block = _sparse_similar (L_0_mat, rows_block, cols_block, ones (N), n_fourier, n_fourier)
403+ M += kron (block, L_p_mat)
404+ end
405+
406+ # Add diagonal blocks (L - i*ωd*n)
379407 for i in 1 : n_fourier
380408 n = n_list[i]
381- M += kron (sparse ([i], [i], one (T), n_fourier, n_fourier), L - 1im * ωd * n * I)
409+ block_diag = L - 1im * ωd * n * I
410+ rows_block = _dense_similar (L_0_mat, N)
411+ cols_block = _dense_similar (L_0_mat, N)
412+ fill! (rows_block, i)
413+ fill! (cols_block, i)
414+ block = _sparse_similar (L_0_mat, rows_block, cols_block, ones (N), n_fourier, n_fourier)
415+ M += kron (block, block_diag)
382416 end
383417
384- v0 = zeros (T, n_fourier * N)
385- v0[n_max* N+ 1 ] = weight
418+ v0 = _dense_similar (L_0_mat, n_fourier * N)
419+ fill! (v0, 0 )
420+ allowed_setindex! (v0, weight, n_max * N + 1 )
386421
387- (haskey (kwargs, :Pl ) || haskey (kwargs, :Pr )) && error (" The use of preconditioners must be defined in the solver." )
388422 if ! isnothing (solver. Pl)
389423 kwargs = merge ((; kwargs... ), (Pl = solver. Pl (M),))
390424 elseif isa (M, SparseMatrixCSC)
391425 kwargs = merge ((; kwargs... ), (Pl = ilu (M, τ = 0.01 ),))
392426 end
393427 ! isnothing (solver. Pr) && (kwargs = merge ((; kwargs... ), (Pr = solver. Pr (M),)))
394- ! haskey (kwargs, :abstol ) && (kwargs = merge ((; kwargs... ), (abstol = tol,)))
395- ! haskey (kwargs, :reltol ) && (kwargs = merge ((; kwargs... ), (reltol = tol,)))
428+ kwargs = merge ((abstol = tol, reltol = tol), kwargs)
396429
397430 prob = LinearProblem (M, v0)
398431 ρtot = solve (prob, solver. alg; kwargs... ). u
0 commit comments