Replace EnumOption with EnumerationArrayOption
[alexxy/gromacs.git] / src / gromacs / coordinateio / requirements.cpp
1 /*
2  * This file is part of the GROMACS molecular simulation package.
3  *
4  * Copyright (c) 2019,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.
8  *
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.
13  *
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.
18  *
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.
23  *
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.
31  *
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.
34  */
35 /*!\internal
36  * \file
37  * \brief
38  * Implements helper function to populate requirements from user input.
39  *
40  * \author Paul Bauer <paul.bauer.q@gmail.com>
41  * \ingroup module_coordinateio
42  */
43
44 #include "gmxpre.h"
45
46 #include "requirements.h"
47
48 #include <algorithm>
49
50 #include "gromacs/options/basicoptions.h"
51 #include "gromacs/options/filenameoption.h"
52 #include "gromacs/options/ioptionscontainer.h"
53 #include "gromacs/utility/enumerationhelpers.h"
54 #include "gromacs/utility/exceptions.h"
55
56 namespace gmx
57 {
58
59 //! Mapping for enums from \ref ChangeSettingType.
60 static const EnumerationArray<ChangeSettingType, const char*> c_changeSettingTypeNames = {
61     { "preserved-if-present", "always", "never" }
62 };
63 //! Mapping for enums from \ref ChangeAtomsType.
64 static const EnumerationArray<ChangeAtomsType, const char*> c_changeAtomsTypeNames = {
65     { "preserved-if-present", "always-from-structure", "never", "always" }
66 };
67 /* Currently unused
68 //! Mapping for enums from \ref ChangeFrameInfoType.
69 static const EnumerationArray<ChangeFrameInfoType, const char*> c_changeFrameInfoTypeNames = { {
70 "preserved-if-present", "always" } };
71 //! Mapping for values from \ref ChangeFrameTimeType.
72 static const EnumerationArray<ChangeFrameTimeType, const char*> c_changeFrameTimeTypeNames = { {
73 "preserved-if-present", "starttime", "timestep", "both" } };
74 */
75
76 void OutputRequirementOptionDirector::initOptions(IOptionsContainer* options)
77 {
78     options->addOption(EnumOption<ChangeSettingType>("vel")
79                                .enumValue(c_changeSettingTypeNames)
80                                .store(&velocity_)
81                                .description("Save velocities from frame if possible"));
82     options->addOption(EnumOption<ChangeSettingType>("force")
83                                .enumValue(c_changeSettingTypeNames)
84                                .store(&force_)
85                                .description("Save forces from frame if possible"));
86     options->addOption(
87             EnumOption<ChangeAtomsType>("atoms").enumValue(c_changeAtomsTypeNames).store(&atoms_).description("Decide on providing new atom information from topology or using current frame atom information"));
88     options->addOption(IntegerOption("precision")
89                                .store(&prec_)
90                                .defaultValue(prec_)
91                                .storeIsSet(&setNewPrecision_)
92                                .description("Set output precision to custom value"));
93     options->addOption(RealOption("starttime")
94                                .store(&startTimeValue_)
95                                .defaultValue(startTimeValue_)
96                                .timeValue()
97                                .storeIsSet(&setNewStartTime_)
98                                .description("Change start time for first frame"));
99     options->addOption(RealOption("timestep")
100                                .store(&timeStepValue_)
101                                .defaultValue(timeStepValue_)
102                                .timeValue()
103                                .storeIsSet(&setNewTimeStep_)
104                                .description("Change time between different frames"));
105     options->addOption(RealOption("box")
106                                .vector()
107                                .storeVector(&newBoxVector_)
108                                .valueCount(3)
109                                .storeIsSet(&setNewBox_)
110                                .description("New diagonal box vector for output frame"));
111 }
112
113 OutputRequirements OutputRequirementOptionDirector::process() const
114 {
115     OutputRequirements requirements;
116     /* If the user has just set the values directly without setting the flags,
117      * we set the flags to state that user requested changes are there.*/
118     if (setNewBox_)
119     {
120         requirements.box = ChangeFrameInfoType::Always;
121         clear_mat(requirements.newBox);
122         for (int i = 0; i < DIM; ++i)
123         {
124             requirements.newBox[i][i] = newBoxVector_[i];
125         }
126     }
127     if (setNewPrecision_)
128     {
129         requirements.precision = ChangeFrameInfoType::Always;
130         requirements.prec      = prec_;
131     }
132     if ((setNewTimeStep_ || setNewStartTime_))
133     {
134         requirements.startTimeValue = startTimeValue_;
135         requirements.timeStepValue  = timeStepValue_;
136         if (setNewTimeStep_ && setNewStartTime_)
137         {
138             requirements.frameTime = ChangeFrameTimeType::Both;
139         }
140         else if (setNewTimeStep_)
141         {
142             requirements.frameTime = ChangeFrameTimeType::TimeStep;
143         }
144         else
145         {
146             requirements.frameTime = ChangeFrameTimeType::StartTime;
147         }
148     }
149     requirements.atoms    = atoms_;
150     requirements.velocity = velocity_;
151     requirements.force    = force_;
152     return requirements;
153 }
154
155 } // namespace gmx