Esempio n. 1
0
def setup_sinusodial():
    # sinusodial test signal:
    s = State()
    y, my, mdy = symbols("y, my, mdy")
    s[my]  = dda.neg(y)
    s[y]   = dda.int(mdy, dt, 0)
    s[mdy] = dda.int(my, dt, 1)
    s[diff_in]     = y # diff_in = cos(t)
    s[diff_out]    = dda.neg(dda.diff(diff_in, dt, 0)) #  0 = sin(0)
    s[int_out]     = dda.neg(dda.int(diff_in, dt, +1)) # -1 = cos(0), but negated
    return s
diff_in, diff_out, diff_loop = symbols("diff_in, diff_out, diff_loop")
s = State()
dt = 0.05
t_final = 10

alpha = 0.9
ic = 0

# test signal:
#s[diff_in]        = dda.int(dda.int(1, dt, 0), dt, 0)   # diff_in = t^2

# sinusodial test signal:
y, my, mdy = symbols("y, my, mdy")
s[my] = dda.neg(y)
s[y] = dda.int(mdy, dt, 0)
s[mdy] = dda.int(my, dt, 1)

s[diff_in] = y  # diff_in = cos(t)

s[diff_out] = dda.neg(dda.sum(diff_in, diff_loop, dda.mult(alpha, diff_out)))
s[diff_loop] = dda.int(diff_out, dt, ic)

data = s.export(to="CppSolver").run(max_iterations=t_final / dt,
                                    rk_order=4).as_recarray()
xtime = np.arange(0, t_final, dt)
assert len(data) == len(xtime)

from matplotlib.pylab import *
ion()
clf()
Esempio n. 3
0
#
# Random number by DDA:
#
# We can use the noise function to get randoms between [-1,1].
# Integration over such noise should give the average, i.e. 0.
#

import numpy as np
from dda import State, Symbol, dda

s = State()
dt = 0.01
t_final = 5

s["a"] = dda.noise(0)
s["i"] = dda.int(Symbol("a"), dt, 0)

data = s.export(to="CppSolver").run(max_iterations=t_final / dt,
                                    rk_order=4).as_recarray()
xtime = np.arange(0, t_final, dt)
#assert len(data) == len(xtime)

from matplotlib.pylab import *
#ion(); clf()

#cols = data.dtype.names
cols = "a i".split()

for n in cols:
    plot(xtime, data[n], label=n)
legend()
Esempio n. 4
0
import numpy as np
from dda import State, symbols, dda

ddy, y, mdy, my, lu, ld, y2, y2ohne = symbols("ddy, y, mdy, my, lu, ld, y2, y2ohne")
s = State()
dt = 0.1
t_final = 50

err = 0.5

ic_y   = 1.0
ic_mdy = 0.0

s[my]  = dda.neg(y)
s[y]   = dda.int(lu, mdy, ld, dda.mult(err,my), dt, ic_y)
s[mdy] = dda.int(my, dt, ic_mdy) # my = ddy

s[lu]  = dda.mult(1, dda.dead_upper(y, +1))
s[ld]  = dda.mult(1, dda.dead_lower(y, -1))

# count zero crossings:
ys, yd, yi = symbols("ys, yd, yi")
s[ys]  = dda.sign(y)
s[yd]  = dda.max(0, dda.min(1, dda.diff(ys, dt, 0)))
s[yi]  = dda.neg(dda.int(dda.mult(yd,1/dt), dt, 0))

# Frequenzverdopplung ohne linearem Offset
s[y2]  = dda.mult(y,y)
s[y2ohne] = dda.diff(y2, dt, 0)
Esempio n. 5
0
# oscillator frqeuency variation
#FREQS = [ 0.95, 1.00, 1.05  ]
freqvarianz = 1.0

# oscillator gain variation
GAINS = [0.95, 1.00, 1.05]
N = len(GAINS)

ddavars = [[Symbol(f"{x}{n}") for n in range(N)]
           for x in symbols("y, limu, limd, mdy, my")]

