ajwdewit / pcse

Repository for the Python Crop Simulation Environment
Other
191 stars 127 forks source link

"apply_npk" signal invaild while sent to Wofost80_NWLP_FD_beta model #67

Closed AlChen94 closed 1 year ago

AlChen94 commented 1 year ago

When "apply_npk" signal is sent, the content of nitrogen, phosphorus and potassium in Wofost80_NWLP_FD_beta model will not change.This problem seems to be related to the RateVariables class.

I modified the code as below, and the problem seems to be solved. ` def _on_APPLY_NPK(self, N_amount=None, P_amount=None, K_amount=None, N_recovery=None, P_recovery=None, K_recovery=None):

    r = self.rates
    r.unlock()
    r.FERT_N_SUPPLY = N_amount * N_recovery
    r.FERT_P_SUPPLY = P_amount * P_recovery
    r.FERT_K_SUPPLY = K_amount * K_recovery
    r.RNAVAIL += r.FERT_N_SUPPLY
    r.RPAVAIL += r.FERT_P_SUPPLY
    r.RKAVAIL += r.FERT_K_SUPPLY
    r.lock()

`

ajwdewit commented 1 year ago

Can you let me know which code you are referring to exactly. In principle applying N/P/K happens here: https://github.com/ajwdewit/pcse/blob/master/pcse/soil/npk_soil_dynamics.py#L264

The N/P/K amounts are first copied to a temporary variable and then inserted into the rate variable at the proper moment in the cycle. It might be that you are referring to an older version of WOFOST8.

AlChen94 commented 1 year ago

Sorry, I just noticed that the version of pcse I used is 5.5.0.But unfortunately, this problem still exists after I updated pcse to 5.5.4.

The problem is at where you mentioned:pcse/soil/npk_soil_dynamics.py line 264.

I don't know the exact cause of this problem, but after making similar modifications, the problem can be solved superficially.

I changed "_on_APPLY_NPK" function , adding several lines of code to update the rates.RNAVAIL when "apply_npk" signal is received.

def _on_APPLY_NPK(self, N_amount=None, P_amount=None, K_amount=None, N_recovery=None, P_recovery=None, K_recovery=None): r = self.rates self._FERT_N_SUPPLY = N_amount * N_recovery self._FERT_P_SUPPLY = P_amount * P_recovery self._FERT_K_SUPPLY = K_amount * K_recovery r.RNAVAIL = self._FERT_N_SUPPLY r.RPAVAIL = self._FERT_P_SUPPLY r.RKAVAIL = self._FERT_K_SUPPLY