MaximeVH / EquivalentCircuits.jl

A julia package to either fit the parameters of a specified equivalent electrical circuit to electrochemical impedance data, or to suggest a plausible circuit configuration for a given set of measurements (either through a comparison of circuits from the literature, or through an evolutionary algorithm approach).
MIT License
22 stars 6 forks source link

How do I add a custom circuit with a CPE element to circuit_evolution() method? #34

Closed Matheus-gif closed 3 months ago

Matheus-gif commented 5 months ago

Greetings everyone,

I understand we can use the file Circuitlibrary.csv as an argument for the circuit_evolution() function. Instead, I'm now trying to use a custom circuit to generate the upcoming circuit tree. Here's my attempt:

guess = Vector{Tuple{String, Vector{Float64}}}([
    ("R1-[C2,R3-[P4,R5]]", [10, 20, 30, 0.9, 10]),
])

@time circuit_evolution(
    measurements, frequencies,
    initial_population = guess,
    generations = 50, 
    population_size = 50,
    terminals = "RCP",
)

The error is: BoundsError: attempt to access 5-element Vector{Float64} at index [6]

How do I proceed? Thank you everyone in advance.

Environment: Julia version: v1.9.2

Steps to Reproduce:

Define the initial population as shown above. Execute the circuit_evolution() function with the specified parameters.

Matheus-gif commented 5 months ago

My bad,

I wasn't setting the initial params correctly, but it seems I'm still not able to set a CPE element in my initial_population argument

guess = Vector{Tuple{String, Vector{Float64}}}([
    ("R1-[P2,R3-[C4,R5]]", [10., [1e-6, 0.9], 10., 1e-6, 10.]),
])

#circuit_library = loadpopulation("Circuitlibrary.csv")

@time circuit_evolution(
    measurements, frequencies,
    initial_population = guess,
    generations = 20, 
    population_size = 10,
    terminals = "RC",
    convergence_threshold=5e-4,
)

MethodError: Cannot convert an object of type Vector{Float64} to an object of type Float64

MaximeVH commented 4 months ago

The issue is that the circuit needs to be encoded properly in order to include a custom library. A library should also include more than one circuit. A possible way to implement what you're requesting is as follows (make sure there is not already a file called ""CircuitLibrary.csv" in your current directory):

using EquivalentCiruciuts

EquivalentCircuits.initialize_circuitlibrary("R1-[P2,R3-[C4,R5]]",[10., [1e-6, 0.9], 10., 1e-6, 10.])
EquivalentCircuits.add_to_circuitlibrary("R1-[P2,R3-[C4,R5]]",[10., [1e-6, 0.9], 10., 1e-6, 10.])

guess =  loadpopulation("CircuitLibrary.csv")
@time circuit_evolution(
    measurements, frequencies,
    initial_population = guess,
    generations = 20, 
    population_size = 10,
    terminals = "RC",
    convergence_threshold=5e-4,
)