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

Add code from #INLINE F90_RCONST_USE at the top of the UPDATE_RCONST subroutine #120

Merged
merged 4 commits into from
Dec 20, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
### Fixed
- Added `char* rootFileName` to functions and function prototypes for `Use_C`, `Use_F`, `Use_F90`, `Use_MATLAB`, and `Generate`
- Updated `docs/requirements.txt` to use `jinja2==3.1.4` (fixes a security issue)
- Updated `gen.c` to write the `INLINED RCONST` section at the top of routine `UPDATE_RCONST`. This will prevent compilation errors when `INLINED_RCONST` contains F90 `USE` statements (as these must precede other F90 statements).

## [3.1.1] - 2024-04-30
### Changed
Expand Down
87 changes: 66 additions & 21 deletions src/gen.c
Original file line number Diff line number Diff line change
Expand Up @@ -2167,38 +2167,41 @@ void GenerateUpdateRconst()
{
int i;
int UPDATE_RCONST;
int YIN,Y;
int YIN, Y;

UseFile( rateFile );

if (useLang==F90_LANG) {
// F90: Declare function with optional YIN argument and local Y variable.
YIN = DefvElmO( "YIN", real, -NVAR, "Optional input concentrations of variable species" );
UPDATE_RCONST = DefFnc( "Update_RCONST", 1, "function to update rate constants");

FunctionBegin( UPDATE_RCONST, YIN );
Y = DefvElm( "Y", real, -NSPEC, "Concentrations of species (local)" );
Declare(Y);
//
// SPECIAL HANDLING FOR F90:
// -------------------------
// Write the INLINED RCONST section immediately after the start of the
// subroutine UPDATE_RCONST. This will avoid compile-time errors if a
// "USE" statement is included via INLINED RCONST. Recall that F90
// "USE" statements must precede variable declarations or any other
// executable statements.
//
// The FunctionBegin() routine writes the variable declaration
// statements immediately after the subroutine declaration line.
// Therefore, we will not be able to use FunctionBegin() to declare
// the UPDATE_RCONST subroutine. Instead, we will manually write
// "SUBROUTINE UPDATE_RCONST ( YIN )" here.
//
// -- Bob Yantosca (19 Dec 2024)
//
bprintf("SUBROUTINE Update_RCONST ( YIN )");
NewLines(1);
} else {
//
// For other languages, declare function w/o any arguments
//
UPDATE_RCONST = DefFnc( "Update_RCONST", 0, "function to update rate constants");
FunctionBegin( UPDATE_RCONST );
}

F77_Inline(" INCLUDE '%s_Global.h'", rootFileName);
MATLAB_Inline("global SUN TEMP RCONST");

switch( useLang ){
case F90_LANG:
WriteComment("Ensure local Y array is filled with variable and constant concentrations");
bprintf(" Y(1:NSPEC) = C(1:NSPEC)\n");
NewLines(1);
WriteComment("Update local Y array if variable concentrations are provided");
bprintf(" if (present(YIN)) Y(1:NVAR) = YIN(1:NVAR)\n");
break;
}

if ( useLang==F77_LANG )
IncludeCode( "%s/util/UserRateLaws_FcnHeader", Home );

Expand All @@ -2223,6 +2226,35 @@ int YIN,Y;
NewLines(1);
WriteComment("End INLINED RCONST");
NewLines(1);
//
// SPECIAL HANDLING FOR F90:
// -------------------------
// Write F90 variable declarations after the INLINED RCONST section.
// This will avoid compile-time errors if a USE statement is placed
// into the INLINED RCONST section, as described above.
//
// -- Bob Yantosca (19 Dec 2024)
//
if (useLang == F90_LANG) {

// Declare optional YIN argument
YIN = DefvElmO( "YIN", real, -NVAR, "Optional input concentrations of variable species" );
Declare(YIN);
NewLines(1);

// Declare local Y variable
Y = DefvElm( "Y", real, -NSPEC, "Concentrations of species (local)" );
Declare(Y);
NewLines(1);

// Copy values of YIN to Y if YIN is present
WriteComment("Ensure local Y array is filled with variable and constant concentrations");
bprintf(" Y(1:NSPEC) = C(1:NSPEC)\n");
NewLines(1);
WriteComment("Update local Y array if variable concentrations are provided");
bprintf(" if (present(YIN)) Y(1:NVAR) = YIN(1:NVAR)\n");
NewLines(2);
}

for( i = 0; i < EqnNr; i++) {
/* mz_rs_20220701+ */
Expand All @@ -2242,9 +2274,22 @@ int YIN,Y;
}

MATLAB_Inline(" RCONST = RCONST(:);");

FunctionEnd( UPDATE_RCONST );
FreeVariable( UPDATE_RCONST );
//
// SPECIAL HANDLING FOR F90:
// -------------------------
// Manually write the "END SUBROUTINE UPDATE_RCONST" line when
// generating F90 output. But if generating C, F77, MatLab output,
// then close the UPDATE_RCONST routine as we normally would.
// -- Bob Yantosca (19 Dec 2024)
//
if (useLang == F90_LANG) {
NewLines(1);
bprintf("END SUBROUTINE UPDATE_RCONST");
NewLines(1);
} else {
FunctionEnd( UPDATE_RCONST );
FreeVariable( UPDATE_RCONST );
}
}


Expand Down