2 * This file is part of the GROMACS molecular simulation package.
4 * Copyright (c) 2020, by the GROMACS development team, led by
5 * Mark Abraham, David van der Spoel, Berk Hess, and Erik Lindahl,
6 * and including many others, as listed in the AUTHORS file in the
7 * top-level source directory and at http://www.gromacs.org.
9 * GROMACS is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU Lesser General Public License
11 * as published by the Free Software Foundation; either version 2.1
12 * of the License, or (at your option) any later version.
14 * GROMACS is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * Lesser General Public License for more details.
19 * You should have received a copy of the GNU Lesser General Public
20 * License along with GROMACS; if not, see
21 * http://www.gnu.org/licenses, or write to the Free Software Foundation,
22 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
24 * If you want to redistribute modifications to GROMACS, please
25 * consider that scientific software is very special. Version
26 * control is crucial - bugs must be traceable. We will be happy to
27 * consider code for inclusion in the official distribution, but
28 * derived work must not be called official GROMACS. Details are found
29 * in the README & COPYING files - if they are missing, get the
30 * official version at http://www.gromacs.org.
32 * To help us fund GROMACS development, we humbly ask that you cite
33 * the research papers on the package. Check out http://www.gromacs.org.
38 #include "simulationinputhandle.h"
42 #include "gromacs/mdrun/legacymdrunoptions.h"
48 * SimulationInput needs much more discussion and refinement before it should be used directly.
49 * Use the interface defined in the headers and refer to #3652 for updates.
51 * Design note: It is probably sufficient for future versions to compose SimulationInput
52 * through a Builder rather than to subclass an Interface or base class. Outside of this
53 * translation unit, we should avoid coupling to the class definition until/unless we
54 * develop a much better understanding of simulation input portability.
60 SimulationInput(const char* tprFilename, const char* cpiFilename) :
61 tprFilename_(tprFilename),
62 cpiFilename_(cpiFilename)
66 std::string tprFilename_;
67 std::string cpiFilename_;
71 class detail::SimulationInputHandleImpl final
74 explicit SimulationInputHandleImpl(std::shared_ptr<SimulationInput> input) :
75 simulationInput_{ std::move(input) }
79 SimulationInputHandleImpl(const SimulationInputHandleImpl&) = default;
80 SimulationInputHandleImpl& operator=(const SimulationInputHandleImpl&) = default;
81 SimulationInputHandleImpl(SimulationInputHandleImpl&&) noexcept = default;
82 SimulationInputHandleImpl& operator=(SimulationInputHandleImpl&&) noexcept = default;
84 [[nodiscard]] SimulationInput* simulationInput() const { return simulationInput_.get(); }
86 /*! \brief Boolean interpretation checks for presence of payload.
88 * \return true if handle has a SimulationInput, else false.
90 operator bool() const { return bool(simulationInput_); }
93 // Note that we should not need to worry about managing the SimulationInput
94 // Deleter as long as we manage the SimulationInputHolderImpl Deleter and
95 // SimulationInputHolderImpl is the only way to own a SimulationInput.
96 // BUT these semantics may change, and we should try to be responsible about
97 // consistent Allocator implementation.
98 std::shared_ptr<SimulationInput> simulationInput_;
102 * TODO: Improve doxygen source scoping.
103 * It seems surprising that doxygen should be looking at this source file, but we
104 * need to exclude the following definitions to avoid warnings.
105 * Possibly related to https://gitlab.com/gromacs/gromacs/-/issues/3402
107 detail::SimulationInputHandleImplDeleter::SimulationInputHandleImplDeleter() = default;
109 detail::SimulationInputHandleImplDeleter::SimulationInputHandleImplDeleter(
110 const SimulationInputHandleImplDeleter&) noexcept = default;
112 detail::SimulationInputHandleImplDeleter::SimulationInputHandleImplDeleter(
113 SimulationInputHandleImplDeleter&&) noexcept = default;
115 detail::SimulationInputHandleImplDeleter& detail::SimulationInputHandleImplDeleter::
116 operator=(const SimulationInputHandleImplDeleter&) noexcept = default;
118 detail::SimulationInputHandleImplDeleter& detail::SimulationInputHandleImplDeleter::
119 operator=(SimulationInputHandleImplDeleter&&) noexcept = default;
121 void detail::SimulationInputHandleImplDeleter::operator()(SimulationInputHandleImpl* impl) const
126 SimulationInputHandle::SimulationInputHandle() = default;
128 SimulationInputHandle::~SimulationInputHandle() = default;
130 SimulationInputHandle::SimulationInputHandle(std::unique_ptr<detail::SimulationInputHandleImpl> impl) :
131 impl_(impl.release())
135 SimulationInputHandle::SimulationInputHandle(const SimulationInputHandle& source)
137 // Dynamically allocate a new holder implementation object using the copy constructor.
138 // Note that make_unique does not provide for custom deleters, and there is no default
139 // overload to move construct/assign from basic unique_ptr to one with custom deleter,
140 // but more elaborate creation functions for SimulationInputHolderImpl seem like
141 // overkill at this point. Still, we use make_unique to confine constructor errors
142 // to the following exception-safe line.
143 auto impl = std::make_unique<detail::SimulationInputHandleImpl>(*source.impl_);
144 // Move ownership of the heap object to the handle with a custom deleter.
145 impl_.reset(impl.release());
148 SimulationInputHandle& SimulationInputHandle::operator=(const SimulationInputHandle& source)
152 // Dynamically allocate a new holder implementation object using the copy constructor.
153 // Note that make_unique does not provide for custom deleters, and there is no default
154 // overload to move construct/assign from basic unique_ptr to one with custom deleter,
155 // but more elaborate creation functions for SimulationInputHolderImpl seem like
156 // overkill at this point. Still, we use make_unique to confine constructor errors
157 // to the following exception-safe line.
158 auto impl = std::make_unique<detail::SimulationInputHandleImpl>(*source.impl_);
159 // Move ownership of the heap object to the handle with a custom deleter.
160 impl_.reset(impl.release());
166 SimulationInput* SimulationInputHandle::get() const noexcept
172 return impl_->simulationInput();
175 SimulationInputHandle::operator bool() const
184 SimulationInputHandle makeSimulationInput(const LegacyMdrunOptions& options)
186 // Note: it seems clear that we will want a SimulationInput to be linked to
187 // a communications context (whether the SimulationContext or something higher level)
188 // so that it can encapsulate the data locality details. Otherwise, we have
189 // to choose whether to read the files everywhere or just to store the
190 // filenames until a communications context is known.
191 const auto* const tprFilename = ftp2fn(efTPR, options.filenames.size(), options.filenames.data());
192 const auto* const cpiFilename = opt2fn("-cpi", options.filenames.size(), options.filenames.data());
193 auto simulationInput = std::make_unique<SimulationInput>(tprFilename, cpiFilename);
194 auto impl = std::make_unique<detail::SimulationInputHandleImpl>(std::move(simulationInput));
196 return SimulationInputHandle(std::move(impl));
199 } // end namespace gmx