-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfillInteractions_df_fun.py
More file actions
127 lines (99 loc) · 5.14 KB
/
Copy pathfillInteractions_df_fun.py
File metadata and controls
127 lines (99 loc) · 5.14 KB
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
# -*- coding: utf-8 -*-
"""
Created on Fri Jul 2 11:19:34 2021
@author: PradoDomercq
"""
import numpy as np
import pandas as pd
def fillInteractions_fun (RC_df, Clist,compartments_prop):
def interact3(sp1,interactions_df,RC_df):
def transportProcess (sp1,sp2,RC_df):
if int(sp2[:-3])+1 == int(sp1[:-3]):#Consecutive river sections
if sp1[-3]!="4":
solution = RC_df[sp2]["advection"]
else:
solution = RC_df[sp2]["sedTransport"]
else:
solution = 0
return solution
def inboxProcess(sp1,sp2,RC_df):
if (sp1[:-1] == sp2[:-1] and sp1[-1] != sp2[-1]):
#fragmentation only will occur from bigger to smaller and in consecutive sieBins Sizebin = sp[-3]
if (sp2[-1] =="b" and sp1[-1] =="a") or (sp2[-1] =="c" and sp1[-1] =="b") or (sp2[-1] =="d" and sp1[-1] =="c") or (sp2[-1] =="e" and sp1[-1] =="d"):
if type(RC_df[sp2]["fragmentation"])is tuple:
frag = RC_df[sp2]["fragmentation"]
fragval = frag[0]*frag[1]
sol = fragval
else:
sol = RC_df[sp2]["fragmentation"]
else:
sol = 0
#only different aggergation states--> heteroagg, biofouling, agg-breackup (aggregation state = sp[-4])
elif sp1[:-2]+sp1[-1] == sp2[:-2]+sp2[-1]:
if (sp2[-2] =="A" and sp1[-2] =="B") or (sp2[-2] =="C" and sp1[-2] =="D"):
sol = RC_df[sp2]["heteroagg"]
elif (sp2[-2] =="B" and sp1[-2] =="A") or (sp2[-2] =="D" and sp1[-2] =="B"):
sol = RC_df[sp2]["breakup"]
elif (sp2[-2] =="A" and sp1[-2] =="C") or (sp2[-2] =="B" and sp1[-2] =="D"):
sol = RC_df[sp2]["biofilm"]
else:
sol=0
#only different compartments-->settling, rising, mixing, resusp (compartment sp[-5])
elif sp1[:-3]+sp1[-2:] == sp2[:-3]+sp2[-2:]:
if (sp2[-3] =="1" and sp1[-3] =="2") or (sp2[-3] =="2" and sp1[-3] =="3") or (sp2[-3] =="3" and sp1[-3] =="4"):
sol = RC_df[sp2]["settling"]+ RC_df[sp2]["mixing"]
elif (sp2[-3] =="3" and sp1[-3] =="2") or (sp2[-3] =="2" and sp1[-3] =="1"):
sol = RC_df[sp2]["rising"] + RC_df[sp2]["mixing"]
elif (sp2[-3] =="4" and sp1[-3] =="3"):
sol = RC_df[sp2]["resusp"]
else:
sol=0
else:
sol=0
return sol
interact_row=[]
for sp2 in interactions_df.index.to_list():
if sp1 == sp2:
interact_row.append(interactions_df[sp2][sp1])
else:#Processess inside the same river section (RS)
if sp1[:-3] == sp2[:-3]:
interact_row.append(inboxProcess(sp1,sp2,RC_df))
#If different river sections:
####Transport between river sections is indicated by the Ocean flow matrix
elif sp2[-3:] == sp1[-3:]:#Only different sector (but all rest same)
interact_row.append(transportProcess (sp1,sp2,RC_df))
else:
interact_row.append(0)
return interact_row
#Remove volume and density from RC_df
RC_df= RC_df.drop("volume_m3")
RC_df= RC_df.drop("density_kg_m3")
#remove C from Clist:
SpeciesList=[]
for a in Clist:
SpeciesList.append(a[2:])
#Diagonal of the df corresponds to the losses of each species
#crate the array of values for the diagonal wich is the sum of all RC corresponding to one species:
diag_list = []
for sp in SpeciesList:
if type(RC_df[sp]["fragmentation"]) is tuple:
# when there are fragements formed extract the fragmentation array to multiply kfrag by number of fragments formed (fro the mas formed)
#IN the lossess (estimation of mass lost) process we dont need to multiply by the number of fragments!!
frag = RC_df[sp]["fragmentation"]
#fragval = frag[0]*frag[1]
RC_df_noFrag = RC_df[sp].drop("fragmentation")
diag_list.append(-(sum(RC_df_noFrag)+frag[0]))
else:
diag_list.append(-(sum(RC_df[sp])))
# diag_array = np.asarray(diag_list)
# diag_array[np.isnan(diag_array)] = 0 #substitute nan by 0
#Dataframe with lossess rate constants
interactions_df = pd.DataFrame(np.diag(diag_list), index=SpeciesList , columns= SpeciesList)
list_sp1=interactions_df.index.to_list()
interactions_df_rows=[]
for sp1 in interactions_df.index.to_list():
interactions_df_rows.append(interact3(sp1,interactions_df,RC_df))
#interact3(sp1) for sp1 in interactions_df.index.to_list()]
array=np.column_stack(interactions_df_rows)
interactions_df_sol=pd.DataFrame(array,index=list_sp1, columns=list_sp1)
return interactions_df_sol