fpex0.example.exampleSetup

  1import importlib.resources as resources
  2import json
  3import numpy as np
  4import sympy
  5
  6from fpex0.setup import Setup, Parameters, Grid, Integration
  7from fpex0 import FokkerPlanck
  8# from fpex0.default import defaultDiffusionFcn, defaultDriftFcn
  9
 10from fpex0.InitialDistribution import InitialDistribution
 11
 12
 13def exampleSetup():
 14    """
 15    Generates example setup for FPEX0.
 16
 17    ## Takes
 18    No input.
 19
 20    ## Returns
 21    **setup** 
 22    <br> An FPEX0 setup configuration (`fpex0.setup.Setup`) example.
 23    <br>
 24    
 25    ### Comments
 26    Diffusion and drift (their value) is connected to the grid step size! <br>
 27    NOTE: So simulations and fits should be done with identical gridT.
 28    
 29    Initial and final diffusion are described by parameters!  
 30    --> Linear diffusion plus condition of a non-negative diffusion at end time
 31    
 32    Formula: 
 33    >       Diffusion = pD1 + t*(pD2-pD1)/betamax   
 34     
 35    where
 36    >       betamax --> maximum heat rate (maximum time in FP) ,
 37    >       pD1     --> initial diffusion at heat rate (time) 0
 38    >       pD2     --> final diffusion at heat rate (time) betamax
 39
 40    NOTE:  Parameter bounds
 41    Parameter bounds should never be active in the solution. 
 42
 43    Their only role is to limit search space and to ensure that no "invalid" valued are attained.
 44    """
 45    # Generate parameters object
 46    p0_FPdrift       = [  0.1  ,   0.1  ]    # linear drift
 47    p_lb_FPdrift     = [ -5.1  ,  -5.1  ]    # lower bounds
 48    p_ub_FPdrift     = [ 10.1  ,  10.1  ]    # upper bounds
 49    
 50    p0_FPdiffusion   = [  0.2  ,   0.1  ]    # linear diffusion
 51    p_lb_FPdiffusion = [  0.00 ,   0.00 ]    # lower bounds
 52    p_ub_FPdiffusion = [ 10.00 ,  10.00 ]    # upper bounds
 53    
 54    p0_iniDist       = [  2  ,  40  , 135 , 15.0 , 0.1000 ]  # Fraser-Sizuki parameters
 55    p_lb_iniDist     = [  1  ,  15  , 110 ,  0.1 , 1e-5   ]  # lower bounds
 56    p_ub_iniDist     = [  3  , 150  , 150 , 50.0 , 1.00   ]  # upper bounds
 57    
 58    parametersObj  = Parameters(    p0_FPdrift,   p0_FPdiffusion,   p0_iniDist,
 59                                    p_lb_FPdrift, p_lb_FPdiffusion, p_lb_iniDist,
 60                                    p_ub_FPdrift, p_ub_FPdiffusion, p_ub_iniDist  )
 61
 62    # Generate grid 
 63    N        = 1001  # space resolution
 64
 65    betamax  = 20   # maximum heat rate
 66    gridT    = np.linspace( 60,      160,  N )   # x-grid = temperatures
 67    gridTdot = np.linspace(  0,  betamax, 20 )   # t-grid = heating rates
 68    gridObj  = Grid(gridT, gridTdot)
 69
 70    # set the FP functions and the initial distribution
 71    FPdriftFcn     = lambda t,p: FokkerPlanck.defaultDriftFcn(t,p)
 72    FPdiffusionFcn = lambda t,p: FokkerPlanck.defaultDiffusionFcn(t,p,betamax)
 73
 74    # Fraser-Suzuki -> uses functions to describe the support
 75    FraserSuzuki_name   = "Fraser-Suzuki"
 76    FraserSuzuki_params = sympy.symbols("r,h,z,wr,sr")
 77    FraserSuzuki_expr   = sympy.sympify("h * exp( -log(r)/(log(sr)^2) * log(((x-z)*(sr^2-1))/(wr*sr) + 1)^2)")
 78    FraserSuzuki_zeropos = sympy.sympify("z - (wr*sr)/(sr^2-1)")
 79    FraserSuzuki_support = (-np.inf, FraserSuzuki_zeropos)
 80    
 81    FraserSuzuki = InitialDistribution(FraserSuzuki_expr, FraserSuzuki_support, FraserSuzuki_params, name=FraserSuzuki_name)
 82    IniDistFcn     = lambda x,p: FraserSuzuki.f(x,p)
 83
 84    # Setup integrator (using defaults)
 85    integrationObj = Integration()
 86
 87    # generate the setup object
 88    FPEX0setup = Setup(gridObj, parametersObj, integrationObj, FPdriftFcn, FPdiffusionFcn, IniDistFcn)
 89
 90    return FPEX0setup
 91
 92
 93def importExampleMeasurements(FPEX0setup, gridskip: int):
 94    """ 
 95    Imports example measurement into a given setup FPEX0setup.
 96    <br>
 97
 98    ## Takes
 99    **FPEX0setup**
100    <br> An FPEX0 setup configuration (`fpex0.setup.Setup`).
101
102    **gridskip**
103    <br> Use grid with given stepsize (gridskip). 
104    <br> For gridskip=2 for example, only every second grid point is used,
105    whereas gridskip=1 uses whole grid.
106
107    ## Returns
108    **FPEX0setup**
109    <br> The FPEX0setup passed in, now having imported example measurements in parameter FPEX0setup.Measurements.
110    
111    """
112    for rate in ["0.60", "1.25", "2.50", "5.00", "10.00", "20.00"]:
113        
114        with resources.path('fpex0.example', f'ID407-rate_{rate}.json') as path:
115            # using 'with' as resources.path() returns a context manager;
116            # that context manager provides an os-path
117            # --> path is now an os-path
118            print(f"    {path}")
119            with open(path) as file:
120                # use again a context manager to let open() care about closing the file
121                data = json.load(file)
122                FPEX0setup.importMeasurements(data["T"], data["latentdata"], data["rate"], data["ID"], gridskip)
123
124    return FPEX0setup
def exampleSetup():
14def exampleSetup():
15    """
16    Generates example setup for FPEX0.
17
18    ## Takes
19    No input.
20
21    ## Returns
22    **setup** 
23    <br> An FPEX0 setup configuration (`fpex0.setup.Setup`) example.
24    <br>
25    
26    ### Comments
27    Diffusion and drift (their value) is connected to the grid step size! <br>
28    NOTE: So simulations and fits should be done with identical gridT.
29    
30    Initial and final diffusion are described by parameters!  
31    --> Linear diffusion plus condition of a non-negative diffusion at end time
32    
33    Formula: 
34    >       Diffusion = pD1 + t*(pD2-pD1)/betamax   
35     
36    where
37    >       betamax --> maximum heat rate (maximum time in FP) ,
38    >       pD1     --> initial diffusion at heat rate (time) 0
39    >       pD2     --> final diffusion at heat rate (time) betamax
40
41    NOTE:  Parameter bounds
42    Parameter bounds should never be active in the solution. 
43
44    Their only role is to limit search space and to ensure that no "invalid" valued are attained.
45    """
46    # Generate parameters object
47    p0_FPdrift       = [  0.1  ,   0.1  ]    # linear drift
48    p_lb_FPdrift     = [ -5.1  ,  -5.1  ]    # lower bounds
49    p_ub_FPdrift     = [ 10.1  ,  10.1  ]    # upper bounds
50    
51    p0_FPdiffusion   = [  0.2  ,   0.1  ]    # linear diffusion
52    p_lb_FPdiffusion = [  0.00 ,   0.00 ]    # lower bounds
53    p_ub_FPdiffusion = [ 10.00 ,  10.00 ]    # upper bounds
54    
55    p0_iniDist       = [  2  ,  40  , 135 , 15.0 , 0.1000 ]  # Fraser-Sizuki parameters
56    p_lb_iniDist     = [  1  ,  15  , 110 ,  0.1 , 1e-5   ]  # lower bounds
57    p_ub_iniDist     = [  3  , 150  , 150 , 50.0 , 1.00   ]  # upper bounds
58    
59    parametersObj  = Parameters(    p0_FPdrift,   p0_FPdiffusion,   p0_iniDist,
60                                    p_lb_FPdrift, p_lb_FPdiffusion, p_lb_iniDist,
61                                    p_ub_FPdrift, p_ub_FPdiffusion, p_ub_iniDist  )
62
63    # Generate grid 
64    N        = 1001  # space resolution
65
66    betamax  = 20   # maximum heat rate
67    gridT    = np.linspace( 60,      160,  N )   # x-grid = temperatures
68    gridTdot = np.linspace(  0,  betamax, 20 )   # t-grid = heating rates
69    gridObj  = Grid(gridT, gridTdot)
70
71    # set the FP functions and the initial distribution
72    FPdriftFcn     = lambda t,p: FokkerPlanck.defaultDriftFcn(t,p)
73    FPdiffusionFcn = lambda t,p: FokkerPlanck.defaultDiffusionFcn(t,p,betamax)
74
75    # Fraser-Suzuki -> uses functions to describe the support
76    FraserSuzuki_name   = "Fraser-Suzuki"
77    FraserSuzuki_params = sympy.symbols("r,h,z,wr,sr")
78    FraserSuzuki_expr   = sympy.sympify("h * exp( -log(r)/(log(sr)^2) * log(((x-z)*(sr^2-1))/(wr*sr) + 1)^2)")
79    FraserSuzuki_zeropos = sympy.sympify("z - (wr*sr)/(sr^2-1)")
80    FraserSuzuki_support = (-np.inf, FraserSuzuki_zeropos)
81    
82    FraserSuzuki = InitialDistribution(FraserSuzuki_expr, FraserSuzuki_support, FraserSuzuki_params, name=FraserSuzuki_name)
83    IniDistFcn     = lambda x,p: FraserSuzuki.f(x,p)
84
85    # Setup integrator (using defaults)
86    integrationObj = Integration()
87
88    # generate the setup object
89    FPEX0setup = Setup(gridObj, parametersObj, integrationObj, FPdriftFcn, FPdiffusionFcn, IniDistFcn)
90
91    return FPEX0setup

