add a new variable to mpas history output file and restart file

Questions about modifying the MPAS-Atmosphere code, and general discussion of MPAS code structure and development
Post Reply
bruceyoung01
Posts: 56
Joined: Mon Sep 10, 2018 6:06 pm

add a new variable to mpas history output file and restart file

Post by bruceyoung01 » Tue Jun 04, 2019 5:50 pm

Hi
I want to add a new variable (temperature) to the MPAS history and restart file. The temperature dimension is T(Time, nCells, nVertLevels). I searched in the registry.xml file, but did not find the temperature variable. That means I need to add a new variable to the registry list. Do you have any idea how to do so? Thank you

Best,
Zhifeng

mcurry
Posts: 77
Joined: Mon Oct 29, 2018 5:33 pm
Location: Boulder, Co

Re: add a new variable to mpas history output file and restart file

Post by mcurry » Tue Jun 18, 2019 8:12 pm

Hi Zhifeng,

Yes, you are correct, to add a new variable to an output stream for MPAS you'll need to edit the Registry.xml file. First, you'll want to define the variable in a <var_struct> ... </var_struct> of your choosing. We recommend defining it your own <var_struct> or the "diag" var_struct, but its best to avoid defining the "state" or "mesh" var_struct.

Define your variable in the var_struct as desired, specifying the name, type and dimensions, similar to how other variables are defined. With your variable defined in a var_struct, MPAS will allocate it into the specified MPAS Pool for you to access.

To get it to output within a history stream, you will need to add your variable to be in the stream_list.atmosphere.output, which will tell the stream manager to include this variable within this output stream. You can also add your variable to the streams definition list in the Registry.xml file which will cause it to be included in the corresponding stream_list file by default.

To access your variable within MPAS, you will use the MPAS pool routines to retrieve your allocated data:

Code: Select all

type (block_type), pointer :: block_ptr
type (mpas_pool_type), pointer :: my_var_struct
real (kind=RKIND), dimension(:), pointer :: my_var

block_ptr => domain % blocklist

call mpas_pool_get_subpool(block_ptr % structs, 'my_var_struct', my_var_struct)
call mpas_pool_get_array(my_var_struct, 'my_var', my_var)

my_var(:) = 0
...
You can then access your variable as you wish which will be written out to the corresponding output stream.


Hope that helps! Please let us know if you have any questions.
NCAR|MMM

bruceyoung01
Posts: 56
Joined: Mon Sep 10, 2018 6:06 pm

Re: add a new variable to mpas history output file and restart file

Post by bruceyoung01 » Wed Jul 03, 2019 7:58 pm

In my case, I need to output temperature into both history and restart files. I have a question about the code. Where should I add the code you mentioned above? For my understanding, first, I need to check where temperature (3D) is calculated in MPAS and the temperature variable name. Second, temperature itself is not prognostic variable, but moist potential temperature. It means even I modified temperature in the restart file, the model will not read the modified temperature when restarting. What I need to do is to let MPAS read modified temperature after restarting. Is there any possible to do this? Thank you

mgduda
Posts: 458
Joined: Mon Feb 26, 2018 7:35 pm

Re: add a new variable to mpas history output file and restart file

Post by mgduda » Mon Jul 08, 2019 11:15 pm

I just moved this topic from the "Running" section to the "Code development" section, as it looks like we'll be getting more into a discussion of code changes.
NCAR/MMM

bruceyoung01
Posts: 56
Joined: Mon Sep 10, 2018 6:06 pm

Re: add a new variable to mpas history output file and restart file

Post by bruceyoung01 » Wed Jul 10, 2019 1:23 pm

Michael, do you have any suggestions on the questions above? Thank you

mgduda
Posts: 458
Joined: Mon Feb 26, 2018 7:35 pm

Re: add a new variable to mpas history output file and restart file

Post by mgduda » Wed Jul 10, 2019 7:47 pm

It sounds like adding temperature as an output field is only a part of what you are trying to accomplish. If you'd like to be able to modify the temperature field in a restart file, and to have the model use the new temperature when restarting, then there will be some non-trivial additional code changes that will be needed.

Before proceeding with some non-trivial code modifications, it may be worth considering whether you can accomplish your objective by other means. For example, if your goal is to be able to run the model, write a restart file, modify the temperature field, then restart the model with the modified temperature, an alternative may be to simply convert between theta_m (moist potential temperature) and temperature outside of the model. Could you use a simply Python script to read an MPAS restart file, compute temperature, and save it to a new netCDF file? After modifying the temperature field, you could have a Python script to do the reverse: take the netCDF file with temperature, compute a new theta_m field, and save this to the model restart file?
NCAR/MMM

bruceyoung01
Posts: 56
Joined: Mon Sep 10, 2018 6:06 pm

Re: add a new variable to mpas history output file and restart file

Post by bruceyoung01 » Wed Jul 10, 2019 8:42 pm

This is a great idea which can save me a lot of efforts and time. Before I do that, I'd like to confirm with you the equations used to convert temperature to moist potential temperature (theta_m) or vice versa.

Relationship between theta and theta_m:
theta_m = theta*(1+q_v*R_v/R_d)
This equation is mentioned in Registry.xml file.

Relationship between theta and t:
theta = t*(p_0/p)**(gamma-1/gamma)

I am not sure whether there are equations in MPAS that can directly calculate temperature to theta_m. Thank you for helping me double check it.

mgduda
Posts: 458
Joined: Mon Feb 26, 2018 7:35 pm

Re: add a new variable to mpas history output file and restart file

Post by mgduda » Thu Jul 11, 2019 1:20 am

To convert moist potential temperature (theta_m) to temperature, you can:
1) Divide theta_m by (1.0 + qv * R_v / R_d) to get potential temperature (theta)
2) Multiply theta by the Exner function (exner) to get temperature

The model restart contains the qv, theta_m, and exner fields. If you wanted to use these all in one go, you could compute:
temperature = exner * theta_m / (1.0 + qv * R_v / R_d)

MPAS-Atmosphere assumes R_v = 461.6 and R_d = 287.0 .
NCAR/MMM

bruceyoung01
Posts: 56
Joined: Mon Sep 10, 2018 6:06 pm

Re: add a new variable to mpas history output file and restart file

Post by bruceyoung01 » Thu Jul 11, 2019 1:46 pm

If all the required variables are in the restart file, the second method would be better.
When I took a look into the restart file, I noticed that there are exner_base and exner. I know in WRF that there are P (perturbation pressure) and PB (base state pressure). I am not sure in MPAS whether exner is perturbation exner or not. If it is perturbation exner, then the actual exner is calculated by
exner_actual = exner_base + exner
Just double check with you. Thank you

mgduda
Posts: 458
Joined: Mon Feb 26, 2018 7:35 pm

Re: add a new variable to mpas history output file and restart file

Post by mgduda » Thu Jul 11, 2019 4:52 pm

The 'exner' field in the restart files is the full Exner function, so there is no need to add 'exner_base'. But, I agree that it's always good to double-check!
NCAR/MMM

Post Reply

Return to “Code development”