Skip to content

Custom generation function - #135

Merged
dilaraabdel merged 5 commits into
masterfrom
feature/custom-generation
Nov 17, 2026
Merged

dilaraabdel merged 5 commits into
masterfrom
feature/custom-generation

Conversation

@Bingjie9978

Copy link
Copy Markdown
Collaborator

What I did:

  • add generation model GenerationCustom, which evaluates a user function customGeneration(node, data) passed as Data(...; customGeneration = f), analogous to customReaction!. As for the other models, the value is multiplied by λ2. The function may depend on time via node.time and can be replaced between solves.
  • add example Ex111: PSC with degradation/recovery reactions written as customReaction! and a time-dependent light ramp using GenerationCustom

@pjaap

pjaap commented Nov 9, 2026

Copy link
Copy Markdown
Member

The matplotlib failure is odd. I think this matplotlib step is not needed any more: #136
We can merge this PR here, if #136 is fine.

Comment thread src/ct_datatypes.jl Outdated
$(TYPEDEF)
Abstract type for a generation rate given by the user function `data.customGeneration`.
"""
abstract type GenerationCustom end

@pjaap pjaap Nov 9, 2026 •

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am not really happy with the name. The name should reflect the intention. What do you think about GenerationByCustomFunction? Or something like that?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think you should be happy now with my changes. We are not in need of GenerationCustom as we already have GenerationData.

@dilaraabdel

Copy link
Copy Markdown
Member

Dear @Bingjie9978, thanks for the changes. I had to time to have a look now. I did following adjustments:

1. No customGeneration needed. GenerationUserDefined already allows a function as generationData, it just wasn't implemented. I removed GenerationCustom and added the function case in src/ct_physics.jl:

user_generation(G::AbstractArray, node, data) = G[node.index]
user_generation(G::Function, node, data) = G(node, data)

The old customGeneration::Function field caused allocations in all examples (e.g. even Ex101 with no generation term).

2. Allocations in Ex111:

a) The bias stored as an arbitrary function (Ref{Function}) allocates. Fix: one fixed function with a phase flag:

phase = Ref(0)   # 0 = dark, 1 = light, 2 = scan
function applied_bias(t)
    if phase[] == 1
        return light_bias(t)
    elseif phase[] == 2
        return scan_bias(t)
    else
        return 0.0
    end
end
contactVoltageFunction = [zeroVoltage, applied_bias]

b) The parameter struct fields have no type, so p.iphip inside the reaction function allocates. Fix: before the function, iphip::Int = p.iphip (same for iphia).

3. I renamed the example to Ex111_PSC_DegradationRecovery, since these are the physics keywords. The test now uses a coarser scan, so it runs much faster.

4. I set vacancyEnergyCalculation = true in equilibrium_solve!. The vacancy energy is a sensitive parameter, so we should also check whether you use the correct value in your other calculations. Let's discuss this together in a couple of days.

@Bingjie9978

Copy link
Copy Markdown
Collaborator Author

Hi @dilaraabdel , thanks! it looks good to me

Bingjie9978 and others added 5 commits November 17, 2026 10:59
The function can now also depend on time (node.time),
e.g. for a time-dependent illumination in a transient solve after
equilibrium_solve!.
Holes ⇌ anion vacancies in the perovskite, written as one customReaction!
function; the light is switched on with a 2 ms Beer-Lambert ramp passed as
userDefinedGeneration, followed by 30 min at 1.1 V
with non-perturbing J-V scans every 300 s. Na = 1e26 m⁻³ and T = 298.15 K are
set in the example; the shared parameter file is unchanged.
@dilaraabdel
dilaraabdel force-pushed the feature/custom-generation branch from e7ec115 to 2d0d665 Compare November 17, 2026 10:04
@pjaap

pjaap commented Nov 17, 2026

Copy link
Copy Markdown
Member

We cleaned up the history and will merge if everything is green :)

@dilaraabdel
dilaraabdel merged commit c969ccc into master Nov 17, 2026
11 checks passed
@dilaraabdel
dilaraabdel deleted the feature/custom-generation branch November 20, 2026 07:36
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

Successfully merging this pull request may close these issues.

3 participants