for y, limu, limd, mdy, my, gain in zip(*ddavars, GAINS):
    s[y] = dda.mult(
        gain,
        dda.int(dda.mult(freqvarianz, mdy), dda.mult(A, my), limu, limd, dt,
                ic_dy))
    s[limu] = dda.mult(stabilization, dda.dead_upper(y, norm))
    s[limd] = dda.mult(stabilization, dda.dead_lower(y, -norm))
    s[mdy] = dda.int(dda.mult(freqvarianz, my), dda.mult(D, mdy), dt, ic_mdy)
    s[my] = dda.sum(y)

runtime_args = {
    'max_iterations': t_final / dt,
    "rk_order": 3,
    "modulo_write": modulo_write,
    "modulo_progress": t_final / dt / 10,
}
fields_to_export = ["y0", "y1", "y2"]
solver = s.export(to="CppSolver", constexpr_consts=False)

Esempio n. 6
0
# Investment in agriculture
delta_capital_investment_in_agriculture = (
    CAPITAL_FRACTION_INDICATE_BY_FOOD_RATIO_TABLE(food_ratio)
    * CAPITAL_INVESTMENT_FROM_QUALITY(
        (
            QUALITY_OF_LIFE_FROM_MATERIAL(standard_of_living)
            / QUALITY_OF_LIFE_FROM_FOOD(food_ratio)
        )  # life quality ratio
    )  # capital investment from quality ratio
    - capital_investment_in_agriculture
) / capital_investment_in_agriculture_frac_adj_time
        
### Actual evolution

population = dda.int(delta_population, delta_t, ic_pollution)
natural_resources = dda.int(delta_natural_resources, delta_t, ic_natural_resources)
capital_investment = dda.int(delta_capital_investment, delta_t, ic_capital_investment)
pollution = dda.int(delta_pollution, delta_t, ic_pollution)
capital_investment_in_agriculture = dda.int(delta_capital_investment_in_agriculture,
                                            delta_t, ic_capital_investment_in_agriculture)


### This is a derived quantity of interest from the solution:
### The quality of life measure

qol = (
        quality_of_life_standard
        * QUALITY_OF_LIFE_FROM_MATERIAL(
            standard_of_living
        )  # material multiplier
Esempio n. 7
0
def setup_polynomial():
    s = State()
    s[diff_in]        = dda.mult(2,dda.int(dda.int(1, dt, 0), dt, 0))   # diff_in = 2*int(1) = t^2
    s[diff_out]       = dda.neg(dda.diff(diff_in, dt, 0))  # 2*t
    s[int_diff]       = dda.neg(dda.int(diff_out, dt, 0)) # t^2 but with increasing deviations due to dt
    return s
Esempio n. 8
0
def setup_constant():
    s = State()
    #s[diff_in]   = dda.const(1)  # TODO: Such an expression does no more work!!
    s[diff_out]  = dda.diff(1, dt, 0)
    s[int_diff]  = dda.int(diff_out, dt, 1) # integrates over 0, should do it.
    return s
Esempio n. 9
0
diff_in, diff_out, diff_loop  = symbols("diff_in, diff_out, diff_loop")
s = State()
dt = 0.01
t_final = 5

alpha  = 0.9
ic     = 0

# test signal:
#s[diff_in]        = dda.int(dda.int(1, dt, 0), dt, 0)   # diff_in = t^2

# sinusodial test signal:
y, my, mdy = symbols("y, my, mdy")
s[my]  = dda.neg(y)
s[y]   = dda.int(mdy, dt, 0)
s[mdy] = dda.int(my, dt, 1)

s[diff_in]  = y # diff_in = cos(t)

s[diff_out] = dda.diff(diff_in, dt, 0)



from dda.cpp_exporter import compile, run
compile(s.export(to="C"))
data = run(arguments={'max_iterations': t_final / dt, "rk_order": 4} )# return_recarray=True)
xtime = np.arange(0, t_final, dt)
assert len(data) == len(xtime)

from matplotlib.pylab import *