Page Menu
Home
c4science
Search
Configure Global Search
Log In
Files
F63132061
specmicp.cpp
No One
Temporary
Actions
Download File
Edit File
Delete File
View Transforms
Subscribe
Mute Notifications
Award Token
Subscribers
None
File Metadata
Details
File Info
Storage
Attached
Created
Fri, May 17, 23:44
Size
8 KB
Mime Type
text/x-c
Expires
Sun, May 19, 23:44 (2 d)
Engine
blob
Format
Raw Data
Handle
17735584
Attached To
rSPECMICP SpecMiCP / ReactMiCP
specmicp.cpp
View Options
/* =============================================================================
Copyright (c) 2014 - 2016
F. Georget <fabieng@princeton.edu> Princeton University
All rights reserved.
Redistribution and use in source and binary forms, with or without modification,
are permitted provided that the following conditions are met:
1. Redistributions of source code must retain the above copyright notice,
this list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright notice,
this list of conditions and the following disclaimer in the documentation
and/or other materials provided with the distribution.
3. Neither the name of the copyright holder nor the names of its
contributors may be used to endorse or promote products derived from this
software without specific prior written permission.
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. *
============================================================================= */
#include "specmicp_common/io/config_yaml_sections.h"
#include "specmicp_common/io/safe_config.hpp"
#include "specmicp_database/database.hpp"
#include "specmicp_database/io/configuration.hpp"
#include "specmicp_common/physics/io/configuration.hpp"
#include "specmicp/problem_solver/reactant_box.hpp"
#include "specmicp/problem_solver/smart_solver.hpp"
#include "specmicp/adimensional/adimensional_system_solver.hpp"
#include "specmicp/adimensional/adimensional_system_solution.hpp"
#include "specmicp/adimensional/adimensional_system_solver_structs.hpp"
#include "specmicp/adimensional/adimensional_system_structs.hpp"
#include "specmicp/io/configuration.hpp"
#include "specmicp/io/print.hpp"
#include "specmicp_common/log.hpp"
#include "specmicp_common/io/configuration.hpp"
#include "specmicp/io/adimensional_system_solution_saver.hpp"
#include <iostream>
#define SECTION_MAIN "__main__"
#define SECTION_SPECIATION "speciation"
#define SECTION_FORMULATION "formulation"
#define SECTION_CONSTRAINTS "constraints"
#define ATTRIBUTE_NAME "name"
#define ATTRIBUTE_OUTPUT "output"
#define ATTRIBUTE_OUTPUT_DB "output_db"
namespace
specmicp
{
void
check_db
(
const
std
::
string
&
filepath
);
void
run_specmicp
(
io
::
YAMLConfigHandle
&
configuration
);
void
run_formulation
(
io
::
YAMLConfigHandle
&&
conf_formulation
,
const
RawDatabasePtr
&
the_database
,
const
AdimensionalSystemSolverOptions
&
the_options
);
}
const
static
char
*
header
=
"SpecMiCP: Speciation solver based on the complementarity condition
\n
"
"(c) Copyright 2014-2016 fabien Georget <fabieng@princeton.edu>
\n
"
;
const
static
char
*
description
=
R
"plop(
Usage:
specmicp
<
configuration_file
>
specmicp
-
C
/--
check
-
db
<
database
>
Options
:
<
configuration_file
>
the
configuration
file
-
c
<
database
>
,
--
check
-
db
<
database
>
check
that
the
database
can
be
parsed
without
error
-
h
,
--
help
produce
this
help
)
plop
";
const
static
char
*
error_print_help
=
"Use -h to print help"
;
int
main
(
int
argc
,
const
char
*
argv
[])
{
specmicp
::
init_logger
(
&
std
::
cerr
,
specmicp
::
logger
::
Warning
);
if
(
argc
<
2
)
{
std
::
cerr
<<
"Error: expecting one argument. "
;
std
::
cerr
<<
error_print_help
<<
std
::
endl
;
return
EXIT_FAILURE
;
}
std
::
cout
<<
header
<<
std
::
endl
;
const
std
::
string
first_arg
=
argv
[
1
];
if
(
first_arg
[
0
]
==
'-'
)
{
if
(
first_arg
==
"-h"
or
first_arg
==
"--help"
)
{
std
::
cout
<<
description
<<
std
::
endl
;
return
EXIT_SUCCESS
;
}
else
if
(
first_arg
==
"-C"
or
first_arg
==
"--check-db"
)
{
if
(
argc
<
3
)
{
std
::
cerr
<<
"Error : "
<<
first_arg
<<
" option requires an extra argument"
<<
std
::
endl
;
return
EXIT_FAILURE
;
}
else
{
specmicp
::
check_db
(
argv
[
2
]);
return
EXIT_SUCCESS
;
}
}
}
const
std
::
string
conf_file
=
first_arg
;
auto
conf
=
specmicp
::
io
::
YAMLConfigFile
::
load
(
conf_file
);
std
::
unique_ptr
<
std
::
ostream
>
out_log
;
std
::
unique_ptr
<
std
::
ostream
>
conf_log
;
if
(
conf
.
has_section
(
SPC_CF_S_LOGS
))
{
// FIXME
out_log
=
specmicp
::
io
::
configure_log
(
conf
.
get_section
(
SPC_CF_S_LOGS
),
nullptr
);
}
if
(
conf
.
has_section
(
SPC_CF_S_CONF_LOGS
))
{
// FIXME
conf_log
=
specmicp
::
io
::
configure_conf_log
(
conf
.
get_section
(
SPC_CF_S_CONF_LOGS
),
nullptr
);
}
specmicp
::
run_specmicp
(
conf
);
return
EXIT_SUCCESS
;
}
namespace
specmicp
{
RawDatabasePtr
get_db
(
io
::
YAMLConfigHandle
&
configuration
)
{
return
io
::
configure_database
(
configuration
.
get_section
(
SPC_CF_S_DATABASE
));
}
units
::
UnitsSet
get_units
(
io
::
YAMLConfigHandle
&
configuration
)
{
return
io
::
configure_units
(
configuration
.
get_section
(
SPC_CF_S_UNITS
));
}
void
run_specmicp
(
io
::
YAMLConfigHandle
&
configuration
)
{
RawDatabasePtr
the_database
=
get_db
(
configuration
);
units
::
UnitsSet
the_units
=
get_units
(
configuration
);
AdimensionalSystemSolverOptions
the_options
;
io
::
configure_specmicp_options
(
the_options
,
the_units
,
configuration
.
get_section
(
SPC_CF_S_SPECMICP
));
if
(
not
configuration
.
has_section
(
SECTION_SPECIATION
))
{
configuration
.
report_error
(
io
::
YAMLConfigError
::
MissingRequiredSection
,
"No system to solve !"
);
}
auto
conf_formulation
=
configuration
.
get_section
(
SECTION_SPECIATION
);
if
(
conf_formulation
.
is_sequence
())
{
uindex_t
size
=
conf_formulation
.
size
();
for
(
uindex_t
ind
=
0
;
ind
<
size
;
++
ind
)
{
run_formulation
(
conf_formulation
.
get_section
(
ind
),
the_database
,
the_options
);
}
}
else
{
run_formulation
(
std
::
move
(
conf_formulation
),
the_database
,
the_options
);
}
}
void
run_formulation
(
io
::
YAMLConfigHandle
&&
conf
,
const
RawDatabasePtr
&
the_database
,
const
AdimensionalSystemSolverOptions
&
the_options
)
{
auto
name
=
conf
.
get_required_attribute
<
std
::
string
>
(
ATTRIBUTE_NAME
);
std
::
string
save_db
=
""
;
if
(
conf
.
has_attribute
(
ATTRIBUTE_OUTPUT_DB
))
{
save_db
=
conf
.
get_attribute
<
std
::
string
>
(
ATTRIBUTE_OUTPUT_DB
);
}
std
::
string
save_solution
=
""
;
if
(
conf
.
has_attribute
(
ATTRIBUTE_OUTPUT
))
{
save_solution
=
conf
.
get_attribute
<
std
::
string
>
(
ATTRIBUTE_OUTPUT
);
}
else
{
WARNING
<<
"No output file set, the solution will be lost in the void."
;
}
auto
rbox
=
io
::
configure_specmicp_reactant_box
(
the_database
,
the_options
.
units_set
,
std
::
move
(
conf
)
);
auto
constraints
=
rbox
.
get_constraints
(
true
);
if
(
not
save_db
.
empty
())
{
database
::
Database
(
the_database
).
save
(
save_db
);
}
SmartAdimSolver
solver
(
the_database
,
constraints
,
the_options
);
// initialization
if
(
conf
.
has_section
(
SPC_CF_S_INITIALIZATION
))
{
io
::
configure_smart_solver_initialization
(
solver
,
conf
.
get_section
(
SPC_CF_S_INITIALIZATION
)
);
}
// solve the problen
auto
is_solved
=
solver
.
solve
();
if
(
not
is_solved
)
{
CRITICAL
<<
"Failed to solve formulation : "
<<
name
<<
"."
;
return
;
}
if
(
not
save_solution
.
empty
())
{
io
::
save_solution_yaml
(
save_solution
,
the_database
,
solver
.
get_solution
(),
save_db
);
}
}
void
check_db
(
const
std
::
string
&
filepath
)
{
std
::
cout
<<
"Checking database : "
<<
filepath
<<
"
\n
------
\n
"
;
database
::
Database
the_database
(
filepath
);
if
(
not
the_database
.
is_valid
())
{
throw
std
::
invalid_argument
(
"The database is not valid !"
);
}
std
::
cout
<<
"Database is valid !"
<<
std
::
endl
;
}
}
//end namespace specmicp
Event Timeline
Log In to Comment