Hide keyboard shortcuts

Hot-keys on this page

r m x p   toggle line displays

j k   next/prev highlighted chunk

0   (zero) top of page

1   (one) first highlighted chunk

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

#!/usr/bin/env python 

# -*- coding: utf-8 -*- 

 

"""Tests for `digital_twin` package.""" 

 

import pytest 

import simpy 

import shapely.geometry 

import logging 

import datetime 

import time 

import json 

import numpy as np 

 

import matplotlib 

from matplotlib.testing.decorators import image_comparison 

import matplotlib.pyplot as plt 

 

from click.testing import CliRunner 

 

from digital_twin import core 

from digital_twin import plot 

from digital_twin import cli 

 

logger = logging.getLogger(__name__) 

 

class BasicStorageUnit(core.HasContainer, core.HasResource, core.Locatable, core.Log): 

def __init__(self, *args, **kwargs): 

super().__init__(*args, **kwargs) 

 

@pytest.fixture 

def env(): 

simulation_start = datetime.datetime(2019, 1, 1) 

my_env = simpy.Environment(initial_time = time.mktime(simulation_start.timetuple())) 

my_env.epoch = time.mktime(simulation_start.timetuple()) 

return my_env 

 

 

@pytest.fixture 

def geometry_a(): 

return shapely.geometry.Point(0, 0) 

 

 

@pytest.fixture 

def geometry_b(): 

return shapely.geometry.Point(1, 1) 

 

 

@pytest.fixture 

def locatable_a(geometry_a): 

return core.Locatable(geometry_a) 

 

 

@pytest.fixture 

def locatable_b(geometry_b): 

return core.Locatable(geometry_b) 

 

 

@pytest.fixture 

def energy_use_sailing(): 

return lambda x: x * 2 

 

 

@pytest.fixture 

def energy_use_loading(): 

return lambda x: x * 4 

 

 

@pytest.fixture 

def energy_use_unloading(): 

return lambda x: x * 3 

 

 

# Test energy use sailing 

def test_movable(env, geometry_a, locatable_a, locatable_b, 

energy_use_sailing, energy_use_loading, energy_use_unloading): 

 

mover = type("Mover", (core.Movable, core.EnergyUse, core.Log), {}) 

 

data_mover = {"v": 10, "geometry":geometry_a, "env": env, 

"energy_use_sailing": energy_use_sailing, 

"energy_use_loading": energy_use_loading, 

"energy_use_unloading": energy_use_unloading} 

 

mover = mover(**data_mover) 

 

# Moving from a to b - energy use should be equal to duration * 2 

start = env.now 

env.process(mover.move(locatable_b)) 

env.run() 

np.testing.assert_almost_equal(mover.log["Value"][-1], (env.now - start) * 2) 

 

# Moving from b to a - energy use should be equal to duration * 2 

start = env.now 

env.process(mover.move(locatable_a)) 

env.run() 

np.testing.assert_almost_equal(mover.log["Value"][-1], (env.now - start) * 2) 

 

 

# Test energy use processing 

def test_processor(env, geometry_a, energy_use_sailing, energy_use_loading, energy_use_unloading): 

 

source = BasicStorageUnit(env=env, geometry = geometry_a, capacity=1000, level=1000, nr_resources=1) 

dest = BasicStorageUnit(env=env, geometry = geometry_a, capacity=1000, level=0, nr_resources=1) 

 

processor = type("processor", (core.Processor, core.EnergyUse, core.Locatable, core.Log), {}) 

 

data_processor = {"env": env, 

"unloading_func": (lambda x: x / 2), 

"loading_func": (lambda x: x / 2), 

"geometry": geometry_a, 

"energy_use_sailing": energy_use_sailing, 

"energy_use_loading": energy_use_loading, 

"energy_use_unloading": energy_use_unloading} 

 

processor = processor(**data_processor) 

processor.rate = processor.loading_func 

 

# Log fuel use of the processor in step 1 

start = env.now 

env.process(processor.process(source, dest, 600)) 

env.run() 

 

np.testing.assert_almost_equal(processor.log["Value"][-1], (env.now - start) * 4) 

 

# Log fuel use of the processor in step 2 

start = env.now 

env.process(processor.process(dest, source, 300)) 

env.run() 

 

np.testing.assert_almost_equal(processor.log["Value"][-1], (env.now - start) * 4) 

 

 

# Test energy use of a TransportProcessingResource 

def test_TransportProcessingResource(env, geometry_a, geometry_b, locatable_a, locatable_b, 

energy_use_sailing, energy_use_loading, energy_use_unloading): 

 

source = BasicStorageUnit(env=env, geometry = geometry_a, capacity=1000, level=1000, nr_resources=1) 

dest = BasicStorageUnit(env=env, geometry = geometry_b, capacity=1000, level=0, nr_resources=1) 

 

# The generic class for an object that can move and transport (a TSHD for example) 

TransportProcessingResource = type('TransportProcessingResource', 

(core.Identifiable, # Give it a name 

core.Log, # Allow logging of all discrete events 

core.ContainerDependentMovable, # A moving container, so capacity and location 

core.Processor, # Allow for loading and unloading 

core.HasResource, # Allow queueing 

core.EnergyUse), # Allow logging energy use 

{}) 

 

# TSHD variables 

