Skip to content

Remove repetitive code in module_sf_urban.F#1593

Merged
davegill merged 6 commits intowrf-model:developfrom
epn09:refactor_sf_urban
Jan 5, 2022
Merged

Remove repetitive code in module_sf_urban.F#1593
davegill merged 6 commits intowrf-model:developfrom
epn09:refactor_sf_urban

Conversation

@epn09
Copy link
Contributor

@epn09 epn09 commented Dec 7, 2021

TYPE: enhancement

KEYWORDS: urban physics

SOURCE: Do Ngoc Khanh (Tokyo Institute of Technology)

DESCRIPTION OF CHANGES:
Problem: There was a code block repeated 12 times.

Solution: Purely refactor, no change in algorithm.

LIST OF MODIFIED FILES:
M phys/module_sf_urban.F

TESTS CONDUCTED

  1. Confirmed bit-by-bit indentical output, before vs after.
  2. Jenkins tests are all passing.

RELEASE NOTE: Remove repetitive code in module_sf_urban.F

@epn09 epn09 requested a review from a team as a code owner December 7, 2021 12:12
Comment on lines -2692 to -2725
IF( IVGTYP(I,J) == LCZ_1) THEN
UTYPE_URB2D(I,J) = 1 ! high-intensity
UTYPE_URB = UTYPE_URB2D(I,J) ! high-intensity
IF (HGT_URB2D(I,J)>0.) THEN
CONTINUE
ELSE
WRITE(mesg,*) 'USING DEFAULT URBAN MORPHOLOGY'
WRITE_MESSAGE(mesg)
LP_URB2D(I,J)=0.
LB_URB2D(I,J)=0.
HGT_URB2D(I,J)=0.
IF ( sf_urban_physics == 1 ) THEN
MH_URB2D(I,J)=0.
STDH_URB2D(I,J)=0.
DO K=1,4
LF_URB2D(I,K,J)=0.
ENDDO
ELSE IF ( ( sf_urban_physics == 2 ) .or. ( sf_urban_physics == 3 ) ) THEN
DO K=1,num_urban_hi
HI_URB2D(I,K,J)=0.
ENDDO
ENDIF
ENDIF
IF (FRC_URB2D(I,J)>0.and.FRC_URB2D(I,J)<=1.) THEN
CONTINUE
ELSE
WRITE(mesg,*) 'WARNING, FRC_URB2D = 0 BUT IVGTYP IS URBAN'
WRITE_MESSAGE(mesg)
WRITE(mesg,*) 'WARNING, THE URBAN FRACTION WILL BE READ FROM URBPARM.TBL'
WRITE_MESSAGE(mesg)
FRC_URB2D(I,J) = FRC_URB_TBL(UTYPE_URB)
ENDIF
SWITCH_URB=1
ENDIF
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should this repeated block appear one time? Do you remove all occurrences?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@davegill One occurrence is kept in the end. I'll do one test with the urban options, but where should I upload the test result to?

@davegill
Copy link
Contributor

davegill commented Dec 7, 2021

@epn09
It looks as if there is missing code that needs to be included at least one time.

@davegill davegill changed the base branch from master to develop December 7, 2021 13:03
@dudhia
Copy link
Collaborator

dudhia commented Dec 7, 2021 via email

@cenlinhe
Copy link
Contributor

cenlinhe commented Dec 7, 2021

No, these parts should not be removed. These if-statements are used to assign the correct values to UTYPE_URB2D and UTYPE_URB according to the LCZ value in each grid. But I agree that there are some repeated codes within each if-statement that could be further refined. However, since this will not affect the model efficiency or accuracy, I suggest not doing any changes to this part right now, or at least the person who submitted this PR should first check with the original code developer @andreazonato

@epn09
Copy link
Contributor Author

epn09 commented Dec 7, 2021

@cenlinhe I understand the purpose of the original code but from LCZ_1 to LCZ_11, the difference is only in the first two lines (written below), the following lines are purely copy and paste.

           IF( IVGTYP(I,J) == LCZ_1) THEN
               UTYPE_URB2D(I,J) = 1  ! high-intensity

IF( IVGTYP(I,J) == LCZ_9) THEN
UTYPE_URB2D(I,J) = 9 ! high-intensity
UTYPE_URB = UTYPE_URB2D(I,J) ! high-intensity
IF (SWITCH_URB == 1) THEN
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@davegill The diff is too large. The "missing code" that you mentioned starts from here.

@cenlinhe
Copy link
Contributor

cenlinhe commented Dec 8, 2021

@epn09 Yes, you are right. Could you please do a test to see if your refined code produces exactly the same results as the original code? If so, we can approve the change. Also, another suggestion: it seems that your modifications are too scattered (e.g., insert or remove things in between if-else statement, etc.), which makes it difficult to check in the 'Files changed' part. It may be better to remove all the repeated parts first, and then lump together your refined code in one integrated part so that it would be easier to compare the new code and original code.

@epn09 epn09 force-pushed the refactor_sf_urban branch from 7a714dd to 3ac1417 Compare December 8, 2021 04:57
@epn09
Copy link
Contributor Author

epn09 commented Dec 8, 2021

@cenlinhe I have split the original commit into 6 smaller commits. Is it easier to understand for you?
I'm also running a test to confirm if the refined version produce the same result.

