Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
E
Eisgenerator
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
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
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
GitLab community forum
Contribute to GitLab
Provide feedback
Terms and privacy
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Klemm, Carl Philipp
Eisgenerator
Commits
7ca549d3
Commit
7ca549d3
authored
3 years ago
by
Carl Klemm
Browse files
Options
Downloads
Patches
Plain Diff
add support for sweeping parameters
parent
9301fdeb
No related branches found
No related tags found
No related merge requests found
Changes
3
Show whitespace changes
Inline
Side-by-side
Showing
3 changed files
main.cpp
+40
-8
40 additions, 8 deletions
main.cpp
model.cpp
+97
-1
97 additions, 1 deletion
model.cpp
model.h
+14
-0
14 additions, 0 deletions
model.h
with
151 additions
and
9 deletions
main.cpp
+
40
−
8
View file @
7ca549d3
...
@@ -4,12 +4,7 @@
...
@@ -4,12 +4,7 @@
#include
"model.h"
#include
"model.h"
double
omegafn
(
size_t
i
)
void
runSingle
()
{
return
i
*
10000
;
}
int
main
(
int
argc
,
char
**
argv
)
{
{
std
::
string
modelStr
(
"r{20e3}p{1e-7, 0.9}"
);
std
::
string
modelStr
(
"r{20e3}p{1e-7, 0.9}"
);
...
@@ -29,9 +24,10 @@ int main(int argc, char** argv)
...
@@ -29,9 +24,10 @@ int main(int argc, char** argv)
std
::
cout
<<
"}
\n
"
;
std
::
cout
<<
"}
\n
"
;
}
}
Model
::
Range
omega
(
0
,
1e6
,
50
);
auto
start
=
std
::
chrono
::
high_resolution_clock
::
now
();
auto
start
=
std
::
chrono
::
high_resolution_clock
::
now
();
for
(
size_t
i
=
0
;
i
<
101
;
++
i
)
results
=
model
.
sweep
(
omega
);
results
.
push_back
(
model
.
execute
(
omegafn
(
i
)));
auto
end
=
std
::
chrono
::
high_resolution_clock
::
now
();
auto
end
=
std
::
chrono
::
high_resolution_clock
::
now
();
auto
duration
=
std
::
chrono
::
duration_cast
<
std
::
chrono
::
microseconds
>
(
end
-
start
);
auto
duration
=
std
::
chrono
::
duration_cast
<
std
::
chrono
::
microseconds
>
(
end
-
start
);
...
@@ -39,5 +35,41 @@ int main(int argc, char** argv)
...
@@ -39,5 +35,41 @@ int main(int argc, char** argv)
std
::
cout
<<
"omega: "
<<
res
.
omega
<<
" real = "
<<
res
.
im
.
real
()
<<
" im = "
<<
res
.
im
.
imag
()
<<
'\n'
;
std
::
cout
<<
"omega: "
<<
res
.
omega
<<
" real = "
<<
res
.
im
.
real
()
<<
" im = "
<<
res
.
im
.
imag
()
<<
'\n'
;
std
::
cout
<<
"time taken: "
<<
duration
.
count
()
<<
" us"
<<
'\n'
;
std
::
cout
<<
"time taken: "
<<
duration
.
count
()
<<
" us"
<<
'\n'
;
}
void
sweepCb
(
std
::
vector
<
Model
::
DataPoint
>&
data
)
{
static
size_t
i
=
0
;
++
i
;
if
((
i
&
0x3FF
)
==
0
)
std
::
cout
<<
'.'
<<
std
::
flush
;
}
void
runSweep
()
{
std
::
string
modelStr
(
"r{20e3}p{1e-7, 0.9}"
);
std
::
vector
<
Model
::
DataPoint
>
results
;
Model
model
(
modelStr
);
std
::
vector
<
Model
::
Range
>
parameters
;
parameters
.
push_back
(
Model
::
Range
(
1e3
,
50e3
,
100
));
parameters
.
push_back
(
Model
::
Range
(
1e-7
,
20e-7
,
100
));
parameters
.
push_back
(
Model
::
Range
(
0.7
,
1.2
,
100
));
Model
::
Range
omega
(
0
,
1e6
,
25
);
auto
start
=
std
::
chrono
::
high_resolution_clock
::
now
();
model
.
sweepParams
(
parameters
,
omega
,
&
sweepCb
);
auto
end
=
std
::
chrono
::
high_resolution_clock
::
now
();
auto
duration
=
std
::
chrono
::
duration_cast
<
std
::
chrono
::
milliseconds
>
(
end
-
start
);
std
::
cout
<<
"
\n
time taken: "
<<
duration
.
count
()
<<
" ms"
<<
'\n'
;
}
int
main
(
int
argc
,
char
**
argv
)
{
runSingle
();
runSweep
();
return
0
;
return
0
;
}
}
This diff is collapsed.
Click to expand it.
model.cpp
+
97
−
1
View file @
7ca549d3
...
@@ -230,6 +230,101 @@ std::vector<Componant*> Model::getFlatComponants()
...
@@ -230,6 +230,101 @@ std::vector<Componant*> Model::getFlatComponants()
return
getFlatComponants
();
return
getFlatComponants
();
}
}
std
::
vector
<
Model
::
DataPoint
>
Model
::
sweep
(
const
Range
&
omega
)
{
double
step
=
(
omega
.
end
-
omega
.
start
)
/
omega
.
count
;
double
currOmega
=
omega
.
start
;
std
::
vector
<
DataPoint
>
results
;
results
.
reserve
(
omega
.
count
);
for
(
size_t
i
=
0
;
i
<
omega
.
count
;
++
i
)
{
results
.
push_back
(
execute
(
currOmega
));
currOmega
+=
step
;
}
return
results
;
}
bool
Model
::
sweepParams
(
const
std
::
vector
<
Range
>&
componantRanges
,
const
Range
&
omega
,
std
::
function
<
void
(
std
::
vector
<
DataPoint
>&
)
>
dataCb
)
{
size_t
parametersCount
=
getFlatParametersCount
();
if
(
componantRanges
.
size
()
!=
parametersCount
)
{
std
::
cout
<<
"Error: a parameter range must be provided for eatch componant parameter
\n
"
;
return
false
;
}
for
(
size_t
i
=
0
;
i
<
parametersCount
;
++
i
)
{
if
(
componantRanges
[
i
].
count
==
0
||
(
componantRanges
[
i
].
count
<
2
&&
componantRanges
[
i
].
start
!=
componantRanges
[
i
].
end
))
{
std
::
cout
<<
"Error: paramter range must specify at least one paramter point if only one paramer point is specified star and end must be the same
\n
"
;
return
false
;
}
else
if
(
componantRanges
[
i
].
start
>
componantRanges
[
i
].
end
)
{
std
::
cout
<<
"Error: paramter range end-start must be positive
\n
"
;
return
false
;
}
}
size_t
stepsRequired
=
1
;
for
(
size_t
i
=
0
;
i
<
parametersCount
;
++
i
)
stepsRequired
*=
componantRanges
[
i
].
count
;
std
::
vector
<
double
>
currentParam
(
parametersCount
,
0
);
std
::
vector
<
double
>
stepSize
(
parametersCount
,
0
);
for
(
size_t
i
=
0
;
i
<
parametersCount
;
++
i
)
{
currentParam
[
i
]
=
componantRanges
[
i
].
start
;
stepSize
[
i
]
=
(
componantRanges
[
i
].
end
-
componantRanges
[
i
].
start
)
/
componantRanges
[
i
].
count
;
}
std
::
cout
<<
"Executing sweep. Steps requried: "
<<
stepsRequired
<<
std
::
endl
;
for
(
size_t
i
=
0
;
i
<
stepsRequired
;
++
i
)
{
for
(
size_t
i
=
0
;
i
<
parametersCount
;
++
i
)
{
currentParam
[
i
]
+=
stepSize
[
i
];
if
(
currentParam
[
i
]
>
componantRanges
[
i
].
end
)
currentParam
[
i
]
=
componantRanges
[
i
].
start
;
else
break
;
}
std
::
vector
<
DataPoint
>
result
=
sweep
(
omega
);
dataCb
(
result
);
}
return
true
;
}
bool
Model
::
setFlatParameters
(
const
std
::
vector
<
double
>&
parameters
)
{
if
(
parameters
.
size
()
!=
getFlatParametersCount
())
return
false
;
size_t
i
=
0
;
for
(
Componant
*
componant
:
getFlatComponants
())
{
std
::
vector
<
double
>
params
;
for
(
size_t
j
=
0
;
j
<
componant
->
paramCount
();
++
j
)
params
.
push_back
(
parameters
[
i
++
]);
componant
->
setParam
(
params
);
}
return
true
;
}
size_t
Model
::
getFlatParametersCount
()
{
size_t
count
=
0
;
for
(
Componant
*
componant
:
getFlatComponants
())
count
+=
componant
->
paramCount
();
return
count
;
}
char
Model
::
getComponantChar
(
Componant
*
componant
)
char
Model
::
getComponantChar
(
Componant
*
componant
)
{
{
if
(
dynamic_cast
<
Resistor
*>
(
componant
))
if
(
dynamic_cast
<
Resistor
*>
(
componant
))
...
@@ -237,9 +332,10 @@ char Model::getComponantChar(Componant* componant)
...
@@ -237,9 +332,10 @@ char Model::getComponantChar(Componant* componant)
if
(
dynamic_cast
<
Cap
*>
(
componant
))
if
(
dynamic_cast
<
Cap
*>
(
componant
))
return
'c'
;
return
'c'
;
if
(
dynamic_cast
<
Cpe
*>
(
componant
))
if
(
dynamic_cast
<
Cpe
*>
(
componant
))
return
'p'
;
return
'p'
;
return
'x'
;
return
'x'
;
}
}
This diff is collapsed.
Click to expand it.
model.h
+
14
−
0
View file @
7ca549d3
...
@@ -3,6 +3,7 @@
...
@@ -3,6 +3,7 @@
#include
<complex>
#include
<complex>
#include
<string>
#include
<string>
#include
<vector>
#include
<vector>
#include
<functional>
#include
"componant.h"
#include
"componant.h"
...
@@ -14,6 +15,15 @@ public:
...
@@ -14,6 +15,15 @@ public:
std
::
complex
<
double
>
im
;
std
::
complex
<
double
>
im
;
double
omega
;
double
omega
;
};
};
struct
Range
{
double
start
;
double
end
;
size_t
count
;
Range
(
double
startI
,
double
endI
,
size_t
countI
)
:
start
(
startI
),
end
(
endI
),
count
(
countI
){}
Range
()
=
default
;
};
private
:
private
:
class
Paralell
:
public
Componant
class
Paralell
:
public
Componant
{
{
...
@@ -49,7 +59,11 @@ private:
...
@@ -49,7 +59,11 @@ private:
public
:
public
:
Model
(
const
std
::
string
&
str
);
Model
(
const
std
::
string
&
str
);
DataPoint
execute
(
double
omaga
);
DataPoint
execute
(
double
omaga
);
std
::
vector
<
DataPoint
>
sweep
(
const
Range
&
omega
);
bool
sweepParams
(
const
std
::
vector
<
Range
>&
componantRanges
,
const
Range
&
omega
,
std
::
function
<
void
(
std
::
vector
<
DataPoint
>&
)
>
dataCb
);
std
::
string
getModelStr
();
std
::
string
getModelStr
();
std
::
vector
<
Componant
*>
getFlatComponants
();
std
::
vector
<
Componant
*>
getFlatComponants
();
size_t
getFlatParametersCount
();
bool
setFlatParameters
(
const
std
::
vector
<
double
>&
parameters
);
static
char
getComponantChar
(
Componant
*
componant
);
static
char
getComponantChar
(
Componant
*
componant
);
};
};
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