Generates example setup for FPEX0.

Takes

No input.

Returns

setup
An FPEX0 setup configuration (fpex0.setup.Setup) example.

Comments

Diffusion and drift (their value) is connected to the grid step size!
NOTE: So simulations and fits should be done with identical gridT.

Initial and final diffusion are described by parameters!
--> Linear diffusion plus condition of a non-negative diffusion at end time

Formula:

  Diffusion = pD1 + t*(pD2-pD1)/betamax

where

  betamax --> maximum heat rate (maximum time in FP) ,
  pD1     --> initial diffusion at heat rate (time) 0
  pD2     --> final diffusion at heat rate (time) betamax

NOTE: Parameter bounds Parameter bounds should never be active in the solution.

Their only role is to limit search space and to ensure that no "invalid" valued are attained.

def importExampleMeasurements(FPEX0setup, gridskip: int):
 94def importExampleMeasurements(FPEX0setup, gridskip: int):
 95    """ 
 96    Imports example measurement into a given setup FPEX0setup.
 97    <br>
 98
 99    ## Takes
100    **FPEX0setup**
101    <br> An FPEX0 setup configuration (`fpex0.setup.Setup`).
102
103    **gridskip**
104    <br> Use grid with given stepsize (gridskip). 
105    <br> For gridskip=2 for example, only every second grid point is used,
106    whereas gridskip=1 uses whole grid.
107
108    ## Returns
109    **FPEX0setup**
110    <br> The FPEX0setup passed in, now having imported example measurements in parameter FPEX0setup.Measurements.
111    
112    """
113    for rate in ["0.60", "1.25", "2.50", "5.00", "10.00", "20.00"]:
114        
115        with resources.path('fpex0.example', f'ID407-rate_{rate}.json') as path:
116            # using 'with' as resources.path() returns a context manager;
117            # that context manager provides an os-path
118            # --> path is now an os-path
119            print(f"    {path}")
120            with open(path) as file:
121                # use again a context manager to let open() care about closing the file
122                data = json.load(file)
123                FPEX0setup.importMeasurements(data["T"], data["latentdata"], data["rate"], data["ID"], gridskip)
124
125    return FPEX0setup

Imports example measurement into a given setup FPEX0setup.

Takes

FPEX0setup
An FPEX0 setup configuration (fpex0.setup.Setup).

gridskip
Use grid with given stepsize (gridskip).
For gridskip=2 for example, only every second grid point is used, whereas gridskip=1 uses whole grid.

Returns

FPEX0setup
The FPEX0setup passed in, now having imported example measurements in parameter FPEX0setup.Measurements.