@epn09
Copy link
Contributor Author

epn09 commented Dec 9, 2021

@cenlinhe @davegill I have conducted a test with urban options and confirm that the output of the original code and the modified code are exactly the same (bit-by-bit equal). How should I share the result?

@epn09 epn09 requested a review from davegill December 9, 2021 09:52
@andreazonato
Copy link
Contributor

Hi all,

I think these modifications make sense, they decrease the computational time for sure.

The only doubt I have regards the first SWITCH_URB=1. Is it in the right position?

Andrea

@epn09
Copy link
Contributor Author

epn09 commented Dec 9, 2021

Hi @andreazonato,

For your concern, you can take a look at this comit a8d55d7 or the explanation below.

Originally, the code was

SWITCH_URB = 0

IF (IVGTYP(I,J) == ISURBAN) THEN
  ! do some thing for ISURBAN
  SWITCH_URB = 1
ENDIF
IF (IVGTYP(I,J) == LCZ_1) THEN
  ! do some thing for LCZ_1
  SWITCH_URB = 1
ENDIF

IF (SWITCH_URB == 0) THEN
  CONTINUE
ELSE
  ! do something for non-urban
ENDIF

The first two ifs are mutually exclusive, so we can combine with ELSE.

SWITCH_URB = 0

IF (IVGTYP(I,J) == ISURBAN) THEN
  ! do some thing for ISURBAN
  SWITCH_URB = 1
ELSE IF (IVGTYP(I,J) == LCZ_1) THEN
  ! do some thing for LCZ_1
  SWITCH_URB = 1
ENDIF

IF (SWITCH_URB == 0) THEN
  CONTINUE
ELSE
  ! do something for non-urban
ENDIF

The role of SWITCH_URB is to check if we've hit any of ISURBAN, LCZ_1, ..., LCZ_11, thus, initially we can set it to 1, and change it back to 0 if none of the ifs match.

SWITCH_URB = 1

IF (IVGTYP(I,J) == ISURBAN) THEN
  ! do some thing for ISURBAN
ELSE IF (IVGTYP(I,J) == LCZ_1) THEN
  ! do some thing for LCZ_1
ELSE
  SWITCH_URB = 0
ENDIF

IF (SWITCH_URB == 0) THEN
  CONTINUE
ELSE
  ! do something for non-urban
ENDIF

@davegill
Copy link
Contributor

@andreazonato @cenlinhe
Folks,
Have your concerns been addressed? We would like to move these PRs into the develop branch if they are ready. Let us know if you have other issues, or are able to accept these mods, AS-IS. A simple "I am OK with these mods" is sufficient.

@epn09
Copy link
Contributor Author

epn09 commented Dec 28, 2021

@davegill @andreazonato @cenlinhe
Hi, is there any update on this?

@andreazonato
Copy link
Contributor

Yes, in my opinion it makes sense. I agree with the modifications!

@cenlinhe
Copy link
Contributor

It looks good to me too.

@weiwangncar
Copy link
Collaborator

@cenlinhe Can you approve this PR? Click on the 'Files changed' option and see if you can see 'Review changes' in a green box on top right. If not, I will do that.

Copy link
Contributor

@cenlinhe cenlinhe left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This re-structuring of the code looks good and the developer has also confirmed the same results before and after this re-structure.

@davegill davegill requested a review from weiwangncar December 28, 2021 17:49
@davegill
Copy link
Contributor

davegill commented Dec 28, 2021

@epn09
Would you please add your affiliation to the pull request commit message?
Go to #1593

As shown below, click on the three dots, then select "Edit".
Screen Shot 2021-12-28 at 10 50 50 AM

Once you have added your affiliation, at the bottom right, click on the green "Update comment" button.

Screen Shot 2021-12-28 at 10 52 18 AM

@cenlinhe
Copy link
Contributor

@epn09 To keep a record, could you please also send us the results of your bit-by-bit comparison before and after the change? Any summary tables, figures, and/or small-size data files will be good. My email: cenlinhe@ucar.edu

Copy link
Collaborator

@weiwangncar weiwangncar left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The LSM/urban experts have said ok to this code change.

@epn09
Copy link
Contributor Author

epn09 commented Jan 2, 2022

@davegill I have added my affiliation.

@epn09
Copy link
Contributor Author

epn09 commented Jan 4, 2022

@davegill I have sent a copy of my test output to @cenlinhe and he said that it's acceptable.

@davegill davegill merged commit c518c43 into wrf-model:develop Jan 5, 2022
@epn09 epn09 deleted the refactor_sf_urban branch January 5, 2022 01:10
vlakshmanan-scala pushed a commit to scala-computing/WRF that referenced this pull request Apr 4, 2024
TYPE: enhancement

KEYWORDS: urban physics

SOURCE: Do Ngoc Khanh (Tokyo Institute of Technology)

DESCRIPTION OF CHANGES:
Problem: There was a code block repeated 12 times.

Solution: Purely refactor, no change in algorithm.

LIST OF MODIFIED FILES:
M       phys/module_sf_urban.F

TESTS CONDUCTED

1. Confirmed bit-by-bit identical output, before vs after.
2. Jenkins tests are all passing.

RELEASE NOTE: Remove repetitive code in module_sf_urban.F
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants