Skip to content

Add adapt_to_depletion_and_bias_voltage!#612

Open
hervasa2 wants to merge 1 commit into
JuliaPhysics:mainfrom
hervasa2:adapt-simulation-to-depletion-and-bias
Open

Add adapt_to_depletion_and_bias_voltage!#612
hervasa2 wants to merge 1 commit into
JuliaPhysics:mainfrom
hervasa2:adapt-simulation-to-depletion-and-bias

Conversation

@hervasa2

@hervasa2 hervasa2 commented Jun 22, 2026

Copy link
Copy Markdown
Collaborator

Summary

Added adapt_to_depletion_and_bias_voltage!.

adapt_to_depletion_and_bias_voltage!(sim, dep, bias) adapts a Simulation in place to a target depletion voltage dep and operating (bias) voltage bias by rescaling the impurity density component of the electric potential (f = dep / dep_sim) and swapping in the bias contact potential via superposition — without re-solving the field. Note that the actual sim.detector.semiconductor.impurity_density_model and sim.detector.contacts[id].potential fields are also modified in place to reflect the change.

Tests check that this is performed correctly, and that after adapt_to_depletion_and_bias_voltage! is run on a simulation, a subsequent calculate_electric_potential! returns compatible results.
This is then compared to the manual way a user would have to do things: initializing a fresh simulation, modifying the sim.detector.semiconductor.impurity_density_model and sim.detector.contacts[id].potential fields and then running calculate_electric_potential!.

This function is useful when the user wants to modify the simulation on the fly to reflect real-world conditions: a different measured depletion voltage and changing operating voltages without having to recalculate the electric potential. Note that in this process the weighting potential will be calculated and populated in the simulation.

Note that this only works on depleted simulations. This check is done when estimate_depletion_voltage is called internally.

Changes

  • Export adapt_to_depletion_and_bias_voltage!.
  • Input validation: dep and bias must share the same (non-zero) sign, and abs(bias) > abs(dep) (the detector must be over-depleted at the operating voltage). Both polarities are supported.
  • Docstring describing the impurity-rescaling / superposition mechanism, arguments, keywords, and an example.
  • Tests in test/test_depletion.jl:
    • round-trip: after adapting, the bias contact sits at bias and the re-estimated depletion voltage ≈ dep;
    • an in-place re-solve still matches;
    • a freshly built equivalent simulation (same adapted impurity model + bias) reproduces the same depletion voltage.

Testing

test/test_depletion.jl passes locally (11/11).

@hervasa2 hervasa2 changed the title Export, document, and guard adapt_simulation_to_depletion_and_bias! Add adapt_simulation_to_depletion_and_bias! Jun 22, 2026
@hervasa2 hervasa2 requested a review from fhagemann June 22, 2026 14:24
@hervasa2 hervasa2 added enhancement Improvement of existing features new feature Add new feature labels Jun 22, 2026
@hervasa2 hervasa2 force-pushed the adapt-simulation-to-depletion-and-bias branch from dd43303 to fe61010 Compare June 22, 2026 14:28
@hervasa2 hervasa2 changed the title Add adapt_simulation_to_depletion_and_bias! Add adapt_to_depletion_and_bias_voltage! Jun 22, 2026
…and tests

- Export `adapt_to_depletion_and_bias_voltage!`.
- Add input validation: depletion and operating voltages must share the same
  (non-zero) sign and `abs(op) > abs(dep)` (detector over-depleted at bias).
- Add a docstring describing the impurity-rescaling / superposition mechanism.
- Add tests covering the round-trip (adapt then re-estimate), an in-place
  re-solve, and a comparison against a freshly built equivalent simulation.
@hervasa2 hervasa2 force-pushed the adapt-simulation-to-depletion-and-bias branch from fe61010 to 10e4637 Compare June 22, 2026 14:33
@fhagemann

Copy link
Copy Markdown
Collaborator

Can you let me know once you’re done force pushing for me to look at this PR? 😅

@hervasa2

Copy link
Copy Markdown
Collaborator Author

Sorry Im done. Just some last minute naming changes for consistency

@fhagemann fhagemann left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

This looks like a very useful function and I think it would be a nice addition to SSD.
I have some comments on the name of the function, the requirement of having to pass BOTH a target depletion and bias voltage, the docstring, and error handling in the tests :)


See also [`estimate_depletion_voltage`](@ref).
"""
function adapt_to_depletion_and_bias_voltage!(sim::Simulation{T}, dep::RealQuantity, bias::RealQuantity;

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

I feel like we can probably find a better name to make clear that ONLY the electric potential (but e.g. not the electric field) is updated.

  • Can we avoid the verb adapt to avoid confusion with adapt from Adapt.jl? I know we used adapt for the internal function to map the grids of electric and weighting potentials, but for an exported function I would like to be a bit more careful with wording.
  • It would also be nice to have a method that doesn't expect a bias and just lets the bias remain whatever it was before. Same thing if someone were to just change the bias without planning on changing the depletion voltage. Would it make sense to split this into two functions (one called something like update_electric_potential_to_match_depletion! and the other one update_electric_potential_to_match_bias! -- these are probably not the best names to choose from), which are then called by this overall function that allows to change both if wanted? Not sure what's the best way to go here, so I'm open for suggestions.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

I am in favor of splitting it into update_electric_potential_to_match_depletion! and update_electric_potential_to_match_bias!. But I would not add a third function that calls both. I am happy with the names you chose

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

I'm not sure I like the verb update, because that's usually the verb we use to actually run the SOR, and this is explicitly not doing that, though 😅

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

So if there's any suggestion other than update or adapt, I'd be happy to discuss :)

Comment thread src/Simulation/Depletion.jl
Comment thread test/test_depletion.jl
dep_target = 2000u"V"
bias_target = 2500u"V"
imp_model_before = sim.detector.semiconductor.impurity_density_model
adapt_to_depletion_and_bias_voltage!(sim, dep_target, bias_target, check_for_depletion = false, verbose = false)

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Can you also add tests to show that the errors are thrown correctly?

Comment thread src/Simulation/Depletion.jl
bias::T = _parse_value(T, bias, internal_voltage_unit)
@assert dep * bias > 0 "The depletion voltage ($(dep)$(internal_voltage_unit)) and operating voltage ($(bias)$(internal_voltage_unit)) must have the same (non-zero) sign."
@assert abs(bias) > abs(dep) "The operating voltage ($(bias)$(internal_voltage_unit)) must exceed the depletion voltage ($(dep)$(internal_voltage_unit)) in magnitude (the detector must be over-depleted)."
dep_sim = _parse_value(T, estimate_depletion_voltage(sim; contact_id = contact_id, verbose = verbose, kwargs...), internal_voltage_unit)

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Would it make sense to allow the user to pass the expected depletion voltage also to the function? I can't judge how long estimate_depletion_voltage takes, especially if sim.electric_potential and sim.weighting_potentials[contact.id] are already defined on the same grid.

verbose::Bool = true,
kwargs...) where {T <: AbstractFloat}
if ismissing(sim.weighting_potentials[contact_id]) || sim.weighting_potentials[contact_id].grid != sim.electric_potential.grid
_adapt_weighting_potential_to_electric_potential_grid!(sim, contact_id)

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

In _adapt_weighting_potential_to_electric_potential_grid!, we run update_till_convergence! and essentially change the weighting potential (grid + update till convergence).
This contradicts the docstring saying that it is not re-solving anything. Maybe make this clearer?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement Improvement of existing features new feature Add new feature

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants