Skip to content

Commit

Permalink
WIP make ngspice steps more efficient
Browse files Browse the repository at this point in the history
  • Loading branch information
Alberto-o committed Jun 25, 2024
1 parent 3616923 commit 6a076ba
Show file tree
Hide file tree
Showing 6 changed files with 66 additions and 2 deletions.
5 changes: 4 additions & 1 deletion .gitattributes
Original file line number Diff line number Diff line change
@@ -1 +1,4 @@
precompiled_libraries/** linguist-vendored
precompiled_libraries/** linguist-vendored
*.sh -crlf
*.ac -crlf
*.am -crlf
11 changes: 11 additions & 0 deletions src_mtln/circuit.F90
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ module circuit_mod
procedure, private :: loadNetlist
procedure :: readInput
procedure :: setStopTimes
procedure :: setModStopTimes
procedure :: getNodeVoltage
procedure :: getNodeCurrent
procedure :: updateNodes
Expand Down Expand Up @@ -148,6 +149,7 @@ subroutine run(this)
end subroutine



subroutine setStopTimes(this, finalTime, dt)
class(circuit_t) :: this
real, intent(in) :: finalTime, dt
Expand All @@ -162,6 +164,15 @@ subroutine setStopTimes(this, finalTime, dt)
end do
end subroutine

subroutine setModStopTimes(this, dt)
class(circuit_t) :: this
real, intent(in) :: dt
character(20) :: charTime
real :: time
write(charTime, *) dt
call command('stop when time mod '//charTime // c_null_char)
end subroutine

subroutine resume(this)
class(circuit_t) :: this
call command('resume ' // c_null_char)
Expand Down
2 changes: 2 additions & 0 deletions src_mtln/ngspice_interface.c
Original file line number Diff line number Diff line change
Expand Up @@ -74,13 +74,15 @@ preceded by token stdout, same with stderr.*/
int
ng_getchar(char* outputreturn, int ident, void* userdata)
{
printf("%s\n", outputreturn);
/* setting a flag if an error message occurred */
return 0;
}

int
ng_getstat(char* outputreturn, int ident, void* userdata)
{
printf("%s\n", outputreturn);
return 0;
}

Expand Down
2 changes: 2 additions & 0 deletions test/mtln/mtln_tests.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ extern "C" int test_spice_multiple();
extern "C" int test_spice_current_source();
extern "C" int test_spice_dc();
extern "C" int test_spice_read_message();
extern "C" int test_spice_stop_mod_times();
extern "C" int test_preprocess_conductors_before_cable();
extern "C" int test_preprocess_conductors_in_level();
extern "C" int test_preprocess_zt_conductor_ranges_2();
Expand Down Expand Up @@ -70,6 +71,7 @@ TEST(mtln, spice_multiple) { EXPECT_EQ(0, test_spice_multiple()); }
TEST(mtln, spice_current_source) { EXPECT_EQ(0, test_spice_current_source()); }
TEST(mtln, spice_dc) { EXPECT_EQ(0, test_spice_dc()); }
TEST(mtln, spice_read_message) { EXPECT_EQ(0, test_spice_read_message()); }
TEST(mtln, spice_mod_times) { EXPECT_EQ(0, test_spice_stop_mod_times()); }

// TEST(mtln, system_coaxial_line_paul_8_6_square) { EXPECT_EQ(0, test_coaxial_line_paul_8_6_square()); }
// TEST(mtln, system_coaxial_line_paul_8_6_triangle) { EXPECT_EQ(0, test_coaxial_line_paul_8_6_triangle()); }
Expand Down
46 changes: 46 additions & 0 deletions test/mtln/test_spice.F90
Original file line number Diff line number Diff line change
Expand Up @@ -248,3 +248,49 @@ integer function test_spice_multiple() bind(C) result(error_cnt)


end function

integer function test_spice_stop_mod_times() bind(C) result(error_cnt)
use circuit_mod
use mtln_testingTools_mod
implicit none

type(circuit_t) :: circuit
character(len=*, kind=c_char), parameter :: netlist= PATH_TO_TEST_DATA//c_char_'netlists/netlist_tran.cir'
real :: finalTime
real :: result(3)
integer :: i
type(string_t), dimension(4) :: names
names(1) = string_t("in", 2)
names(2) = string_t("int", 3)
names(3) = string_t("out", 3)
names(4) = string_t("time", 4)

result = [5.0,0.092995181699999999,0.053166680000000001]

circuit%time = 0.0
circuit%dt = 50e-6
finalTime = 200e-6

error_cnt = 0
call circuit%init(names=names,netlist=netlist)
call circuit%setModStopTimes(circuit%dt)
do while (circuit%time < finalTime)
call circuit%step()
circuit%time = circuit%time + circuit%dt
if (checkNear(circuit%getTime(), circuit%time, 0.01) .eqv. .false. ) then
error_cnt = error_cnt + 1
end if
end do
if (checkNear(circuit%getNodeVoltage("in"), result(1), 0.01) .eqv. .false. ) then
error_cnt = error_cnt + 1
end if
if (checkNear(circuit%getNodeVoltage("int"), result(2), 0.01) .eqv. .false. ) then
error_cnt = error_cnt + 1
end if
if (checkNear(circuit%getNodeVoltage("out"), result(3), 0.01) .eqv. .false. ) then
error_cnt = error_cnt + 1
end if

! call circuit%quit()

end function
2 changes: 1 addition & 1 deletion testData/netlists/netlist_tran.cir
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,6 @@ C1 int 0 1u
C2 out 0 100n
* .option trtol=1 klu method=gear reltol=1m
* .option sparse
.tran 50u 200u 0 10u
.tran 50u 200u 0 0.1u
.save v(in) v(int) v(out)
.end

0 comments on commit 6a076ba

Please sign in to comment.