Skip to content

Commit fbfac24

Browse files
committed
Start implementing rawfield and tests
1 parent a7a67ed commit fbfac24

File tree

8 files changed

+152
-46
lines changed

8 files changed

+152
-46
lines changed

Changes

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@ Revision history for Photonic
22

33
{{$NEXT}}
44

5+
- Program rawfield to obtain the unnormalized fields, assuming the
6+
external source is an external polarization. Currently, modified
7+
LE::NR2::Field, LE::S::Field, Roles::Field, WE::S::Field, and the tests.
58
- Add conversion routines between frequency, energy and wavelength
69
- Avoid duplicate arguments in cgtsv
710

lib/Photonic/LE/NR2/Field.pm

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,7 @@ evaluation of the field
8686
use namespace::autoclean;
8787
use PDL::Lite;
8888
use PDL::NiceSlice;
89+
use PDL::Constants qw(PI);
8990
use Photonic::LE::NR2::Haydock;
9091
use Photonic::Utils qw(cgtsv GtoR linearCombineIt);
9192
use Photonic::Types -all;
@@ -165,15 +166,16 @@ sub _build_field {
165166

166167
sub _build_rawfield {
167168
my $self=shift;
168-
my $Es = $self->_Fn*$self->u/$self->epsA;
169+
my $Es = -4*PI*$self->_Fn*$self->u/$self->epsA; # assume drive is external polarization
169170
my $states=$self->haydock->states->dummy(0);
170171
my $nrGnorm = $self->haydock->GNorm;
171172
#field is cartesian,nx,ny...
172173
my $field_G=linearCombineIt($Es, $nrGnorm*$states); #En ^G|psi_n>
173174
$field_G *= $self->filter->(*1) if $self->has_filter;
174175
# fourier transform vector field.
175176
my $field_R=GtoR($field_G, $self->haydock->ndims, 1);
176-
$field_R*=$self->haydock->B->nelem; #scale FFT
177+
my $b0=$self->haydock->bs->((0));
178+
$field_R*=$b0*$self->haydock->B->nelem; #scale FFT
177179
return $field_R; #result is cartesian, nx, ny,...
178180
}
179181

lib/Photonic/LE/S/Field.pm

Lines changed: 35 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,7 @@ evaluation of the field
7272
use namespace::autoclean;
7373
use PDL::Lite;
7474
use PDL::NiceSlice;
75+
use PDL::Constants qw(PI);
7576
use Photonic::LE::S::Haydock;
7677
use Photonic::Utils qw(cgtsv GtoR linearCombineIt);
7778
use Photonic::Types -all;
@@ -95,8 +96,10 @@ sub _build_epsL {
9596
$self->epsL; # danger of infinite recursion?
9697
}
9798

99+
has '_Fn'=>(is=>'lazy', isa=>PDLComplex, init_arg=>undef,
100+
documentation=>'Solution of (epsLL)F=(1,0...) in Haydock basis');
98101

99-
sub _build_field {
102+
sub _build__Fn{
100103
my $self=shift;
101104
my $as=$self->haydock->as;
102105
my $bs=$self->haydock->bs;
@@ -118,15 +121,22 @@ sub _build_field {
118121
# Add spinor normalization.
119122
my $epsL=1/$result->((0));
120123
$self->_epsL($epsL);
124+
return $result;
125+
}
126+
127+
sub _build_field {
128+
my $self=shift;
129+
my $result=$self->_Fn;
130+
my $epsL=$self->epsL;
121131
my $norm=sqrt(2)*$epsL;
122132
# Normalize result so macroscopic field is 1.
123133
my $Es = $result*$norm;
124134
#states are xy,nx,ny...
125-
my $stateit=$self->haydock->states->slice("*1");
135+
my $states=$self->haydock->states->slice("*1"); #dummy(0)
126136
#pmGnorm is xy,pm,nx,ny...
127137
my $pmGNorm=$self->haydock->pmGNorm;
128138
#field is xy,pm,nx,ny...
129-
my $field_G=linearCombineIt($Es, $pmGNorm*$stateit); #En ^G|psi_n>
139+
my $field_G=linearCombineIt($Es, $pmGNorm*$states); #En ^G|psi_n>
130140
#Choose +k
131141
my $Esp=$field_G->(:,(0)); #xy,nx,ny
132142
$Esp *= $self->filter->(*1) if $self->has_filter;
@@ -136,6 +146,28 @@ sub _build_field {
136146
return $field_R; #result is xy,nx,ny,...
137147
}
138148

149+
sub _build_rawfield {
150+
my $self=shift;
151+
my $Es=-4*PI*$self->_Fn; # Drive is external polarization
152+
# Multiply by sqrt(2)? Size of firststate is in b0? Included in $Es?
153+
#states are xy,nx,ny...
154+
my $states=$self->haydock->states->slice("*1"); #dummy(0)
155+
#pmGnorm is xy,pm,nx,ny...
156+
my $pmGNorm=$self->haydock->pmGNorm;
157+
#field is xy,pm,nx,ny...
158+
my $field_G=linearCombineIt($Es, $pmGNorm*$states); #En ^G|psi_n>
159+
#Choose +k
160+
my $Esp=$field_G->(:,(0)); #xy,nx,ny
161+
$Esp *= $self->filter->(*1) if $self->has_filter;
162+
#get cartesian out of the way, fourier transform, put cartesian.
163+
my $field_R=GtoR($Esp, $self->haydock->B->ndims, 1);
164+
$field_R*=$self->haydock->B->nelem; #scale FFT
165+
my $b0=$self->haydock->bs->slice((0)); # Should I multiply by $b0?
166+
$field_R*=$b0*sqrt(2); # sqrt 2 from spinor normalization
167+
return $field_R; #result is xy,nx,ny,...
168+
}
169+
170+
139171
__PACKAGE__->meta->make_immutable;
140172

141173
1;

lib/Photonic/Roles/Field.pm

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,7 @@ real space field normalized to macroscopic field in format cartesian, nx, ny,...
9696
=item * rawfield
9797
9898
real space field normalized to external source in format cartesian, nx, ny,... (lazy-built).
99+
Source is interpreted as external polarization.
99100
100101
=item * nh
101102

lib/Photonic/WE/S/Field.pm

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -61,15 +61,16 @@ Consumes L<Photonic::Roles::Field>
6161
use namespace::autoclean;
6262
use PDL::Lite;
6363
use PDL::NiceSlice;
64+
use PDL::Constants qw(PI);
6465
use Photonic::WE::S::Haydock;
6566
use Photonic::Utils qw(cgtsv GtoR linearCombineIt);
6667
use Photonic::Types -all;
6768
use Moo;
6869
use MooX::StrictConstructor;
6970

7071
# Temporary:
71-
has 'rawfield'=>(is=>'lazy', isa=>PDLComplex,
72-
documentation=>'Calculated real space field, unnormalized');
72+
#has 'rawfield'=>(is=>'lazy', isa=>PDLComplex,
73+
# documentation=>'Calculated real space field, unnormalized');
7374

7475
with 'Photonic::Roles::Field';
7576

@@ -105,14 +106,14 @@ sub _build_rawfield {
105106
my $Es=$self->haydock->applyMetric($field_G);
106107
#Comment as unnormalized
107108
#$Es*=$bs->((0))/$self->haydock->metric->epsilon;
108-
my $Esp=$Es(:,(0)); # choose +k spinor component.
109+
my $Esp=sqrt(2)*$Es(:,(0)); # choose +k spinor component.
109110
$Esp *= $self->filter->(*1) if $self->has_filter;
110111
##get cartesian out of the way, fourier transform, put cartesian.
111112
my $field_R=GtoR($Esp, $ndims, 1);
112113
$field_R*=$self->haydock->B->nelem; #scale??
113114
my $b0=$self->haydock->bs->slice('(0)'); # #First state normalization factor
114115
$field_R*=$b0; #scale??
115-
$field_R/=$self->haydock->B->nelem; #normalize FT?
116+
$field_R*= -4*PI; # Interpreting the first state as external polarization.
116117
return $field_R; #result is xy,nx,ny...
117118
}
118119

t/field-lenr2.t

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301 USA
3131
use strict;
3232
use warnings;
3333
use PDL;
34+
use PDL::Constants qw(PI);
3435
use Photonic::LE::NR2::Haydock;
3536
use Photonic::LE::NR2::Field;
3637
use Photonic::LE::NR2::SHP;
@@ -55,8 +56,8 @@ my $fla=1/$ea;
5556
my $flb=1/$eb;
5657
my $fproml=$fla*(1-$gl->f)+$flb*($gl->f);
5758
my $flex=1/$fproml;
58-
($fla, $flb)=map {$_/$fproml} ($fla, $flb);
59-
my $flx=($fla*(1-$B)+$flb*$B)->slice("*1");
59+
my ($flan, $flbn)=map {$_/$fproml} ($fla, $flb);
60+
my $flx=($flan*(1-$B)+$flbn*$B)->slice("*1");
6061
ok(Cagree($flv, $flx), "1D long field") or diag "got: $flv\nexpected: $flx";
6162
ok(Cagree($fle, $flex), "1D long response") or diag "got: $fle\nexpected: $flex";
6263
#View 2D from 1D superlattice.
@@ -71,6 +72,12 @@ ok(Cagree($ftv, $ftx), "1D trans field") or diag "got: $ftv\nexpected: $ftx";;
7172
my $fpromt=$ea*(1-$gt->f)+$eb*($gt->f);
7273
ok(Cagree($fte, $fpromt), "1D trans response") or diag "got: $fte\nexpected: $fpromt";
7374

75+
# check raw fields
76+
# Longitudinal
77+
my $flv_raw=$flo->rawfield;
78+
my $flx_raw=-4*PI*($fla*(1-$B)+$flb*$B)->dummy(0);
79+
ok(Cagree($flv_raw, $flx_raw), "1D long raw field");
80+
7481

7582
my ($dA, $dB) = (0, 1); # vacuum, then anything as is normalised to dB
7683
my $nrshp=Photonic::LE::NR2::SHP->new(

t/field-les.t

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -31,15 +31,19 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301 USA
3131
use strict;
3232
use warnings;
3333
use PDL;
34+
use PDL::Constants qw(PI);
3435
use Photonic::LE::S::Haydock;
3536
use Photonic::LE::S::Field;
36-
37-
use Test::More tests => 4;
37+
use Photonic::Geometry::FromB;
38+
use Photonic::Geometry::FromEpsilon;
39+
use Test::More tests => 6;
3840
use lib 't/lib';
3941
use TestUtils;
4042

41-
my $ea=1+2*i;
42-
my $eb=3+4*i;
43+
#my $ea=1+2*i;
44+
#my $eb=3+4*i;
45+
my $ea=1+0*i;
46+
my $eb=3+0*i;
4347
#Check field for simple 1D system
4448
my $B=zeroes(11)->xvals<5; #1D system
4549
my $epsilon=$ea*(1-$B)+$eb*$B;
@@ -48,6 +52,7 @@ my $haydock=Photonic::LE::S::Haydock->new(geometry=>$gl, nh=>10,
4852
keepStates=>1, epsilon=>$epsilon);
4953
my $flo=Photonic::LE::S::Field->new(haydock=>$haydock, nh=>10);
5054
my $flv=$flo->field;
55+
5156
my $fle=$flo->epsL;
5257
my $fla=1/$ea;
5358
my $flb=1/$eb;
@@ -57,7 +62,6 @@ my $flex=1/$fproml;
5762
my $flx=($fla*(1-$B)+$flb*$B)->slice("*1");
5863
ok(Cagree($flv, $flx), "1D long field") or diag "got: $flv\nexpected: $flx";
5964
ok(Cagree($fle, $flex), "1D long response") or diag "got: $fle\nexpected: $flex";
60-
6165
#View 2D from 1D superlattice.
6266
my $Bt=zeroes(1,11)->yvals<5; #2D flat system
6367
my $epsilont=$ea*(1-$Bt)+$eb*$Bt;
@@ -71,3 +75,12 @@ my $ftx=pdl([1, 0])->r2C;
7175
ok(Cagree($ftv, $ftx), "1D trans field") or diag "got: $ftv\nexpected: $ftx";;
7276
my $fpromt=$ea*(1-$gt->f)+$eb*($gt->f);
7377
ok(Cagree($fte, $fpromt), "1D trans response") or diag "got: $fte\nexpected: $fpromt";
78+
79+
# check raw fields
80+
my $flv_raw=$flo->rawfield;
81+
my $flx_raw=-4*PI*((1-$B)/$ea+$B/$eb)->dummy(0);
82+
ok(Cagree($flv_raw, $flx_raw), "1D long raw field");
83+
84+
my $ftv_raw=$fto->rawfield;
85+
my $ftx_raw=-(4*PI+0*i)/($ea*(1-$gt->f)+$eb*$gt->f)*pdl([1,0]);
86+
ok(Cagree($ftv_raw, $ftx_raw), "1D transverse raw field");

t/field-wes.t

Lines changed: 77 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -31,45 +31,92 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301 USA
3131
use strict;
3232
use warnings;
3333
use PDL;
34+
use PDL::Constants qw(PI);
3435
use Photonic::WE::S::Haydock;
3536
use Photonic::WE::S::Metric;
3637
use Photonic::WE::S::Field;
3738

38-
use Test::More tests => 2;
39+
use Test::More tests => 4;
3940
use lib 't/lib';
4041
use TestUtils;
4142

4243
my $ea=r2C(1);
4344
my $eb=3+4*i;
4445

45-
#Check field for simple 1D system. Longitudinal case
46-
my $B=zeroes(11)->xvals<5; #1D system
47-
my $epsilon=$ea*(1-$B)+$eb*$B;
48-
my $gl=Photonic::Geometry::FromB->new(B=>$B); #long
49-
my $ml=Photonic::WE::S::Metric->new(geometry=>$gl, epsilon=>pdl(1),
50-
wavenumber=>pdl(1), wavevector=>pdl([0.01]));
51-
my $haydock=Photonic::WE::S::Haydock->new(
52-
metric=>$ml, nh=>10, keepStates=>1, polarization=>pdl([1])->r2C,
53-
epsilon=>$epsilon);
54-
my $flo=Photonic::WE::S::Field->new(haydock=>$haydock, nh=>10);
55-
my $flv=$flo->field;
56-
my $fla=1/$ea;
57-
my $flb=1/$eb;
58-
my $fproml=$fla*(1-$gl->f)+$flb*($gl->f);
59-
($fla, $flb)=map {$_/$fproml} ($fla, $flb);
60-
my $flx=($fla*(1-$B)+$flb*$B)->transpose;
61-
ok(Cagree($flv, $flx), "1D long field");
46+
#Check field for simple 1D system.
47+
{
48+
#Longitudinal case
49+
my $B=zeroes(11)->xvals<5; #1D system
50+
my $epsilon=$ea*(1-$B)+$eb*$B;
51+
my $gl=Photonic::Geometry::FromB->new(B=>$B); #long
52+
my $ml=Photonic::WE::S::Metric->new(geometry=>$gl, epsilon=>pdl(1),
53+
wavenumber=>pdl(1), wavevector=>pdl([0.01]));
54+
my $haydock=Photonic::WE::S::Haydock->new(
55+
metric=>$ml, nh=>10, keepStates=>1, polarization=>pdl([1])->r2C,
56+
epsilon=>$epsilon);
57+
my $flo=Photonic::WE::S::Field->new(haydock=>$haydock, nh=>10);
58+
my $flv=$flo->field;
59+
my $fla=1/$ea;
60+
my $flb=1/$eb;
61+
my $fproml=$fla*(1-$gl->f)+$flb*($gl->f);
62+
($fla, $flb)=map {$_/$fproml} ($fla, $flb);
63+
my $flx=($fla*(1-$B)+$flb*$B)->transpose;
64+
ok(Cagree($flv, $flx), "1D long field");
65+
}
66+
67+
{
68+
#View 2D from 1D superlattice. Long wavelength transverse case
69+
my $Bt=zeroes(1,11)->yvals<5; #2D flat system
70+
my $epsilont=$ea*(1-$Bt)+$eb*$Bt;
71+
my $gt=Photonic::Geometry::FromB->new(B=>$Bt); #trans
72+
my $mt=Photonic::WE::S::Metric->new(geometry=>$gt, epsilon=>pdl(1),
73+
wavenumber=>pdl(0.001), wavevector=>pdl([0,0.0001]));
74+
my $nt=Photonic::WE::S::Haydock->new(
75+
metric=>$mt, nh=>10, keepStates=>1, polarization=>pdl([1,0])->r2C,
76+
epsilon=>$epsilont);
77+
my $fto=Photonic::WE::S::Field->new(haydock=>$nt, nh=>10);
78+
my $ftv=$fto->field;
79+
my $ftx=r2C(pdl [1, 0]);
80+
ok(Cagree($ftv, $ftx), "1D trans field");
81+
}
82+
83+
#Check rawfields for simple 1D system.
84+
# Longitudinal case
85+
{
86+
my $B=zeroes(11)->xvals<5; #1D system
87+
my $epsilon=$ea*(1-$B)+$eb*$B;
88+
my $gl=Photonic::Geometry::FromB->new(B=>$B); #long
89+
my $ml=Photonic::WE::S::Metric->new(geometry=>$gl, epsilon=>pdl(1),
90+
wavenumber=>pdl(1), wavevector=>pdl([0.01]));
91+
my $haydock=Photonic::WE::S::Haydock->new(
92+
metric=>$ml, nh=>10, keepStates=>1, polarization=>pdl([1])->r2C,
93+
epsilon=>$epsilon);
94+
my $flo=Photonic::WE::S::Field->new(haydock=>$haydock, nh=>10);
95+
my $flv=$flo->rawfield;
96+
my $fla=1/$ea;
97+
my $flb=1/$eb;
98+
#my $fproml=$fla*(1-$gl->f)+$flb*($gl->f);
99+
#($fla, $flb)=map {$_/$fproml} ($fla, $flb);
100+
my $flx=-4*PI*($fla*(1-$B)+$flb*$B)->transpose;
101+
ok(Cagree($flv, $flx), "1D long rawfield");
102+
}
62103

63104
#View 2D from 1D superlattice. Long wavelength transverse case
64-
my $Bt=zeroes(1,11)->yvals<5; #2D flat system
65-
my $epsilont=$ea*(1-$Bt)+$eb*$Bt;
66-
my $gt=Photonic::Geometry::FromB->new(B=>$Bt); #trans
67-
my $mt=Photonic::WE::S::Metric->new(geometry=>$gt, epsilon=>pdl(1),
68-
wavenumber=>pdl(0.001), wavevector=>pdl([0,0.0001]));
69-
my $nt=Photonic::WE::S::Haydock->new(
70-
metric=>$mt, nh=>10, keepStates=>1, polarization=>pdl([1,0])->r2C,
71-
epsilon=>$epsilont);
72-
my $fto=Photonic::WE::S::Field->new(haydock=>$nt, nh=>10);
73-
my $ftv=$fto->field;
74-
my $ftx=r2C(pdl [1, 0]);
75-
ok(Cagree($ftv, $ftx), "1D trans field");
105+
{
106+
my $Bt=zeroes(1,11)->yvals<5; #2D flat system
107+
my $epsilont=$ea*(1-$Bt)+$eb*$Bt;
108+
my $gt=Photonic::Geometry::FromB->new(B=>$Bt); #trans
109+
my $q=pdl(0.0001);
110+
my $k=pdl([0,0.0002]);
111+
my $mt=Photonic::WE::S::Metric->new(geometry=>$gt, epsilon=>pdl(1),
112+
wavenumber=>$q, wavevector=>$k);
113+
my $nt=Photonic::WE::S::Haydock->new(
114+
metric=>$mt, nh=>10, keepStates=>1, polarization=>pdl([1,0])->r2C,
115+
epsilon=>$epsilont);
116+
my $fto=Photonic::WE::S::Field->new(haydock=>$nt, nh=>10);
117+
my $ftv=$fto->rawfield;
118+
my $f=$gt->f;
119+
my $epsM=(1-$f)*$ea+$f*$eb;
120+
my $ftx=-4*PI*r2C(pdl [1, 0])->dummy(2,11)*$q**2/($epsM*$q**2-($k**2)->sumover);
121+
ok(Cagree($ftv, $ftx), "1D trans rawfield");
122+
}

0 commit comments

Comments
 (0)