data_hopper = {"env": env, # The simpy environment  

"name": "Hopper", # Name 

"geometry": geometry_a, # It starts at the "from site" 

"unloading_func": (lambda x: x / 1), # Unloading rate  

"loading_func": (lambda x: x / 2), # Loading rate 

"capacity": 5_000, # Capacity of the hopper 

"compute_v": lambda x: 1, # Variable speed  

"energy_use_loading": energy_use_loading, # Variable fuel use 

"energy_use_sailing": energy_use_sailing, # Variable fuel use 

"energy_use_unloading": energy_use_unloading} # Variable fuel use 

 

# The simulation object 

hopper = TransportProcessingResource(**data_hopper) 

 

# Simulation starts with loading 

hopper.rate = hopper.loading_func 

start = env.now 

env.process(hopper.process(source, hopper, 500)) 

env.run() 

 

# Duration should be amount / 2 

# Energy use duration * 4 

np.testing.assert_almost_equal(hopper.log["Value"][-2], (env.now - start) * 4) 

 

 

# Simulation continues with moving from A to B 

start = env.now 

env.process(hopper.move(locatable_b)) 

env.run() 

 

np.testing.assert_almost_equal(np.ceil(hopper.log["Value"][-1]), np.ceil((env.now - start)) * 2) 

 

 

# Simulation ends with unloading 

hopper.rate = hopper.unloading_func 

start = env.now 

env.process(hopper.process(hopper, dest, 500)) 

env.run() 

 

np.testing.assert_almost_equal(hopper.log["Value"][-2], (env.now - start) * 3) 

 

 

# Test energy use of a Processor and ContainerDependentMovable 

def test_Processor_ContainerDependentMovable(env, geometry_a, geometry_b, locatable_a, locatable_b, 

energy_use_sailing, energy_use_loading, energy_use_unloading): 

 

source = BasicStorageUnit(env=env, geometry = geometry_a, capacity=1000, level=1000, nr_resources=1) 

dest = BasicStorageUnit(env=env, geometry = geometry_b, capacity=1000, level=0, nr_resources=1) 

 

# The generic class for an object that can process (a quay crane for example) 

ProcessingResource = type('ProcessingResource', 

(core.Identifiable, # Give it a name 

core.Locatable, # Allow logging of location 

core.Log, # Allow logging of all discrete events 

core.Processor, # Allow for loading and unloading 

core.HasResource, # Add information on serving equipment 

core.EnergyUse), # Add information on fuel 

{}) 

 

processor_1 = {"env": env, # The simpy environment 

"name": "Processor 1", # Name 

"geometry": geometry_a, # It is located at location A 

"unloading_func": (lambda x: x / 1), # Unloading rate  

"loading_func": (lambda x: x / 2), # Loading rate 

"energy_use_loading": energy_use_loading, # Variable fuel use 

"energy_use_sailing": energy_use_sailing, # Variable fuel use 

"energy_use_unloading": energy_use_loading} # Variable fuel use 

processor_2 = {"env": env, # The simpy environment 

"name": "Processor 2", # Name 

"geometry": geometry_b, # It is located at location B 

"unloading_func": (lambda x: x / 1), # Unloading rate  

"loading_func": (lambda x: x / 2), # Loading rate 

"energy_use_loading": energy_use_unloading, # Variable fuel use 

"energy_use_sailing": energy_use_sailing, # Variable fuel use 

"energy_use_unloading": energy_use_unloading} # Variable fuel use 

 

# The generic class for an object that can move an amount (a containervessel) 

mover = type("Mover", 

(core.ContainerDependentMovable, 

core.EnergyUse, 

core.HasResource, 

core.Log), 

{}) 

 

data_mover = {"compute_v": lambda x: 1, 

"geometry":geometry_a, 

"env": env, 

"capacity": 1000, 

"energy_use_sailing": energy_use_sailing, 

"energy_use_loading": energy_use_loading, 

"energy_use_unloading": energy_use_unloading} 

 

# The simulation objects 

processor_1 = ProcessingResource(**processor_1) 

processor_2 = ProcessingResource(**processor_2) 

containervessel = mover(**data_mover) 

 

# Simulation starts with loading 

processor_1.rate = processor_1.loading_func 

start = env.now 

env.process(processor_1.process(source, containervessel, 500)) 

env.run() 

 

np.testing.assert_almost_equal(processor_1.log["Value"][-1], (env.now - start) * 4) 

 

 

# Simulation continues with moving from A to B 

start = env.now 

env.process(containervessel.move(locatable_b)) 

env.run() 

 

np.testing.assert_almost_equal(np.ceil(containervessel.log["Value"][-1]), np.ceil((env.now - start)) * 2) 

 

 

# Simulation ends with unloading 

processor_2.rate = processor_2.unloading_func 

start = env.now 

env.process(processor_2.process(containervessel, dest, 500)) 

env.run() 

 

np.testing.assert_almost_equal(processor_2.log["Value"][-1], (env.now - start) * 3) 

 

@image_comparison(baseline_images=['fuel_use_transportprocessing'], extensions=['png']) 

def test_transportprocessing_plot(): 

 

class vessel(): 

def __init__(self, log, name): 

self.log = log 

self.name = name 

 

with open('energy_use.json') as f: 

data = json.load(f) 

 

vessel = vessel(data, "Test vessel") 

 

plot.energy_use(vessel, testing = True)