#!/usr/bin/env python3
"""Floating-point isotropic interface reference for a cubic flat three-torus."""
import argparse
import json
import math
from pathlib import Path

SOURCE='https://github.com/openai/math/blob/fd4aeeb2ee4fc729c18d98444fed42fd0529eeeb/preprints/The-Isoperimetric-Conjecture-for-the-Cubic-Flat-Three-Torus-September-24-2026/article.pdf'


def reference(volume_fraction,side_length=1.0):
    for value,name in ((volume_fraction,'volume_fraction'),(side_length,'side_length')):
        if isinstance(value,bool) or not isinstance(value,(int,float)) or not math.isfinite(value):
            raise ValueError(f'{name} must be finite')
    if not 0<=volume_fraction<=1 or side_length<=0:raise ValueError('Require volume fraction in [0,1] and a positive side length')
    fraction=min(volume_fraction,1-volume_fraction)
    area_scale=side_length*side_length
    if not math.isfinite(area_scale):raise ValueError('Area scale overflows floating point')
    v1,v2=4*math.pi/81,1/math.pi
    candidates=dict(ball=(36*math.pi)**(1/3)*fraction**(2/3),tube=2*math.sqrt(math.pi*fraction),slab=2.0)
    if fraction==0:
        minimum=0.0;phase=['empty_or_full']
    else:
        minimum=min(candidates.values())
        # Equality recognition is numerical, not an exact symbolic certificate.
        phase=[name for name,area in candidates.items() if math.isclose(area,minimum,rel_tol=1e-12,abs_tol=1e-15)]
    return dict(volume_fraction=volume_fraction,side_length=side_length,
                reduced_volume_fraction=fraction,complement=volume_fraction>0.5,
                model_minimum_area=minimum*area_scale,candidate_areas={k:v*area_scale for k,v in candidates.items()},
                candidate_minimizer_types=phase,transition_volume_fractions=[v1,v2],
                model='flat cubic periodic cell with isotropic continuum perimeter and fixed volume',
                numeric_evidence='floating_point_formula_evaluation',independent_theorem_verification='not_run',
                source_url=SOURCE,
                limitations=['Not a rectangular-cell profile','No anisotropic tension or elastic energy','No diffuse-interface or voxel area certification'])


def main():
    parser=argparse.ArgumentParser(description=__doc__)
    parser.add_argument('--volume-fraction',type=float,required=True)
    parser.add_argument('--side-length',type=float,default=1.0)
    parser.add_argument('--output',type=Path)
    args=parser.parse_args();body=json.dumps(reference(args.volume_fraction,args.side_length),indent=2,allow_nan=False)+'\n'
    if args.output:
        with args.output.open('x') as stream:stream.write(body)
    else:print(body,end='')


if __name__=='__main__':main()
