<chapter id="building_ccsm">
<title>Building CESM</title>

<para>The following summarizes details of building the model exectuable.</para>

<sect1> 
<title> How do I build my model?</title>

<para> After calling <command>cesm_setup</command>, you can build the  model executable 
by running <command>$CASE.build</command>. Running this will:
</para>

<orderedlist>
<listitem><para> create the component namelists in $&RUNDIR; (by
calling the <filename>Buildconf/$component.buildnml.csh</filename>
scripts).
</para></listitem>

<listitem><para> check for
the <link linkend="env_run_inputdata">required input data sets</link>
and download missing data automatically on local disk, and if
successful proceed to the following steps.
</para></listitem>

<listitem><para> create the necessary utility libraries by calling 
<filename>Buildconf/mct.buildlib</filename>, 
<filename>Buildconf/pio.buildlib</filename> and 
<filename>Buildconf/gptl.buildlib</filename> and 
<filename>Buildconf/csm_share.buildlib</filename>. 
</para></listitem>

<listitem><para> create the necessary component libraries by calling
<filename> Buildconf/$component.buildexe.csh</filename>, where
$component is the name of atm, lnd, rof, ocn, cice, glc and cpl components
(which depends on the compset being used). 
</para></listitem>

<listitem><para> create the model executable by calling 
<filename>Buildconf/cesm.buildexe.csh</filename>.
</para></listitem>
</orderedlist>

<para> <filename>$CASEROOT/Tools/Makefile</filename> and
<filename>$CASEROOT/Macros</filename> (generated by calling
<command>cesm_setup</command>) are used to generate the utility and component
libraries and the model executable.  You do not need to change the
default build settings to create the executable. However, since the
&cesm; scripts provide you with a great deal of flexibility in
customizing various aspects of the build process, it is useful to
become familiar with these in order to make optimal use of the
system. </para>

<para>
The <ulink url="../modelnl/env_build.html">&env_build.xml;
variables</ulink>, control various aspects of building the model
executable. Most of the variables should not be modified by users.
Among the variables that you can modify are EXEROOT, RUNDIR,
BUILD_THREADED, DEBUG and GMAKE_J.  A full documentation for each
variable is provided in The <ulink
url="../modelnl/env_build.html">&env_build.xml; variables</ulink>.
</para>

<screen>
> cd $CASEROOT
> $CASE.build
</screen>

<para>
Diagnostic comments will appear as the build proceeds.
The following line indicates that the component namelists have
been generated successfully:
<screen>
....
CCSM BUILDNML SCRIPT HAS FINISHED SUCCESSFULLY
....
</screen>
When the required case input data in $&DIN_LOC_ROOT; has been
successfully checked, you will see:
<screen>
CCSM PRESTAGE SCRIPT STARTING
...
CCSM PRESTAGE SCRIPT HAS FINISHED SUCCESSFULLY
</screen>
</para>

<para>
Finally, the build script generates the utility and component
libraries and the model executable. There should be a line for the mct
and pio libraries, as well as each of the components. Each is date
stamped, and a pointer to the build log file for that library or
component is created. Successful completion is indicated by:
</para>

<screen>
CCSM BUILDEXE SCRIPT HAS FINISHED SUCCESSFULLY
</screen>

<para>
The build log files have names of the form $model.bldlog.$datestamp
and are located in $&RUNDIR;. If they are compressed (indicated by
a .gz file extension), then the build ran successfully.
</para>

<para>
Invoking <command>$CASE.build</command> creates the following
directory structure in $&EXEROOT;:
</para>

<screen>
$EXEROOT/atm
$EXEROOT/cesm
$EXEROOT/cpl
$EXEROOT/csm_share
$EXEROOT/glc
$EXEROOT/ice
$EXEROOT/lib
$EXEROOT/lnd
$EXEROOT/mct
$EXEROOT/ocn
$EXEROOT/pio
$EXEROOT/rof
</screen>

<para>
The atm/, cesm/, cpl/, glc/, ice/, lnd/, ocn/ and rof/ subdirectories
in $&EXEROOT; each contain an 'obj/' directory where the compiled
object files for the target model component is placed.  These object
files are collected into libraries that are placed in 'lib/' along
with the mct/mpeu, pio, gptl, and csm_share libraries. Special include
modules are also placed in lib/include. The model executable
'cesm.exe' is placed directly in $&EXEROOT;.  On the other hand,
component namelists, component logs, output datasets, and restart
files are placed in $&RUNDIR;. It is important to note that in CESM1.1
$&RUNDIR; and $&EXEROOT; are independent variables which are set in
the file <filename>config_machines.xml</filename> in the directory
$<filename>CCSMROOT/scripts/ccsm_utils/Machines/</filename>.
</para>

</sect1>

<!--
     =======================================================================
     -->
<sect1 id="env_run_inputdata">
<title>Input data</title>

<para> All active and data components use input datasets. A local disk
needs $<envar>DIN_LOC_ROOT</envar> to be populated with input data in order to run
&cesm;with these components. For all machines, input data is provided
as part of the release via data from a subversion input data
server. However, on supported machines (and some non-supported
machines), data already exists in the default local-disk input data
area as specified by $<envar>DIN_LOC_ROOT</envar> (see below).</para>

