Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update mad_mkthin.cpp #1173

Closed
Closed
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
78 changes: 41 additions & 37 deletions src/mad_mkthin.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
2019 : New elements from element definition, all attributes enabled
2020 : Tapering, wire compensation
2021 : Permanent misalignment
2023 : k0 in dipedge

*/

Expand Down Expand Up @@ -156,8 +157,8 @@ class SeqElList // sequence with elements considered for slicing
double hybrid_at_shift(const int slices, const int slice_no) const;
double at_shift(const int slices, const int slice_no,const std::string local_slice_style) const; // return at relative shifts from centre of unsliced magnet
void kn_ks_from_thick_elem(const element* thick_elem,command_parameter* kn_pars[4],command_parameter* ks_pars[4]) const; // read k0-k3, k0s-k3s in thick_elem and put them in kn_pars, ks_pars
// void add_ktap(command_parameter* k_param,const element* thick_elem);
// void add_ktap_i(const int i,command_parameter* k_param,const std::string k_name,const std::string ktap_name,const element* thick_elem);
void add_ktap(command_parameter* k_param,const element* thick_elem);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

k1tap, k2tap are obsolete. All elements store tapering information on ktap, therefore add_ktap should be changed

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

True, add_ktap and add_ktap_i are not needed any more and should be removed from the code. Done on my fork.

Checks done to make sure that ktap is automatically transmitted when defined, which is the case for rbend, sbend, quadrupole, sextupole, octupole and multipole. Also ok for thick slicing.

void add_ktap_i(const int i,command_parameter* k_param,const std::string k_name,const std::string ktap_name,const element* thick_elem);
command_parameter* make_k_list(const std::string parnam,command_parameter* k_pars[4]) const; // from k values 0-3 to expr lists
element* new_marker_element(const std::string el_name, const element* el_inp);
element* create_wire_element(const element* thick_elem, int slice_no);
Expand Down Expand Up @@ -1697,30 +1698,29 @@ void SeqElList::kn_ks_from_thick_elem(const element* thick_elem,command_paramete
for(unsigned int i=0;i<ks_name.size();++i) ks_pars[i] = (p=return_param_recurse(ks_name[i].c_str() ,thick_elem)) ? clone_command_parameter(p) : nullptr;
}

// void SeqElList::add_ktap(command_parameter* k_param,const element* thick_elem)
// {
// if(!k_param) return;
// std::string k_param_name=k_param->name;
// std::string k_name,ktap_name;
// for(unsigned int i=1;i<3;++i)
// {
// // k_name="k"+std::to_string(i); // k1, k2
// // if(k_param_name=="ksl") k_name=k_name+"s"; // or k1s, k2s
// // ktap_name=k_name+"tap"; // corresponding tap attribute
// ktap_name="ktap";
// add_ktap_i(i,k_param,k_name,ktap_name,thick_elem);
// }
// }

// void SeqElList::add_ktap_i(const int i,command_parameter* k_param,const std::string k_name,const std::string ktap_name,const element* thick_elem)
// {
// if( command_parameter *p = return_param_recurse(k_name.c_str(), thick_elem) ) // has k1n, or k1s, k2n, k2s
// if( command_parameter *p_tap = return_param_recurse(ktap_name.c_str(), thick_elem) )// has corresponding tap
// {
// if(p->expr) k_param->expr_list->list[i] = compound_expr(p->expr, p->double_value, "+", p_tap->expr, p_tap->double_value, 0);
// else k_param->expr_list->list[i] = compound_expr(expr_from_value(p->double_value), p->double_value, "+", p_tap->expr, p_tap->double_value, 0);
// }
// }
void SeqElList::add_ktap(command_parameter* k_param,const element* thick_elem)
{
if(!k_param) return;
std::string k_param_name=k_param->name;
std::string k_name,ktap_name;
for(unsigned int i=1;i<3;++i)
{
k_name="k"+std::to_string(i); // k1, k2
if(k_param_name=="ksl") k_name=k_name+"s"; // or k1s, k2s
ktap_name=k_name+"tap"; // corresponding tap attribute
add_ktap_i(i,k_param,k_name,ktap_name,thick_elem);
}
}

void SeqElList::add_ktap_i(const int i,command_parameter* k_param,const std::string k_name,const std::string ktap_name,const element* thick_elem)
{
if( command_parameter *p = return_param_recurse(k_name.c_str(), thick_elem) ) // has k1n, or k1s, k2n, k2s
if( command_parameter *p_tap = return_param_recurse(ktap_name.c_str(), thick_elem) )// has corresponding tap
{
if(p->expr) k_param->expr_list->list[i] = compound_expr(p->expr, p->double_value, "+", p_tap->expr, p_tap->double_value, 0);
else k_param->expr_list->list[i] = compound_expr(expr_from_value(p->double_value), p->double_value, "+", p_tap->expr, p_tap->double_value, 0);
}
}

command_parameter* SeqElList::make_k_list(const std::string parnam,command_parameter* k_pars[4]) const
{
Expand Down Expand Up @@ -1780,17 +1780,21 @@ element* SeqElList::create_bend_dipedge_element(const element* thick_elem,const
std::string dipedge_cmd_name="dipedge";
if(Entry) dipedge_cmd_name+="_l_"; else dipedge_cmd_name+="_r_";
dipedge_cmd_name+="cmd";

const double eps=1.e-15;

expression* l_par_expr=my_get_param_expression(thick_elem, "l"); // with this l_par_expr should not be NULL
expression* angle_par_expr = my_get_param_expression(thick_elem,"angle");
command_parameter* hparam=new_command_parameter("h",k_double);
hparam->expr=compound_expr(angle_par_expr,0.,"/",l_par_expr,0,1); // this also updates the value

command* dipedge_cmd = new_cmdptr( find_element("dipedge", base_type_list) );
const command_parameter* k0_par = return_param_recurse("k0",thick_elem);
if(k0_par)
{ // k0 is there, used it
SetParameter_in_cmd(dipedge_cmd,k0_par,"h",1); // copy k0 from thick bend as dipedge h
}
else // otherwise h = angle / l
{
command_parameter* hparam=new_command_parameter("h",k_double);
expression* l_par_expr=my_get_param_expression(thick_elem, "l"); // with this l_par_expr should not be NULL
expression* angle_par_expr = my_get_param_expression(thick_elem,"angle");
hparam->expr=compound_expr(angle_par_expr,0.,"/",l_par_expr,0,1); // this also updates the value
SetParameter_in_cmd(dipedge_cmd,hparam,"h",1);
}

SetParameter_in_cmd(dipedge_cmd,hparam,"h",1);
if(dipedge_h1_h2_fl)
{
if(Entry) SetParameter_in_cmd(dipedge_cmd, return_param_recurse("h1",thick_elem), "he",1); // at entry, copy h1 from thick bend as dipedge he
Expand Down Expand Up @@ -2100,8 +2104,8 @@ element* SeqElList::create_thin_slices(const element* thick_elem, int slice_no)
knl_param=make_k_list("knl",kn_pars); // make new knl 0,1,2,3 expr_list based on kn_pars from thick
kns_param=make_k_list("ksl",ks_pars); // make new ksl 0,1,2,3 expr_list based on ks_pars from thick

// add_ktap(knl_param,thick_elem);
// add_ktap(kns_param,thick_elem);
add_ktap(knl_param,thick_elem);
add_ktap(kns_param,thick_elem);

// multiply the k by length and divide by slice
knl_param = scale_and_slice(knl_param,length_param,nslices,mult_with_length);
Expand Down