1 | from fielddisplay import fielddisplay
|
---|
2 | from checkfield import checkfield
|
---|
3 | from WriteData import WriteData
|
---|
4 |
|
---|
5 |
|
---|
6 | class timesteppingadaptive(object):
|
---|
7 | """
|
---|
8 | TIMESTEPPINGADAPTIVE Class definition
|
---|
9 |
|
---|
10 | Usage:
|
---|
11 | timesteppingadaptive = timesteppingadaptive()
|
---|
12 | """
|
---|
13 |
|
---|
14 | def __init__(self, *args): # {{{
|
---|
15 | if not len(args):
|
---|
16 | self.start_time = 0.
|
---|
17 | self.final_time = 0.
|
---|
18 | self.time_step_min = 0.
|
---|
19 | self.time_step_max = 0.
|
---|
20 | self.cfl_coefficient = 0.
|
---|
21 | self.interp_forcing = 1
|
---|
22 | self.average_forcing = 0
|
---|
23 | self.cycle_forcing = 0
|
---|
24 | self.coupling_time = 0.
|
---|
25 |
|
---|
26 | #set defaults
|
---|
27 | self.setdefaultparameters()
|
---|
28 |
|
---|
29 | elif len(args) == 1 and args[0].__module__ == 'timestepping':
|
---|
30 | old = args[0]
|
---|
31 | #first call setdefaultparameters:
|
---|
32 | self.setdefaultparameters()
|
---|
33 | self.start_time = old.start_time
|
---|
34 | self.final_time = old.final_time
|
---|
35 | self.interp_forcing = old.interp_forcing
|
---|
36 | self.average_forcing = old.average_forcing
|
---|
37 | self.cycle_forcing = old.cycle_forcing
|
---|
38 | self.coupling_time = old.coupling_time
|
---|
39 |
|
---|
40 | else:
|
---|
41 | raise Exception('constructor not supported')
|
---|
42 | #}}}
|
---|
43 |
|
---|
44 | def __repr__(self): # {{{
|
---|
45 | string = " timesteppingadaptive parameters:"
|
---|
46 | string = '{}\n{}'.format (string, fielddisplay(self, "start_time", "simulation starting time [yr]"))
|
---|
47 | string = '{}\n{}'.format(string, fielddisplay(self, "final_time", "final time to stop the simulation [yr]"))
|
---|
48 | string = '{}\n{}'.format (string, fielddisplay(self, "time_step_min", "minimum length of time steps [yr]"))
|
---|
49 | string = '{}\n{}'.format (string, fielddisplay(self, "time_step_max", "maximum length of time steps [yr]"))
|
---|
50 | string = '{}\n{}'.format (string, fielddisplay(self, "cfl_coefficient", "coefficient applied to cfl condition"))
|
---|
51 | string = '{}\n{}'.format (string, fielddisplay(self, "interp_forcing", "interpolate in time between requested forcing values ? (0 or 1)"))
|
---|
52 | string = '{}\n{}'.format(string, fielddisplay(self, 'average_forcing', 'average in time if there are several forcing values between steps? (0 or 1, default is 0)'))
|
---|
53 | string = '{}\n{}'.format(string, fielddisplay(self, "cycle_forcing", "cycle through forcing ? (0 or 1)"))
|
---|
54 | string = '{}\n{}'.format(string, fielddisplay(self, "coupling_time", "coupling time steps with ocean model [yr]"))
|
---|
55 | return string
|
---|
56 | # }}}
|
---|
57 |
|
---|
58 | def setdefaultparameters(self): # {{{
|
---|
59 | #time between 2 time steps
|
---|
60 | self.time_step_min = 0.01
|
---|
61 | self.time_step_max = 10.
|
---|
62 | #final time
|
---|
63 | self.final_time = 10. * self.time_step_max
|
---|
64 | #time adaptation?
|
---|
65 | self.cfl_coefficient = 0.5
|
---|
66 | #should we interpolate forcing between timesteps?
|
---|
67 | self.interp_forcing = 1
|
---|
68 | self.average_forcing = 0
|
---|
69 | self.cycle_forcing = 0
|
---|
70 | return self
|
---|
71 | #}}}
|
---|
72 |
|
---|
73 | def checkconsistency(self, md, solution, analyses): # {{{
|
---|
74 | md = checkfield(md, 'fieldname', 'timestepping.start_time', 'numel', [1], 'NaN', 1, 'Inf', 1)
|
---|
75 | md = checkfield(md, 'fieldname', 'timestepping.final_time', 'numel', [1], 'NaN', 1, 'Inf', 1)
|
---|
76 | md = checkfield(md, 'fieldname', 'timestepping.time_step_min', 'numel', [1], '>=', 0, 'NaN', 1, 'Inf', 1)
|
---|
77 | md = checkfield(md, 'fieldname', 'timestepping.time_step_max', 'numel', [1], '>=', md.timestepping.time_step_min, 'NaN', 1, 'Inf', 1)
|
---|
78 | md = checkfield(md, 'fieldname', 'timestepping.cfl_coefficient', 'numel', [1], '>', 0, '<=', 1)
|
---|
79 | if self.final_time - self.start_time < 0:
|
---|
80 | md.checkmessage("timestepping.final_time should be larger than timestepping.start_time")
|
---|
81 | md = checkfield(md, 'fieldname', 'timestepping.interp_forcing', 'numel', [1], 'values', [0, 1])
|
---|
82 | md = checkfield(md, 'fieldname', 'timestepping.average_forcing', 'numel', [1], 'values', [0, 1])
|
---|
83 | md = checkfield(md, 'fieldname', 'timestepping.cycle_forcing', 'numel', [1], 'values', [0, 1])
|
---|
84 | md = checkfield(md, 'fieldname', 'timestepping.coupling_time', 'numel', [1], '>=', 0, 'NaN', 1, 'Inf', 1)
|
---|
85 |
|
---|
86 | return md
|
---|
87 | # }}}
|
---|
88 |
|
---|
89 | def marshall(self, prefix, md, fid): # {{{
|
---|
90 | yts = md.constants.yts
|
---|
91 | WriteData(fid, prefix, 'name', 'md.timestepping.type', 'data', 2, 'format', 'Integer')
|
---|
92 | WriteData(fid, prefix, 'object', self, 'class', 'timestepping', 'fieldname', 'start_time', 'format', 'Double', 'scale', yts)
|
---|
93 | WriteData(fid, prefix, 'object', self, 'class', 'timestepping', 'fieldname', 'final_time', 'format', 'Double', 'scale', yts)
|
---|
94 | WriteData(fid, prefix, 'object', self, 'class', 'timestepping', 'fieldname', 'time_step_min', 'format', 'Double', 'scale', yts)
|
---|
95 | WriteData(fid, prefix, 'object', self, 'class', 'timestepping', 'fieldname', 'time_step_max', 'format', 'Double', 'scale', yts)
|
---|
96 | WriteData(fid, prefix, 'object', self, 'class', 'timestepping', 'fieldname', 'cfl_coefficient', 'format', 'Double')
|
---|
97 | WriteData(fid, prefix, 'object', self, 'class', 'timestepping', 'fieldname', 'interp_forcing', 'format', 'Boolean')
|
---|
98 | WriteData(fid, prefix, 'object', self, 'class', 'timestepping', 'fieldname', 'average_forcing', 'format', 'Boolean')
|
---|
99 | WriteData(fid, prefix, 'object', self, 'class', 'timestepping', 'fieldname', 'cycle_forcing', 'format', 'Boolean')
|
---|
100 | WriteData(fid, prefix, 'object', self, 'class', 'timestepping', 'fieldname', 'coupling_time', 'format', 'Double', 'scale', yts)
|
---|
101 | # }}}
|
---|