|
| 1 | +import torch |
| 2 | +import torch_geometric as pyg |
| 3 | +import torch_geometric.utils as pyg_utils |
| 4 | +from ParticleGraph.utils import to_numpy |
| 5 | + |
| 6 | +class PDE_D_MichaelisMenten(pyg.nn.MessagePassing): |
| 7 | + """ |
| 8 | + Michaelis-Menten saturating kinetics particle model. |
| 9 | +
|
| 10 | + Consumption and production rates depend on local field concentration |
| 11 | + via Michaelis-Menten saturation: rate_eff = base_rate * |C1| / (Km + |C1|). |
| 12 | +
|
| 13 | + Literature: |
| 14 | + - Michaelis, L. & Menten, M. L. (1913) Biochem Z 49:333-369 |
| 15 | + "Die Kinetik der Invertinwirkung" |
| 16 | +
|
| 17 | + Physics: |
| 18 | + rate_effective = base_rate * |C1| / (Km + |C1|) |
| 19 | + At low C1: rate ~ base_rate * C1/Km (linear, slow) |
| 20 | + At high C1: rate ~ base_rate (saturated, maximum) |
| 21 | +
|
| 22 | + Per-type params layout: [M1, M2, consumption, production, ar_p1, ar_p2, ar_p3, ar_p4] |
| 23 | + """ |
| 24 | + |
| 25 | + PARAMS_DOC = { |
| 26 | + "model_name": "MichaelisMenten", |
| 27 | + "literature": "Michaelis & Menten (1913) Biochem Z 49:333-369", |
| 28 | + "description": "Saturating concentration-dependent consumption/production kinetics", |
| 29 | + "equations": { |
| 30 | + "field_to_particle": "v = (M1*grad_C1 + M2*grad_C2) * dir", |
| 31 | + "particle_to_field": "dC1 = -consumption * |C1|/(Km+|C1|) * w(r), dC2 = production * |C1|/(Km+|C1|) * w(r)", |
| 32 | + "particle_to_particle": "f = (p1*exp(-d^(2p2)/(2sigma^2)) - p3*exp(-d^(2p4)/(2sigma^2))) * dir" |
| 33 | + }, |
| 34 | + "params_mesh": [ |
| 35 | + { |
| 36 | + "row": 0, "description": "C1 field parameters (shared with mesh model)", |
| 37 | + "slots": [ |
| 38 | + {"index": 0, "name": "D1", "description": "Diffusion coeff for C1 (mesh model)", "typical_range": [0.01, 0.5]}, |
| 39 | + {"index": 1, "name": "Da_c", "description": "Damkohler number (mesh model)", "typical_range": [1.0, 50.0]}, |
| 40 | + {"index": 2, "name": "A", "description": "Brusselator param A (mesh model)", "typical_range": [0.5, 5.0]}, |
| 41 | + {"index": 3, "name": "B", "description": "Brusselator param B (mesh model)", "typical_range": [1.0, 10.0]}, |
| 42 | + {"index": 4, "name": "mu", "description": "Morphological parameter (mesh model)", "typical_range": [0.01, 0.1]}, |
| 43 | + {"index": 5, "name": "M1", "description": "Mobility coefficient for C1 gradients", "typical_range": [-16, 16]} |
| 44 | + ] |
| 45 | + }, |
| 46 | + { |
| 47 | + "row": 1, "description": "C2 field parameters + MM control", |
| 48 | + "slots": [ |
| 49 | + {"index": 0, "name": "D2", "description": "Diffusion coeff for C2 (mesh model)", "typical_range": [0.1, 1.0]}, |
| 50 | + {"index": 1, "name": "M2", "description": "Mobility coefficient for C2 gradients", "typical_range": [-16, 16]}, |
| 51 | + {"index": 2, "name": "mm_Km", "description": "Michaelis-Menten half-saturation constant (0=constant rate)", "typical_range": [0.1, 5.0]} |
| 52 | + ] |
| 53 | + }, |
| 54 | + { |
| 55 | + "row": 2, "description": "Particle-field coupling", |
| 56 | + "slots": [ |
| 57 | + {"index": 0, "name": "Pe", "description": "Peclet number", "typical_range": [0.5, 2.0]}, |
| 58 | + {"index": 1, "name": "consumption", "description": "Particle consumption rate of C1", "typical_range": [10, 200]}, |
| 59 | + {"index": 2, "name": "production", "description": "Particle production rate of C2", "typical_range": [-200, -10]}, |
| 60 | + {"index": 3, "name": "influence_radius", "description": "Gaussian influence radius for pf coupling", "typical_range": [0.01, 0.1]} |
| 61 | + ] |
| 62 | + } |
| 63 | + ], |
| 64 | + "particle_params": { |
| 65 | + "description": "Per-type params from simulation.params (one row per n_particle_types)", |
| 66 | + "slots": [ |
| 67 | + {"index": 0, "name": "M1", "description": "Per-type mobility for C1"}, |
| 68 | + {"index": 1, "name": "M2", "description": "Per-type mobility for C2"}, |
| 69 | + {"index": 2, "name": "consumption", "description": "Per-type consumption rate"}, |
| 70 | + {"index": 3, "name": "production", "description": "Per-type production rate"}, |
| 71 | + {"index": 4, "name": "ar_p1", "description": "Attraction strength"}, |
| 72 | + {"index": 5, "name": "ar_p2", "description": "Attraction exponent"}, |
| 73 | + {"index": 6, "name": "ar_p3", "description": "Repulsion strength"}, |
| 74 | + {"index": 7, "name": "ar_p4", "description": "Repulsion exponent"} |
| 75 | + ] |
| 76 | + } |
| 77 | + } |
| 78 | + |
| 79 | + def __init__(self, aggr_type='mean', p=None, particle_params=None, bc_dpos=None, dimension=2, sigma=0.005): |
| 80 | + super(PDE_D_MichaelisMenten, self).__init__(aggr=aggr_type) |
| 81 | + |
| 82 | + self.p = p |
| 83 | + self.particle_params = particle_params |
| 84 | + self.bc_dpos = bc_dpos |
| 85 | + self.dimension = dimension |
| 86 | + self.sigma = sigma |
| 87 | + |
| 88 | + self.M1 = p[0, 5] |
| 89 | + self.M2 = p[1, 1] |
| 90 | + self.consumption_rate = p[2, 1] |
| 91 | + self.production_rate = p[2, 2] |
| 92 | + self.influence_radius = p[2, 3] |
| 93 | + self.Pe = p[2, 0] |
| 94 | + self.repulsion_strength = 50 |
| 95 | + self.repulsion_range = 0.04 |
| 96 | + |
| 97 | + self.mm_Km = p[1, 2] if p.shape[1] > 2 else 0.0 |
| 98 | + |
| 99 | + print(f"initialized PDE_D_MichaelisMenten with parameters:") |
| 100 | + print(f" mobility: M1={self.M1.item()}, M2={self.M2.item()}") |
| 101 | + mm_val = self.mm_Km.item() if hasattr(self.mm_Km, 'item') else self.mm_Km |
| 102 | + print(f" Km={mm_val:.3f} (rate = base * |C1|/(Km+|C1|), Michaelis-Menten 1913)") |
| 103 | + print(f" Pe={self.Pe.item():.3f}, sigma={self.sigma}") |
| 104 | + print(f" particle->field: consumption={self.consumption_rate.item()}, production={self.production_rate.item()}, influence_radius={self.influence_radius.item():.3f}") |
| 105 | + if particle_params is not None: |
| 106 | + print(f" multi-type support: {particle_params.shape[0]} particle types") |
| 107 | + |
| 108 | + def forward(self, data, direction='fp'): |
| 109 | + x, edge_index = data.x, data.edge_index |
| 110 | + edge_index, _ = pyg_utils.remove_self_loops(edge_index) |
| 111 | + |
| 112 | + if self.particle_params is not None: |
| 113 | + particle_type = x[:, 1 + 2*self.dimension].long() |
| 114 | + max_type = particle_type.max().item() |
| 115 | + n_param_rows = self.particle_params.shape[0] |
| 116 | + if max_type >= n_param_rows: |
| 117 | + raise ValueError( |
| 118 | + f"PDE_D_MichaelisMenten: particle_params has {n_param_rows} rows but found " |
| 119 | + f"particle type {max_type}. Need {max_type + 1} rows in simulation.params." |
| 120 | + ) |
| 121 | + parameters = self.particle_params[to_numpy(particle_type), :] |
| 122 | + else: |
| 123 | + parameters = None |
| 124 | + |
| 125 | + if direction == 'interpolate': |
| 126 | + result = self.propagate(edge_index, x=x, mode='interpolate', parameters=parameters) |
| 127 | + pos = x[:, 1:self.dimension+1] |
| 128 | + in_box = ((pos >= 0) & (pos <= 1)).all(dim=1, keepdim=True) |
| 129 | + result = result * in_box.float() |
| 130 | + return result |
| 131 | + elif direction == 'fp': |
| 132 | + result = self.propagate(edge_index, x=x, mode='fp', parameters=parameters) |
| 133 | + pos = x[:, 1:self.dimension+1] |
| 134 | + in_box = ((pos >= 0) & (pos <= 1)).all(dim=1, keepdim=True) |
| 135 | + result = result * in_box.float() |
| 136 | + return result |
| 137 | + elif direction == 'pf': |
| 138 | + result = self.propagate(edge_index, x=x, mode='pf', parameters=parameters) |
| 139 | + return result |
| 140 | + else: |
| 141 | + result = self.propagate(edge_index, x=x, mode='pp', parameters=parameters) |
| 142 | + return result |
| 143 | + |
| 144 | + def message(self, edge_index_i, edge_index_j, x_i, x_j, mode=None, parameters_i=None): |
| 145 | + pos_i = x_i[:, 1:self.dimension+1] |
| 146 | + pos_j = x_j[:, 1:self.dimension+1] |
| 147 | + |
| 148 | + d_pos = self.bc_dpos(pos_j - pos_i) |
| 149 | + dist = torch.sqrt(torch.sum(d_pos**2, dim=1)) |
| 150 | + dist_safe = torch.clamp(dist, min=1e-6) |
| 151 | + |
| 152 | + if mode == 'interpolate': |
| 153 | + C1_mesh = x_j[:, 6:7] |
| 154 | + C2_mesh = x_j[:, 7:8] |
| 155 | + weight = torch.exp(-dist / 0.01).unsqueeze(1) |
| 156 | + return torch.cat([C1_mesh * weight, C2_mesh * weight, weight], dim=1) |
| 157 | + |
| 158 | + elif mode == 'fp': |
| 159 | + fields_i = x_i[:, 6:8] |
| 160 | + fields_j = x_j[:, 6:8] |
| 161 | + |
| 162 | + dC1 = fields_j[:, 0:1] - fields_i[:, 0:1] |
| 163 | + dC2 = fields_j[:, 1:2] - fields_i[:, 1:2] |
| 164 | + |
| 165 | + kernel = torch.exp(-dist / 0.05) |
| 166 | + dir_norm = d_pos / dist_safe.unsqueeze(1) |
| 167 | + domain_scale = 32.0 |
| 168 | + grad_C1 = (dC1 * kernel.unsqueeze(1)) / (dist_safe.unsqueeze(1) * domain_scale) |
| 169 | + grad_C2 = (dC2 * kernel.unsqueeze(1)) / (dist_safe.unsqueeze(1) * domain_scale) |
| 170 | + |
| 171 | + if parameters_i is not None: |
| 172 | + M1 = parameters_i[:, 0:1] |
| 173 | + M2 = parameters_i[:, 1:2] |
| 174 | + else: |
| 175 | + M1 = self.M1 |
| 176 | + M2 = self.M2 |
| 177 | + |
| 178 | + velocities = (M1 * grad_C1 + M2 * grad_C2) * dir_norm |
| 179 | + return velocities |
| 180 | + |
| 181 | + elif mode == 'pf': |
| 182 | + weights = torch.exp(-dist**2 / (2 * (self.influence_radius/3)**2)) |
| 183 | + |
| 184 | + if parameters_i is not None: |
| 185 | + consumption = parameters_i[:, 2] |
| 186 | + production = parameters_i[:, 3] |
| 187 | + else: |
| 188 | + consumption = self.consumption_rate |
| 189 | + production = self.production_rate |
| 190 | + |
| 191 | + # Michaelis-Menten modulation |
| 192 | + if self.mm_Km > 0: |
| 193 | + C1_local = torch.abs(x_i[:, 6]) + 1e-6 |
| 194 | + mm_factor = C1_local / (self.mm_Km + C1_local) |
| 195 | + consumption = consumption * mm_factor |
| 196 | + production = production * mm_factor |
| 197 | + |
| 198 | + field_updates = torch.zeros((pos_i.size(0), 2), device=pos_i.device) |
| 199 | + field_updates[:, 0] = -consumption * weights |
| 200 | + field_updates[:, 1] = production * weights |
| 201 | + return field_updates |
| 202 | + |
| 203 | + else: # mode == 'pp' |
| 204 | + if parameters_i is not None: |
| 205 | + p1 = parameters_i[:, 4] |
| 206 | + p2 = parameters_i[:, 5] |
| 207 | + p3 = parameters_i[:, 6] |
| 208 | + p4 = parameters_i[:, 7] |
| 209 | + |
| 210 | + f = (p1 * torch.exp(-dist ** (2 * p2) / (2 * self.sigma ** 2)) |
| 211 | + - p3 * torch.exp(-dist ** (2 * p4) / (2 * self.sigma ** 2))) |
| 212 | + |
| 213 | + forces = f[:, None] * d_pos / dist_safe.unsqueeze(1) |
| 214 | + else: |
| 215 | + forces = torch.zeros_like(pos_i) |
| 216 | + in_range = dist < self.repulsion_range |
| 217 | + if in_range.any(): |
| 218 | + dir_norm = d_pos / dist_safe.unsqueeze(1) |
| 219 | + repulsion_mag = self.repulsion_strength * torch.exp( |
| 220 | + -5.0 * dist[in_range] / self.repulsion_range |
| 221 | + ) |
| 222 | + forces[in_range] = -dir_norm[in_range] * repulsion_mag.unsqueeze(1) |
| 223 | + |
| 224 | + return forces |
| 225 | + |
| 226 | + def update(self, aggr_out, mode=None): |
| 227 | + if mode == 'interpolate': |
| 228 | + C1_weighted = aggr_out[:, 0:1] |
| 229 | + C2_weighted = aggr_out[:, 1:2] |
| 230 | + weight_sum = aggr_out[:, 2:3] |
| 231 | + weight_sum = torch.clamp(weight_sum, min=1e-10) |
| 232 | + return torch.cat([C1_weighted / weight_sum, C2_weighted / weight_sum], dim=1) |
| 233 | + else: |
| 234 | + return aggr_out |
0 commit comments