YetAnotherCoupler 3.1.1
Loading...
Searching...
No Matches
driver.py

The driver to use the different component classes

1#!/usr/bin/env python3
2
3# Copyright (c) 2024 The YAC Authors
4#
5# SPDX-License-Identifier: BSD-3-Clause
6
7import sys
8import datetime
9import numpy as np
10from threading import Thread
11
12from yac import *
13
14
15def_calendar(Calendar.PROLEPTIC_GREGORIAN)
16
17class Driver:
18 def __init__(self, start=None, end=None, verbose=0, yac = None, multithreading = False):
19 self.yac = yac or YAC.default_instance
20 self.yac.def_datetime(start, end)
21 self.verbose = verbose
22 self.multithreading = multithreading
23
24 def run(self, *components):
25 # finish component definition by adding a own component
26 self.yac.def_comps()
27 if self.verbose > 0: print("Components: ", self.yac.component_names)
28
29 # let the components setup their grids, fields etc.
30 for comp in components:
31 comp.setup()
32
33 self.yac.sync_def()
34
35 # some components might add something after synchronization
36 for comp in components:
37 comp.def_couples()
38
39 self.yac.enddef()
40
41 end_datetime = datetime.datetime.fromisoformat(self.yac.end_datetime)
42 for c in components: c.datetime = datetime.datetime.fromisoformat(self.yac.start_datetime)
43
44 if self.multithreading:
45 def run_comp(comp):
46 while comp.datetime <= end_datetime:
47 comp.datetime = datetime.datetime.fromisoformat(comp.step())
48 threads = [Thread(target=run_comp, args=(comp,)) for comp in components]
49 for t in threads: t.start()
50 for t in threads: t.join()
51 else:
52 while True:
53 min_comp = min(components, key=lambda c: c.datetime)
54 if min_comp.datetime > end_datetime:
55 break
56 nxt = min_comp.step()
57 min_comp.datetime = datetime.datetime.fromisoformat(nxt)