Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improvements for APSIM code generation #238

Open
hol353 opened this issue Sep 13, 2024 · 1 comment
Open

Improvements for APSIM code generation #238

hol353 opened this issue Sep 13, 2024 · 1 comment

Comments

@hol353
Copy link

hol353 commented Sep 13, 2024

I have played with the APSIM code generated by Crop2ML to make it simpler for set up in APSIM.

I am wondering if model unit code could be generated like below. If so, then we don't need components or wrappers.

  • Have [Link] statements for State, State1, Rate, Auxillary, Exogenous classes etc rather than passing them into Init and CalculateModel. If they aren't being used then don't declare them in the class.
  • Add [EventSubscribe("Crop2MLInit")] before Init method
  • Add [EventSubscribe("Crop2MLCalculateModel")] before CalculateModel method
  • Remove all constructors (including copy constructor) as they aren't needed by APSIM.
  • For exogenous variables, link to [Link] Crop2MLExogenous ex = null; rather than SurfaceTemperaturePartonExogenous. We will create Crop2MLExogenous so there is no need to have Crop2ML generate this.
  • For parameters, use the same link as for exogenous rather than have them as class properties.

If this is possible then I might be able to create a PR to do this. I can see the APSIM generator code here: https://github.com/AgriculturalModelExchangeInitiative/PyCrop2ML/blob/master/src/pycropml/transpiler/generators/apsimGenerator.py.

using System;
using Models.Core;
namespace Models.Crop2ML.Bioma;

/// <summary>
///- Name: SurfaceTemperatureParton -Version: 001, -Time step: 1
///- Description:
///            * Title: SurfaceTemperatureParton model
///            * Authors: simone.bregaglio
///            * Reference: ('http://bioma.jrc.ec.europa.eu/ontology/JRC_MARS_biophysical_domain.owl',)
///            * Institution: University Of Milan
///            * ExtendedDescription: Strategy for the calculation of soil surface temperature with Parton's method. Reference: Parton, W. J. 1984. Predicting soil temperatures in a shortgrass steppe. Soil Science 138:93-101.
///            * ShortDescription: None
/// </summary>
/// <remarks>
///- inputs:
///            * name: DayLength
///                          ** description : Length of the day
///                          ** inputtype : variable
///                          ** variablecategory : exogenous
///                          ** datatype : DOUBLE
///                          ** max : 24
///                          ** min : 0
///                          ** default : 10
///                          ** unit : h
///  ...
/// </remarks>
[Serializable]
[PresenterName("UserInterface.Presenters.PropertyPresenter")]
[ViewName("UserInterface.Views.PropertyView")]
[ValidParent(ParentType = typeof(Zone))]
public class NewSurfaceTemperatureParton : Model
{
    [Link] SurfaceTemperaturePartonAuxiliary a = null;
    [Link] Crop2MLExogenous ex = null;

    /// <summary>Algorithm of the SurfaceTemperatureParton component</summary>
    [EventSubscribe("Crop2MLCalculateModel")]
    public void  CalculateModel()
    {
        double DayLength = ex.DayLength;
        double AirTemperatureMaximum = ex.AirTemperatureMaximum;
        double AirTemperatureMinimum = ex.AirTemperatureMinimum;
        double AboveGroundBiomass = ex.AboveGroundBiomass;
        double GlobalSolarRadiation = ex.GlobalSolarRadiation;
        double SurfaceTemperatureMinimum;
        double SurfaceTemperatureMaximum;
        double SurfaceSoilTemperature;
        double _AGB;
        double _AirTMax;
        double _AirTmin;
        double _SolarRad;
        _AGB = AboveGroundBiomass / 10000;
        _AirTMax = AirTemperatureMaximum;
        _AirTmin = AirTemperatureMinimum;
        _SolarRad = GlobalSolarRadiation;
        if (_AGB > 0.15)
        {
            SurfaceTemperatureMaximum = _AirTMax + ((24 * (1 - Math.Exp(-0.038 * _SolarRad)) + (0.35 * _AirTMax)) * (Math.Exp(-4.8 * _AGB) - 0.13));
            SurfaceTemperatureMinimum = _AirTmin + (6 * _AGB) - 1.82;
        }
        else
        {
            SurfaceTemperatureMaximum = AirTemperatureMaximum;
            SurfaceTemperatureMinimum = AirTemperatureMinimum;
        }
        SurfaceSoilTemperature = 0.41 * SurfaceTemperatureMaximum + (0.59 * SurfaceTemperatureMinimum);
        if (DayLength != (double)(0))
        {
            SurfaceSoilTemperature = DayLength / 24 * _AirTMax + ((1 - (DayLength / 24)) * _AirTmin);
        }
        a.SurfaceTemperatureMinimum= SurfaceTemperatureMinimum;
        a.SurfaceTemperatureMaximum= SurfaceTemperatureMaximum;
        a.SurfaceSoilTemperature= SurfaceSoilTemperature;
    }
}
@cyrillemidingoyi
Copy link
Contributor

Hi @hol353 Yes it is possible. You can create this PR

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants