The interaction of the solar wind with a planetary magnetic field causes electrical currents that modify the magnetic field distribution around the planet. We present an approach to estimating the planetary magnetic field from in situ spacecraft data using a magnetohydrodynamic (MHD) simulation approach. The method is developed with respect to the upcoming BepiColombo mission to planet Mercury aimed at determining the planet's magnetic field and its interior electrical conductivity distribution. In contrast to the widely used empirical models, global MHD simulations allow the calculation of the strongly time-dependent interaction process of the solar wind with the planet. As a first approach, we use a simple MHD simulation code that includes time-dependent solar wind and magnetic field parameters. The planetary parameters are estimated by minimizing the misfit of spacecraft data and simulation results with a gradient-based optimization. As the calculation of gradients with respect to many parameters is usually very time-consuming, we investigate the application of an adjoint MHD model. This adjoint MHD model is generated by an automatic differentiation tool to compute the gradients efficiently. The computational cost for determining the gradient with an adjoint approach is nearly independent of the number of parameters. Our method is validated by application to THEMIS (Time History of Events and Macroscale Interactions during Substorms) magnetosheath data to estimate Earth's dipole moment.

Planets with an intrinsically generated magnetic field, such as Earth or Mercury, interact with the solar wind. This causes electrical currents that modify the planetary magnetic field. The properties of the interaction not only depend on the planetary magnetic field but also on the continuously varying solar wind conditions. A spacecraft orbiting a planet in such a highly variable environment measures the modified magnetic field distribution.

In 2025 the BepiColombo mission

So far, the planetary magnetic field of Mercury has been determined using
empirical models of the interaction between the solar wind and the planetary
magnetic field with spacecraft data from MESSENGER or Mariner 10

Mercury's magnetic field close to the subsolar magnetopause has a strength of about
60

As a first approach, we consider a simple MHD simulation code based on the
MHD code presented by

In this paper we investigate the applicability of an adjoint approach to a MHD simulation code using automatic
differentiation

Our approach to estimating planetary parameters using data from a
multi-spacecraft mission is validated with the THEMIS (Time History of Events
and Macroscale Interactions during Substorms) mission

The interaction of the planetary magnetic field with the solar wind is
computed by a MHD simulation code. The MHD simulation has to be efficient to
perform the time-consuming estimation procedure of the planetary parameters.
Furthermore, the simulation code should be simple in its numerical
implementation structure to simplify the application of the adjoint approach
using automatic differentiation. For these reasons, as a first approach, a
simple MHD simulation code is developed, which is based on the simulation code
described by

The magnetic field in the simulation code by

The lowest-order coefficients for

Dipole coefficients of the IGRF in 2010. On the left side are the
Gauss coefficients according to

Similar to the dipole, higher-order moments of the planetary magnetic field
can be taken into account. Thereby, the tensor structure of the Cartesian
representation becomes more complex for higher orders. For example, the
quadrupole can be expressed by a symmetric, traceless matrix

The simulation code according to

The interaction of the solar wind with the planetary magnetic field is
calculated by solving the MHD equations. Thereby, these equations are solved
within a box sketched in Fig.

The MHD equations provide solutions for the mass density

The simulation box contains the planet with its magnetic field. The
origin of the coordinate system is in the planet's center and the

According to

Boundary conditions are required to solve the MHD equations. The inflow
boundary conditions at

In addition to the boundary conditions, our simulation requires initial
conditions. Therefore, at time step

The extended magnetic field geometry, especially the arbitrarily aligned
dipole moment, can cause a complex motion of the plasma in the magnetosphere
due to co-rotation of the plasma or magnetic reconnection, for example. Therefore,
different from the simulation described by

Density and pressure gradients across the planetary surface are related to forces if the MHD equations are solved within the entire simulation box. To minimize this interaction, the density and the pressure at the planetary surface are set to the values outside the planet.

Density and pressure gradients between the planet's interior and the plasma
outside must not cause forces on the plasma. However, the MHD equations are
solved within the entire simulation domain. This can lead to an interaction
as sketched in Fig.

