# import isotherms
%run import.ipynb

# import the characterisation module
import pygaps.characterisation as pgc

"""
Explanation: Specific surface area determination
BET surface area
Let's do a calculation of the BET area for these samples. First, make sure the data is imported by running the previous notebook.
End of explanation
"""


isotherm = next(i for i in isotherms_n2_77k if i.material == 'MCM-41')
print(isotherm.material)
results = pgc.area_BET(isotherm, verbose=True)

"""
Explanation: pyGAPS attempts to calculate the applicable BET region on its own by using the
Rouquerol rules. The function is pygaps.characterisation.area_BET, where we
set the verbose parameter to obtain a printed output of results and plots.
End of explanation
"""


results = pgc.area_BET(isotherm, p_limits=(0.05, 0.2), verbose=True)

"""
Explanation: It looks that the correlation is reasonably good. A warning is emitted if this
is not the case. We can also restrict the pressure range manually to see what
difference it would make, by using the limits parameter.
End of explanation
"""


results = []
for isotherm in isotherms_n2_77k:
    results.append((isotherm.material, pgc.area_BET(isotherm)))

[(x, f"{y['area']:.2f}") for (x, y) in results]

"""
Explanation: Now let's do the analysis on all of the nitrogen samples. We'll assume the
framework makes a reasonably accurate choice for the applicable range. The
function returns a dictionary with all the calculated parameters, and we'll
print the BET area from there.
End of explanation
"""


isotherm = next(i for i in isotherms_calorimetry if i.material == 'Takeda 5A')
print(isotherm.material)
results = pgc.area_BET(isotherm, verbose=True)

"""
Explanation: We also have isotherms which were measured with $CO_2$ at room temperature.
While there's no guarantee that the BET method is still applicable with this
adsorbate and temperature, we can still attempt to perform the calculations.
It just happens that the isotherms were recorded on the same Takeda 5A carbon
sample (see $N_2$ results above). Let's see how the $CO_2$ BET surface area
looks in comparison.
End of explanation
"""


isotherm = next(i for i in isotherms_n2_77k if i.material == 'MCM-41')
print(isotherm.material)
results = pgc.area_langmuir(isotherm, verbose=True)

"""
Explanation: The surface area obtained with carbon dioxide is around 740 $m^2$. Compared to
the nitrogen surface area of 1100 $m^2$, it is much smaller. While the checks
implemented did not find anything wrong, this is likely due to interactions
between carbon dioxide and the carbon surface leading to the breakdown of the
BET theory.
While any kind of adsorbate and temperature (below critical) can be used, result
interpretation is left at the discretion of the user. More info can be found in
the documentation of the module.
Langmuir surface area
Another common method of calculating specific surface area relies on fitting the
isotherm with a Langmuir model. This model assumes adsorption occurs on active
surface/pore sites, in a single layer. We use the
pygaps.characterisation.area_langmuir function, which defaults to a region of
0.1-0.9 p/p0 for the fitting (see
documentation for details). We
pass the verbose parameter to display the results automatically.
End of explanation
"""


print(isotherm.material)
results = pgc.area_langmuir(isotherm, p_limits=(0.05, 0.3), verbose=True)

"""
Explanation: The correlation is not very good due to condensation in mesopores of MCM-41,
which is not predicted by the Langmuir model. Due to this, the area calculated
is not realistic. We can manually select a range in the monolayer adsorption
regime for a better fit.
End of explanation
"""


import matplotlib.pyplot as plt

area_langmuir = []
area_langmuir_lim = []
area_bet = []
for isotherm in isotherms_n2_77k:
    area_bet.append(pgc.area_BET(isotherm)['area'])
    area_langmuir.append(pgc.area_langmuir(isotherm)['area'])
    area_langmuir_lim.append(pgc.area_langmuir(isotherm, p_limits=(0.01, 0.3))['area'])

fig, (ax1, ax2) = plt.subplots(1, 2, figsize=(10, 5))
ax1.scatter(area_langmuir, area_bet)
ax2.scatter(area_langmuir_lim, area_bet)

ax1.set_title('BET v. Langmuir area, full range')
ax2.set_title('BET v. Langmuir area, LP range')

ax1.plot([0, 2000], [0, 2000], 'k--')
ax2.plot([0, 2000], [0, 2000], 'k--')

ax1.set_xlim(left=0, right=2000)
ax1.set_ylim(bottom=0, top=2000)
ax2.set_xlim(left=0, right=2000)
ax2.set_ylim(bottom=0, top=2000)

ax1.set_xlabel('Langmuir surface area [m2/g]')
ax1.set_ylabel('BET surface area [m2/g]')
ax2.set_xlabel('Langmuir surface area [m2/g]')
ax2.set_ylabel('BET surface area [m2/g]')


"""
Explanation: The fit is now better and the calculated area is also realistic. Comparing it to
the BET area obtained previously, we see that it is higher by about 150 m2.
Since adsorption is more likely to occur in monolayers until pore condensation,
this method is not really suited for MCM-41. In general the Langmuir surface
area is not as widely applicable as the BET one.
Now let's do the analysis on all of the nitrogen samples and compare the
obtained surface areas with the BET ones.
End of explanation
"""


