TheAlgorithms/Python · error · ValueError

Acceptor concentration should be greater than intrinsic conc

Error message

Acceptor concentration should be greater than intrinsic concentration

What it means

Raised by builtin_voltage() when acceptor_conc <= intrinsic_conc while all earlier checks pass (donor_conc > 0, acceptor_conc > 0, intrinsic_conc > 0, donor_conc > intrinsic_conc). The formula requires Na > ni so the junction is meaningfully doped on both sides; equality also triggers it. Final gate before the Boltzmann/log computation.

Source

Thrown at electronics/builtin_voltage.py:52

    ValueError: Donor concentration should be greater than intrinsic concentration
    >>> builtin_voltage(donor_conc=3000, acceptor_conc=1000, intrinsic_conc=2000)
    Traceback (most recent call last):
      ...
    ValueError: Acceptor concentration should be greater than intrinsic concentration
    """

    if donor_conc <= 0:
        raise ValueError("Donor concentration should be positive")
    elif acceptor_conc <= 0:
        raise ValueError("Acceptor concentration should be positive")
    elif intrinsic_conc <= 0:
        raise ValueError("Intrinsic concentration should be positive")
    elif donor_conc <= intrinsic_conc:
        raise ValueError(
            "Donor concentration should be greater than intrinsic concentration"
        )
    elif acceptor_conc <= intrinsic_conc:
        raise ValueError(
            "Acceptor concentration should be greater than intrinsic concentration"
        )
    else:
        return (
            Boltzmann
            * T
            * log((donor_conc * acceptor_conc) / intrinsic_conc**2)
            / physical_constants["electron volt"][0]
        )


if __name__ == "__main__":
    import doctest

    doctest.testmod()

View on GitHub (pinned to f5988cc097)

Solutions

  1. Verify argument order and values: donor_conc first, acceptor_conc second, both above ni.
  2. Align units across all three concentrations (cm^-3 vs m^-3 mismatch is the classic cause).
  3. If modeling a real lightly-doped junction, use the full depletion approximation equations instead of this simplified check.

Example fix

# before
builtin_voltage(donor_conc=3000, acceptor_conc=1000, intrinsic_conc=2000)  # ValueError

# after
builtin_voltage(donor_conc=1e16, acceptor_conc=1e15, intrinsic_conc=1e10)
Defensive patterns

Strategy: validation

Validate before calling

def valid_junction(nd: float, na: float, ni: float) -> bool:
    return nd > ni > 0 and na > ni

Prevention

When it happens

Trigger: Calling builtin_voltage(donor_conc=3000, acceptor_conc=1000, intrinsic_conc=2000) as in the doctest; any input where 0 < donor_conc, intrinsic_conc < donor_conc, and acceptor_conc <= intrinsic_conc.

Common situations: Swapped donor/acceptor arguments (Na value passed as Nd and vice versa can make one side fail the ni comparison); unit mismatch between Na and ni; lightly doped p-region near intrinsic level.

Related errors


AI-assisted analysis of TheAlgorithms/Python@f5988cc097 (2026-08-14). Data as JSON: /api/errors/ebe2b7db12d33681. Report an issue: GitHub.