In contrast to the density and gas pressure, the magnetic field can interact
with the planet due to electromagnetic induction, which is additionally implemented. Time-dependent variations in the magnetic field inside the
planet are calculated by Eq. (

The simulation uses solar wind parameters as boundary conditions. With
respect to the two spacecraft in mission BepiColombo, simultaneous observations
of the solar wind as well as the magnetic field close to the planet will be
available in the future. Thereby, the solar wind conditions can be determined
either directly by in situ measurements or by using the reconstruction method
by

For a comparison between simulation results and spacecraft data, the data
need to be transferred into MSP coordinates. Therefore, the THEMIS
data are first transferred into geographic (GEO) coordinates. Vectors in these
coordinates can be transferred into MSP coordinates by rotation matrices

For the validation of the code, the known planetary dipole moment components
of Earth according to Table

To validate the modified simulation code, we compare a simulation using the
known dipole moment of Earth according to Table

THC magnetosheath data (blue) observed on 24 August 2008 and the corresponding MHD simulation results (black) using the solar wind observations of THB (red).

The bow shock is observed at about 00:30 UT and the magnetopause at about
03:30 UT in accordance with the simulation results. Most physical quantities
show a good agreement between actual observations and simulation results.
Only the ion density in the magnetosheath is observed to be higher than in
the simulation. Furthermore, the magnetic field in the magnetosphere is about
15

In the previous section, spacecraft data were qualitatively compared to the
results of the MHD simulation. To quantify the deviations, a cost function is
introduced. Therefore, the method of least squares is used. The sum of
squared residuals, FQ, of

A gradient-based optimization can be used to minimize the cost function with
respect to the parameters of the model

The algorithm requires the gradient of the cost function

Each calculation of the cost function for a certain set of parameters
requires a full global MHD simulation of the data along the spacecraft's
trajectory. Thus,

Another possibility to calculate the gradient

The left column sketches the time iteration of the MHD simulation
code starting from the initial state at

The function

This procedure is called forward differentiation because the gradient is
calculated parallel to the execution of the time loop in the simulation code.
The advantage over the computation of the gradient using difference quotients
according to Eq. (

An AD tool is able to differentiate a numerical code automatically, i.e., the tool can be applied without considering details of the implementation. However, for complex numerical codes, such as MHD simulation codes, problems might occur. For example, codes using parallel computing function calls by the message-passing interface (MPI), as they are also used for our simulation code, usually need further treatment. The analytical forward differentiation with an AD tool is also called automatic forward differentiation.

The computational costs for the calculation of the gradient with difference
quotients or using automatic forward differentiation do not differ much.
However, the latter procedure leads to a more efficient approach, the adjoint
method. The adjoint method is extensively used for optimization problems in
fluid dynamics, e.g., drag minimization by variations in surface geometry

The adjoint method can be introduced with systems of linear equations, as
described briefly in the following

Another possibility is to use the adjoint method. To deduce the method, the
product of a vector

The computational costs are mainly determined by the number of multiplications
and differ for both possibilities of calculating the scalar
product (Eq.

The adjoint approach can be applied to the calculation of the gradient in
Eq. (

During the analytical forward differentiation, the gradient is computed
successively using chain rule from the right to the left. This corresponds to
a procedure, where, at first, the system of linear Eq. (

The dimensions of the vectors and matrices involved are

However, the numerical implementation of the adjoint method is more difficult
than the analytical forward differentiation. As described, the calculation of
the gradient with the analytical forward differentiation is parallel to the
execution of the time loop in the simulation code. In contrast, the solution
at the last time iteration

There are AD tools that can derive codes not only according to forward
differentiation but also according to the adjoint method. However, the
available memory on a computer is often too small to store all the required
results in the central memory. The memory consumption

To minimize the access to the hard disk, checkpointing can be used.
Thereby, the main iteration loop of the algorithm is split at certain checkpoints into smaller loops.
Then, the smaller loop iterates over

Note that instead of using only observations of a single spacecraft,
simultaneous measurements from multiple spacecraft at different locations can
be calculated in Eq. (

The relative errors for gradients determined by difference quotients and the adjoint method for the dipole components. Thereby, on the left side, different points in parameter space are considered. On the right side, the dependence of the error on a different number of time iteration steps is shown.

The runtime of calculating gradients using the adjoint method

The AD tool applied for an automatic backward differentiation transfers a
numerical code for the calculation of a cost function into an adjoint code,
which can compute the gradient according to the adjoint approach. This was
done for the MHD simulation code presented in the previous chapter by the TAF
tool of the company FastOpt. Thereby, the parameter space of the dipole and
quadrupole parameters

To validate the adjoint MHD simulation code, the gradients produced by the
adjoint code are compared to those calculated by difference quotients
according to Eq. (

Now, the interaction of the planet with the solar wind is taken into account.
Thereby, the gradients calculated by the adjoint method can be compared to
gradients computed by difference quotients for a different number of time
iterations. The corresponding relative errors of the dipole components of the
gradient are shown in Fig.

Estimation of planetary moments neglecting the interaction with the solar wind and using synthetic data. The reconstructions shown in the upper panels take only a dipole moment into account. These two results consider a different spacecraft trajectory, with a trajectory closer to the planet corresponding to the left figure. All reconstructions presented use the same dipole moment for calculating the synthetic spacecraft data. However, in the lower panels, an additional quadrupole component was introduced. The reconstructions in the lower panels use a different choice of the initial parameters for the estimation.

To determine the runtime of the adjoint code, the gradient calculations are
performed on a test computer for different numbers of time iteration steps.
The test computer uses 64 GB of central memory and has an Intel Xeon E5
processor with 12 cores and 24 threads at 2.5 GHz. The results are
shown in Fig.

Consequently, the test computer configuration is not optimal to achieve the best performance. The performance can be improved by using a computer cluster with distributed memory space. Then, each core can access its own memory space and checkpointing can be avoided. This can increase the performance. Furthermore, it should be noted that without additional computational costs and memory requirements, more parameters can be introduced in the estimation process of the adjoint approach, such as octupole planetary magnetic field parameters.

At first, the results of data assimilation using synthetically produced data
are considered, neglecting the interaction of the planetary magnetic field. The
simulation box has a length of 60.2

The first trajectory considered here is

Next, a different trajectory is considered to produce the synthetic data:

Estimation of Earth's dipole moment from magnetosheath data of
THC on 24 August 2008 using the THB solar wind observations, which are presented
in Fig.

The simultaneous estimation of dipole and quadrupole parameters is considered
as well by using magnetic field data

After proving the general functionality of the algorithm, it is applied using
THEMIS spacecraft data at Earth. Thereby, data from the magnetosheath, a
region strongly influenced by the interaction process of the solar wind, is
considered. Different to the situation at Earth, spacecraft magnetic field
observations in Mercury's magnetosphere are strongly modified due to the
magnetosphere's small size. Here, we restrict our approach to Earth's
magnetosheath data to consider a strongly disturbed magnetic environment
comparable to the situation at Mercury. However, in final application,
magnetospheric data will be used as well to reduce errors. Due to the weak
components of the quadrupole at Earth, their influence is negligible
close to the magnetopause. The largest quadrupole moment corresponds to a
magnetic field of

The magnetosheath transition observed by THC on 24 August 2008, presented in
Fig.

We introduced an approach to estimating planetary parameters using global MHD simulations of the interaction of the solar wind with a planet. A simple MHD simulation code was introduced and prepared for an automatic differentiation tool to obtain an adjoint MHD simulation code. The differences of spacecraft data and corresponding simulation results are quantified by a cost function, which is minimized by a gradient-based optimization. The adjoint code computes the gradient with lower computational effort compared to a difference quotient calculation.

Our approach is designed to estimate planetary magnetic fields, especially if
the field strength is weak so that the interaction strongly modifies the
magnetic field of the planet's environment. We used THEMIS data of Earth's magnetosheath to simulate such an environment to test our approach.
The results of the estimation process can be affected by statistical and systematic errors. Therefore, statistical errors will not contribute to the
mean values of the estimated planetary magnetic field if a sufficiently large
number of magnetosheath transitions are considered. For example, the solar
wind density can be measured incorrectly due to a spacecraft potential

We used a simple MHD simulation code to investigate the automatic
differentiation procedure. As a next step, the simulation code needs to be
improved, e.g., by an adaptive mesh refinement, to reduce numerical errors.
Also, kinetic or hybrid simulation codes can be considered and treated with
an automatic differentiation tool. The limiting factor for applying the
automatic differentiation is not the complexity of the code but the memory
consumption. Using our test computer, the adjoint approach was about 33 %
faster than a finite difference approach. Although the adjoint MHD code
does not calculate the gradient very much faster than using difference quotients,
it has the advantage that further parameters such as higher-order magnetic
field moments or parameters of the planet's conductivity can be included with
nearly no additional computational costs. Nonetheless, the performance of the
adjoint code is, related to memory limitations of our test computer, much
lower than expected from theory. Thus, as a further step, the test computer
configuration needs to be modified to increase performance. It is beneficial
for the adjoint approach that each core has access to its own memory, which is
different from our test computer. Thus, instead of using traditional
supercomputers with fewer more powerful computers, a computer cluster using
many commoditized computers with their own memory should be considered. These
computer configurations recently became very popular in big data analysis
using Google's well-known MapReduce technique

Data from the THEMIS mission are publicly available and can
be obtained from

The authors declare that they have no conflict of interest.

This work is financially supported by the German Ministerium für Wirtschaft und Energie and the German DLR under grants 50OC1403 and 50QW1501. We acknowledge NASA contract NAS5-02099 and V. Angelopoulos for use of data from the THEMIS mission. We specifically thank C. W. Carlson and J. P. McFadden for use of ESA data. We thank T. Ogino and K. Fukazawa for providing a version of their MHD simulation code. Furthermore, we thank FastOpt (R. Giering and T. Kaminski) for applying their TAF tool for the automatic differentiation procedure. The topical editor, M. Palmroth, thanks I. Honkonen and the anonymous referee for help in evaluating this paper.