<para> Input data is handled by the build process as follows:</para>

<itemizedlist>

<listitem><para> The buildnml scripts in <filename>Buildconf/i</filename> create listings of
required component input datasets in the
<filename>Buildconf/$component.input_data_list</filename> files.
</para></listitem> 

<listitem><para> <filename>$CASE.build</filename> checks for
the presence of the required input data files in the root directory
$&DIN_LOC_ROOT;. If all required data sets are found on local disk,
then the build can proceed.  </para></listitem>

<listitem><para> If any of the required input data sets are not found,
the build script will abort and the files that are missing will be
listed. At this point, you must obtain the required data from the
<link linkend="input_data_server">input data server</link> using
&check_input_data; with the -export option.  </para></listitem>
</itemizedlist>

<para> The &env_run.xml; variables <envar>DIN_LOC_ROOT</envar> and
<envar>DIN_LOC_ROOT_CLMFORC</envar> determine where you should expect
input data to reside on local disk. See the <ulink
url="../modelnl/env_run.html#run_din">input data variables</ulink>.
</para>

<!-- ======================================================================= -->
<sect2 id="user_input_data">
<title>User-created input data </title>

<para>
If you want to use new user-created dataset(s) and give these
dataset(s) names that are different than the names in $&DIN_LOC_ROOT;,
we recommend using the script &link_dirtree; in the directory
<filename>$CCSMROOT/scripts</filename>.  &link_dirtree; creates a
virtual copy of the input data directory by linking one directory tree
to another.  The full directory structure of the original directory is
duplicated and the files are linked. To use this script, use the -h optiion
for usage.
</para>

<screen>
> cd $CCSMROOT/scripts 
> link_dirtree -h
</screen>

<para>
&link_dirtree; can be conveniently used to generate the equivalent of
a local copy of $&DIN_LOC_ROOT; which can then be
populated with user-specified input datasets.  For example, you can
first generate a virtual copy of $&DIN_LOC_ROOT;
in /user/home/newdata with the following command:
</para>

<screen>
> link_dirtree $DIN_LOC_ROOT /user/home/newdata
</screen>

<para>
then incorporate the new dataset(s) directly into the appropriate
directory in /user/home/newdata. 
</para>

</sect2>

<!-- ======================================================================= -->
<sect2 id="input_data_server">
<title>Using the input data server </title>

<para>
The script <filename>$CASEROOT/check_input_data</filename> determines
if the required data files for the case exist on local disk in the
appropriate subdirectory of $&DIN_LOC_ROOT;. If any of the required
datasets do not exist locally, <command>check_input_data</command>
provides the capability for downloading them to the $&DIN_LOC_ROOT;
directory hierarchy via interaction with the input data server. You
can independently verify that the required data is present locally by
using the following commands:
</para>

<screen>
> cd $CASEROOT
> check_input_data -help
> check_input_data -inputdata $DIN_LOC_ROOT -check
</screen>

<para>
If input data sets are missing, you must obtain the datasets from
the input data server:
</para>

<screen>
> cd $CASEROOT
> check_input_data -inputdata $DIN_LOC_ROOT -export
</screen>

<para>
Required data files not on local disk will be downloaded through
interaction with the Subversion input data server.  These will be
placed in the appropriate subdirectory of $&DIN_LOC_ROOT;.
For what to expect when interacting with a Subversion repository, see
<link linkend="download_ccsm_inputdata">downloading input data</link>.
</para>

</sect2>
</sect1>

<!-- ======================================================================= -->
<sect1 id="rebuild_executable">
<title>Rebuilding the model</title>

<para>
You should rebuild the model under the following circumstances:
</para>

<para> If either &env_build.xml; or <filename>Macros</filename> has
been modified, and/or if code is added to
<filename>SourceMods/src.*</filename>, then it's safest to clean the
build and rebuild from scratch as follows,
</para>

<screen>
> cd $CASEROOT
> $CASE.clean_build
> $CASE.build
</screen>

<para> If you have ONLY modified the PE layout in &env_mach_pes.xml;
(see <link linkend="case_conf_setting_pes">setting the PE layout</link>) 
then it's possible that a clean is not required.</para>
<screen>
> cd $CASEROOT
> $CASE.build
</screen>
<para>
But if the threading has been turned on or off in any component
relative to the previous build, then the build script should fail with
the following error</para>

<screen>
  ERROR SMP STATUS HAS CHANGED
    SMP_BUILD = a0l0i0o0g0c0
    SMP_VALUE = a1l0i0o0g0c0
    A manual clean of your obj directories is strongly recommendend
    You should execute the following:
      ./b39pA1.bluefire.clean_build
    Then rerun the build script interactively
    ---- OR ----
    You can override this error message at your own risk by executing
      ./xmlchange -id SMP_BUILD -val 0 
    Then rerun the build script interactively
</screen>
<para>
and suggest that the model be rebuilt from scratch.
</para>

<para>
You are responsible for manually rebuilding the model when needed. If there is
any doubt, you should rebuild.
</para>

</sect1>

</chapter>

