  We study the electronic and structural properties of substitutional
impurities of graphenelike nanoporous materials C$_2$N, $tg$-, and
$hg$-C$_3$N$_4$ by means of density functional theory calculations. We consider
four types of impurities; boron substitution on carbon sites (B(C)), carbon
substitution on nitrogen sites (C(N)), nitrogen substitution on carbon sites
(N(C)), and sulfur substitution on nitrogen sites (S(N)). From cohesive energy
calculations, we find that the C(N) and B(C) substitutions are the most
energetically favorable and induce small bond modifications in the vicinity of
the impurity, while the S(N) induces strong lattice distortions. Though all of
the studied impurities induce defect levels inside the band gap of these
materials, their electronic properties are poles apart depending on the
behavior of the impurity as an acceptor or a donor. It is also observed that
acceptor (donor) wavefunctions are composed only of $\sigma$ ($\pi$) orbitals
from the impurity itself and/or neighboring sites. Consequently, acceptor
wavefunctions are directed towards the pores and donor wavefunctions are more
extended throughout the neighboring atoms, a property that could further be
explored to modify the interaction between these materials and adsorbates.
Moreover, impurity properties display a strong site sensitivity and ground
state binding energies ranging from $0.03$ to $1.13$ eV, thus offering an
interesting route for tuning the optical properties of these materials.
Finally, spin-polarized calculations reveal that all impurity configurations
have a magnetic ground state that rises from the spin splitting of the impurity
levels. In a few configurations, more than one impurity level can be found
inside the gap and two of them could potentially be explored as two-level
systems for single-photon emission, following similar proposals recently made
on defect complexes on TMDCs.
