-
Notifications
You must be signed in to change notification settings - Fork 1
/
norofrenkel.py
652 lines (532 loc) · 16.5 KB
/
norofrenkel.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
"""
Noro-Frenkel pair potential analysis (:mod:`analphipy.norofrenkel`)
===================================================================
A collection of routines to analyze pair potentials using Noro-Frenkel analysis.
References
----------
.. [1] {ref_Noro_Frenkel}
.. [2] {ref_Barker_Henderson}
.. [3] {ref_WCA}
"""
from __future__ import annotations
from typing import TYPE_CHECKING, cast
import numpy as np
from module_utilities import cached
from ._docstrings import docfiller
from .measures import secondvirial, secondvirial_dbeta, secondvirial_sw
from .utils import TWO_PI, add_quad_kws, is_float, minimize_phi, quad_segments
if TYPE_CHECKING:
from typing import Any, Mapping, Sequence
from typing_extensions import Self
from analphipy.base_potential import PhiAbstract
from ._typing import (
Array,
ArrayLike,
Float_or_Array,
Float_or_ArrayLike,
Phi_Signature,
QuadSegments,
)
# Hack to document module level docstring
__doc__ = __doc__.format(**docfiller.data)
__all__ = [
"sig_nf",
"sig_nf_dbeta",
"lam_nf",
"lam_nf_dbeta",
"NoroFrenkelPair",
]
d = docfiller.update(
summary="Noro-Frenkel/Barker-Henderson effective hard sphere diameter.",
extended_summary=r"""
This is calculated using the formula [1]_ [2]_
.. math::
\sigma_{{\rm BH}}(\beta) = \int_0^{{\infty}} dr \left( 1 - \exp[-\beta \phi_{{\rm rep}}(r)]\right)
where :math:`\phi_{{\rm rep}}(r)` is the repulsive part of the potential [3]_.
""",
).dedent()
@d.decorate
def sig_nf(
phi_rep: Phi_Signature,
beta: float,
segments: ArrayLike,
err: bool = False,
full_output: bool = False,
**kws: Any,
) -> QuadSegments:
r"""
{summary}
{extended_summary}
Parameters
----------
{phi_rep}
{beta}
{segments}
phi_rep : callable
Repulsive part of pair potential.
beta : float
Inverse temperature.
segments : array-like
Integration segments.
err : bool, default=False
If True, return error value.
full_output : bool, default=True
If True, return full_output.
Returns
-------
sig_nf : float or list of float
Value of integral.
errors : float or list of float, optional
If `err` or `full_output` are True, then return sum of errors.
outputs : object
Output from :func:`scipy.integrate.quad`. If multiple segments, return a list of output.
See Also
--------
~analphipy.utils.quad_segments
"""
def integrand(r: Float_or_Array) -> Array:
out: Array = 1.0 - np.exp(-beta * phi_rep(r))
return out
return quad_segments(
integrand,
segments=segments,
sum_integrals=True,
sum_errors=True,
err=err,
full_output=full_output,
**kws,
)
d = docfiller.update(
summary="Derivative with respect to inverse temperature ``beta`` of ``sig_nf``.",
extended_summary=r"""
See refs [1]_ [2]_ [3]_
.. math::
\frac{d \sigma_{\rm BH}}{d\beta} = \int_0^{\infty} dr \phi_{\rm rep}(r) \exp[-\beta \phi_{\rm rep}(r)]
""",
).dedent()
# @doc_inherit(sig_nf, style="numpy_with_merge")
@d(sig_nf)
def sig_nf_dbeta(
phi_rep: Phi_Signature,
beta: float,
segments: ArrayLike,
err: bool = False,
full_output: bool = False,
**kws: Any,
) -> QuadSegments:
def integrand(r: Float_or_Array) -> Array:
v = phi_rep(r)
if np.isinf(v):
out = np.array(0.0)
else:
out = v * np.exp(-beta * v)
return cast("Array", out)
return quad_segments(
integrand,
segments=segments,
sum_integrals=True,
sum_errors=True,
err=err,
full_output=full_output,
**kws,
)
@docfiller.decorate
def lam_nf(beta: float, sig: float, eps: float, B2: float) -> float:
r"""
Noro-Frenkel effective lambda parameter
This is the value of :math:`\lambda` in a square well potential which matches second virial
coefficients. The square well fluid is defined as [1]_
.. math::
\phi_{{\rm sw}}(r) =
\begin{{cases}}
\infty & r \leq \sigma \\
\epsilon & \sigma < r \leq \lambda \sigma \\
0 & r > \lambda \sigma
\end{{cases}}
Parameters
----------
{beta}
sig : float
Particle diameter.
eps : float
Energy parameter in square well potential. The convention is that ``eps`` is the same as the value of ``phi`` at the minimum.
B2 : float
Second virial coefficient to match.
Returns
-------
lam_nf : float
Value of `lambda` in an effective square well fluid which matches ``B2``.
"""
B2star = B2 / (TWO_PI / 3.0 * sig**3)
# B2s_SW = 1 + (1-exp(-beta epsilon)) * (lambda**3 - 1)
# = B2s
lam: float = ((B2star - 1.0) / (1.0 - np.exp(-beta * eps)) + 1.0) ** (1.0 / 3.0)
return lam
@docfiller.decorate
def lam_nf_dbeta(
beta: float,
sig: float,
eps: float,
lam: float,
B2: float,
B2_dbeta: float,
sig_dbeta: float,
) -> float:
"""
Calculate derivative of ``lam_nf`` with respect to ``beta``.
Parameters
----------
{beta}
sig : float
Particle diameter.
eps : float
Energy parameter in square well potential. The convention is that ``eps`` is the same as the value of ``phi`` at the minimum.
B2 : float
Second virial coefficient to match.
lam : float
Value from :func:`analphipy.norofrenkel.lam_nf`.
B2_dbeta : float
d(B2)/d(beta) at ``beta``
sig_dbeta : float
derivative of Noro-Frenkel sigma w.r.t inverse temperature at `beta`
Returns
-------
lam_nf_dbeta : float
Value of ``d(lam_nf)/d(beta)``
See Also
--------
lam_nf
"""
B2_hs = TWO_PI / 3.0 * sig**3
dB2stardbeta = 1.0 / B2_hs * (B2_dbeta - B2 * 3.0 * sig_dbeta / sig)
# e = np.exp(-beta * eps)
# f = 1. - e
e = np.exp(beta * eps)
out: float = (
1.0 / (3 * lam**2 * (e - 1)) * (dB2stardbeta * e - eps * (lam**3 - 1))
)
return out
@docfiller.decorate
class NoroFrenkelPair:
"""
Class to calculate Noro-Frenkel parameters.
See [1]_ [2]_ [3]_
Parameters
----------
{phi}
{segments}
{r_min_exact}
{phi_min_exact}
{quad_kws}
"""
def __init__(
self,
phi: Phi_Signature,
segments: ArrayLike,
r_min: float,
phi_min: Float_or_Array | None,
quad_kws: Mapping[str, Any] | None = None,
):
self.phi = phi
self.r_min = r_min
if phi_min is None:
phi_min = phi(r_min)
self.phi_min = phi_min
self.segments = segments
if quad_kws is None:
quad_kws = {}
self.quad_kws = quad_kws
self._cache: dict[str, Any] = {}
def __repr__(self) -> str:
params = ",\n ".join(
[
f"{v}={getattr(self, v)}"
for v in ["phi", "segments", "r_min", "phi_min", "quad_kws"]
]
)
return f"{type(self).__name__}({params})"
@docfiller.decorate
def phi_rep(self, r: Float_or_ArrayLike) -> Array:
"""
Repulsive part of potential.
This is the Weeks-Chandler-Anderson decomposition.
Parameters
----------
{r}
Returns
-------
output : float or ndarray
Value of ``phi_ref`` at separation(s) ``r``.
"""
r = np.array(r)
phi = np.empty_like(r)
m = r <= self.r_min
phi[m] = self.phi(r[m]) - self.phi_min
phi[~m] = 0.0
return phi
@classmethod
@docfiller.decorate
def from_phi(
cls,
phi: Phi_Signature,
segments: ArrayLike,
r_min: float | None = None,
bounds: ArrayLike | None = None,
quad_kws: Mapping[str, Any] | None = None,
**kws: Any,
) -> Self:
"""
Create object from pair potential function.
Parameters
----------
{phi}
{segments}
r_min : float, optional
Optional guess for numerically finding minimum in `phi`.
bounds : array-like, optional
Optional bounds for numerically locating ``r_min``.
quad_kws : mapping, optional
Optional arguments to :func:`analphipy.utils.quad_segments`.
**kws :
Extra arguments to :func:`analphipy.utils.minimize_phi`.
Returns
-------
output : object
instance of calling class
"""
if bounds is None:
bounds = (segments[0], segments[-1])
if bounds[-1] == np.inf and r_min is None:
raise ValueError("if specify infinite bounds, must supply guess")
if r_min is None:
r_min = cast(float, np.mean(bounds))
r_min, phi_min, _ = minimize_phi(phi, r0=r_min, bounds=bounds, **kws)
return cls(
phi=phi, r_min=r_min, phi_min=phi_min, segments=segments, quad_kws=quad_kws
)
@classmethod
def from_phi_class(
cls,
phi: PhiAbstract,
r_min: float | None = None,
bounds: tuple[float, float] | None = None,
quad_kws: dict[str, Any] | None = None,
**kws: Any,
) -> Self:
"""
Create object, trying to use pre computed values for ``r_min``, ``phi_min``.
Parameters
----------
phi : :class:`analphipy.base_potential.PhiAbstract`
r_min : float, optional
Optional guess for numerically finding minimum in `phi`.
bounds : array-like, optional
Optional bounds for numerically locating ``r_min``.
quad_kws : mapping, optional
Optional arguments to :func:`analphipy.utils.quad_segments`.
**kws :
Extra arguments to :func:`analphipy.utils.minimize_phi`.
Returns
-------
output : object
instance of calling class
"""
if (
phi.segments is not None # pyright: ignore
and phi.r_min is not None
and phi.phi_min is not None
):
return cls(
phi=phi.phi,
segments=phi.segments,
r_min=phi.r_min,
phi_min=phi.phi_min,
quad_kws=quad_kws,
)
else:
assert phi.segments is not None
return cls.from_phi(
phi=phi.phi,
segments=phi.segments,
r_min=r_min,
bounds=bounds,
quad_kws=quad_kws,
**kws,
)
@cached.meth
@add_quad_kws
def secondvirial(self, /, beta: float, **kws: Any) -> QuadSegments:
"""
Second virial coefficient.
See Also
--------
~analphipy.measures.secondvirial
"""
return secondvirial(phi=self.phi, beta=beta, segments=self.segments, **kws)
@cached.prop
def _segments_rep(self) -> list[float]:
return [x for x in self.segments if x < self.r_min] + [self.r_min]
@cached.meth
@add_quad_kws
def sig(self, /, beta: float, **kws: Any) -> QuadSegments:
"""
Effective hard sphere diameter.
See Also
--------
~analphipy.norofrenkel.sig_nf
"""
return sig_nf(
self.phi_rep,
beta=beta,
segments=self._segments_rep,
**kws,
)
def eps(self, beta: float, **kws: Any) -> float:
"""
Effective square well epsilon.
This is equal to the minimum in ``phi``.
"""
return cast(float, self.phi_min)
@cached.meth
@add_quad_kws
def lam(self, /, beta: float, **kws: Any) -> float:
"""
Effective square well lambda.
See Also
--------
~analphipy.norofrenkel.lam_nf
"""
sig = self.sig(beta, **kws)
B2 = self.secondvirial(beta, **kws)
if is_float(sig) and is_float(B2):
return lam_nf(
beta=beta,
sig=sig,
eps=self.eps(beta, **kws),
B2=B2,
)
else:
raise ValueError(f"Bad kws={kws}")
@cached.meth
@add_quad_kws
def sw_dict(self, /, beta: float, **kws: Any) -> dict[str, float]:
"""Dictionary view of Noro-Frenkel parameters."""
sig = self.sig(beta, **kws)
eps = self.eps(beta, **kws)
B2 = self.secondvirial(beta, **kws)
if is_float(sig) and is_float(B2):
lam = lam_nf(beta=beta, sig=sig, eps=eps, B2=B2)
return {"sig": sig, "eps": eps, "lam": lam}
else:
raise ValueError(f"Bad kws={kws}")
@cached.meth
@add_quad_kws
def secondvirial_dbeta(self, /, beta: float, **kws: Any) -> QuadSegments:
"""
Derivative of ``secondvirial`` with respect to ``beta``.
See Also
--------
~analphipy.measures.secondvirial_dbeta
"""
return secondvirial_dbeta(
phi=self.phi, beta=beta, segments=self.segments, **kws
)
@cached.meth
@add_quad_kws
def sig_dbeta(self, /, beta: float, **kws: Any) -> QuadSegments:
"""
Derivative of effective hard-sphere diameter with respect to ``beta``.
See Also
--------
~analphipy.norofrenkel.sig_nf_dbeta
"""
return sig_nf_dbeta(self.phi_rep, beta=beta, segments=self.segments, **kws)
@cached.meth
def lam_dbeta(self, /, beta: float, **kws: Any) -> float:
"""
Derivative of effective lambda parameter with respect to ``beta``.
See Also
--------
~analphipy.norofrenkel.lam_nf_dbeta
"""
sig = self.sig(beta, **kws)
eps = self.eps(beta, **kws)
lam = self.lam(beta, **kws)
B2 = self.secondvirial(beta, **kws)
B2_dbeta = self.secondvirial_dbeta(beta, **kws)
sig_dbeta = self.sig_dbeta(beta, **kws)
if (
is_float(sig)
and is_float(lam)
and is_float(B2)
and is_float(B2_dbeta)
and is_float(sig_dbeta)
):
return lam_nf_dbeta(
beta=beta,
sig=sig,
eps=eps,
lam=lam,
B2=B2,
B2_dbeta=B2_dbeta,
sig_dbeta=sig_dbeta,
)
else:
raise ValueError(f"Bad kws={kws}") # pragma: no cover
@cached.meth
def secondvirial_sw(self, /, beta: float, **kws: Any) -> float:
"""
Second virial coefficient of effective square well fluid.
For testing. This should be the same of value from :meth:`secondvirial`
"""
sig = self.sig(beta, **kws)
eps = self.eps(beta, **kws)
lam = self.lam(beta, **kws)
if is_float(sig) and is_float(lam):
return secondvirial_sw(
beta=beta,
sig=sig,
eps=eps,
lam=lam,
)
else:
raise ValueError(f"Bad kws={kws}") # pragma: no cover
def B2(self, beta: float, **kws: Any) -> QuadSegments:
"""Alias to :meth:`secondvirial`."""
return self.secondvirial(beta, **kws)
def B2_dbeta(self, beta: float, **kws: Any) -> QuadSegments:
"""Alias to :meth:`secondvirial_dbeta`."""
return self.secondvirial_dbeta(beta, **kws)
def B2_sw(self, beta: float, **kws: Any) -> QuadSegments:
"""Alias to :meth:`secondvirial_sw`."""
return self.secondvirial_sw(beta, **kws)
def table(
self,
betas: ArrayLike,
props: Sequence[str] | None = None,
key_format: str = "{prop}",
**kws: Any,
) -> dict[str, Any]:
"""
Create a dictionary of outputs for multiple values of inverse temperature ``beta``.
Parameters
----------
betas : array-like
Array of values of inverse temperature ``beta``.
props : sequence of string
Name of methods to access.
key_format : string, default="{prop}"
Each key in the output dictionary will have the value ``key_format.format(prop=prop)``
Returns
-------
output : dict
dictionary of arrays.
"""
if props is None:
props = ["B2", "sig", "eps", "lam"]
table = {"beta": betas}
for prop in props:
f = getattr(self, prop)
key = key_format.format(prop=prop)
table[key] = [f(beta=beta, **kws) for beta in betas]
return table