Skip to content

Commit aad25fc

Browse files
committed
Simplify and document Utils. Remove unused SH calculation
1 parent c45fab3 commit aad25fc

File tree

15 files changed

+45
-131
lines changed

15 files changed

+45
-131
lines changed

Changes

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,12 @@
11
Revision history for Photonic
22

33
{{$NEXT}}
4-
4+
- Change cartesian_product from Utils to a simpler code. Fix
5+
documentation.
6+
- Remove wave_operator from Utils, as it is a plain inverse.
7+
- Remove unused/unproven alternative method for SH calculation
8+
- Remove unused and maybe mistaken third parameter from linearCombineIt
9+
- Remove the It as we no longer use iterators
510
- Add conversion routines between frequency, energy and wavelength
611
- Avoid duplicate arguments in cgtsv
712

lib/Photonic/LE/NR2/Field.pm

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ use namespace::autoclean;
8787
use PDL::Lite;
8888
use PDL::NiceSlice;
8989
use Photonic::LE::NR2::Haydock;
90-
use Photonic::Utils qw(cgtsv GtoR linearCombineIt);
90+
use Photonic::Utils qw(cgtsv GtoR linearCombine);
9191
use Photonic::Types -all;
9292
use Moo;
9393
use MooX::StrictConstructor;
@@ -150,7 +150,7 @@ sub _build_field {
150150
my $Es= $result*$Norm;
151151
my $nrGnorm = $self->haydock->GNorm;
152152
#field is cartesian,nx,ny...
153-
my $field_G=linearCombineIt($Es, $nrGnorm*$stateit); #En ^G|psi_n>
153+
my $field_G=linearCombine($Es, $nrGnorm*$stateit); #En ^G|psi_n>
154154
$field_G *= $self->filter->(*1) if $self->has_filter;
155155
#get cartesian out of the way, fourier transform, put cartesian.
156156
my $field_R=GtoR($field_G, $self->haydock->ndims, 1);

lib/Photonic/LE/NR2/SH.pm

Lines changed: 2 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -192,7 +192,7 @@ use namespace::autoclean;
192192
use PDL::Lite;
193193
use PDL::NiceSlice;
194194
use Photonic::LE::NR2::Haydock;
195-
use Photonic::Utils qw(RtoG GtoR HProd linearCombineIt any_complex cgtsv top_slice mvN);
195+
use Photonic::Utils qw(RtoG GtoR HProd linearCombine any_complex cgtsv top_slice mvN);
196196
use Photonic::Types -all;
197197
use PDL::Constants qw(PI);
198198
use Moo;
@@ -276,12 +276,6 @@ has 'P2'=>(is=>'lazy', isa=>PDLComplex,
276276
'SH self consistent total polarization vector
277277
field in real space');
278278

279-
has 'P2LMCalt'=>(is=>'lazy', isa=>PDLComplex,
280-
init_arg=>undef,
281-
documentation=>
282-
'SH self consistent total macroscopic polarization
283-
in real space. Alternative');
284-
285279
has 'filterflag'=>(is=>'rw',
286280
documentation=>'Filter results in reciprocal space');
287281

@@ -445,7 +439,7 @@ sub _build_selfConsistentL_G {
445439
$self->filterflag(0);
446440
my $PLn=$self->selfConsistentL_n;
447441
my $stateit=$self->HP->states;
448-
my $result=linearCombineIt($PLn, $stateit);
442+
my $result=linearCombine($PLn, $stateit);
449443
$self->filterflag($filterflag);
450444
$result=$self->_filter($result,0) if $filterflag;
451445
return $result;
@@ -470,48 +464,6 @@ sub _build_P2 {
470464
-4*PI*($alpha2*$density)->(*1)*$PL+$Pext;
471465
}
472466

473-
sub _build_P2LMCalt {
474-
my $self=shift;
475-
my $PexL_G=$self->externalL_G; #external long 2w polarization
476-
my $PexM=$self->external_G->(:,(0),(0)); #macroscopic external.
477-
my $haydock=$self->haydock;
478-
my $geom=$haydock->geometry;
479-
my $ndims=$geom->ndims;
480-
my $nelem=$geom->npoints;
481-
my $k=$geom->Direction0;
482-
my $epsA2=$self->epsA2;
483-
my $u2=$self->u2;
484-
my $B=$geom->B;
485-
my $as=$haydock->as;
486-
my $bs=$haydock->bs;
487-
my $nh=$self->nh; #desired number of Haydock terms
488-
#don't go beyond available values.
489-
$nh=$haydock->iteration if $nh>=$haydock->iteration;
490-
# calculate using lapack for tridiag system
491-
# solve \epsilon^LL \vec E^L=|0>.
492-
my $diag=$self->u2->conj - $as->(0:$nh-1);
493-
# rotate complex zero from first to last element.
494-
my $subdiag=-$bs->(0:$nh-1)->rotate(-1)->r2C;
495-
my $supradiag=$subdiag->rotate(-1);
496-
my $rhs=PDL->zeroes($nh);
497-
$rhs->((0)).=1;
498-
$rhs=$rhs->r2C;
499-
my $phi_n = cgtsv($subdiag, $diag, $supradiag, $rhs);
500-
my $states=$haydock->states;
501-
my $phi_G=linearCombineIt($phi_n, $states);
502-
my $Pphi=$k*(1-$epsA2)*$u2/$epsA2*HProd($phi_G, $PexL_G, $self->ndims);
503-
my $beta_G=RtoG($B*GtoR($haydock->firstState,$ndims,0), $ndims,0);
504-
my $betaV_G=$beta_G->(*1)*$geom->GNorm;
505-
$states=$haydock->states->dummy(0);
506-
my $betaV_n=HProd($betaV_G,$states, $self->ndims, 1);
507-
my $psi = cgtsv($subdiag, $diag, $supradiag, $betaV_n->mv(0,-1)); # nx ny .... cartesian nh - threading
508-
$states=$haydock->states;
509-
my $psi_G=linearCombineIt($psi, $states->dummy(-1), 1);
510-
my $Ppsi = HProd($psi_G, $PexL_G, $self->ndims);
511-
my $P2M=$Pphi+$Ppsi+$PexM*$nelem; # Unnormalize Pex !!
512-
return $P2M->(,*1,*1);
513-
}
514-
515467
sub _build_u1 {
516468
my $self=shift;
517469
$self->_nrf1->u;

lib/Photonic/LE/NR2/SHChiTensor.pm

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -235,7 +235,6 @@ with 'Photonic::Roles::KeepStates', 'Photonic::Roles::UseMask';
235235
my %KIND2METHOD = (
236236
f => 'P2',
237237
l => 'selfConsistentVecL',
238-
a => 'P2LMCalt',
239238
d => 'dipolar',
240239
q => 'quadrupolar',
241240
e => 'external',

lib/Photonic/LE/S/Field.pm

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ use namespace::autoclean;
7373
use PDL::Lite;
7474
use PDL::NiceSlice;
7575
use Photonic::LE::S::Haydock;
76-
use Photonic::Utils qw(cgtsv GtoR linearCombineIt);
76+
use Photonic::Utils qw(cgtsv GtoR linearCombine);
7777
use Photonic::Types -all;
7878
use Moo;
7979
use MooX::StrictConstructor;
@@ -126,7 +126,7 @@ sub _build_field {
126126
#pmGnorm is xy,pm,nx,ny...
127127
my $pmGNorm=$self->haydock->pmGNorm;
128128
#field is xy,pm,nx,ny...
129-
my $field_G=linearCombineIt($Es, $pmGNorm*$stateit); #En ^G|psi_n>
129+
my $field_G=linearCombine($Es, $pmGNorm*$stateit); #En ^G|psi_n>
130130
#Choose +k
131131
my $Esp=$field_G->(:,(0)); #xy,nx,ny
132132
$Esp *= $self->filter->(*1) if $self->has_filter;

lib/Photonic/LE/ST/Field.pm

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ use namespace::autoclean;
7373
use PDL::Lite;
7474
use PDL::NiceSlice;
7575
use Photonic::LE::S::Haydock;
76-
use Photonic::Utils qw(cgtsv GtoR linearCombineIt);
76+
use Photonic::Utils qw(cgtsv GtoR linearCombine);
7777
use Photonic::Types -all;
7878
use Moo;
7979
use MooX::StrictConstructor;
@@ -126,7 +126,7 @@ sub _build_field {
126126
#pmGnorm is xy,pm,nx,ny...
127127
my $pmGNorm=$self->haydock->pmGNorm;
128128
#field is xy,pm,nx,ny...
129-
my $field_G=linearCombineIt($Es, $pmGNorm*$stateit); #En ^G|psi_n>
129+
my $field_G=linearCombine($Es, $pmGNorm*$stateit); #En ^G|psi_n>
130130
#Choose +k
131131
my $Esp=$field_G->(:,(0)); #xy,nx,ny
132132
$Esp *= $self->filter->(*1) if $self->has_filter;

lib/Photonic/Utils.pm

Lines changed: 19 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -41,10 +41,10 @@ require Exporter;
4141
our @ISA=qw(Exporter);
4242
our @EXPORT_OK=qw(vectors2Dlist tile RtoG GtoR
4343
HProd MHProd EProd VSProd SProd
44-
corner_rotate mvN top_slice linearCombineIt lentzCF any_complex tensor
44+
corner_rotate mvN top_slice linearCombine lentzCF any_complex tensor
4545
make_haydock make_greenp
4646
cartesian_product dummyN triangle_coords incarnate_as
47-
wave_operator apply_longitudinal_projection make_dyads
47+
apply_longitudinal_projection make_dyads
4848
cgtsv lu_decomp lu_solve convert_units upper_sqrt
4949
);
5050
use PDL::LiteF;
@@ -58,12 +58,16 @@ require List::Util;
5858

5959

6060
sub top_slice :lvalue {
61+
# slices an ndarray for a given last dimension
6162
my ($pdl, $index) = @_;
6263
my $slice_arg = join ',', (map ':', 1..($pdl->ndims-1)), $index;
6364
$pdl->slice($slice_arg);
6465
}
6566

6667
sub dummyN {
68+
# Inserts a block of $how_many dummy dimensions of size $dim_size
69+
# (default 1) starting at dimension $where (default 0) into an
70+
# ndarray $pdl.
6771
my ($pdl, $how_many, $where, $dim_size) = @_;
6872
return $pdl if $how_many <= 0;
6973
my $ndims=$pdl->ndims;
@@ -76,35 +80,21 @@ sub dummyN {
7680
$pdl->slice($slice_arg);
7781
}
7882

79-
sub linearCombineIt { #complex linear combination of states
80-
my ($coefficients, $states, $thread_dims)=@_;
81-
$thread_dims //= 0;
83+
sub linearCombine { # linear combination of states
84+
my ($coefficients, $states)=@_;
8285
$coefficients=dummyN($coefficients, $states->ndims-$coefficients->ndims);
83-
($coefficients*$states)->mv(-$thread_dims-1,0)->sumover;
86+
($coefficients*$states)->mv(-1,0)->sumover;
8487
}
8588

86-
sub any_complex {
89+
sub any_complex { # test if an ndarray is any kind of complex
8790
grep ref $_ && ($_->isnull || !$_->type->real), @_;
8891
}
8992

90-
sub wave_operator {
91-
my ($green, $nd) = @_;
92-
lu_solve([lu_decomp($green)], r2C(PDL::MatrixOps::identity($nd)));
93-
}
94-
95-
sub cartesian_product {
96-
my ($s1, $s2) = @_;
97-
my $ndims_target = List::Util::max(2, map $_->ndims, $s1, $s2);
98-
$_ = dummyN($_, $ndims_target-$_->ndims) for grep $_->ndims < $ndims_target, $s1, $s2;
99-
my ($nd1, $nd2) = map $_->ndims, $s1, $s2;
100-
my @dims = $s1->dims;
101-
$dims[-2] += $s2->dim(-2); # X1+X2
102-
$dims[-1] *= $s2->dim(-1); # m*n
103-
my $res = zeroes(@dims);
104-
my ($res_mv, $s1_mv, $s2_mv) = map mvN($_, 0, $nd1-3, -1), $res, $s1, $s2; # now work with first 2 dims
105-
$res->slice('0:'.($s1_mv->dim(-2)-1)) .= $s1_mv->dummy(2, $s2_mv->dim(-1))->clump(1,2);
106-
$res->slice($s1_mv->dim(-2).':-1') .= $s2_mv->dummy(1, $s1_mv->dim(-1))->clump(1,2);
107-
$res;
93+
sub cartesian_product { # all pairs of elements of $s1 and $s2
94+
my ($s1, $s2) = @_;
95+
my ($a1, $a2)=map {$_->ndims==1?$_->dummy(0):$_} ($s1, $s2);
96+
my $r=append($a1->dummy(2), $a2->dummy(1));
97+
$r->reshape($r->dim(0), $r->dim(1)*$r->dim(2));
10898
}
10999

110100
sub triangle_coords {
@@ -507,7 +497,7 @@ ndarray.
507497
Adds C<$how_many> (no-op if <= 0) dummy dimensions of size C<$dim_size>
508498
(default 1) in the C<$which_dim> (default 0) position.
509499
510-
=item * $r=linearCombineIt($c, $it, $thread_dims)
500+
=item * $r=linearCombine($c, $it, $thread_dims)
511501
512502
Complex linear combination of states. $c is a 'complex'
513503
ndarray and $it is an ndarray of states from a L<Photonic::Roles::Haydock>.
@@ -609,21 +599,15 @@ True if any of the args are a complex PDL.
609599
610600
=item * cartesian_product
611601
612-
Given two ndarrays a(Z,x1,m), b(Z,x2,n), return c(Z,x1+x2,m*n), with
613-
each row from C<b> appended to all rows from C<a>. C<Z> can be empty
614-
but must be compatible; the shorter one will be "dummied up" from the
615-
zero end as necessary.
602+
Given two ndarrays a(x1,m), b(x2,n), return c(x1+x2,m*n), with
603+
each row from C<b> appended to all rows from C<a>. If one of them is
604+
1D, a dummy first index is added.
616605
617606
=item * tensor
618607
619608
Given a complex PDL, an LU decomposition array-ref as returned by
620609
L</lu_decomp>, and the size of the tensor, returns the tensor.
621610
622-
=item * wave_operator
623-
624-
Given a Green tensor and number of dimension in the geometry, returns
625-
a wave operator.
626-
627611
=item * make_haydock
628612
629613
=item * make_greenp

lib/Photonic/WE/R2/Field.pm

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ use namespace::autoclean;
8181
use PDL::Lite;
8282
use PDL::NiceSlice;
8383
use Photonic::WE::R2::Haydock;
84-
use Photonic::Utils qw(cgtsv GtoR linearCombineIt);
84+
use Photonic::Utils qw(cgtsv GtoR linearCombine);
8585
use Photonic::Types -all;
8686
use Moo;
8787
use MooX::StrictConstructor;
@@ -137,7 +137,7 @@ sub _build_field {
137137
#field is xy,nx,ny...
138138
my $ndims=$self->haydock->B->ndims; # num. of dims of space
139139
#field is cartesian, nx, ny...
140-
my $field_G=linearCombineIt($giEs, $stateit); #En ^G|psi_n>
140+
my $field_G=linearCombine($giEs, $stateit); #En ^G|psi_n>
141141
my $Es=$self->haydock->applyMetric($field_G);
142142
my $e_0=1/($Es->slice(":" . ",(0)" x $ndims)
143143
*$self->haydock->polarization->conj)->sumover;

lib/Photonic/WE/R2/Green.pm

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,7 @@ use PDL::NiceSlice;
139139
use Photonic::WE::R2::Haydock;
140140
use Photonic::WE::R2::GreenP;
141141
use Photonic::Types -all;
142-
use Photonic::Utils qw(tensor make_haydock make_greenp wave_operator triangle_coords);
142+
use Photonic::Utils qw(tensor make_haydock make_greenp triangle_coords);
143143

144144
use List::Util qw(all any);
145145
use Moo;
@@ -242,7 +242,7 @@ sub _build_cGreenP {
242242

243243
sub _build_waveOperator {
244244
my $self=shift;
245-
wave_operator($self->greenTensor, $self->geometry->ndims);
245+
$self->greenTensor->inv;
246246
}
247247

248248
sub _build_epsilonTensor {

lib/Photonic/WE/S/Field.pm

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ use namespace::autoclean;
6262
use PDL::Lite;
6363
use PDL::NiceSlice;
6464
use Photonic::WE::S::Haydock;
65-
use Photonic::Utils qw(cgtsv GtoR linearCombineIt);
65+
use Photonic::Utils qw(cgtsv GtoR linearCombine);
6666
use Photonic::Types -all;
6767
use Moo;
6868
use MooX::StrictConstructor;
@@ -96,7 +96,7 @@ sub _build_field {
9696
my $stateit=$self->haydock->states;
9797
my $ndims=$self->haydock->B->ndims; # num. of dims of space
9898
#field is xy,pm,nx,ny...
99-
my $field_G=linearCombineIt($giEs, $stateit); #En ^G|psi_n>
99+
my $field_G=linearCombine($giEs, $stateit); #En ^G|psi_n>
100100
my $Es=$self->haydock->applyMetric($field_G);
101101
#Comment as normalization below makes it useless
102102
#$Es*=$bs->((0))/$self->haydock->metric->epsilon;

0 commit comments

Comments
 (0)