Skip to content

Commit

Permalink
reverte R&A2017, added tanh in ann and set FromObs model
Browse files Browse the repository at this point in the history
  • Loading branch information
forefire committed Sep 16, 2024
1 parent 3f8ed09 commit b15f3f5
Show file tree
Hide file tree
Showing 5 changed files with 311 additions and 115 deletions.
13 changes: 7 additions & 6 deletions src/ANN.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 US

// Activation functions
inline float sigmoid(float x) {

return 1.0f / (1.0f + std::exp(-x));
}

Expand All @@ -44,10 +43,16 @@ inline float linear(float x) {
return x;
}

// Define the tanh activation function
inline float tanh_activation(float x) {
return std::tanh(x);
}

// Activation function lookup
inline float (*getActivationFunction(const std::string& name))(float) {
if (name == "RELU") return relu;
if (name == "SIGM") return sigmoid;
if (name == "TANH") return tanh_activation; // Added tanh
if (name == "LINE") return linear;
return nullptr;
}
Expand Down Expand Up @@ -138,10 +143,6 @@ struct Network {
if (std::string(header) != "FFANN001") {
throw std::runtime_error("Invalid file format.");
}

// int inputSize = 0; // This will be set based on the first layer's weight matrix dimensions
//std::vector<std::unique_ptr<BaseLayer>> layers; // Vector to store layers polymorphically

for (int i = 0; i < numLayers; ++i) {
char activation[5] = {0};
int width, height;
Expand Down Expand Up @@ -171,7 +172,6 @@ struct Network {
layer->loadWeightsAndBiases(weightData, biasData);
layers.push_back(std::unique_ptr<BaseLayer>(layer));
std::cout << "adding dense "<<std::endl;
// inputSize = height; // Update inputSize for next layer
}
}

Expand Down Expand Up @@ -207,6 +207,7 @@ struct Network {
if (denseLayer->activation == relu) act = "ReLU";
if (denseLayer->activation == sigmoid) act = "Sigmoid";
if (denseLayer->activation == linear) act = "Linear";
if (denseLayer->activation == tanh_activation) act = "TanH";
ss << "Dense Layer: Input Size = " << denseLayer->weights[0].size()
<< ", Output Size = " << denseLayer->neurons.size()
<< ", Activation Function = " << act << "\n";
Expand Down
42 changes: 42 additions & 0 deletions src/FromObsModels.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
/*
Copyright (C) 2012 ForeFire Team, SPE, Universit� de Corse.
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
This program 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
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 US
*/

#ifndef FROMOBSMODEL_H_
#define FROMOBSMODEL_H_

#include "FireDomain.h"

using namespace std;

namespace libforefire {

struct SensibleheatFlux{
double flaming;
double smoldering;

SensibleheatFlux(double f, double s) : flaming(f), smoldering(s) {}
};

/*! \compute heat flux from local input */
SensibleheatFlux computeHeatFLuxFromBmap(const double&, const double&, const double&, const double&, const double&, const double&, const double&);

} /* namespace libforefire */

#endif /* FROMOBSMODEL_H_ */
116 changes: 51 additions & 65 deletions src/HeatFluxFromObsModel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 US

#include "FluxModel.h"
#include "FireDomain.h"

#include "FromObsModels.h"
using namespace std;

namespace libforefire {
Expand Down Expand Up @@ -61,70 +61,6 @@ class HeatFluxFromObsModel: public FluxModel {

};

struct SensibleheatFlux{
double flaming;
double smoldering;

SensibleheatFlux(double f, double s) : flaming(f), smoldering(s) {}
};



FluxModel* getHeatFluxFromObsModel(const int& = 0, DataBroker* = 0);

/*! \compute heat flux from local input */
SensibleheatFlux computeHeatFLuxFromBmap(const double&, const double&, const double&, const double&, const double&, const double&, const double&);


/* name of the model */
const string HeatFluxFromObsModel::name = "heatFluxFromObs";

/* registration */
int HeatFluxFromObsModel::isInitialized =
FireDomain::registerFluxModelInstantiator(name, getHeatFluxFromObsModel );

/* instantiation */
FluxModel* getHeatFluxFromObsModel(const int& index, DataBroker* db) {
return new HeatFluxFromObsModel(index, db);
}

/* constructor */
HeatFluxFromObsModel::HeatFluxFromObsModel(
const int & mindex, DataBroker* db) : FluxModel(mindex, db) {

/* defining the properties needed for the model */
evaporationTime_data = registerProperty("FromObs_evaporationTime");
residenceTime_data = registerProperty("FromObs_residenceTime");
burningTime_data = registerProperty("FromObs_burningTime");
nominalHeatFlux_f_data = registerProperty("FromObs_NominalHeatFlux_flaming");
nominalHeatFlux_s_data = registerProperty("FromObs_NominalHeatFlux_smoldering");

/* allocating the vector for the values of these properties */
if ( numProperties > 0 ) properties = new double[numProperties];

/* registering the model in the data broker */
dataBroker->registerFluxModel(this);

/* Definition of the coefficients */
/*nominalHeatFlux = 150000.;
if ( params->isValued("nominalHeatFlux") )
nominalHeatFlux = params->getDouble("nominalHeatFlux");*/

}

/* destructor (shoudn't be modified) */
HeatFluxFromObsModel::~HeatFluxFromObsModel() {
if ( properties != 0 ) delete properties;
}

/* accessor to the name of the model */
string HeatFluxFromObsModel::getName(){
return name;
}

/* ****************** */
/* Model for the flux */
/* ****************** */

SensibleheatFlux computeHeatFLuxFromBmap(const double& burningTime, const double& residenceTime, const double& nominalHeatFlux_f, const double& nominalHeatFlux_s,
const double& bt, const double& et, const double& at){
Expand Down Expand Up @@ -186,6 +122,56 @@ SensibleheatFlux computeHeatFLuxFromBmap(const double& burningTime, const doubl
}


FluxModel* getHeatFluxFromObsModel(const int& = 0, DataBroker* = 0);

/* name of the model */
const string HeatFluxFromObsModel::name = "heatFluxFromObs";

/* registration */
int HeatFluxFromObsModel::isInitialized =
FireDomain::registerFluxModelInstantiator(name, getHeatFluxFromObsModel );

/* instantiation */
FluxModel* getHeatFluxFromObsModel(const int& index, DataBroker* db) {
return new HeatFluxFromObsModel(index, db);
}

/* constructor */
HeatFluxFromObsModel::HeatFluxFromObsModel(
const int & mindex, DataBroker* db) : FluxModel(mindex, db) {

/* defining the properties needed for the model */
evaporationTime_data = registerProperty("FromObs_evaporationTime");
residenceTime_data = registerProperty("FromObs_residenceTime");
burningTime_data = registerProperty("FromObs_burningTime");
nominalHeatFlux_f_data = registerProperty("FromObs_NominalHeatFlux_flaming");
nominalHeatFlux_s_data = registerProperty("FromObs_NominalHeatFlux_smoldering");

/* allocating the vector for the values of these properties */
if ( numProperties > 0 ) properties = new double[numProperties];

/* registering the model in the data broker */
dataBroker->registerFluxModel(this);

/* Definition of the coefficients */
/*nominalHeatFlux = 150000.;
if ( params->isValued("nominalHeatFlux") )
nominalHeatFlux = params->getDouble("nominalHeatFlux");*/

}

/* destructor (shoudn't be modified) */
HeatFluxFromObsModel::~HeatFluxFromObsModel() {
if ( properties != 0 ) delete properties;
}

/* accessor to the name of the model */
string HeatFluxFromObsModel::getName(){
return name;
}



double HeatFluxFromObsModel::getValue(double* valueOf
, const double& bt, const double& et, const double& at){

Expand Down
96 changes: 52 additions & 44 deletions src/RothermelAndrews2018.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -45,17 +45,17 @@ class RothermelAndrews2018: public PropagationModel {
static int isInitialized;
double windReductionFactor;
/*! properties needed by the model */
size_t wv;
size_t slope;
size_t wo;
size_t fd;
size_t fpsa;
size_t mf;
size_t pp;
size_t h;
size_t st;
size_t se;
size_t me;
size_t wv_;
size_t slope_;
size_t wo_;
size_t fd_;
size_t fpsa_;
size_t mf_;
size_t pp_;
size_t h_;
size_t st_;
size_t se_;
size_t me_;

/*! result of the model */
double getSpeed(double*);
Expand Down Expand Up @@ -89,18 +89,18 @@ RothermelAndrews2018::RothermelAndrews2018(const int & mindex, DataBroker* db)
/* defining the properties needed for the model */
windReductionFactor = params->getDouble("windReductionFactor");

wv = registerProperty("normalWind");
slope = registerProperty("slope");

wo = registerProperty("fuel.fl1h_tac");
fd = registerProperty("fuel.fd_ft");
fpsa = registerProperty("fuel.SAVcar_ftinv");
mf = registerProperty("fuel.mdOnDry1h_r");
pp = registerProperty("fuel.fuelDens_lbft3");
h = registerProperty("fuel.H_BTUlb");
me = registerProperty("fuel.Dme_pc");
st = registerProperty("fuel.totMineral_r");
se = registerProperty("fuel.effectMineral_r");
wv_ = registerProperty("normalWind");
slope_ = registerProperty("slope");

wo_ = registerProperty("fuel.fl1h_tac");
fd_ = registerProperty("fuel.fd_ft");
fpsa_ = registerProperty("fuel.SAVcar_ftinv");
mf_ = registerProperty("fuel.mdOnDry1h_r");
pp_ = registerProperty("fuel.fuelDens_lbft3");
h_ = registerProperty("fuel.H_BTUlb");
me_ = registerProperty("fuel.Dme_pc");
st_ = registerProperty("fuel.totMineral_r");
se_ = registerProperty("fuel.effectMineral_r");

/* allocating the vector for the values of these properties */
if ( numProperties > 0 ) properties = new double[numProperties];
Expand All @@ -124,7 +124,7 @@ string RothermelAndrews2018::getName(){
/* Model for the propagation velovity of the front */
/* *********************************************** */

double RothermelAndrews2018::getSpeed(double* Z){
double RothermelAndrews2018::getSpeed(double* valueOf){

// Constants
double msToftmin = 196.85039;
Expand All @@ -133,35 +133,43 @@ double RothermelAndrews2018::getSpeed(double* Z){
double tacTokgm2 = 0.224;
double pcTor = 0.01;

double wvC = msToftmin * Z[wv]; // convert m/s to feat/min
double meC = Z[me] * pcTor; // convert percentage to ratio
double woC = Z[wo] * tacTokgm2 / lbft2Tokgm2; // convert US short tons per acre to pounds per square foot

if (wvC < 0) wvC = 0;

if(woC > 0){
double Beta_op = 3.348 * pow(Z[fpsa], -0.8189); // Optimum packing ratio
double ODBD = woC / Z[fd]; // Ovendry bulk density
// Fuel properties
double tan_slope = 0;
if (valueOf[slope_]>0){ // Only defined for null or positive slope
tan_slope = valueOf[slope_];
}
double wv = msToftmin * valueOf[wv_]; // convert m/s to feat/min
double me = valueOf[me_] * pcTor; // convert percentage to ratio
double pp = valueOf[pp_];
double mf = valueOf[mf_];
double fpsa = valueOf[fpsa_];
double fd = valueOf[fd_];
double wo = valueOf[wo_] * tacTokgm2 / lbft2Tokgm2; // convert US short tons per acre to pounds per square foot
double h = valueOf[h_];
double st = valueOf[st_];
double se = valueOf[se_];

if (wv < 0) wv = 0;

if(wo > 0){
double Beta_op = 3.348 * pow(fpsa, -0.8189); // Optimum packing ratio
double ODBD = wo / fd; // Ovendry bulk density
double Beta = ODBD / pp; // Packing ratio
double WN = woC / (1 + Z[st]); // Net fuel loading
double WN = wo / (1 + st); // Net fuel loading
double A = 133.0 / pow(fpsa, 0.7913); // updated A
double T_max = pow(fpsa,1.5) * pow(495.0 + 0.0594 * pow(fpsa, 1.5),-1.0); // Maximum reaction velocity
double T = T_max * pow((Beta / Beta_op), A) * exp(A * (1 - Beta / Beta_op)); // Optimum reaction velocity
double NM = 1. - 2.59 * (Z[mf] / meC) + 5.11 * pow(mf / meC, 2.) - 3.52 * pow(mf / meC,3.); // Moisture damping coeff.
double NS = 0.174 * pow(Z[se], -0.19); // Mineral damping coefficient
double RI = T * WN * Z[h] * NM * NS;
double NM = 1. - 2.59 * (mf / me) + 5.11 * pow(mf / me, 2.) - 3.52 * pow(mf / me,3.); // Moisture damping coeff.
double NS = 0.174 * pow(se, -0.19); // Mineral damping coefficient
double RI = T * WN * h * NM * NS;
double PFR = pow(192.0 + 0.2595 * fpsa, -1) * exp((0.792 + 0.681 * pow(fpsa, 0.5)) * (Beta + 0.1)); // Propogating flux ratio
// Wind Coefficien t
double B = 0.02526 * pow(fpsa, 0.54);
double C = 7.47 * exp(-0.1333 * pow(fpsa, 0.55));
double E = 0.715 * exp(-3.59 * pow(10, -4) * fpsa);
if (wvC > (0.9 * RI)) wvC = 0.9 * RI;
double WC = (C * pow(wvC, B)) * pow((Beta / Beta_op), (-E));

double SC = 0;
if (Z[slope] >0){ // Rothermel only for upslope, assuming no slope if negative slope is encountered
SC = 5.275*(pow(Beta, -0.3))*pow(Z[slope], 2);
}
if (wv > (0.9 * RI)) wv = 0.9 * RI;
double WC = (C * pow(wv, B)) * pow((Beta / Beta_op), (-E));
double SC = 5.275*(pow(Beta, -0.3))*pow(tan_slope, 2);
// Heat sink
double EHN = exp(-138. / fpsa); // Effective Heating Number = f(surface are volume ratio)
double QIG = 250. + 1116. * mf; // Heat of preignition= f(moisture content)
Expand Down
Loading

0 comments on commit b15f3f5

Please sign in to comment.