# host_parasite.py # ============================================================================= # # This file is part of SimElements. # ---------------------------------- # # SimElements is a software package designed to be used for continuous and # discrete event simulation. It requires Python 3.0 or later versions. # # Copyright (C) 2010 Nils A. Kjellbert # E-mail: # # SimElements is free software: you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by # the Free Software Foundation, either version 3 of the License, or # (at your option) any later version. # # SimElements is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU General Public License for more details. # # You should have received a copy of the GNU General Public License # along with this program. If not, see . # #------------------------------------------------------------------------------ """ In order for this demo program to work you will probably have to type in one of the following two paragraphs - or both - depending on your platform and os: #! /usr/local/bin/python # ...or what's needed on your computer from sys import path # Slashes or backslashes...: path.append('/Path of directory where you place simelements/simelements') """ # And: newline = '\n' # '\n' on Unix and MacOS X, '\r\n' on Windows and '\r' on Mac Classic # ----------------------------------------------------------------------------- """ This program demonstrates how to use dynamic/continuous simulation with with SimElements to solve a well-known problem known as the host-parasite or predator-prey problem. The dictionary formulation made possible by SimElements' Dynamics class is used for demonstration. The output is sent to two tab-delimited text files for further processing using standard spreadsheet software or other software suited for graphical presentation: host-parasitep.txt containing numbers with decimal points, and host-parasitec.txt containing numbers with decimal commas. """ # ----------------------------------------------------------------------------- from dynamics import Dynamics from misclib.inandout import writetab # ----------------------------------------------------------------------------- # Initial population sizes number_of = {} number_of['hosts'] = 1000.0 number_of['parasites'] = 500.0 # Rate constants k1 = 0.05 # hosts per host and hour k2 = - 0.0002 # hosts per parasite, host and hour k3 = - 0.10 # parasites per parasite and hour k4 = 0.0002 # parasites per host, parasite and hour # -- Model is placed in a separate function -------------------- def model(tim, number_of): growth_rate = {} growth_rate['hosts'] = \ k1*number_of['hosts'] + k2*number_of['parasites']*number_of['hosts'] growth_rate['parasites'] = \ k3*number_of['parasites'] + k4*number_of['hosts']*number_of['parasites'] return growth_rate # -------------------------------------------------------------- # Tab-delimited file with decimal point: points = open('host-parasitep.txt', 'w') # Tab-delimited file with decimal comma: commas = open('host-parasitec.txt', 'w') inputline = "Time\tNo. of hosts\tNo. of parasites" + newline points.write(inputline) commas.write(inputline) tstart = 0.0 tstep = 0.125 tfin = 550.0 solution = Dynamics(model, number_of) tim = tstart while tim < tfin: tnext = tim + tstep tim, number_of = solution.rke4(tim, tnext) # 4th order Runge-Kutta if number_of['hosts'] >= 1.0 and number_of['parasites'] >= 1.0: inputline = tim, number_of['hosts'], number_of['parasites'] writetab(points, commas, inputline, newline=newline) else: break points.close() commas.close() print("Results in files host-parasitep.txt and host-parasitec.txt") # -----------------------------------------------------------------------------