# IDA solver error (possibly) for a DAE problem with complex number

**URL:** https://discourse.julialang.org/t/ida-solver-error-possibly-for-a-dae-problem-with-complex-number/115045
**Category:** New to Julia
**Tags:** solver
**Created:** [June 1, 2024, 6:45am UTC](https://discourse.julialang.org/t/ida-solver-error-possibly-for-a-dae-problem-with-complex-number/115045 "2024-06-01T06:45:56Z")
**Posts on this page:** 12
**Page:** 1

<div class="post-metadata">

### Author: ![Hamed](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/hamed/32/209654_2.png) [@Hamed](https://discourse.julialang.org/u/Hamed)
#### Post date: [June 1, 2024, 6:45am UTC](https://discourse.julialang.org/t/ida-solver-error-possibly-for-a-dae-problem-with-complex-number/115045/1 "2024-06-01T06:45:56Z")

</div>

Hi there. I have a problem with this code here. I’m trying to simulate a three-phase short circuit fault in a power system according to the instructions in Example 2 here:

[https://github.com/NREL-Sienna/PowerSimulationsDynamics.jl/blob/main/docs/src/perturbations.md](https://github.com/NREL-Sienna/PowerSimulationsDynamics.jl/blob/main/docs/src/perturbations.md)

The power system is also taken from the SIIPExamples repository. The main thing to do for simulating this scenario is using the NetworkSwitch function, which accepts complex numbers as input, and I don’t want to define new method for this function because the numbers are meant to be complex. Anyway, in this scenario, we introduce two perturbations and feed it into a simulation function. My code executes up until the last line. When I attempt to execute the simulation using IDA solver, I get this error:

```julia
Error: Execution failed
│ exception =
│ BoundsError: attempt to access 28×28 SparseMatrixCSC{Float64, Int64} with 257 stored entries at index [785]
│ Stacktrace:
│ [1] throw_boundserror(A::SparseMatrixCSC{Float64, Int64}, I::Tuple{Int64})
│ @ Base ./abstractarray.jl:737
│ [2] checkbounds
│ @ ./abstractarray.jl:702 [inlined]
│ [3] _setindex!
│ @ ./abstractarray.jl:1430 [inlined]
│ [4] setindex!
│ @ ./abstractarray.jl:1396 [inlined]
│ [5] (::PowerSimulationsDynamics.var"#27#28"{NetworkSwitch})

```

which is strange because the apply\_fault and clear\_fault are both 28\*28 matrices, which mean that there should only be 784 cells. I double checked all the matrices and made sure that they all have proper dimensions and the code is still not running.

What was confusing to me was that IDA solver works with non-complex values. But I have to have complex values. I have tried several other solvers but could not find anything that works. I’m a newbie to Julia so I suspect that the problem is with something different and I have no clue. Can you help me?

This is the code:

```julia
using SIIPExamples
using PowerSimulationsDynamics
import PowerSimulationsDynamics as PSD
using PowerSystems
using Sundials
using SparseArrays
using OrdinaryDiffEq

# Load the power system model
file_dir = joinpath(dirname(dirname(pathof(SIIPExamples))), "script", "4_PowerSimulationsDynamics_examples", "Data")
sys = System(joinpath(file_dir, "14bus.raw"), joinpath(file_dir, "dyn_data.dyr"))

# Retrieve the Y-bus matrix and bus mapping
(ybus_matrix, bus_mapping) = PowerSimulationsDynamics._get_ybus(sys)

# Transform ybus_matrix into Complex type
ybus_matrix = SparseMatrixCSC{ComplexF64, Int64}(ybus_matrix)

# Store the original Y-Bus Matrix
original_ybus_matrix = copy(ybus_matrix)

# The original Y-Bus Matrix is already in Complex type
typeof(original_ybus_matrix)

# Modify Y-Bus Matrix for Fault Condition
bus_index = bus_mapping[6]
large_value = 1.0e4 + 0.0im
ybus_matrix[bus_index, bus_index] += large_value

# Create Sparse Matrices for Fault Condition and Clearing Fault
fault_admittance = sparse(ybus_matrix)
clear_fault_matrix = sparse(original_ybus_matrix)

# Define NetworkSwitch Events
time_of_fault = 1.0 # Time when the fault occurs
time_of_clear = 1.1 # Time when the fault is cleared

# fault_admittance = SparseMatrixCSC{Float64, Int64}(fault_admittance)
apply_fault = NetworkSwitch(time_of_fault, fault_admittance)
clear_fault = NetworkSwitch(time_of_clear, clear_fault_matrix)

three_phase_fault = [apply_fault, clear_fault]

# Define and Run Simulation
sim = PSD.Simulation(
    ResidualModel, # Type of model used
    sys, # System
    file_dir, # Path for the simulation output
    (0.0, 10.0), # Time span
    three_phase_fault, # Events
)

# Execute the simulation with IDA
PSD.execute!(sim, IDA(); abstol = 1e-8)

```

---

<div class="post-metadata">

### Author: ![ChrisRackauckas](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/chrisrackauckas/32/77_2.png) [@ChrisRackauckas](https://discourse.julialang.org/u/ChrisRackauckas)
#### Post date: [June 1, 2024, 7:32am UTC](https://discourse.julialang.org/t/ida-solver-error-possibly-for-a-dae-problem-with-complex-number/115045/2 "2024-06-01T07:32:45Z")

</div>

Just split the input to real and complex parts

---

<div class="post-metadata">

### Author: ![Hamed](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/hamed/32/209654_2.png) [@Hamed](https://discourse.julialang.org/u/Hamed)
#### Post date: [June 1, 2024, 7:53am UTC](https://discourse.julialang.org/t/ida-solver-error-possibly-for-a-dae-problem-with-complex-number/115045/3 "2024-06-01T07:53:23Z")

</div>

Inputs to which function? Can you elaborate?

---

<div class="post-metadata">

### Author: ![ChrisRackauckas](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/chrisrackauckas/32/77_2.png) [@ChrisRackauckas](https://discourse.julialang.org/u/ChrisRackauckas)
#### Post date: [June 1, 2024, 8:02am UTC](https://discourse.julialang.org/t/ida-solver-error-possibly-for-a-dae-problem-with-complex-number/115045/4 "2024-06-01T08:02:56Z")

</div>

The initial condition, I. Just write it with reals and combine to complex within f

---

<div class="post-metadata">

### Author: ![Hamed](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/hamed/32/209654_2.png) [@Hamed](https://discourse.julialang.org/u/Hamed)
#### Post date: [June 1, 2024, 8:06am UTC](https://discourse.julialang.org/t/ida-solver-error-possibly-for-a-dae-problem-with-complex-number/115045/5 "2024-06-01T08:06:03Z")

</div>

Well they should eventually be combined for NetworkSwitch function, as this function only supports complex. But I have no problems with the data before this function.

---

<div class="post-metadata">

### Author: ![ChrisRackauckas](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/chrisrackauckas/32/77_2.png) [@ChrisRackauckas](https://discourse.julialang.org/u/ChrisRackauckas)
#### Post date: [June 1, 2024, 9:29am UTC](https://discourse.julialang.org/t/ida-solver-error-possibly-for-a-dae-problem-with-complex-number/115045/6 "2024-06-01T09:29:25Z")

</div>

Yes cool so then if you just pack it it’s solved in 3 lines of code, so it’s at least a solution for now

---

<div class="post-metadata">

### Author: ![Hamed](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/hamed/32/209654_2.png) [@Hamed](https://discourse.julialang.org/u/Hamed)
#### Post date: [June 1, 2024, 10:00am UTC](https://discourse.julialang.org/t/ida-solver-error-possibly-for-a-dae-problem-with-complex-number/115045/7 "2024-06-01T10:00:15Z")

</div>

Sorry but I don’t understand what you’re saying. Can you explain what you mean?

---

<div class="post-metadata">

### Author: ![ChrisRackauckas](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/chrisrackauckas/32/77_2.png) [@ChrisRackauckas](https://discourse.julialang.org/u/ChrisRackauckas)
#### Post date: [June 1, 2024, 11:22am UTC](https://discourse.julialang.org/t/ida-solver-error-possibly-for-a-dae-problem-with-complex-number/115045/8 "2024-06-01T11:22:36Z")

</div>

```julia
function f(u)
  _f(ComplexF64.(u[1,:], u[2,:]))
end

```

Makes f take in real numbers and reformat it for a function \_f that wants complex numbers. It’s just a reinterpretation.

Of course you can make that even faster with reinterpret(Vector{ComplexF64}, u), interpreting it differently though

---

<div class="post-metadata">

### Author: ![Hamed](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/hamed/32/209654_2.png) [@Hamed](https://discourse.julialang.org/u/Hamed)
#### Post date: [June 1, 2024, 5:15pm UTC](https://discourse.julialang.org/t/ida-solver-error-possibly-for-a-dae-problem-with-complex-number/115045/9 "2024-06-01T17:15:21Z")

</div>

Thank you, but I still don’t see the point. I have a simulation scenario with 2 perturbations with complex values. What is the point of separating the two parts? Do you mean I should solve the simulation twice (once for real values and the other time for imaginary)?

Can you tell me what should I do with this f? I would be very happy if you elaborate on your suggestion as I’m confused.

---

<div class="post-metadata">

### Author: ![ChrisRackauckas](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/chrisrackauckas/32/77_2.png) [@ChrisRackauckas](https://discourse.julialang.org/u/ChrisRackauckas)
#### Post date: [June 1, 2024, 6:32pm UTC](https://discourse.julialang.org/t/ida-solver-error-possibly-for-a-dae-problem-with-complex-number/115045/10 "2024-06-01T18:32:45Z")

</div>

That makes it real valued

---

<div class="post-metadata">

### Author: ![Hamed](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/hamed/32/209654_2.png) [@Hamed](https://discourse.julialang.org/u/Hamed)
#### Post date: [June 2, 2024, 2:12am UTC](https://discourse.julialang.org/t/ida-solver-error-possibly-for-a-dae-problem-with-complex-number/115045/11 "2024-06-02T02:12:52Z")

</div>

What makes what real valued? Can you please use longer sentences and be more specific?

---

<div class="post-metadata">

### Author: ![ChrisRackauckas](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/chrisrackauckas/32/77_2.png) [@ChrisRackauckas](https://discourse.julialang.org/u/ChrisRackauckas)
#### Post date: [June 2, 2024, 9:21am UTC](https://discourse.julialang.org/t/ida-solver-error-possibly-for-a-dae-problem-with-complex-number/115045/12 "2024-06-02T09:21:41Z")

</div>

Complex numbers are just two real numbers. Write a function that takes in 2N real numbers, converts them into Complex numbers, does the computation in Complex, and then converts the result back to real.
