Skip to content
GitLab
Explore
Sign in
Register
Primary navigation
Search or go to…
Project
O
ogs
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Requirements
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Locked files
Build
Pipelines
Jobs
Pipeline schedules
Test cases
Artifacts
Deploy
Releases
Package registry
Container Registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Service Desk
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Code review analytics
Issue analytics
Insights
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Chaofan Chen
ogs
Commits
60e495ae
Commit
60e495ae
authored
5 months ago
by
Max Jaeschke
Committed by
Dmitri Naumov
5 months ago
Browse files
Options
Downloads
Patches
Plain Diff
Move read binary vector to BaseLib
parent
9ae89761
No related branches found
No related tags found
No related merge requests found
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
BaseLib/FileTools.cpp
+48
-0
48 additions, 0 deletions
BaseLib/FileTools.cpp
BaseLib/FileTools.h
+3
-0
3 additions, 0 deletions
BaseLib/FileTools.h
MathLib/Curve/CreatePiecewiseLinearCurve.cpp
+8
-33
8 additions, 33 deletions
MathLib/Curve/CreatePiecewiseLinearCurve.cpp
with
59 additions
and
33 deletions
BaseLib/FileTools.cpp
+
48
−
0
View file @
60e495ae
...
...
@@ -17,6 +17,7 @@
#include
<spdlog/fmt/bundled/core.h>
#include
<boost/algorithm/string/predicate.hpp>
#include
<boost/endian/conversion.hpp>
#include
<filesystem>
#include
<fstream>
#include
<typeindex>
...
...
@@ -332,6 +333,53 @@ std::vector<T> readBinaryArray(std::string const& filename, std::size_t const n)
template
std
::
vector
<
float
>
readBinaryArray
<
float
>
(
std
::
string
const
&
,
std
::
size_t
const
);
template
<
typename
T
>
std
::
vector
<
T
>
readBinaryVector
(
std
::
string
const
&
filename
)
{
std
::
ifstream
file
(
filename
,
std
::
ios
::
binary
);
if
(
!
file
)
{
OGS_FATAL
(
"File {:s} for curve definition not found"
,
filename
);
}
// Determine file size
file
.
seekg
(
0
,
std
::
ios
::
end
);
std
::
streamsize
size
=
file
.
tellg
();
file
.
seekg
(
0
,
std
::
ios
::
beg
);
size_t
num_elements
=
size
/
sizeof
(
T
);
// Initialize vector with the right size
std
::
vector
<
T
>
result
(
num_elements
);
// Read data directly into the vector
if
(
!
file
.
read
(
reinterpret_cast
<
char
*>
(
result
.
data
()),
size
))
{
OGS_FATAL
(
"Could not read data from file {:s}."
,
filename
);
}
if
constexpr
(
std
::
endian
::
native
!=
std
::
endian
::
little
)
{
boost
::
endian
::
endian_reverse_inplace
(
result
);
}
std
::
streamsize
bytes_read
=
file
.
gcount
();
if
(
bytes_read
!=
size
)
{
OGS_FATAL
(
"Incomplete read: Expected {:d} bytes, but read {:d} bytes "
"from "
"file {:s}."
,
size
,
bytes_read
,
filename
);
}
return
result
;
}
// explicit template instantiation
template
std
::
vector
<
float
>
readBinaryVector
<
float
>
(
std
::
string
const
&
);
template
std
::
vector
<
double
>
readBinaryVector
<
double
>
(
std
::
string
const
&
);
template
<
typename
T
>
void
writeValueBinary
(
std
::
ostream
&
out
,
T
const
&
val
)
{
...
...
This diff is collapsed.
Click to expand it.
BaseLib/FileTools.h
+
3
−
0
View file @
60e495ae
...
...
@@ -92,6 +92,9 @@ template <typename T>
std
::
vector
<
T
>
readBinaryArray
(
std
::
string
const
&
filename
,
std
::
size_t
const
n
);
template
<
typename
T
>
std
::
vector
<
T
>
readBinaryVector
(
std
::
string
const
&
filename
);
/**
* Extracts basename from given pathname with extension.
*
...
...
This diff is collapsed.
Click to expand it.
MathLib/Curve/CreatePiecewiseLinearCurve.cpp
+
8
−
33
View file @
60e495ae
...
...
@@ -29,41 +29,15 @@ std::vector<double> readDoublesFromBinaryFile(const std::string& filename)
{
auto
prj_dir
=
BaseLib
::
getProjectDirectory
();
std
::
string
path_to_file
=
BaseLib
::
joinPaths
(
prj_dir
,
filename
);
std
::
ifstream
file
(
path_to_file
,
std
::
ios
::
binary
);
if
(
!
file
)
{
OGS_FATAL
(
"File {:s} at path {:s} for curve definition not found"
,
filename
,
prj_dir
);
}
// Determine file size
file
.
seekg
(
0
,
std
::
ios
::
end
);
std
::
streamsize
size
=
file
.
tellg
();
file
.
seekg
(
0
,
std
::
ios
::
beg
);
// Initialize vector with the right size
std
::
vector
<
double
>
result
(
size
/
sizeof
(
double
));
// read data directly into the vector
if
(
!
file
.
read
(
reinterpret_cast
<
char
*>
(
result
.
data
()),
size
))
std
::
string
file_extension
=
BaseLib
::
getFileExtension
(
filename
);
if
(
file_extension
!=
".bin"
)
{
OGS_FATAL
(
"Could not read curve definition from file {:s}."
,
filename
);
}
if
constexpr
(
std
::
endian
::
native
!=
std
::
endian
::
little
)
{
// swap endians if needed
std
::
transform
(
result
.
begin
(),
result
.
end
(),
result
.
begin
(),
[](
double
value
)
{
return
std
::
bit_cast
<
double
>
(
boost
::
endian
::
endian_reverse
(
std
::
bit_cast
<
uint64_t
>
(
value
)));
});
OGS_FATAL
(
"Currently only binary files with extension '.bin' supported. The "
"specified file has extension {:s}."
,
file_extension
)
}
return
result
;
return
BaseLib
::
readBinaryVector
<
double
>
(
path_to_file
);
}
PiecewiseLinearCurveConfig
parsePiecewiseLinearCurveConfig
(
...
...
@@ -80,6 +54,7 @@ PiecewiseLinearCurveConfig parsePiecewiseLinearCurveConfig(
auto
const
coords_file_name
=
//! \ogs_file_param{curve__coords}
config
.
getConfigParameter
<
std
::
string
>
(
"coords"
);
auto
const
values_file_name
=
//! \ogs_file_param{curve__values}
config
.
getConfigParameter
<
std
::
string
>
(
"values"
);
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment