File: testGasStorageHighLevelMpi.py

package info (click to toggle)
stopt 5.5%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 8,772 kB
  • sloc: cpp: 70,373; python: 5,942; makefile: 67; sh: 57
file content (176 lines) | stat: -rw-r--r-- 5,848 bytes parent folder | download | duplicates (3)
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
#!/usr/bin/python3

# Copyright (C) 2016 EDF
# All Rights Reserved
# This code is published under the GNU Lesser General Public License (GNU LGPL)
import numpy as np
import math
import StOptReg as reg
import StOptGrids
import StOptGlobal
import Utils
import Simulators as sim
import Optimizers as opt
import dp.DynamicProgrammingByRegressionDist as dynmpi
import dp.SimulateRegressionControlDist as srtmpi
import unittest
import importlib

accuracyClose = 1e6
accuracyEqual = 0.0001

# valorization of a given gas storage on a  grid
# p_grid             the grid
# p_maxLevelStorage  maximum level
def gasStorage(p_grid, p_maxLevelStorage) :
    
    from mpi4py import MPI
    world = MPI.COMM_WORLD
    # storage
    injectionRateStorage = 60000.
    withdrawalRateStorage = 45000.
    injectionCostStorage = 0.35
    withdrawalCostStorage = 0.35

    maturity = 1.
    nstep = 10

    # define a a time grid
    timeGrid = StOptGrids.OneDimRegularSpaceGrid(0., maturity / nstep, nstep)
    # future values
    futValues = []

    # periodicity factor
    iPeriod = 52

    for i in list(range(nstep + 1)) :
        futValues.append(50. + 20. * math.sin((math.pi * i * iPeriod) / nstep))

    # define the future curve
    futureGrid = Utils.FutureCurve(timeGrid, futValues)

    # one dimensional factors
    nDim = 1
    sigma = np.zeros(nDim) + 0.94
    mr = np.zeros(nDim) + 0.29
    # number of simulations
    nbsimulOpt = 20000

    # no actualization
    rate =0.
    # a backward simulator
    bForward = False

    backSimulator = sim.MeanRevertingSimulator(futureGrid, sigma, mr, rate, maturity, nstep, nbsimulOpt, bForward)
    # optimizer                  
    storage = opt.OptimizeGasStorageMeanReverting(injectionRateStorage, withdrawalRateStorage, injectionCostStorage, withdrawalCostStorage)

    # regressor
    nMesh = 6
    nbMesh = np.zeros(1, dtype = np.int32) + nMesh
    regressor = reg.LocalLinearRegression(nbMesh)
    # final value
    vFunction = Utils.ZeroPayOff()

    # initial values
    initialStock = np.zeros(1) + p_maxLevelStorage
    initialRegime = 0 # only one regime

    # Optimize
    fileToDump = "CondExpGasStorageHLMpi"
    bOneFile = True

    # link the simulations to the optimizer
    storage.setSimulator(backSimulator)
    valueOptimMpi = dynmpi.DynamicProgrammingByRegressionDist(p_grid, storage, regressor, vFunction, initialStock, initialRegime, fileToDump, bOneFile)
    print("valOptimMpi", valueOptimMpi)

    world.barrier()

    nbsimulSim = 40000
    bForward = True
    forSimulator = sim.MeanRevertingSimulator(futureGrid, sigma, mr, rate,maturity, nstep, nbsimulSim, bForward)
    storage.setSimulator(forSimulator)
    valSimuMpi = srtmpi.SimulateRegressionControlDist(p_grid, storage, vFunction, initialStock, initialRegime, fileToDump, bOneFile)
    print("valSimuMpi", valSimuMpi)

    return valueOptimMpi, valSimuMpi
                    
class testGasStorageTest(unittest.TestCase):
           
    def test_simpleStorageMpi(self):
               
        moduleMpi4Py=importlib.util.find_spec('mpi4py')
        if (moduleMpi4Py is not None):
            from mpi4py import MPI
            world = MPI.COMM_WORLD
            # storage
            maxLevelStorage = 90000
            # grid
            nGrid = 10
            lowValues = np.zeros(1)
            step = np.zeros(1) + (maxLevelStorage / nGrid)
            nbStep = np.zeros(1, dtype = np.int32) + nGrid
            grid = StOptGrids.RegularSpaceGrid(lowValues, step, nbStep)
                   
            val = gasStorage(grid, maxLevelStorage)
            
            if world.rank == 0:
                self.assertAlmostEqual(val[0], val[1], None, None, accuracyClose)    
            
            world.barrier()
            
            # grid
            ######
            poly = np.zeros(1, dtype = np.int32) + 1
            gridL = StOptGrids.RegularLegendreGrid(lowValues, step, nbStep, poly)
            
            valLegendre = gasStorage(gridL, maxLevelStorage)
            
            if world.rank == 0:
                self.assertAlmostEqual(valLegendre[0], valLegendre[1], None, None, accuracyClose)
         
    def test_simpleStorageLegendreQuadratic(self):
         
        moduleMpi4Py=importlib.util.find_spec('mpi4py')
        if (moduleMpi4Py is not None):
            from mpi4py import MPI
            world = MPI.COMM_WORLD
            # storage
            maxLevelStorage = 90000
            # grid
            nGrid = 5
            lowValues = np.zeros(1)
            step = np.zeros(1) + (maxLevelStorage / nGrid)
            nbStep = np.zeros(1, dtype = np.int32) + nGrid
            poly = np.zeros(1, dtype = np.int32) + 2
            grid = StOptGrids.RegularLegendreGrid(lowValues, step, nbStep, poly)
             
            val = gasStorage(grid, maxLevelStorage)
            
            if world.rank == 0:
                self.assertAlmostEqual(val[0], val[1], None, None, accuracyClose)
         
    def test_simpleStorageLegendreCubic(self):
         
        moduleMpi4Py=importlib.util.find_spec('mpi4py')
        if (moduleMpi4Py is not None):
            from mpi4py import MPI
            world = MPI.COMM_WORLD
            # storage
            maxLevelStorage = 90000
            # grid
            nGrid = 5
            lowValues = np.zeros(1)
            step = np.zeros(1) + (maxLevelStorage / nGrid)
            nbStep = np.zeros(1, dtype = np.int32) + nGrid
            poly = np.zeros(1, dtype = np.int32) + 3
            grid = StOptGrids.RegularLegendreGrid(lowValues, step, nbStep, poly)
             
            val = gasStorage(grid, maxLevelStorage)
            
            if world.rank == 0:
                self.assertAlmostEqual(val[0], val[1], None, None, accuracyClose)
              
if __name__ == '__main__':
    unittest.main()